1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
8 pdftitle={SDCC Compiler User Guide},
9 pdfauthor={SDCC development team},
10 pdfsubject={installation, user manual},
11 pdfkeywords={8032, 8051, ansi, c, compiler, CPU, DS390,
12 embedded, GPL, HC08, manual, mcs51, PIC, Z80},
14 linkcolor=blue] {hyperref}
18 \emergencystretch=30pt
23 \inputencoding default
26 \paperfontsize default
28 \papersize letterpaper
33 \use_numerical_citations 0
34 \paperorientation portrait
41 \paragraph_separation indent
43 \quotes_language swedish
51 Please note: double dashed longoptions (e.g.
52 --version) are written this way: -
66 three consecutive dashes simply result in a long resp.
70 Architecture specific stuff (like memory models, code examples) should maybe
74 into seperate sections/chapters/appendices (it is hard to document PIC or
78 a 8051 centered document) - for now simply add.
81 SDCC Compiler User Guide
95 The above strings enclosed in $ are automatically updated by cvs
99 \begin_inset LatexCommand \tableofcontents{}
132 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
137 designed for 8 bit Microprocessors.
138 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
140 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
144 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
145 Zilog Z80 based MCUs.
146 It can be retargetted for other microprocessors, support for Microchip
147 PIC, Atmel AVR is under development.
148 The entire source code for the compiler is distributed under GPL.
150 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
155 \begin_inset LatexCommand \index{aslink}
159 , an open source retargettable assembler & linker.
160 SDCC has extensive language extensions suitable for utilizing various microcont
161 rollers and underlying hardware effectively.
166 In addition to the MCU specific optimizations SDCC also does a host of standard
170 global sub expression elimination,
173 loop optimizations (loop invariant, strength reduction of induction variables
177 constant folding & propagation,
183 dead code elimination
193 For the back-end SDCC uses a global register allocation scheme which should
194 be well suited for other 8 bit MCUs.
199 The peep hole optimizer uses a rule based substitution mechanism which is
205 Supported data-types are:
208 char (8 bits, 1 byte),
211 short and int (16 bits, 2 bytes),
214 long (32 bit, 4 bytes)
221 The compiler also allows
223 inline assembler code
225 to be embedded anywhere in a function.
226 In addition, routines developed in assembly can also be called.
230 SDCC also provides an option (-
240 -cyclomatic) to report the relative complexity of a function.
241 These functions can then be further optimized, or hand coded in assembly
247 SDCC also comes with a companion source level debugger SDCDB, the debugger
248 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
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
355 This version has numerous bug fixes compared with the previous version.
356 But we also introduced some incompatibilities with older versions.
357 Not just for the fun of it, but to make the compiler more stable, efficient
359 \begin_inset LatexCommand \index{ANSI-compliance}
364 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
368 for ANSI-Compliance).
374 short is now equivalent to int (16 bits), it used to be equivalent to char
375 (8 bits) which is not ANSI compliant.
378 the default directory for gcc-builds where include, library and documentation
379 files are stored is now in /usr/local/share.
382 char type parameters to vararg functions are casted to int unless explicitly
399 will push a as an int and as a char resp.
412 -regextend has been removed.
425 -noregparms has been removed.
438 -stack-after-data has been removed.
441 bit and sbit types now consistently behave like the C99 _Bool type with
442 respect to type conversion.
443 The most common incompatibility resulting from this change is related to
444 bit toggling idioms, e.g.:
454 b = ~b; /* equivalent to b=1 instead of toggling b */
458 b = !b; /* toggles b */
462 In previous versions, both forms would have toggled the bit.
467 <pending: more incompatibilities?>
473 What do you need before you start installation of SDCC? A computer, and
475 The preferred method of installation is to compile SDCC from source using
477 For Windows some pre-compiled binary distributions are available for your
479 You should have some experience with command line tools and compiler use.
485 The SDCC home page at
486 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
490 is a great place to find distribution sets.
491 You can also find links to the user mailing lists that offer help or discuss
492 SDCC with other SDCC users.
493 Web links to other SDCC related sites can also be found here.
494 This document can be found in the DOC directory of the source package as
496 A pdf version of this document is available at
497 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
502 Some of the other tools (simulator and assembler) included with SDCC contain
503 their own documentation and can be found in the source distribution.
504 If you want the latest unreleased software, the complete source package
505 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
508 Wishes for the future
511 There are (and always will be) some things that could be done.
512 Here are some I can think of:
519 char KernelFunction3(char p) at 0x340;
527 \begin_inset LatexCommand \index{code banking (not supported)}
537 If you can think of some more, please see the section
538 \begin_inset LatexCommand \ref{sub:Requesting-Features}
542 about filing feature requests
543 \begin_inset LatexCommand \index{Requesting features}
548 \begin_inset LatexCommand \index{Feature request}
558 \begin_inset LatexCommand \index{Installation}
565 For most users it is sufficient to skip to either section
566 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
571 \begin_inset LatexCommand \ref{sub:Windows-Install}
576 More detailled instructions follow below.
580 \begin_inset LatexCommand \index{Options SDCC configuration}
587 The install paths, search paths and other options are defined when running
589 The defaults can be overridden by:
591 \labelwidthstring 00.00.0000
603 -prefix see table below
605 \labelwidthstring 00.00.0000
617 -exec_prefix see table below
619 \labelwidthstring 00.00.0000
631 -bindir see table below
633 \labelwidthstring 00.00.0000
645 -datadir see table below
647 \labelwidthstring 00.00.0000
649 docdir environment variable, see table below
651 \labelwidthstring 00.00.0000
653 include_dir_suffix environment variable, see table below
655 \labelwidthstring 00.00.0000
657 lib_dir_suffix environment variable, see table below
659 \labelwidthstring 00.00.0000
661 sdccconf_h_dir_separator environment variable, either / or
666 This character will only be used in sdccconf.h; don't forget it's a C-header,
667 therefore a double-backslash is needed there.
669 \labelwidthstring 00.00.0000
681 -disable-mcs51-port Excludes the Intel mcs51 port
683 \labelwidthstring 00.00.0000
695 -disable-gbz80-port Excludes the Gameboy gbz80 port
697 \labelwidthstring 00.00.0000
709 -disable-z80-port Excludes the z80 port
711 \labelwidthstring 00.00.0000
723 -disable-avr-port Excludes the AVR port
725 \labelwidthstring 00.00.0000
737 -disable-ds390-port Excludes the DS390 port
739 \labelwidthstring 00.00.0000
751 -disable-hc08-port Excludes the HC08 port
753 \labelwidthstring 00.00.0000
765 -disable-pic-port Excludes the PIC port
767 \labelwidthstring 00.00.0000
779 -disable-xa51-port Excludes the XA51 port
781 \labelwidthstring 00.00.0000
793 -disable-ucsim Disables configuring and building of ucsim
795 \labelwidthstring 00.00.0000
807 -disable-device-lib-build Disables automatically building device libraries
809 \labelwidthstring 00.00.0000
821 -disable-packihx Disables building packihx
823 \labelwidthstring 00.00.0000
835 -enable-libgc Use the Bohem memory allocator.
836 Lower runtime footprint.
839 Furthermore the environment variables CC, CFLAGS, ...
840 the tools and their arguments can be influenced.
841 Please see `configure -
851 -help` and the man/info pages of `configure` for details.
855 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
856 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
857 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
858 At the moment it's not possible to change the default settings (it was
859 simply never required).
863 These configure options are compiled into the binaries, and can only be
864 changed by rerunning 'configure' and recompiling SDCC.
865 The configure options are written in
869 to distinguish them from run time environment variables (see section search
875 \begin_inset Quotes sld
879 \begin_inset Quotes srd
882 are used by the SDCC team to build the official Win32 binaries.
883 The SDCC team uses Mingw32 to build the official Windows binaries, because
890 a gcc compiler and last but not least
893 the binaries can be built by cross compiling on Sourceforge's compile farm.
896 See the examples, how to pass the Win32 settings to 'configure'.
897 The other Win32 builds using Borland, VC or whatever don't use 'configure',
898 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
910 <lyxtabular version="3" rows="8" columns="3">
912 <column alignment="block" valignment="top" leftline="true" width="0in">
913 <column alignment="block" valignment="top" leftline="true" width="0in">
914 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
915 <row topline="true" bottomline="true">
916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
952 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1003 <row topline="true">
1004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1014 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1041 <row topline="true">
1042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1064 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1075 <row topline="true">
1076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1113 <row topline="true">
1114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1141 <row topline="true" bottomline="true">
1142 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1160 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1178 'configure' also computes relative paths.
1179 This is needed for full relocatability of a binary package and to complete
1180 search paths (see section search paths below):
1186 \begin_inset Tabular
1187 <lyxtabular version="3" rows="4" columns="3">
1189 <column alignment="block" valignment="top" leftline="true" width="0in">
1190 <column alignment="block" valignment="top" leftline="true" width="0in">
1191 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1192 <row topline="true" bottomline="true">
1193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1218 <row topline="true" bottomline="true">
1219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1246 <row bottomline="true">
1247 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1265 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1274 <row bottomline="true">
1275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1326 \begin_inset Quotes srd
1330 \begin_inset Quotes srd
1344 \begin_inset Quotes srd
1348 \begin_inset Quotes srd
1376 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1385 \begin_inset Quotes srd
1388 i586-mingw32msvc-gcc
1389 \begin_inset Quotes srd
1393 \begin_inset Quotes srd
1396 i586-mingw32msvc-g++
1397 \begin_inset Quotes srd
1405 \begin_inset Quotes srd
1408 i586-mingw32msvc-ranlib
1409 \begin_inset Quotes srd
1417 \begin_inset Quotes srd
1420 i586-mingw32msvc-strip
1421 \begin_inset Quotes srd
1439 \begin_inset Quotes srd
1443 \begin_inset Quotes srd
1461 \begin_inset Quotes srd
1465 \begin_inset Quotes srd
1473 \begin_inset Quotes srd
1477 \begin_inset Quotes srd
1485 \begin_inset Quotes srd
1489 \begin_inset Quotes srd
1497 \begin_inset Quotes srd
1501 \begin_inset Quotes srd
1508 sdccconf_h_dir_separator=
1509 \begin_inset Quotes srd
1521 \begin_inset Quotes srd
1538 -disable-device-lib-build
1566 -host=i586-mingw32msvc -
1576 -build=unknown-unknown-linux-gnu
1580 \begin_inset Quotes sld
1584 \begin_inset Quotes srd
1587 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1596 \begin_inset Quotes srd
1600 \begin_inset Quotes srd
1608 \begin_inset Quotes srd
1612 \begin_inset Quotes srd
1630 \begin_inset Quotes srd
1634 \begin_inset Quotes srd
1652 \begin_inset Quotes srd
1656 \begin_inset Quotes srd
1664 \begin_inset Quotes srd
1668 \begin_inset Quotes srd
1676 \begin_inset Quotes srd
1680 \begin_inset Quotes srd
1688 \begin_inset Quotes srd
1692 \begin_inset Quotes srd
1699 sdccconf_h_dir_separator=
1700 \begin_inset Quotes srd
1712 \begin_inset Quotes srd
1732 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1743 -C' turns on caching, which gives a little bit extra speed.
1744 However if options are changed, it can be necessary to delete the config.cache
1749 \begin_inset LatexCommand \label{sub:Install-paths}
1754 \begin_inset LatexCommand \index{Install paths}
1760 \added_space_top medskip \align center
1762 \begin_inset Tabular
1763 <lyxtabular version="3" rows="5" columns="4">
1765 <column alignment="center" valignment="top" leftline="true" width="0">
1766 <column alignment="center" valignment="top" leftline="true" width="0">
1767 <column alignment="center" valignment="top" leftline="true" width="0">
1768 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1769 <row topline="true" bottomline="true">
1770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1811 <row topline="true">
1812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1851 <row topline="true">
1852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1867 $DATADIR/ $INCLUDE_DIR_SUFFIX
1870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1875 /usr/local/share/sdcc/include
1878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1891 <row topline="true">
1892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1907 $DATADIR/$LIB_DIR_SUFFIX
1910 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1915 /usr/local/share/sdcc/lib
1918 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1931 <row topline="true" bottomline="true">
1932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1955 /usr/local/share/sdcc/doc
1958 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1980 *compiler, preprocessor, assembler, and linker
1986 is auto-appended by the compiler, e.g.
1987 small, large, z80, ds390 etc
1990 The install paths can still be changed during `make install` with e.g.:
1993 make install prefix=$(HOME)/local/sdcc
1996 Of course this doesn't change the search paths compiled into the binaries.
2000 Moreover the install path can be changed by defining DESTDIR
2001 \begin_inset LatexCommand \index{DESTDIR}
2008 make install DESTDIR=$(HOME)/sdcc.rpm/
2011 Please note that DESTDIR must have a trailing slash!
2015 \begin_inset LatexCommand \label{sub:Search-Paths}
2020 \begin_inset LatexCommand \index{Search path}
2027 Some search paths or parts of them are determined by configure variables
2032 , see section above).
2033 Further search paths are determined by environment variables during runtime.
2036 The paths searched when running the compiler are as follows (the first catch
2042 Binary files (preprocessor, assembler and linker)
2048 \begin_inset Tabular
2049 <lyxtabular version="3" rows="4" columns="3">
2051 <column alignment="block" valignment="top" leftline="true" width="0in">
2052 <column alignment="block" valignment="top" leftline="true" width="0in">
2053 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2054 <row topline="true" bottomline="true">
2055 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2063 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2071 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2080 <row topline="true">
2081 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2099 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2110 <row topline="true">
2111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2116 Path of argv[0] (if available)
2119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2127 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2136 <row topline="true" bottomline="true">
2137 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2178 \begin_inset Tabular
2179 <lyxtabular version="3" rows="6" columns="3">
2181 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2182 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2183 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2184 <row topline="true" bottomline="true">
2185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2210 <row topline="true">
2211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2247 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2266 <row topline="true">
2267 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2283 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2292 <row topline="true">
2293 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2307 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2330 <row topline="true">
2331 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2349 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2412 <row topline="true" bottomline="true">
2413 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2429 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2434 /usr/local/share/sdcc/
2439 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2467 -nostdinc disables the last two search paths.
2477 With the exception of
2478 \begin_inset Quotes sld
2492 \begin_inset Quotes srd
2499 is auto-appended by the compiler (e.g.
2500 small, large, z80, ds390 etc.).
2507 \begin_inset Tabular
2508 <lyxtabular version="3" rows="6" columns="3">
2510 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2511 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2512 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2513 <row topline="true" bottomline="true">
2514 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2522 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2530 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2539 <row topline="true">
2540 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2576 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2595 <row topline="true">
2596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2608 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2620 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2635 <row topline="true">
2636 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2647 $LIB_DIR_SUFFIX/<model>
2650 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2664 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2681 <row topline="true">
2682 <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">
2753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2809 <row topline="true" bottomline="true">
2810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2819 $LIB_DIR_SUFFIX/<model>
2822 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2827 /usr/local/share/sdcc/
2834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2852 Don't delete any of the stray spaces in the table above without checking
2853 the HTML output (last line)!
2869 -nostdlib disables the last two search paths.
2873 \begin_inset LatexCommand \index{Building SDCC}
2880 Building SDCC on Linux
2881 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2890 Download the source package
2892 either from the SDCC CVS repository or from the nightly snapshots
2894 , it will be named something like sdcc
2905 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2914 Bring up a command line terminal, such as xterm.
2919 Unpack the file using a command like:
2922 "tar -xvzf sdcc.src.tar.gz
2927 , this will create a sub-directory called sdcc with all of the sources.
2930 Change directory into the main SDCC directory, for example type:
2947 This configures the package for compilation on your system.
2963 All of the source packages will compile, this can take a while.
2979 This copies the binary executables, the include files, the libraries and
2980 the documentation to the install directories.
2981 Proceed with section
2982 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
2989 Building SDCC on OSX 2.x
2992 Follow the instruction for Linux.
2996 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2997 )) fails to compile SDCC.
2998 Fortunately there's also gcc 2.9.x installed, which works fine.
2999 This compiler can be selected by running 'configure' with:
3002 ./configure CC=gcc2 CXX=g++2
3005 Cross compiling SDCC on Linux for Windows
3008 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3009 See section 'Configure Options'.
3012 Building SDCC on Windows
3015 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3017 They use Unix-sockets, which are not available on Win32.
3020 Building SDCC using Cygwin and Mingw32
3023 For building and installing a Cygwin executable follow the instructions
3029 \begin_inset Quotes sld
3033 \begin_inset Quotes srd
3036 Win32-binary can be built, which will not need the Cygwin-DLL.
3037 For the necessary 'configure' options see section 'configure options' or
3038 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3042 In order to install Cygwin on Windows download setup.exe from
3043 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3049 \begin_inset Quotes sld
3052 default text file type
3053 \begin_inset Quotes srd
3057 \begin_inset Quotes sld
3061 \begin_inset Quotes srd
3064 and download/install at least the following packages.
3065 Some packages are selected by default, others will be automatically selected
3066 because of dependencies with the manually selected packages.
3067 Never deselect these packages!
3076 gcc ; version 3.x is fine, no need to use the old 2.9x
3079 binutils ; selected with gcc
3085 rxvt ; a nice console, which makes life much easier under windoze (see below)
3088 man ; not really needed for building SDCC, but you'll miss it sooner or
3092 less ; not really needed for building SDCC, but you'll miss it sooner or
3096 cvs ; only if you use CVS access
3099 If you want to develop something you'll need:
3102 python ; for the regression tests
3105 gdb ; the gnu debugger, together with the nice GUI
3106 \begin_inset Quotes sld
3110 \begin_inset Quotes srd
3116 openssh ; to access the CF or commit changes
3119 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3120 use autoconf-stable!
3123 rxvt is a nice console with history.
3124 Replace in your cygwin.bat the line
3143 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3146 -bg black -fg white -geometry 100x65 -e bash -
3159 Text selected with the mouse is automatically copied to the clipboard, pasting
3160 works with shift-insert.
3164 The other good tip is to make sure you have no //c/-style paths anywhere,
3165 use /cygdrive/c/ instead.
3166 Using // invokes a network lookup which is very slow.
3168 \begin_inset Quotes sld
3172 \begin_inset Quotes srd
3175 is too long, you can change it with e.g.
3181 SDCC sources use the unix line ending LF.
3182 Life is much easier, if you store the source tree on a drive which is mounted
3184 And use an editor which can handle LF-only line endings.
3185 Make sure not to commit files with windows line endings.
3186 The tabulator spacing
3187 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3191 used in the project is 8.
3192 Although a tabulator spacing of 8 is a sensible choice for programmers
3193 (it's a power of 2 and allows to display 8/16 bit signed variables without
3194 loosing columns) the plan is to move towards using only spaces in the source.
3197 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3202 Download the source package
3204 either from the SDCC CVS repository or from the
3205 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3211 , it will be named something like sdcc
3218 SDCC is distributed with all the projects, workspaces, and files you need
3219 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3220 doesn't build under MSVC).
3221 The workspace name is 'sdcc.dsw'.
3222 Please note that as it is now, all the executables are created in a folder
3226 Once built you need to copy the executables from sdcc
3230 bin before running SDCC.
3235 WARNING: Visual studio is very picky with line terminations; it expects
3236 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3237 If you are getting a message such as "This makefile was not generated by
3238 Developer Studio etc.
3240 \begin_inset Quotes srd
3243 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3244 need to convert the Unix style line endings to DOS style line endings.
3245 To do so you can use the
3246 \begin_inset Quotes sld
3250 \begin_inset Quotes srd
3253 utility freely available on the internet.
3254 Doug Hawkins reported in the sdcc-user list that this works:
3262 SDCC> unix2dos sdcc.dsw
3268 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3272 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3273 flex.exe, and gawk.exe.
3274 One good place to get them is
3275 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3283 Download the file UnxUtils
3284 \begin_inset LatexCommand \index{UnxUtils}
3289 Now you have to install the utilities and setup MSVC so it can locate the
3291 Here there are two alternatives (choose one!):
3298 a) Extract UnxUtils.zip to your C:
3300 hard disk PRESERVING the original paths, otherwise bison won't work.
3301 (If you are using WinZip make certain that 'Use folder names' is selected)
3305 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3306 in 'Show directories for:' select 'Executable files', and in the directories
3307 window add a new path: 'C:
3317 (As a side effect, you get a bunch of Unix utilities that could be useful,
3318 such as diff and patch.)
3325 This one avoids extracting a bunch of files you may not use, but requires
3330 a) Create a directory were to put the tools needed, or use a directory already
3338 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3339 to such directory WITHOUT preserving the original paths.
3340 (If you are using WinZip make certain that 'Use folder names' is not selected)
3344 c) Rename bison.exe to '_bison.exe'.
3348 d) Create a batch file 'bison.bat' in 'C:
3352 ' and add these lines:
3372 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3376 Steps 'c' and 'd' are needed because bison requires by default that the
3377 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3378 '/usr/local/share/' I think.
3379 So it is necessary to tell bison where those files are located if they
3380 are not in such directory.
3381 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3385 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3386 in 'Show directories for:' select 'Executable files', and in the directories
3387 window add a new path: 'c:
3390 Note that you can use any other path instead of 'c:
3392 util', even the path where the Visual C++ tools are, probably: 'C:
3396 Microsoft Visual Studio
3401 So you don't have to execute step 'e' :)
3405 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3406 the executables from sdcc
3410 bin, and you can compile using SDCC.
3413 Building SDCC Using Borland
3416 From the sdcc directory, run the command "make -f Makefile.bcc".
3417 This should regenerate all the .exe files in the bin directory except for
3418 sdcdb.exe (which currently doesn't build under Borland C++).
3421 If you modify any source files and need to rebuild, be aware that the dependenci
3422 es may not be correctly calculated.
3423 The safest option is to delete all .obj files and run the build again.
3424 From a Cygwin BASH prompt, this can easily be done with the command (be
3425 sure you are in the sdcc directory):
3435 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3437 ) -print -exec rm {}
3446 or on Windows NT/2000/XP from the command prompt with the command:
3453 del /s *.obj *.lib *.rul
3456 from the sdcc directory.
3459 Windows Install Using a Binary Package
3460 \begin_inset LatexCommand \label{sub:Windows-Install}
3467 Download the binary package from
3468 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3472 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3473 This should unpack to a group of sub-directories.
3474 An example directory structure after unpacking the mingw32 package is:
3479 bin for the executables, c:
3487 lib for the include and libraries.
3490 Adjust your environment variable PATH to include the location of the bin
3491 directory or start sdcc using the full path.
3494 Building the Documentation
3497 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3498 as changing into the doc directory and typing
3502 \begin_inset Quotes srd
3506 \begin_inset Quotes srd
3513 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3516 \begin_inset LatexCommand \url{www.lyx.org}
3520 as editor this is straightforward.
3521 Prebuilt documentation in html and pdf format is available from
3522 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3529 Reading the Documentation
3532 Currently reading the document in pdf format is recommended, as for unknown
3533 reason the hyperlinks are working there whereas in the html version they
3540 If you should know why please drop us a note
3544 You'll find the pdf version at
3545 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
3551 This documentation is in some aspects different from a commercial documentation:
3555 It tries to document SDCC for several processor architectures in one document
3556 (commercially these probably would be separate documents/products).
3558 \begin_inset LatexCommand \index{Status of documentation}
3562 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3564 Z80, PIC14, PIC16 and HC08.
3567 There are many references pointing away from this documentation.
3568 Don't let this distract you.
3570 was a reference like
3571 \begin_inset LatexCommand \url{www.opencores.org}
3575 together with a statement
3576 \begin_inset Quotes sld
3579 some processors which are targetted by SDCC can be implemented in a
3596 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3601 \begin_inset Quotes srd
3604 we expect you to have a quick look there and come back.
3605 If you read this you are on the right track.
3608 Some sections attribute more space to problems, restrictions and warnings
3609 than to the solution.
3612 The installation section and the section about the debugger is intimidating.
3615 There are still lots of typos and there are more different writing styles
3619 Testing the SDCC Compiler
3620 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3627 The first thing you should do after installing your SDCC compiler is to
3643 \begin_inset LatexCommand \index{version}
3650 at the prompt, and the program should run and tell you the version.
3651 If it doesn't run, or gives a message about not finding sdcc program, then
3652 you need to check over your installation.
3653 Make sure that the sdcc bin directory is in your executable search path
3654 defined by the PATH environment setting (
3659 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3666 Install trouble-shooting for suggestions
3669 Make sure that the sdcc program is in the bin folder, if not perhaps something
3670 did not install correctly.
3678 is commonly installed as described in section
3679 \begin_inset Quotes sld
3682 Install and search paths
3683 \begin_inset Quotes srd
3692 Make sure the compiler works on a very simple example.
3693 Type in the following test.c program using your favorite
3719 Compile this using the following command:
3728 If all goes well, the compiler will generate a test.asm and test.rel file.
3729 Congratulations, you've just compiled your first program with SDCC.
3730 We used the -c option to tell SDCC not to link the generated code, just
3731 to keep things simple for this step.
3739 The next step is to try it with the linker.
3749 If all goes well the compiler will link with the libraries and produce
3750 a test.ihx output file.
3755 (no test.ihx, and the linker generates warnings), then the problem is most
3764 usr/local/share/sdcc/lib directory
3771 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3778 Install trouble-shooting for suggestions).
3786 The final test is to ensure
3794 header files and libraries.
3795 Edit test.c and change it to the following:
3812 strcpy(str1, "testing");
3819 Compile this by typing
3826 This should generate a test.ihx output file, and it should give no warnings
3827 such as not finding the string.h file.
3828 If it cannot find the string.h file, then the problem is that
3832 cannot find the /usr/local/share/sdcc/include directory
3839 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3846 Install trouble-shooting section for suggestions).
3864 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3868 to find exactly where SDCC is looking for the include and lib files.
3871 Install Trouble-shooting
3872 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3877 \begin_inset LatexCommand \index{Install trouble-shooting}
3884 If SDCC does not build correctly
3887 A thing to try is starting from scratch by unpacking the .tgz source package
3888 again in an empty directory.
3896 ./configure 2>&1 | tee configure.log
3910 make 2>&1 | tee make.log
3917 If anything goes wrong, you can review the log files to locate the problem.
3918 Or a relevant part of this can be attached to an email that could be helpful
3919 when requesting help from the mailing list.
3923 \begin_inset Quotes sld
3927 \begin_inset Quotes srd
3934 \begin_inset Quotes sld
3938 \begin_inset Quotes srd
3941 command is a script that analyzes your system and performs some configuration
3942 to ensure the source package compiles on your system.
3943 It will take a few minutes to run, and will compile a few tests to determine
3944 what compiler features are installed.
3948 \begin_inset Quotes sld
3952 \begin_inset Quotes srd
3958 This runs the GNU make tool, which automatically compiles all the source
3959 packages into the final installed binary executables.
3963 \begin_inset Quotes sld
3967 \begin_inset Quotes erd
3973 This will install the compiler, other executables libraries and include
3974 files into the appropriate directories.
3976 \begin_inset LatexCommand \ref{sub:Install-paths}
3982 \begin_inset LatexCommand \ref{sub:Search-Paths}
3987 about install and search paths.
3989 On most systems you will need super-user privileges to do this.
3995 SDCC is not just a compiler, but a collection of tools by various developers.
3996 These include linkers, assemblers, simulators and other components.
3997 Here is a summary of some of the components.
3998 Note that the included simulator and assembler have separate documentation
3999 which you can find in the source package in their respective directories.
4000 As SDCC grows to include support for other processors, other packages from
4001 various developers are included and may have their own sets of documentation.
4005 You might want to look at the files which are installed in <installdir>.
4006 At the time of this writing, we find the following programs for gcc-builds:
4010 In <installdir>/bin:
4013 sdcc - The compiler.
4016 sdcpp - The C preprocessor.
4019 asx8051 - The assembler for 8051 type processors.
4026 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4029 aslink -The linker for 8051 type processors.
4036 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4039 s51 - The ucSim 8051 simulator.
4042 sdcdb - The source debugger.
4045 packihx - A tool to pack (compress) Intel hex files.
4048 In <installdir>/share/sdcc/include
4054 In <installdir>/share/sdcc/lib
4057 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4061 In <installdir>/share/sdcc/doc
4067 As development for other processors proceeds, this list will expand to include
4068 executables to support processors like AVR, PIC, etc.
4074 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4075 the assembler and linkage editor.
4078 sdcpp - The C-Preprocessor
4082 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4086 is a modified version of the GNU preprocessor.
4087 The C preprocessor is used to pull in #include sources, process #ifdef
4088 statements, #defines and so on.
4099 - The Assemblers and Linkage Editors
4102 This is retargettable assembler & linkage editor, it was developed by Alan
4104 John Hartman created the version for 8051, and I (Sandeep) have made some
4105 enhancements and bug fixes for it to work properly with SDCC.
4112 \begin_inset LatexCommand \index{s51}
4116 is a freeware, opensource simulator developed by Daniel Drotos (
4117 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
4122 The simulator is built as part of the build process.
4123 For more information visit Daniel's web site at:
4124 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4129 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4133 sdcdb - Source Level Debugger
4137 \begin_inset LatexCommand \index{sdcdb (debugger)}
4141 is the companion source level debugger.
4142 More about sdcdb in section
4143 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4148 The current version of the debugger uses Daniel's Simulator S51
4149 \begin_inset LatexCommand \index{s51}
4153 , but can be easily changed to use other simulators.
4163 Single Source File Projects
4166 For single source file 8051 projects the process is very simple.
4167 Compile your programs with the following command
4170 "sdcc sourcefile.c".
4174 This will compile, assemble and link your source file.
4175 Output files are as follows:
4179 \begin_inset LatexCommand \index{<file>.asm}
4184 \begin_inset LatexCommand \index{Assembler source}
4188 file created by the compiler
4192 \begin_inset LatexCommand \index{<file>.lst}
4197 \begin_inset LatexCommand \index{Assembler listing}
4201 file created by the Assembler
4205 \begin_inset LatexCommand \index{<file>.rst}
4210 \begin_inset LatexCommand \index{Assembler listing}
4214 file updated with linkedit information, created by linkage editor
4218 \begin_inset LatexCommand \index{<file>.sym}
4223 \begin_inset LatexCommand \index{Symbol listing}
4227 for the sourcefile, created by the assembler
4231 \begin_inset LatexCommand \index{<file>.rel}
4236 \begin_inset LatexCommand \index{<file>.o}
4241 \begin_inset LatexCommand \index{Object file}
4245 created by the assembler, input to Linkage editor
4249 \begin_inset LatexCommand \index{<file>.map}
4254 \begin_inset LatexCommand \index{Memory map}
4258 for the load module, created by the Linker
4262 \begin_inset LatexCommand \index{<file>.mem}
4266 - A file with a summary of the memory usage
4270 \begin_inset LatexCommand \index{<file>.ihx}
4274 - The load module in Intel hex format
4275 \begin_inset LatexCommand \index{Intel hex format}
4279 (you can select the Motorola S19 format
4280 \begin_inset LatexCommand \index{Motorola S19 format}
4295 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4300 If you need another format you might want to use
4307 \begin_inset LatexCommand \index{objdump (tool)}
4318 \begin_inset LatexCommand \index{srecord (tool)}
4323 Both formats are documented in the documentation of srecord
4324 \begin_inset LatexCommand \index{srecord (tool)}
4332 \begin_inset LatexCommand \index{<file>.adb}
4336 - An intermediate file containing debug information needed to create the
4348 \begin_inset LatexCommand \index{-\/-debug}
4356 \begin_inset LatexCommand \index{<file>.cdb}
4360 - An optional file (with -
4370 -debug) containing debug information.
4371 The format is documented in cdbfileformat.pdf.
4376 \begin_inset LatexCommand \index{<file> (no extension)}
4380 An optional AOMF or AOMF51
4381 \begin_inset LatexCommand \index{AOMF, AOMF51}
4385 file containing debug information (generated with option -
4412 ormat is commonly used by third party tools (debuggers
4413 \begin_inset LatexCommand \index{Debugger}
4417 , simulators, emulators)
4421 \begin_inset LatexCommand \index{<file>.dump*}
4425 - Dump file to debug the compiler it self (generated with option -
4435 -dumpall) (see section
4436 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4442 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4448 \begin_inset Quotes sld
4451 Anatomy of the compiler
4452 \begin_inset Quotes srd
4458 Projects with Multiple Source Files
4461 SDCC can compile only ONE file at a time.
4462 Let us for example assume that you have a project containing the following
4467 foo1.c (contains some functions)
4469 foo2.c (contains some more functions)
4471 foomain.c (contains more functions and the function main)
4479 The first two files will need to be compiled separately with the commands:
4511 Then compile the source file containing the
4516 \begin_inset LatexCommand \index{Linker}
4520 the files together with the following command:
4528 foomain.c\SpecialChar ~
4529 foo1.rel\SpecialChar ~
4534 \begin_inset LatexCommand \index{<file>.rel}
4546 can be separately compiled as well:
4557 sdcc foomain.rel foo1.rel foo2.rel
4564 The file containing the
4579 file specified in the command line, since the linkage editor processes
4580 file in the order they are presented to it.
4581 The linker is invoked from SDCC using a script file with extension .lnk
4582 \begin_inset LatexCommand \index{<file>.lnk}
4587 You can view this file to troubleshoot linking problems such as those arising
4588 from missing libraries.
4591 Projects with Additional Libraries
4592 \begin_inset LatexCommand \index{Libraries}
4599 Some reusable routines may be compiled into a library, see the documentation
4600 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4604 \begin_inset LatexCommand \index{<file>.lib}
4611 Libraries created in this manner can be included in the command line.
4612 Make sure you include the -L <library-path> option to tell the linker where
4613 to look for these files if they are not in the current directory.
4614 Here is an example, assuming you have the source file
4626 (if that is not the same as your current project):
4633 sdcc foomain.c foolib.lib -L mylib
4644 must be an absolute path name.
4648 The most efficient way to use libraries is to keep separate modules in separate
4650 The lib file now should name all the modules.rel
4651 \begin_inset LatexCommand \index{<file>.rel}
4656 For an example see the standard library file
4660 in the directory <installdir>/share/lib/small.
4663 Using sdcclib to Create and Manage Libraries
4664 \begin_inset LatexCommand \index{sdcclib}
4671 Alternatively, instead of having a .rel file for each entry on the library
4672 file as described in the preceding section, sdcclib can be used to embed
4673 all the modules belonging to such library in the library file itself.
4674 This results in a larger library file, but it greatly reduces the number
4675 of disk files accessed by the linker.
4676 Additionally, the packed library file contains an index of all include
4677 modules and symbols that significantly speeds up the linking process.
4678 To display a list of options supported by sdcclib type:
4687 \begin_inset LatexCommand \index{sdcclib}
4698 To create a new library file, start by compiling all the required modules.
4736 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4738 The next step is to add the .rel files to the library file:
4746 sdcclib libint.lib _divsint.rel
4749 \begin_inset LatexCommand \index{sdcclib}
4759 sdcclib libint.lib _divuint.rel
4765 sdcclib libint.lib _modsint.rel
4771 sdcclib libint.lib _moduint.rel
4777 sdcclib libint.lib _mulint.rel
4784 If the file already exists in the library, it will be replaced.
4785 To see what modules and symbols are included in the library, options -s
4786 and -m are available.
4794 sdcclib -s libint.lib
4797 \begin_inset LatexCommand \index{sdcclib}
4907 If the source files are compiled using -
4918 \begin_inset LatexCommand \index{-\/-debug}
4922 , the corresponding debug information file .adb will be include in the library
4924 The library files created with sdcclib are plain text files, so they can
4925 be viewed with a text editor.
4926 It is not recomended to modify a library file created with sdcclib using
4927 a text editor, as there are file indexes numbers located accross the file
4928 used by the linker to quickly locate the required module to link.
4929 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
4930 it can be safely deleted, since all the information required for linking
4931 is embedded in the library file itself.
4932 Library files created using sdcclib are used as described in the preceding
4936 Command Line Options
4937 \begin_inset LatexCommand \index{Command Line Options}
4944 Processor Selection Options
4945 \begin_inset LatexCommand \index{Options processor selection}
4950 \begin_inset LatexCommand \index{Processor selection options}
4956 \labelwidthstring 00.00.0000
4961 \begin_inset LatexCommand \index{-mmcs51}
4967 Generate code for the Intel MCS51
4968 \begin_inset LatexCommand \index{MCS51}
4972 family of processors.
4973 This is the default processor target.
4975 \labelwidthstring 00.00.0000
4980 \begin_inset LatexCommand \index{-mds390}
4986 Generate code for the Dallas DS80C390
4987 \begin_inset LatexCommand \index{DS80C390}
4993 \labelwidthstring 00.00.0000
4998 \begin_inset LatexCommand \index{-mds400}
5004 Generate code for the Dallas DS80C400
5005 \begin_inset LatexCommand \index{DS80C400}
5011 \labelwidthstring 00.00.0000
5016 \begin_inset LatexCommand \index{-mhc08}
5022 Generate code for the Freescale/Motorola HC08
5023 \begin_inset LatexCommand \index{HC08}
5027 family of processors.
5029 \labelwidthstring 00.00.0000
5034 \begin_inset LatexCommand \index{-mz80}
5040 Generate code for the Zilog Z80
5041 \begin_inset LatexCommand \index{Z80}
5045 family of processors.
5047 \labelwidthstring 00.00.0000
5052 \begin_inset LatexCommand \index{-mgbz80}
5058 Generate code for the GameBoy Z80
5059 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5063 processor (Not actively maintained).
5065 \labelwidthstring 00.00.0000
5070 \begin_inset LatexCommand \index{-mavr}
5076 Generate code for the Atmel AVR
5077 \begin_inset LatexCommand \index{AVR}
5081 processor (In development, not complete).
5082 AVR users should probably have a look at avr-gcc
5083 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
5088 \begin_inset LatexCommand \url{http://winavr.sourceforge.net}
5095 I think it is fair to direct users there for now.
5096 Open source is also about avoiding unnecessary work .
5097 But I didn't find the 'official' link.
5099 \labelwidthstring 00.00.0000
5104 \begin_inset LatexCommand \index{-mpic14}
5110 Generate code for the Microchip PIC 14
5111 \begin_inset LatexCommand \index{PIC14}
5115 -bit processors (p16f84 and variants.
5116 In development, not complete).
5119 p16f627 p16f628 p16f84 p16f873 p16f877?
5121 \labelwidthstring 00.00.0000
5126 \begin_inset LatexCommand \index{-mpic16}
5132 Generate code for the Microchip PIC 16
5133 \begin_inset LatexCommand \index{PIC16}
5137 -bit processors (p18f452 and variants.
5138 In development, not complete).
5140 \labelwidthstring 00.00.0000
5146 Generate code for the Toshiba TLCS-900H
5147 \begin_inset LatexCommand \index{TLCS-900H}
5151 processor (Not maintained, not complete).
5153 \labelwidthstring 00.00.0000
5158 \begin_inset LatexCommand \index{-mxa51}
5164 Generate code for the Phillips XA51
5165 \begin_inset LatexCommand \index{XA51}
5169 processor (Not maintained, not complete).
5172 Preprocessor Options
5173 \begin_inset LatexCommand \index{Options preprocessor}
5178 \begin_inset LatexCommand \index{Preprocessor options}
5183 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5189 \labelwidthstring 00.00.0000
5194 \begin_inset LatexCommand \index{-I<path>}
5200 The additional location where the pre processor will look for <..h> or
5201 \begin_inset Quotes eld
5205 \begin_inset Quotes erd
5210 \labelwidthstring 00.00.0000
5215 \begin_inset LatexCommand \index{-D<macro[=value]>}
5221 Command line definition of macros.
5222 Passed to the preprocessor.
5224 \labelwidthstring 00.00.0000
5229 \begin_inset LatexCommand \index{-M}
5235 Tell the preprocessor to output a rule suitable for make describing the
5236 dependencies of each object file.
5237 For each source file, the preprocessor outputs one make-rule whose target
5238 is the object file name for that source file and whose dependencies are
5239 all the files `#include'd in it.
5240 This rule may be a single line or may be continued with `
5242 '-newline if it is long.
5243 The list of rules is printed on standard output instead of the preprocessed
5246 \begin_inset LatexCommand \index{-E}
5252 \labelwidthstring 00.00.0000
5257 \begin_inset LatexCommand \index{-C}
5263 Tell the preprocessor not to discard comments.
5264 Used with the `-E' option.
5266 \labelwidthstring 00.00.0000
5271 \begin_inset LatexCommand \index{-MM}
5282 Like `-M' but the output mentions only the user header files included with
5284 \begin_inset Quotes eld
5288 System header files included with `#include <file>' are omitted.
5290 \labelwidthstring 00.00.0000
5295 \begin_inset LatexCommand \index{-Aquestion(answer)}
5301 Assert the answer answer for question, in case it is tested with a preprocessor
5302 conditional such as `#if #question(answer)'.
5303 `-A-' disables the standard assertions that normally describe the target
5306 \labelwidthstring 00.00.0000
5311 \begin_inset LatexCommand \index{-Umacro}
5317 Undefine macro macro.
5318 `-U' options are evaluated after all `-D' options, but before any `-include'
5319 and `-imacros' options.
5321 \labelwidthstring 00.00.0000
5326 \begin_inset LatexCommand \index{-dM}
5332 Tell the preprocessor to output only a list of the macro definitions that
5333 are in effect at the end of preprocessing.
5334 Used with the `-E' option.
5336 \labelwidthstring 00.00.0000
5341 \begin_inset LatexCommand \index{-dD}
5347 Tell the preprocessor to pass all macro definitions into the output, in
5348 their proper sequence in the rest of the output.
5350 \labelwidthstring 00.00.0000
5355 \begin_inset LatexCommand \index{-dN}
5366 Like `-dD' except that the macro arguments and contents are omitted.
5367 Only `#define name' is included in the output.
5369 \labelwidthstring 00.00.0000
5374 preprocessorOption[,preprocessorOption]
5377 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5382 Pass the preprocessorOption to the preprocessor
5387 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5392 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5393 Collection (gcc), if you need more dedicated options please refer to the
5395 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5403 \begin_inset LatexCommand \index{Options linker}
5408 \begin_inset LatexCommand \index{Linker options}
5414 \labelwidthstring 00.00.0000
5434 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5439 \begin_inset LatexCommand \index{-L -\/-lib-path}
5446 <absolute path to additional libraries> This option is passed to the linkage
5447 editor's additional libraries
5448 \begin_inset LatexCommand \index{Libraries}
5453 The path name must be absolute.
5454 Additional library files may be specified in the command line.
5455 See section Compiling programs for more details.
5457 \labelwidthstring 00.00.0000
5474 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5479 <Value> The start location of the external ram
5480 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5484 , default value is 0.
5485 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5495 -xram-loc 0x8000 or -
5507 \labelwidthstring 00.00.0000
5524 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5529 <Value> The start location of the code
5530 \begin_inset LatexCommand \index{code}
5534 segment, default value 0.
5535 Note when this option is used the interrupt vector table is also relocated
5536 to the given address.
5537 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5547 -code-loc 0x8000 or -
5559 \labelwidthstring 00.00.0000
5576 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5581 <Value> By default the stack
5582 \begin_inset LatexCommand \index{stack}
5586 is placed after the data segment.
5587 Using this option the stack can be placed anywhere in the internal memory
5589 The value entered can be in Hexadecimal or Decimal format, e.g.
5600 -stack-loc 0x20 or -
5611 Since the sp register is incremented before a push or call, the initial
5612 sp will be set to one byte prior the provided value.
5613 The provided value should not overlap any other memory areas such as used
5614 register banks or the data segment and with enough space for the current
5632 \begin_inset LatexCommand \index{-\/-pack-iram}
5636 option (which is now a default setting) will override this setting, so
5637 you should also specify the .
5654 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5658 option if you need to manually place the stack.
5660 \labelwidthstring 00.00.0000
5677 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5682 <Value> The start location of the internal ram data
5683 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5688 The value entered can be in Hexadecimal or Decimal format, eg.
5710 (By default, the start location of the internal ram data segment is set
5711 as low as possible in memory, taking into account the used register banks
5712 and the bit segment at address 0x20.
5713 For example if register banks 0 and 1 are used without bit variables, the
5714 data segment will be set, if -
5724 -data-loc is not used, to location 0x10.)
5726 \labelwidthstring 00.00.0000
5743 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5748 <Value> The start location of the indirectly addressable internal ram
5749 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5753 of the 8051, default value is 0x80.
5754 The value entered can be in Hexadecimal or Decimal format, eg.
5765 -idata-loc 0x88 or -
5777 \labelwidthstring 00.00.0000
5794 <Value> The start location of the bit
5795 \begin_inset LatexCommand \index{bit}
5799 addressable internal ram of the 8051.
5805 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5808 \labelwidthstring 00.00.0000
5823 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5832 The linker output (final object code) is in Intel Hex format.
5833 \begin_inset LatexCommand \index{Intel hex format}
5837 This is the default option.
5838 The format itself is documented in the documentation of srecord
5839 \begin_inset LatexCommand \index{srecord (tool)}
5845 \labelwidthstring 00.00.0000
5860 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5869 The linker output (final object code) is in Motorola S19 format
5870 \begin_inset LatexCommand \index{Motorola S19 format}
5875 The format itself is documented in the documentation of srecord.
5877 \labelwidthstring 00.00.0000
5892 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5901 The linker output (final object code) is in ELF format
5902 \begin_inset LatexCommand \index{ELF format}
5907 (Currently only supported for the HC08 processors)
5909 \labelwidthstring 00.00.0000
5914 linkOption[,linkOption]
5917 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5922 Pass the linkOption to the linker.
5923 See file sdcc/as/doc/asxhtm.html for more on linker options.
5927 \begin_inset LatexCommand \index{Options MCS51}
5932 \begin_inset LatexCommand \index{MCS51 options}
5938 \labelwidthstring 00.00.0000
5953 \begin_inset LatexCommand \index{-\/-model-small}
5964 Generate code for Small Model programs, see section Memory Models for more
5966 This is the default model.
5968 \labelwidthstring 00.00.0000
5983 \begin_inset LatexCommand \index{-\/-model-large}
5989 Generate code for Large model programs, see section Memory Models for more
5991 If this option is used all source files in the project have to be compiled
5994 \labelwidthstring 00.00.0000
6009 \begin_inset LatexCommand \index{-\/-xstack}
6015 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6016 variables and passing parameters.
6018 \begin_inset LatexCommand \ref{sub:External-Stack}
6023 External Stack for more details.
6025 \labelwidthstring 00.00.0000
6043 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6047 Causes the linker to check if the internal ram usage is within limits of
6050 \labelwidthstring 00.00.0000
6068 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6072 Causes the linker to check if the external ram usage is within limits of
6075 \labelwidthstring 00.00.0000
6093 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6097 Causes the linker to check if the code memory usage is within limits of
6100 \labelwidthstring 00.00.0000
6118 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6122 Causes the linker to check if there is at minimum <Value> bytes for stack.
6124 \labelwidthstring 00.00.0000
6142 \begin_inset LatexCommand \index{-\/-pack-iram}
6146 Causes the linker to use unused register banks for data variables and pack
6147 data, idata and stack together.
6148 This is the default now.
6150 \labelwidthstring 00.00.0000
6168 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6172 Causes the linker to use old style for allocating memory areas.
6175 DS390 / DS400 Options
6176 \begin_inset LatexCommand \index{Options DS390}
6181 \begin_inset LatexCommand \index{DS390 options}
6187 \labelwidthstring 00.00.0000
6204 \begin_inset LatexCommand \index{-\/-model-flat24}
6214 Generate 24-bit flat mode code.
6215 This is the one and only that the ds390 code generator supports right now
6216 and is default when using
6221 See section Memory Models for more details.
6223 \labelwidthstring 00.00.0000
6238 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6244 disable interrupts during ESP:SP updates.
6246 \labelwidthstring 00.00.0000
6263 \begin_inset LatexCommand \index{-\/-stack-10bit}
6267 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6268 This is the one and only that the ds390 code generator supports right now
6269 and is default when using
6274 In this mode, the stack is located in the lower 1K of the internal RAM,
6275 which is mapped to 0x400000.
6276 Note that the support is incomplete, since it still uses a single byte
6277 as the stack pointer.
6278 This means that only the lower 256 bytes of the potential 1K stack space
6279 will actually be used.
6280 However, this does allow you to reclaim the precious 256 bytes of low RAM
6281 for use for the DATA and IDATA segments.
6282 The compiler will not generate any code to put the processor into 10 bit
6284 It is important to ensure that the processor is in this mode before calling
6285 any re-entrant functions compiled with this option.
6286 In principle, this should work with the
6299 \begin_inset LatexCommand \index{-\/-stack-auto}
6305 option, but that has not been tested.
6306 It is incompatible with the
6319 \begin_inset LatexCommand \index{-\/-xstack}
6326 It also only makes sense if the processor is in 24 bit contiguous addressing
6339 -model-flat24 option
6343 \labelwidthstring 00.00.0000
6358 \begin_inset LatexCommand \index{-\/-stack-probe}
6364 insert call to function __stack_probe at each function prologue.
6366 \labelwidthstring 00.00.0000
6381 \begin_inset LatexCommand \index{-\/-tini-libid}
6387 <nnnn> LibraryID used in -mTININative.
6390 \labelwidthstring 00.00.0000
6405 \begin_inset LatexCommand \index{-\/-use-accelerator}
6411 generate code for DS390 Arithmetic Accelerator.
6416 \begin_inset LatexCommand \index{Options Z80}
6421 \begin_inset LatexCommand \index{Z80 options}
6427 \labelwidthstring 00.00.0000
6444 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6454 Force a called function to always save BC.
6456 \labelwidthstring 00.00.0000
6473 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6477 When linking, skip the standard crt0.o object file.
6478 You must provide your own crt0.o for your system when linking.
6482 Optimization Options
6483 \begin_inset LatexCommand \index{Options optimization}
6488 \begin_inset LatexCommand \index{Optimization options}
6494 \labelwidthstring 00.00.0000
6509 \begin_inset LatexCommand \index{-\/-nogcse}
6515 Will not do global subexpression elimination, this option may be used when
6516 the compiler creates undesirably large stack/data spaces to store compiler
6518 A warning message will be generated when this happens and the compiler
6519 will indicate the number of extra bytes it allocated.
6520 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6522 \begin_inset LatexCommand \index{\#pragma nogcse}
6526 can be used to turn off global subexpression elimination
6527 \begin_inset LatexCommand \index{Subexpression elimination}
6531 for a given function only.
6533 \labelwidthstring 00.00.0000
6548 \begin_inset LatexCommand \index{-\/-noinvariant}
6554 Will not do loop invariant optimizations, this may be turned off for reasons
6555 explained for the previous option.
6556 For more details of loop optimizations performed see Loop Invariants in
6558 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6563 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6565 \begin_inset LatexCommand \index{\#pragma noinvariant}
6569 can be used to turn off invariant optimizations for a given function only.
6571 \labelwidthstring 00.00.0000
6586 \begin_inset LatexCommand \index{-\/-noinduction}
6592 Will not do loop induction optimizations, see section strength reduction
6594 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6596 \begin_inset LatexCommand \index{\#pragma noinduction}
6600 can be used to turn off induction optimizations for a given function only.
6602 \labelwidthstring 00.00.0000
6617 \begin_inset LatexCommand \index{-\/-nojtbound}
6628 Will not generate boundary condition check when switch statements
6629 \begin_inset LatexCommand \index{switch statement}
6633 are implemented using jump-tables.
6635 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6640 Switch Statements for more details.
6641 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6643 \begin_inset LatexCommand \index{\#pragma nojtbound}
6647 can be used to turn off boundary checking for jump tables for a given function
6650 \labelwidthstring 00.00.0000
6665 \begin_inset LatexCommand \index{-\/-noloopreverse}
6674 Will not do loop reversal
6675 \begin_inset LatexCommand \index{Loop reversing}
6681 \labelwidthstring 00.00.0000
6698 \begin_inset LatexCommand \index{-\/-nolabelopt }
6702 Will not optimize labels (makes the dumpfiles more readable).
6704 \labelwidthstring 00.00.0000
6719 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6725 Will not memcpy initialized data from code space into xdata space.
6726 This saves a few bytes in code space if you don't have initialized data.
6728 \labelwidthstring 00.00.0000
6743 \begin_inset LatexCommand \index{-\/-nooverlay}
6749 The compiler will not overlay parameters and local variables of any function,
6750 see section Parameters and local variables for more details.
6752 \labelwidthstring 00.00.0000
6767 \begin_inset LatexCommand \index{-\/-no-peep}
6773 Disable peep-hole optimization.
6775 \labelwidthstring 00.00.0000
6792 \begin_inset LatexCommand \index{-\/-peep-file}
6797 <filename> This option can be used to use additional rules to be used by
6798 the peep hole optimizer.
6800 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6805 Peep Hole optimizations for details on how to write these rules.
6807 \labelwidthstring 00.00.0000
6822 \begin_inset LatexCommand \index{-\/-peep-asm}
6828 Pass the inline assembler code through the peep hole optimizer.
6829 This can cause unexpected changes to inline assembler code, please go through
6830 the peephole optimizer
6831 \begin_inset LatexCommand \index{Peephole optimizer}
6835 rules defined in the source file tree '<target>/peeph.def' before using
6838 \labelwidthstring 00.00.0000
6853 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6859 The compiler will optimize code generation towards fast code, possibly
6860 at the expense of code size.
6862 \labelwidthstring 00.00.0000
6877 \begin_inset LatexCommand \index{-\/-opt-code-size}
6883 The compiler will optimize code generation towards compact code, possibly
6884 at the expense of code speed.
6888 \begin_inset LatexCommand \index{Options other}
6894 \labelwidthstring 00.00.0000
6910 \begin_inset LatexCommand \index{-\/-compile-only}
6915 \begin_inset LatexCommand \index{-c -\/-compile-only}
6921 will compile and assemble the source, but will not call the linkage editor.
6923 \labelwidthstring 00.00.0000
6942 \begin_inset LatexCommand \index{-\/-c1mode}
6948 reads the preprocessed source from standard input and compiles it.
6949 The file name for the assembler output must be specified using the -o option.
6951 \labelwidthstring 00.00.0000
6956 \begin_inset LatexCommand \index{-E}
6962 Run only the C preprocessor.
6963 Preprocess all the C source files specified and output the results to standard
6966 \labelwidthstring 00.00.0000
6972 \begin_inset LatexCommand \index{-o <path/file>}
6978 The output path resp.
6979 file where everything will be placed.
6980 If the parameter is a path, it must have a trailing slash (or backslash
6981 for the Windows binaries) to be recognized as a path.
6984 \labelwidthstring 00.00.0000
6999 \begin_inset LatexCommand \index{-\/-stack-auto}
7010 All functions in the source file will be compiled as
7015 \begin_inset LatexCommand \index{reentrant}
7020 the parameters and local variables will be allocated on the stack
7021 \begin_inset LatexCommand \index{stack}
7027 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7031 Parameters and Local Variables for more details.
7032 If this option is used all source files in the project should be compiled
7034 It automatically implies --int-long-reent and --float-reent.
7037 \labelwidthstring 00.00.0000
7052 \begin_inset LatexCommand \index{-\/-callee-saves}
7056 function1[,function2][,function3]....
7059 The compiler by default uses a caller saves convention for register saving
7060 across function calls, however this can cause unnecessary register pushing
7061 & popping when calling small functions from larger functions.
7062 This option can be used to switch the register saving convention for the
7063 function names specified.
7064 The compiler will not save registers when calling these functions, no extra
7065 code will be generated at the entry & exit (function prologue
7068 \begin_inset LatexCommand \index{function prologue}
7077 \begin_inset LatexCommand \index{function epilogue}
7083 ) for these functions to save & restore the registers used by these functions,
7084 this can SUBSTANTIALLY reduce code & improve run time performance of the
7086 In the future the compiler (with inter procedural analysis) will be able
7087 to determine the appropriate scheme to use for each function call.
7088 DO NOT use this option for built-in functions such as _mulint..., if this
7089 option is used for a library function the appropriate library function
7090 needs to be recompiled with the same option.
7091 If the project consists of multiple source files then all the source file
7092 should be compiled with the same -
7102 -callee-saves option string.
7103 Also see #pragma\SpecialChar ~
7105 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7111 \labelwidthstring 00.00.0000
7126 \begin_inset LatexCommand \index{-\/-debug}
7135 When this option is used the compiler will generate debug information.
7136 The debug information collected in a file with .cdb extension can be used
7138 For more information see documentation for SDCDB.
7139 Another file with no extension contains debug information in AOMF or AOMF51
7140 \begin_inset LatexCommand \index{AOMF, AOMF51}
7144 format which is commonly used by third party tools.
7146 \labelwidthstring 00.00.0000
7151 \begin_inset LatexCommand \index{-S}
7162 Stop after the stage of compilation proper; do not assemble.
7163 The output is an assembler code file for the input file specified.
7165 \labelwidthstring 00.00.0000
7180 \begin_inset LatexCommand \index{-\/-int-long-reent}
7186 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7187 Note by default these libraries are compiled as non-reentrant.
7188 See section Installation for more details.
7190 \labelwidthstring 00.00.0000
7205 \begin_inset LatexCommand \index{-\/-cyclomatic}
7214 This option will cause the compiler to generate an information message for
7215 each function in the source file.
7216 The message contains some
7220 information about the function.
7221 The number of edges and nodes the compiler detected in the control flow
7222 graph of the function, and most importantly the
7224 cyclomatic complexity
7225 \begin_inset LatexCommand \index{Cyclomatic complexity}
7231 see section on Cyclomatic Complexity for more details.
7233 \labelwidthstring 00.00.0000
7248 \begin_inset LatexCommand \index{-\/-float-reent}
7254 Floating point library is compiled as reentrant
7255 \begin_inset LatexCommand \index{reentrant}
7260 See section Installation for more details.
7262 \labelwidthstring 00.00.0000
7277 \begin_inset LatexCommand \index{-\/-main-return}
7283 This option can be used if the code generated is called by a monitor program
7284 or if the main routine includes an endless loop.
7285 This option might result in slightly smaller code and save two bytes of
7287 The return from the 'main'
7288 \begin_inset LatexCommand \index{main return}
7292 function will return to the function calling main.
7293 The default setting is to lock up i.e.
7300 \labelwidthstring 00.00.0000
7315 \begin_inset LatexCommand \index{-\/-nostdinc}
7321 This will prevent the compiler from passing on the default include path
7322 to the preprocessor.
7324 \labelwidthstring 00.00.0000
7339 \begin_inset LatexCommand \index{-\/-nostdlib}
7345 This will prevent the compiler from passing on the default library
7346 \begin_inset LatexCommand \index{Libraries}
7352 \labelwidthstring 00.00.0000
7367 \begin_inset LatexCommand \index{-\/-verbose}
7373 Shows the various actions the compiler is performing.
7375 \labelwidthstring 00.00.0000
7380 \begin_inset LatexCommand \index{-V}
7386 Shows the actual commands the compiler is executing.
7388 \labelwidthstring 00.00.0000
7403 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7409 Hides your ugly and inefficient c-code from the asm file, so you can always
7410 blame the compiler :)
7412 \labelwidthstring 00.00.0000
7427 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7433 Will not include peep-hole comments in the generated files.
7435 \labelwidthstring 00.00.0000
7450 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7456 Include i-codes in the asm file.
7457 Sounds like noise but is most helpful for debugging the compiler itself.
7459 \labelwidthstring 00.00.0000
7474 \begin_inset LatexCommand \index{-\/-less-pedantic}
7480 Disable some of the more pedantic warnings
7481 \begin_inset LatexCommand \index{Warnings}
7485 (jwk burps: please be more specific here, please!).
7486 If you want rather more than less warnings you should consider using a
7487 separate tool dedicated to syntax checking like splint
7488 \begin_inset LatexCommand \url{www.splint.org}
7494 \labelwidthstring 00.00.0000
7508 -disable-warning\SpecialChar ~
7510 \begin_inset LatexCommand \index{-\/-disable-warning}
7516 Disable specific warning with number <nnnn>.
7518 \labelwidthstring 00.00.0000
7533 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7539 Display the directories in the compiler's search path
7541 \labelwidthstring 00.00.0000
7556 \begin_inset LatexCommand \index{-\/-vc}
7562 Display errors and warnings using MSVC style, so you can use SDCC with
7565 \labelwidthstring 00.00.0000
7580 \begin_inset LatexCommand \index{-\/-use-stdout}
7586 Send errors and warnings to stdout instead of stderr.
7588 \labelwidthstring 00.00.0000
7593 asmOption[,asmOption]
7596 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7601 Pass the asmOption to the assembler
7602 \begin_inset LatexCommand \index{Options assembler}
7607 \begin_inset LatexCommand \index{Assembler options}
7612 See file sdcc/as/doc/asxhtm.html for assembler options.
7614 \labelwidthstring 00.00.0000
7629 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7635 Generally follow the C89 standard, but allow SDCC features that conflict
7636 with the standard (default).
7638 \labelwidthstring 00.00.0000
7653 \begin_inset LatexCommand \index{-\/-std-c89}
7659 Follow the C89 standard and disable SDCC features that conflict with the
7662 \labelwidthstring 00.00.0000
7677 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7683 Generally follow the C99 standard, but allow SDCC features that conflict
7684 with the standard (incomplete support).
7686 \labelwidthstring 00.00.0000
7701 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7707 Follow the C99 standard and disable SDCC features that conflict with the
7708 standard (incomplete support).
7711 Intermediate Dump Options
7712 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7717 \begin_inset LatexCommand \index{Options intermediate dump}
7722 \begin_inset LatexCommand \index{Intermediate dump options}
7729 The following options are provided for the purpose of retargetting and debugging
7731 These provided a means to dump the intermediate code (iCode
7732 \begin_inset LatexCommand \index{iCode}
7736 ) generated by the compiler in human readable form at various stages of
7737 the compilation process.
7738 More on iCodes see chapter
7739 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7744 \begin_inset Quotes srd
7747 The anatomy of the compiler
7748 \begin_inset Quotes srd
7753 \labelwidthstring 00.00.0000
7768 \begin_inset LatexCommand \index{-\/-dumpraw}
7774 This option will cause the compiler to dump the intermediate code into
7777 <source filename>.dumpraw
7779 just after the intermediate code has been generated for a function, i.e.
7780 before any optimizations are done.
7782 \begin_inset LatexCommand \index{Basic blocks}
7786 at this stage ordered in the depth first number, so they may not be in
7787 sequence of execution.
7789 \labelwidthstring 00.00.0000
7804 \begin_inset LatexCommand \index{-\/-dumpgcse}
7810 Will create a dump of iCode's, after global subexpression elimination
7811 \begin_inset LatexCommand \index{Global subexpression elimination}
7817 <source filename>.dumpgcse.
7819 \labelwidthstring 00.00.0000
7834 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7840 Will create a dump of iCode's, after deadcode elimination
7841 \begin_inset LatexCommand \index{Dead-code elimination}
7847 <source filename>.dumpdeadcode.
7849 \labelwidthstring 00.00.0000
7864 \begin_inset LatexCommand \index{-\/-dumploop}
7873 Will create a dump of iCode's, after loop optimizations
7874 \begin_inset LatexCommand \index{Loop optimization}
7880 <source filename>.dumploop.
7882 \labelwidthstring 00.00.0000
7897 \begin_inset LatexCommand \index{-\/-dumprange}
7906 Will create a dump of iCode's, after live range analysis
7907 \begin_inset LatexCommand \index{Live range analysis}
7913 <source filename>.dumprange.
7915 \labelwidthstring 00.00.0000
7930 \begin_inset LatexCommand \index{-\/-dumlrange}
7936 Will dump the life ranges
7937 \begin_inset LatexCommand \index{Live range analysis}
7943 \labelwidthstring 00.00.0000
7958 \begin_inset LatexCommand \index{-\/-dumpregassign}
7967 Will create a dump of iCode's, after register assignment
7968 \begin_inset LatexCommand \index{Register assignment}
7974 <source filename>.dumprassgn.
7976 \labelwidthstring 00.00.0000
7991 \begin_inset LatexCommand \index{-\/-dumplrange}
7997 Will create a dump of the live ranges of iTemp's
7999 \labelwidthstring 00.00.0000
8014 \begin_inset LatexCommand \index{-\/-dumpall}
8025 Will cause all the above mentioned dumps to be created.
8028 Redirecting output on Windows Shells
8031 By default SDCC writes it's error messages to
8032 \begin_inset Quotes sld
8036 \begin_inset Quotes srd
8040 To force all messages to
8041 \begin_inset Quotes sld
8045 \begin_inset Quotes srd
8069 \begin_inset LatexCommand \index{-\/-use-stdout}
8074 Additionally, if you happen to have visual studio installed in your windows
8075 machine, you can use it to compile your sources using a custom build and
8091 \begin_inset LatexCommand \index{-\/-vc}
8096 Something like this should work:
8140 -model-large -c $(InputPath)
8143 Environment variables
8144 \begin_inset LatexCommand \index{Environment variables}
8151 SDCC recognizes the following environment variables:
8153 \labelwidthstring 00.00.0000
8158 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8164 SDCC installs a signal handler
8165 \begin_inset LatexCommand \index{signal handler}
8169 to be able to delete temporary files after an user break (^C) or an exception.
8170 If this environment variable is set, SDCC won't install the signal handler
8171 in order to be able to debug SDCC.
8173 \labelwidthstring 00.00.0000
8180 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8186 Path, where temporary files will be created.
8187 The order of the variables is the search order.
8188 In a standard *nix environment these variables are not set, and there's
8189 no need to set them.
8190 On Windows it's recommended to set one of them.
8192 \labelwidthstring 00.00.0000
8197 \begin_inset LatexCommand \index{SDCC\_HOME}
8204 \begin_inset LatexCommand \ref{sub:Install-paths}
8210 \begin_inset Quotes sld
8214 \begin_inset Quotes srd
8219 \labelwidthstring 00.00.0000
8224 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8231 \begin_inset LatexCommand \ref{sub:Search-Paths}
8237 \begin_inset Quotes sld
8241 \begin_inset Quotes srd
8246 \labelwidthstring 00.00.0000
8251 \begin_inset LatexCommand \index{SDCC\_LIB}
8258 \begin_inset LatexCommand \ref{sub:Search-Paths}
8264 \begin_inset Quotes sld
8268 \begin_inset Quotes srd
8274 There are some more environment variables recognized by SDCC, but these
8275 are solely used for debugging purposes.
8276 They can change or disappear very quickly, and will never be documented.
8279 Storage Class Language Extensions
8282 MCS51/DS390 Storage Class
8283 \begin_inset LatexCommand \index{Storage class}
8290 In addition to the ANSI storage classes SDCC allows the following MCS51
8291 specific storage classes:
8292 \layout Subsubsection
8295 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8300 \begin_inset LatexCommand \index{near (storage class)}
8311 storage class for the Small Memory model (
8319 can be used synonymously).
8320 Variables declared with this storage class will be allocated in the directly
8321 addressable portion of the internal RAM of a 8051, e.g.:
8326 data unsigned char test_data;
8329 Writing 0x01 to this variable generates the assembly code:
8334 75*00 01\SpecialChar ~
8340 \layout Subsubsection
8343 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8348 \begin_inset LatexCommand \index{far (storage class)}
8355 Variables declared with this storage class will be placed in the external
8361 storage class for the Large Memory model, e.g.:
8366 xdata unsigned char test_xdata;
8369 Writing 0x01 to this variable generates the assembly code:
8374 90s00r00\SpecialChar ~
8403 \layout Subsubsection
8406 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8413 Variables declared with this storage class will be allocated into the indirectly
8414 addressable portion of the internal ram of a 8051, e.g.:
8419 idata unsigned char test_idata;
8422 Writing 0x01 to this variable generates the assembly code:
8451 Please note, the first 128 byte of idata physically access the same RAM
8453 The original 8051 had 128 byte idata memory, nowadays most devices have
8454 256 byte idata memory.
8456 \begin_inset LatexCommand \index{stack}
8460 is located in idata memory.
8461 \layout Subsubsection
8464 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8471 Paged xdata access is just as straightforward as using the other addressing
8473 It is typically located at the start of xdata and has a maximum size of
8475 The following example writes 0x01 to the address pointed to.
8476 Please note, pdata access physically accesses xdata memory.
8477 The high byte of the address is determined by port P2
8478 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8482 (or in case of some 8051 variants by a separate Special Function Register,
8484 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8493 pdata unsigned char *test_pdata_ptr;
8505 test_pdata_ptr = (pdata *)0xfe;
8511 *test_pdata_ptr = 1;
8516 Generates the assembly code:
8521 75*01 FE\SpecialChar ~
8525 _test_pdata_ptr,#0xFE
8568 \begin_inset LatexCommand \index{-\/-xstack}
8572 option is used the pdata memory area is followed by the xstack memory area
8573 and the sum of their sizes is limited to 256 bytes.
8574 \layout Subsubsection
8577 \begin_inset LatexCommand \index{code}
8584 'Variables' declared with this storage class will be placed in the code
8590 code unsigned char test_code;
8593 Read access to this variable generates the assembly code:
8598 90s00r6F\SpecialChar ~
8601 mov dptr,#_test_code
8630 indexed arrays of characters in code memory can be accessed efficiently:
8635 code char test_array[] = {'c','h','e','a','p'};
8638 Read access to this array using an 8-bit unsigned index generates the assembly
8655 90s00r41\SpecialChar ~
8658 mov dptr,#_test_array
8673 \layout Subsubsection
8676 \begin_inset LatexCommand \index{bit}
8683 This is a data-type and a storage class specifier.
8684 When a variable is declared as a bit, it is allocated into the bit addressable
8685 memory of 8051, e.g.:
8693 Writing 1 to this variable generates the assembly code:
8709 The bit addressable memory consists of 128 bits which are located from 0x20
8710 to 0x2f in data memory.
8713 Apart from this 8051 specific storage class most architectures support ANSI-C
8715 \begin_inset LatexCommand \index{bitfields}
8725 Not really meant as examples, but nevertheless showing what bitfields are
8726 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8730 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8731 signed modifier are implemented as unsigned.
8732 \layout Subsubsection
8735 \begin_inset LatexCommand \index{sfr}
8740 \begin_inset LatexCommand \index{sbit}
8747 Like the bit keyword,
8751 signifies both a data-type and storage class, they are used to describe
8772 variables of a 8051, eg:
8778 \begin_inset LatexCommand \index{at}
8782 0x80 P0;\SpecialChar ~
8783 /* special function register P0 at location 0x80 */
8785 sbit at 0xd7 CY; /* CY (Carry Flag
8786 \begin_inset LatexCommand \index{Flags}
8791 \begin_inset LatexCommand \index{Carry flag}
8798 Special function registers which are located on an address dividable by
8799 8 are bit-addressable, an
8803 addresses a specific bit within these sfr.
8804 \layout Subsubsection
8807 \begin_inset LatexCommand \index{Pointer}
8811 to MCS51/DS390 specific memory spaces
8814 SDCC allows (via language extensions) pointers to explicitly point to any
8815 of the memory spaces
8816 \begin_inset LatexCommand \index{Memory model}
8821 In addition to the explicit pointers, the compiler uses (by default) generic
8822 pointers which can be used to point to any of the memory spaces.
8826 Pointer declaration examples:
8831 /* pointer physically in internal ram pointing to object in external ram
8834 xdata unsigned char * data p;
8838 /* pointer physically in external ram pointing to object in internal ram
8841 data unsigned char * xdata p;
8845 /* pointer physically in code rom pointing to data in xdata space */
8847 xdata unsigned char * code p;
8851 /* pointer physically in code space pointing to data in code space */
8853 code unsigned char * code p;
8857 /* the following is a generic pointer physically located in xdata space
8863 Well you get the idea.
8868 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
8881 The highest order byte of the
8885 pointers contains the data space information.
8886 Assembler support routines are called whenever data is stored or retrieved
8892 These are useful for developing reusable library
8893 \begin_inset LatexCommand \index{Libraries}
8898 Explicitly specifying the pointer type will generate the most efficient
8900 \layout Subsubsection
8902 Notes on MCS51 memory
8903 \begin_inset LatexCommand \index{MCS51 memory}
8910 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
8911 RAM memory which is structured as follows:
8915 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
8918 - Bytes 20-2F - 16 bytes to hold 128 bit
8919 \begin_inset LatexCommand \index{bit}
8925 - Bytes 30-7F - 80 bytes for general purpose use.
8930 Additionally some members of the MCS51 family may have up to 128 bytes of
8931 additional, indirectly addressable, internal RAM memory (
8936 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8941 Furthermore, some chips may have some built in external memory (
8946 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8950 ) which should not be confused with the internal, directly addressable RAM
8956 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8961 Sometimes this built in
8965 memory has to be activated before using it (you can probably find this
8966 information on the datasheet of the microcontroller your are using, see
8968 \begin_inset LatexCommand \ref{sub:Startup-Code}
8976 Normally SDCC will only use the first bank
8977 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
8981 of registers (register bank 0), but it is possible to specify that other
8982 banks of registers should be used in interrupt
8983 \begin_inset LatexCommand \index{interrupt}
8988 By default, the compiler will place the stack after the last byte of allocated
8989 memory for variables.
8990 For example, if the first 2 banks of registers are used, and only four
8995 variables, it will position the base of the internal stack at address 20
8997 This implies that as the stack
8998 \begin_inset LatexCommand \index{stack}
9002 grows, it will use up the remaining register banks, and the 16 bytes used
9003 by the 128 bit variables, and 80 bytes for general purpose use.
9004 If any bit variables are used, the data variables will be placed in unused
9005 register banks and after the byte holding the last bit variable.
9006 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9011 variables will be placed starting from address 0x10 to 0x20 and continue
9024 \begin_inset LatexCommand \index{-\/-data-loc<Value>}
9028 to specify the start address of the
9042 -iram-size to specify the size of the total internal RAM (
9054 By default the 8051 linker will place the stack after the last byte of (i)data
9067 \begin_inset LatexCommand \index{-\/-stack-loc<Value>}
9071 allows you to specify the start of the stack, i.e.
9072 you could start it after any data in the general purpose area.
9073 If your microcontroller has additional indirectly addressable internal
9078 ) you can place the stack on it.
9079 You may also need to use -
9090 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9094 to set the start address of the external RAM (
9109 \begin_inset LatexCommand \index{-\/-data-loc}
9113 to specify its size.
9114 Same goes for the code memory, using -
9125 \begin_inset LatexCommand \index{-\/-data-loc}
9140 \begin_inset LatexCommand \index{-\/-data-loc}
9145 If in doubt, don't specify any options and see if the resulting memory
9146 layout is appropriate, then you can adjust it.
9149 The linker generates two files with memory allocation information.
9150 The first, with extension .map
9151 \begin_inset LatexCommand \index{<file>.map}
9155 shows all the variables and segments.
9156 The second with extension .mem
9157 \begin_inset LatexCommand \index{<file>.mem}
9161 shows the final memory layout.
9162 The linker will complain either if memory segments overlap, there is not
9163 enough memory, or there is not enough space for stack.
9164 If you get any linking warnings and/or errors related to stack or segments
9165 allocation, take a look at either the .map or .mem files to find out what
9167 The .mem file may even suggest a solution to the problem.
9170 Z80/Z180 Storage Class
9171 \begin_inset LatexCommand \index{Storage class}
9176 \layout Subsubsection
9179 \begin_inset LatexCommand \index{sfr}
9183 (in/out to 8-bit addresses)
9187 \begin_inset LatexCommand \index{Z80}
9191 family has separate address spaces for memory and
9201 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9205 is accessed with special instructions, e.g.:
9210 sfr at 0x78 IoPort;\SpecialChar ~
9212 /* define a var in I/O space at 78h called IoPort */
9216 Writing 0x01 to this variable generates the assembly code:
9236 \layout Subsubsection
9239 \begin_inset LatexCommand \index{sfr}
9243 (in/out to 16-bit addresses)
9250 is used to support 16 bit addresses in I/O memory e.g.:
9256 \begin_inset LatexCommand \index{at}
9263 Writing 0x01 to this variable generates the assembly code:
9268 01 23 01\SpecialChar ~
9288 \layout Subsubsection
9291 \begin_inset LatexCommand \index{sfr}
9295 (in0/out0 to 8 bit addresses on Z180
9296 \begin_inset LatexCommand \index{Z180}
9301 \begin_inset LatexCommand \index{HD64180}
9308 The compiler option -
9318 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9320 \begin_inset LatexCommand \index{\#pragma portmode}
9324 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9334 If you include the file z180.h this will be set automatically.
9338 \begin_inset LatexCommand \index{Storage class}
9343 \layout Subsubsection
9346 \begin_inset LatexCommand \index{data (hc08 storage class)}
9353 The data storage class declares a variable that resides in the first 256
9354 bytes of memory (the direct page).
9355 The HC08 is most efficient at accessing variables (especially pointers)
9357 \layout Subsubsection
9360 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9367 The xdata storage class declares a variable that can reside anywhere in
9369 This is the default if no storage class is specified.
9374 \begin_inset LatexCommand \index{Absolute addressing}
9381 Data items can be assigned an absolute address with the
9384 \begin_inset LatexCommand \index{at}
9390 keyword, in addition to a storage class, e.g.:
9396 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9401 \begin_inset LatexCommand \index{at}
9405 0x7ffe unsigned int chksum;
9408 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9409 of the external ram.
9414 reserve any space for variables declared in this way (they are implemented
9415 with an equate in the assembler).
9416 Thus it is left to the programmer to make sure there are no overlaps with
9417 other variables that are declared without the absolute address.
9418 The assembler listing file (.lst
9419 \begin_inset LatexCommand \index{<file>.lst}
9423 ) and the linker output files (.rst
9424 \begin_inset LatexCommand \index{<file>.rst}
9429 \begin_inset LatexCommand \index{<file>.map}
9433 ) are good places to look for such overlaps.
9434 Variables with an absolute address are
9439 \begin_inset LatexCommand \index{Variable initialization}
9446 In case of memory mapped I/O devices the keyword
9450 has to be used to tell the compiler that accesses might not be removed:
9456 \begin_inset LatexCommand \index{volatile}
9461 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9466 \begin_inset LatexCommand \index{at}
9470 0x8000 unsigned char PORTA_8255;
9473 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9478 \begin_inset LatexCommand \index{Aligned array}
9485 starts at a block (256 byte) boundary
9486 \begin_inset LatexCommand \index{block boundary}
9491 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9497 Absolute addresses can be specified for variables in all storage classes,
9504 \begin_inset LatexCommand \index{bit}
9509 \begin_inset LatexCommand \index{at}
9516 The above example will allocate the variable at offset 0x02 in the bit-addressab
9518 There is no real advantage to assigning absolute addresses to variables
9519 in this manner, unless you want strict control over all the variables allocated.
9520 One possible use would be to write hardware portable code.
9521 For example, if you have a routine that uses one or more of the microcontroller
9522 I/O pins, and such pins are different for two different hardwares, you
9523 can declare the I/O pins in your routine using:
9528 extern volatile bit MOSI;\SpecialChar ~
9532 /* master out, slave in */
9534 extern volatile bit MISO;\SpecialChar ~
9538 /* master in, slave out */
9540 extern volatile bit MCLK;\SpecialChar ~
9548 /* Input and Output of a byte on a 3-wire serial bus.
9553 If needed adapt polarity of clock, polarity of data and bit order
9558 unsigned char spi_io(unsigned char out_byte)
9582 MOSI = out_byte & 0x80;
9612 /* _asm nop _endasm; */\SpecialChar ~
9620 /* for slow peripherals */
9671 Then, someplace in the code for the first hardware you would use
9676 bit at 0x80 MOSI;\SpecialChar ~
9680 /* I/O port 0, bit 0 */
9682 bit at 0x81 MISO;\SpecialChar ~
9686 /* I/O port 0, bit 1 */
9688 bit at 0x82 MCLK;\SpecialChar ~
9692 /* I/O port 0, bit 2 */
9695 Similarly, for the second hardware you would use
9700 bit at 0x83 MOSI;\SpecialChar ~
9704 /* I/O port 0, bit 3 */
9706 bit at 0x91 MISO;\SpecialChar ~
9710 /* I/O port 1, bit 1 */
9713 \begin_inset LatexCommand \index{bit}
9717 at 0x92 MCLK;\SpecialChar ~
9721 /* I/O port 1, bit 2 */
9724 and you can use the same hardware dependent routine without changes, as
9725 for example in a library.
9726 This is somehow similar to sbit, but only one absolute address has to be
9727 specified in the whole project.
9731 \begin_inset LatexCommand \index{Parameters}
9736 \begin_inset LatexCommand \index{function parameter}
9741 \begin_inset LatexCommand \index{local variables}
9746 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9753 Automatic (local) variables and parameters to functions can either be placed
9754 on the stack or in data-space.
9755 The default action of the compiler is to place these variables in the internal
9756 RAM (for small model) or external RAM (for large model).
9757 This in fact makes them similar to
9760 \begin_inset LatexCommand \index{static}
9766 so by default functions are non-reentrant
9767 \begin_inset LatexCommand \index{reentrant}
9776 They can be placed on the stack
9777 \begin_inset LatexCommand \index{stack}
9794 \begin_inset LatexCommand \index{-\/-stack-auto}
9802 #pragma\SpecialChar ~
9806 \begin_inset LatexCommand \index{\#pragma stackauto}
9813 \begin_inset LatexCommand \index{reentrant}
9819 keyword in the function declaration, e.g.:
9824 unsigned char foo(char i) reentrant
9838 Since stack space on 8051 is limited, the
9856 option should be used sparingly.
9857 Note that the reentrant keyword just means that the parameters & local
9858 variables will be allocated to the stack, it
9862 mean that the function is register bank
9863 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9872 \begin_inset LatexCommand \index{local variables}
9876 can be assigned storage classes and absolute
9877 \begin_inset LatexCommand \index{Absolute addressing}
9894 xdata unsigned char i;
9907 \begin_inset LatexCommand \index{at}
9911 0x31 unsigned char j;
9923 In the above example the variable
9927 will be allocated in the external ram,
9931 in bit addressable space and
9950 or when a function is declared as
9954 this should only be done for static variables.
9958 \begin_inset LatexCommand \index{function parameter}
9962 however are not allowed any storage class
9963 \begin_inset LatexCommand \index{Storage class}
9967 , (storage classes for parameters will be ignored), their allocation is
9968 governed by the memory model in use, and the reentrancy options.
9972 \begin_inset LatexCommand \label{sub:Overlaying}
9977 \begin_inset LatexCommand \index{Overlaying}
9985 \begin_inset LatexCommand \index{reentrant}
9989 functions SDCC will try to reduce internal ram space usage by overlaying
9990 parameters and local variables of a function (if possible).
9991 Parameters and local variables
9992 \begin_inset LatexCommand \index{local variables}
9996 of a function will be allocated to an overlayable segment if the function
9999 no other function calls and the function is non-reentrant and the memory
10001 \begin_inset LatexCommand \index{Memory model}
10008 If an explicit storage class
10009 \begin_inset LatexCommand \index{Storage class}
10013 is specified for a local variable, it will NOT be overlayed.
10016 Note that the compiler (not the linkage editor) makes the decision for overlayin
10018 Functions that are called from an interrupt service routine should be preceded
10019 by a #pragma\SpecialChar ~
10021 \begin_inset LatexCommand \index{\#pragma nooverlay}
10025 if they are not reentrant.
10028 Also note that the compiler does not do any processing of inline assembler
10029 code, so the compiler might incorrectly assign local variables and parameters
10030 of a function into the overlay segment if the inline assembler code calls
10031 other c-functions that might use the overlay.
10032 In that case the #pragma\SpecialChar ~
10033 nooverlay should be used.
10036 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10038 \begin_inset LatexCommand \index{Multiplication}
10043 \begin_inset LatexCommand \index{Division}
10047 will NOT be overlayed since these are implemented using external functions,
10056 \begin_inset LatexCommand \index{\#pragma nooverlay}
10062 void set_error(unsigned char errcd)
10078 void some_isr () interrupt
10079 \begin_inset LatexCommand \index{interrupt}
10109 In the above example the parameter
10117 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10119 not present, this could cause unpredictable runtime behavior when called
10120 from an interrupt service routine.
10121 The #pragma\SpecialChar ~
10122 nooverlay ensures that the parameters and local variables for
10123 the function are NOT overlayed.
10126 Interrupt Service Routines
10127 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10134 General Information
10149 outines to be coded in C, with some extended keywords.
10154 void timer_isr (void) interrupt 1 using 1
10168 The optional number following the
10171 \begin_inset LatexCommand \index{interrupt}
10177 keyword is the interrupt number this routine will service.
10178 When present, the compiler will insert a call to this routine in the interrupt
10179 vector table for the interrupt number specified.
10180 If you have multiple source files in your project, interrupt service routines
10181 can be present in any of them, but a prototype of the isr MUST be present
10182 or included in the file that contains the function
10191 keyword can be used to tell the compiler to use the specified register
10192 bank (8051 specific) when generating code for this function.
10198 Interrupt service routines open the door for some very interesting bugs:
10200 If an interrupt service routine changes variables which are accessed by
10201 other functions these variables have to be declared
10206 \begin_inset LatexCommand \index{volatile}
10214 If the access to these variables is not
10217 \begin_inset LatexCommand \index{atomic}
10224 the processor needs more than one instruction for the access and could
10225 be interrupted while accessing the variable) the interrupt must be disabled
10226 during the access to avoid inconsistent data.
10227 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10228 and should be protected by disabling interrupts.
10229 You're not automatically on the safe side if you use 8 bit variables though.
10230 We need an example here: f.e.
10231 on the 8051 the harmless looking
10232 \begin_inset Quotes srd
10237 flags\SpecialChar ~
10242 \begin_inset Quotes sld
10251 \begin_inset Quotes srd
10256 flags\SpecialChar ~
10261 \begin_inset Quotes sld
10264 from within an interrupt routine might get lost if the interrupt occurs
10267 \begin_inset Quotes sld
10272 counter\SpecialChar ~
10277 \begin_inset Quotes srd
10280 is not atomic on the 8051 even if
10284 is located in data memory.
10285 Bugs like these are hard to reproduce and can cause a lot of trouble.
10289 The return address and the registers used in the interrupt service routine
10290 are saved on the stack
10291 \begin_inset LatexCommand \index{stack}
10295 so there must be sufficient stack space.
10296 If there isn't variables or registers (or even the return address itself)
10303 \begin_inset LatexCommand \index{stack overflow}
10307 is most likely to happen if the interrupt occurs during the
10308 \begin_inset Quotes sld
10312 \begin_inset Quotes srd
10315 subroutine when the stack is already in use for f.e.
10316 many return addresses.
10319 A special note here, int (16 bit) and long (32 bit) integer division
10320 \begin_inset LatexCommand \index{Division}
10325 \begin_inset LatexCommand \index{Multiplication}
10330 \begin_inset LatexCommand \index{Modulus}
10335 \begin_inset LatexCommand \index{Floating point support}
10339 operations are implemented using external support routines developed in
10341 If an interrupt service routine needs to do any of these operations then
10342 the support routines (as mentioned in a following section) will have to
10343 be recompiled using the
10356 \begin_inset LatexCommand \index{-\/-stack-auto}
10362 option and the source file will need to be compiled using the
10377 \begin_inset LatexCommand \index{-\/-int-long-reent}
10384 Calling other functions from an interrupt service routine is not recommended,
10385 avoid it if possible.
10386 Note that when some function is called from an interrupt service routine
10387 it should be preceded by a #pragma\SpecialChar ~
10389 \begin_inset LatexCommand \index{\#pragma nooverlay}
10393 if it is not reentrant.
10394 Furthermore nonreentrant functions should not be called from the main program
10395 while the interrupt service routine might be active.
10401 \begin_inset LatexCommand \ref{sub:Overlaying}
10406 about Overlaying and section
10407 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10412 about Functions using private register banks.
10415 MCS51/DS390 Interrupt Service Routines
10418 Interrupt numbers and the corresponding address & descriptions for the Standard
10419 8051/8052 are listed below.
10420 SDCC will automatically adjust the interrupt vector table to the maximum
10421 interrupt number specified.
10427 \begin_inset Tabular
10428 <lyxtabular version="3" rows="7" columns="3">
10430 <column alignment="center" valignment="top" leftline="true" width="0in">
10431 <column alignment="center" valignment="top" leftline="true" width="0in">
10432 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10433 <row topline="true" bottomline="true">
10434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10459 <row topline="true">
10460 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10468 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10476 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10485 <row topline="true">
10486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10494 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10502 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10511 <row topline="true">
10512 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10537 <row topline="true">
10538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10554 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10563 <row topline="true">
10564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10580 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10589 <row topline="true" bottomline="true">
10590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10598 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10606 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10624 If the interrupt service routine is defined without
10627 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10633 a register bank or with register bank 0 (
10637 0), the compiler will save the registers used by itself on the stack upon
10638 entry and restore them at exit, however if such an interrupt service routine
10639 calls another function then the entire register bank will be saved on the
10641 This scheme may be advantageous for small interrupt service routines which
10642 have low register usage.
10645 If the interrupt service routine is defined to be using a specific register
10650 & psw are saved and restored, if such an interrupt service routine calls
10651 another function (using another register bank) then the entire register
10652 bank of the called function will be saved on the stack.
10653 This scheme is recommended for larger interrupt service routines.
10656 HC08 Interrupt Service Routines
10659 Since the number of interrupts available is chip specific and the interrupt
10660 vector table always ends at the last byte of memory, the interrupt numbers
10661 corresponds to the interrupt vectors in reverse order of address.
10662 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10663 2 will use the interrupt vector at 0xfffa, and so on.
10664 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10665 this way; instead see section
10666 \begin_inset LatexCommand \ref{sub:Startup-Code}
10670 for details on customizing startup.
10673 Z80 Interrupt Service Routines
10676 The Z80 uses several different methods for determining the correct interrupt
10677 vector depending on the hardware implementation.
10678 Therefore, SDCC ignores the optional interrupt number and does not attempt
10679 to generate an interrupt vector table.
10682 By default, SDCC generates code for a maskable interrupt, which uses an
10683 RETI instruction to return from the interrupt.
10684 To write an interrupt handler for the non-maskable interrupt, which needs
10685 an RETN instruction instead, add the
10694 void nmi_isr (void) critical interrupt
10708 Enabling and Disabling Interrupts
10711 Critical Functions and Critical Statements
10714 A special keyword may be associated with a block or a function declaring
10720 SDCC will generate code to disable all interrupts
10721 \begin_inset LatexCommand \index{interrupt}
10725 upon entry to a critical function and restore the interrupt enable to the
10726 previous state before returning.
10727 Nesting critical functions will need one additional byte on the stack
10728 \begin_inset LatexCommand \index{stack}
10737 int foo () critical
10738 \begin_inset LatexCommand \index{critical}
10763 The critical attribute maybe used with other attributes like
10773 may also be used to disable interrupts more locally:
10781 More than one statement could have been included in the block.
10784 Enabling and Disabling Interrupts directly
10788 \begin_inset LatexCommand \index{interrupt}
10792 can also be disabled and enabled directly (8051):
10797 EA = 0;\SpecialChar ~
10860 EA = 1;\SpecialChar ~
10927 On other architectures which have seperate opcodes for enabling and disabling
10928 interrupts you might want to make use of defines with inline assembly
10929 \begin_inset LatexCommand \index{Assembler routines}
10939 \begin_inset LatexCommand \index{\_asm}
10948 \begin_inset LatexCommand \index{\_endasm}
10957 #define SEI _asm\SpecialChar ~
10969 Note: it is sometimes sufficient to disable only a specific interrupt source
10971 a timer or serial interrupt by manipulating an
10974 \begin_inset LatexCommand \index{interrupt mask}
10984 Usually the time during which interrupts are disabled should be kept as
10986 This minimizes both
10991 \begin_inset LatexCommand \index{interrupt latency}
10995 (the time between the occurrence of the interrupt and the execution of
10996 the first code in the interrupt routine) and
11001 \begin_inset LatexCommand \index{interrupt jitter}
11005 (the difference between the shortest and the longest interrupt latency).
11006 These really are something different, f.e.
11007 a serial interrupt has to be served before its buffer overruns so it cares
11008 for the maximum interrupt latency, whereas it does not care about jitter.
11009 On a loudspeaker driven via a digital to analog converter which is fed
11010 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11011 a much smaller jitter will be very audible.
11014 You can reenable interrupts within an interrupt routine and on some architecture
11015 s you can make use of two (or more) levels of
11017 interrupt priorities
11020 \begin_inset LatexCommand \index{interrupt priority}
11025 On some architectures which don't support interrupt priorities these can
11026 be implemented by manipulating the interrupt mask and reenabling interrupts
11027 within the interrupt routine.
11028 Check there is sufficient space on the stack
11029 \begin_inset LatexCommand \index{stack}
11033 and don't add complexity unless you have to.
11038 \begin_inset LatexCommand \index{semaphore}
11042 locking (mcs51/ds390)
11045 Some architectures (mcs51/ds390) have an atomic
11046 \begin_inset LatexCommand \index{atomic}
11059 These type of instructions are typically used in preemptive multitasking
11060 systems, where a routine f.e.
11061 claims the use of a data structure ('acquires a lock
11062 \begin_inset LatexCommand \index{lock}
11066 on it'), makes some modifications and then releases the lock when the data
11067 structure is consistent again.
11068 The instruction may also be used if interrupt and non-interrupt code have
11069 to compete for a resource.
11070 With the atomic bit test and clear instruction interrupts
11071 \begin_inset LatexCommand \index{interrupt}
11075 don't have to be disabled for the locking operation.
11079 SDCC generates this instruction if the source follows this pattern:
11084 volatile bit resource_is_free;
11088 if (resource_is_free)
11098 resource_is_free=0;
11111 resource_is_free=1;
11118 Note, mcs51 and ds390 support only an atomic
11119 \begin_inset LatexCommand \index{atomic}
11127 instruction (as opposed to atomic bit test and
11132 Functions using private register banks
11133 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11140 Some architectures have support for quickly changing register sets.
11141 SDCC supports this feature with the
11144 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11150 attribute (which tells the compiler to use a register bank
11151 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11155 other than the default bank zero).
11156 It should only be applied to
11159 \begin_inset LatexCommand \index{interrupt}
11165 functions (see footnote below).
11166 This will in most circumstances make the generated ISR code more efficient
11167 since it will not have to save registers on the stack.
11174 attribute will have no effect on the generated code for a
11178 function (but may occasionally be useful anyway
11184 possible exception: if a function is called ONLY from 'interrupt' functions
11185 using a particular bank, it can be declared with the same 'using' attribute
11186 as the calling 'interrupt' functions.
11187 For instance, if you have several ISRs using bank one, and all of them
11188 call memcpy(), it might make sense to create a specialized version of memcpy()
11189 'using 1', since this would prevent the ISR from having to save bank zero
11190 to the stack on entry and switch to bank zero before calling the function
11197 (pending: I don't think this has been done yet)
11204 function using a non-zero bank will assume that it can trash that register
11205 bank, and will not save it.
11206 Since high-priority interrupts
11207 \begin_inset LatexCommand \index{interrupt priority}
11211 can interrupt low-priority ones on the 8051 and friends, this means that
11212 if a high-priority ISR
11216 a particular bank occurs while processing a low-priority ISR
11220 the same bank, terrible and bad things can happen.
11221 To prevent this, no single register bank should be
11225 by both a high priority and a low priority ISR.
11226 This is probably most easily done by having all high priority ISRs use
11227 one bank and all low priority ISRs use another.
11228 If you have an ISR which can change priority at runtime, you're on your
11229 own: I suggest using the default bank zero and taking the small performance
11233 It is most efficient if your ISR calls no other functions.
11234 If your ISR must call other functions, it is most efficient if those functions
11235 use the same bank as the ISR (see note 1 below); the next best is if the
11236 called functions use bank zero.
11237 It is very inefficient to call a function using a different, non-zero bank
11243 \begin_inset LatexCommand \label{sub:Startup-Code}
11248 \begin_inset LatexCommand \index{Startup code}
11255 MCS51/DS390 Startup Code
11258 The compiler inserts a call to the C routine
11260 _sdcc_external_startup()
11261 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11270 at the start of the CODE area.
11271 This routine is in the runtime library
11272 \begin_inset LatexCommand \index{Runtime library}
11277 By default this routine returns 0, if this routine returns a non-zero value,
11278 the static & global variable initialization will be skipped and the function
11279 main will be invoked.
11280 Otherwise static & global variables will be initialized before the function
11284 _sdcc_external_startup()
11286 routine to your program to override the default if you need to setup hardware
11287 or perform some other critical operation prior to static & global variable
11289 On some mcs51 variants xdata has to be explicitly enabled before it can
11290 be accessed or if the watchdog needs to be disabled, this is the place
11292 The startup code clears all internal data memory, 256 bytes by default,
11293 but from 0 to n-1 if
11306 \begin_inset LatexCommand \index{-\/-iram-size}
11313 (recommended for Chipcon CC1010).
11316 See also the compiler option
11335 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11340 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11345 about MCS51-variants.
11351 The HC08 startup code follows the same scheme as the MCS51 startup code.
11357 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11358 from sdcc/device/lib/z80/crt0.s.
11359 If you need a different startup code you can use the compiler option
11380 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11384 and provide your own crt0.o.
11388 Inline Assembler Code
11389 \begin_inset LatexCommand \index{Assembler routines}
11396 A Step by Step Introduction
11397 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11404 Starting from a small snippet of c-code this example shows for the MCS51
11405 how to use inline assembly, access variables, a function parameter and
11406 an array in xdata memory.
11407 The example uses an MCS51 here but is easily adapted for other architectures.
11408 This is a buffer routine which should be optimized:
11415 \begin_inset LatexCommand \index{far (storage class)}
11420 \begin_inset LatexCommand \index{at}
11425 \begin_inset LatexCommand \index{Aligned array}
11431 unsigned char head,tail;
11435 void to_buffer( unsigned char c )
11443 if( head != tail-1 )
11453 buf[ head++ ] = c;\SpecialChar ~
11457 /* access to a 256 byte aligned array */
11462 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11463 then a corresponding buffer.asm file is generated.
11464 We define a new function
11468 in file buffer.c in which we cut and paste the generated code, removing
11469 unwanted comments and some ':'.
11471 \begin_inset Quotes sld
11475 \begin_inset Quotes srd
11479 \begin_inset Quotes sld
11483 \begin_inset Quotes srd
11486 to the beginning and the end of the function body:
11492 /* With a cut and paste from the .asm file, we have something to start with.
11497 The function is not yet OK! (registers aren't saved) */
11499 void to_buffer_asm( unsigned char c )
11508 \begin_inset LatexCommand \index{\_asm}
11522 ;buffer.c if( head != tail-1 )
11570 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11571 \begin_inset LatexCommand \index{Aligned array}
11640 The new file buffer.c should compile with only one warning about the unreferenced
11641 function argument 'c'.
11642 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11643 (1) and finally have:
11649 unsigned char far at 0x7f00 buf[0x100];
11651 unsigned char head,tail;
11653 #define USE_ASSEMBLY (1)
11661 void to_buffer( unsigned char c )
11669 if( head != tail-1 )
11689 void to_buffer( unsigned char c )
11697 c; // to avoid warning: unreferenced function argument
11704 \begin_inset LatexCommand \index{\_asm}
11718 ; save used registers here.
11729 ; If we were still using r2,r3 we would have to push them here.
11732 ; if( head != tail-1 )
11775 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
11799 ; buf[ head++ ] = c;
11810 a,dpl \SpecialChar ~
11817 ; dpl holds lower byte of function argument
11828 dpl,_head \SpecialChar ~
11831 ; buf is 0x100 byte aligned so head can be used directly
11873 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
11885 ; restore used registers here
11898 The inline assembler code can contain any valid code understood by the assembler
11899 , this includes any assembler directives and comment lines
11905 The assembler does not like some characters like ':' or ''' in comments.
11906 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
11910 The compiler does not do any validation of the code within the
11913 \begin_inset LatexCommand \index{\_asm}
11921 Specifically it will not know which registers are used and thus register
11923 \begin_inset LatexCommand \index{push/pop}
11927 has to be done manually.
11931 It is recommended that each assembly instruction (including labels) be placed
11932 in a separate line (as the example shows).
11946 \begin_inset LatexCommand \index{-\/-peep-asm}
11952 command line option is used, the inline assembler code will be passed through
11953 the peephole optimizer
11954 \begin_inset LatexCommand \index{Peephole optimizer}
11959 There are only a few (if any) cases where this option makes sense, it might
11960 cause some unexpected changes in the inline assembler code.
11961 Please go through the peephole optimizer rules defined in file
11965 before using this option.
11969 \begin_inset LatexCommand \label{sub:Naked-Functions}
11974 \begin_inset LatexCommand \index{Naked functions}
11981 A special keyword may be associated with a function declaring it as
11984 \begin_inset LatexCommand \index{\_naked}
11995 function modifier attribute prevents the compiler from generating prologue
11996 \begin_inset LatexCommand \index{function prologue}
12001 \begin_inset LatexCommand \index{function epilogue}
12005 code for that function.
12006 This means that the user is entirely responsible for such things as saving
12007 any registers that may need to be preserved, selecting the proper register
12008 bank, generating the
12012 instruction at the end, etc.
12013 Practically, this means that the contents of the function must be written
12014 in inline assembler.
12015 This is particularly useful for interrupt functions, which can have a large
12016 (and often unnecessary) prologue/epilogue.
12017 For example, compare the code generated by these two functions:
12023 \begin_inset LatexCommand \index{volatile}
12027 data unsigned char counter;
12031 void simpleInterrupt(void) interrupt
12032 \begin_inset LatexCommand \index{interrupt}
12050 void nakedInterrupt(void) interrupt 2 _naked
12059 \begin_inset LatexCommand \index{\_asm}
12076 _counter ; does not change flags, no need to save psw
12088 ; MUST explicitly include ret or reti in _naked function.
12095 \begin_inset LatexCommand \index{\_endasm}
12104 For an 8051 target, the generated simpleInterrupt looks like:
12245 whereas nakedInterrupt looks like:
12260 _counter ; does not change flags, no need to save psw
12278 ; MUST explicitly include ret or reti in _naked function
12281 The related directive #pragma exclude
12282 \begin_inset LatexCommand \index{\#pragma exclude}
12286 allows a more fine grained control over pushing & popping
12287 \begin_inset LatexCommand \index{push/pop}
12294 While there is nothing preventing you from writing C code inside a
12298 function, there are many ways to shoot yourself in the foot doing this,
12299 and it is recommended that you stick to inline assembler.
12302 Use of Labels within Inline Assembler
12305 SDCC allows the use of in-line assembler with a few restrictions regarding
12307 In older versions of the compiler all labels defined within inline assembler
12316 where nnnn is a number less than 100 (which implies a limit of utmost 100
12317 inline assembler labels
12331 \begin_inset LatexCommand \index{\_asm}
12361 \begin_inset LatexCommand \index{\_endasm}
12368 Inline assembler code cannot reference any C-Labels, however it can reference
12370 \begin_inset LatexCommand \index{Labels}
12374 defined by the inline assembler, e.g.:
12399 ; some assembler code
12419 /* some more c code */
12421 clabel:\SpecialChar ~
12423 /* inline assembler cannot reference this label */
12435 $0003: ;label (can be referenced by inline assembler only)
12447 /* some more c code */
12452 In other words inline assembly code can access labels defined in inline
12453 assembly within the scope of the function.
12454 The same goes the other way, i.e.
12455 labels defines in inline assembly can not be accessed by C statements.
12458 Interfacing with Assembler Code
12459 \begin_inset LatexCommand \index{Assembler routines}
12466 Global Registers used for Parameter Passing
12467 \begin_inset LatexCommand \index{Parameter passing}
12474 The compiler always uses the global registers
12477 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12482 \begin_inset LatexCommand \index{DPTR}
12487 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12496 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12502 to pass the first parameter to a routine.
12503 The second parameter onwards is either allocated on the stack (for reentrant
12514 -stack-auto is used) or in data / xdata memory (depending on the memory
12519 Assembler Routine (non-reentrant)
12522 In the following example
12523 \begin_inset LatexCommand \index{reentrant}
12528 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12532 the function c_func calls an assembler routine asm_func, which takes two
12534 \begin_inset LatexCommand \index{function parameter}
12543 extern int asm_func(unsigned char, unsigned char);
12547 int c_func (unsigned char i, unsigned char j)
12555 return asm_func(i,j);
12569 return c_func(10,9);
12574 The corresponding assembler function is:
12579 .globl _asm_func_PARM_2
12680 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12697 Note here that the return values
12698 \begin_inset LatexCommand \index{return value}
12702 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
12704 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12705 b' & 'acc' for four byte values.
12708 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12709 the parameter number starting from 1, and counting from the left.
12710 The first parameter is passed in
12711 \begin_inset Quotes eld
12715 \begin_inset Quotes erd
12718 for a one byte parameter,
12719 \begin_inset Quotes eld
12723 \begin_inset Quotes erd
12727 \begin_inset Quotes eld
12731 \begin_inset Quotes erd
12734 for three bytes and
12735 \begin_inset Quotes eld
12739 \begin_inset Quotes erd
12742 for a four bytes parameter.
12743 The variable name for the second parameter will be _<function_name>_PARM_2.
12747 Assemble the assembler routine with the following command:
12754 asx8051 -losg asmfunc.asm
12761 Then compile and link the assembler routine to the C source file with the
12769 sdcc cfunc.c asmfunc.rel
12772 Assembler Routine (reentrant)
12776 \begin_inset LatexCommand \index{reentrant}
12781 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12785 the second parameter
12786 \begin_inset LatexCommand \index{function parameter}
12790 onwards will be passed on the stack, the parameters are pushed from right
12792 after the call the leftmost parameter will be on the top of the stack.
12793 Here is an example:
12798 extern int asm_func(unsigned char, unsigned char);
12802 int c_func (unsigned char i, unsigned char j) reentrant
12810 return asm_func(i,j);
12824 return c_func(10,9);
12829 The corresponding assembler routine is:
12929 The compiling and linking procedure remains the same, however note the extra
12930 entry & exit linkage required for the assembler code, _bp is the stack
12931 frame pointer and is used to compute the offset into the stack for parameters
12932 and local variables.
12936 \begin_inset LatexCommand \index{int (16 bit)}
12941 \begin_inset LatexCommand \index{long (32 bit)}
12948 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
12949 multiplication and modulus operations are implemented by support routines.
12950 These support routines are all developed in ANSI-C to facilitate porting
12951 to other MCUs, although some model specific assembler optimizations are
12953 The following files contain the described routines, all of them can be
12954 found in <installdir>/share/sdcc/lib.
12960 \begin_inset Tabular
12961 <lyxtabular version="3" rows="11" columns="2">
12963 <column alignment="center" valignment="top" leftline="true" width="0">
12964 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12965 <row topline="true" bottomline="true">
12966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12987 <row topline="true">
12988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13001 16 bit multiplication
13005 <row topline="true">
13006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13019 signed 16 bit division (calls _divuint)
13023 <row topline="true">
13024 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13037 unsigned 16 bit division
13041 <row topline="true">
13042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13050 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13055 signed 16 bit modulus (calls _moduint)
13059 <row topline="true">
13060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13073 unsigned 16 bit modulus
13077 <row topline="true">
13078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13086 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13091 32 bit multiplication
13095 <row topline="true">
13096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13109 signed 32 division (calls _divulong)
13113 <row topline="true">
13114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13127 unsigned 32 division
13131 <row topline="true">
13132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13145 signed 32 bit modulus (calls _modulong)
13149 <row topline="true" bottomline="true">
13150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13158 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13163 unsigned 32 bit modulus
13176 Since they are compiled as
13181 \begin_inset LatexCommand \index{reentrant}
13186 \begin_inset LatexCommand \index{interrupt}
13190 service routines should not do any of the above operations.
13191 If this is unavoidable then the above routines will need to be compiled
13205 \begin_inset LatexCommand \index{-\/-stack-auto}
13211 option, after which the source program will have to be compiled with
13224 \begin_inset LatexCommand \index{-\/-int-long-reent}
13231 Notice that you don't have to call these routines directly.
13232 The compiler will use them automatically every time an integer operation
13236 Floating Point Support
13237 \begin_inset LatexCommand \index{Floating point support}
13244 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13245 floating point support routines are derived from gcc's floatlib.c and consist
13246 of the following routines:
13254 \begin_inset Tabular
13255 <lyxtabular version="3" rows="17" columns="2">
13257 <column alignment="center" valignment="top" leftline="true" width="0">
13258 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13259 <row topline="true" bottomline="true">
13260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13277 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13286 <row topline="true">
13287 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13304 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13318 add floating point numbers
13322 <row topline="true">
13323 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13340 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13354 subtract floating point numbers
13358 <row topline="true">
13359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13376 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13390 divide floating point numbers
13394 <row topline="true">
13395 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13412 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13426 multiply floating point numbers
13430 <row topline="true">
13431 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13448 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13462 convert floating point to unsigned char
13466 <row topline="true">
13467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13484 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13498 convert floating point to signed char
13502 <row topline="true">
13503 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13520 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13534 convert floating point to unsigned int
13538 <row topline="true">
13539 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13556 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13570 convert floating point to signed int
13574 <row topline="true">
13575 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13601 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13615 convert floating point to unsigned long
13619 <row topline="true">
13620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13651 convert floating point to signed long
13655 <row topline="true">
13656 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13673 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13687 convert unsigned char to floating point
13691 <row topline="true">
13692 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13723 convert char to floating point number
13727 <row topline="true">
13728 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13745 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13759 convert unsigned int to floating point
13763 <row topline="true">
13764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13781 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13795 convert int to floating point numbers
13799 <row topline="true">
13800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13817 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13831 convert unsigned long to floating point number
13835 <row topline="true" bottomline="true">
13836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13867 convert long to floating point number
13880 These support routines are developed in ANSI-C so there is room for space
13881 and speed improvement.
13882 Note if all these routines are used simultaneously the data space might
13884 For serious floating point usage it is recommended that the large model
13886 Also notice that you don't have to call this routines directly.
13887 The compiler will use them automatically every time a floating point operation
13892 \begin_inset LatexCommand \index{Libraries}
13901 <pending: this is messy and incomplete - a little more information is in
13902 sdcc/doc/libdoc.txt
13907 Compiler support routines (_gptrget, _mulint etc.)
13910 Stdclib functions (puts, printf, strcat etc.)
13911 \layout Subsubsection
13917 \begin_inset LatexCommand \index{<stdio.h>}
13921 As usual on embedded systems you have to provide your own
13924 \begin_inset LatexCommand \index{getchar()}
13933 \begin_inset LatexCommand \index{putchar()}
13940 SDCC does not know whether the system connects to a serial line with or
13941 without handshake, LCD, keyboard or other device.
13942 You'll find examples for serial routines f.e.
13943 in sdcc/device/lib.
13949 \begin_inset LatexCommand \index{printf()}
13959 does not support float (except on ds390).
13960 To enable this recompile it with the option
13973 \begin_inset LatexCommand \index{USE\_FLOATS}
13979 on the command line.
13993 \begin_inset LatexCommand \index{-\/-model-large}
13999 for the mcs51 port, since this uses a lot of memory.
14002 If you're short on memory you might want to use
14015 For the mcs51 there is an assembly version
14019 which should fit the requirements of many embedded systems (by unsetting
14020 #defines it can be customized to
14024 support long variables and field widths).
14027 Math functions (sin, pow, sqrt etc.)
14034 \begin_inset LatexCommand \index{Libraries}
14038 included in SDCC should have a license at least as liberal as the GNU Lesser
14039 General Public License
14040 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14051 license statements for the libraries are missing.
14052 sdcc/device/lib/ser_ir.c
14056 come with a GPL (as opposed to LGPL) License - this will not be liberal
14057 enough for many embedded programmers.
14060 If you have ported some library or want to share experience about some code
14062 falls into any of these categories Busses (I
14063 \begin_inset Formula $^{\textrm{2}}$
14066 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14067 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14068 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14069 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14074 would certainly like to hear about it.
14075 Programmers coding for embedded systems are not especially famous for being
14076 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14077 e these references are very valuable.
14078 Let's help to create a climate where information is shared.
14084 MCS51 Memory Models
14085 \begin_inset LatexCommand \index{Memory model}
14090 \begin_inset LatexCommand \index{MCS51 memory model}
14095 \layout Subsubsection
14100 SDCC allows two memory models for MCS51 code,
14109 Modules compiled with different memory models should
14113 be combined together or the results would be unpredictable.
14114 The library routines supplied with the compiler are compiled as both small
14116 The compiled library modules are contained in separate directories as small
14117 and large so that you can link to either set.
14121 When the large model is used all variables declared without a storage class
14122 will be allocated into the external ram, this includes all parameters and
14123 local variables (for non-reentrant
14124 \begin_inset LatexCommand \index{reentrant}
14129 When the small model is used variables without storage class are allocated
14130 in the internal ram.
14133 Judicious usage of the processor specific storage classes
14134 \begin_inset LatexCommand \index{Storage class}
14138 and the 'reentrant' function type will yield much more efficient code,
14139 than using the large model.
14140 Several optimizations are disabled when the program is compiled using the
14141 large model, it is therefore recommended that the small model be used unless
14142 absolutely required.
14143 \layout Subsubsection
14146 \begin_inset LatexCommand \label{sub:External-Stack}
14151 \begin_inset LatexCommand \index{stack}
14156 \begin_inset LatexCommand \index{External stack (mcs51)}
14163 The external stack (-
14174 \begin_inset LatexCommand \index{-\/-xstack}
14178 ) is located in pdata
14179 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14183 memory (usually at the start of the external ram segment) and uses all
14184 unused space in pdata (max.
14196 -xstack option is used to compile the program, the parameters and local
14198 \begin_inset LatexCommand \index{local variables}
14202 of all reentrant functions are allocated in this area.
14203 This option is provided for programs with large stack space requirements.
14204 When used with the -
14215 \begin_inset LatexCommand \index{-\/-stack-auto}
14219 option, all parameters and local variables are allocated on the external
14220 stack (note: support libraries will need to be recompiled with the same
14222 There is a predefined target in the library makefile).
14225 The compiler outputs the higher order address byte of the external ram segment
14227 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14232 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14236 ), therefore when using the External Stack option, this port
14240 be used by the application program.
14244 \begin_inset LatexCommand \index{Memory model}
14249 \begin_inset LatexCommand \index{DS390 memory model}
14256 The only model supported is Flat 24
14257 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14262 This generates code for the 24 bit contiguous addressing mode of the Dallas
14264 In this mode, up to four meg of external RAM or code space can be directly
14266 See the data sheets at www.dalsemi.com for further information on this part.
14270 Note that the compiler does not generate any code to place the processor
14271 into 24 bitmode (although
14275 in the ds390 libraries will do that for you).
14281 \begin_inset LatexCommand \index{Tinibios (DS390)}
14285 , the boot loader or similar code must ensure that the processor is in 24
14286 bit contiguous addressing mode before calling the SDCC startup code.
14304 option, variables will by default be placed into the XDATA segment.
14309 Segments may be placed anywhere in the 4 meg address space using the usual
14321 Note that if any segments are located above 64K, the -r flag must be passed
14322 to the linker to generate the proper segment relocations, and the Intel
14323 HEX output format must be used.
14324 The -r flag can be passed to the linker by using the option
14328 on the SDCC command line.
14329 However, currently the linker can not handle code segments > 64k.
14333 \begin_inset LatexCommand \index{Pragmas}
14340 SDCC supports the following #pragma directives:
14344 \begin_inset LatexCommand \index{\#pragma save}
14348 - this will save all current options to the save/restore stack.
14349 See #pragma\SpecialChar ~
14354 \begin_inset LatexCommand \index{\#pragma restore}
14358 - will restore saved options from the last save.
14359 saves & restores can be nested.
14360 SDCC uses a save/restore stack: save pushes current options to the stack,
14361 restore pulls current options from the stack.
14362 See #pragma\SpecialChar ~
14369 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14374 \begin_inset LatexCommand \index{function prologue}
14378 function1[,function2[,function3...]] - The compiler by default uses a caller
14379 saves convention for register saving across function calls, however this
14380 can cause unnecessary register pushing & popping
14381 \begin_inset LatexCommand \index{push/pop}
14385 when calling small functions from larger functions.
14386 This option can be used to switch off the register saving convention for
14387 the function names specified.
14388 The compiler will not save registers when calling these functions, extra
14389 code need to be manually inserted at the entry & exit for these functions
14390 to save & restore the registers used by these functions, this can SUBSTANTIALLY
14391 reduce code & improve run time performance of the generated code.
14392 In the future the compiler (with inter procedural analysis) may be able
14393 to determine the appropriate scheme to use for each function call.
14404 -callee-saves command line option is used, the function names specified
14405 in #pragma\SpecialChar ~
14407 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14411 is appended to the list of functions specified in the command line.
14415 \begin_inset LatexCommand \index{\#pragma exclude}
14419 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
14420 of pairs of push/pop
14421 \begin_inset LatexCommand \index{push/pop}
14430 \begin_inset LatexCommand \index{interrupt}
14443 The directive should be placed immediately before the ISR function definition
14444 and it affects ALL ISR functions following it.
14445 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
14446 exclude\SpecialChar ~
14448 \begin_inset LatexCommand \index{\#pragma exclude}
14453 See also the related keyword _naked
14454 \begin_inset LatexCommand \index{\_naked}
14462 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
14466 - the compiler will not warn you anymore for obvious mistakes, you'r on
14470 disable_warning <nnnn>
14471 \begin_inset LatexCommand \index{\#pragma disable\_warning}
14475 - the compiler will not warn you anymore about warning number <nnnn>.
14479 \begin_inset LatexCommand \index{\#pragma nogcse}
14483 - will stop global common subexpression elimination.
14487 \begin_inset LatexCommand \index{\#pragma noinduction}
14491 - will stop loop induction optimizations.
14495 \begin_inset LatexCommand \index{\#pragma noinvariant}
14499 - will not do loop invariant optimizations.
14500 For more details see Loop Invariants in section
14501 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14509 \begin_inset LatexCommand \index{\#pragma noiv}
14513 - Do not generate interrupt
14514 \begin_inset LatexCommand \index{interrupt}
14518 vector table entries for all ISR functions defined after the pragma.
14519 This is useful in cases where the interrupt vector table must be defined
14520 manually, or when there is a secondary, manually defined interrupt vector
14522 for the autovector feature of the Cypress EZ-USB FX2).
14523 More elegantly this can be achieved by obmitting the optional interrupt
14524 number after the interrupt keyword, see section
14525 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14534 \begin_inset LatexCommand \index{\#pragma nojtbound}
14538 - will not generate code for boundary value checking, when switch statements
14539 are turned into jump-tables (dangerous).
14540 For more details see section
14541 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14549 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14553 - Will not do loop reversal optimization
14557 \begin_inset LatexCommand \index{\#pragma nooverlay}
14561 - the compiler will not overlay the parameters and local variables of a
14566 \begin_inset LatexCommand \index{\#pragma stackauto}
14581 \begin_inset LatexCommand \index{-\/-stack-auto}
14586 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
14590 Parameters and Local Variables.
14594 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
14598 - The compiler will optimize code generation towards fast code, possibly
14599 at the expense of code size.
14603 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
14607 - The compiler will optimize code generation towards compact code, possibly
14608 at the expense of code speed.
14612 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
14616 - The compiler will attempt to generate code that is both compact and fast,
14617 as long as meeting one goal is not a detriment to the other (this is the
14623 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
14627 - Generally follow the C89 standard, but allow SDCC features that conflict
14628 with the standard (default).
14632 \begin_inset LatexCommand \index{\#pragma std\_c89}
14636 - Follow the C89 standard and disable SDCC features that conflict with the
14641 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
14645 - Generally follow the C99 standard, but allow SDCC features that conflict
14646 with the standard (incomplete support).
14650 \begin_inset LatexCommand \index{\#pragma std\_c99}
14654 - Follow the C99 standard and disable SDCC features that conflict with the
14655 standard (incomplete support).
14658 SDCPP supports the following #pragma directives:
14662 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
14666 (+ | -) - switch _asm _endasm block preprocessing on / off.
14670 The pragma's are intended to be used to turn-on or off certain optimizations
14671 which might cause the compiler to generate extra stack / data space to
14672 store compiler generated temporary variables.
14673 This usually happens in large functions.
14674 Pragma directives should be used as shown in the following example, they
14675 are used to control options & optimizations for a given function; pragmas
14676 should be placed before and/or after a function, placing pragma's inside
14677 a function body could have unpredictable results.
14683 \begin_inset LatexCommand \index{\#pragma save}
14694 /* save the current settings */
14697 \begin_inset LatexCommand \index{\#pragma nogcse}
14706 /* turnoff global subexpression elimination */
14708 #pragma noinduction
14709 \begin_inset LatexCommand \index{\#pragma noinduction}
14713 /* turn off induction optimizations */
14736 \begin_inset LatexCommand \index{\#pragma restore}
14740 /* turn the optimizations back on */
14743 The compiler will generate a warning message when extra space is allocated.
14744 It is strongly recommended that the save and restore pragma's be used when
14745 changing options for a function.
14748 Defines Created by the Compiler
14751 The compiler creates the following #defines
14752 \begin_inset LatexCommand \index{\#defines}
14757 \begin_inset LatexCommand \index{Defines created by the compiler}
14767 \begin_inset Tabular
14768 <lyxtabular version="3" rows="10" columns="2">
14770 <column alignment="center" valignment="top" leftline="true" width="0">
14771 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14772 <row topline="true" bottomline="true">
14773 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14794 <row topline="true">
14795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14801 \begin_inset LatexCommand \index{SDCC}
14808 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14813 this Symbol is always defined
14817 <row topline="true">
14818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14824 \begin_inset LatexCommand \index{SDCC\_mcs51}
14829 \begin_inset LatexCommand \index{SDCC\_ds390}
14834 \begin_inset LatexCommand \index{SDCC\_z80}
14841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14846 depending on the model used (e.g.: -mds390
14850 <row topline="true">
14851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14857 \begin_inset LatexCommand \index{\_\_mcs51}
14862 \begin_inset LatexCommand \index{\_\_ds390}
14867 \begin_inset LatexCommand \index{\_\_hc08}
14872 \begin_inset LatexCommand \index{\_\_z80}
14879 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14884 depending on the model used (e.g.
14889 <row topline="true">
14890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14896 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
14903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14926 <row topline="true">
14927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14933 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
14940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14963 <row topline="true">
14964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14970 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
14977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15000 <row topline="true">
15001 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15007 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15037 <row topline="true">
15038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15044 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15051 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15064 <row topline="true" bottomline="true">
15065 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15071 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15078 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15098 Notes on supported Processors
15102 \begin_inset LatexCommand \label{sub:MCS51-variants}
15107 \begin_inset LatexCommand \index{MCS51 variants}
15114 MCS51 processors are available from many vendors and come in many different
15116 While they might differ considerably in respect to Special Function Registers
15117 the core MCS51 is usually not modified or is kept compatible.
15121 pdata access by SFR
15124 With the upcome of devices with internal xdata and flash memory devices
15126 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15130 as dedicated I/O port is becoming more popular.
15131 Switching the high byte for pdata
15132 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15136 access which was formerly done by port P2 is then achieved by a Special
15138 \begin_inset LatexCommand \index{sfr}
15143 In well-established MCS51 tradition the address of this
15147 is where the chip designers decided to put it.
15148 Needless to say that they didn't agree on a common name either.
15149 So that the startup code can correctly initialize xdata variables, you
15150 should define an sfr with the name _XPAGE
15153 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15159 at the appropriate location if the default, port P2, is not used for this.
15165 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15170 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15175 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15178 For more exotic implementations further customizations may be needed.
15180 \begin_inset LatexCommand \ref{sub:Startup-Code}
15184 for other possibilities.
15187 Other Features available by SFR
15190 Some MCS51 variants offer features like Double DPTR
15191 \begin_inset LatexCommand \index{DPTR}
15195 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
15196 These are currently not used for the MCS51 port.
15197 If you absolutely need them you can fall back to inline assembly or submit
15204 The DS80C400 microcontroller has a rich set of peripherals.
15205 In its built-in ROM library it includes functions to access some of the
15206 features, among them is a TCP stack with IP4 and IP6 support.
15207 Library headers (currently in beta status) and other files are provided
15211 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
15219 The Z80 and gbz80 port
15222 SDCC can target both the Zilog
15223 \begin_inset LatexCommand \index{Z80}
15227 and the Nintendo Gameboy's Z80-like gbz80
15228 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
15233 The Z80 port is passed through the same
15236 \begin_inset LatexCommand \index{Regression test}
15242 as the MCS51 and DS390 ports, so floating point support, support for long
15243 variables and bitfield support is fine.
15244 See mailing lists and forums about interrupt routines.
15247 As always, the code is the authoritative reference - see z80/ralloc.c and
15250 \begin_inset LatexCommand \index{stack}
15254 frame is similar to that generated by the IAR Z80 compiler.
15255 IX is used as the base pointer, HL and IY are used as a temporary registers,
15256 and BC and DE are available for holding variables.
15258 \begin_inset LatexCommand \index{return value}
15262 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
15264 The gbz80 port use the same set of registers for the return values, but
15265 in a different order of significance: E (one byte), DE (two bytes), or
15272 The port to the Motorola HC08
15273 \begin_inset LatexCommand \index{HC08}
15277 family has been added in October 2003, and is still undergoing some basic
15279 The code generator is complete, but the register allocation is still quite
15281 Some of the SDCC's standard C library functions have embedded non-HC08
15282 inline assembly and so are not yet usable.
15293 \begin_inset LatexCommand \index{PIC14}
15297 port still requires a major effort from the development community.
15298 However it can work for very simple code.
15301 C code and 14bit PIC code page
15302 \begin_inset LatexCommand \index{code page (pic14)}
15307 \begin_inset LatexCommand \index{RAM bank (pic14)}
15314 The linker organizes allocation for the code page and RAM banks.
15315 It does not have intimate knowledge of the code flow.
15316 It will put all the code section of a single asm file into a single code
15318 In order to make use of multiple code pages, separate asm files must be
15320 The compiler treats all functions of a single C file as being in the same
15321 code page unless it is non static.
15322 The compiler treats all local variables of a single C file as being in
15323 the same RAM bank unless it is an extern.
15327 To get the best follow these guide lines:
15330 make local functions static, as non static functions require code page selection
15334 Make local variables static as extern variables require RAM bank selection
15338 For devices that have multiple code pages it is more efficient to use the
15339 same number of files as pages, i.e.
15340 for the 16F877 use 4 separate files and i.e.
15341 for the 16F874 use 2 separate files.
15342 This way the linker can put the code for each file into different code
15343 pages and the compiler can allocate reusable variables more efficiently
15344 and there's less page selection overhead.
15345 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
15346 instruction set) use 'unsigned char' whereever possible instead of 'int'.
15349 Creating a device include file
15352 For generating a device include file use the support perl script inc2h.pl
15353 kept in directory support/script.
15359 For the interrupt function, use the keyword 'interrupt'
15360 \begin_inset LatexCommand \index{interrupt}
15364 with level number of 0 (PIC14 only has 1 interrupt so this number is only
15365 there to avoid a syntax error - it ought to be fixed).
15371 void Intr(void) interrupt 0
15377 T0IF = 0; /* Clear timer interrupt */
15382 Linking and assembling
15385 For assembling you can use either GPUTILS'
15386 \begin_inset LatexCommand \index{gputils (pic tools)}
15390 gpasm.exe or MPLAB's mpasmwin.exe.
15391 GPUTILS is available from
15392 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
15397 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
15398 If you use MPLAB and an interrupt function then the linker script file
15399 vectors section will need to be enlarged to link with mplink.
15422 sdcc -S -V -mpic14 -p16F877 $<
15436 $(PRJ).hex: $(OBJS)
15446 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
15468 sdcc -S -V -mpic14 -p16F877 $<
15478 mpasmwin /q /o $*.asm
15482 $(PRJ).hex: $(OBJS)
15492 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
15495 Please note that indentations within a
15499 have to be done with a tabulator character.
15503 \begin_inset LatexCommand \index{PIC16}
15511 \begin_inset LatexCommand \index{PIC16}
15515 port is the portion of SDCC that is responsible to produce code for the
15517 \begin_inset LatexCommand \index{Microchip}
15521 (TM) microcontrollers with 16 bit core.
15522 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
15523 Currently supported devices are:
15527 \begin_inset Tabular
15528 <lyxtabular version="3" rows="4" columns="6">
15530 <column alignment="center" valignment="top" leftline="true" width="0">
15531 <column alignment="center" valignment="top" leftline="true" width="0">
15532 <column alignment="center" valignment="top" leftline="true" width="0">
15533 <column alignment="center" valignment="top" leftline="true" width="0">
15534 <column alignment="center" valignment="top" leftline="true" width="0">
15535 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15536 <row topline="true">
15537 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15577 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15586 <row topline="true">
15587 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15603 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15611 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15619 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15636 <row topline="true">
15637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15653 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15661 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15669 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15677 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15686 <row topline="true" bottomline="true">
15687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15711 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15718 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15743 PIC16 port supports the standard command line arguments as supposed, with
15744 the exception of certain cases that will be mentioned in the following
15747 \labelwidthstring 00.00.0000
15759 -callee-saves See -
15771 \labelwidthstring 00.00.0000
15783 -all-callee-saves All function arguments are passed on stack by default.
15786 There is no need to specify this in the command line.
15788 \labelwidthstring 00.00.0000
15800 -fommit-frame-pointer Frame pointer will be omitted when the function uses
15801 no local variables.
15804 Port Specific Options
15805 \begin_inset LatexCommand \index{Options PIC16}
15812 The port specific options appear after the global options in the sdcc --help
15814 \layout Subsubsection
15819 General options enable certain port features and optimizations.
15821 \labelwidthstring 00.00.0000
15833 -stack-model=[model] Used in conjuction with the command above.
15834 Defines the stack model to be used, valid stack models are :
15837 \labelwidthstring 00.00.0000
15843 Selects small stack model.
15844 8 bit stack and frame pointers.
15845 Supports 256 bytes stack size.
15847 \labelwidthstring 00.00.0000
15853 Selects large stack model.
15854 16 bit stack and frame pointers.
15855 Supports 65536 bytes stack size.
15858 \labelwidthstring 00.00.0000
15870 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
15871 unitialized data variables with [kword].
15872 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
15874 \labelwidthstring 00.00.0000
15886 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
15887 Useful for bootloaders.
15889 \labelwidthstring 00.00.0000
15901 -asm= sets the full path and name of an external assembler to call.
15903 \labelwidthstring 00.00.0000
15915 -link= sets the full path and name of an external linker to call.
15917 \labelwidthstring 00.00.0000
15929 -mplab-comp MPLAB compatibility option.
15930 Currently only suppresses special gpasm directives.
15931 \layout Subsubsection
15933 Optimization Options
15935 \labelwidthstring 00.00.0000
15947 -optimize-goto Try to use (conditional) BRA instead of GOTO
15949 \labelwidthstring 00.00.0000
15961 -optimize-cmp Try to optimize some compares.
15963 \labelwidthstring 00.00.0000
15975 -obanksel=nn Set optimization level for inserting BANKSELs.
15980 \labelwidthstring 00.00.0000
15984 \labelwidthstring 00.00.0000
15986 1 checks previous used register and if it is the same then does not emit
15987 BANKSEL, accounts only for labels.
15989 \labelwidthstring 00.00.0000
15991 2 tries to check the location of (even different) symbols and removes BANKSELs
15992 if they are in the same bank.
15997 Important: There might be problems if the linker script has data sections
15998 across bank borders!
16000 \layout Subsubsection
16004 \labelwidthstring 00.00.0000
16016 -nodefaultlibs do not link default libraries when linking
16018 \labelwidthstring 00.00.0000
16030 -no-crt Don't link the default run-time modules
16032 \labelwidthstring 00.00.0000
16044 -use-crt= Use a custom run-time module instead of the defaults.
16045 \layout Subsubsection
16050 Debugging options enable extra debugging information in the output files.
16052 \labelwidthstring 00.00.0000
16064 -debug-xtra Similar to -
16075 \begin_inset LatexCommand \index{-\/-debug}
16079 , but dumps more information.
16081 \labelwidthstring 00.00.0000
16093 -debug-ralloc Force register allocator to dump <source>.d file with debugging
16095 <source> is the name of the file compiled.
16097 \labelwidthstring 00.00.0000
16109 -pcode-verbose Enable pcode debugging information in translation.
16111 \labelwidthstring 00.00.0000
16123 -denable-peeps Force the usage of peepholes.
16126 \labelwidthstring 00.00.0000
16138 -gstack Trace push/pops for stack pointer overflow
16140 \labelwidthstring 00.00.0000
16152 -call-tree dump call tree in .calltree file
16155 Enviromental Variables
16158 There is a number of enviromental variables that can be used when running
16159 SDCC to enable certain optimizations or force a specific program behaviour.
16160 these variables are primarily for debugging purposes so they can be enabled/dis
16164 Currently there is only two such variables available:
16166 \labelwidthstring 00.00.0000
16168 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
16169 bitfields is optimized by directly loading FSR0 with the address of the
16170 bitfield structure.
16171 Normally SDCC will cast the bitfield structure to a bitfield pointer and
16173 This step saves data ram and code space for functions that perform heavy
16176 80 bytes of code space are saved when compiling malloc.c with this option).
16179 \labelwidthstring 00.00.0000
16181 NO_REG_OPT do not perform pCode registers optimization.
16182 This should be used for debugging purposes.
16183 In some where bugs in the pcode optimizer are found, users can benefit
16184 from temporarily disabling the optimizer until the bug is fixed.
16187 Preprocessor Macros
16190 PIC16 port defines the following preprocessor macros while translating a
16195 \begin_inset Tabular
16196 <lyxtabular version="3" rows="6" columns="2">
16198 <column alignment="center" valignment="top" leftline="true" width="0">
16199 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16200 <row topline="true" bottomline="true">
16201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16218 <row topline="true">
16219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16232 Port identification
16236 <row topline="true">
16237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16255 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16260 Port identification (same as above)
16264 <row topline="true">
16265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16278 MCU Identification.
16283 is the microcontrol identification number, i.e.
16288 <row topline="true">
16289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16307 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16312 MCU Identification (same as above)
16316 <row topline="true" bottomline="true">
16317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16325 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16330 nnn = SMALL or LARGE respectively according to the stack model used
16341 In addition the following macros are defined when calling assembler:
16345 \begin_inset Tabular
16346 <lyxtabular version="3" rows="4" columns="2">
16348 <column alignment="center" valignment="top" leftline="true" width="0">
16349 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16350 <row topline="true" bottomline="true">
16351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16368 <row topline="true">
16369 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16377 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16382 MCU Identification.
16387 is the microcontrol identification number, i.e.
16392 <row topline="true">
16393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16401 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16406 nnn = SMALL or LARGE respectively according to the memory model used for
16411 <row topline="true" bottomline="true">
16412 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16420 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16425 nnn = SMALL or LARGE respectively according to the stack model used
16440 \begin_inset LatexCommand \index{PIC16}
16444 port uses the following directories for searching header files and libraries.
16448 \begin_inset Tabular
16449 <lyxtabular version="3" rows="3" columns="4">
16451 <column alignment="center" valignment="top" leftline="true" width="0">
16452 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16453 <column alignment="center" valignment="top" width="0">
16454 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16455 <row topline="true" bottomline="true">
16456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16480 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16489 <row topline="true">
16490 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16495 PREFIX/sdcc/include/pic16
16498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16503 PIC16 specific headers
16506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16514 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16523 <row topline="true" bottomline="true">
16524 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16529 PREFIX/sdcc/lib/pic16
16532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16537 PIC16 specific libraries
16540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16565 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
16572 PIC16 port currently supports the following pragmas:
16574 \labelwidthstring 00.00.0000
16576 stack pragma stack forces the code generator to initialize the stack & frame
16577 pointers at a specific address.
16578 This is an adhoc solution for cases where no STACK directive is available
16579 in the linker script or gplink is not instructed to create a stack section.
16581 The stack pragma should be used only once in a project.
16582 Multiple pragmas may result in indeterminate behaviour of the program.
16588 The old format (ie.
16589 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
16590 cross page boundaries (or even exceed the available data RAM) and crash
16592 Make sure that stack does not cross page boundaries when using the SMALL
16598 The format is as follows:
16601 #pragma stack bottom_address [stack_size]
16608 is the lower bound of the stack section.
16609 The stack pointer initially will point at address (bottom_address+stack_size-1).
16617 /* initializes stack of 100 bytes at RAM address 0x200 */
16620 #pragma stack 0x200 100
16623 If the stack_size field is omitted then a stack is created with the default
16625 This size might be enough for most programs, but its not enough for operations
16626 with deep function nesting or excessive stack usage.
16628 \labelwidthstring 00.00.0000
16632 This pragma is deprecated.
16633 Its use will cause a warning message to be issued.
16639 \labelwidthstring 00.00.0000
16641 code place a function symbol at static FLASH address
16649 /* place function test_func at 0x4000 */
16652 #pragma code test_func 0x4000
16656 \labelwidthstring 00.00.0000
16658 library instructs the linker to use a library module.
16663 #pragma library module_name
16670 can be any library or object file (including its path).
16671 Note that there are four reserved keywords which have special meaning.
16676 \begin_inset Tabular
16677 <lyxtabular version="3" rows="6" columns="3">
16679 <column alignment="center" valignment="top" leftline="true" width="0">
16680 <column alignment="block" valignment="top" leftline="true" width="20page%">
16681 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
16682 <row topline="true" bottomline="true">
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">
16708 <row topline="true">
16709 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16724 ignore all library pragmas
16727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16738 <row topline="true">
16739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16770 <row topline="true">
16771 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16781 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16786 link the Math libarary
16789 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16802 <row topline="true">
16803 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16818 link the I/O library
16821 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16834 <row topline="true" bottomline="true">
16835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16850 link the debug library
16853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16872 * is the device number, i.e.
16873 452 for PIC18F452 MCU.
16876 This feature allows for linking with specific libraries withoug having to
16877 explicit name them in the command line.
16882 keyword will reject all modules specified by the library pragma.
16884 \labelwidthstring 00.00.0000
16886 udata pragma udata instructs the compiler to emit code so that linker will
16887 place a variable at a specific memory bank
16895 /* places variable foo at bank2 */
16898 #pragma udata bank2 foo
16904 In order for this pragma to work extra SECTION directives should be added
16905 in the .lkr script.
16906 In the following example a sample .lkr file is shown:
16911 // Sample linker script for the PIC18F452 processor
16917 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
16920 CODEPAGE NAME=page START=0x2A END=0x7FFF
16923 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
16926 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
16929 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
16932 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
16935 ACCESSBANK NAME=accessram START=0x0 END=0x7F
16940 DATABANK NAME=gpr0 START=0x80 END=0xFF
16943 DATABANK NAME=gpr1 START=0x100 END=0x1FF
16946 DATABANK NAME=gpr2 START=0x200 END=0x2FF
16949 DATABANK NAME=gpr3 START=0x300 END=0x3FF
16952 DATABANK NAME=gpr4 START=0x400 END=0x4FF
16955 DATABANK NAME=gpr5 START=0x500 END=0x5FF
16958 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
16963 SECTION NAME=CONFIG ROM=config
16968 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
16971 SECTION NAME=bank1 RAM=gpr1 # should be added to link
16974 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
16977 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
16980 SECTION NAME=bank4 RAM=gpr4
16983 SECTION NAME=bank5 RAM=gpr5
16986 The linker will recognise the section name set in the pragma statement and
16987 will position the variable at the memory bank set with the RAM field at
16988 the SECTION line in the linker script file.
16992 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
16999 There is one main header file that can be included to the source files using
17006 This header file contains the definitions for the processor special registers,
17007 so it is necessary if the source accesses them.
17008 It can be included by adding the following line in the beginning of the
17012 #include <pic18fregs.h>
17015 The specific microcontroller is selected within the pic18fregs.h automatically,
17016 so the same source can be used with a variety of devices.
17022 The libraries that PIC16
17023 \begin_inset LatexCommand \index{PIC16}
17027 port depends on are the microcontroller device libraries which contain
17028 the symbol definitions for the microcontroller special function registers.
17029 These libraries have the format pic18fxxxx.lib, where
17033 is the microcontroller identification number.
17034 The specific library is selected automatically by the compiler at link
17035 stage according to the selected device.
17038 Libraries are created with gplib which is part of the gputils package
17039 \begin_inset LatexCommand \url{http://gputils.sourceforge.net}
17044 \layout Subsubsection*
17046 Building the libraries
17049 Before using SDCC/pic16 there are some libraries that need to be compiled.
17050 This process is not done automatically by SDCC since not all users use
17051 SDCC for pic16 projects.
17052 So each user should compile the libraries separately.
17055 The steps to compile the pic16 libraries under Linux are:
17058 cd device/lib/pic16
17073 su -c 'make install' # install the libraries, you need the root password
17076 If you need to install the headers too, do:
17082 su -c 'make install' # install the headers, you need the root password
17085 There exist a special target to build the I/O libraries.
17086 This target is not automatically build because it will build the I/O library
17092 This way building will take quite a lot of time.
17093 Users are advised to edit the
17095 device/lib/pic16/pics.build
17097 file and then execute:
17106 The following memory models are supported by the PIC16 port:
17115 Memory model affects the default size of pointers within the source.
17116 The sizes are shown in the next table:
17120 \begin_inset Tabular
17121 <lyxtabular version="3" rows="3" columns="3">
17123 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17124 <column alignment="center" valignment="top" leftline="true" width="0">
17125 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17126 <row topline="true" bottomline="true">
17127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17132 Pointer sizes according to memory model
17135 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17143 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17152 <row topline="true" bottomline="true">
17153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17161 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17169 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17178 <row topline="true" bottomline="true">
17179 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17187 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
17195 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17211 It is advisable that all sources within a project are compiled with the
17213 If one wants to override the default memory model, this can be done by
17214 declaring a pointer as
17223 Far selects large memory model's pointers, while near selects small memory
17227 The standard device libraries (see
17228 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
17232 ) contain no reference to pointers, so they can be used with both memory
17239 The stack implementation for the PIC16 port uses two indirect registers,
17242 \labelwidthstring 00.00.0000
17244 FSR1 is assigned as stack pointer
17246 \labelwidthstring 00.00.0000
17248 FSR2 is assigned as frame pointer
17251 The following stack models are supported by the PIC16 port
17272 model means that only the FSRxL byte is used to access stack and frame,
17279 uses both FSRxL and FSRxH registers.
17280 The following table shows the stack/frame pointers sizes according to stack
17281 model and the maximum space they can address:
17285 \begin_inset Tabular
17286 <lyxtabular version="3" rows="3" columns="3">
17288 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17289 <column alignment="center" valignment="top" leftline="true" width="0">
17290 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17291 <row topline="true" bottomline="true">
17292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17297 Stack & Frame pointer sizes according to stack model
17300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17317 <row topline="true">
17318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17343 <row topline="true" bottomline="true">
17344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17352 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17360 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17380 stack model is currently not working properly throughout the code generator.
17381 So its use is not advised.
17382 Also there are some other points that need special care:
17387 Do not create stack sections with size more than one physical bank (that
17391 Stack sections should no cross physical bank limits (i.e.
17392 #pragma stack 0x50 0x100)
17395 These limitations are caused by the fact that only FSRxL is modified when
17396 using SMALL stack model, so no more than 256 bytes of stack can be used.
17397 This problem will disappear after LARGE model is fully implemented.
17403 In addition to the standard SDCC function keywords, PIC16 port makes available
17406 \labelwidthstring 00.00.0000
17408 wparam Use the WREG to pass one byte of the first function argument.
17409 This improves speed but you may not use this for functions with arguments
17410 that are called via function pointers, otherwise the first byte of the
17411 first parameter will get lost.
17415 void func_wparam(int a) wparam
17421 /* WREG hold the lower part of a */
17424 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
17434 This keyword replaces the deprecated wparam pragma.
17436 \labelwidthstring 00.00.0000
17438 shadowregs When entering/exiting an ISR, it is possible to take advantage
17439 of the PIC18F hardware shadow registers which hold the values of WREG,
17440 STATUS and BSR registers.
17441 This can be done by adding the keyword
17449 keyword in the function's header.
17452 void isr_shadow(void) shadowregs interrupt 1
17468 instructs the code generator not to store/restore WREG, STATUS, BSR when
17469 entering/exiting the ISR.
17472 Function return values
17475 Return values from functions are placed to the appropriate registers following
17476 a modified Microchip policy optimized for SDCC.
17477 The following table shows these registers:
17481 \begin_inset Tabular
17482 <lyxtabular version="3" rows="6" columns="2">
17484 <column alignment="center" valignment="top" leftline="true" width="0">
17485 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17486 <row topline="true" bottomline="true">
17487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17500 destination register
17504 <row topline="true">
17505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17513 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17522 <row topline="true">
17523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17531 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17540 <row topline="true">
17541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17558 <row topline="true">
17559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17567 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17572 FSR0L:PRODH:PRODL:WREG
17576 <row topline="true" bottomline="true">
17577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17590 on stack, FSR0 points to the beginning
17604 An interrupt servive routine (ISR) is declared using the
17611 void isr(void) interrupt
17629 is the interrupt number, which for PIC18F devices can be:
17633 \begin_inset Tabular
17634 <lyxtabular version="3" rows="4" columns="3">
17636 <column alignment="center" valignment="top" leftline="true" width="0">
17637 <column alignment="center" valignment="top" leftline="true" width="0">
17638 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17639 <row topline="true" bottomline="true">
17640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17650 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17658 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17663 Interrupt Vector Address
17667 <row topline="true">
17668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17693 <row topline="true">
17694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17725 HIGH priority interrupts
17728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17737 <row topline="true" bottomline="true">
17738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17751 LOW priority interrupts
17754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17770 When generating assembly code for ISR the code generator places a
17776 Interrupt Vector Address
17778 which points at the genetated ISR.
17779 This single GOTO instruction is part of an automatically generated
17781 interrupt entry point
17784 The actuall ISR code is placed as normally would in the code space.
17785 Upon interrupt request, the GOTO instruction is executed which jumps to
17787 When declaring interrupt functions as _naked this GOTO instruction is
17792 The whole interrupt functions is therefore placed at the Interrupt Vector
17793 Address of the specific interrupt.
17794 This is not a problem for the LOW priority interrupts, but it is a problem
17795 for the RESET and the HIGH priority interrupts because code may be written
17796 at the next interrupt´s vector address and cause undeterminate program
17797 behaviour if that interrupt is raised.
17803 This is not a problem when
17806 this is a HIGH interrupt ISR and LOW interrupts are
17813 when the ISR is small enough not to reach the next interrupt´s vector address.
17823 is possible to be omitted.
17824 This way a function is generated similar to an ISR, but it is not assigned
17828 When entering an interrupt, currently the PIC16
17829 \begin_inset LatexCommand \index{PIC16}
17833 port automatically saves the following registers:
17845 PROD (PRODL and PRODH)
17848 FSR0 (FSR0L and FSR0H)
17851 These registers are restored upon return from the interrupt routine.
17857 NOTE that when the _naked attribute is specified for an interrupt routine,
17858 then NO registers are stored or restored.
17867 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
17868 There are 3 types of generic pointers currently implemented data, code
17869 and eeprom pointers.
17870 They are differentiated by the value of the 7th and 6th bits of the upper
17875 \begin_inset Tabular
17876 <lyxtabular version="3" rows="5" columns="5">
17878 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17879 <column alignment="center" valignment="top" width="0">
17880 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17881 <column alignment="center" valignment="top" width="0">
17882 <column alignment="left" valignment="top" rightline="true" width="0">
17883 <row topline="true" bottomline="true">
17884 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17900 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17913 rest of the pointer
17916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17925 <row topline="true" bottomline="true">
17926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17942 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17950 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17959 uuuuuu uuuuxxxx xxxxxxxx
17962 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17967 a 12-bit data pointer in data RAM memory
17971 <row bottomline="true">
17972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17980 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17988 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18005 uxxxxx xxxxxxxx xxxxxxxx
18008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18013 a 21-bit code pointer in FLASH memory
18017 <row bottomline="true">
18018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18051 uuuuuu uuuuuuxx xxxxxxxx
18054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18059 a 10-bit eeprom pointer in EEPROM memory
18063 <row bottomline="true">
18064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18072 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18080 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18088 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18097 xxxxxx xxxxxxxx xxxxxxxx
18100 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18105 unimplemented pointer type
18116 Generic pointer are read and written with a set of library functions which
18117 read/write 1, 2, 3, 4 bytes.
18121 \layout Subsubsection
18123 Standard I/O Streams
18130 the type FILE is defined as:
18133 typedef char * FILE;
18136 This type is the stream type implemented I/O in the PIC18F devices.
18137 Also the standard input and output streams are declared in stdio.h:
18140 extern FILE * stdin;
18143 extern FILE * stdout;
18146 The FILE type is actually a generic pointer which defines one more type
18147 of generic pointers, the
18152 This new type has the format:
18156 \begin_inset Tabular
18157 <lyxtabular version="3" rows="2" columns="7">
18159 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18160 <column alignment="center" valignment="top" width="0">
18161 <column alignment="center" valignment="top" leftline="true" width="0">
18162 <column alignment="center" valignment="top" leftline="true" width="0">
18163 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18164 <column alignment="center" valignment="top" width="0">
18165 <column alignment="left" valignment="top" rightline="true" width="0">
18166 <row topline="true" bottomline="true">
18167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18183 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18191 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18199 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18207 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18212 rest of the pointer
18215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18224 <row topline="true" bottomline="true">
18225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18265 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18277 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18282 upper byte high nubble is 0x2n, the rest are zeroes
18293 Currently implemented there are 3 types of streams defined:
18297 \begin_inset Tabular
18298 <lyxtabular version="3" rows="4" columns="4">
18300 <column alignment="center" valignment="top" leftline="true" width="0">
18301 <column alignment="center" valignment="top" leftline="true" width="0">
18302 <column alignment="center" valignment="top" leftline="true" width="0">
18303 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18304 <row topline="true" bottomline="true">
18305 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18313 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18338 <row topline="true">
18339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18347 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18370 Writes/Reads characters via the USART peripheral
18374 <row topline="true">
18375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18401 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18406 Writes/Reads characters via the MSSP peripheral
18410 <row topline="true" bottomline="true">
18411 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18419 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18429 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18437 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18442 Writes/Reads characters via used defined functions
18453 The stream identifiers are declared as macros in the stdio.h header.
18456 In the libc library there exist the functions that are used to write to
18457 each of the above streams.
18460 \labelwidthstring 00.00.0000
18472 _stream_usart_putchar writes a character at the USART stream
18474 \labelwidthstring 00.00.0000
18486 _stream_mssp_putchar writes a character at the MSSP stream
18488 \labelwidthstring 00.00.0000
18490 putchar dummy function.
18491 This writes a character to a user specified manner.
18494 In order to increase performance
18498 is declared in stdio.h as having its parameter in WREG (it has the wparam
18500 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
18501 in a user-friendly way.
18506 is the name of the variable that holds the character to print.
18507 An example follows:
18510 #include <pic18fregs.h>
18522 PORTA = c; /* dump character c to PORTA */
18535 stdout = STREAM_USER; /* this is not necessery, since stdout points
18538 * by default to STREAM_USER */
18541 printf (¨This is a printf test
18549 \layout Subsubsection
18554 PIC16 contains an implementation of the printf-family of functions.
18555 There exist the following functions:
18558 extern unsigned int sprintf(char *buf, char *fmt, ...);
18561 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
18566 extern unsigned int printf(char *fmt, ...);
18569 extern unsigned int vprintf(char *fmt, va_lista ap);
18574 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
18577 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
18580 For sprintf and vsprintf
18584 should normally be a data pointer where the resulting string will be placed.
18585 No range checking is done so the user should allocate the necessery buffer.
18586 For fprintf and vfprintf
18590 should be a stream pointer (i.e.
18591 stdout, STREAM_MSSP, etc...).
18592 \layout Subsubsection
18597 The PIC18F family of microcontrollers supports a number of interrupt sources.
18598 A list of these interrupts is shown in the following table:
18602 \begin_inset Tabular
18603 <lyxtabular version="3" rows="11" columns="4">
18605 <column alignment="left" valignment="top" leftline="true" width="0">
18606 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18607 <column alignment="left" valignment="top" leftline="true" width="0">
18608 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18609 <row topline="true" bottomline="true">
18610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18618 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18626 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18634 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18643 <row topline="true">
18644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18652 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18657 PORTB change interrupt
18660 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18668 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18673 EEPROM/FLASH write complete interrupt
18677 <row topline="true">
18678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18686 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18691 INT0 external interrupt
18694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18707 Bus collision interrupt
18711 <row topline="true">
18712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18725 INT1 external interrupt
18728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18736 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18741 Low voltage detect interrupt
18745 <row topline="true">
18746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18759 INT2 external interrupt
18762 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18775 Parallel slave port interrupt
18779 <row topline="true">
18780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18788 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18793 CCP1 module interrupt
18796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18809 AD convertion complete interrupt
18813 <row topline="true">
18814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18827 CCP2 module interrupt
18830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18843 USART receive interrupt
18847 <row topline="true">
18848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18861 TMR0 overflow interrupt
18864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18877 USART transmit interrupt
18881 <row topline="true">
18882 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18895 TMR1 overflow interrupt
18898 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18911 SSP receive/transmit interrupt
18915 <row topline="true">
18916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18924 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18929 TMR2 matches PR2 interrupt
18932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18939 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18947 <row topline="true" bottomline="true">
18948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18956 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18961 TMR3 overflow interrupt
18964 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18971 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18986 The prototypes for these names are defined in the header file
18993 In order to simplify signal handling, a number of macros is provided:
18995 \labelwidthstring 00.00.0000
18997 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
18998 high priority interrupts.
19003 is the function name to use.
19005 \labelwidthstring 00.00.0000
19007 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19008 low priority interrupt.
19013 is the function name to use.
19015 \labelwidthstring 00.00.0000
19017 DEF_HANDLER(sig,handler) define a handler for signal
19021 \labelwidthstring 00.00.0000
19023 END_DEF end the declaration of the dispatch table.
19026 Additionally there are two more macros to simplify the declaration of the
19029 \labelwidthstring 00.00.0000
19033 SIGHANDLER(handler)
19035 this declares the function prototype for the
19041 \labelwidthstring 00.00.0000
19043 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19046 An example of using the macros above is shown below:
19049 #include <pic18fregs.h>
19052 #include <signal.h>
19056 DEF_INTHIGH(high_int)
19059 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
19062 DEF_HANDLER(SIG_BCOL, _bcol_handler)
19069 SIGHANDLER(_tmr0_handler)
19075 /* action to be taken when timer 0 overflows */
19082 SIGHANDLERNAKED(_bcol_handler)
19091 /* action to be taken when bus collision occurs */
19107 Special care should be taken when using the above scheme:
19110 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
19113 when declaring SIGHANDLERNAKED handler never forget to use
19117 for proper returning.
19123 Here you can find some general tips for compiling programs with SDCC/pic16.
19124 \layout Subsubsection
19129 The default stack size (that is 64 bytes) probably is enough for many programs.
19130 One must take care that when there are many levels of function nesting,
19131 or there is excessive usage of stack, its size should be extended.
19132 An example of such a case is the printf/sprintf family of functions.
19133 If you encounter problems like not being able to print integers, then you
19134 need to set the stack size around the maximum (256 for small stack model).
19135 The following diagram shows what happens when calling printf to print an
19139 printf () --> ltoa () --> ultoa () --> divschar ()
19142 It is should be understood that stack is easily consumed when calling complicate
19144 Using command line arguments like -
19154 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
19156 Other ways to reduce stack usage may exist.
19159 Debugging with SDCDB
19160 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
19165 \begin_inset LatexCommand \index{sdcdb (debugger)}
19172 SDCC is distributed with a source level debugger
19173 \begin_inset LatexCommand \index{Debugger}
19178 The debugger uses a command line interface, the command repertoire of the
19179 debugger has been kept as close to gdb
19180 \begin_inset LatexCommand \index{gdb}
19184 (the GNU debugger) as possible.
19185 The configuration and build process is part of the standard compiler installati
19186 on, which also builds and installs the debugger in the target directory
19187 specified during configuration.
19188 The debugger allows you debug BOTH at the C source and at the ASM source
19190 Sdcdb is available on Unix platforms only.
19193 Compiling for Debugging
19207 \begin_inset LatexCommand \index{-\/-debug}
19211 option must be specified for all files for which debug information is to
19213 The complier generates a .adb file for each of these files.
19214 The linker creates the .cdb
19215 \begin_inset LatexCommand \index{<file>.cdb}
19220 \begin_inset LatexCommand \index{<file>.adb}
19224 files and the address information.
19225 This .cdb is used by the debugger.
19228 How the Debugger Works
19241 -debug option is specified the compiler generates extra symbol information
19242 some of which are put into the assembler source and some are put into the
19244 Then the linker creates the .cdb file from the individual .adb files with
19245 the address information for the symbols.
19246 The debugger reads the symbolic information generated by the compiler &
19247 the address information generated by the linker.
19248 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
19249 execution is controlled by the debugger.
19250 When a command is issued for the debugger, it translates it into appropriate
19251 commands for the simulator.
19254 Starting the Debugger
19257 The debugger can be started using the following command line.
19258 (Assume the file you are debugging has the file name foo).
19272 The debugger will look for the following files.
19275 foo.c - the source file.
19278 foo.cdb - the debugger symbol information file.
19281 foo.ihx - the Intel hex format
19282 \begin_inset LatexCommand \index{Intel hex format}
19289 Command Line Options.
19302 -directory=<source file directory> this option can used to specify the directory
19304 The debugger will look into the directory list specified for source, cdb
19306 The items in the directory list must be separated by ':', e.g.
19307 if the source files can be in the directories /home/src1 and /home/src2,
19318 -directory option should be -
19328 -directory=/home/src1:/home/src2.
19329 Note there can be no spaces in the option.
19333 -cd <directory> - change to the <directory>.
19336 -fullname - used by GUI front ends.
19339 -cpu <cpu-type> - this argument is passed to the simulator please see the
19340 simulator docs for details.
19343 -X <Clock frequency > this options is passed to the simulator please see
19344 the simulator docs for details.
19347 -s <serial port file> passed to simulator see the simulator docs for details.
19350 -S <serial in,out> passed to simulator see the simulator docs for details.
19353 -k <port number> passed to simulator see the simulator docs for details.
19359 As mentioned earlier the command interface for the debugger has been deliberatel
19360 y kept as close the GNU debugger gdb, as possible.
19361 This will help the integration with existing graphical user interfaces
19362 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
19363 If you use a graphical user interface for the debugger you can skip the
19365 \layout Subsubsection*
19367 break [line | file:line | function | file:function]
19370 Set breakpoint at specified line or function:
19379 sdcdb>break foo.c:100
19381 sdcdb>break funcfoo
19383 sdcdb>break foo.c:funcfoo
19384 \layout Subsubsection*
19386 clear [line | file:line | function | file:function ]
19389 Clear breakpoint at specified line or function:
19398 sdcdb>clear foo.c:100
19400 sdcdb>clear funcfoo
19402 sdcdb>clear foo.c:funcfoo
19403 \layout Subsubsection*
19408 Continue program being debugged, after breakpoint.
19409 \layout Subsubsection*
19414 Execute till the end of the current function.
19415 \layout Subsubsection*
19420 Delete breakpoint number 'n'.
19421 If used without any option clear ALL user defined break points.
19422 \layout Subsubsection*
19424 info [break | stack | frame | registers ]
19427 info break - list all breakpoints
19430 info stack - show the function call stack.
19433 info frame - show information about the current execution frame.
19436 info registers - show content of all registers.
19437 \layout Subsubsection*
19442 Step program until it reaches a different source line.
19443 Note: pressing <return> repeats the last command.
19444 \layout Subsubsection*
19449 Step program, proceeding through subroutine calls.
19450 \layout Subsubsection*
19455 Start debugged program.
19456 \layout Subsubsection*
19461 Print type information of the variable.
19462 \layout Subsubsection*
19467 print value of variable.
19468 \layout Subsubsection*
19473 load the given file name.
19474 Note this is an alternate method of loading file for debugging.
19475 \layout Subsubsection*
19480 print information about current frame.
19481 \layout Subsubsection*
19486 Toggle between C source & assembly source.
19487 \layout Subsubsection*
19489 ! simulator command
19492 Send the string following '!' to the simulator, the simulator response is
19494 Note the debugger does not interpret the command being sent to the simulator,
19495 so if a command like 'go' is sent the debugger can loose its execution
19496 context and may display incorrect values.
19497 \layout Subsubsection*
19504 My name is Bobby Brown"
19507 Interfacing with XEmacs
19508 \begin_inset LatexCommand \index{XEmacs}
19513 \begin_inset LatexCommand \index{Emacs}
19520 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
19521 sdcdb.el and sdcdbsrc.el.
19522 These two files can be found in the $(prefix)/bin directory after the installat
19524 These files need to be loaded into XEmacs for the interface to work.
19525 This can be done at XEmacs startup time by inserting the following into
19526 your '.xemacs' file (which can be found in your HOME directory):
19532 (load-file sdcdbsrc.el)
19538 .xemacs is a lisp file so the () around the command is REQUIRED.
19539 The files can also be loaded dynamically while XEmacs is running, set the
19540 environment variable 'EMACSLOADPATH' to the installation bin directory
19541 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
19542 To start the interface enter the following command:
19556 You will prompted to enter the file name to be debugged.
19561 The command line options that are passed to the simulator directly are bound
19562 to default values in the file sdcdbsrc.el.
19563 The variables are listed below, these values maybe changed as required.
19566 sdcdbsrc-cpu-type '51
19569 sdcdbsrc-frequency '11059200
19572 sdcdbsrc-serial nil
19575 The following is a list of key mapping for the debugger interface.
19586 ;;key\SpecialChar ~
19600 binding\SpecialChar ~
19624 ;;---\SpecialChar ~
19638 -------\SpecialChar ~
19680 sdcdb-next-from-src\SpecialChar ~
19708 sdcdb-back-from-src\SpecialChar ~
19736 sdcdb-cont-from-src\SpecialChar ~
19746 SDCDB continue command
19764 sdcdb-step-from-src\SpecialChar ~
19792 sdcdb-whatis-c-sexp\SpecialChar ~
19802 SDCDB ptypecommand for data at
19869 sdcdbsrc-delete\SpecialChar ~
19883 SDCDB Delete all breakpoints if no arg
19932 given or delete arg (C-u arg x)
19950 sdcdbsrc-frame\SpecialChar ~
19965 SDCDB Display current frame if no arg,
20014 given or display frame arg
20081 sdcdbsrc-goto-sdcdb\SpecialChar ~
20091 Goto the SDCDB output buffer
20109 sdcdb-print-c-sexp\SpecialChar ~
20120 SDCDB print command for data at
20187 sdcdbsrc-goto-sdcdb\SpecialChar ~
20197 Goto the SDCDB output buffer
20215 sdcdbsrc-mode\SpecialChar ~
20231 Toggles Sdcdbsrc mode (turns it off)
20246 sdcdb-finish-from-src\SpecialChar ~
20254 SDCDB finish command
20269 sdcdb-break\SpecialChar ~
20287 Set break for line with point
20302 sdcdbsrc-mode\SpecialChar ~
20318 Toggle Sdcdbsrc mode
20333 sdcdbsrc-srcmode\SpecialChar ~
20356 Here are a few guidelines that will help the compiler generate more efficient
20357 code, some of the tips are specific to this compiler others are generally
20358 good programming practice.
20361 Use the smallest data type to represent your data-value.
20362 If it is known in advance that the value is going to be less than 256 then
20363 use an 'unsigned char' instead of a 'short' or 'int'.
20364 Please note, that ANSI C requires both signed and unsigned chars to be
20365 promoted to 'signed int' before doing any operation.
20366 This promotion can be omitted, if the result is the same.
20367 The effect of the promotion rules together with the sign-extension is often
20374 unsigned char uc = 0xfe;
20376 if (uc * uc < 0) /* this is true! */
20395 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
20405 (unsigned char) -12 / (signed char) -3 = ...
20408 No, the result is not 4:
20413 (int) (unsigned char) -12 / (int) (signed char) -3 =
20415 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
20417 (int) 0x00f4 / (int) 0xfffd =
20419 (int) 0x00f4 / (int) 0xfffd =
20421 (int) 244 / (int) -3 =
20423 (int) -81 = (int) 0xffaf;
20426 Don't complain, that gcc gives you a different result.
20427 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
20428 Therefore the results are different.
20431 \begin_inset Quotes sld
20435 \begin_inset Quotes srd
20441 If well-defined overflow characteristics are important and negative values
20442 are not, or if you want to steer clear of sign-extension problems when
20443 manipulating bits or bytes, use one of the corresponding unsigned types.
20444 (Beware when mixing signed and unsigned values in expressions, though.)
20446 Although character types (especially unsigned char) can be used as "tiny"
20447 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
20448 ble sign extension and increased code size.
20452 Use unsigned when it is known in advance that the value is not going to
20454 This helps especially if you are doing division or multiplication, bit-shifting
20455 or are using an array index.
20458 NEVER jump into a LOOP.
20461 Declare the variables to be local
20462 \begin_inset LatexCommand \index{local variables}
20466 whenever possible, especially loop control variables (induction).
20469 Since the compiler does not always do implicit integral promotion, the programme
20470 r should do an explicit cast when integral promotion is required.
20473 Reducing the size of division, multiplication & modulus operations can reduce
20474 code size substantially.
20475 Take the following code for example.
20481 foobar(unsigned int p1, unsigned char ch)
20489 unsigned char ch1 = p1 % ch ;
20500 For the modulus operation the variable ch will be promoted to unsigned int
20501 first then the modulus operation will be performed (this will lead to a
20502 call to support routine _moduint()), and the result will be casted to a
20504 If the code is changed to
20509 foobar(unsigned int p1, unsigned char ch)
20517 unsigned char ch1 = (unsigned char)p1 % ch ;
20528 It would substantially reduce the code generated (future versions of the
20529 compiler will be smart enough to detect such optimization opportunities).
20533 Have a look at the assembly listing to get a
20534 \begin_inset Quotes sld
20538 \begin_inset Quotes srd
20541 for the code generation.
20545 \begin_inset LatexCommand \index{Tools}
20549 included in the distribution
20553 \begin_inset Tabular
20554 <lyxtabular version="3" rows="12" columns="3">
20556 <column alignment="center" valignment="top" leftline="true" width="0pt">
20557 <column alignment="center" valignment="top" leftline="true" width="0pt">
20558 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
20559 <row topline="true" bottomline="true">
20560 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20576 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20585 <row topline="true">
20586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20599 Simulator for various architectures
20602 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20611 <row topline="true">
20612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20625 header file conversion
20628 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20633 sdcc/support/scripts
20637 <row topline="true">
20638 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20651 header file conversion
20654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20659 sdcc/support/scripts
20663 <row topline="true">
20664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20672 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20680 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20698 <row topline="true">
20699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20707 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20733 <row topline="true">
20734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20750 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20768 <row topline="true">
20769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20785 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20803 <row topline="true">
20804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20820 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20838 <row topline="true">
20839 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20847 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20855 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20873 <row topline="true">
20874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20882 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20908 <row topline="true" bottomline="true">
20909 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20917 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20925 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20953 \begin_inset LatexCommand \index{Documentation}
20957 included in the distribution
20961 \begin_inset Tabular
20962 <lyxtabular version="3" rows="10" columns="2">
20964 <column alignment="left" valignment="top" leftline="true" width="0">
20965 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
20966 <row topline="true" bottomline="true">
20967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20980 Where to get / filename
20984 <row topline="true">
20985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20990 SDCC Compiler User Guide
20993 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20998 You're reading it right now
21002 <row topline="true">
21003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21011 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21020 <row topline="true">
21021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21027 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
21032 \begin_inset LatexCommand \index{Assembler documentation}
21036 Assemblers and ASLINK
21037 \begin_inset LatexCommand \index{aslink}
21042 \begin_inset LatexCommand \index{Linker documentation}
21049 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21054 sdcc/as/doc/asxhtm.html
21058 <row topline="true">
21059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21064 SDCC regression test
21065 \begin_inset LatexCommand \index{Regression test}
21072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21077 sdcc/doc/test_suite_spec.pdf
21081 <row topline="true">
21082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21099 <row topline="true">
21100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21105 Notes on debugging with sdcdb
21106 \begin_inset LatexCommand \index{sdcdb (debugger)}
21113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21118 sdcc/debugger/README
21122 <row topline="true">
21123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21128 Software simulator for microcontrollers
21131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21158 <row topline="true">
21159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21164 Temporary notes on the pic16
21165 \begin_inset LatexCommand \index{PIC16}
21172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21177 sdcc/src/pic16/NOTES
21181 <row topline="true" bottomline="true">
21182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21187 SDCC internal documentation (debugging file format)
21190 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21226 Related open source tools
21227 \begin_inset LatexCommand \index{Related tools}
21235 \begin_inset Tabular
21236 <lyxtabular version="3" rows="11" columns="3">
21238 <column alignment="center" valignment="top" leftline="true" width="0pt">
21239 <column alignment="block" valignment="top" leftline="true" width="30line%">
21240 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
21241 <row topline="true" bottomline="true">
21242 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21258 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21267 <row topline="true">
21268 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21274 \begin_inset LatexCommand \index{gpsim (pic simulator)}
21281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21289 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21295 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
21303 <row topline="true">
21304 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21310 \begin_inset LatexCommand \index{gputils (pic tools)}
21317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21325 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21331 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
21339 <row topline="true">
21340 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21348 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21356 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21362 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
21370 <row topline="true">
21371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21377 \begin_inset LatexCommand \index{indent (source formatting tool)}
21384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21389 Formats C source - Master of the white spaces
21392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21398 \begin_inset LatexCommand \url{http://home.hccnet.nl/d.ingamells/beautify.html}
21406 <row topline="true">
21407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21413 \begin_inset LatexCommand \index{srecord (tool)}
21420 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21425 Object file conversion, checksumming, ...
21428 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21434 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
21442 <row topline="true">
21443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21449 \begin_inset LatexCommand \index{objdump (tool)}
21456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21461 Object file conversion, ...
21464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21469 Part of binutils (should be there anyway)
21473 <row topline="true">
21474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21480 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
21487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21492 Source code documentation system
21495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21501 \begin_inset LatexCommand \url{http://www.doxygen.org}
21509 <row topline="true">
21510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21518 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21523 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
21526 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21532 \begin_inset LatexCommand \url{http://www.kdevelop.org}
21540 <row topline="true">
21541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21547 \begin_inset LatexCommand \index{splint (syntax checking tool)}
21554 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21559 Statically checks c sources (has anyone adapted splint for SDCC?)
21562 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21568 \begin_inset LatexCommand \url{http://www.splint.org}
21576 <row topline="true" bottomline="true">
21577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21583 \begin_inset LatexCommand \index{ddd (debugger)}
21590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21595 Debugger, serves nicely as GUI to sdcdb
21596 \begin_inset LatexCommand \index{sdcdb (debugger)}
21603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21609 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
21626 Related documentation / recommended reading
21630 \begin_inset Tabular
21631 <lyxtabular version="3" rows="6" columns="3">
21633 <column alignment="center" valignment="top" leftline="true" width="0pt">
21634 <column alignment="block" valignment="top" leftline="true" width="30line%">
21635 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
21636 <row topline="true" bottomline="true">
21637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21662 <row topline="true">
21663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21680 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21686 \begin_inset LatexCommand \index{C Reference card}
21693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21699 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
21707 <row topline="true">
21708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21716 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21724 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21730 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
21738 <row topline="true">
21739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21751 Latest datasheet of the target CPU
21754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21763 <row topline="true">
21764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21771 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21776 Revision history of datasheet
21779 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21788 <row topline="true" bottomline="true">
21789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21804 Advanced Compiler Design and Implementation
21807 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21812 bookstore (very dedicated, probably read other books first)
21828 Some questions answered, some pointers given - it might be time to in turn
21836 can you solve your project with the selected microcontroller? Would you
21837 find out early or rather late that your target is too small/slow/whatever?
21838 Can you switch to a slightly better device if it doesn't fit?
21841 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
21842 and/or another programming language be more adequate? Would an operating
21843 system on the target device help?
21846 if you solved the problem, will the marketing department be happy?
21849 if the marketing department is happy, will customers be happy?
21852 if you're the project manager, marketing department and maybe even the customer
21853 in one person, have you tried to see the project from the outside?
21856 is the project done if you think it is done? Or is just that other interface/pro
21857 tocol/feature/configuration/option missing? How about website, manual(s),
21858 internationali(z|s)ation, packaging, labels, 2nd source for components,
21859 electromagnetic compatability/interference, documentation for production,
21860 production test software, update mechanism, patent issues?
21863 is your project adequately positioned in that magic triangle: fame, fortune,
21867 Maybe not all answers to these questions are known and some answers may
21872 , nevertheless knowing these questions may help you to avoid burnout
21878 burnout is bad for electronic devices, programmers and motorcycle tyres
21882 Chances are you didn't want to hear some of them...
21886 \begin_inset LatexCommand \index{Support}
21893 SDCC has grown to be a large project.
21894 The compiler alone (without the preprocessor, assembler and linker) is
21895 well over 100,000 lines of code (blank stripped).
21896 The open source nature of this project is a key to its continued growth
21898 You gain the benefit and support of many active software developers and
21900 Is SDCC perfect? No, that's why we need your help.
21901 The developers take pride in fixing reported bugs.
21902 You can help by reporting the bugs and helping other SDCC users.
21903 There are lots of ways to contribute, and we encourage you to take part
21904 in making SDCC a great software package.
21908 The SDCC project is hosted on the SDCC sourceforge site at
21909 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
21914 You'll find the complete set of mailing lists
21915 \begin_inset LatexCommand \index{Mailing list(s)}
21919 , forums, bug reporting system, patch submission
21920 \begin_inset LatexCommand \index{Patch submission}
21925 \begin_inset LatexCommand \index{download}
21929 area and cvs code repository
21930 \begin_inset LatexCommand \index{cvs code repository}
21938 \begin_inset LatexCommand \index{Bug reporting}
21943 \begin_inset LatexCommand \index{Reporting bugs}
21950 The recommended way of reporting bugs is using the infrastructure of the
21952 You can follow the status of bug reports there and have an overview about
21956 Bug reports are automatically forwarded to the developer mailing list and
21957 will be fixed ASAP.
21958 When reporting a bug, it is very useful to include a small test program
21959 (the smaller the better) which reproduces the problem.
21960 If you can isolate the problem by looking at the generated assembly code,
21961 this can be very helpful.
21962 Compiling your program with the -
21973 \begin_inset LatexCommand \index{-\/-dumpall}
21977 option can sometimes be useful in locating optimization problems.
21978 When reporting a bug please maker sure you:
21981 Attach the code you are compiling with SDCC.
21985 Specify the exact command you use to run SDCC, or attach your Makefile.
21989 Specify the SDCC version (type "
21995 "), your platform, and operating system.
21999 Provide an exact copy of any error message or incorrect output.
22003 Put something meaningful in the subject of your message.
22006 Please attempt to include these 5 important parts, as applicable, in all
22007 requests for support or when reporting any problems or bugs with SDCC.
22008 Though this will make your message lengthy, it will greatly improve your
22009 chance that SDCC users and developers will be able to help you.
22010 Some SDCC developers are frustrated by bug reports without code provided
22011 that they can use to reproduce and ultimately fix the problem, so please
22012 be sure to provide sample code if you are reporting a bug!
22015 Please have a short check that you are using a recent version of SDCC and
22016 the bug is not yet known.
22017 This is the link for reporting bugs:
22018 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
22025 Requesting Features
22026 \begin_inset LatexCommand \label{sub:Requesting-Features}
22031 \begin_inset LatexCommand \index{Feature request}
22036 \begin_inset LatexCommand \index{Requesting features}
22043 Like bug reports feature requests are forwarded to the developer mailing
22045 This is the link for requesting features:
22046 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
22056 Like bug reports contributed patches are forwarded to the developer mailing
22058 This is the link for submitting patches
22059 \begin_inset LatexCommand \index{Patch submission}
22064 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
22071 You need to specify some parameters to the
22075 command for the patches to be useful.
22076 If you modified more than one file a patch created f.e.
22081 \begin_inset Quotes sld
22084 diff -Naur unmodified_directory modified_directory >my_changes.patch
22085 \begin_inset Quotes srd
22091 will be fine, otherwise
22095 \begin_inset Quotes sld
22098 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
22099 \begin_inset Quotes srd
22112 These links should take you directly to the
22113 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
22123 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
22124 automated messages (mid 2003)
22128 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
22133 \begin_inset LatexCommand \index{Mailing list(s)}
22137 and forums are archived and searchable so if you are lucky someone already
22138 had a similar problem.
22139 While mails to the lists themselves are delivered promptly their web front
22140 end on sourceforge sometimes shows a severe time lag (up to several weeks),
22141 if you're seriously using SDCC please consider subscribing to the lists.
22147 You can follow the status of the cvs version
22148 \begin_inset LatexCommand \index{version}
22152 of SDCC by watching the Changelog
22153 \begin_inset LatexCommand \index{Changelog}
22157 in the cvs-repository
22162 \begin_inset LatexCommand \htmlurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
22170 \begin_inset LatexCommand \index{Release policy}
22177 Historically there often were long delays between official releases and
22178 the sourceforge download area tends to get not updated at all.
22179 Excuses in the past might have referred to problems with live range analysis,
22180 but as this was fixed a while ago, the current problem is that another
22181 excuse has to be found.
22182 Kidding aside, we have to get better there! On the other hand there are
22183 daily snapshots available at
22184 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
22188 , and you can always build the very last version (hopefully with many bugs
22189 fixed, and features added) from the source code available at
22190 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
22198 \begin_inset LatexCommand \index{Examples}
22205 You'll find some small examples in the directory
22207 sdcc/device/examples/.
22210 More examples and libraries are available at
22212 The SDCC Open Knowledge Resource
22213 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
22220 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
22227 I did insert a reference to Paul's web site here although it seems rather
22228 dedicated to a specific 8032 board (I think it's okay because it f.e.
22229 shows LCD/Harddisc interface and has a free 8051 monitor.
22230 Independent 8032 board vendors face hard competition of heavily subsidized
22231 development boards anyway).
22234 Maybe we should include some links to real world applications.
22235 Preferably pointer to pointers (one for each architecture) so this stays
22240 \begin_inset LatexCommand \index{Quality control}
22247 The compiler is passed through nightly compile and build checks.
22253 \begin_inset LatexCommand \index{Regression test}
22257 check that SDCC itself compiles flawlessly on several platforms and checks
22258 the quality of the code generated by SDCC by running the code through simulator
22260 There is a separate document
22263 \begin_inset LatexCommand \index{Test suite}
22272 You'll find the test code in the directory
22274 sdcc/support/regression
22277 You can run these tests manually by running
22281 in this directory (or f.e.
22286 \begin_inset Quotes sld
22290 \begin_inset Quotes srd
22296 if you don't want to run the complete tests).
22297 The test code might also be interesting if you want to look for examples
22298 \begin_inset LatexCommand \index{Examples}
22302 checking corner cases of SDCC or if you plan to submit patches
22303 \begin_inset LatexCommand \index{Patch submission}
22310 The pic port uses a different set of regression tests, you'll find them
22313 sdcc/src/regression
22318 SDCC Technical Data
22322 \begin_inset LatexCommand \index{Optimizations}
22329 SDCC performs a host of standard optimizations in addition to some MCU specific
22334 Sub-expression Elimination
22335 \begin_inset LatexCommand \index{Subexpression elimination}
22342 The compiler does local and
22368 will be translated to
22380 Some subexpressions are not as obvious as the above example, e.g.:
22390 In this case the address arithmetic a->b[i] will be computed only once;
22391 the equivalent code in C would be.
22403 The compiler will try to keep these temporary variables in registers.
22406 Dead-Code Elimination
22407 \begin_inset LatexCommand \index{Dead-code elimination}
22428 i = 1; \SpecialChar ~
22437 global = 1;\SpecialChar ~
22450 global = 3;\SpecialChar ~
22475 \begin_inset LatexCommand \index{Copy propagation}
22531 Note: the dead stores created by this copy propagation will be eliminated
22532 by dead-code elimination.
22536 \begin_inset LatexCommand \index{Loop optimization}
22541 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
22548 Two types of loop optimizations are done by SDCC
22556 of loop induction variables.
22557 In addition to the strength reduction the optimizer marks the induction
22558 variables and the register allocator tries to keep the induction variables
22559 in registers for the duration of the loop.
22560 Because of this preference of the register allocator
22561 \begin_inset LatexCommand \index{Register allocation}
22565 , loop induction optimization causes an increase in register pressure, which
22566 may cause unwanted spilling of other temporary variables into the stack
22567 \begin_inset LatexCommand \index{stack}
22572 The compiler will generate a warning message when it is forced to allocate
22573 extra space either on the stack or data space.
22574 If this extra space allocation is undesirable then induction optimization
22575 can be eliminated either for the entire source file (with -
22585 -noinduction option) or for a given function only using #pragma\SpecialChar ~
22587 \begin_inset LatexCommand \index{\#pragma noinduction}
22600 for (i = 0 ; i < 100 ; i ++)
22616 for (i = 0; i < 100; i++)
22625 As mentioned previously some loop invariants are not as apparent, all static
22626 address computations are also moved out of the loop.
22631 \begin_inset LatexCommand \index{Strength reduction}
22635 , this optimization substitutes an expression by a cheaper expression:
22640 for (i=0;i < 100; i++)
22658 for (i=0;i< 100;i++) {
22664 ar[itemp1] = itemp2;
22681 The more expensive multiplication
22682 \begin_inset LatexCommand \index{Multiplication}
22686 is changed to a less expensive addition.
22690 \begin_inset LatexCommand \index{Loop reversing}
22697 This optimization is done to reduce the overhead of checking loop boundaries
22698 for every iteration.
22699 Some simple loops can be reversed and implemented using a
22700 \begin_inset Quotes eld
22703 decrement and jump if not zero
22704 \begin_inset Quotes erd
22708 SDCC checks for the following criterion to determine if a loop is reversible
22709 (note: more sophisticated compilers use data-dependency analysis to make
22710 this determination, SDCC uses a more simple minded analysis).
22713 The 'for' loop is of the form
22719 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
22729 The <for body> does not contain
22730 \begin_inset Quotes eld
22734 \begin_inset Quotes erd
22738 \begin_inset Quotes erd
22744 All goto's are contained within the loop.
22747 No function calls within the loop.
22750 The loop control variable <sym> is not assigned any value within the loop
22753 The loop control variable does NOT participate in any arithmetic operation
22757 There are NO switch statements in the loop.
22760 Algebraic Simplifications
22763 SDCC does numerous algebraic simplifications, the following is a small sub-set
22764 of these optimizations.
22769 i = j + 0;\SpecialChar ~
22773 /* changed to: */\SpecialChar ~
22779 i /= 2;\SpecialChar ~
22786 /* changed to: */\SpecialChar ~
22792 i = j - j;\SpecialChar ~
22796 /* changed to: */\SpecialChar ~
22802 i = j / 1;\SpecialChar ~
22806 /* changed to: */\SpecialChar ~
22813 Note the subexpressions
22814 \begin_inset LatexCommand \index{Subexpression}
22818 given above are generally introduced by macro expansions or as a result
22819 of copy/constant propagation.
22822 'switch' Statements
22823 \begin_inset LatexCommand \label{sub:'switch'-Statements}
22828 \begin_inset LatexCommand \index{switch statement}
22835 SDCC can optimize switch statements to jump tables
22836 \begin_inset LatexCommand \index{jump tables}
22841 It makes the decision based on an estimate of the generated code size.
22842 SDCC is quite liberal in the requirements for jump table generation:
22845 The labels need not be in order, and the starting number need not be one
22846 or zero, the case labels are in numerical sequence or not too many case
22847 labels are missing.
22853 switch(i) {\SpecialChar ~
22884 case 4: ...\SpecialChar ~
22916 case 5: ...\SpecialChar ~
22948 case 3: ...\SpecialChar ~
22979 case 6: ...\SpecialChar ~
23011 case 7: ...\SpecialChar ~
23043 case 8: ...\SpecialChar ~
23075 case 9: ...\SpecialChar ~
23107 case 10: ...\SpecialChar ~
23138 case 11: ...\SpecialChar ~
23205 Both the above switch statements will be implemented using a jump-table.
23206 The example to the right side is slightly more efficient as the check for
23207 the lower boundary of the jump-table is not needed.
23211 The number of case labels is not larger than supported by the target architectur
23215 If the case labels are not in numerical sequence ('gaps' between cases)
23216 SDCC checks whether a jump table with additionally inserted dummy cases
23217 is still attractive.
23221 If the starting number is not zero and a check for the lower boundary of
23222 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
23227 Switch statements which have large gaps in the numeric sequence or those
23228 that have too many case labels can be split into more than one switch statement
23229 for efficient code generation, e.g.:
23309 If the above switch statement is broken down into two switch statements
23399 then both the switch statements will be implemented using jump-tables whereas
23400 the unmodified switch statement will not be.
23403 There might be reasons which SDCC cannot know about to either favour or
23404 not favour jump tables.
23405 If the target system has to be as quick for the last switch case as for
23406 the first (pro jump table), or if the switch argument is known to be zero
23407 in the majority of the cases (contra jump table).
23410 The pragma nojtbound
23411 \begin_inset LatexCommand \index{\#pragma nojtbound}
23415 can be used to turn off checking the
23428 It has no effect if a default label is supplied.
23429 Use of this pragma is dangerous: if the switch argument is not matched
23430 by a case statement the processor will happily jump into Nirvana.
23433 Bit-shifting Operations
23434 \begin_inset LatexCommand \index{Bit shifting}
23441 Bit shifting is one of the most frequently used operation in embedded programmin
23443 SDCC tries to implement bit-shift operations in the most efficient way
23459 generates the following code:
23476 In general SDCC will never setup a loop if the shift count is known.
23519 \begin_inset LatexCommand \index{Bit rotation}
23526 A special case of the bit-shift operation is bit rotation
23527 \begin_inset LatexCommand \index{rotating bits}
23531 , SDCC recognizes the following expression to be a left bit-rotation:
23541 char i;\SpecialChar ~
23552 /* unsigned is needed for rotation */
23557 i = ((i << 1) | (i >> 7));
23566 will generate the following code:
23585 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
23586 ns of this case will also be recognized as bit-rotation, i.e.:
23591 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
23594 Nibble and Byte Swapping
23597 Other special cases of the bit-shift operations are nibble or byte swapping
23598 \begin_inset LatexCommand \index{swapping nibbles/bytes}
23602 , SDCC recognizes the following expressions:
23625 i = ((i << 4) | (i >> 4));
23631 j = ((j << 8) | (j >> 8));
23634 and generates a swap instruction for the nibble swapping
23635 \begin_inset LatexCommand \index{Nibble swapping}
23639 or move instructions for the byte swapping
23640 \begin_inset LatexCommand \index{Byte swapping}
23646 \begin_inset Quotes sld
23650 \begin_inset Quotes srd
23653 example can be used to convert from little to big-endian or vice versa.
23654 If you want to change the endianness of a
23658 integer you have to cast to
23665 Note that SDCC stores numbers in little-endian
23671 Usually 8-bit processors don't care much about endianness.
23672 This is not the case for the standard 8051 which only has an instruction
23678 \begin_inset LatexCommand \index{DPTR}
23686 so little-endian is the more efficient byte order.
23690 \begin_inset LatexCommand \index{little-endian}
23695 \begin_inset LatexCommand \index{Endianness}
23700 lowest order first).
23704 \begin_inset LatexCommand \index{Highest Order Bit}
23711 It is frequently required to obtain the highest order bit of an integral
23712 type (long, int, short or char types).
23713 SDCC recognizes the following expression to yield the highest order bit
23714 and generates optimized code for it, e.g.:
23736 hob = (gint >> 15) & 1;
23746 will generate the following code:
23779 000A E5*01\SpecialChar ~
23806 000C 23\SpecialChar ~
23837 000D 54 01\SpecialChar ~
23864 000F F5*02\SpecialChar ~
23892 Variations of this case however will
23897 It is a standard C expression, so I heartily recommend this be the only
23898 way to get the highest order bit, (it is portable).
23899 Of course it will be recognized even if it is embedded in other expressions,
23905 xyz = gint + ((gint >> 15) & 1);
23908 will still be recognized.
23912 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
23917 \begin_inset LatexCommand \index{Peephole optimizer}
23924 The compiler uses a rule based, pattern matching and re-writing mechanism
23925 for peep-hole optimization.
23930 a peep-hole optimizer by Christopher W.
23931 Fraser (cwfraser@microsoft.com).
23932 A default set of rules are compiled into the compiler, additional rules
23933 may be added with the
23946 \begin_inset LatexCommand \index{-\/-peep-file}
23953 The rule language is best illustrated with examples.
23977 The above rule will change the following assembly
23978 \begin_inset LatexCommand \index{Assembler routines}
24000 Note: All occurrences of a
24004 (pattern variable) must denote the same string.
24005 With the above rule, the assembly sequence:
24015 will remain unmodified.
24019 Other special case optimizations may be added by the user (via
24035 some variants of the 8051 MCU
24036 \begin_inset LatexCommand \index{MCS51 variants}
24049 The following two rules will change all
24068 replace { lcall %1 } by { acall %1 }
24070 replace { ljmp %1 } by { ajmp %1 }
24075 inline-assembler code
24077 is also passed through the peep hole optimizer, thus the peephole optimizer
24078 can also be used as an assembly level macro expander.
24079 The rules themselves are MCU dependent whereas the rule language infra-structur
24080 e is MCU independent.
24081 Peephole optimization rules for other MCU can be easily programmed using
24086 The syntax for a rule is as follows:
24091 rule := replace [ restart ] '{' <assembly sequence> '
24129 <assembly sequence> '
24147 '}' [if <functionName> ] '
24152 <assembly sequence> := assembly instruction (each instruction including
24153 labels must be on a separate line).
24157 The optimizer will apply to the rules one by one from the top in the sequence
24158 of their appearance, it will terminate when all rules are exhausted.
24159 If the 'restart' option is specified, then the optimizer will start matching
24160 the rules again from the top, this option for a rule is expensive (performance)
24161 , it is intended to be used in situations where a transformation will trigger
24162 the same rule again.
24163 An example of this (not a good one, it has side effects) is the following
24186 Note that the replace pattern cannot be a blank, but can be a comment line.
24187 Without the 'restart' option only the innermost 'pop' 'push' pair would
24188 be eliminated, i.e.:
24218 the restart option the rule will be applied again to the resulting code
24219 and then all the pop-push pairs will be eliminated to yield:
24229 A conditional function can be attached to a rule.
24230 Attaching rules are somewhat more involved, let me illustrate this with
24257 The optimizer does a look-up of a function name table defined in function
24262 in the source file SDCCpeeph.c, with the name
24267 If it finds a corresponding entry the function is called.
24268 Note there can be no parameters specified for these functions, in this
24273 is crucial, since the function
24277 expects to find the label in that particular variable (the hash table containin
24278 g the variable bindings is passed as a parameter).
24279 If you want to code more such functions, take a close look at the function
24280 labelInRange and the calling mechanism in source file SDCCpeeph.c.
24281 Currently implemented are
24283 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
24284 24bitMode, portIsDS390, 24bitModeAndPortDS390
24293 I know this whole thing is a little kludgey, but maybe some day we will
24294 have some better means.
24295 If you are looking at this file, you will see the default rules that are
24296 compiled into the compiler, you can add your own rules in the default set
24297 there if you get tired of specifying the -
24311 \begin_inset LatexCommand \index{ANSI-compliance}
24316 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
24323 Deviations from the compliance:
24326 functions are not reentrant
24327 \begin_inset LatexCommand \index{reentrant}
24331 unless explicitly declared as such or the
24344 \begin_inset LatexCommand \index{-\/-stack-auto}
24350 command line option is specified.
24353 structures and unions cannot be assigned values directly, cannot be passed
24354 as function parameters or assigned to each other and cannot be a return
24355 value from a function, e.g.:
24381 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
24392 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
24414 return rets;/* is invalid in SDCC although allowed in ANSI */
24421 \begin_inset LatexCommand \index{long long (not supported)}
24426 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
24434 \begin_inset LatexCommand \index{double (not supported)}
24438 ' precision floating point
24439 \begin_inset LatexCommand \index{Floating point support}
24446 No support for setjmp
24447 \begin_inset LatexCommand \index{setjmp (not supported)}
24452 \begin_inset LatexCommand \index{longjmp (not supported)}
24459 Certain words that are valid identifiers in the standard may be reserved
24460 words in SDCC unless the
24473 \begin_inset LatexCommand \index{-\/-std-c89}
24488 \begin_inset LatexCommand \index{-\/-std-c99}
24494 command line options are used.
24498 \begin_inset LatexCommand \index{K\&R style}
24502 function declarations are NOT allowed.
24508 foo(i,j) /* this old style of function declarations */
24510 int i,j; /* are valid in ANSI but not valid in SDCC */
24525 Certain words that are valid identifiers in the standard may be reserved
24526 words in SDCC unless the
24539 \begin_inset LatexCommand \index{-\/-std-c89}
24554 \begin_inset LatexCommand \index{-\/-std-c99}
24560 command line options are used.
24561 These may include (depending on the selected processor): 'at', 'banked',
24562 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
24563 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
24564 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
24566 Compliant equivalents of these keywords are always available in a form
24567 that begin with two underscores, f.e.
24568 '__data' instead of 'data'.
24571 Cyclomatic Complexity
24572 \begin_inset LatexCommand \index{Cyclomatic complexity}
24579 Cyclomatic complexity of a function is defined as the number of independent
24580 paths the program can take during execution of the function.
24581 This is an important number since it defines the number test cases you
24582 have to generate to validate the function.
24583 The accepted industry standard for complexity number is 10, if the cyclomatic
24584 complexity reported by SDCC exceeds 10 you should think about simplification
24585 of the function logic.
24586 Note that the complexity level is not related to the number of lines of
24587 code in a function.
24588 Large functions can have low complexity, and small functions can have large
24594 SDCC uses the following formula to compute the complexity:
24599 complexity = (number of edges in control flow graph) - (number of nodes
24600 in control flow graph) + 2;
24604 Having said that the industry standard is 10, you should be aware that in
24605 some cases it be may unavoidable to have a complexity level of less than
24607 For example if you have switch statement with more than 10 case labels,
24608 each case label adds one to the complexity level.
24609 The complexity level is by no means an absolute measure of the algorithmic
24610 complexity of the function, it does however provide a good starting point
24611 for which functions you might look at for further optimization.
24614 Retargetting for other Processors
24617 The issues for retargetting the compiler are far too numerous to be covered
24619 What follows is a brief description of each of the seven phases of the
24620 compiler and its MCU dependency.
24623 Parsing the source and building the annotated parse tree.
24624 This phase is largely MCU independent (except for the language extensions).
24625 Syntax & semantic checks are also done in this phase, along with some initial
24626 optimizations like back patching labels and the pattern matching optimizations
24627 like bit-rotation etc.
24630 The second phase involves generating an intermediate code which can be easy
24631 manipulated during the later phases.
24632 This phase is entirely MCU independent.
24633 The intermediate code generation assumes the target machine has unlimited
24634 number of registers, and designates them with the name iTemp.
24635 The compiler can be made to dump a human readable form of the code generated
24649 This phase does the bulk of the standard optimizations and is also MCU independe
24651 This phase can be broken down into several sub-phases:
24655 Break down intermediate code (iCode) into basic blocks.
24657 Do control flow & data flow analysis on the basic blocks.
24659 Do local common subexpression elimination, then global subexpression elimination
24661 Dead code elimination
24665 If loop optimizations caused any changes then do 'global subexpression eliminati
24666 on' and 'dead code elimination' again.
24669 This phase determines the live-ranges; by live range I mean those iTemp
24670 variables defined by the compiler that still survive after all the optimization
24672 Live range analysis
24673 \begin_inset LatexCommand \index{Live range analysis}
24677 is essential for register allocation, since these computation determines
24678 which of these iTemps will be assigned to registers, and for how long.
24681 Phase five is register allocation.
24682 There are two parts to this process.
24686 The first part I call 'register packing' (for lack of a better term).
24687 In this case several MCU specific expression folding is done to reduce
24692 The second part is more MCU independent and deals with allocating registers
24693 to the remaining live ranges.
24694 A lot of MCU specific code does creep into this phase because of the limited
24695 number of index registers available in the 8051.
24698 The Code generation phase is (unhappily), entirely MCU dependent and very
24699 little (if any at all) of this code can be reused for other MCU.
24700 However the scheme for allocating a homogenized assembler operand for each
24701 iCode operand may be reused.
24704 As mentioned in the optimization section the peep-hole optimizer is rule
24705 based system, which can reprogrammed for other MCUs.
24709 \begin_inset LatexCommand \index{Compiler internals}
24716 The anatomy of the compiler
24717 \begin_inset LatexCommand \label{sub:The-anatomy-of}
24726 This is an excerpt from an article published in Circuit Cellar Magazine
24728 It's a little outdated (the compiler is much more efficient now and user/develo
24729 per friendly), but pretty well exposes the guts of it all.
24735 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
24736 It is fairly easy to retarget for other 8-bit MCU.
24737 Here we take a look at some of the internals of the compiler.
24742 \begin_inset LatexCommand \index{Parsing}
24749 Parsing the input source file and creating an AST (Annotated Syntax Tree
24750 \begin_inset LatexCommand \index{Annotated syntax tree}
24755 This phase also involves propagating types (annotating each node of the
24756 parse tree with type information) and semantic analysis.
24757 There are some MCU specific parsing rules.
24758 For example the storage classes, the extended storage classes are MCU specific
24759 while there may be a xdata storage class for 8051 there is no such storage
24760 class for z80 or Atmel AVR.
24761 SDCC allows MCU specific storage class extensions, i.e.
24762 xdata will be treated as a storage class specifier when parsing 8051 C
24763 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
24768 \begin_inset LatexCommand \index{iCode}
24775 Intermediate code generation.
24776 In this phase the AST is broken down into three-operand form (iCode).
24777 These three operand forms are represented as doubly linked lists.
24778 ICode is the term given to the intermediate form generated by the compiler.
24779 ICode example section shows some examples of iCode generated for some simple
24780 C source functions.
24784 \begin_inset LatexCommand \index{Optimizations}
24791 Bulk of the target independent optimizations is performed in this phase.
24792 The optimizations include constant propagation, common sub-expression eliminati
24793 on, loop invariant code movement, strength reduction of loop induction variables
24794 and dead-code elimination.
24797 Live range analysis
24798 \begin_inset LatexCommand \index{Live range analysis}
24805 During intermediate code generation phase, the compiler assumes the target
24806 machine has infinite number of registers and generates a lot of temporary
24808 The live range computation determines the lifetime of each of these compiler-ge
24809 nerated temporaries.
24810 A picture speaks a thousand words.
24811 ICode example sections show the live range annotations for each of the
24813 It is important to note here, each iCode is assigned a number in the order
24814 of its execution in the function.
24815 The live ranges are computed in terms of these numbers.
24816 The from number is the number of the iCode which first defines the operand
24817 and the to number signifies the iCode which uses this operand last.
24820 Register Allocation
24821 \begin_inset LatexCommand \index{Register allocation}
24828 The register allocation determines the type and number of registers needed
24830 In most MCUs only a few registers can be used for indirect addressing.
24831 In case of 8051 for example the registers R0 & R1 can be used to indirectly
24832 address the internal ram and DPTR to indirectly address the external ram.
24833 The compiler will try to allocate the appropriate register to pointer variables
24835 ICode example section shows the operands annotated with the registers assigned
24837 The compiler will try to keep operands in registers as much as possible;
24838 there are several schemes the compiler uses to do achieve this.
24839 When the compiler runs out of registers the compiler will check to see
24840 if there are any live operands which is not used or defined in the current
24841 basic block being processed, if there are any found then it will push that
24842 operand and use the registers in this block, the operand will then be popped
24843 at the end of the basic block.
24847 There are other MCU specific considerations in this phase.
24848 Some MCUs have an accumulator; very short-lived operands could be assigned
24849 to the accumulator instead of a general-purpose register.
24855 Figure II gives a table of iCode operations supported by the compiler.
24856 The code generation involves translating these operations into corresponding
24857 assembly code for the processor.
24858 This sounds overly simple but that is the essence of code generation.
24859 Some of the iCode operations are generated on a MCU specific manner for
24860 example, the z80 port does not use registers to pass parameters so the
24861 SEND and RECV iCode operations will not be generated, and it also does
24862 not support JUMPTABLES.
24869 <Where is Figure II?>
24872 In the original article Figure II was announced to be downloadable on
24877 Unfortunately it never seemed to have shown up there, so: where is Figure
24882 \begin_inset LatexCommand \index{iCode}
24889 This section shows some details of iCode.
24890 The example C code does not do anything useful; it is used as an example
24891 to illustrate the intermediate code generated by the compiler.
24903 /* This function does nothing useful.
24910 for the purpose of explaining iCode */
24913 short function (data int *x)
24921 short i=10; \SpecialChar ~
24923 /* dead initialization eliminated */
24928 short sum=10; /* dead initialization eliminated */
24941 while (*x) *x++ = *p++;
24955 /* compiler detects i,j to be induction variables */
24959 for (i = 0, j = 10 ; i < 10 ; i++, j
24985 mul += i * 3; \SpecialChar ~
24987 /* this multiplication remains */
24993 gint += j * 3;\SpecialChar ~
24995 /* this multiplication changed to addition */
25009 In addition to the operands each iCode contains information about the filename
25010 and line it corresponds to in the source file.
25011 The first field in the listing should be interpreted as follows:
25016 Filename(linenumber: iCode Execution sequence number : ICode hash table
25017 key : loop depth of the iCode).
25022 Then follows the human readable form of the ICode operation.
25023 Each operand of this triplet form can be of three basic types a) compiler
25024 generated temporary b) user defined variable c) a constant value.
25025 Note that local variables and parameters are replaced by compiler generated
25028 \begin_inset LatexCommand \index{Live range analysis}
25032 are computed only for temporaries (i.e.
25033 live ranges are not computed for global variables).
25035 \begin_inset LatexCommand \index{Register allocation}
25039 are allocated for temporaries only.
25040 Operands are formatted in the following manner:
25045 Operand Name [lr live-from : live-to ] { type information } [ registers
25051 As mentioned earlier the live ranges are computed in terms of the execution
25052 sequence number of the iCodes, for example
25054 the iTemp0 is live from (i.e.
25055 first defined in iCode with execution sequence number 3, and is last used
25056 in the iCode with sequence number 5).
25057 For induction variables such as iTemp21 the live range computation extends
25058 the lifetime from the start to the end of the loop.
25060 The register allocator used the live range information to allocate registers,
25061 the same registers may be used for different temporaries if their live
25062 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
25063 iTemp17 since their live ranges do not overlap.
25064 In addition the allocator also takes into consideration the type and usage
25065 of a temporary, for example itemp6 is a pointer to near space and is used
25066 as to fetch data from (i.e.
25067 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
25068 Some short lived temporaries are allocated to special registers which have
25069 meaning to the code generator e.g.
25070 iTemp13 is allocated to a pseudo register CC which tells the back end that
25071 the temporary is used only for a conditional jump the code generation makes
25072 use of this information to optimize a compare and jump ICode.
25074 There are several loop optimizations
25075 \begin_inset LatexCommand \index{Loop optimization}
25079 performed by the compiler.
25080 It can detect induction variables iTemp21(i) and iTemp23(j).
25081 Also note the compiler does selective strength reduction
25082 \begin_inset LatexCommand \index{Strength reduction}
25087 the multiplication of an induction variable in line 18 (gint = j * 3) is
25088 changed to addition, a new temporary iTemp17 is allocated and assigned
25089 a initial value, a constant 3 is then added for each iteration of the loop.
25090 The compiler does not change the multiplication
25091 \begin_inset LatexCommand \index{Multiplication}
25095 in line 17 however since the processor does support an 8 * 8 bit multiplication.
25097 Note the dead code elimination
25098 \begin_inset LatexCommand \index{Dead-code elimination}
25102 optimization eliminated the dead assignments in line 7 & 8 to I and sum
25110 Sample.c (5:1:0:0) _entry($9) :
25115 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
25120 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
25125 Sample.c(11:4:53:0) preHeaderLbl0($11) :
25130 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
25136 Sample.c(11:6:5:1) _whilecontinue_0($1) :
25141 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
25147 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25152 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
25158 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
25164 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
25170 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
25176 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
25177 * int}[r0] + 0x2 {short}
25182 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
25187 Sample.c(11:17:21:0)_whilebreak_0($3) :
25192 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
25197 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
25202 Sample.c(15:20:54:0)preHeaderLbl1($13) :
25207 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
25212 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
25217 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
25222 Sample.c(15:24:26:1)_forcond_0($4) :
25227 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
25233 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
25238 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
25239 + ITemp21 [lr21:38]{short}[r4]
25244 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
25250 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
25251 + iTemp15 [lr29:30]{short}[r1]
25256 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
25262 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
25268 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
25274 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
25280 Sample.c(19:38:47:1) goto _forcond_0($4)
25285 Sample.c(19:39:48:0)_forbreak_0($7) :
25290 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
25291 + ITemp11 [lr19:40]{short}[r3]
25296 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
25301 Sample.c(20:42:51:0)_return($8) :
25306 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
25312 Finally the code generated for this function:
25353 ; ----------------------------------------------
25358 ; function function
25363 ; ----------------------------------------------
25373 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
25385 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
25397 ;_whilecontinue_0($1) :
25407 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
25412 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25471 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
25490 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
25537 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
25577 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
25603 ; iTemp6 [lr5:16]{_near * int}[r0] =
25608 ; iTemp6 [lr5:16]{_near * int}[r0] +
25625 ; goto _whilecontinue_0($1)
25637 ; _whilebreak_0($3) :
25647 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
25659 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
25671 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
25683 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
25702 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
25731 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
25736 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
25781 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
25786 ; iTemp21 [lr21:38]{short}[r4]
25812 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
25845 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
25850 ; iTemp15 [lr29:30]{short}[r1]
25869 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
25916 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
25963 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
25975 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
25989 cjne r5,#0xff,00104$
26001 ; goto _forcond_0($4)
26013 ; _forbreak_0($7) :
26023 ; ret iTemp24 [lr40:41]{short}
26066 A few words about basic block successors, predecessors and dominators
26069 Successors are basic blocks
26070 \begin_inset LatexCommand \index{Basic blocks}
26074 that might execute after this basic block.
26076 Predecessors are basic blocks that might execute before reaching this basic
26079 Dominators are basic blocks that WILL execute before reaching this basic
26113 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
26116 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
26119 c) domVect of [BB4] = BB1 ...
26120 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
26128 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
26138 Thanks to all the other volunteer developers who have helped with coding,
26139 testing, web-page creation, distribution sets, etc.
26140 You know who you are :-)
26147 This document was initially written by Sandeep Dutta
26150 All product names mentioned herein may be trademarks
26151 \begin_inset LatexCommand \index{Trademarks}
26155 of their respective companies.
26162 To avoid confusion, the installation and building options for SDCC itself
26163 (chapter 2) are not part of the index.
26167 \begin_inset LatexCommand \printindex{}