1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
6 pdftitle={SDCC Compiler User Guide},
7 pdfauthor={SDCC development team},
8 pdfsubject={installation, user manual},
9 pdfkeywords={8032 8051 ansi c compiler CPU DS390
10 embedded GPL HC08 manual mcs51 microcontroller PIC Z80},
12 linkcolor=blue] {hyperref}
16 \emergencystretch=30pt
21 \inputencoding default
24 \paperfontsize default
26 \papersize letterpaper
31 \use_numerical_citations 0
32 \paperorientation portrait
39 \paragraph_separation indent
41 \quotes_language swedish
49 Please note: double dashed longoptions (e.g.
50 --version) are written this way: -
64 three consecutive dashes simply result in a long resp.
68 Architecture specific stuff (like memory models, code examples) should maybe
72 into seperate sections/chapters/appendices (it is hard to document PIC or
76 a 8051 centered document) - for now simply add.
79 SDCC Compiler User Guide
93 The above strings enclosed in $ are automatically updated by cvs
97 \begin_inset LatexCommand \tableofcontents{}
130 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
135 designed for 8 bit Microprocessors.
136 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
138 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
142 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
143 Zilog Z80 based MCUs.
144 It can be retargetted for other microprocessors, support for Microchip
145 PIC, Atmel AVR is under development.
146 The entire source code for the compiler is distributed under GPL.
148 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
153 \begin_inset LatexCommand \index{aslink}
157 , an open source retargettable assembler & linker.
158 SDCC has extensive language extensions suitable for utilizing various microcont
159 rollers and underlying hardware effectively.
164 In addition to the MCU specific optimizations SDCC also does a host of standard
168 global sub expression elimination,
171 loop optimizations (loop invariant, strength reduction of induction variables
175 constant folding & propagation,
181 dead code elimination
191 For the back-end SDCC uses a global register allocation scheme which should
192 be well suited for other 8 bit MCUs.
197 The peep hole optimizer uses a rule based substitution mechanism which is
203 Supported data-types are:
206 char (8 bits, 1 byte),
209 short and int (16 bits, 2 bytes),
212 long (32 bit, 4 bytes)
219 The compiler also allows
221 inline assembler code
223 to be embedded anywhere in a function.
224 In addition, routines developed in assembly can also be called.
228 SDCC also provides an option (-
238 -cyclomatic) to report the relative complexity of a function.
239 These functions can then be further optimized, or hand coded in assembly
245 SDCC also comes with a companion source level debugger SDCDB, the debugger
246 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
251 The latest version can be downloaded from
252 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
262 Please note: the compiler will probably always be some steps ahead of this
267 \begin_inset LatexCommand \index{Status of documentation}
277 Obviously this has pros and cons
286 All packages used in this compiler system are
294 ; source code for all the sub-packages (pre-processor, assemblers, linkers
295 etc) is distributed with the package.
296 This documentation is maintained using a freeware word processor (LyX).
298 This program is free software; you can redistribute it and/or modify it
299 under the terms of the GNU General Public License
300 \begin_inset LatexCommand \index{GNU General Public License, GPL}
304 as published by the Free Software Foundation; either version 2, or (at
305 your option) any later version.
306 This program is distributed in the hope that it will be useful, but WITHOUT
307 ANY WARRANTY; without even the implied warranty
308 \begin_inset LatexCommand \index{warranty}
312 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
313 See the GNU General Public License for more details.
314 You should have received a copy of the GNU General Public License along
315 with this program; if not, write to the Free Software Foundation, 59 Temple
316 Place - Suite 330, Boston, MA 02111-1307, USA.
317 In other words, you are welcome to use, share and improve this program.
318 You are forbidden to forbid anyone else to use, share and improve what
320 Help stamp out software-hoarding!
323 Typographic conventions
324 \begin_inset LatexCommand \index{Typographic conventions}
331 Throughout this manual, we will use the following convention.
332 Commands you have to type in are printed in
340 Code samples are printed in
345 Interesting items and new terms are printed in
350 Compatibility with previous versions
351 \begin_inset LatexCommand \index{Compatibility with previous versions}
358 This version has numerous bug fixes compared with the previous version.
359 But we also introduced some incompatibilities with older versions.
360 Not just for the fun of it, but to make the compiler more stable, efficient
362 \begin_inset LatexCommand \index{ANSI-compliance}
367 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
371 for ANSI-Compliance).
377 short is now equivalent to int (16 bits), it used to be equivalent to char
378 (8 bits) which is not ANSI compliant.
381 the default directory for gcc-builds where include, library and documentation
382 files are stored is now in /usr/local/share.
385 char type parameters to vararg functions are casted to int unless explicitly
402 will push a as an int and as a char resp.
415 -regextend has been removed.
428 -noregparms has been removed.
441 -stack-after-data has been removed.
445 \begin_inset LatexCommand \index{bit}
450 \begin_inset LatexCommand \index{sbit}
455 \begin_inset LatexCommand \index{\_\_sbit}
459 types now consistently behave like the C99 _Bool type with respect to type
461 \begin_inset LatexCommand \index{type conversion}
466 \begin_inset LatexCommand \index{type promotion}
471 The most common incompatibility resulting from this change is related to
473 \begin_inset LatexCommand \index{Bit toggling}
487 b = ~b; /* equivalent to b=1 instead of toggling b */
491 b = !b; /* toggles b */
495 In previous versions, both forms would have toggled the bit.
500 <pending: more incompatibilities?>
506 What do you need before you start installation of SDCC? A computer, and
508 The preferred method of installation is to compile SDCC from source using
510 For Windows some pre-compiled binary distributions are available for your
512 You should have some experience with command line tools and compiler use.
518 The SDCC home page at
519 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
523 is a great place to find distribution sets.
524 You can also find links to the user mailing lists that offer help or discuss
525 SDCC with other SDCC users.
526 Web links to other SDCC related sites can also be found here.
527 This document can be found in the DOC directory of the source package as
529 A pdf version of this document is available at
530 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
535 Some of the other tools (simulator and assembler) included with SDCC contain
536 their own documentation and can be found in the source distribution.
537 If you want the latest unreleased software, the complete source package
538 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
541 Wishes for the future
544 There are (and always will be) some things that could be done.
545 Here are some I can think of:
552 char KernelFunction3(char p) at 0x340;
560 \begin_inset LatexCommand \index{code banking (not supported)}
570 If you can think of some more, please see the section
571 \begin_inset LatexCommand \ref{sub:Requesting-Features}
575 about filing feature requests
576 \begin_inset LatexCommand \index{Requesting features}
581 \begin_inset LatexCommand \index{Feature request}
591 \begin_inset LatexCommand \index{Installation}
598 For most users it is sufficient to skip to either section
599 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
604 \begin_inset LatexCommand \ref{sub:Windows-Install}
609 More detailled instructions follow below.
613 \begin_inset LatexCommand \index{Options SDCC configuration}
620 The install paths, search paths and other options are defined when running
622 The defaults can be overridden by:
624 \labelwidthstring 00.00.0000
636 -prefix see table below
638 \labelwidthstring 00.00.0000
650 -exec_prefix see table below
652 \labelwidthstring 00.00.0000
664 -bindir see table below
666 \labelwidthstring 00.00.0000
678 -datadir see table below
680 \labelwidthstring 00.00.0000
682 docdir environment variable, see table below
684 \labelwidthstring 00.00.0000
686 include_dir_suffix environment variable, see table below
688 \labelwidthstring 00.00.0000
690 lib_dir_suffix environment variable, see table below
692 \labelwidthstring 00.00.0000
694 sdccconf_h_dir_separator environment variable, either / or
699 This character will only be used in sdccconf.h; don't forget it's a C-header,
700 therefore a double-backslash is needed there.
702 \labelwidthstring 00.00.0000
714 -disable-mcs51-port Excludes the Intel mcs51 port
716 \labelwidthstring 00.00.0000
728 -disable-gbz80-port Excludes the Gameboy gbz80 port
730 \labelwidthstring 00.00.0000
742 -disable-z80-port Excludes the z80 port
744 \labelwidthstring 00.00.0000
756 -disable-avr-port Excludes the AVR port
758 \labelwidthstring 00.00.0000
770 -disable-ds390-port Excludes the DS390 port
772 \labelwidthstring 00.00.0000
784 -disable-hc08-port Excludes the HC08 port
786 \labelwidthstring 00.00.0000
798 -disable-pic-port Excludes the PIC port
800 \labelwidthstring 00.00.0000
812 -disable-xa51-port Excludes the XA51 port
814 \labelwidthstring 00.00.0000
826 -disable-ucsim Disables configuring and building of ucsim
828 \labelwidthstring 00.00.0000
840 -disable-device-lib-build Disables automatically building device libraries
842 \labelwidthstring 00.00.0000
854 -disable-packihx Disables building packihx
856 \labelwidthstring 00.00.0000
868 -enable-libgc Use the Bohem memory allocator.
869 Lower runtime footprint.
872 Furthermore the environment variables CC, CFLAGS, ...
873 the tools and their arguments can be influenced.
874 Please see `configure -
884 -help` and the man/info pages of `configure` for details.
888 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
889 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
890 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
891 At the moment it's not possible to change the default settings (it was
892 simply never required).
896 These configure options are compiled into the binaries, and can only be
897 changed by rerunning 'configure' and recompiling SDCC.
898 The configure options are written in
902 to distinguish them from run time environment variables (see section search
908 \begin_inset Quotes sld
912 \begin_inset Quotes srd
915 are used by the SDCC team to build the official Win32 binaries.
916 The SDCC team uses Mingw32 to build the official Windows binaries, because
923 a gcc compiler and last but not least
926 the binaries can be built by cross compiling on Sourceforge's compile farm.
929 See the examples, how to pass the Win32 settings to 'configure'.
930 The other Win32 builds using Borland, VC or whatever don't use 'configure',
931 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
943 <lyxtabular version="3" rows="8" columns="3">
945 <column alignment="block" valignment="top" leftline="true" width="0in">
946 <column alignment="block" valignment="top" leftline="true" width="0in">
947 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
948 <row topline="true" bottomline="true">
949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
965 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
975 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
993 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1004 <row topline="true">
1005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1025 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1036 <row topline="true">
1037 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1047 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1059 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1074 <row topline="true">
1075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1085 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1097 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1108 <row topline="true">
1109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1146 <row topline="true">
1147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1174 <row topline="true" bottomline="true">
1175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1193 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1211 'configure' also computes relative paths.
1212 This is needed for full relocatability of a binary package and to complete
1213 search paths (see section search paths below):
1219 \begin_inset Tabular
1220 <lyxtabular version="3" rows="4" columns="3">
1222 <column alignment="block" valignment="top" leftline="true" width="0in">
1223 <column alignment="block" valignment="top" leftline="true" width="0in">
1224 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1225 <row topline="true" bottomline="true">
1226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1234 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1242 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1251 <row topline="true" bottomline="true">
1252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1270 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1279 <row bottomline="true">
1280 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1298 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1307 <row bottomline="true">
1308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1359 \begin_inset Quotes srd
1363 \begin_inset Quotes srd
1377 \begin_inset Quotes srd
1381 \begin_inset Quotes srd
1409 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1418 \begin_inset Quotes srd
1421 i586-mingw32msvc-gcc
1422 \begin_inset Quotes srd
1426 \begin_inset Quotes srd
1429 i586-mingw32msvc-g++
1430 \begin_inset Quotes srd
1438 \begin_inset Quotes srd
1441 i586-mingw32msvc-ranlib
1442 \begin_inset Quotes srd
1450 \begin_inset Quotes srd
1453 i586-mingw32msvc-strip
1454 \begin_inset Quotes srd
1472 \begin_inset Quotes srd
1476 \begin_inset Quotes srd
1494 \begin_inset Quotes srd
1498 \begin_inset Quotes srd
1506 \begin_inset Quotes srd
1510 \begin_inset Quotes srd
1518 \begin_inset Quotes srd
1522 \begin_inset Quotes srd
1530 \begin_inset Quotes srd
1534 \begin_inset Quotes srd
1541 sdccconf_h_dir_separator=
1542 \begin_inset Quotes srd
1554 \begin_inset Quotes srd
1571 -disable-device-lib-build
1599 -host=i586-mingw32msvc -
1609 -build=unknown-unknown-linux-gnu
1613 \begin_inset Quotes sld
1617 \begin_inset Quotes srd
1620 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1629 \begin_inset Quotes srd
1633 \begin_inset Quotes srd
1641 \begin_inset Quotes srd
1645 \begin_inset Quotes srd
1663 \begin_inset Quotes srd
1667 \begin_inset Quotes srd
1685 \begin_inset Quotes srd
1689 \begin_inset Quotes srd
1697 \begin_inset Quotes srd
1701 \begin_inset Quotes srd
1709 \begin_inset Quotes srd
1713 \begin_inset Quotes srd
1721 \begin_inset Quotes srd
1725 \begin_inset Quotes srd
1732 sdccconf_h_dir_separator=
1733 \begin_inset Quotes srd
1745 \begin_inset Quotes srd
1765 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1776 -C' turns on caching, which gives a little bit extra speed.
1777 However if options are changed, it can be necessary to delete the config.cache
1782 \begin_inset LatexCommand \label{sub:Install-paths}
1787 \begin_inset LatexCommand \index{Install paths}
1793 \added_space_top medskip \align center
1795 \begin_inset Tabular
1796 <lyxtabular version="3" rows="5" columns="4">
1798 <column alignment="center" valignment="top" leftline="true" width="0">
1799 <column alignment="center" valignment="top" leftline="true" width="0">
1800 <column alignment="center" valignment="top" leftline="true" width="0">
1801 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1802 <row topline="true" bottomline="true">
1803 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1833 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1844 <row topline="true">
1845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1853 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1871 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1884 <row topline="true">
1885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1900 $DATADIR/ $INCLUDE_DIR_SUFFIX
1903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1908 /usr/local/share/sdcc/include
1911 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1924 <row topline="true">
1925 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1940 $DATADIR/$LIB_DIR_SUFFIX
1943 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1948 /usr/local/share/sdcc/lib
1951 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1964 <row topline="true" bottomline="true">
1965 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1973 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1983 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1988 /usr/local/share/sdcc/doc
1991 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2013 *compiler, preprocessor, assembler, and linker
2019 is auto-appended by the compiler, e.g.
2020 small, large, z80, ds390 etc
2023 The install paths can still be changed during `make install` with e.g.:
2026 make install prefix=$(HOME)/local/sdcc
2029 Of course this doesn't change the search paths compiled into the binaries.
2033 Moreover the install path can be changed by defining DESTDIR
2034 \begin_inset LatexCommand \index{DESTDIR}
2041 make install DESTDIR=$(HOME)/sdcc.rpm/
2044 Please note that DESTDIR must have a trailing slash!
2048 \begin_inset LatexCommand \label{sub:Search-Paths}
2053 \begin_inset LatexCommand \index{Search path}
2060 Some search paths or parts of them are determined by configure variables
2065 , see section above).
2066 Further search paths are determined by environment variables during runtime.
2069 The paths searched when running the compiler are as follows (the first catch
2075 Binary files (preprocessor, assembler and linker)
2081 \begin_inset Tabular
2082 <lyxtabular version="3" rows="4" columns="3">
2084 <column alignment="block" valignment="top" leftline="true" width="0in">
2085 <column alignment="block" valignment="top" leftline="true" width="0in">
2086 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2087 <row topline="true" bottomline="true">
2088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2113 <row topline="true">
2114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2143 <row topline="true">
2144 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2149 Path of argv[0] (if available)
2152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2160 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2169 <row topline="true" bottomline="true">
2170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2178 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2186 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2211 \begin_inset Tabular
2212 <lyxtabular version="3" rows="6" columns="3">
2214 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2215 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2216 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2217 <row topline="true" bottomline="true">
2218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2243 <row topline="true">
2244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2299 <row topline="true">
2300 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2325 <row topline="true">
2326 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2340 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2363 <row topline="true">
2364 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2445 <row topline="true" bottomline="true">
2446 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2462 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2467 /usr/local/share/sdcc/
2472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2500 -nostdinc disables the last two search paths.
2510 With the exception of
2511 \begin_inset Quotes sld
2525 \begin_inset Quotes srd
2532 is auto-appended by the compiler (e.g.
2533 small, large, z80, ds390 etc.).
2540 \begin_inset Tabular
2541 <lyxtabular version="3" rows="6" columns="3">
2543 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2544 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2545 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2546 <row topline="true" bottomline="true">
2547 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2555 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2563 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2572 <row topline="true">
2573 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2591 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2628 <row topline="true">
2629 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2641 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2668 <row topline="true">
2669 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2680 $LIB_DIR_SUFFIX/<model>
2683 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2697 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2714 <row topline="true">
2715 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2730 $LIB_DIR_SUFFIX/<model>
2733 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2842 <row topline="true" bottomline="true">
2843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2852 $LIB_DIR_SUFFIX/<model>
2855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2860 /usr/local/share/sdcc/
2867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2885 Don't delete any of the stray spaces in the table above without checking
2886 the HTML output (last line)!
2902 -nostdlib disables the last two search paths.
2906 \begin_inset LatexCommand \index{Building SDCC}
2913 Building SDCC on Linux
2914 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2923 Download the source package
2925 either from the SDCC CVS repository or from the nightly snapshots
2927 , it will be named something like sdcc
2938 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2947 Bring up a command line terminal, such as xterm.
2952 Unpack the file using a command like:
2955 "tar -xvzf sdcc.src.tar.gz
2960 , this will create a sub-directory called sdcc with all of the sources.
2963 Change directory into the main SDCC directory, for example type:
2980 This configures the package for compilation on your system.
2996 All of the source packages will compile, this can take a while.
3012 This copies the binary executables, the include files, the libraries and
3013 the documentation to the install directories.
3014 Proceed with section
3015 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3022 Building SDCC on OSX 2.x
3025 Follow the instruction for Linux.
3029 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3030 )) fails to compile SDCC.
3031 Fortunately there's also gcc 2.9.x installed, which works fine.
3032 This compiler can be selected by running 'configure' with:
3035 ./configure CC=gcc2 CXX=g++2
3038 Cross compiling SDCC on Linux for Windows
3041 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3042 See section 'Configure Options'.
3045 Building SDCC on Windows
3048 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3050 They use Unix-sockets, which are not available on Win32.
3053 Building SDCC using Cygwin and Mingw32
3056 For building and installing a Cygwin executable follow the instructions
3062 \begin_inset Quotes sld
3066 \begin_inset Quotes srd
3069 Win32-binary can be built, which will not need the Cygwin-DLL.
3070 For the necessary 'configure' options see section 'configure options' or
3071 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3075 In order to install Cygwin on Windows download setup.exe from
3076 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3082 \begin_inset Quotes sld
3085 default text file type
3086 \begin_inset Quotes srd
3090 \begin_inset Quotes sld
3094 \begin_inset Quotes srd
3097 and download/install at least the following packages.
3098 Some packages are selected by default, others will be automatically selected
3099 because of dependencies with the manually selected packages.
3100 Never deselect these packages!
3109 gcc ; version 3.x is fine, no need to use the old 2.9x
3112 binutils ; selected with gcc
3118 rxvt ; a nice console, which makes life much easier under windoze (see below)
3121 man ; not really needed for building SDCC, but you'll miss it sooner or
3125 less ; not really needed for building SDCC, but you'll miss it sooner or
3129 cvs ; only if you use CVS access
3132 If you want to develop something you'll need:
3135 python ; for the regression tests
3138 gdb ; the gnu debugger, together with the nice GUI
3139 \begin_inset Quotes sld
3143 \begin_inset Quotes srd
3149 openssh ; to access the CF or commit changes
3152 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3153 use autoconf-stable!
3156 rxvt is a nice console with history.
3157 Replace in your cygwin.bat the line
3176 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3179 -bg black -fg white -geometry 100x65 -e bash -
3192 Text selected with the mouse is automatically copied to the clipboard, pasting
3193 works with shift-insert.
3197 The other good tip is to make sure you have no //c/-style paths anywhere,
3198 use /cygdrive/c/ instead.
3199 Using // invokes a network lookup which is very slow.
3201 \begin_inset Quotes sld
3205 \begin_inset Quotes srd
3208 is too long, you can change it with e.g.
3214 SDCC sources use the unix line ending LF.
3215 Life is much easier, if you store the source tree on a drive which is mounted
3217 And use an editor which can handle LF-only line endings.
3218 Make sure not to commit files with windows line endings.
3219 The tabulator spacing
3220 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3224 used in the project is 8.
3225 Although a tabulator spacing of 8 is a sensible choice for programmers
3226 (it's a power of 2 and allows to display 8/16 bit signed variables without
3227 loosing columns) the plan is to move towards using only spaces in the source.
3230 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3235 Download the source package
3237 either from the SDCC CVS repository or from the
3238 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3244 , it will be named something like sdcc
3251 SDCC is distributed with all the projects, workspaces, and files you need
3252 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3253 doesn't build under MSVC).
3254 The workspace name is 'sdcc.dsw'.
3255 Please note that as it is now, all the executables are created in a folder
3259 Once built you need to copy the executables from sdcc
3263 bin before running SDCC.
3268 WARNING: Visual studio is very picky with line terminations; it expects
3269 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3270 If you are getting a message such as "This makefile was not generated by
3271 Developer Studio etc.
3273 \begin_inset Quotes srd
3276 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3277 need to convert the Unix style line endings to DOS style line endings.
3278 To do so you can use the
3279 \begin_inset Quotes sld
3283 \begin_inset Quotes srd
3286 utility freely available on the internet.
3287 Doug Hawkins reported in the sdcc-user list that this works:
3295 SDCC> unix2dos sdcc.dsw
3301 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3305 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3306 flex.exe, and gawk.exe.
3307 One good place to get them is
3308 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3316 Download the file UnxUtils
3317 \begin_inset LatexCommand \index{UnxUtils}
3322 Now you have to install the utilities and setup MSVC so it can locate the
3324 Here there are two alternatives (choose one!):
3331 a) Extract UnxUtils.zip to your C:
3333 hard disk PRESERVING the original paths, otherwise bison won't work.
3334 (If you are using WinZip make certain that 'Use folder names' is selected)
3338 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3339 in 'Show directories for:' select 'Executable files', and in the directories
3340 window add a new path: 'C:
3350 (As a side effect, you get a bunch of Unix utilities that could be useful,
3351 such as diff and patch.)
3358 This one avoids extracting a bunch of files you may not use, but requires
3363 a) Create a directory were to put the tools needed, or use a directory already
3371 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3372 to such directory WITHOUT preserving the original paths.
3373 (If you are using WinZip make certain that 'Use folder names' is not selected)
3377 c) Rename bison.exe to '_bison.exe'.
3381 d) Create a batch file 'bison.bat' in 'C:
3385 ' and add these lines:
3405 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3409 Steps 'c' and 'd' are needed because bison requires by default that the
3410 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3411 '/usr/local/share/' I think.
3412 So it is necessary to tell bison where those files are located if they
3413 are not in such directory.
3414 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3418 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3419 in 'Show directories for:' select 'Executable files', and in the directories
3420 window add a new path: 'c:
3423 Note that you can use any other path instead of 'c:
3425 util', even the path where the Visual C++ tools are, probably: 'C:
3429 Microsoft Visual Studio
3434 So you don't have to execute step 'e' :)
3438 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3439 the executables from sdcc
3443 bin, and you can compile using SDCC.
3446 Building SDCC Using Borland
3449 From the sdcc directory, run the command "make -f Makefile.bcc".
3450 This should regenerate all the .exe files in the bin directory except for
3451 sdcdb.exe (which currently doesn't build under Borland C++).
3454 If you modify any source files and need to rebuild, be aware that the dependenci
3455 es may not be correctly calculated.
3456 The safest option is to delete all .obj files and run the build again.
3457 From a Cygwin BASH prompt, this can easily be done with the command (be
3458 sure you are in the sdcc directory):
3468 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3470 ) -print -exec rm {}
3479 or on Windows NT/2000/XP from the command prompt with the command:
3486 del /s *.obj *.lib *.rul
3489 from the sdcc directory.
3492 Windows Install Using a ZIP Package
3495 Download the binary zip package from
3496 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3500 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3501 This should unpack to a group of sub-directories.
3502 An example directory structure after unpacking the mingw32 package is:
3507 bin for the executables, c:
3515 lib for the include and libraries.
3518 Adjust your environment variable PATH to include the location of the bin
3519 directory or start sdcc using the full path.
3522 Windows Install Using the Setup Program
3523 \begin_inset LatexCommand \label{sub:Windows-Install}
3530 Download the setup program
3532 sdcc-x.y.z-setup.exe
3534 for an official release from
3537 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3541 or a setup program for one of the snapshots
3543 sdcc_yyyymmdd_setup.exe
3546 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3551 A windows typical installer will guide you through the installation process.
3554 Building the Documentation
3557 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3558 as changing into the doc directory and typing
3562 \begin_inset Quotes srd
3566 \begin_inset Quotes srd
3573 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3576 \begin_inset LatexCommand \url{http://www.lyx.org}
3580 as editor this is straightforward.
3581 Prebuilt documentation in html and pdf format is available from
3582 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3589 Reading the Documentation
3592 Currently reading the document in pdf format is recommended, as for unknown
3593 reason the hyperlinks are working there whereas in the html version they
3600 If you should know why please drop us a note
3606 You'll find the pdf version
3607 \begin_inset LatexCommand \index{PDF version of this document}
3612 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3620 \begin_inset LatexCommand \index{HTML version of this document}
3625 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3631 This documentation is in some aspects different from a commercial documentation:
3635 It tries to document SDCC for several processor architectures in one document
3636 (commercially these probably would be separate documents/products).
3638 \begin_inset LatexCommand \index{Status of documentation}
3642 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3644 Z80, PIC14, PIC16 and HC08.
3647 There are many references pointing away from this documentation.
3648 Don't let this distract you.
3650 was a reference like
3651 \begin_inset LatexCommand \url{http://www.opencores.org}
3655 together with a statement
3656 \begin_inset Quotes sld
3659 some processors which are targetted by SDCC can be implemented in a
3676 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3681 \begin_inset Quotes srd
3684 we expect you to have a quick look there and come back.
3685 If you read this you are on the right track.
3688 Some sections attribute more space to problems, restrictions and warnings
3689 than to the solution.
3692 The installation section and the section about the debugger is intimidating.
3695 There are still lots of typos and there are more different writing styles
3699 Testing the SDCC Compiler
3700 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3707 The first thing you should do after installing your SDCC compiler is to
3723 \begin_inset LatexCommand \index{version}
3730 at the prompt, and the program should run and tell you the version.
3731 If it doesn't run, or gives a message about not finding sdcc program, then
3732 you need to check over your installation.
3733 Make sure that the sdcc bin directory is in your executable search path
3734 defined by the PATH environment setting (
3739 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3746 Install trouble-shooting for suggestions
3749 Make sure that the sdcc program is in the bin folder, if not perhaps something
3750 did not install correctly.
3758 is commonly installed as described in section
3759 \begin_inset Quotes sld
3762 Install and search paths
3763 \begin_inset Quotes srd
3772 Make sure the compiler works on a very simple example.
3773 Type in the following test.c program using your favorite
3799 Compile this using the following command:
3808 If all goes well, the compiler will generate a test.asm and test.rel file.
3809 Congratulations, you've just compiled your first program with SDCC.
3810 We used the -c option to tell SDCC not to link the generated code, just
3811 to keep things simple for this step.
3819 The next step is to try it with the linker.
3829 If all goes well the compiler will link with the libraries and produce
3830 a test.ihx output file.
3835 (no test.ihx, and the linker generates warnings), then the problem is most
3844 usr/local/share/sdcc/lib directory
3851 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3858 Install trouble-shooting for suggestions).
3866 The final test is to ensure
3874 header files and libraries.
3875 Edit test.c and change it to the following:
3892 strcpy(str1, "testing");
3899 Compile this by typing
3906 This should generate a test.ihx output file, and it should give no warnings
3907 such as not finding the string.h file.
3908 If it cannot find the string.h file, then the problem is that
3912 cannot find the /usr/local/share/sdcc/include directory
3919 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3926 Install trouble-shooting section for suggestions).
3944 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3948 to find exactly where SDCC is looking for the include and lib files.
3951 Install Trouble-shooting
3952 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3957 \begin_inset LatexCommand \index{Install trouble-shooting}
3964 If SDCC does not build correctly
3967 A thing to try is starting from scratch by unpacking the .tgz source package
3968 again in an empty directory.
3976 ./configure 2>&1 | tee configure.log
3990 make 2>&1 | tee make.log
3997 If anything goes wrong, you can review the log files to locate the problem.
3998 Or a relevant part of this can be attached to an email that could be helpful
3999 when requesting help from the mailing list.
4003 \begin_inset Quotes sld
4007 \begin_inset Quotes srd
4014 \begin_inset Quotes sld
4018 \begin_inset Quotes srd
4021 command is a script that analyzes your system and performs some configuration
4022 to ensure the source package compiles on your system.
4023 It will take a few minutes to run, and will compile a few tests to determine
4024 what compiler features are installed.
4028 \begin_inset Quotes sld
4032 \begin_inset Quotes srd
4038 This runs the GNU make tool, which automatically compiles all the source
4039 packages into the final installed binary executables.
4043 \begin_inset Quotes sld
4047 \begin_inset Quotes erd
4053 This will install the compiler, other executables libraries and include
4054 files into the appropriate directories.
4056 \begin_inset LatexCommand \ref{sub:Install-paths}
4062 \begin_inset LatexCommand \ref{sub:Search-Paths}
4067 about install and search paths.
4069 On most systems you will need super-user privileges to do this.
4075 SDCC is not just a compiler, but a collection of tools by various developers.
4076 These include linkers, assemblers, simulators and other components.
4077 Here is a summary of some of the components.
4078 Note that the included simulator and assembler have separate documentation
4079 which you can find in the source package in their respective directories.
4080 As SDCC grows to include support for other processors, other packages from
4081 various developers are included and may have their own sets of documentation.
4085 You might want to look at the files which are installed in <installdir>.
4086 At the time of this writing, we find the following programs for gcc-builds:
4090 In <installdir>/bin:
4093 sdcc - The compiler.
4096 sdcpp - The C preprocessor.
4099 asx8051 - The assembler for 8051 type processors.
4106 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4109 aslink -The linker for 8051 type processors.
4116 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4119 s51 - The ucSim 8051 simulator.
4122 sdcdb - The source debugger.
4125 packihx - A tool to pack (compress) Intel hex files.
4128 In <installdir>/share/sdcc/include
4134 In <installdir>/share/sdcc/lib
4137 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4141 In <installdir>/share/sdcc/doc
4147 As development for other processors proceeds, this list will expand to include
4148 executables to support processors like AVR, PIC, etc.
4154 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4155 the assembler and linkage editor.
4158 sdcpp - The C-Preprocessor
4162 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4166 is a modified version of the GNU preprocessor.
4167 The C preprocessor is used to pull in #include sources, process #ifdef
4168 statements, #defines and so on.
4179 - The Assemblers and Linkage Editors
4182 This is retargettable assembler & linkage editor, it was developed by Alan
4184 John Hartman created the version for 8051, and I (Sandeep) have made some
4185 enhancements and bug fixes for it to work properly with SDCC.
4192 \begin_inset LatexCommand \index{s51}
4196 is a freeware, opensource simulator developed by Daniel Drotos.
4197 The simulator is built as part of the build process.
4198 For more information visit Daniel's web site at:
4199 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4204 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4208 sdcdb - Source Level Debugger
4212 \begin_inset LatexCommand \index{sdcdb (debugger)}
4216 is the companion source level debugger.
4217 More about sdcdb in section
4218 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4223 The current version of the debugger uses Daniel's Simulator S51
4224 \begin_inset LatexCommand \index{s51}
4228 , but can be easily changed to use other simulators.
4238 Single Source File Projects
4241 For single source file 8051 projects the process is very simple.
4242 Compile your programs with the following command
4245 "sdcc sourcefile.c".
4249 This will compile, assemble and link your source file.
4250 Output files are as follows:
4254 \begin_inset LatexCommand \index{<file>.asm}
4259 \begin_inset LatexCommand \index{Assembler source}
4263 file created by the compiler
4267 \begin_inset LatexCommand \index{<file>.lst}
4272 \begin_inset LatexCommand \index{Assembler listing}
4276 file created by the Assembler
4280 \begin_inset LatexCommand \index{<file>.rst}
4285 \begin_inset LatexCommand \index{Assembler listing}
4289 file updated with linkedit information, created by linkage editor
4293 \begin_inset LatexCommand \index{<file>.sym}
4298 \begin_inset LatexCommand \index{Symbol listing}
4302 for the sourcefile, created by the assembler
4306 \begin_inset LatexCommand \index{<file>.rel}
4311 \begin_inset LatexCommand \index{<file>.o}
4316 \begin_inset LatexCommand \index{Object file}
4320 created by the assembler, input to Linkage editor
4324 \begin_inset LatexCommand \index{<file>.map}
4329 \begin_inset LatexCommand \index{Memory map}
4333 for the load module, created by the Linker
4337 \begin_inset LatexCommand \index{<file>.mem}
4341 - A file with a summary of the memory usage
4345 \begin_inset LatexCommand \index{<file>.ihx}
4349 - The load module in Intel hex format
4350 \begin_inset LatexCommand \index{Intel hex format}
4354 (you can select the Motorola S19 format
4355 \begin_inset LatexCommand \index{Motorola S19 format}
4370 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4375 If you need another format you might want to use
4382 \begin_inset LatexCommand \index{objdump (tool)}
4393 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4398 Both formats are documented in the documentation of srecord
4399 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4407 \begin_inset LatexCommand \index{<file>.adb}
4411 - An intermediate file containing debug information needed to create the
4423 \begin_inset LatexCommand \index{-\/-debug}
4431 \begin_inset LatexCommand \index{<file>.cdb}
4435 - An optional file (with -
4445 -debug) containing debug information.
4446 The format is documented in cdbfileformat.pdf
4451 \begin_inset LatexCommand \index{<file> (no extension)}
4455 An optional AOMF or AOMF51
4456 \begin_inset LatexCommand \index{AOMF, AOMF51}
4460 file containing debug information (generated with option -
4487 ormat is commonly used by third party tools (debuggers
4488 \begin_inset LatexCommand \index{Debugger}
4492 , simulators, emulators)
4496 \begin_inset LatexCommand \index{<file>.dump*}
4500 - Dump file to debug the compiler it self (generated with option -
4510 -dumpall) (see section
4511 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4517 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4523 \begin_inset Quotes sld
4526 Anatomy of the compiler
4527 \begin_inset Quotes srd
4533 Projects with Multiple Source Files
4536 SDCC can compile only ONE file at a time.
4537 Let us for example assume that you have a project containing the following
4542 foo1.c (contains some functions)
4544 foo2.c (contains some more functions)
4546 foomain.c (contains more functions and the function main)
4554 The first two files will need to be compiled separately with the commands:
4586 Then compile the source file containing the
4591 \begin_inset LatexCommand \index{Linker}
4595 the files together with the following command:
4603 foomain.c\SpecialChar ~
4604 foo1.rel\SpecialChar ~
4609 \begin_inset LatexCommand \index{<file>.rel}
4621 can be separately compiled as well:
4632 sdcc foomain.rel foo1.rel foo2.rel
4639 The file containing the
4654 file specified in the command line, since the linkage editor processes
4655 file in the order they are presented to it.
4656 The linker is invoked from SDCC using a script file with extension .lnk
4657 \begin_inset LatexCommand \index{<file>.lnk}
4662 You can view this file to troubleshoot linking problems such as those arising
4663 from missing libraries.
4666 Projects with Additional Libraries
4667 \begin_inset LatexCommand \index{Libraries}
4674 Some reusable routines may be compiled into a library, see the documentation
4675 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4679 \begin_inset LatexCommand \index{<file>.lib}
4686 Libraries created in this manner can be included in the command line.
4687 Make sure you include the -L <library-path> option to tell the linker where
4688 to look for these files if they are not in the current directory.
4689 Here is an example, assuming you have the source file
4701 (if that is not the same as your current project):
4708 sdcc foomain.c foolib.lib -L mylib
4719 must be an absolute path name.
4723 The most efficient way to use libraries is to keep separate modules in separate
4725 The lib file now should name all the modules.rel
4726 \begin_inset LatexCommand \index{<file>.rel}
4731 For an example see the standard library file
4735 in the directory <installdir>/share/lib/small.
4738 Using sdcclib to Create and Manage Libraries
4739 \begin_inset LatexCommand \index{sdcclib}
4746 Alternatively, instead of having a .rel file for each entry on the library
4747 file as described in the preceding section, sdcclib can be used to embed
4748 all the modules belonging to such library in the library file itself.
4749 This results in a larger library file, but it greatly reduces the number
4750 of disk files accessed by the linker.
4751 Additionally, the packed library file contains an index of all include
4752 modules and symbols that significantly speeds up the linking process.
4753 To display a list of options supported by sdcclib type:
4762 \begin_inset LatexCommand \index{sdcclib}
4773 To create a new library file, start by compiling all the required modules.
4811 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4813 The next step is to add the .rel files to the library file:
4821 sdcclib libint.lib _divsint.rel
4824 \begin_inset LatexCommand \index{sdcclib}
4834 sdcclib libint.lib _divuint.rel
4840 sdcclib libint.lib _modsint.rel
4846 sdcclib libint.lib _moduint.rel
4852 sdcclib libint.lib _mulint.rel
4859 If the file already exists in the library, it will be replaced.
4860 To see what modules and symbols are included in the library, options -s
4861 and -m are available.
4869 sdcclib -s libint.lib
4872 \begin_inset LatexCommand \index{sdcclib}
4982 If the source files are compiled using -
4993 \begin_inset LatexCommand \index{-\/-debug}
4997 , the corresponding debug information file .adb will be include in the library
4999 The library files created with sdcclib are plain text files, so they can
5000 be viewed with a text editor.
5001 It is not recomended to modify a library file created with sdcclib using
5002 a text editor, as there are file indexes numbers located accross the file
5003 used by the linker to quickly locate the required module to link.
5004 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5005 it can be safely deleted, since all the information required for linking
5006 is embedded in the library file itself.
5007 Library files created using sdcclib are used as described in the preceding
5011 Command Line Options
5012 \begin_inset LatexCommand \index{Command Line Options}
5019 Processor Selection Options
5020 \begin_inset LatexCommand \index{Options processor selection}
5025 \begin_inset LatexCommand \index{Processor selection options}
5031 \labelwidthstring 00.00.0000
5036 \begin_inset LatexCommand \index{-mmcs51}
5042 Generate code for the Intel MCS51
5043 \begin_inset LatexCommand \index{MCS51}
5047 family of processors.
5048 This is the default processor target.
5050 \labelwidthstring 00.00.0000
5055 \begin_inset LatexCommand \index{-mds390}
5061 Generate code for the Dallas DS80C390
5062 \begin_inset LatexCommand \index{DS80C390}
5068 \labelwidthstring 00.00.0000
5073 \begin_inset LatexCommand \index{-mds400}
5079 Generate code for the Dallas DS80C400
5080 \begin_inset LatexCommand \index{DS80C400}
5086 \labelwidthstring 00.00.0000
5091 \begin_inset LatexCommand \index{-mhc08}
5097 Generate code for the Freescale/Motorola HC08
5098 \begin_inset LatexCommand \index{HC08}
5102 family of processors.
5104 \labelwidthstring 00.00.0000
5109 \begin_inset LatexCommand \index{-mz80}
5115 Generate code for the Zilog Z80
5116 \begin_inset LatexCommand \index{Z80}
5120 family of processors.
5122 \labelwidthstring 00.00.0000
5127 \begin_inset LatexCommand \index{-mgbz80}
5133 Generate code for the GameBoy Z80
5134 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5138 processor (Not actively maintained).
5140 \labelwidthstring 00.00.0000
5145 \begin_inset LatexCommand \index{-mavr}
5151 Generate code for the Atmel AVR
5152 \begin_inset LatexCommand \index{AVR}
5156 processor (In development, not complete).
5157 AVR users should probably have a look at winavr
5158 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5163 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5170 I think it is fair to direct users there for now.
5171 Open source is also about avoiding unnecessary work .
5172 But I didn't find the 'official' link.
5174 \labelwidthstring 00.00.0000
5179 \begin_inset LatexCommand \index{-mpic14}
5185 Generate code for the Microchip PIC 14
5186 \begin_inset LatexCommand \index{PIC14}
5190 -bit processors (p16f84 and variants.
5191 In development, not complete).
5194 p16f627 p16f628 p16f84 p16f873 p16f877?
5196 \labelwidthstring 00.00.0000
5201 \begin_inset LatexCommand \index{-mpic16}
5207 Generate code for the Microchip PIC 16
5208 \begin_inset LatexCommand \index{PIC16}
5212 -bit processors (p18f452 and variants.
5213 In development, not complete).
5215 \labelwidthstring 00.00.0000
5221 Generate code for the Toshiba TLCS-900H
5222 \begin_inset LatexCommand \index{TLCS-900H}
5226 processor (Not maintained, not complete).
5228 \labelwidthstring 00.00.0000
5233 \begin_inset LatexCommand \index{-mxa51}
5239 Generate code for the Phillips XA51
5240 \begin_inset LatexCommand \index{XA51}
5244 processor (Not maintained, not complete).
5247 Preprocessor Options
5248 \begin_inset LatexCommand \index{Options preprocessor}
5253 \begin_inset LatexCommand \index{Preprocessor options}
5258 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5264 \labelwidthstring 00.00.0000
5269 \begin_inset LatexCommand \index{-I<path>}
5275 The additional location where the pre processor will look for <..h> or
5276 \begin_inset Quotes eld
5280 \begin_inset Quotes erd
5285 \labelwidthstring 00.00.0000
5290 \begin_inset LatexCommand \index{-D<macro[=value]>}
5296 Command line definition of macros.
5297 Passed to the preprocessor.
5299 \labelwidthstring 00.00.0000
5304 \begin_inset LatexCommand \index{-M}
5310 Tell the preprocessor to output a rule suitable for make describing the
5311 dependencies of each object file.
5312 For each source file, the preprocessor outputs one make-rule whose target
5313 is the object file name for that source file and whose dependencies are
5314 all the files `#include'd in it.
5315 This rule may be a single line or may be continued with `
5317 '-newline if it is long.
5318 The list of rules is printed on standard output instead of the preprocessed
5321 \begin_inset LatexCommand \index{-E}
5327 \labelwidthstring 00.00.0000
5332 \begin_inset LatexCommand \index{-C}
5338 Tell the preprocessor not to discard comments.
5339 Used with the `-E' option.
5341 \labelwidthstring 00.00.0000
5346 \begin_inset LatexCommand \index{-MM}
5357 Like `-M' but the output mentions only the user header files included with
5359 \begin_inset Quotes eld
5363 System header files included with `#include <file>' are omitted.
5365 \labelwidthstring 00.00.0000
5370 \begin_inset LatexCommand \index{-Aquestion(answer)}
5376 Assert the answer answer for question, in case it is tested with a preprocessor
5377 conditional such as `#if #question(answer)'.
5378 `-A-' disables the standard assertions that normally describe the target
5381 \labelwidthstring 00.00.0000
5386 \begin_inset LatexCommand \index{-Umacro}
5392 Undefine macro macro.
5393 `-U' options are evaluated after all `-D' options, but before any `-include'
5394 and `-imacros' options.
5396 \labelwidthstring 00.00.0000
5401 \begin_inset LatexCommand \index{-dM}
5407 Tell the preprocessor to output only a list of the macro definitions that
5408 are in effect at the end of preprocessing.
5409 Used with the `-E' option.
5411 \labelwidthstring 00.00.0000
5416 \begin_inset LatexCommand \index{-dD}
5422 Tell the preprocessor to pass all macro definitions into the output, in
5423 their proper sequence in the rest of the output.
5425 \labelwidthstring 00.00.0000
5430 \begin_inset LatexCommand \index{-dN}
5441 Like `-dD' except that the macro arguments and contents are omitted.
5442 Only `#define name' is included in the output.
5444 \labelwidthstring 00.00.0000
5449 preprocessorOption[,preprocessorOption]
5452 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5457 Pass the preprocessorOption to the preprocessor
5462 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5467 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5468 Collection (gcc), if you need more dedicated options please refer to the
5470 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5478 \begin_inset LatexCommand \index{Options linker}
5483 \begin_inset LatexCommand \index{Linker options}
5489 \labelwidthstring 00.00.0000
5509 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5514 \begin_inset LatexCommand \index{-L -\/-lib-path}
5521 <absolute path to additional libraries> This option is passed to the linkage
5522 editor's additional libraries
5523 \begin_inset LatexCommand \index{Libraries}
5528 The path name must be absolute.
5529 Additional library files may be specified in the command line.
5530 See section Compiling programs for more details.
5532 \labelwidthstring 00.00.0000
5549 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5554 <Value> The start location of the external ram
5555 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5559 , default value is 0.
5560 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5570 -xram-loc 0x8000 or -
5582 \labelwidthstring 00.00.0000
5599 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5604 <Value> The start location of the code
5605 \begin_inset LatexCommand \index{code}
5609 segment, default value 0.
5610 Note when this option is used the interrupt vector table is also relocated
5611 to the given address.
5612 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5622 -code-loc 0x8000 or -
5634 \labelwidthstring 00.00.0000
5651 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5656 <Value> By default the stack
5657 \begin_inset LatexCommand \index{stack}
5661 is placed after the data segment.
5662 Using this option the stack can be placed anywhere in the internal memory
5664 The value entered can be in Hexadecimal or Decimal format, e.g.
5675 -stack-loc 0x20 or -
5686 Since the sp register is incremented before a push or call, the initial
5687 sp will be set to one byte prior the provided value.
5688 The provided value should not overlap any other memory areas such as used
5689 register banks or the data segment and with enough space for the current
5707 \begin_inset LatexCommand \index{-\/-pack-iram}
5711 option (which is now a default setting) will override this setting, so
5712 you should also specify the
5728 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5732 option if you need to manually place the stack.
5734 \labelwidthstring 00.00.0000
5751 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5756 <Value> The start location of the internal ram data
5757 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5762 The value entered can be in Hexadecimal or Decimal format, eg.
5784 (By default, the start location of the internal ram data segment is set
5785 as low as possible in memory, taking into account the used register banks
5786 and the bit segment at address 0x20.
5787 For example if register banks 0 and 1 are used without bit variables, the
5788 data segment will be set, if -
5798 -data-loc is not used, to location 0x10.)
5800 \labelwidthstring 00.00.0000
5817 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5822 <Value> The start location of the indirectly addressable internal ram
5823 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5827 of the 8051, default value is 0x80.
5828 The value entered can be in Hexadecimal or Decimal format, eg.
5839 -idata-loc 0x88 or -
5851 \labelwidthstring 00.00.0000
5868 <Value> The start location of the bit
5869 \begin_inset LatexCommand \index{bit}
5873 addressable internal ram of the 8051.
5879 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5882 \labelwidthstring 00.00.0000
5897 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5906 The linker output (final object code) is in Intel Hex format.
5907 \begin_inset LatexCommand \index{Intel hex format}
5911 This is the default option.
5912 The format itself is documented in the documentation of srecord
5913 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5919 \labelwidthstring 00.00.0000
5934 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5943 The linker output (final object code) is in Motorola S19 format
5944 \begin_inset LatexCommand \index{Motorola S19 format}
5949 The format itself is documented in the documentation of srecord.
5951 \labelwidthstring 00.00.0000
5966 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5975 The linker output (final object code) is in ELF format
5976 \begin_inset LatexCommand \index{ELF format}
5981 (Currently only supported for the HC08 processors)
5983 \labelwidthstring 00.00.0000
5988 linkOption[,linkOption]
5991 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5996 Pass the linkOption to the linker.
5997 See file sdcc/as/doc/asxhtm.html for more on linker options.
6001 \begin_inset LatexCommand \index{Options MCS51}
6006 \begin_inset LatexCommand \index{MCS51 options}
6012 \labelwidthstring 00.00.0000
6027 \begin_inset LatexCommand \index{-\/-model-small}
6038 Generate code for Small Model programs, see section Memory Models for more
6040 This is the default model.
6042 \labelwidthstring 00.00.0000
6057 \begin_inset LatexCommand \index{-\/-model-large}
6063 Generate code for Large model programs, see section Memory Models for more
6065 If this option is used all source files in the project have to be compiled
6068 \labelwidthstring 00.00.0000
6083 \begin_inset LatexCommand \index{-\/-xstack}
6089 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6090 variables and passing parameters.
6092 \begin_inset LatexCommand \ref{sub:External-Stack}
6097 External Stack for more details.
6099 \labelwidthstring 00.00.0000
6117 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6121 Causes the linker to check if the internal ram usage is within limits of
6124 \labelwidthstring 00.00.0000
6142 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6146 Causes the linker to check if the external ram usage is within limits of
6149 \labelwidthstring 00.00.0000
6167 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6171 Causes the linker to check if the code memory usage is within limits of
6174 \labelwidthstring 00.00.0000
6192 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6196 Causes the linker to check if there is at minimum <Value> bytes for stack.
6198 \labelwidthstring 00.00.0000
6216 \begin_inset LatexCommand \index{-\/-pack-iram}
6220 Causes the linker to use unused register banks for data variables and pack
6221 data, idata and stack together.
6222 This is the default now.
6224 \labelwidthstring 00.00.0000
6242 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6246 Causes the linker to use old style for allocating memory areas.
6249 DS390 / DS400 Options
6250 \begin_inset LatexCommand \index{Options DS390}
6255 \begin_inset LatexCommand \index{DS390 options}
6261 \labelwidthstring 00.00.0000
6278 \begin_inset LatexCommand \index{-\/-model-flat24}
6288 Generate 24-bit flat mode code.
6289 This is the one and only that the ds390 code generator supports right now
6290 and is default when using
6295 See section Memory Models for more details.
6297 \labelwidthstring 00.00.0000
6312 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6318 disable interrupts during ESP:SP updates.
6320 \labelwidthstring 00.00.0000
6337 \begin_inset LatexCommand \index{-\/-stack-10bit}
6341 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6342 This is the one and only that the ds390 code generator supports right now
6343 and is default when using
6348 In this mode, the stack is located in the lower 1K of the internal RAM,
6349 which is mapped to 0x400000.
6350 Note that the support is incomplete, since it still uses a single byte
6351 as the stack pointer.
6352 This means that only the lower 256 bytes of the potential 1K stack space
6353 will actually be used.
6354 However, this does allow you to reclaim the precious 256 bytes of low RAM
6355 for use for the DATA and IDATA segments.
6356 The compiler will not generate any code to put the processor into 10 bit
6358 It is important to ensure that the processor is in this mode before calling
6359 any re-entrant functions compiled with this option.
6360 In principle, this should work with the
6373 \begin_inset LatexCommand \index{-\/-stack-auto}
6379 option, but that has not been tested.
6380 It is incompatible with the
6393 \begin_inset LatexCommand \index{-\/-xstack}
6400 It also only makes sense if the processor is in 24 bit contiguous addressing
6413 -model-flat24 option
6417 \labelwidthstring 00.00.0000
6432 \begin_inset LatexCommand \index{-\/-stack-probe}
6438 insert call to function __stack_probe at each function prologue.
6440 \labelwidthstring 00.00.0000
6455 \begin_inset LatexCommand \index{-\/-tini-libid}
6461 <nnnn> LibraryID used in -mTININative.
6464 \labelwidthstring 00.00.0000
6479 \begin_inset LatexCommand \index{-\/-use-accelerator}
6485 generate code for DS390 Arithmetic Accelerator.
6490 \begin_inset LatexCommand \index{Options Z80}
6495 \begin_inset LatexCommand \index{Z80 options}
6501 \labelwidthstring 00.00.0000
6518 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6528 Force a called function to always save BC.
6530 \labelwidthstring 00.00.0000
6547 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6551 When linking, skip the standard crt0.o object file.
6552 You must provide your own crt0.o for your system when linking.
6556 Optimization Options
6557 \begin_inset LatexCommand \index{Options optimization}
6562 \begin_inset LatexCommand \index{Optimization options}
6568 \labelwidthstring 00.00.0000
6583 \begin_inset LatexCommand \index{-\/-nogcse}
6589 Will not do global subexpression elimination, this option may be used when
6590 the compiler creates undesirably large stack/data spaces to store compiler
6600 \begin_inset LatexCommand \index{sloc (spill location)}
6605 A warning message will be generated when this happens and the compiler
6606 will indicate the number of extra bytes it allocated.
6607 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6609 \begin_inset LatexCommand \index{\#pragma nogcse}
6613 can be used to turn off global subexpression elimination
6614 \begin_inset LatexCommand \index{Subexpression elimination}
6618 for a given function only.
6620 \labelwidthstring 00.00.0000
6635 \begin_inset LatexCommand \index{-\/-noinvariant}
6641 Will not do loop invariant optimizations, this may be turned off for reasons
6642 explained for the previous option.
6643 For more details of loop optimizations performed see Loop Invariants in
6645 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6650 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6652 \begin_inset LatexCommand \index{\#pragma noinvariant}
6656 can be used to turn off invariant optimizations for a given function only.
6658 \labelwidthstring 00.00.0000
6673 \begin_inset LatexCommand \index{-\/-noinduction}
6679 Will not do loop induction optimizations, see section strength reduction
6681 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6683 \begin_inset LatexCommand \index{\#pragma noinduction}
6687 can be used to turn off induction optimizations for a given function only.
6689 \labelwidthstring 00.00.0000
6704 \begin_inset LatexCommand \index{-\/-nojtbound}
6715 Will not generate boundary condition check when switch statements
6716 \begin_inset LatexCommand \index{switch statement}
6720 are implemented using jump-tables.
6722 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6727 Switch Statements for more details.
6728 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6730 \begin_inset LatexCommand \index{\#pragma nojtbound}
6734 can be used to turn off boundary checking for jump tables for a given function
6737 \labelwidthstring 00.00.0000
6752 \begin_inset LatexCommand \index{-\/-noloopreverse}
6761 Will not do loop reversal
6762 \begin_inset LatexCommand \index{Loop reversing}
6768 \labelwidthstring 00.00.0000
6785 \begin_inset LatexCommand \index{-\/-nolabelopt }
6789 Will not optimize labels (makes the dumpfiles more readable).
6791 \labelwidthstring 00.00.0000
6806 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6812 Will not memcpy initialized data from code space into xdata space.
6813 This saves a few bytes in code space if you don't have initialized data
6814 \begin_inset LatexCommand \index{Variable initialization}
6820 \labelwidthstring 00.00.0000
6835 \begin_inset LatexCommand \index{-\/-nooverlay}
6841 The compiler will not overlay parameters and local variables of any function,
6842 see section Parameters and local variables for more details.
6844 \labelwidthstring 00.00.0000
6859 \begin_inset LatexCommand \index{-\/-no-peep}
6865 Disable peep-hole optimization.
6867 \labelwidthstring 00.00.0000
6884 \begin_inset LatexCommand \index{-\/-peep-file}
6889 <filename> This option can be used to use additional rules to be used by
6890 the peep hole optimizer.
6892 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6897 Peep Hole optimizations for details on how to write these rules.
6899 \labelwidthstring 00.00.0000
6914 \begin_inset LatexCommand \index{-\/-peep-asm}
6920 Pass the inline assembler code through the peep hole optimizer.
6921 This can cause unexpected changes to inline assembler code, please go through
6922 the peephole optimizer
6923 \begin_inset LatexCommand \index{Peephole optimizer}
6927 rules defined in the source file tree '<target>/peeph.def' before using
6930 \labelwidthstring 00.00.0000
6945 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6951 The compiler will optimize code generation towards fast code, possibly
6952 at the expense of code size.
6954 \labelwidthstring 00.00.0000
6969 \begin_inset LatexCommand \index{-\/-opt-code-size}
6975 The compiler will optimize code generation towards compact code, possibly
6976 at the expense of code speed.
6980 \begin_inset LatexCommand \index{Options other}
6986 \labelwidthstring 00.00.0000
7002 \begin_inset LatexCommand \index{-\/-compile-only}
7007 \begin_inset LatexCommand \index{-c -\/-compile-only}
7013 will compile and assemble the source, but will not call the linkage editor.
7015 \labelwidthstring 00.00.0000
7034 \begin_inset LatexCommand \index{-\/-c1mode}
7040 reads the preprocessed source from standard input and compiles it.
7041 The file name for the assembler output must be specified using the -o option.
7043 \labelwidthstring 00.00.0000
7048 \begin_inset LatexCommand \index{-E}
7054 Run only the C preprocessor.
7055 Preprocess all the C source files specified and output the results to standard
7058 \labelwidthstring 00.00.0000
7064 \begin_inset LatexCommand \index{-o <path/file>}
7070 The output path resp.
7071 file where everything will be placed.
7072 If the parameter is a path, it must have a trailing slash (or backslash
7073 for the Windows binaries) to be recognized as a path.
7076 \labelwidthstring 00.00.0000
7091 \begin_inset LatexCommand \index{-\/-stack-auto}
7102 All functions in the source file will be compiled as
7107 \begin_inset LatexCommand \index{reentrant}
7112 the parameters and local variables will be allocated on the stack
7113 \begin_inset LatexCommand \index{stack}
7119 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7123 Parameters and Local Variables for more details.
7124 If this option is used all source files in the project should be compiled
7126 It automatically implies --int-long-reent and --float-reent.
7129 \labelwidthstring 00.00.0000
7144 \begin_inset LatexCommand \index{-\/-callee-saves}
7148 function1[,function2][,function3]....
7151 The compiler by default uses a caller saves convention for register saving
7152 across function calls, however this can cause unnecessary register pushing
7153 & popping when calling small functions from larger functions.
7154 This option can be used to switch the register saving convention for the
7155 function names specified.
7156 The compiler will not save registers when calling these functions, no extra
7157 code will be generated at the entry & exit (function prologue
7160 \begin_inset LatexCommand \index{function prologue}
7169 \begin_inset LatexCommand \index{function epilogue}
7175 ) for these functions to save & restore the registers used by these functions,
7176 this can SUBSTANTIALLY reduce code & improve run time performance of the
7178 In the future the compiler (with inter procedural analysis) will be able
7179 to determine the appropriate scheme to use for each function call.
7180 DO NOT use this option for built-in functions such as _mulint..., if this
7181 option is used for a library function the appropriate library function
7182 needs to be recompiled with the same option.
7183 If the project consists of multiple source files then all the source file
7184 should be compiled with the same -
7194 -callee-saves option string.
7195 Also see #pragma\SpecialChar ~
7197 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7203 \labelwidthstring 00.00.0000
7218 \begin_inset LatexCommand \index{-\/-debug}
7227 When this option is used the compiler will generate debug information.
7228 The debug information collected in a file with .cdb extension can be used
7230 For more information see documentation for SDCDB.
7231 Another file with no extension contains debug information in AOMF or AOMF51
7232 \begin_inset LatexCommand \index{AOMF, AOMF51}
7236 format which is commonly used by third party tools.
7238 \labelwidthstring 00.00.0000
7243 \begin_inset LatexCommand \index{-S}
7254 Stop after the stage of compilation proper; do not assemble.
7255 The output is an assembler code file for the input file specified.
7257 \labelwidthstring 00.00.0000
7272 \begin_inset LatexCommand \index{-\/-int-long-reent}
7278 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7279 Note by default these libraries are compiled as non-reentrant.
7280 See section Installation for more details.
7282 \labelwidthstring 00.00.0000
7297 \begin_inset LatexCommand \index{-\/-cyclomatic}
7306 This option will cause the compiler to generate an information message for
7307 each function in the source file.
7308 The message contains some
7312 information about the function.
7313 The number of edges and nodes the compiler detected in the control flow
7314 graph of the function, and most importantly the
7316 cyclomatic complexity
7317 \begin_inset LatexCommand \index{Cyclomatic complexity}
7323 see section on Cyclomatic Complexity for more details.
7325 \labelwidthstring 00.00.0000
7340 \begin_inset LatexCommand \index{-\/-float-reent}
7346 Floating point library is compiled as reentrant
7347 \begin_inset LatexCommand \index{reentrant}
7352 See section Installation for more details.
7354 \labelwidthstring 00.00.0000
7369 \begin_inset LatexCommand \index{-\/-main-return}
7375 This option can be used if the code generated is called by a monitor program
7376 or if the main routine includes an endless loop.
7377 This option might result in slightly smaller code and save two bytes of
7379 The return from the 'main'
7380 \begin_inset LatexCommand \index{main return}
7384 function will return to the function calling main.
7385 The default setting is to lock up i.e.
7392 \labelwidthstring 00.00.0000
7407 \begin_inset LatexCommand \index{-\/-nostdinc}
7413 This will prevent the compiler from passing on the default include path
7414 to the preprocessor.
7416 \labelwidthstring 00.00.0000
7431 \begin_inset LatexCommand \index{-\/-nostdlib}
7437 This will prevent the compiler from passing on the default library
7438 \begin_inset LatexCommand \index{Libraries}
7444 \labelwidthstring 00.00.0000
7459 \begin_inset LatexCommand \index{-\/-verbose}
7465 Shows the various actions the compiler is performing.
7467 \labelwidthstring 00.00.0000
7472 \begin_inset LatexCommand \index{-V}
7478 Shows the actual commands the compiler is executing.
7480 \labelwidthstring 00.00.0000
7495 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7501 Hides your ugly and inefficient c-code from the asm file, so you can always
7502 blame the compiler :)
7504 \labelwidthstring 00.00.0000
7519 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7525 Will not include peep-hole comments in the generated files.
7527 \labelwidthstring 00.00.0000
7542 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7548 Include i-codes in the asm file.
7549 Sounds like noise but is most helpful for debugging the compiler itself.
7551 \labelwidthstring 00.00.0000
7566 \begin_inset LatexCommand \index{-\/-less-pedantic}
7572 Disable some of the more pedantic warnings
7573 \begin_inset LatexCommand \index{Warnings}
7577 (jwk burps: please be more specific here, please!).
7578 If you want rather more than less warnings you should consider using a
7579 separate tool dedicated to syntax checking like splint
7580 \begin_inset LatexCommand \url{http://www.splint.org}
7586 \labelwidthstring 00.00.0000
7600 -disable-warning\SpecialChar ~
7602 \begin_inset LatexCommand \index{-\/-disable-warning}
7608 Disable specific warning with number <nnnn>.
7610 \labelwidthstring 00.00.0000
7625 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7631 Display the directories in the compiler's search path
7633 \labelwidthstring 00.00.0000
7648 \begin_inset LatexCommand \index{-\/-vc}
7654 Display errors and warnings using MSVC style, so you can use SDCC with
7657 \labelwidthstring 00.00.0000
7672 \begin_inset LatexCommand \index{-\/-use-stdout}
7678 Send errors and warnings to stdout instead of stderr.
7680 \labelwidthstring 00.00.0000
7685 asmOption[,asmOption]
7688 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7693 Pass the asmOption to the assembler
7694 \begin_inset LatexCommand \index{Options assembler}
7699 \begin_inset LatexCommand \index{Assembler options}
7704 See file sdcc/as/doc/asxhtm.html for assembler options.
7706 \labelwidthstring 00.00.0000
7721 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7727 Generally follow the C89 standard, but allow SDCC features that conflict
7728 with the standard (default).
7730 \labelwidthstring 00.00.0000
7745 \begin_inset LatexCommand \index{-\/-std-c89}
7751 Follow the C89 standard and disable SDCC features that conflict with the
7754 \labelwidthstring 00.00.0000
7769 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7775 Generally follow the C99 standard, but allow SDCC features that conflict
7776 with the standard (incomplete support).
7778 \labelwidthstring 00.00.0000
7793 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7799 Follow the C99 standard and disable SDCC features that conflict with the
7800 standard (incomplete support).
7803 Intermediate Dump Options
7804 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7809 \begin_inset LatexCommand \index{Options intermediate dump}
7814 \begin_inset LatexCommand \index{Intermediate dump options}
7821 The following options are provided for the purpose of retargetting and debugging
7823 They provide a means to dump the intermediate code (iCode
7824 \begin_inset LatexCommand \index{iCode}
7828 ) generated by the compiler in human readable form at various stages of
7829 the compilation process.
7830 More on iCodes see chapter
7831 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7836 \begin_inset Quotes srd
7839 The anatomy of the compiler
7840 \begin_inset Quotes srd
7845 \labelwidthstring 00.00.0000
7860 \begin_inset LatexCommand \index{-\/-dumpraw}
7866 This option will cause the compiler to dump the intermediate code into
7869 <source filename>.dumpraw
7871 just after the intermediate code has been generated for a function, i.e.
7872 before any optimizations are done.
7874 \begin_inset LatexCommand \index{Basic blocks}
7878 at this stage ordered in the depth first number, so they may not be in
7879 sequence of execution.
7881 \labelwidthstring 00.00.0000
7896 \begin_inset LatexCommand \index{-\/-dumpgcse}
7902 Will create a dump of iCode's, after global subexpression elimination
7903 \begin_inset LatexCommand \index{Global subexpression elimination}
7909 <source filename>.dumpgcse.
7911 \labelwidthstring 00.00.0000
7926 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7932 Will create a dump of iCode's, after deadcode elimination
7933 \begin_inset LatexCommand \index{Dead-code elimination}
7939 <source filename>.dumpdeadcode.
7941 \labelwidthstring 00.00.0000
7956 \begin_inset LatexCommand \index{-\/-dumploop}
7965 Will create a dump of iCode's, after loop optimizations
7966 \begin_inset LatexCommand \index{Loop optimization}
7972 <source filename>.dumploop.
7974 \labelwidthstring 00.00.0000
7989 \begin_inset LatexCommand \index{-\/-dumprange}
7998 Will create a dump of iCode's, after live range analysis
7999 \begin_inset LatexCommand \index{Live range analysis}
8005 <source filename>.dumprange.
8007 \labelwidthstring 00.00.0000
8022 \begin_inset LatexCommand \index{-\/-dumlrange}
8028 Will dump the life ranges
8029 \begin_inset LatexCommand \index{Live range analysis}
8035 \labelwidthstring 00.00.0000
8050 \begin_inset LatexCommand \index{-\/-dumpregassign}
8059 Will create a dump of iCode's, after register assignment
8060 \begin_inset LatexCommand \index{Register assignment}
8066 <source filename>.dumprassgn.
8068 \labelwidthstring 00.00.0000
8083 \begin_inset LatexCommand \index{-\/-dumplrange}
8089 Will create a dump of the live ranges of iTemp's
8091 \labelwidthstring 00.00.0000
8106 \begin_inset LatexCommand \index{-\/-dumpall}
8117 Will cause all the above mentioned dumps to be created.
8120 Redirecting output on Windows Shells
8123 By default SDCC writes it's error messages to
8124 \begin_inset Quotes sld
8128 \begin_inset Quotes srd
8132 To force all messages to
8133 \begin_inset Quotes sld
8137 \begin_inset Quotes srd
8161 \begin_inset LatexCommand \index{-\/-use-stdout}
8166 Additionally, if you happen to have visual studio installed in your windows
8167 machine, you can use it to compile your sources using a custom build and
8183 \begin_inset LatexCommand \index{-\/-vc}
8188 Something like this should work:
8232 -model-large -c $(InputPath)
8235 Environment variables
8236 \begin_inset LatexCommand \index{Environment variables}
8243 SDCC recognizes the following environment variables:
8245 \labelwidthstring 00.00.0000
8250 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8256 SDCC installs a signal handler
8257 \begin_inset LatexCommand \index{signal handler}
8261 to be able to delete temporary files after an user break (^C) or an exception.
8262 If this environment variable is set, SDCC won't install the signal handler
8263 in order to be able to debug SDCC.
8265 \labelwidthstring 00.00.0000
8272 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8278 Path, where temporary files will be created.
8279 The order of the variables is the search order.
8280 In a standard *nix environment these variables are not set, and there's
8281 no need to set them.
8282 On Windows it's recommended to set one of them.
8284 \labelwidthstring 00.00.0000
8289 \begin_inset LatexCommand \index{SDCC\_HOME}
8296 \begin_inset LatexCommand \ref{sub:Install-paths}
8302 \begin_inset Quotes sld
8306 \begin_inset Quotes srd
8311 \labelwidthstring 00.00.0000
8316 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8323 \begin_inset LatexCommand \ref{sub:Search-Paths}
8329 \begin_inset Quotes sld
8333 \begin_inset Quotes srd
8338 \labelwidthstring 00.00.0000
8343 \begin_inset LatexCommand \index{SDCC\_LIB}
8350 \begin_inset LatexCommand \ref{sub:Search-Paths}
8356 \begin_inset Quotes sld
8360 \begin_inset Quotes srd
8366 There are some more environment variables recognized by SDCC, but these
8367 are solely used for debugging purposes.
8368 They can change or disappear very quickly, and will never be documented.
8371 Storage Class Language Extensions
8374 MCS51/DS390 Storage Class
8375 \begin_inset LatexCommand \index{Storage class}
8382 In addition to the ANSI storage classes SDCC allows the following MCS51
8383 specific storage classes:
8384 \layout Subsubsection
8387 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8392 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8397 \begin_inset LatexCommand \index{near (storage class)}
8402 \begin_inset LatexCommand \index{\_\_near (storage class)}
8413 storage class for the Small Memory model (
8421 can be used synonymously).
8422 Variables declared with this storage class will be allocated in the directly
8423 addressable portion of the internal RAM of a 8051, e.g.:
8428 data unsigned char test_data;
8431 Writing 0x01 to this variable generates the assembly code:
8436 75*00 01\SpecialChar ~
8442 \layout Subsubsection
8445 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8450 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8455 \begin_inset LatexCommand \index{far (storage class)}
8460 \begin_inset LatexCommand \index{\_\_far (storage class)}
8467 Variables declared with this storage class will be placed in the external
8473 storage class for the Large Memory model, e.g.:
8478 xdata unsigned char test_xdata;
8481 Writing 0x01 to this variable generates the assembly code:
8486 90s00r00\SpecialChar ~
8515 \layout Subsubsection
8518 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8523 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8530 Variables declared with this storage class will be allocated into the indirectly
8531 addressable portion of the internal ram of a 8051, e.g.:
8536 idata unsigned char test_idata;
8539 Writing 0x01 to this variable generates the assembly code:
8568 Please note, the first 128 byte of idata physically access the same RAM
8570 The original 8051 had 128 byte idata memory, nowadays most devices have
8571 256 byte idata memory.
8573 \begin_inset LatexCommand \index{stack}
8577 is located in idata memory.
8578 \layout Subsubsection
8581 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8586 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8593 Paged xdata access is just as straightforward as using the other addressing
8595 It is typically located at the start of xdata and has a maximum size of
8597 The following example writes 0x01 to the address pointed to.
8598 Please note, pdata access physically accesses xdata memory.
8599 The high byte of the address is determined by port P2
8600 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8604 (or in case of some 8051 variants by a separate Special Function Register,
8606 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8615 pdata unsigned char *test_pdata_ptr;
8627 test_pdata_ptr = (pdata *)0xfe;
8633 *test_pdata_ptr = 1;
8638 Generates the assembly code:
8643 75*01 FE\SpecialChar ~
8647 _test_pdata_ptr,#0xFE
8690 \begin_inset LatexCommand \index{-\/-xstack}
8694 option is used the pdata memory area is followed by the xstack memory area
8695 and the sum of their sizes is limited to 256 bytes.
8696 \layout Subsubsection
8699 \begin_inset LatexCommand \index{code}
8704 \begin_inset LatexCommand \index{\_\_code}
8711 'Variables' declared with this storage class will be placed in the code
8717 code unsigned char test_code;
8720 Read access to this variable generates the assembly code:
8725 90s00r6F\SpecialChar ~
8728 mov dptr,#_test_code
8757 indexed arrays of characters in code memory can be accessed efficiently:
8762 code char test_array[] = {'c','h','e','a','p'};
8765 Read access to this array using an 8-bit unsigned index generates the assembly
8782 90s00r41\SpecialChar ~
8785 mov dptr,#_test_array
8800 \layout Subsubsection
8803 \begin_inset LatexCommand \index{bit}
8808 \begin_inset LatexCommand \index{\_\_bit}
8815 This is a data-type and a storage class specifier.
8816 When a variable is declared as a bit, it is allocated into the bit addressable
8817 memory of 8051, e.g.:
8825 Writing 1 to this variable generates the assembly code:
8841 The bit addressable memory consists of 128 bits which are located from 0x20
8842 to 0x2f in data memory.
8845 Apart from this 8051 specific storage class most architectures support ANSI-C
8847 \begin_inset LatexCommand \index{bitfields}
8857 Not really meant as examples, but nevertheless showing what bitfields are
8858 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8862 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8863 signed modifier are implemented as unsigned.
8864 \layout Subsubsection
8867 \begin_inset LatexCommand \index{sfr}
8872 \begin_inset LatexCommand \index{\_\_sfr}
8877 \begin_inset LatexCommand \index{\_\_sbit}
8884 Like the bit keyword,
8888 signifies both a data-type and storage class, they are used to describe
8909 variables of a 8051, eg:
8915 \begin_inset LatexCommand \index{at}
8920 \begin_inset LatexCommand \index{\_\_at}
8924 0x80 P0;\SpecialChar ~
8925 /* special function register P0 at location 0x80 */
8927 sbit at 0xd7 CY; /* CY (Carry Flag
8928 \begin_inset LatexCommand \index{Flags}
8933 \begin_inset LatexCommand \index{Carry flag}
8940 Special function registers which are located on an address dividable by
8941 8 are bit-addressable, an
8945 addresses a specific bit within these sfr.
8946 \layout Subsubsection
8949 \begin_inset LatexCommand \index{Pointer}
8953 to MCS51/DS390 specific memory spaces
8956 SDCC allows (via language extensions) pointers to explicitly point to any
8957 of the memory spaces
8958 \begin_inset LatexCommand \index{Memory model}
8963 In addition to the explicit pointers, the compiler uses (by default) generic
8964 pointers which can be used to point to any of the memory spaces.
8968 Pointer declaration examples:
8973 /* pointer physically in internal ram pointing to object in external ram
8976 xdata unsigned char * data p;
8980 /* pointer physically in external ram pointing to object in internal ram
8983 data unsigned char * xdata p;
8987 /* pointer physically in code rom pointing to data in xdata space */
8989 xdata unsigned char * code p;
8993 /* pointer physically in code space pointing to data in code space */
8995 code unsigned char * code p;
8999 /* the following is a generic pointer physically located in xdata space
9005 Well you get the idea.
9010 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9023 The highest order byte of the
9027 pointers contains the data space information.
9028 Assembler support routines are called whenever data is stored or retrieved
9034 These are useful for developing reusable library
9035 \begin_inset LatexCommand \index{Libraries}
9040 Explicitly specifying the pointer type will generate the most efficient
9042 \layout Subsubsection
9044 Notes on MCS51 memory
9045 \begin_inset LatexCommand \index{MCS51 memory}
9052 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9053 RAM memory which is structured as follows:
9057 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9060 - Bytes 20-2F - 16 bytes to hold 128 bit
9061 \begin_inset LatexCommand \index{bit}
9067 - Bytes 30-7F - 80 bytes for general purpose use.
9072 Additionally some members of the MCS51 family may have up to 128 bytes of
9073 additional, indirectly addressable, internal RAM memory (
9078 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9083 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9088 Furthermore, some chips may have some built in external memory (
9093 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9098 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9102 ) which should not be confused with the internal, directly addressable RAM
9108 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9113 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9118 Sometimes this built in
9122 memory has to be activated before using it (you can probably find this
9123 information on the datasheet of the microcontroller your are using, see
9125 \begin_inset LatexCommand \ref{sub:Startup-Code}
9133 Normally SDCC will only use the first bank
9134 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9138 of registers (register bank 0), but it is possible to specify that other
9139 banks of registers (keyword
9146 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9151 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9157 ) should be used in interrupt
9158 \begin_inset LatexCommand \index{interrupt}
9163 \begin_inset LatexCommand \index{\_\_interrupt}
9168 By default, the compiler will place the stack after the last byte of allocated
9169 memory for variables.
9170 For example, if the first 2 banks of registers are used, and only four
9175 variables, it will position the base of the internal stack at address 20
9177 This implies that as the stack
9178 \begin_inset LatexCommand \index{stack}
9182 grows, it will use up the remaining register banks, and the 16 bytes used
9183 by the 128 bit variables, and 80 bytes for general purpose use.
9184 If any bit variables are used, the data variables will be placed in unused
9185 register banks and after the byte holding the last bit variable.
9186 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9191 variables will be placed starting from address 0x10 to 0x20 and continue
9204 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9208 to specify the start address of the
9223 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9227 to specify the size of the total internal RAM (
9239 By default the 8051 linker will place the stack after the last byte of (i)data
9252 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9256 allows you to specify the start of the stack, i.e.
9257 you could start it after any data in the general purpose area.
9258 If your microcontroller has additional indirectly addressable internal
9263 ) you can place the stack on it.
9264 You may also need to use -
9275 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9279 to set the start address of the external RAM (
9294 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9298 to specify its size.
9299 Same goes for the code memory, using -
9310 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9325 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9330 If in doubt, don't specify any options and see if the resulting memory
9331 layout is appropriate, then you can adjust it.
9334 The linker generates two files with memory allocation information.
9335 The first, with extension .map
9336 \begin_inset LatexCommand \index{<file>.map}
9340 shows all the variables and segments.
9341 The second with extension .mem
9342 \begin_inset LatexCommand \index{<file>.mem}
9346 shows the final memory layout.
9347 The linker will complain either if memory segments overlap, there is not
9348 enough memory, or there is not enough space for stack.
9349 If you get any linking warnings and/or errors related to stack or segments
9350 allocation, take a look at either the .map or .mem files to find out what
9352 The .mem file may even suggest a solution to the problem.
9355 Z80/Z180 Storage Class
9356 \begin_inset LatexCommand \index{Storage class}
9361 \layout Subsubsection
9364 \begin_inset LatexCommand \index{sfr}
9369 \begin_inset LatexCommand \index{\_\_sfr}
9373 (in/out to 8-bit addresses)
9377 \begin_inset LatexCommand \index{Z80}
9381 family has separate address spaces for memory and
9391 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9395 is accessed with special instructions, e.g.:
9400 sfr at 0x78 IoPort;\SpecialChar ~
9402 /* define a var in I/O space at 78h called IoPort */
9406 Writing 0x01 to this variable generates the assembly code:
9426 \layout Subsubsection
9429 \begin_inset LatexCommand \index{sfr}
9434 \begin_inset LatexCommand \index{\_\_sfr}
9438 (in/out to 16-bit addresses)
9445 is used to support 16 bit addresses in I/O memory e.g.:
9451 \begin_inset LatexCommand \index{at}
9456 \begin_inset LatexCommand \index{\_\_at}
9463 Writing 0x01 to this variable generates the assembly code:
9468 01 23 01\SpecialChar ~
9488 \layout Subsubsection
9491 \begin_inset LatexCommand \index{sfr}
9496 \begin_inset LatexCommand \index{\_\_sfr}
9500 (in0/out0 to 8 bit addresses on Z180
9501 \begin_inset LatexCommand \index{Z180}
9506 \begin_inset LatexCommand \index{HD64180}
9513 The compiler option -
9523 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9525 \begin_inset LatexCommand \index{\#pragma portmode}
9529 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9539 If you include the file z180.h this will be set automatically.
9543 \begin_inset LatexCommand \index{Storage class}
9548 \layout Subsubsection
9551 \begin_inset LatexCommand \index{data (hc08 storage class)}
9556 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9563 The data storage class declares a variable that resides in the first 256
9564 bytes of memory (the direct page).
9565 The HC08 is most efficient at accessing variables (especially pointers)
9567 \layout Subsubsection
9570 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9575 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9582 The xdata storage class declares a variable that can reside anywhere in
9584 This is the default if no storage class is specified.
9589 \begin_inset LatexCommand \index{Absolute addressing}
9596 Data items can be assigned an absolute address with the
9599 \begin_inset LatexCommand \index{at}
9604 \begin_inset LatexCommand \index{\_\_at}
9610 keyword, in addition to a storage class, e.g.:
9616 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9621 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9626 \begin_inset LatexCommand \index{at}
9631 \begin_inset LatexCommand \index{\_\_at}
9635 0x7ffe unsigned int chksum;
9638 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9639 of the external ram.
9644 reserve any space for variables declared in this way (they are implemented
9645 with an equate in the assembler).
9646 Thus it is left to the programmer to make sure there are no overlaps with
9647 other variables that are declared without the absolute address.
9648 The assembler listing file (.lst
9649 \begin_inset LatexCommand \index{<file>.lst}
9653 ) and the linker output files (.rst
9654 \begin_inset LatexCommand \index{<file>.rst}
9659 \begin_inset LatexCommand \index{<file>.map}
9663 ) are good places to look for such overlaps.
9664 Variables with an absolute address are
9669 \begin_inset LatexCommand \index{Variable initialization}
9676 In case of memory mapped I/O devices the keyword
9680 has to be used to tell the compiler that accesses might not be removed:
9686 \begin_inset LatexCommand \index{volatile}
9691 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9696 \begin_inset LatexCommand \index{at}
9700 0x8000 unsigned char PORTA_8255;
9703 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9708 \begin_inset LatexCommand \index{Aligned array}
9715 starts at a block (256 byte) boundary
9716 \begin_inset LatexCommand \index{block boundary}
9721 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9727 Absolute addresses can be specified for variables in all storage classes,
9734 \begin_inset LatexCommand \index{bit}
9739 \begin_inset LatexCommand \index{at}
9746 The above example will allocate the variable at offset 0x02 in the bit-addressab
9748 There is no real advantage to assigning absolute addresses to variables
9749 in this manner, unless you want strict control over all the variables allocated.
9750 One possible use would be to write hardware portable code.
9751 For example, if you have a routine that uses one or more of the microcontroller
9752 I/O pins, and such pins are different for two different hardwares, you
9753 can declare the I/O pins in your routine using:
9759 \begin_inset LatexCommand \index{volatile}
9763 bit MOSI;\SpecialChar ~
9767 /* master out, slave in */
9769 extern volatile bit MISO;\SpecialChar ~
9773 /* master in, slave out */
9775 extern volatile bit MCLK;\SpecialChar ~
9783 /* Input and Output of a byte on a 3-wire serial bus.
9788 If needed adapt polarity of clock, polarity of data and bit order
9793 unsigned char spi_io(unsigned char out_byte)
9817 MOSI = out_byte & 0x80;
9847 /* _asm nop _endasm; */\SpecialChar ~
9855 /* for slow peripherals */
9906 Then, someplace in the code for the first hardware you would use
9912 \begin_inset LatexCommand \index{at}
9917 \begin_inset LatexCommand \index{\_\_at}
9921 0x80 MOSI;\SpecialChar ~
9925 /* I/O port 0, bit 0 */
9927 bit at 0x81 MISO;\SpecialChar ~
9931 /* I/O port 0, bit 1 */
9933 bit at 0x82 MCLK;\SpecialChar ~
9937 /* I/O port 0, bit 2 */
9940 Similarly, for the second hardware you would use
9945 bit at 0x83 MOSI;\SpecialChar ~
9949 /* I/O port 0, bit 3 */
9951 bit at 0x91 MISO;\SpecialChar ~
9955 /* I/O port 1, bit 1 */
9958 \begin_inset LatexCommand \index{bit}
9962 at 0x92 MCLK;\SpecialChar ~
9966 /* I/O port 1, bit 2 */
9969 and you can use the same hardware dependent routine without changes, as
9970 for example in a library.
9971 This is somehow similar to sbit, but only one absolute address has to be
9972 specified in the whole project.
9976 \begin_inset LatexCommand \index{Parameters}
9981 \begin_inset LatexCommand \index{function parameter}
9986 \begin_inset LatexCommand \index{local variables}
9991 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9998 Automatic (local) variables and parameters to functions can either be placed
9999 on the stack or in data-space.
10000 The default action of the compiler is to place these variables in the internal
10001 RAM (for small model) or external RAM (for large model).
10002 This in fact makes them similar to
10005 \begin_inset LatexCommand \index{static}
10011 so by default functions are non-reentrant
10012 \begin_inset LatexCommand \index{reentrant}
10021 They can be placed on the stack
10022 \begin_inset LatexCommand \index{stack}
10039 \begin_inset LatexCommand \index{-\/-stack-auto}
10047 #pragma\SpecialChar ~
10051 \begin_inset LatexCommand \index{\#pragma stackauto}
10058 \begin_inset LatexCommand \index{reentrant}
10064 keyword in the function declaration, e.g.:
10069 unsigned char foo(char i) reentrant
10083 Since stack space on 8051 is limited, the
10101 option should be used sparingly.
10102 Note that the reentrant keyword just means that the parameters & local
10103 variables will be allocated to the stack, it
10107 mean that the function is register bank
10108 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10117 \begin_inset LatexCommand \index{local variables}
10121 can be assigned storage classes and absolute
10122 \begin_inset LatexCommand \index{Absolute addressing}
10131 unsigned char foo()
10139 xdata unsigned char i;
10152 \begin_inset LatexCommand \index{at}
10156 0x31 unsigned char j;
10168 In the above example the variable
10172 will be allocated in the external ram,
10176 in bit addressable space and
10195 or when a function is declared as
10199 this should only be done for static variables.
10203 \begin_inset LatexCommand \index{function parameter}
10207 however are not allowed any storage class
10208 \begin_inset LatexCommand \index{Storage class}
10212 , (storage classes for parameters will be ignored), their allocation is
10213 governed by the memory model in use, and the reentrancy options.
10217 \begin_inset LatexCommand \label{sub:Overlaying}
10222 \begin_inset LatexCommand \index{Overlaying}
10230 \begin_inset LatexCommand \index{reentrant}
10234 functions SDCC will try to reduce internal ram space usage by overlaying
10235 parameters and local variables of a function (if possible).
10236 Parameters and local variables
10237 \begin_inset LatexCommand \index{local variables}
10241 of a function will be allocated to an overlayable segment if the function
10244 no other function calls and the function is non-reentrant and the memory
10246 \begin_inset LatexCommand \index{Memory model}
10253 If an explicit storage class
10254 \begin_inset LatexCommand \index{Storage class}
10258 is specified for a local variable, it will NOT be overlayed.
10261 Note that the compiler (not the linkage editor) makes the decision for overlayin
10263 Functions that are called from an interrupt service routine should be preceded
10264 by a #pragma\SpecialChar ~
10266 \begin_inset LatexCommand \index{\#pragma nooverlay}
10270 if they are not reentrant.
10273 Also note that the compiler does not do any processing of inline assembler
10274 code, so the compiler might incorrectly assign local variables and parameters
10275 of a function into the overlay segment if the inline assembler code calls
10276 other c-functions that might use the overlay.
10277 In that case the #pragma\SpecialChar ~
10278 nooverlay should be used.
10281 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10283 \begin_inset LatexCommand \index{Multiplication}
10288 \begin_inset LatexCommand \index{Division}
10292 will NOT be overlayed since these are implemented using external functions,
10301 \begin_inset LatexCommand \index{\#pragma nooverlay}
10307 void set_error(unsigned char errcd)
10323 void some_isr () interrupt
10324 \begin_inset LatexCommand \index{interrupt}
10354 In the above example the parameter
10362 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10364 not present, this could cause unpredictable runtime behavior when called
10365 from an interrupt service routine.
10366 The #pragma\SpecialChar ~
10367 nooverlay ensures that the parameters and local variables for
10368 the function are NOT overlayed.
10371 Interrupt Service Routines
10372 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10379 General Information
10394 outines to be coded in C, with some extended keywords.
10399 void timer_isr (void) interrupt 1 using 1
10413 The optional number following the
10416 \begin_inset LatexCommand \index{interrupt}
10421 \begin_inset LatexCommand \index{\_\_interrupt}
10427 keyword is the interrupt number this routine will service.
10428 When present, the compiler will insert a call to this routine in the interrupt
10429 vector table for the interrupt number specified.
10430 If you have multiple source files in your project, interrupt service routines
10431 can be present in any of them, but a prototype of the isr MUST be present
10432 or included in the file that contains the function
10440 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10445 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10451 keyword can be used to tell the compiler to use the specified register
10452 bank (8051 specific) when generating code for this function.
10458 Interrupt service routines open the door for some very interesting bugs:
10460 If an interrupt service routine changes variables which are accessed by
10461 other functions these variables have to be declared
10466 \begin_inset LatexCommand \index{volatile}
10474 If the access to these variables is not
10477 \begin_inset LatexCommand \index{atomic}
10484 the processor needs more than one instruction for the access and could
10485 be interrupted while accessing the variable) the interrupt must be disabled
10486 during the access to avoid inconsistent data.
10487 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10488 and should be protected by disabling interrupts.
10489 You're not automatically on the safe side if you use 8 bit variables though.
10490 We need an example here: f.e.
10491 on the 8051 the harmless looking
10492 \begin_inset Quotes srd
10497 flags\SpecialChar ~
10502 \begin_inset Quotes sld
10511 \begin_inset Quotes srd
10516 flags\SpecialChar ~
10521 \begin_inset Quotes sld
10524 from within an interrupt routine might get lost if the interrupt occurs
10527 \begin_inset Quotes sld
10532 counter\SpecialChar ~
10537 \begin_inset Quotes srd
10540 is not atomic on the 8051 even if
10544 is located in data memory.
10545 Bugs like these are hard to reproduce and can cause a lot of trouble.
10549 The return address and the registers used in the interrupt service routine
10550 are saved on the stack
10551 \begin_inset LatexCommand \index{stack}
10555 so there must be sufficient stack space.
10556 If there isn't variables or registers (or even the return address itself)
10563 \begin_inset LatexCommand \index{stack overflow}
10567 is most likely to happen if the interrupt occurs during the
10568 \begin_inset Quotes sld
10572 \begin_inset Quotes srd
10575 subroutine when the stack is already in use for f.e.
10576 many return addresses.
10579 A special note here, int (16 bit) and long (32 bit) integer division
10580 \begin_inset LatexCommand \index{Division}
10585 \begin_inset LatexCommand \index{Multiplication}
10590 \begin_inset LatexCommand \index{Modulus}
10595 \begin_inset LatexCommand \index{Floating point support}
10599 operations are implemented using external support routines developed in
10601 If an interrupt service routine needs to do any of these operations then
10602 the support routines (as mentioned in a following section) will have to
10603 be recompiled using the
10616 \begin_inset LatexCommand \index{-\/-stack-auto}
10622 option and the source file will need to be compiled using the
10637 \begin_inset LatexCommand \index{-\/-int-long-reent}
10644 Calling other functions from an interrupt service routine is not recommended,
10645 avoid it if possible.
10646 Note that when some function is called from an interrupt service routine
10647 it should be preceded by a #pragma\SpecialChar ~
10649 \begin_inset LatexCommand \index{\#pragma nooverlay}
10653 if it is not reentrant.
10654 Furthermore nonreentrant functions should not be called from the main program
10655 while the interrupt service routine might be active.
10661 \begin_inset LatexCommand \ref{sub:Overlaying}
10666 about Overlaying and section
10667 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10672 about Functions using private register banks.
10675 MCS51/DS390 Interrupt Service Routines
10678 Interrupt numbers and the corresponding address & descriptions for the Standard
10679 8051/8052 are listed below.
10680 SDCC will automatically adjust the interrupt vector table to the maximum
10681 interrupt number specified.
10687 \begin_inset Tabular
10688 <lyxtabular version="3" rows="7" columns="3">
10690 <column alignment="center" valignment="top" leftline="true" width="0in">
10691 <column alignment="center" valignment="top" leftline="true" width="0in">
10692 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10693 <row topline="true" bottomline="true">
10694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10710 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10719 <row topline="true">
10720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10728 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10736 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10745 <row topline="true">
10746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10762 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10771 <row topline="true">
10772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10788 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10797 <row topline="true">
10798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10814 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10823 <row topline="true">
10824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10832 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10849 <row topline="true" bottomline="true">
10850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10884 If the interrupt service routine is defined without
10887 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10892 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10898 a register bank or with register bank 0 (
10902 0), the compiler will save the registers used by itself on the stack upon
10903 entry and restore them at exit, however if such an interrupt service routine
10904 calls another function then the entire register bank will be saved on the
10906 This scheme may be advantageous for small interrupt service routines which
10907 have low register usage.
10910 If the interrupt service routine is defined to be using a specific register
10915 & psw are saved and restored, if such an interrupt service routine calls
10916 another function (using another register bank) then the entire register
10917 bank of the called function will be saved on the stack.
10918 This scheme is recommended for larger interrupt service routines.
10921 HC08 Interrupt Service Routines
10924 Since the number of interrupts available is chip specific and the interrupt
10925 vector table always ends at the last byte of memory, the interrupt numbers
10926 corresponds to the interrupt vectors in reverse order of address.
10927 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10928 2 will use the interrupt vector at 0xfffa, and so on.
10929 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10930 this way; instead see section
10931 \begin_inset LatexCommand \ref{sub:Startup-Code}
10935 for details on customizing startup.
10938 Z80 Interrupt Service Routines
10941 The Z80 uses several different methods for determining the correct interrupt
10942 vector depending on the hardware implementation.
10943 Therefore, SDCC ignores the optional interrupt number and does not attempt
10944 to generate an interrupt vector table.
10947 By default, SDCC generates code for a maskable interrupt, which uses an
10948 RETI instruction to return from the interrupt.
10949 To write an interrupt handler for the non-maskable interrupt, which needs
10950 an RETN instruction instead, add the
10959 void nmi_isr (void) critical interrupt
10973 Enabling and Disabling Interrupts
10976 Critical Functions and Critical Statements
10979 A special keyword may be associated with a block or a function declaring
10985 SDCC will generate code to disable all interrupts
10986 \begin_inset LatexCommand \index{interrupt}
10990 upon entry to a critical function and restore the interrupt enable to the
10991 previous state before returning.
10992 Nesting critical functions will need one additional byte on the stack
10993 \begin_inset LatexCommand \index{stack}
11002 int foo () critical
11003 \begin_inset LatexCommand \index{critical}
11008 \begin_inset LatexCommand \index{\_\_critical}
11033 The critical attribute maybe used with other attributes like
11043 may also be used to disable interrupts more locally:
11051 More than one statement could have been included in the block.
11054 Enabling and Disabling Interrupts directly
11058 \begin_inset LatexCommand \index{interrupt}
11062 can also be disabled and enabled directly (8051):
11067 EA = 0;\SpecialChar ~
11130 EA = 1;\SpecialChar ~
11197 On other architectures which have seperate opcodes for enabling and disabling
11198 interrupts you might want to make use of defines with inline assembly
11199 \begin_inset LatexCommand \index{Assembler routines}
11209 \begin_inset LatexCommand \index{\_asm}
11218 \begin_inset LatexCommand \index{\_endasm}
11227 #define SEI _asm\SpecialChar ~
11239 Note: it is sometimes sufficient to disable only a specific interrupt source
11241 a timer or serial interrupt by manipulating an
11244 \begin_inset LatexCommand \index{interrupt mask}
11254 Usually the time during which interrupts are disabled should be kept as
11256 This minimizes both
11261 \begin_inset LatexCommand \index{interrupt latency}
11265 (the time between the occurrence of the interrupt and the execution of
11266 the first code in the interrupt routine) and
11271 \begin_inset LatexCommand \index{interrupt jitter}
11275 (the difference between the shortest and the longest interrupt latency).
11276 These really are something different, f.e.
11277 a serial interrupt has to be served before its buffer overruns so it cares
11278 for the maximum interrupt latency, whereas it does not care about jitter.
11279 On a loudspeaker driven via a digital to analog converter which is fed
11280 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11281 a much smaller jitter will be very audible.
11284 You can reenable interrupts within an interrupt routine and on some architecture
11285 s you can make use of two (or more) levels of
11287 interrupt priorities
11290 \begin_inset LatexCommand \index{interrupt priority}
11295 On some architectures which don't support interrupt priorities these can
11296 be implemented by manipulating the interrupt mask and reenabling interrupts
11297 within the interrupt routine.
11298 Check there is sufficient space on the stack
11299 \begin_inset LatexCommand \index{stack}
11303 and don't add complexity unless you have to.
11308 \begin_inset LatexCommand \index{semaphore}
11312 locking (mcs51/ds390)
11315 Some architectures (mcs51/ds390) have an atomic
11316 \begin_inset LatexCommand \index{atomic}
11329 These type of instructions are typically used in preemptive multitasking
11330 systems, where a routine f.e.
11331 claims the use of a data structure ('acquires a lock
11332 \begin_inset LatexCommand \index{lock}
11336 on it'), makes some modifications and then releases the lock when the data
11337 structure is consistent again.
11338 The instruction may also be used if interrupt and non-interrupt code have
11339 to compete for a resource.
11340 With the atomic bit test and clear instruction interrupts
11341 \begin_inset LatexCommand \index{interrupt}
11345 don't have to be disabled for the locking operation.
11349 SDCC generates this instruction if the source follows this pattern:
11355 \begin_inset LatexCommand \index{volatile}
11359 bit resource_is_free;
11363 if (resource_is_free)
11373 resource_is_free=0;
11386 resource_is_free=1;
11393 Note, mcs51 and ds390 support only an atomic
11394 \begin_inset LatexCommand \index{atomic}
11402 instruction (as opposed to atomic bit test and
11407 Functions using private register banks
11408 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11415 Some architectures have support for quickly changing register sets.
11416 SDCC supports this feature with the
11419 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11424 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11430 attribute (which tells the compiler to use a register bank
11431 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11435 other than the default bank zero).
11436 It should only be applied to
11439 \begin_inset LatexCommand \index{interrupt}
11445 functions (see footnote below).
11446 This will in most circumstances make the generated ISR code more efficient
11447 since it will not have to save registers on the stack.
11454 attribute will have no effect on the generated code for a
11458 function (but may occasionally be useful anyway
11464 possible exception: if a function is called ONLY from 'interrupt' functions
11465 using a particular bank, it can be declared with the same 'using' attribute
11466 as the calling 'interrupt' functions.
11467 For instance, if you have several ISRs using bank one, and all of them
11468 call memcpy(), it might make sense to create a specialized version of memcpy()
11469 'using 1', since this would prevent the ISR from having to save bank zero
11470 to the stack on entry and switch to bank zero before calling the function
11477 (pending: I don't think this has been done yet)
11484 function using a non-zero bank will assume that it can trash that register
11485 bank, and will not save it.
11486 Since high-priority interrupts
11487 \begin_inset LatexCommand \index{interrupts}
11492 \begin_inset LatexCommand \index{interrupt priority}
11496 can interrupt low-priority ones on the 8051 and friends, this means that
11497 if a high-priority ISR
11501 a particular bank occurs while processing a low-priority ISR
11505 the same bank, terrible and bad things can happen.
11506 To prevent this, no single register bank should be
11510 by both a high priority and a low priority ISR.
11511 This is probably most easily done by having all high priority ISRs use
11512 one bank and all low priority ISRs use another.
11513 If you have an ISR which can change priority at runtime, you're on your
11514 own: I suggest using the default bank zero and taking the small performance
11518 It is most efficient if your ISR calls no other functions.
11519 If your ISR must call other functions, it is most efficient if those functions
11520 use the same bank as the ISR (see note 1 below); the next best is if the
11521 called functions use bank zero.
11522 It is very inefficient to call a function using a different, non-zero bank
11528 \begin_inset LatexCommand \label{sub:Startup-Code}
11533 \begin_inset LatexCommand \index{Startup code}
11540 MCS51/DS390 Startup Code
11543 The compiler inserts a call to the C routine
11545 _sdcc_external_startup()
11546 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11555 at the start of the CODE area.
11556 This routine is in the runtime library
11557 \begin_inset LatexCommand \index{Runtime library}
11562 By default this routine returns 0, if this routine returns a non-zero value,
11563 the static & global variable initialization will be skipped and the function
11564 main will be invoked.
11565 Otherwise static & global variables will be initialized before the function
11569 _sdcc_external_startup()
11571 routine to your program to override the default if you need to setup hardware
11572 or perform some other critical operation prior to static & global variable
11574 \begin_inset LatexCommand \index{Variable initialization}
11579 On some mcs51 variants xdata
11580 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11584 memory has to be explicitly enabled before it can be accessed or if the
11585 watchdog needs to be disabled, this is the place to do it.
11586 The startup code clears all internal data memory, 256 bytes by default,
11587 but from 0 to n-1 if
11600 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11607 (recommended for Chipcon CC1010).
11610 See also the compiler option
11629 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11634 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11639 about MCS51-variants.
11645 The HC08 startup code follows the same scheme as the MCS51 startup code.
11651 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11652 from sdcc/device/lib/z80/crt0.s.
11653 If you need a different startup code you can use the compiler option
11674 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11678 and provide your own crt0.o.
11682 Inline Assembler Code
11683 \begin_inset LatexCommand \index{Assembler routines}
11690 A Step by Step Introduction
11691 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11698 Starting from a small snippet of c-code this example shows for the MCS51
11699 how to use inline assembly, access variables, a function parameter and
11700 an array in xdata memory.
11701 The example uses an MCS51 here but is easily adapted for other architectures.
11702 This is a buffer routine which should be optimized:
11709 \begin_inset LatexCommand \index{far (storage class)}
11714 \begin_inset LatexCommand \index{\_\_far (storage class)}
11719 \begin_inset LatexCommand \index{at}
11724 \begin_inset LatexCommand \index{\_\_at}
11729 \begin_inset LatexCommand \index{Aligned array}
11735 unsigned char head,tail;
11739 void to_buffer( unsigned char c )
11747 if( head != tail-1 )
11757 buf[ head++ ] = c;\SpecialChar ~
11761 /* access to a 256 byte aligned array */
11766 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11767 then a corresponding buffer.asm file is generated.
11768 We define a new function
11772 in file buffer.c in which we cut and paste the generated code, removing
11773 unwanted comments and some ':'.
11775 \begin_inset Quotes sld
11779 \begin_inset Quotes srd
11783 \begin_inset Quotes sld
11787 \begin_inset Quotes srd
11790 to the beginning and the end of the function body:
11796 /* With a cut and paste from the .asm file, we have something to start with.
11801 The function is not yet OK! (registers aren't saved) */
11803 void to_buffer_asm( unsigned char c )
11812 \begin_inset LatexCommand \index{\_asm}
11817 \begin_inset LatexCommand \index{\_\_asm}
11831 ;buffer.c if( head != tail-1 )
11879 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11880 \begin_inset LatexCommand \index{Aligned array}
11945 \begin_inset LatexCommand \index{\_endasm}
11950 \begin_inset LatexCommand \index{\_\_endasm}
11959 The new file buffer.c should compile with only one warning about the unreferenced
11960 function argument 'c'.
11961 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11962 (1) and finally have:
11968 unsigned char far at 0x7f00 buf[0x100];
11970 unsigned char head,tail;
11972 #define USE_ASSEMBLY (1)
11980 void to_buffer( unsigned char c )
11988 if( head != tail-1 )
12008 void to_buffer( unsigned char c )
12016 c; // to avoid warning: unreferenced function argument
12023 \begin_inset LatexCommand \index{\_asm}
12028 \begin_inset LatexCommand \index{\_\_asm}
12042 ; save used registers here.
12053 ; If we were still using r2,r3 we would have to push them here.
12056 ; if( head != tail-1 )
12099 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12123 ; buf[ head++ ] = c;
12134 a,dpl \SpecialChar ~
12141 ; dpl holds lower byte of function argument
12152 dpl,_head \SpecialChar ~
12155 ; buf is 0x100 byte aligned so head can be used directly
12197 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12209 ; restore used registers here
12216 \begin_inset LatexCommand \index{\_endasm}
12221 \begin_inset LatexCommand \index{\_\_endasm}
12232 The inline assembler code can contain any valid code understood by the assembler
12233 , this includes any assembler directives and comment lines
12239 The assembler does not like some characters like ':' or ''' in comments.
12240 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12241 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12246 \begin_inset LatexCommand \index{Assembler documentation}
12254 The compiler does not do any validation of the code within the
12257 \begin_inset LatexCommand \index{\_asm}
12262 \begin_inset LatexCommand \index{\_\_asm}
12270 \begin_inset LatexCommand \index{\_endasm}
12275 \begin_inset LatexCommand \index{\_\_endasm}
12284 Specifically it will not know which registers are used and thus register
12286 \begin_inset LatexCommand \index{push/pop}
12290 has to be done manually.
12294 It is recommended that each assembly instruction (including labels) be placed
12295 in a separate line (as the example shows).
12309 \begin_inset LatexCommand \index{-\/-peep-asm}
12315 command line option is used, the inline assembler code will be passed through
12316 the peephole optimizer
12317 \begin_inset LatexCommand \index{Peephole optimizer}
12322 There are only a few (if any) cases where this option makes sense, it might
12323 cause some unexpected changes in the inline assembler code.
12324 Please go through the peephole optimizer rules defined in file
12328 before using this option.
12332 \begin_inset LatexCommand \label{sub:Naked-Functions}
12337 \begin_inset LatexCommand \index{Naked functions}
12344 A special keyword may be associated with a function declaring it as
12347 \begin_inset LatexCommand \index{\_naked}
12352 \begin_inset LatexCommand \index{\_\_naked}
12363 function modifier attribute prevents the compiler from generating prologue
12364 \begin_inset LatexCommand \index{function prologue}
12369 \begin_inset LatexCommand \index{function epilogue}
12373 code for that function.
12374 This means that the user is entirely responsible for such things as saving
12375 any registers that may need to be preserved, selecting the proper register
12376 bank, generating the
12380 instruction at the end, etc.
12381 Practically, this means that the contents of the function must be written
12382 in inline assembler.
12383 This is particularly useful for interrupt functions, which can have a large
12384 (and often unnecessary) prologue/epilogue.
12385 For example, compare the code generated by these two functions:
12391 \begin_inset LatexCommand \index{volatile}
12395 data unsigned char counter;
12399 void simpleInterrupt(void) interrupt
12400 \begin_inset LatexCommand \index{interrupt}
12405 \begin_inset LatexCommand \index{\_\_interrupt}
12423 void nakedInterrupt(void) interrupt 2 _naked
12432 \begin_inset LatexCommand \index{\_asm}
12437 \begin_inset LatexCommand \index{\_\_asm}
12454 _counter ; does not change flags, no need to save psw
12466 ; MUST explicitly include ret or reti in _naked function.
12473 \begin_inset LatexCommand \index{\_endasm}
12478 \begin_inset LatexCommand \index{\_\_endasm}
12487 For an 8051 target, the generated simpleInterrupt looks like:
12628 whereas nakedInterrupt looks like:
12643 _counter ; does not change flags, no need to save psw
12661 ; MUST explicitly include ret or reti in _naked function
12664 The related directive #pragma exclude
12665 \begin_inset LatexCommand \index{\#pragma exclude}
12669 allows a more fine grained control over pushing & popping
12670 \begin_inset LatexCommand \index{push/pop}
12677 While there is nothing preventing you from writing C code inside a
12681 function, there are many ways to shoot yourself in the foot doing this,
12682 and it is recommended that you stick to inline assembler.
12685 Use of Labels within Inline Assembler
12688 SDCC allows the use of in-line assembler with a few restrictions regarding
12690 In older versions of the compiler all labels defined within inline assembler
12699 where nnnn is a number less than 100 (which implies a limit of utmost 100
12700 inline assembler labels
12714 \begin_inset LatexCommand \index{\_asm}
12719 \begin_inset LatexCommand \index{\_\_asm}
12749 \begin_inset LatexCommand \index{\_endasm}
12754 \begin_inset LatexCommand \index{\_\_endasm}
12761 Inline assembler code cannot reference any C-Labels, however it can reference
12763 \begin_inset LatexCommand \index{Labels}
12767 defined by the inline assembler, e.g.:
12792 ; some assembler code
12812 /* some more c code */
12814 clabel:\SpecialChar ~
12816 /* inline assembler cannot reference this label */
12828 $0003: ;label (can be referenced by inline assembler only)
12835 \begin_inset LatexCommand \index{\_endasm}
12840 \begin_inset LatexCommand \index{\_\_endasm}
12850 /* some more c code */
12855 In other words inline assembly code can access labels defined in inline
12856 assembly within the scope of the function.
12857 The same goes the other way, i.e.
12858 labels defines in inline assembly can not be accessed by C statements.
12861 Interfacing with Assembler Code
12862 \begin_inset LatexCommand \index{Assembler routines}
12869 Global Registers used for Parameter Passing
12870 \begin_inset LatexCommand \index{Parameter passing}
12877 The compiler always uses the global registers
12880 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12885 \begin_inset LatexCommand \index{DPTR}
12890 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12899 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12905 to pass the first parameter to a routine.
12906 The second parameter onwards is either allocated on the stack (for reentrant
12917 -stack-auto is used) or in data / xdata memory (depending on the memory
12922 Assembler Routine (non-reentrant)
12925 In the following example
12926 \begin_inset LatexCommand \index{reentrant}
12931 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12935 the function c_func calls an assembler routine asm_func, which takes two
12937 \begin_inset LatexCommand \index{function parameter}
12946 extern int asm_func(unsigned char, unsigned char);
12950 int c_func (unsigned char i, unsigned char j)
12958 return asm_func(i,j);
12972 return c_func(10,9);
12977 The corresponding assembler function is:
12982 .globl _asm_func_PARM_2
13083 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13100 Note here that the return values
13101 \begin_inset LatexCommand \index{return value}
13105 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13107 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13108 b' & 'acc' for four byte values.
13111 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13112 the parameter number starting from 1, and counting from the left.
13113 The first parameter is passed in
13114 \begin_inset Quotes eld
13118 \begin_inset Quotes erd
13121 for a one byte parameter,
13122 \begin_inset Quotes eld
13126 \begin_inset Quotes erd
13130 \begin_inset Quotes eld
13134 \begin_inset Quotes erd
13137 for three bytes and
13138 \begin_inset Quotes eld
13142 \begin_inset Quotes erd
13145 for a four bytes parameter.
13146 The variable name for the second parameter will be _<function_name>_PARM_2.
13150 Assemble the assembler routine with the following command:
13157 asx8051 -losg asmfunc.asm
13164 Then compile and link the assembler routine to the C source file with the
13172 sdcc cfunc.c asmfunc.rel
13175 Assembler Routine (reentrant)
13179 \begin_inset LatexCommand \index{reentrant}
13184 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13188 the second parameter
13189 \begin_inset LatexCommand \index{function parameter}
13193 onwards will be passed on the stack, the parameters are pushed from right
13195 after the call the leftmost parameter will be on the top of the stack.
13196 Here is an example:
13201 extern int asm_func(unsigned char, unsigned char);
13205 int c_func (unsigned char i, unsigned char j) reentrant
13213 return asm_func(i,j);
13227 return c_func(10,9);
13232 The corresponding assembler routine is:
13332 The compiling and linking procedure remains the same, however note the extra
13333 entry & exit linkage required for the assembler code, _bp is the stack
13334 frame pointer and is used to compute the offset into the stack for parameters
13335 and local variables.
13339 \begin_inset LatexCommand \index{int (16 bit)}
13344 \begin_inset LatexCommand \index{long (32 bit)}
13351 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13352 multiplication and modulus operations are implemented by support routines.
13353 These support routines are all developed in ANSI-C to facilitate porting
13354 to other MCUs, although some model specific assembler optimizations are
13356 The following files contain the described routines, all of them can be
13357 found in <installdir>/share/sdcc/lib.
13363 \begin_inset Tabular
13364 <lyxtabular version="3" rows="11" columns="2">
13366 <column alignment="center" valignment="top" leftline="true" width="0">
13367 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13368 <row topline="true" bottomline="true">
13369 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13379 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13390 <row topline="true">
13391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13404 16 bit multiplication
13408 <row topline="true">
13409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13422 signed 16 bit division (calls _divuint)
13426 <row topline="true">
13427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13435 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13440 unsigned 16 bit division
13444 <row topline="true">
13445 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13453 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13458 signed 16 bit modulus (calls _moduint)
13462 <row topline="true">
13463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13471 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13476 unsigned 16 bit modulus
13480 <row topline="true">
13481 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13489 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13494 32 bit multiplication
13498 <row topline="true">
13499 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13507 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13512 signed 32 division (calls _divulong)
13516 <row topline="true">
13517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13525 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13530 unsigned 32 division
13534 <row topline="true">
13535 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13543 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13548 signed 32 bit modulus (calls _modulong)
13552 <row topline="true" bottomline="true">
13553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13566 unsigned 32 bit modulus
13579 Since they are compiled as
13584 \begin_inset LatexCommand \index{reentrant}
13589 \begin_inset LatexCommand \index{interrupt}
13593 service routines should not do any of the above operations.
13594 If this is unavoidable then the above routines will need to be compiled
13608 \begin_inset LatexCommand \index{-\/-stack-auto}
13614 option, after which the source program will have to be compiled with
13627 \begin_inset LatexCommand \index{-\/-int-long-reent}
13634 Notice that you don't have to call these routines directly.
13635 The compiler will use them automatically every time an integer operation
13639 Floating Point Support
13640 \begin_inset LatexCommand \index{Floating point support}
13647 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13648 floating point support routines are derived from gcc's floatlib.c and consist
13649 of the following routines:
13657 \begin_inset Tabular
13658 <lyxtabular version="3" rows="17" columns="2">
13660 <column alignment="center" valignment="top" leftline="true" width="0">
13661 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13662 <row topline="true" bottomline="true">
13663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13680 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13689 <row topline="true">
13690 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13721 add floating point numbers
13725 <row topline="true">
13726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13743 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13757 subtract floating point numbers
13761 <row topline="true">
13762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13779 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13793 divide floating point numbers
13797 <row topline="true">
13798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13829 multiply floating point numbers
13833 <row topline="true">
13834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13851 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13865 convert floating point to unsigned char
13869 <row topline="true">
13870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13887 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13901 convert floating point to signed char
13905 <row topline="true">
13906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13923 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13937 convert floating point to unsigned int
13941 <row topline="true">
13942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13959 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13973 convert floating point to signed int
13977 <row topline="true">
13978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14018 convert floating point to unsigned long
14022 <row topline="true">
14023 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14040 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14054 convert floating point to signed long
14058 <row topline="true">
14059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14090 convert unsigned char to floating point
14094 <row topline="true">
14095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14112 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14126 convert char to floating point number
14130 <row topline="true">
14131 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14162 convert unsigned int to floating point
14166 <row topline="true">
14167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14198 convert int to floating point numbers
14202 <row topline="true">
14203 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14220 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14234 convert unsigned long to floating point number
14238 <row topline="true" bottomline="true">
14239 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14270 convert long to floating point number
14283 These support routines are developed in ANSI-C so there is room for space
14284 and speed improvement
14290 The floating point routines for the mcs51 are implemented in assembler
14294 Note if all these routines are used simultaneously the data space might
14296 For serious floating point usage the large model might be needed.
14297 Also notice that you don't have to call this routines directly.
14298 The compiler will use them automatically every time a floating point operation
14303 \begin_inset LatexCommand \index{Libraries}
14312 <pending: this is messy and incomplete - a little more information is in
14313 sdcc/doc/libdoc.txt
14318 Compiler support routines (_gptrget, _mulint etc.)
14321 Stdclib functions (puts, printf, strcat etc.)
14322 \layout Subsubsection
14328 \begin_inset LatexCommand \index{<stdio.h>}
14332 As usual on embedded systems you have to provide your own
14335 \begin_inset LatexCommand \index{getchar()}
14344 \begin_inset LatexCommand \index{putchar()}
14351 SDCC does not know whether the system connects to a serial line with or
14352 without handshake, LCD, keyboard or other device.
14353 You'll find examples for serial routines f.e.
14354 in sdcc/device/lib.
14360 \begin_inset LatexCommand \index{printf()}
14370 does not support float (except on ds390).
14371 To enable this recompile it with the option
14384 \begin_inset LatexCommand \index{USE\_FLOATS}
14390 on the command line.
14404 \begin_inset LatexCommand \index{-\/-model-large}
14410 for the mcs51 port, since this uses a lot of memory.
14413 If you're short on memory you might want to use
14416 \begin_inset LatexCommand \index{printf\_small()}
14431 For the mcs51 there additionally are assembly versions
14434 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14443 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14452 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14458 which should fit the requirements of many embedded systems (printf_fast()
14459 can be customized by unsetting #defines to
14463 support long variables and field widths).
14466 Math functions (sin, pow, sqrt etc.)
14473 \begin_inset LatexCommand \index{Libraries}
14477 included in SDCC should have a license at least as liberal as the GNU Lesser
14478 General Public License
14479 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14490 license statements for the libraries are missing.
14491 sdcc/device/lib/ser_ir.c
14495 come with a GPL (as opposed to LGPL) License - this will not be liberal
14496 enough for many embedded programmers.
14499 If you have ported some library or want to share experience about some code
14501 falls into any of these categories Busses (I
14502 \begin_inset Formula $^{\textrm{2}}$
14505 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14506 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14507 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14508 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14513 would certainly like to hear about it.
14514 Programmers coding for embedded systems are not especially famous for being
14515 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14516 e these references are very valuable.
14517 Let's help to create a climate where information is shared.
14523 MCS51 Memory Models
14524 \begin_inset LatexCommand \index{Memory model}
14529 \begin_inset LatexCommand \index{MCS51 memory model}
14534 \layout Subsubsection
14539 SDCC allows two memory models for MCS51 code,
14548 Modules compiled with different memory models should
14552 be combined together or the results would be unpredictable.
14553 The library routines supplied with the compiler are compiled as both small
14555 The compiled library modules are contained in separate directories as small
14556 and large so that you can link to either set.
14560 When the large model is used all variables declared without a storage class
14561 will be allocated into the external ram, this includes all parameters and
14562 local variables (for non-reentrant
14563 \begin_inset LatexCommand \index{reentrant}
14568 When the small model is used variables without storage class are allocated
14569 in the internal ram.
14572 Judicious usage of the processor specific storage classes
14573 \begin_inset LatexCommand \index{Storage class}
14577 and the 'reentrant' function type will yield much more efficient code,
14578 than using the large model.
14579 Several optimizations are disabled when the program is compiled using the
14580 large model, it is therefore recommended that the small model be used unless
14581 absolutely required.
14582 \layout Subsubsection
14585 \begin_inset LatexCommand \label{sub:External-Stack}
14590 \begin_inset LatexCommand \index{stack}
14595 \begin_inset LatexCommand \index{External stack (mcs51)}
14602 The external stack (-
14613 \begin_inset LatexCommand \index{-\/-xstack}
14617 ) is located in pdata
14618 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14622 memory (usually at the start of the external ram segment) and uses all
14623 unused space in pdata (max.
14635 -xstack option is used to compile the program, the parameters and local
14637 \begin_inset LatexCommand \index{local variables}
14641 of all reentrant functions are allocated in this area.
14642 This option is provided for programs with large stack space requirements.
14643 When used with the -
14654 \begin_inset LatexCommand \index{-\/-stack-auto}
14658 option, all parameters and local variables are allocated on the external
14659 stack (note: support libraries will need to be recompiled with the same
14661 There is a predefined target in the library makefile).
14664 The compiler outputs the higher order address byte of the external ram segment
14666 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14671 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14675 ), therefore when using the External Stack option, this port
14679 be used by the application program.
14683 \begin_inset LatexCommand \index{Memory model}
14688 \begin_inset LatexCommand \index{DS390 memory model}
14695 The only model supported is Flat 24
14696 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14701 This generates code for the 24 bit contiguous addressing mode of the Dallas
14703 In this mode, up to four meg of external RAM or code space can be directly
14705 See the data sheets at www.dalsemi.com for further information on this part.
14709 Note that the compiler does not generate any code to place the processor
14710 into 24 bitmode (although
14714 in the ds390 libraries will do that for you).
14720 \begin_inset LatexCommand \index{Tinibios (DS390)}
14724 , the boot loader or similar code must ensure that the processor is in 24
14725 bit contiguous addressing mode before calling the SDCC startup code.
14743 option, variables will by default be placed into the XDATA segment.
14748 Segments may be placed anywhere in the 4 meg address space using the usual
14760 Note that if any segments are located above 64K, the -r flag must be passed
14761 to the linker to generate the proper segment relocations, and the Intel
14762 HEX output format must be used.
14763 The -r flag can be passed to the linker by using the option
14767 on the SDCC command line.
14768 However, currently the linker can not handle code segments > 64k.
14772 \begin_inset LatexCommand \index{Pragmas}
14779 SDCC supports the following #pragma directives:
14783 \begin_inset LatexCommand \index{\#pragma save}
14787 - this will save all current options to the save/restore stack.
14788 See #pragma\SpecialChar ~
14793 \begin_inset LatexCommand \index{\#pragma restore}
14797 - will restore saved options from the last save.
14798 saves & restores can be nested.
14799 SDCC uses a save/restore stack: save pushes current options to the stack,
14800 restore pulls current options from the stack.
14801 See #pragma\SpecialChar ~
14808 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14813 \begin_inset LatexCommand \index{function prologue}
14817 function1[,function2[,function3...]] - The compiler by default uses a caller
14818 saves convention for register saving across function calls, however this
14819 can cause unnecessary register pushing & popping
14820 \begin_inset LatexCommand \index{push/pop}
14824 when calling small functions from larger functions.
14825 This option can be used to switch off the register saving convention for
14826 the function names specified.
14827 The compiler will not save registers when calling these functions, extra
14828 code need to be manually inserted at the entry & exit for these functions
14829 to save & restore the registers used by these functions, this can SUBSTANTIALLY
14830 reduce code & improve run time performance of the generated code.
14831 In the future the compiler (with inter procedural analysis) may be able
14832 to determine the appropriate scheme to use for each function call.
14843 -callee-saves command line option is used, the function names specified
14844 in #pragma\SpecialChar ~
14846 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14850 is appended to the list of functions specified in the command line.
14854 \begin_inset LatexCommand \index{\#pragma exclude}
14858 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
14859 of pairs of push/pop
14860 \begin_inset LatexCommand \index{push/pop}
14869 \begin_inset LatexCommand \index{interrupt}
14882 The directive should be placed immediately before the ISR function definition
14883 and it affects ALL ISR functions following it.
14884 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
14885 exclude\SpecialChar ~
14887 \begin_inset LatexCommand \index{\#pragma exclude}
14892 See also the related keyword _naked
14893 \begin_inset LatexCommand \index{\_naked}
14898 \begin_inset LatexCommand \index{\_\_naked}
14906 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
14910 - the compiler will not warn you anymore for obvious mistakes, you'r on
14914 disable_warning <nnnn>
14915 \begin_inset LatexCommand \index{\#pragma disable\_warning}
14919 - the compiler will not warn you anymore about warning number <nnnn>.
14923 \begin_inset LatexCommand \index{\#pragma nogcse}
14927 - will stop global common subexpression elimination.
14931 \begin_inset LatexCommand \index{\#pragma noinduction}
14935 - will stop loop induction optimizations.
14939 \begin_inset LatexCommand \index{\#pragma noinvariant}
14943 - will not do loop invariant optimizations.
14944 For more details see Loop Invariants in section
14945 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14953 \begin_inset LatexCommand \index{\#pragma noiv}
14957 - Do not generate interrupt
14958 \begin_inset LatexCommand \index{interrupt}
14962 vector table entries for all ISR functions defined after the pragma.
14963 This is useful in cases where the interrupt vector table must be defined
14964 manually, or when there is a secondary, manually defined interrupt vector
14966 for the autovector feature of the Cypress EZ-USB FX2).
14967 More elegantly this can be achieved by obmitting the optional interrupt
14968 number after the interrupt keyword, see section
14969 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14978 \begin_inset LatexCommand \index{\#pragma nojtbound}
14982 - will not generate code for boundary value checking, when switch statements
14983 are turned into jump-tables (dangerous).
14984 For more details see section
14985 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14993 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14997 - Will not do loop reversal optimization
15001 \begin_inset LatexCommand \index{\#pragma nooverlay}
15005 - the compiler will not overlay the parameters and local variables of a
15010 \begin_inset LatexCommand \index{\#pragma stackauto}
15025 \begin_inset LatexCommand \index{-\/-stack-auto}
15030 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15034 Parameters and Local Variables.
15038 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15042 - The compiler will optimize code generation towards fast code, possibly
15043 at the expense of code size.
15047 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15051 - The compiler will optimize code generation towards compact code, possibly
15052 at the expense of code speed.
15056 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15060 - The compiler will attempt to generate code that is both compact and fast,
15061 as long as meeting one goal is not a detriment to the other (this is the
15067 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15071 - Generally follow the C89 standard, but allow SDCC features that conflict
15072 with the standard (default).
15076 \begin_inset LatexCommand \index{\#pragma std\_c89}
15080 - Follow the C89 standard and disable SDCC features that conflict with the
15085 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15089 - Generally follow the C99 standard, but allow SDCC features that conflict
15090 with the standard (incomplete support).
15094 \begin_inset LatexCommand \index{\#pragma std\_c99}
15098 - Follow the C99 standard and disable SDCC features that conflict with the
15099 standard (incomplete support).
15102 SDCPP supports the following #pragma directives:
15106 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15110 (+ | -) - switch _asm _endasm block preprocessing on / off.
15114 The pragma's are intended to be used to turn-on or off certain optimizations
15115 which might cause the compiler to generate extra stack / data space to
15116 store compiler generated temporary variables.
15117 This usually happens in large functions.
15118 Pragma directives should be used as shown in the following example, they
15119 are used to control options & optimizations for a given function; pragmas
15120 should be placed before and/or after a function, placing pragma's inside
15121 a function body could have unpredictable results.
15127 \begin_inset LatexCommand \index{\#pragma save}
15138 /* save the current settings */
15141 \begin_inset LatexCommand \index{\#pragma nogcse}
15150 /* turnoff global subexpression elimination */
15152 #pragma noinduction
15153 \begin_inset LatexCommand \index{\#pragma noinduction}
15157 /* turn off induction optimizations */
15180 \begin_inset LatexCommand \index{\#pragma restore}
15184 /* turn the optimizations back on */
15187 The compiler will generate a warning message when extra space is allocated.
15188 It is strongly recommended that the save and restore pragma's be used when
15189 changing options for a function.
15192 Defines Created by the Compiler
15195 The compiler creates the following #defines
15196 \begin_inset LatexCommand \index{\#defines}
15201 \begin_inset LatexCommand \index{Defines created by the compiler}
15211 \begin_inset Tabular
15212 <lyxtabular version="3" rows="10" columns="2">
15214 <column alignment="center" valignment="top" leftline="true" width="0">
15215 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15216 <row topline="true" bottomline="true">
15217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15238 <row topline="true">
15239 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15245 \begin_inset LatexCommand \index{SDCC}
15252 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15257 this Symbol is always defined
15261 <row topline="true">
15262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15268 \begin_inset LatexCommand \index{SDCC\_mcs51}
15273 \begin_inset LatexCommand \index{SDCC\_ds390}
15278 \begin_inset LatexCommand \index{SDCC\_z80}
15285 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15290 depending on the model used (e.g.: -mds390
15294 <row topline="true">
15295 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15301 \begin_inset LatexCommand \index{\_\_mcs51}
15306 \begin_inset LatexCommand \index{\_\_ds390}
15311 \begin_inset LatexCommand \index{\_\_hc08}
15316 \begin_inset LatexCommand \index{\_\_z80}
15323 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15328 depending on the model used (e.g.
15333 <row topline="true">
15334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15340 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15347 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15370 <row topline="true">
15371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15377 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15384 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15407 <row topline="true">
15408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15414 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15421 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15444 <row topline="true">
15445 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15451 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15458 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15481 <row topline="true">
15482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15488 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15508 <row topline="true" bottomline="true">
15509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15515 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15542 Notes on supported Processors
15546 \begin_inset LatexCommand \label{sub:MCS51-variants}
15551 \begin_inset LatexCommand \index{MCS51 variants}
15558 MCS51 processors are available from many vendors and come in many different
15560 While they might differ considerably in respect to Special Function Registers
15561 the core MCS51 is usually not modified or is kept compatible.
15565 pdata access by SFR
15568 With the upcome of devices with internal xdata and flash memory devices
15570 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15574 as dedicated I/O port is becoming more popular.
15575 Switching the high byte for pdata
15576 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15580 access which was formerly done by port P2 is then achieved by a Special
15582 \begin_inset LatexCommand \index{sfr}
15587 In well-established MCS51 tradition the address of this
15591 is where the chip designers decided to put it.
15592 Needless to say that they didn't agree on a common name either.
15593 So that the startup code can correctly initialize xdata variables, you
15594 should define an sfr with the name _XPAGE
15597 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15603 at the appropriate location if the default, port P2, is not used for this.
15609 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15614 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15619 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15622 For more exotic implementations further customizations may be needed.
15624 \begin_inset LatexCommand \ref{sub:Startup-Code}
15628 for other possibilities.
15631 Other Features available by SFR
15634 Some MCS51 variants offer features like Double DPTR
15635 \begin_inset LatexCommand \index{DPTR}
15639 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
15640 These are currently not used for the MCS51 port.
15641 If you absolutely need them you can fall back to inline assembly or submit
15648 The DS80C400 microcontroller has a rich set of peripherals.
15649 In its built-in ROM library it includes functions to access some of the
15650 features, among them is a TCP stack with IP4 and IP6 support.
15651 Library headers (currently in beta status) and other files are provided
15655 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
15663 The Z80 and gbz80 port
15666 SDCC can target both the Zilog
15667 \begin_inset LatexCommand \index{Z80}
15671 and the Nintendo Gameboy's Z80-like gbz80
15672 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
15677 The Z80 port is passed through the same
15680 \begin_inset LatexCommand \index{Regression test}
15686 as the MCS51 and DS390 ports, so floating point support, support for long
15687 variables and bitfield support is fine.
15688 See mailing lists and forums about interrupt routines.
15691 As always, the code is the authoritative reference - see z80/ralloc.c and
15694 \begin_inset LatexCommand \index{stack}
15698 frame is similar to that generated by the IAR Z80 compiler.
15699 IX is used as the base pointer, HL and IY are used as a temporary registers,
15700 and BC and DE are available for holding variables.
15702 \begin_inset LatexCommand \index{return value}
15706 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
15708 The gbz80 port use the same set of registers for the return values, but
15709 in a different order of significance: E (one byte), DE (two bytes), or
15716 The port to the Motorola HC08
15717 \begin_inset LatexCommand \index{HC08}
15721 family has been added in October 2003, and is still undergoing some basic
15723 The code generator is complete, but the register allocation is still quite
15725 Some of the SDCC's standard C library functions have embedded non-HC08
15726 inline assembly and so are not yet usable.
15737 \begin_inset LatexCommand \index{PIC14}
15741 port still requires a major effort from the development community.
15742 However it can work for very simple code.
15745 C code and 14bit PIC code page
15746 \begin_inset LatexCommand \index{code page (pic14)}
15751 \begin_inset LatexCommand \index{RAM bank (pic14)}
15758 The linker organizes allocation for the code page and RAM banks.
15759 It does not have intimate knowledge of the code flow.
15760 It will put all the code section of a single asm file into a single code
15762 In order to make use of multiple code pages, separate asm files must be
15764 The compiler treats all functions of a single C file as being in the same
15765 code page unless it is non static.
15766 The compiler treats all local variables of a single C file as being in
15767 the same RAM bank unless it is an extern.
15771 To get the best follow these guide lines:
15774 make local functions static, as non static functions require code page selection
15778 Make local variables static as extern variables require RAM bank selection
15782 For devices that have multiple code pages it is more efficient to use the
15783 same number of files as pages, i.e.
15784 for the 16F877 use 4 separate files and i.e.
15785 for the 16F874 use 2 separate files.
15786 This way the linker can put the code for each file into different code
15787 pages and the compiler can allocate reusable variables more efficiently
15788 and there's less page selection overhead.
15789 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
15790 instruction set) use 'unsigned char' whereever possible instead of 'int'.
15793 Creating a device include file
15796 For generating a device include file use the support perl script inc2h.pl
15797 kept in directory support/script.
15803 For the interrupt function, use the keyword 'interrupt'
15804 \begin_inset LatexCommand \index{interrupt}
15808 with level number of 0 (PIC14 only has 1 interrupt so this number is only
15809 there to avoid a syntax error - it ought to be fixed).
15815 void Intr(void) interrupt 0
15821 T0IF = 0; /* Clear timer interrupt */
15826 Linking and assembling
15829 For assembling you can use either GPUTILS'
15830 \begin_inset LatexCommand \index{gputils (pic tools)}
15834 gpasm.exe or MPLAB's mpasmwin.exe.
15835 GPUTILS is available from
15836 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
15841 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
15842 If you use MPLAB and an interrupt function then the linker script file
15843 vectors section will need to be enlarged to link with mplink.
15866 sdcc -S -V -mpic14 -p16F877 $<
15880 $(PRJ).hex: $(OBJS)
15890 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
15912 sdcc -S -V -mpic14 -p16F877 $<
15922 mpasmwin /q /o $*.asm
15926 $(PRJ).hex: $(OBJS)
15936 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
15939 Please note that indentations within a
15943 have to be done with a tabulator character.
15947 \begin_inset LatexCommand \index{PIC16}
15955 \begin_inset LatexCommand \index{PIC16}
15959 port is the portion of SDCC that is responsible to produce code for the
15961 \begin_inset LatexCommand \index{Microchip}
15965 (TM) microcontrollers with 16 bit core.
15966 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
15967 Currently supported devices are:
15971 \begin_inset Tabular
15972 <lyxtabular version="3" rows="4" columns="6">
15974 <column alignment="center" valignment="top" leftline="true" width="0">
15975 <column alignment="center" valignment="top" leftline="true" width="0">
15976 <column alignment="center" valignment="top" leftline="true" width="0">
15977 <column alignment="center" valignment="top" leftline="true" width="0">
15978 <column alignment="center" valignment="top" leftline="true" width="0">
15979 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15980 <row topline="true">
15981 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16013 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16030 <row topline="true">
16031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16047 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16055 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16063 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16071 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16080 <row topline="true">
16081 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16121 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16130 <row topline="true" bottomline="true">
16131 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16155 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16162 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16169 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16187 PIC16 port supports the standard command line arguments as supposed, with
16188 the exception of certain cases that will be mentioned in the following
16191 \labelwidthstring 00.00.0000
16203 -callee-saves See -
16215 \labelwidthstring 00.00.0000
16227 -all-callee-saves All function arguments are passed on stack by default.
16230 There is no need to specify this in the command line.
16232 \labelwidthstring 00.00.0000
16244 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16245 no local variables.
16248 Port Specific Options
16249 \begin_inset LatexCommand \index{Options PIC16}
16256 The port specific options appear after the global options in the sdcc --help
16258 \layout Subsubsection
16263 General options enable certain port features and optimizations.
16265 \labelwidthstring 00.00.0000
16277 -stack-model=[model] Used in conjuction with the command above.
16278 Defines the stack model to be used, valid stack models are :
16281 \labelwidthstring 00.00.0000
16287 Selects small stack model.
16288 8 bit stack and frame pointers.
16289 Supports 256 bytes stack size.
16291 \labelwidthstring 00.00.0000
16297 Selects large stack model.
16298 16 bit stack and frame pointers.
16299 Supports 65536 bytes stack size.
16302 \labelwidthstring 00.00.0000
16314 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16315 unitialized data variables with [kword].
16316 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16318 \labelwidthstring 00.00.0000
16330 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16331 Useful for bootloaders.
16333 \labelwidthstring 00.00.0000
16345 -asm= sets the full path and name of an external assembler to call.
16347 \labelwidthstring 00.00.0000
16359 -link= sets the full path and name of an external linker to call.
16361 \labelwidthstring 00.00.0000
16373 -mplab-comp MPLAB compatibility option.
16374 Currently only suppresses special gpasm directives.
16375 \layout Subsubsection
16377 Optimization Options
16379 \labelwidthstring 00.00.0000
16391 -optimize-goto Try to use (conditional) BRA instead of GOTO
16393 \labelwidthstring 00.00.0000
16405 -optimize-cmp Try to optimize some compares.
16407 \labelwidthstring 00.00.0000
16419 -obanksel=nn Set optimization level for inserting BANKSELs.
16424 \labelwidthstring 00.00.0000
16428 \labelwidthstring 00.00.0000
16430 1 checks previous used register and if it is the same then does not emit
16431 BANKSEL, accounts only for labels.
16433 \labelwidthstring 00.00.0000
16435 2 tries to check the location of (even different) symbols and removes BANKSELs
16436 if they are in the same bank.
16441 Important: There might be problems if the linker script has data sections
16442 across bank borders!
16444 \layout Subsubsection
16448 \labelwidthstring 00.00.0000
16460 -nodefaultlibs do not link default libraries when linking
16462 \labelwidthstring 00.00.0000
16474 -no-crt Don't link the default run-time modules
16476 \labelwidthstring 00.00.0000
16488 -use-crt= Use a custom run-time module instead of the defaults.
16489 \layout Subsubsection
16494 Debugging options enable extra debugging information in the output files.
16496 \labelwidthstring 00.00.0000
16508 -debug-xtra Similar to -
16519 \begin_inset LatexCommand \index{-\/-debug}
16523 , but dumps more information.
16525 \labelwidthstring 00.00.0000
16537 -debug-ralloc Force register allocator to dump <source>.d file with debugging
16539 <source> is the name of the file compiled.
16541 \labelwidthstring 00.00.0000
16553 -pcode-verbose Enable pcode debugging information in translation.
16555 \labelwidthstring 00.00.0000
16567 -denable-peeps Force the usage of peepholes.
16570 \labelwidthstring 00.00.0000
16582 -gstack Trace push/pops for stack pointer overflow
16584 \labelwidthstring 00.00.0000
16596 -call-tree dump call tree in .calltree file
16599 Enviromental Variables
16602 There is a number of enviromental variables that can be used when running
16603 SDCC to enable certain optimizations or force a specific program behaviour.
16604 these variables are primarily for debugging purposes so they can be enabled/dis
16608 Currently there is only two such variables available:
16610 \labelwidthstring 00.00.0000
16612 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
16613 bitfields is optimized by directly loading FSR0 with the address of the
16614 bitfield structure.
16615 Normally SDCC will cast the bitfield structure to a bitfield pointer and
16617 This step saves data ram and code space for functions that perform heavy
16620 80 bytes of code space are saved when compiling malloc.c with this option).
16623 \labelwidthstring 00.00.0000
16625 NO_REG_OPT do not perform pCode registers optimization.
16626 This should be used for debugging purposes.
16627 In some where bugs in the pcode optimizer are found, users can benefit
16628 from temporarily disabling the optimizer until the bug is fixed.
16631 Preprocessor Macros
16634 PIC16 port defines the following preprocessor macros while translating a
16639 \begin_inset Tabular
16640 <lyxtabular version="3" rows="6" columns="2">
16642 <column alignment="center" valignment="top" leftline="true" width="0">
16643 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16644 <row topline="true" bottomline="true">
16645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16662 <row topline="true">
16663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16676 Port identification
16680 <row topline="true">
16681 <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">
16704 Port identification (same as above)
16708 <row topline="true">
16709 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16717 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16722 MCU Identification.
16727 is the microcontrol identification number, i.e.
16732 <row topline="true">
16733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16756 MCU Identification (same as above)
16760 <row topline="true" bottomline="true">
16761 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16769 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16774 nnn = SMALL or LARGE respectively according to the stack model used
16785 In addition the following macros are defined when calling assembler:
16789 \begin_inset Tabular
16790 <lyxtabular version="3" rows="4" columns="2">
16792 <column alignment="center" valignment="top" leftline="true" width="0">
16793 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16794 <row topline="true" bottomline="true">
16795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16803 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16812 <row topline="true">
16813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16821 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16826 MCU Identification.
16831 is the microcontrol identification number, i.e.
16836 <row topline="true">
16837 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16845 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16850 nnn = SMALL or LARGE respectively according to the memory model used for
16855 <row topline="true" bottomline="true">
16856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16869 nnn = SMALL or LARGE respectively according to the stack model used
16884 \begin_inset LatexCommand \index{PIC16}
16888 port uses the following directories for searching header files and libraries.
16892 \begin_inset Tabular
16893 <lyxtabular version="3" rows="3" columns="4">
16895 <column alignment="center" valignment="top" leftline="true" width="0">
16896 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16897 <column alignment="center" valignment="top" width="0">
16898 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16899 <row topline="true" bottomline="true">
16900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16924 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16933 <row topline="true">
16934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16939 PREFIX/sdcc/include/pic16
16942 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16947 PIC16 specific headers
16950 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16958 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16967 <row topline="true" bottomline="true">
16968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16973 PREFIX/sdcc/lib/pic16
16976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16981 PIC16 specific libraries
16984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17009 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17016 PIC16 port currently supports the following pragmas:
17018 \labelwidthstring 00.00.0000
17020 stack pragma stack forces the code generator to initialize the stack & frame
17021 pointers at a specific address.
17022 This is an adhoc solution for cases where no STACK directive is available
17023 in the linker script or gplink is not instructed to create a stack section.
17025 The stack pragma should be used only once in a project.
17026 Multiple pragmas may result in indeterminate behaviour of the program.
17032 The old format (ie.
17033 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17034 cross page boundaries (or even exceed the available data RAM) and crash
17036 Make sure that stack does not cross page boundaries when using the SMALL
17042 The format is as follows:
17045 #pragma stack bottom_address [stack_size]
17052 is the lower bound of the stack section.
17053 The stack pointer initially will point at address (bottom_address+stack_size-1).
17061 /* initializes stack of 100 bytes at RAM address 0x200 */
17064 #pragma stack 0x200 100
17067 If the stack_size field is omitted then a stack is created with the default
17069 This size might be enough for most programs, but its not enough for operations
17070 with deep function nesting or excessive stack usage.
17072 \labelwidthstring 00.00.0000
17076 This pragma is deprecated.
17077 Its use will cause a warning message to be issued.
17083 \labelwidthstring 00.00.0000
17085 code place a function symbol at static FLASH address
17093 /* place function test_func at 0x4000 */
17096 #pragma code test_func 0x4000
17100 \labelwidthstring 00.00.0000
17102 library instructs the linker to use a library module.
17107 #pragma library module_name
17114 can be any library or object file (including its path).
17115 Note that there are four reserved keywords which have special meaning.
17120 \begin_inset Tabular
17121 <lyxtabular version="3" rows="6" columns="3">
17123 <column alignment="center" valignment="top" leftline="true" width="0">
17124 <column alignment="block" valignment="top" leftline="true" width="20page%">
17125 <column alignment="left" 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">
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">
17153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17168 ignore all library pragmas
17171 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17182 <row topline="true">
17183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17214 <row topline="true">
17215 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17230 link the Math libarary
17233 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17246 <row topline="true">
17247 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17262 link the I/O library
17265 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17278 <row topline="true" bottomline="true">
17279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17294 link the debug library
17297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17316 * is the device number, i.e.
17317 452 for PIC18F452 MCU.
17320 This feature allows for linking with specific libraries withoug having to
17321 explicit name them in the command line.
17326 keyword will reject all modules specified by the library pragma.
17328 \labelwidthstring 00.00.0000
17330 udata pragma udata instructs the compiler to emit code so that linker will
17331 place a variable at a specific memory bank
17339 /* places variable foo at bank2 */
17342 #pragma udata bank2 foo
17348 In order for this pragma to work extra SECTION directives should be added
17349 in the .lkr script.
17350 In the following example a sample .lkr file is shown:
17355 // Sample linker script for the PIC18F452 processor
17361 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17364 CODEPAGE NAME=page START=0x2A END=0x7FFF
17367 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17370 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17373 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17376 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17379 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17384 DATABANK NAME=gpr0 START=0x80 END=0xFF
17387 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17390 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17393 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17396 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17399 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17402 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17407 SECTION NAME=CONFIG ROM=config
17412 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17415 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17418 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17421 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17424 SECTION NAME=bank4 RAM=gpr4
17427 SECTION NAME=bank5 RAM=gpr5
17430 The linker will recognise the section name set in the pragma statement and
17431 will position the variable at the memory bank set with the RAM field at
17432 the SECTION line in the linker script file.
17436 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17443 There is one main header file that can be included to the source files using
17450 This header file contains the definitions for the processor special registers,
17451 so it is necessary if the source accesses them.
17452 It can be included by adding the following line in the beginning of the
17456 #include <pic18fregs.h>
17459 The specific microcontroller is selected within the pic18fregs.h automatically,
17460 so the same source can be used with a variety of devices.
17466 The libraries that PIC16
17467 \begin_inset LatexCommand \index{PIC16}
17471 port depends on are the microcontroller device libraries which contain
17472 the symbol definitions for the microcontroller special function registers.
17473 These libraries have the format pic18fxxxx.lib, where
17477 is the microcontroller identification number.
17478 The specific library is selected automatically by the compiler at link
17479 stage according to the selected device.
17482 Libraries are created with gplib which is part of the gputils package
17483 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17488 \layout Subsubsection*
17490 Building the libraries
17493 Before using SDCC/pic16 there are some libraries that need to be compiled.
17494 This process is not done automatically by SDCC since not all users use
17495 SDCC for pic16 projects.
17496 So each user should compile the libraries separately.
17499 The steps to compile the pic16 libraries under Linux are:
17502 cd device/lib/pic16
17517 su -c 'make install' # install the libraries, you need the root password
17520 If you need to install the headers too, do:
17526 su -c 'make install' # install the headers, you need the root password
17529 There exist a special target to build the I/O libraries.
17530 This target is not automatically build because it will build the I/O library
17536 This way building will take quite a lot of time.
17537 Users are advised to edit the
17539 device/lib/pic16/pics.build
17541 file and then execute:
17550 The following memory models are supported by the PIC16 port:
17559 Memory model affects the default size of pointers within the source.
17560 The sizes are shown in the next table:
17564 \begin_inset Tabular
17565 <lyxtabular version="3" rows="3" columns="3">
17567 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17568 <column alignment="center" valignment="top" leftline="true" width="0">
17569 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17570 <row topline="true" bottomline="true">
17571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17576 Pointer sizes according to memory model
17579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17596 <row topline="true" bottomline="true">
17597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17605 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17613 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17622 <row topline="true" bottomline="true">
17623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17631 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
17639 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17655 It is advisable that all sources within a project are compiled with the
17657 If one wants to override the default memory model, this can be done by
17658 declaring a pointer as
17667 Far selects large memory model's pointers, while near selects small memory
17671 The standard device libraries (see
17672 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
17676 ) contain no reference to pointers, so they can be used with both memory
17683 The stack implementation for the PIC16 port uses two indirect registers,
17686 \labelwidthstring 00.00.0000
17688 FSR1 is assigned as stack pointer
17690 \labelwidthstring 00.00.0000
17692 FSR2 is assigned as frame pointer
17695 The following stack models are supported by the PIC16 port
17716 model means that only the FSRxL byte is used to access stack and frame,
17723 uses both FSRxL and FSRxH registers.
17724 The following table shows the stack/frame pointers sizes according to stack
17725 model and the maximum space they can address:
17729 \begin_inset Tabular
17730 <lyxtabular version="3" rows="3" columns="3">
17732 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17733 <column alignment="center" valignment="top" leftline="true" width="0">
17734 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17735 <row topline="true" bottomline="true">
17736 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17741 Stack & Frame pointer sizes according to stack model
17744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17761 <row topline="true">
17762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17787 <row topline="true" bottomline="true">
17788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17824 stack model is currently not working properly throughout the code generator.
17825 So its use is not advised.
17826 Also there are some other points that need special care:
17831 Do not create stack sections with size more than one physical bank (that
17835 Stack sections should no cross physical bank limits (i.e.
17836 #pragma stack 0x50 0x100)
17839 These limitations are caused by the fact that only FSRxL is modified when
17840 using SMALL stack model, so no more than 256 bytes of stack can be used.
17841 This problem will disappear after LARGE model is fully implemented.
17847 In addition to the standard SDCC function keywords, PIC16 port makes available
17850 \labelwidthstring 00.00.0000
17852 wparam Use the WREG to pass one byte of the first function argument.
17853 This improves speed but you may not use this for functions with arguments
17854 that are called via function pointers, otherwise the first byte of the
17855 first parameter will get lost.
17859 void func_wparam(int a) wparam
17865 /* WREG hold the lower part of a */
17868 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
17878 This keyword replaces the deprecated wparam pragma.
17880 \labelwidthstring 00.00.0000
17882 shadowregs When entering/exiting an ISR, it is possible to take advantage
17883 of the PIC18F hardware shadow registers which hold the values of WREG,
17884 STATUS and BSR registers.
17885 This can be done by adding the keyword
17893 keyword in the function's header.
17896 void isr_shadow(void) shadowregs interrupt 1
17912 instructs the code generator not to store/restore WREG, STATUS, BSR when
17913 entering/exiting the ISR.
17916 Function return values
17919 Return values from functions are placed to the appropriate registers following
17920 a modified Microchip policy optimized for SDCC.
17921 The following table shows these registers:
17925 \begin_inset Tabular
17926 <lyxtabular version="3" rows="6" columns="2">
17928 <column alignment="center" valignment="top" leftline="true" width="0">
17929 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17930 <row topline="true" bottomline="true">
17931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17939 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17944 destination register
17948 <row topline="true">
17949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17957 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17966 <row topline="true">
17967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17984 <row topline="true">
17985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17993 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18002 <row topline="true">
18003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18011 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18016 FSR0L:PRODH:PRODL:WREG
18020 <row topline="true" bottomline="true">
18021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18029 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18034 on stack, FSR0 points to the beginning
18048 An interrupt servive routine (ISR) is declared using the
18055 void isr(void) interrupt
18073 is the interrupt number, which for PIC18F devices can be:
18077 \begin_inset Tabular
18078 <lyxtabular version="3" rows="4" columns="3">
18080 <column alignment="center" valignment="top" leftline="true" width="0">
18081 <column alignment="center" valignment="top" leftline="true" width="0">
18082 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18083 <row topline="true" bottomline="true">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18102 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18107 Interrupt Vector Address
18111 <row topline="true">
18112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18137 <row topline="true">
18138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18169 HIGH priority interrupts
18172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18181 <row topline="true" bottomline="true">
18182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18195 LOW priority interrupts
18198 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18214 When generating assembly code for ISR the code generator places a
18220 Interrupt Vector Address
18222 which points at the genetated ISR.
18223 This single GOTO instruction is part of an automatically generated
18225 interrupt entry point
18228 The actuall ISR code is placed as normally would in the code space.
18229 Upon interrupt request, the GOTO instruction is executed which jumps to
18231 When declaring interrupt functions as _naked this GOTO instruction is
18236 The whole interrupt functions is therefore placed at the Interrupt Vector
18237 Address of the specific interrupt.
18238 This is not a problem for the LOW priority interrupts, but it is a problem
18239 for the RESET and the HIGH priority interrupts because code may be written
18240 at the next interrupt´s vector address and cause undeterminate program
18241 behaviour if that interrupt is raised.
18247 This is not a problem when
18250 this is a HIGH interrupt ISR and LOW interrupts are
18257 when the ISR is small enough not to reach the next interrupt´s vector address.
18267 is possible to be omitted.
18268 This way a function is generated similar to an ISR, but it is not assigned
18272 When entering an interrupt, currently the PIC16
18273 \begin_inset LatexCommand \index{PIC16}
18277 port automatically saves the following registers:
18289 PROD (PRODL and PRODH)
18292 FSR0 (FSR0L and FSR0H)
18295 These registers are restored upon return from the interrupt routine.
18301 NOTE that when the _naked attribute is specified for an interrupt routine,
18302 then NO registers are stored or restored.
18311 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18312 There are 3 types of generic pointers currently implemented data, code
18313 and eeprom pointers.
18314 They are differentiated by the value of the 7th and 6th bits of the upper
18319 \begin_inset Tabular
18320 <lyxtabular version="3" rows="5" columns="5">
18322 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18323 <column alignment="center" valignment="top" width="0">
18324 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18325 <column alignment="center" valignment="top" width="0">
18326 <column alignment="left" valignment="top" rightline="true" width="0">
18327 <row topline="true" bottomline="true">
18328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18344 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18357 rest of the pointer
18360 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18369 <row topline="true" bottomline="true">
18370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18378 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18386 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18394 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18403 uuuuuu uuuuxxxx xxxxxxxx
18406 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18411 a 12-bit data pointer in data RAM memory
18415 <row bottomline="true">
18416 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18424 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18440 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18449 uxxxxx xxxxxxxx xxxxxxxx
18452 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18457 a 21-bit code pointer in FLASH memory
18461 <row bottomline="true">
18462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18470 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18478 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18486 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18495 uuuuuu uuuuuuxx xxxxxxxx
18498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18503 a 10-bit eeprom pointer in EEPROM memory
18507 <row bottomline="true">
18508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18524 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18541 xxxxxx xxxxxxxx xxxxxxxx
18544 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18549 unimplemented pointer type
18560 Generic pointer are read and written with a set of library functions which
18561 read/write 1, 2, 3, 4 bytes.
18565 \layout Subsubsection
18567 Standard I/O Streams
18574 the type FILE is defined as:
18577 typedef char * FILE;
18580 This type is the stream type implemented I/O in the PIC18F devices.
18581 Also the standard input and output streams are declared in stdio.h:
18584 extern FILE * stdin;
18587 extern FILE * stdout;
18590 The FILE type is actually a generic pointer which defines one more type
18591 of generic pointers, the
18596 This new type has the format:
18600 \begin_inset Tabular
18601 <lyxtabular version="3" rows="2" columns="7">
18603 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18604 <column alignment="center" valignment="top" width="0">
18605 <column alignment="center" valignment="top" leftline="true" width="0">
18606 <column alignment="center" valignment="top" leftline="true" width="0">
18607 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18608 <column alignment="center" valignment="top" width="0">
18609 <column alignment="left" valignment="top" rightline="true" width="0">
18610 <row topline="true" bottomline="true">
18611 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18635 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18656 rest of the pointer
18659 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18668 <row topline="true" bottomline="true">
18669 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18677 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18685 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18726 upper byte high nubble is 0x2n, the rest are zeroes
18737 Currently implemented there are 3 types of streams defined:
18741 \begin_inset Tabular
18742 <lyxtabular version="3" rows="4" columns="4">
18744 <column alignment="center" valignment="top" leftline="true" width="0">
18745 <column alignment="center" valignment="top" leftline="true" width="0">
18746 <column alignment="center" valignment="top" leftline="true" width="0">
18747 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18748 <row topline="true" bottomline="true">
18749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18782 <row topline="true">
18783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18814 Writes/Reads characters via the USART peripheral
18818 <row topline="true">
18819 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18827 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18837 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18845 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18850 Writes/Reads characters via the MSSP peripheral
18854 <row topline="true" bottomline="true">
18855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18873 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18881 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18886 Writes/Reads characters via used defined functions
18897 The stream identifiers are declared as macros in the stdio.h header.
18900 In the libc library there exist the functions that are used to write to
18901 each of the above streams.
18904 \labelwidthstring 00.00.0000
18916 _stream_usart_putchar writes a character at the USART stream
18918 \labelwidthstring 00.00.0000
18930 _stream_mssp_putchar writes a character at the MSSP stream
18932 \labelwidthstring 00.00.0000
18934 putchar dummy function.
18935 This writes a character to a user specified manner.
18938 In order to increase performance
18942 is declared in stdio.h as having its parameter in WREG (it has the wparam
18944 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
18945 in a user-friendly way.
18950 is the name of the variable that holds the character to print.
18951 An example follows:
18954 #include <pic18fregs.h>
18966 PORTA = c; /* dump character c to PORTA */
18979 stdout = STREAM_USER; /* this is not necessery, since stdout points
18982 * by default to STREAM_USER */
18985 printf (¨This is a printf test
18993 \layout Subsubsection
18998 PIC16 contains an implementation of the printf-family of functions.
18999 There exist the following functions:
19002 extern unsigned int sprintf(char *buf, char *fmt, ...);
19005 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19010 extern unsigned int printf(char *fmt, ...);
19013 extern unsigned int vprintf(char *fmt, va_lista ap);
19018 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19021 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19024 For sprintf and vsprintf
19028 should normally be a data pointer where the resulting string will be placed.
19029 No range checking is done so the user should allocate the necessery buffer.
19030 For fprintf and vfprintf
19034 should be a stream pointer (i.e.
19035 stdout, STREAM_MSSP, etc...).
19036 \layout Subsubsection
19041 The PIC18F family of microcontrollers supports a number of interrupt sources.
19042 A list of these interrupts is shown in the following table:
19046 \begin_inset Tabular
19047 <lyxtabular version="3" rows="11" columns="4">
19049 <column alignment="left" valignment="top" leftline="true" width="0">
19050 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19051 <column alignment="left" valignment="top" leftline="true" width="0">
19052 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19053 <row topline="true" bottomline="true">
19054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19062 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19070 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19078 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19087 <row topline="true">
19088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19096 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19101 PORTB change interrupt
19104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19112 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19117 EEPROM/FLASH write complete interrupt
19121 <row topline="true">
19122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19135 INT0 external interrupt
19138 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19146 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19151 Bus collision interrupt
19155 <row topline="true">
19156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19169 INT1 external interrupt
19172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19180 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19185 Low voltage detect interrupt
19189 <row topline="true">
19190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19198 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19203 INT2 external interrupt
19206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19214 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19219 Parallel slave port interrupt
19223 <row topline="true">
19224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19237 CCP1 module interrupt
19240 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19248 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19253 AD convertion complete interrupt
19257 <row topline="true">
19258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19266 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19271 CCP2 module interrupt
19274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19282 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19287 USART receive interrupt
19291 <row topline="true">
19292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19300 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19305 TMR0 overflow interrupt
19308 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19321 USART transmit interrupt
19325 <row topline="true">
19326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19339 TMR1 overflow interrupt
19342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19350 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19355 SSP receive/transmit interrupt
19359 <row topline="true">
19360 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19373 TMR2 matches PR2 interrupt
19376 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19383 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19391 <row topline="true" bottomline="true">
19392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19405 TMR3 overflow interrupt
19408 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19415 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19430 The prototypes for these names are defined in the header file
19437 In order to simplify signal handling, a number of macros is provided:
19439 \labelwidthstring 00.00.0000
19441 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19442 high priority interrupts.
19447 is the function name to use.
19449 \labelwidthstring 00.00.0000
19451 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19452 low priority interrupt.
19457 is the function name to use.
19459 \labelwidthstring 00.00.0000
19461 DEF_HANDLER(sig,handler) define a handler for signal
19465 \labelwidthstring 00.00.0000
19467 END_DEF end the declaration of the dispatch table.
19470 Additionally there are two more macros to simplify the declaration of the
19473 \labelwidthstring 00.00.0000
19477 SIGHANDLER(handler)
19479 this declares the function prototype for the
19485 \labelwidthstring 00.00.0000
19487 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19490 An example of using the macros above is shown below:
19493 #include <pic18fregs.h>
19496 #include <signal.h>
19500 DEF_INTHIGH(high_int)
19503 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
19506 DEF_HANDLER(SIG_BCOL, _bcol_handler)
19513 SIGHANDLER(_tmr0_handler)
19519 /* action to be taken when timer 0 overflows */
19526 SIGHANDLERNAKED(_bcol_handler)
19535 /* action to be taken when bus collision occurs */
19551 Special care should be taken when using the above scheme:
19554 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
19557 when declaring SIGHANDLERNAKED handler never forget to use
19561 for proper returning.
19567 Here you can find some general tips for compiling programs with SDCC/pic16.
19568 \layout Subsubsection
19573 The default stack size (that is 64 bytes) probably is enough for many programs.
19574 One must take care that when there are many levels of function nesting,
19575 or there is excessive usage of stack, its size should be extended.
19576 An example of such a case is the printf/sprintf family of functions.
19577 If you encounter problems like not being able to print integers, then you
19578 need to set the stack size around the maximum (256 for small stack model).
19579 The following diagram shows what happens when calling printf to print an
19583 printf () --> ltoa () --> ultoa () --> divschar ()
19586 It is should be understood that stack is easily consumed when calling complicate
19588 Using command line arguments like -
19598 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
19600 Other ways to reduce stack usage may exist.
19603 Debugging with SDCDB
19604 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
19609 \begin_inset LatexCommand \index{sdcdb (debugger)}
19616 SDCC is distributed with a source level debugger
19617 \begin_inset LatexCommand \index{Debugger}
19622 The debugger uses a command line interface, the command repertoire of the
19623 debugger has been kept as close to gdb
19624 \begin_inset LatexCommand \index{gdb}
19628 (the GNU debugger) as possible.
19629 The configuration and build process is part of the standard compiler installati
19630 on, which also builds and installs the debugger in the target directory
19631 specified during configuration.
19632 The debugger allows you debug BOTH at the C source and at the ASM source
19634 Sdcdb is available on Unix platforms only.
19637 Compiling for Debugging
19651 \begin_inset LatexCommand \index{-\/-debug}
19655 option must be specified for all files for which debug information is to
19657 The complier generates a .adb file for each of these files.
19658 The linker creates the .cdb
19659 \begin_inset LatexCommand \index{<file>.cdb}
19664 \begin_inset LatexCommand \index{<file>.adb}
19668 files and the address information.
19669 This .cdb is used by the debugger.
19672 How the Debugger Works
19685 -debug option is specified the compiler generates extra symbol information
19686 some of which are put into the assembler source and some are put into the
19688 Then the linker creates the .cdb file from the individual .adb files with
19689 the address information for the symbols.
19690 The debugger reads the symbolic information generated by the compiler &
19691 the address information generated by the linker.
19692 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
19693 execution is controlled by the debugger.
19694 When a command is issued for the debugger, it translates it into appropriate
19695 commands for the simulator.
19698 Starting the Debugger
19701 The debugger can be started using the following command line.
19702 (Assume the file you are debugging has the file name foo).
19716 The debugger will look for the following files.
19719 foo.c - the source file.
19722 foo.cdb - the debugger symbol information file.
19725 foo.ihx - the Intel hex format
19726 \begin_inset LatexCommand \index{Intel hex format}
19733 Command Line Options.
19746 -directory=<source file directory> this option can used to specify the directory
19748 The debugger will look into the directory list specified for source, cdb
19750 The items in the directory list must be separated by ':', e.g.
19751 if the source files can be in the directories /home/src1 and /home/src2,
19762 -directory option should be -
19772 -directory=/home/src1:/home/src2.
19773 Note there can be no spaces in the option.
19777 -cd <directory> - change to the <directory>.
19780 -fullname - used by GUI front ends.
19783 -cpu <cpu-type> - this argument is passed to the simulator please see the
19784 simulator docs for details.
19787 -X <Clock frequency > this options is passed to the simulator please see
19788 the simulator docs for details.
19791 -s <serial port file> passed to simulator see the simulator docs for details.
19794 -S <serial in,out> passed to simulator see the simulator docs for details.
19797 -k <port number> passed to simulator see the simulator docs for details.
19803 As mentioned earlier the command interface for the debugger has been deliberatel
19804 y kept as close the GNU debugger gdb, as possible.
19805 This will help the integration with existing graphical user interfaces
19806 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
19807 If you use a graphical user interface for the debugger you can skip the
19809 \layout Subsubsection*
19811 break [line | file:line | function | file:function]
19814 Set breakpoint at specified line or function:
19823 sdcdb>break foo.c:100
19825 sdcdb>break funcfoo
19827 sdcdb>break foo.c:funcfoo
19828 \layout Subsubsection*
19830 clear [line | file:line | function | file:function ]
19833 Clear breakpoint at specified line or function:
19842 sdcdb>clear foo.c:100
19844 sdcdb>clear funcfoo
19846 sdcdb>clear foo.c:funcfoo
19847 \layout Subsubsection*
19852 Continue program being debugged, after breakpoint.
19853 \layout Subsubsection*
19858 Execute till the end of the current function.
19859 \layout Subsubsection*
19864 Delete breakpoint number 'n'.
19865 If used without any option clear ALL user defined break points.
19866 \layout Subsubsection*
19868 info [break | stack | frame | registers ]
19871 info break - list all breakpoints
19874 info stack - show the function call stack.
19877 info frame - show information about the current execution frame.
19880 info registers - show content of all registers.
19881 \layout Subsubsection*
19886 Step program until it reaches a different source line.
19887 Note: pressing <return> repeats the last command.
19888 \layout Subsubsection*
19893 Step program, proceeding through subroutine calls.
19894 \layout Subsubsection*
19899 Start debugged program.
19900 \layout Subsubsection*
19905 Print type information of the variable.
19906 \layout Subsubsection*
19911 print value of variable.
19912 \layout Subsubsection*
19917 load the given file name.
19918 Note this is an alternate method of loading file for debugging.
19919 \layout Subsubsection*
19924 print information about current frame.
19925 \layout Subsubsection*
19930 Toggle between C source & assembly source.
19931 \layout Subsubsection*
19933 ! simulator command
19936 Send the string following '!' to the simulator, the simulator response is
19938 Note the debugger does not interpret the command being sent to the simulator,
19939 so if a command like 'go' is sent the debugger can loose its execution
19940 context and may display incorrect values.
19941 \layout Subsubsection*
19948 My name is Bobby Brown"
19951 Interfacing with XEmacs
19952 \begin_inset LatexCommand \index{XEmacs}
19957 \begin_inset LatexCommand \index{Emacs}
19964 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
19965 sdcdb.el and sdcdbsrc.el.
19966 These two files can be found in the $(prefix)/bin directory after the installat
19968 These files need to be loaded into XEmacs for the interface to work.
19969 This can be done at XEmacs startup time by inserting the following into
19970 your '.xemacs' file (which can be found in your HOME directory):
19976 (load-file sdcdbsrc.el)
19982 .xemacs is a lisp file so the () around the command is REQUIRED.
19983 The files can also be loaded dynamically while XEmacs is running, set the
19984 environment variable 'EMACSLOADPATH' to the installation bin directory
19985 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
19986 To start the interface enter the following command:
20000 You will prompted to enter the file name to be debugged.
20005 The command line options that are passed to the simulator directly are bound
20006 to default values in the file sdcdbsrc.el.
20007 The variables are listed below, these values maybe changed as required.
20010 sdcdbsrc-cpu-type '51
20013 sdcdbsrc-frequency '11059200
20016 sdcdbsrc-serial nil
20019 The following is a list of key mapping for the debugger interface.
20030 ;;key\SpecialChar ~
20044 binding\SpecialChar ~
20068 ;;---\SpecialChar ~
20082 -------\SpecialChar ~
20124 sdcdb-next-from-src\SpecialChar ~
20152 sdcdb-back-from-src\SpecialChar ~
20180 sdcdb-cont-from-src\SpecialChar ~
20190 SDCDB continue command
20208 sdcdb-step-from-src\SpecialChar ~
20236 sdcdb-whatis-c-sexp\SpecialChar ~
20246 SDCDB ptypecommand for data at
20313 sdcdbsrc-delete\SpecialChar ~
20327 SDCDB Delete all breakpoints if no arg
20376 given or delete arg (C-u arg x)
20394 sdcdbsrc-frame\SpecialChar ~
20409 SDCDB Display current frame if no arg,
20458 given or display frame arg
20525 sdcdbsrc-goto-sdcdb\SpecialChar ~
20535 Goto the SDCDB output buffer
20553 sdcdb-print-c-sexp\SpecialChar ~
20564 SDCDB print command for data at
20631 sdcdbsrc-goto-sdcdb\SpecialChar ~
20641 Goto the SDCDB output buffer
20659 sdcdbsrc-mode\SpecialChar ~
20675 Toggles Sdcdbsrc mode (turns it off)
20690 sdcdb-finish-from-src\SpecialChar ~
20698 SDCDB finish command
20713 sdcdb-break\SpecialChar ~
20731 Set break for line with point
20746 sdcdbsrc-mode\SpecialChar ~
20762 Toggle Sdcdbsrc mode
20777 sdcdbsrc-srcmode\SpecialChar ~
20800 Here are a few guidelines that will help the compiler generate more efficient
20801 code, some of the tips are specific to this compiler others are generally
20802 good programming practice.
20805 Use the smallest data type to represent your data-value.
20806 If it is known in advance that the value is going to be less than 256 then
20807 use an 'unsigned char' instead of a 'short' or 'int'.
20808 Please note, that ANSI C requires both signed and unsigned chars to be
20809 promoted to 'signed int' before doing any operation.
20811 \begin_inset LatexCommand \index{type promotion}
20815 can be omitted, if the result is the same.
20816 The effect of the promotion rules together with the sign-extension is often
20823 unsigned char uc = 0xfe;
20825 if (uc * uc < 0) /* this is true! */
20844 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
20854 (unsigned char) -12 / (signed char) -3 = ...
20857 No, the result is not 4:
20862 (int) (unsigned char) -12 / (int) (signed char) -3 =
20864 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
20866 (int) 0x00f4 / (int) 0xfffd =
20868 (int) 0x00f4 / (int) 0xfffd =
20870 (int) 244 / (int) -3 =
20872 (int) -81 = (int) 0xffaf;
20875 Don't complain, that gcc gives you a different result.
20876 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
20877 Therefore the results are different.
20880 \begin_inset Quotes sld
20884 \begin_inset Quotes srd
20890 If well-defined overflow characteristics are important and negative values
20891 are not, or if you want to steer clear of sign-extension problems when
20892 manipulating bits or bytes, use one of the corresponding unsigned types.
20893 (Beware when mixing signed and unsigned values in expressions, though.)
20895 Although character types (especially unsigned char) can be used as "tiny"
20896 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
20897 ble sign extension and increased code size.
20901 Use unsigned when it is known in advance that the value is not going to
20903 This helps especially if you are doing division or multiplication, bit-shifting
20904 or are using an array index.
20907 NEVER jump into a LOOP.
20910 Declare the variables to be local
20911 \begin_inset LatexCommand \index{local variables}
20915 whenever possible, especially loop control variables (induction).
20918 Since the compiler does not always do implicit integral promotion, the programme
20919 r should do an explicit cast when integral promotion is required.
20922 Reducing the size of division, multiplication & modulus operations can reduce
20923 code size substantially.
20924 Take the following code for example.
20930 foobar(unsigned int p1, unsigned char ch)
20938 unsigned char ch1 = p1 % ch ;
20949 For the modulus operation the variable ch will be promoted to unsigned int
20950 first then the modulus operation will be performed (this will lead to a
20951 call to support routine _moduint()), and the result will be casted to a
20953 If the code is changed to
20958 foobar(unsigned int p1, unsigned char ch)
20966 unsigned char ch1 = (unsigned char)p1 % ch ;
20977 It would substantially reduce the code generated (future versions of the
20978 compiler will be smart enough to detect such optimization opportunities).
20982 Have a look at the assembly listing to get a
20983 \begin_inset Quotes sld
20987 \begin_inset Quotes srd
20990 for the code generation.
20994 \begin_inset LatexCommand \index{Tools}
20998 included in the distribution
21002 \begin_inset Tabular
21003 <lyxtabular version="3" rows="12" columns="3">
21005 <column alignment="center" valignment="top" leftline="true" width="0pt">
21006 <column alignment="center" valignment="top" leftline="true" width="0pt">
21007 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
21008 <row topline="true" bottomline="true">
21009 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21025 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21034 <row topline="true">
21035 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21048 Simulator for various architectures
21051 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21060 <row topline="true">
21061 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21069 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21074 header file conversion
21077 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21082 sdcc/support/scripts
21086 <row topline="true">
21087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21100 header file conversion
21103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21108 sdcc/support/scripts
21112 <row topline="true">
21113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21147 <row topline="true">
21148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21182 <row topline="true">
21183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21199 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21217 <row topline="true">
21218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21252 <row topline="true">
21253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21269 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21287 <row topline="true">
21288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21304 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21322 <row topline="true">
21323 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21331 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21339 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21357 <row topline="true" bottomline="true">
21358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21366 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21402 \begin_inset LatexCommand \index{Documentation}
21406 included in the distribution
21410 \begin_inset Tabular
21411 <lyxtabular version="3" rows="10" columns="2">
21413 <column alignment="left" valignment="top" leftline="true" width="0">
21414 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21415 <row topline="true" bottomline="true">
21416 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21429 Where to get / filename
21433 <row topline="true">
21434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21439 SDCC Compiler User Guide
21442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21447 You're reading it right now
21451 <row topline="true">
21452 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21469 <row topline="true">
21470 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21476 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
21481 \begin_inset LatexCommand \index{Assembler documentation}
21485 Assemblers and ASLINK
21486 \begin_inset LatexCommand \index{aslink}
21491 \begin_inset LatexCommand \index{Linker documentation}
21498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21503 sdcc/as/doc/asxhtm.html
21507 <row topline="true">
21508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21513 SDCC regression test
21514 \begin_inset LatexCommand \index{Regression test}
21521 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21526 sdcc/doc/test_suite_spec.pdf
21530 <row topline="true">
21531 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21548 <row topline="true">
21549 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21554 Notes on debugging with sdcdb
21555 \begin_inset LatexCommand \index{sdcdb (debugger)}
21562 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21567 sdcc/debugger/README
21571 <row topline="true">
21572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21577 Software simulator for microcontrollers
21580 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21607 <row topline="true">
21608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21613 Temporary notes on the pic16
21614 \begin_inset LatexCommand \index{PIC16}
21621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21626 sdcc/src/pic16/NOTES
21630 <row topline="true" bottomline="true">
21631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21636 SDCC internal documentation (debugging file format)
21639 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21675 Related open source tools
21676 \begin_inset LatexCommand \index{Related tools}
21684 \begin_inset Tabular
21685 <lyxtabular version="3" rows="11" columns="3">
21687 <column alignment="center" valignment="top" leftline="true" width="0pt">
21688 <column alignment="block" valignment="top" leftline="true" width="30line%">
21689 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
21690 <row topline="true" bottomline="true">
21691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21716 <row topline="true">
21717 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21723 \begin_inset LatexCommand \index{gpsim (pic simulator)}
21730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21738 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21744 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
21752 <row topline="true">
21753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21759 \begin_inset LatexCommand \index{gputils (pic tools)}
21766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21774 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21780 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
21788 <row topline="true">
21789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21797 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21805 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21811 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
21819 <row topline="true">
21820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21826 \begin_inset LatexCommand \index{indent (source formatting tool)}
21833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21838 Formats C source - Master of the white spaces
21841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21847 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
21855 <row topline="true">
21856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21862 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
21869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21874 Object file conversion, checksumming, ...
21877 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21883 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
21891 <row topline="true">
21892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21898 \begin_inset LatexCommand \index{objdump (tool)}
21905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21910 Object file conversion, ...
21913 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21918 Part of binutils (should be there anyway)
21922 <row topline="true">
21923 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21929 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
21936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21941 Source code documentation system
21944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21950 \begin_inset LatexCommand \url{http://www.doxygen.org}
21958 <row topline="true">
21959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21972 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
21975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21981 \begin_inset LatexCommand \url{http://www.kdevelop.org}
21989 <row topline="true">
21990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21996 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22008 Statically checks c sources (has anyone adapted splint for SDCC?)
22011 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22017 \begin_inset LatexCommand \url{http://www.splint.org}
22025 <row topline="true" bottomline="true">
22026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22032 \begin_inset LatexCommand \index{ddd (debugger)}
22039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22044 Debugger, serves nicely as GUI to sdcdb
22045 \begin_inset LatexCommand \index{sdcdb (debugger)}
22052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22058 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22075 Related documentation / recommended reading
22079 \begin_inset Tabular
22080 <lyxtabular version="3" rows="6" columns="3">
22082 <column alignment="center" valignment="top" leftline="true" width="0pt">
22083 <column alignment="block" valignment="top" leftline="true" width="30line%">
22084 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22085 <row topline="true" bottomline="true">
22086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22102 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22111 <row topline="true">
22112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22135 \begin_inset LatexCommand \index{C Reference card}
22142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22148 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22156 <row topline="true">
22157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22179 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22187 <row topline="true">
22188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22200 Latest datasheet of the target CPU
22203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22212 <row topline="true">
22213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22225 Revision history of datasheet
22228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22237 <row topline="true" bottomline="true">
22238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22253 Advanced Compiler Design and Implementation
22256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22261 bookstore (very dedicated, probably read other books first)
22277 Some questions answered, some pointers given - it might be time to in turn
22285 can you solve your project with the selected microcontroller? Would you
22286 find out early or rather late that your target is too small/slow/whatever?
22287 Can you switch to a slightly better device if it doesn't fit?
22290 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22291 and/or another programming language be more adequate? Would an operating
22292 system on the target device help?
22295 if you solved the problem, will the marketing department be happy?
22298 if the marketing department is happy, will customers be happy?
22301 if you're the project manager, marketing department and maybe even the customer
22302 in one person, have you tried to see the project from the outside?
22305 is the project done if you think it is done? Or is just that other interface/pro
22306 tocol/feature/configuration/option missing? How about website, manual(s),
22307 internationali(z|s)ation, packaging, labels, 2nd source for components,
22308 electromagnetic compatability/interference, documentation for production,
22309 production test software, update mechanism, patent issues?
22312 is your project adequately positioned in that magic triangle: fame, fortune,
22316 Maybe not all answers to these questions are known and some answers may
22321 , nevertheless knowing these questions may help you to avoid burnout
22327 burnout is bad for electronic devices, programmers and motorcycle tyres
22331 Chances are you didn't want to hear some of them...
22335 \begin_inset LatexCommand \index{Support}
22342 SDCC has grown to be a large project.
22343 The compiler alone (without the preprocessor, assembler and linker) is
22344 well over 100,000 lines of code (blank stripped).
22345 The open source nature of this project is a key to its continued growth
22347 You gain the benefit and support of many active software developers and
22349 Is SDCC perfect? No, that's why we need your help.
22350 The developers take pride in fixing reported bugs.
22351 You can help by reporting the bugs and helping other SDCC users.
22352 There are lots of ways to contribute, and we encourage you to take part
22353 in making SDCC a great software package.
22357 The SDCC project is hosted on the SDCC sourceforge site at
22358 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22363 You'll find the complete set of mailing lists
22364 \begin_inset LatexCommand \index{Mailing list(s)}
22368 , forums, bug reporting system, patch submission
22369 \begin_inset LatexCommand \index{Patch submission}
22374 \begin_inset LatexCommand \index{download}
22378 area and cvs code repository
22379 \begin_inset LatexCommand \index{cvs code repository}
22387 \begin_inset LatexCommand \index{Bug reporting}
22392 \begin_inset LatexCommand \index{Reporting bugs}
22399 The recommended way of reporting bugs is using the infrastructure of the
22401 You can follow the status of bug reports there and have an overview about
22405 Bug reports are automatically forwarded to the developer mailing list and
22406 will be fixed ASAP.
22407 When reporting a bug, it is very useful to include a small test program
22408 (the smaller the better) which reproduces the problem.
22409 If you can isolate the problem by looking at the generated assembly code,
22410 this can be very helpful.
22411 Compiling your program with the -
22422 \begin_inset LatexCommand \index{-\/-dumpall}
22426 option can sometimes be useful in locating optimization problems.
22427 When reporting a bug please maker sure you:
22430 Attach the code you are compiling with SDCC.
22434 Specify the exact command you use to run SDCC, or attach your Makefile.
22438 Specify the SDCC version (type "
22444 "), your platform, and operating system.
22448 Provide an exact copy of any error message or incorrect output.
22452 Put something meaningful in the subject of your message.
22455 Please attempt to include these 5 important parts, as applicable, in all
22456 requests for support or when reporting any problems or bugs with SDCC.
22457 Though this will make your message lengthy, it will greatly improve your
22458 chance that SDCC users and developers will be able to help you.
22459 Some SDCC developers are frustrated by bug reports without code provided
22460 that they can use to reproduce and ultimately fix the problem, so please
22461 be sure to provide sample code if you are reporting a bug!
22464 Please have a short check that you are using a recent version of SDCC and
22465 the bug is not yet known.
22466 This is the link for reporting bugs:
22467 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
22474 Requesting Features
22475 \begin_inset LatexCommand \label{sub:Requesting-Features}
22480 \begin_inset LatexCommand \index{Feature request}
22485 \begin_inset LatexCommand \index{Requesting features}
22492 Like bug reports feature requests are forwarded to the developer mailing
22494 This is the link for requesting features:
22495 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
22505 Like bug reports contributed patches are forwarded to the developer mailing
22507 This is the link for submitting patches
22508 \begin_inset LatexCommand \index{Patch submission}
22513 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
22520 You need to specify some parameters to the
22524 command for the patches to be useful.
22525 If you modified more than one file a patch created f.e.
22530 \begin_inset Quotes sld
22533 diff -Naur unmodified_directory modified_directory >my_changes.patch
22534 \begin_inset Quotes srd
22540 will be fine, otherwise
22544 \begin_inset Quotes sld
22547 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
22548 \begin_inset Quotes srd
22561 These links should take you directly to the
22562 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
22572 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
22573 automated messages (mid 2003)
22577 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
22582 \begin_inset LatexCommand \index{Mailing list(s)}
22586 and forums are archived and searchable so if you are lucky someone already
22587 had a similar problem.
22588 While mails to the lists themselves are delivered promptly their web front
22589 end on sourceforge sometimes shows a severe time lag (up to several weeks),
22590 if you're seriously using SDCC please consider subscribing to the lists.
22596 You can follow the status of the cvs version
22597 \begin_inset LatexCommand \index{version}
22601 of SDCC by watching the Changelog
22602 \begin_inset LatexCommand \index{Changelog}
22606 in the cvs-repository
22611 \begin_inset LatexCommand \htmlurl{http://cvs.sf.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
22619 \begin_inset LatexCommand \index{Release policy}
22626 Historically there often were long delays between official releases and
22627 the sourceforge download area tends to get not updated at all.
22628 Excuses in the past might have referred to problems with live range analysis,
22629 but as this was fixed a while ago, the current problem is that another
22630 excuse has to be found.
22631 Kidding aside, we have to get better there! On the other hand there are
22632 daily snapshots available at
22633 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
22637 , and you can always build the very last version (hopefully with many bugs
22638 fixed, and features added) from the source code available at
22639 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
22647 \begin_inset LatexCommand \index{Examples}
22654 You'll find some small examples in the directory
22656 sdcc/device/examples/.
22659 More examples and libraries are available at
22661 The SDCC Open Knowledge Resource
22662 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
22669 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
22676 I did insert a reference to Paul's web site here although it seems rather
22677 dedicated to a specific 8032 board (I think it's okay because it f.e.
22678 shows LCD/Harddisc interface and has a free 8051 monitor.
22679 Independent 8032 board vendors face hard competition of heavily subsidized
22680 development boards anyway).
22683 Maybe we should include some links to real world applications.
22684 Preferably pointer to pointers (one for each architecture) so this stays
22689 \begin_inset LatexCommand \index{Quality control}
22696 The compiler is passed through nightly compile and build checks.
22702 \begin_inset LatexCommand \index{Regression test}
22706 check that SDCC itself compiles flawlessly on several platforms and checks
22707 the quality of the code generated by SDCC by running the code through simulator
22709 There is a separate document
22712 \begin_inset LatexCommand \index{Test suite}
22721 You'll find the test code in the directory
22723 sdcc/support/regression
22726 You can run these tests manually by running
22730 in this directory (or f.e.
22735 \begin_inset Quotes sld
22739 \begin_inset Quotes srd
22745 if you don't want to run the complete tests).
22746 The test code might also be interesting if you want to look for examples
22747 \begin_inset LatexCommand \index{Examples}
22751 checking corner cases of SDCC or if you plan to submit patches
22752 \begin_inset LatexCommand \index{Patch submission}
22759 The pic port uses a different set of regression tests, you'll find them
22762 sdcc/src/regression
22767 SDCC Technical Data
22771 \begin_inset LatexCommand \index{Optimizations}
22778 SDCC performs a host of standard optimizations in addition to some MCU specific
22783 Sub-expression Elimination
22784 \begin_inset LatexCommand \index{Subexpression elimination}
22791 The compiler does local and
22817 will be translated to
22829 Some subexpressions are not as obvious as the above example, e.g.:
22839 In this case the address arithmetic a->b[i] will be computed only once;
22840 the equivalent code in C would be.
22852 The compiler will try to keep these temporary variables in registers.
22855 Dead-Code Elimination
22856 \begin_inset LatexCommand \index{Dead-code elimination}
22877 i = 1; \SpecialChar ~
22886 global = 1;\SpecialChar ~
22899 global = 3;\SpecialChar ~
22924 \begin_inset LatexCommand \index{Copy propagation}
22980 Note: the dead stores created by this copy propagation will be eliminated
22981 by dead-code elimination.
22985 \begin_inset LatexCommand \index{Loop optimization}
22990 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
22997 Two types of loop optimizations are done by SDCC
23005 of loop induction variables.
23006 In addition to the strength reduction the optimizer marks the induction
23007 variables and the register allocator tries to keep the induction variables
23008 in registers for the duration of the loop.
23009 Because of this preference of the register allocator
23010 \begin_inset LatexCommand \index{Register allocation}
23014 , loop induction optimization causes an increase in register pressure, which
23015 may cause unwanted spilling of other temporary variables into the stack
23016 \begin_inset LatexCommand \index{stack}
23021 The compiler will generate a warning message when it is forced to allocate
23022 extra space either on the stack or data space.
23023 If this extra space allocation is undesirable then induction optimization
23024 can be eliminated either for the entire source file (with -
23034 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23036 \begin_inset LatexCommand \index{\#pragma noinduction}
23049 for (i = 0 ; i < 100 ; i ++)
23065 for (i = 0; i < 100; i++)
23074 As mentioned previously some loop invariants are not as apparent, all static
23075 address computations are also moved out of the loop.
23080 \begin_inset LatexCommand \index{Strength reduction}
23084 , this optimization substitutes an expression by a cheaper expression:
23089 for (i=0;i < 100; i++)
23107 for (i=0;i< 100;i++) {
23113 ar[itemp1] = itemp2;
23130 The more expensive multiplication
23131 \begin_inset LatexCommand \index{Multiplication}
23135 is changed to a less expensive addition.
23139 \begin_inset LatexCommand \index{Loop reversing}
23146 This optimization is done to reduce the overhead of checking loop boundaries
23147 for every iteration.
23148 Some simple loops can be reversed and implemented using a
23149 \begin_inset Quotes eld
23152 decrement and jump if not zero
23153 \begin_inset Quotes erd
23157 SDCC checks for the following criterion to determine if a loop is reversible
23158 (note: more sophisticated compilers use data-dependency analysis to make
23159 this determination, SDCC uses a more simple minded analysis).
23162 The 'for' loop is of the form
23168 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23178 The <for body> does not contain
23179 \begin_inset Quotes eld
23183 \begin_inset Quotes erd
23187 \begin_inset Quotes erd
23193 All goto's are contained within the loop.
23196 No function calls within the loop.
23199 The loop control variable <sym> is not assigned any value within the loop
23202 The loop control variable does NOT participate in any arithmetic operation
23206 There are NO switch statements in the loop.
23209 Algebraic Simplifications
23212 SDCC does numerous algebraic simplifications, the following is a small sub-set
23213 of these optimizations.
23218 i = j + 0;\SpecialChar ~
23222 /* changed to: */\SpecialChar ~
23228 i /= 2;\SpecialChar ~
23235 /* changed to: */\SpecialChar ~
23241 i = j - j;\SpecialChar ~
23245 /* changed to: */\SpecialChar ~
23251 i = j / 1;\SpecialChar ~
23255 /* changed to: */\SpecialChar ~
23262 Note the subexpressions
23263 \begin_inset LatexCommand \index{Subexpression}
23267 given above are generally introduced by macro expansions or as a result
23268 of copy/constant propagation.
23271 'switch' Statements
23272 \begin_inset LatexCommand \label{sub:'switch'-Statements}
23277 \begin_inset LatexCommand \index{switch statement}
23284 SDCC can optimize switch statements to jump tables
23285 \begin_inset LatexCommand \index{jump tables}
23290 It makes the decision based on an estimate of the generated code size.
23291 SDCC is quite liberal in the requirements for jump table generation:
23294 The labels need not be in order, and the starting number need not be one
23295 or zero, the case labels are in numerical sequence or not too many case
23296 labels are missing.
23302 switch(i) {\SpecialChar ~
23333 case 4: ...\SpecialChar ~
23365 case 5: ...\SpecialChar ~
23397 case 3: ...\SpecialChar ~
23428 case 6: ...\SpecialChar ~
23460 case 7: ...\SpecialChar ~
23492 case 8: ...\SpecialChar ~
23524 case 9: ...\SpecialChar ~
23556 case 10: ...\SpecialChar ~
23587 case 11: ...\SpecialChar ~
23654 Both the above switch statements will be implemented using a jump-table.
23655 The example to the right side is slightly more efficient as the check for
23656 the lower boundary of the jump-table is not needed.
23660 The number of case labels is not larger than supported by the target architectur
23664 If the case labels are not in numerical sequence ('gaps' between cases)
23665 SDCC checks whether a jump table with additionally inserted dummy cases
23666 is still attractive.
23670 If the starting number is not zero and a check for the lower boundary of
23671 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
23676 Switch statements which have large gaps in the numeric sequence or those
23677 that have too many case labels can be split into more than one switch statement
23678 for efficient code generation, e.g.:
23758 If the above switch statement is broken down into two switch statements
23848 then both the switch statements will be implemented using jump-tables whereas
23849 the unmodified switch statement will not be.
23852 There might be reasons which SDCC cannot know about to either favour or
23853 not favour jump tables.
23854 If the target system has to be as quick for the last switch case as for
23855 the first (pro jump table), or if the switch argument is known to be zero
23856 in the majority of the cases (contra jump table).
23859 The pragma nojtbound
23860 \begin_inset LatexCommand \index{\#pragma nojtbound}
23864 can be used to turn off checking the
23877 It has no effect if a default label is supplied.
23878 Use of this pragma is dangerous: if the switch
23879 \begin_inset LatexCommand \index{switch statement}
23883 argument is not matched by a case statement the processor will happily
23887 Bit-shifting Operations
23888 \begin_inset LatexCommand \index{Bit shifting}
23895 Bit shifting is one of the most frequently used operation in embedded programmin
23897 SDCC tries to implement bit-shift operations in the most efficient way
23913 generates the following code:
23930 In general SDCC will never setup a loop if the shift count is known.
23973 \begin_inset LatexCommand \index{Bit rotation}
23980 A special case of the bit-shift operation is bit rotation
23981 \begin_inset LatexCommand \index{rotating bits}
23985 , SDCC recognizes the following expression to be a left bit-rotation:
23995 char i;\SpecialChar ~
24006 /* unsigned is needed for rotation */
24011 i = ((i << 1) | (i >> 7));
24020 will generate the following code:
24039 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24040 ns of this case will also be recognized as bit-rotation, i.e.:
24045 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24048 Nibble and Byte Swapping
24051 Other special cases of the bit-shift operations are nibble or byte swapping
24052 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24056 , SDCC recognizes the following expressions:
24079 i = ((i << 4) | (i >> 4));
24085 j = ((j << 8) | (j >> 8));
24088 and generates a swap instruction for the nibble swapping
24089 \begin_inset LatexCommand \index{Nibble swapping}
24093 or move instructions for the byte swapping
24094 \begin_inset LatexCommand \index{Byte swapping}
24100 \begin_inset Quotes sld
24104 \begin_inset Quotes srd
24107 example can be used to convert from little to big-endian or vice versa.
24108 If you want to change the endianness of a
24112 integer you have to cast to
24119 Note that SDCC stores numbers in little-endian
24125 Usually 8-bit processors don't care much about endianness.
24126 This is not the case for the standard 8051 which only has an instruction
24132 \begin_inset LatexCommand \index{DPTR}
24140 so little-endian is the more efficient byte order.
24144 \begin_inset LatexCommand \index{little-endian}
24149 \begin_inset LatexCommand \index{Endianness}
24154 lowest order first).
24158 \begin_inset LatexCommand \index{Highest Order Bit}
24165 It is frequently required to obtain the highest order bit of an integral
24166 type (long, int, short or char types).
24167 SDCC recognizes the following expression to yield the highest order bit
24168 and generates optimized code for it, e.g.:
24190 hob = (gint >> 15) & 1;
24200 will generate the following code:
24233 000A E5*01\SpecialChar ~
24260 000C 23\SpecialChar ~
24291 000D 54 01\SpecialChar ~
24318 000F F5*02\SpecialChar ~
24346 Variations of this case however will
24351 It is a standard C expression, so I heartily recommend this be the only
24352 way to get the highest order bit, (it is portable).
24353 Of course it will be recognized even if it is embedded in other expressions,
24359 xyz = gint + ((gint >> 15) & 1);
24362 will still be recognized.
24366 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
24371 \begin_inset LatexCommand \index{Peephole optimizer}
24378 The compiler uses a rule based, pattern matching and re-writing mechanism
24379 for peep-hole optimization.
24384 a peep-hole optimizer by Christopher W.
24385 Fraser (cwfraser\SpecialChar ~
24388 A default set of rules are compiled into the compiler, additional rules
24389 may be added with the
24402 \begin_inset LatexCommand \index{-\/-peep-file}
24409 The rule language is best illustrated with examples.
24433 The above rule will change the following assembly
24434 \begin_inset LatexCommand \index{Assembler routines}
24456 Note: All occurrences of a
24460 (pattern variable) must denote the same string.
24461 With the above rule, the assembly sequence:
24471 will remain unmodified.
24475 Other special case optimizations may be added by the user (via
24491 some variants of the 8051 MCU
24492 \begin_inset LatexCommand \index{MCS51 variants}
24505 The following two rules will change all
24524 replace { lcall %1 } by { acall %1 }
24526 replace { ljmp %1 } by { ajmp %1 }
24531 inline-assembler code
24533 is also passed through the peep hole optimizer, thus the peephole optimizer
24534 can also be used as an assembly level macro expander.
24535 The rules themselves are MCU dependent whereas the rule language infra-structur
24536 e is MCU independent.
24537 Peephole optimization rules for other MCU can be easily programmed using
24542 The syntax for a rule is as follows:
24547 rule := replace [ restart ] '{' <assembly sequence> '
24585 <assembly sequence> '
24603 '}' [if <functionName> ] '
24608 <assembly sequence> := assembly instruction (each instruction including
24609 labels must be on a separate line).
24613 The optimizer will apply to the rules one by one from the top in the sequence
24614 of their appearance, it will terminate when all rules are exhausted.
24615 If the 'restart' option is specified, then the optimizer will start matching
24616 the rules again from the top, this option for a rule is expensive (performance)
24617 , it is intended to be used in situations where a transformation will trigger
24618 the same rule again.
24619 An example of this (not a good one, it has side effects) is the following
24642 Note that the replace pattern cannot be a blank, but can be a comment line.
24643 Without the 'restart' option only the innermost 'pop' 'push' pair would
24644 be eliminated, i.e.:
24674 the restart option the rule will be applied again to the resulting code
24675 and then all the pop-push pairs will be eliminated to yield:
24685 A conditional function can be attached to a rule.
24686 Attaching rules are somewhat more involved, let me illustrate this with
24713 The optimizer does a look-up of a function name table defined in function
24718 in the source file SDCCpeeph.c, with the name
24723 If it finds a corresponding entry the function is called.
24724 Note there can be no parameters specified for these functions, in this
24729 is crucial, since the function
24733 expects to find the label in that particular variable (the hash table containin
24734 g the variable bindings is passed as a parameter).
24735 If you want to code more such functions, take a close look at the function
24736 labelInRange and the calling mechanism in source file SDCCpeeph.c.
24737 Currently implemented are
24739 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
24740 24bitMode, portIsDS390, 24bitModeAndPortDS390
24749 I know this whole thing is a little kludgey, but maybe some day we will
24750 have some better means.
24751 If you are looking at this file, you will see the default rules that are
24752 compiled into the compiler, you can add your own rules in the default set
24753 there if you get tired of specifying the -
24767 \begin_inset LatexCommand \index{ANSI-compliance}
24772 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
24779 Deviations from the compliance:
24782 functions are not reentrant
24783 \begin_inset LatexCommand \index{reentrant}
24787 unless explicitly declared as such or the
24800 \begin_inset LatexCommand \index{-\/-stack-auto}
24806 command line option is specified.
24809 structures and unions cannot be assigned values directly, cannot be passed
24810 as function parameters or assigned to each other and cannot be a return
24811 value from a function, e.g.:
24837 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
24848 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
24870 return rets;/* is invalid in SDCC although allowed in ANSI */
24877 \begin_inset LatexCommand \index{long long (not supported)}
24882 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
24890 \begin_inset LatexCommand \index{double (not supported)}
24894 ' precision floating point
24895 \begin_inset LatexCommand \index{Floating point support}
24902 No support for setjmp
24903 \begin_inset LatexCommand \index{setjmp (not supported)}
24908 \begin_inset LatexCommand \index{longjmp (not supported)}
24916 \begin_inset LatexCommand \index{K\&R style}
24920 function declarations are NOT allowed.
24926 foo(i,j) /* this old style of function declarations */
24928 int i,j; /* are valid in ANSI but not valid in SDCC */
24943 Certain words that are valid identifiers in the standard may be reserved
24944 words in SDCC unless the
24957 \begin_inset LatexCommand \index{-\/-std-c89}
24972 \begin_inset LatexCommand \index{-\/-std-c99}
24978 command line options are used.
24979 These may include (depending on the selected processor): 'at', 'banked',
24980 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
24981 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
24982 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
24984 Compliant equivalents of these keywords are always available in a form
24985 that begin with two underscores
24986 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
24991 '__data' instead of 'data'.
24994 Cyclomatic Complexity
24995 \begin_inset LatexCommand \index{Cyclomatic complexity}
25002 Cyclomatic complexity of a function is defined as the number of independent
25003 paths the program can take during execution of the function.
25004 This is an important number since it defines the number test cases you
25005 have to generate to validate the function.
25006 The accepted industry standard for complexity number is 10, if the cyclomatic
25007 complexity reported by SDCC exceeds 10 you should think about simplification
25008 of the function logic.
25009 Note that the complexity level is not related to the number of lines of
25010 code in a function.
25011 Large functions can have low complexity, and small functions can have large
25017 SDCC uses the following formula to compute the complexity:
25022 complexity = (number of edges in control flow graph) - (number of nodes
25023 in control flow graph) + 2;
25027 Having said that the industry standard is 10, you should be aware that in
25028 some cases it be may unavoidable to have a complexity level of less than
25030 For example if you have switch statement with more than 10 case labels,
25031 each case label adds one to the complexity level.
25032 The complexity level is by no means an absolute measure of the algorithmic
25033 complexity of the function, it does however provide a good starting point
25034 for which functions you might look at for further optimization.
25037 Retargetting for other Processors
25040 The issues for retargetting the compiler are far too numerous to be covered
25042 What follows is a brief description of each of the seven phases of the
25043 compiler and its MCU dependency.
25046 Parsing the source and building the annotated parse tree.
25047 This phase is largely MCU independent (except for the language extensions).
25048 Syntax & semantic checks are also done in this phase, along with some initial
25049 optimizations like back patching labels and the pattern matching optimizations
25050 like bit-rotation etc.
25053 The second phase involves generating an intermediate code which can be easy
25054 manipulated during the later phases.
25055 This phase is entirely MCU independent.
25056 The intermediate code generation assumes the target machine has unlimited
25057 number of registers, and designates them with the name iTemp.
25058 The compiler can be made to dump a human readable form of the code generated
25072 This phase does the bulk of the standard optimizations and is also MCU independe
25074 This phase can be broken down into several sub-phases:
25078 Break down intermediate code (iCode) into basic blocks.
25080 Do control flow & data flow analysis on the basic blocks.
25082 Do local common subexpression elimination, then global subexpression elimination
25084 Dead code elimination
25088 If loop optimizations caused any changes then do 'global subexpression eliminati
25089 on' and 'dead code elimination' again.
25092 This phase determines the live-ranges; by live range I mean those iTemp
25093 variables defined by the compiler that still survive after all the optimization
25095 Live range analysis
25096 \begin_inset LatexCommand \index{Live range analysis}
25100 is essential for register allocation, since these computation determines
25101 which of these iTemps will be assigned to registers, and for how long.
25104 Phase five is register allocation.
25105 There are two parts to this process.
25109 The first part I call 'register packing' (for lack of a better term).
25110 In this case several MCU specific expression folding is done to reduce
25115 The second part is more MCU independent and deals with allocating registers
25116 to the remaining live ranges.
25117 A lot of MCU specific code does creep into this phase because of the limited
25118 number of index registers available in the 8051.
25121 The Code generation phase is (unhappily), entirely MCU dependent and very
25122 little (if any at all) of this code can be reused for other MCU.
25123 However the scheme for allocating a homogenized assembler operand for each
25124 iCode operand may be reused.
25127 As mentioned in the optimization section the peep-hole optimizer is rule
25128 based system, which can reprogrammed for other MCUs.
25132 \begin_inset LatexCommand \index{Compiler internals}
25139 The anatomy of the compiler
25140 \begin_inset LatexCommand \label{sub:The-anatomy-of}
25149 This is an excerpt from an article published in Circuit Cellar Magazine
25155 It's a little outdated (the compiler is much more efficient now and user/develo
25156 per friendly), but pretty well exposes the guts of it all.
25162 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
25163 It is fairly easy to retarget for other 8-bit MCU.
25164 Here we take a look at some of the internals of the compiler.
25169 \begin_inset LatexCommand \index{Parsing}
25176 Parsing the input source file and creating an AST (Annotated Syntax Tree
25177 \begin_inset LatexCommand \index{Annotated syntax tree}
25182 This phase also involves propagating types (annotating each node of the
25183 parse tree with type information) and semantic analysis.
25184 There are some MCU specific parsing rules.
25185 For example the storage classes, the extended storage classes are MCU specific
25186 while there may be a xdata storage class for 8051 there is no such storage
25187 class for z80 or Atmel AVR.
25188 SDCC allows MCU specific storage class extensions, i.e.
25189 xdata will be treated as a storage class specifier when parsing 8051 C
25190 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
25195 \begin_inset LatexCommand \index{iCode}
25202 Intermediate code generation.
25203 In this phase the AST is broken down into three-operand form (iCode).
25204 These three operand forms are represented as doubly linked lists.
25205 ICode is the term given to the intermediate form generated by the compiler.
25206 ICode example section shows some examples of iCode generated for some simple
25207 C source functions.
25211 \begin_inset LatexCommand \index{Optimizations}
25218 Bulk of the target independent optimizations is performed in this phase.
25219 The optimizations include constant propagation, common sub-expression eliminati
25220 on, loop invariant code movement, strength reduction of loop induction variables
25221 and dead-code elimination.
25224 Live range analysis
25225 \begin_inset LatexCommand \index{Live range analysis}
25232 During intermediate code generation phase, the compiler assumes the target
25233 machine has infinite number of registers and generates a lot of temporary
25235 The live range computation determines the lifetime of each of these compiler-ge
25236 nerated temporaries.
25237 A picture speaks a thousand words.
25238 ICode example sections show the live range annotations for each of the
25240 It is important to note here, each iCode is assigned a number in the order
25241 of its execution in the function.
25242 The live ranges are computed in terms of these numbers.
25243 The from number is the number of the iCode which first defines the operand
25244 and the to number signifies the iCode which uses this operand last.
25247 Register Allocation
25248 \begin_inset LatexCommand \index{Register allocation}
25255 The register allocation determines the type and number of registers needed
25257 In most MCUs only a few registers can be used for indirect addressing.
25258 In case of 8051 for example the registers R0 & R1 can be used to indirectly
25259 address the internal ram and DPTR to indirectly address the external ram.
25260 The compiler will try to allocate the appropriate register to pointer variables
25262 ICode example section shows the operands annotated with the registers assigned
25264 The compiler will try to keep operands in registers as much as possible;
25265 there are several schemes the compiler uses to do achieve this.
25266 When the compiler runs out of registers the compiler will check to see
25267 if there are any live operands which is not used or defined in the current
25268 basic block being processed, if there are any found then it will push that
25269 operand and use the registers in this block, the operand will then be popped
25270 at the end of the basic block.
25274 There are other MCU specific considerations in this phase.
25275 Some MCUs have an accumulator; very short-lived operands could be assigned
25276 to the accumulator instead of a general-purpose register.
25282 Figure II gives a table of iCode operations supported by the compiler.
25283 The code generation involves translating these operations into corresponding
25284 assembly code for the processor.
25285 This sounds overly simple but that is the essence of code generation.
25286 Some of the iCode operations are generated on a MCU specific manner for
25287 example, the z80 port does not use registers to pass parameters so the
25288 SEND and RECV iCode operations will not be generated, and it also does
25289 not support JUMPTABLES.
25296 <Where is Figure II?>
25299 In the original article Figure II was announced to be downloadable on
25304 Unfortunately it never seemed to have shown up there, so: where is Figure
25309 \begin_inset LatexCommand \index{iCode}
25316 This section shows some details of iCode.
25317 The example C code does not do anything useful; it is used as an example
25318 to illustrate the intermediate code generated by the compiler.
25330 /* This function does nothing useful.
25337 for the purpose of explaining iCode */
25340 short function (data int *x)
25348 short i=10; \SpecialChar ~
25350 /* dead initialization eliminated */
25355 short sum=10; /* dead initialization eliminated */
25368 while (*x) *x++ = *p++;
25382 /* compiler detects i,j to be induction variables */
25386 for (i = 0, j = 10 ; i < 10 ; i++, j
25412 mul += i * 3; \SpecialChar ~
25414 /* this multiplication remains */
25420 gint += j * 3;\SpecialChar ~
25422 /* this multiplication changed to addition */
25436 In addition to the operands each iCode contains information about the filename
25437 and line it corresponds to in the source file.
25438 The first field in the listing should be interpreted as follows:
25443 Filename(linenumber: iCode Execution sequence number : ICode hash table
25444 key : loop depth of the iCode).
25449 Then follows the human readable form of the ICode operation.
25450 Each operand of this triplet form can be of three basic types a) compiler
25451 generated temporary b) user defined variable c) a constant value.
25452 Note that local variables and parameters are replaced by compiler generated
25455 \begin_inset LatexCommand \index{Live range analysis}
25459 are computed only for temporaries (i.e.
25460 live ranges are not computed for global variables).
25462 \begin_inset LatexCommand \index{Register allocation}
25466 are allocated for temporaries only.
25467 Operands are formatted in the following manner:
25472 Operand Name [lr live-from : live-to ] { type information } [ registers
25478 As mentioned earlier the live ranges are computed in terms of the execution
25479 sequence number of the iCodes, for example
25481 the iTemp0 is live from (i.e.
25482 first defined in iCode with execution sequence number 3, and is last used
25483 in the iCode with sequence number 5).
25484 For induction variables such as iTemp21 the live range computation extends
25485 the lifetime from the start to the end of the loop.
25487 The register allocator used the live range information to allocate registers,
25488 the same registers may be used for different temporaries if their live
25489 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
25490 iTemp17 since their live ranges do not overlap.
25491 In addition the allocator also takes into consideration the type and usage
25492 of a temporary, for example itemp6 is a pointer to near space and is used
25493 as to fetch data from (i.e.
25494 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
25495 Some short lived temporaries are allocated to special registers which have
25496 meaning to the code generator e.g.
25497 iTemp13 is allocated to a pseudo register CC which tells the back end that
25498 the temporary is used only for a conditional jump the code generation makes
25499 use of this information to optimize a compare and jump ICode.
25501 There are several loop optimizations
25502 \begin_inset LatexCommand \index{Loop optimization}
25506 performed by the compiler.
25507 It can detect induction variables iTemp21(i) and iTemp23(j).
25508 Also note the compiler does selective strength reduction
25509 \begin_inset LatexCommand \index{Strength reduction}
25514 the multiplication of an induction variable in line 18 (gint = j * 3) is
25515 changed to addition, a new temporary iTemp17 is allocated and assigned
25516 a initial value, a constant 3 is then added for each iteration of the loop.
25517 The compiler does not change the multiplication
25518 \begin_inset LatexCommand \index{Multiplication}
25522 in line 17 however since the processor does support an 8 * 8 bit multiplication.
25524 Note the dead code elimination
25525 \begin_inset LatexCommand \index{Dead-code elimination}
25529 optimization eliminated the dead assignments in line 7 & 8 to I and sum
25537 Sample.c (5:1:0:0) _entry($9) :
25542 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
25547 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
25552 Sample.c(11:4:53:0) preHeaderLbl0($11) :
25557 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
25563 Sample.c(11:6:5:1) _whilecontinue_0($1) :
25568 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
25574 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25579 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
25585 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
25591 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
25597 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
25603 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
25604 * int}[r0] + 0x2 {short}
25609 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
25614 Sample.c(11:17:21:0)_whilebreak_0($3) :
25619 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
25624 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
25629 Sample.c(15:20:54:0)preHeaderLbl1($13) :
25634 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
25639 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
25644 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
25649 Sample.c(15:24:26:1)_forcond_0($4) :
25654 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
25660 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
25665 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
25666 + ITemp21 [lr21:38]{short}[r4]
25671 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
25677 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
25678 + iTemp15 [lr29:30]{short}[r1]
25683 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
25689 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
25695 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
25701 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
25707 Sample.c(19:38:47:1) goto _forcond_0($4)
25712 Sample.c(19:39:48:0)_forbreak_0($7) :
25717 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
25718 + ITemp11 [lr19:40]{short}[r3]
25723 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
25728 Sample.c(20:42:51:0)_return($8) :
25733 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
25739 Finally the code generated for this function:
25780 ; ----------------------------------------------
25785 ; function function
25790 ; ----------------------------------------------
25800 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
25812 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
25824 ;_whilecontinue_0($1) :
25834 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
25839 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25898 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
25917 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
25964 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
26004 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
26030 ; iTemp6 [lr5:16]{_near * int}[r0] =
26035 ; iTemp6 [lr5:16]{_near * int}[r0] +
26052 ; goto _whilecontinue_0($1)
26064 ; _whilebreak_0($3) :
26074 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
26086 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
26098 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
26110 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
26129 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
26158 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
26163 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
26208 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
26213 ; iTemp21 [lr21:38]{short}[r4]
26239 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
26272 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
26277 ; iTemp15 [lr29:30]{short}[r1]
26296 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
26343 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
26390 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
26402 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
26416 cjne r5,#0xff,00104$
26428 ; goto _forcond_0($4)
26440 ; _forbreak_0($7) :
26450 ; ret iTemp24 [lr40:41]{short}
26493 A few words about basic block successors, predecessors and dominators
26496 Successors are basic blocks
26497 \begin_inset LatexCommand \index{Basic blocks}
26501 that might execute after this basic block.
26503 Predecessors are basic blocks that might execute before reaching this basic
26506 Dominators are basic blocks that WILL execute before reaching this basic
26540 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
26543 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
26546 c) domVect of [BB4] = BB1 ...
26547 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
26555 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
26565 Thanks to all the other volunteer developers who have helped with coding,
26566 testing, web-page creation, distribution sets, etc.
26567 You know who you are :-)
26574 This document was initially written by Sandeep Dutta
26577 All product names mentioned herein may be trademarks
26578 \begin_inset LatexCommand \index{Trademarks}
26582 of their respective companies.
26589 To avoid confusion, the installation and building options for SDCC itself
26590 (chapter 2) are not part of the index.
26594 \begin_inset LatexCommand \printindex{}