1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
5 \pdfoptionpdfminorversion=3
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={8032 8051 ansi c compiler CPU DS390
11 embedded GPL HC08 manual mcs51 microcontroller PIC Z80},
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by Subversion
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
136 designed for 8 bit Microprocessors.
137 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
139 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
143 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
144 Zilog Z80 based MCUs.
145 It can be retargetted for other microprocessors, support for Microchip
146 PIC, Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargettable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
209 <lyxtabular version="3" rows="8" columns="5">
211 <column alignment="center" valignment="top" leftline="true" width="0">
212 <column alignment="center" valignment="top" leftline="true" width="0">
213 <column alignment="center" valignment="top" leftline="true" width="0">
214 <column alignment="center" valignment="top" leftline="true" width="0">
215 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
216 <row topline="true" bottomline="true">
217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
401 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
426 <row topline="true" bottomline="true">
427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
456 -2.147.483.648, +2.147.483.647
459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
468 <row topline="true" bottomline="true">
469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
508 <row topline="true" bottomline="true">
509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
554 The compiler also allows
556 inline assembler code
558 to be embedded anywhere in a function.
559 In addition, routines developed in assembly can also be called.
563 SDCC also provides an option (-
573 -cyclomatic) to report the relative complexity of a function.
574 These functions can then be further optimized, or hand coded in assembly
580 SDCC also comes with a companion source level debugger SDCDB, the debugger
581 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
582 SDCDB and ucSim are currently not available on Win32 platforms.
587 The latest version can be downloaded from
588 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
598 Please note: the compiler will probably always be some steps ahead of this
603 \begin_inset LatexCommand \index{Status of documentation}
613 Obviously this has pros and cons
622 All packages used in this compiler system are
630 ; source code for all the sub-packages (pre-processor, assemblers, linkers
631 etc) is distributed with the package.
632 This documentation is maintained using a freeware word processor (LyX).
634 This program is free software; you can redistribute it and/or modify it
635 under the terms of the GNU General Public License
636 \begin_inset LatexCommand \index{GNU General Public License, GPL}
640 as published by the Free Software Foundation; either version 2, or (at
641 your option) any later version.
642 This program is distributed in the hope that it will be useful, but WITHOUT
643 ANY WARRANTY; without even the implied warranty
644 \begin_inset LatexCommand \index{warranty}
648 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
649 See the GNU General Public License for more details.
650 You should have received a copy of the GNU General Public License along
651 with this program; if not, write to the Free Software Foundation, 59 Temple
652 Place - Suite 330, Boston, MA 02111-1307, USA.
653 In other words, you are welcome to use, share and improve this program.
654 You are forbidden to forbid anyone else to use, share and improve what
656 Help stamp out software-hoarding!
659 Typographic conventions
660 \begin_inset LatexCommand \index{Typographic conventions}
667 Throughout this manual, we will use the following convention.
668 Commands you have to type in are printed in
676 Code samples are printed in
681 Interesting items and new terms are printed in
686 Compatibility with previous versions
687 \begin_inset LatexCommand \index{Compatibility with previous versions}
694 This version has numerous bug fixes compared with the previous version.
695 But we also introduced some incompatibilities with older versions.
696 Not just for the fun of it, but to make the compiler more stable, efficient
698 \begin_inset LatexCommand \index{ANSI-compliance}
703 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
707 for ANSI-Compliance).
713 short is now equivalent to int (16 bits), it used to be equivalent to char
714 (8 bits) which is not ANSI compliant.
717 the default directory for gcc-builds where include, library and documentation
718 files are stored is now in /usr/local/share.
721 char type parameters to vararg functions are casted to int unless explicitly
738 will push a as an int and as a char resp.
751 -regextend has been removed.
764 -noregparms has been removed.
777 -stack-after-data has been removed.
781 \begin_inset LatexCommand \index{bit}
786 \begin_inset LatexCommand \index{sbit}
791 \begin_inset LatexCommand \index{\_\_sbit}
795 types now consistently behave like the C99 _Bool type with respect to type
797 \begin_inset LatexCommand \index{type conversion}
802 \begin_inset LatexCommand \index{type promotion}
807 The most common incompatibility resulting from this change is related to
809 \begin_inset LatexCommand \index{Bit toggling}
823 b = ~b; /* equivalent to b=1 instead of toggling b */
827 b = !b; /* toggles b */
831 In previous versions, both forms would have toggled the bit.
836 <pending: more incompatibilities?>
842 What do you need before you start installation of SDCC? A computer, and
844 The preferred method of installation is to compile SDCC from source using
846 For Windows some pre-compiled binary distributions are available for your
848 You should have some experience with command line tools and compiler use.
854 The SDCC home page at
855 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
859 is a great place to find distribution sets.
860 You can also find links to the user mailing lists that offer help or discuss
861 SDCC with other SDCC users.
862 Web links to other SDCC related sites can also be found here.
863 This document can be found in the DOC directory of the source package as
865 A pdf version of this document is available at
866 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
871 Some of the other tools (simulator and assembler) included with SDCC contain
872 their own documentation and can be found in the source distribution.
873 If you want the latest unreleased software, the complete source package
874 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
878 Wishes for the future
881 There are (and always will be) some things that could be done.
882 Here are some I can think of:
889 char KernelFunction3(char p) at 0x340;
897 \begin_inset LatexCommand \index{code banking (limited support)}
907 If you can think of some more, please see the section
908 \begin_inset LatexCommand \ref{sub:Requesting-Features}
912 about filing feature requests
913 \begin_inset LatexCommand \index{Requesting features}
918 \begin_inset LatexCommand \index{Feature request}
928 \begin_inset LatexCommand \index{Installation}
935 For most users it is sufficient to skip to either section
936 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
941 \begin_inset LatexCommand \ref{sub:Windows-Install}
946 More detailled instructions follow below.
950 \begin_inset LatexCommand \index{Options SDCC configuration}
957 The install paths, search paths and other options are defined when running
959 The defaults can be overridden by:
961 \labelwidthstring 00.00.0000
973 -prefix see table below
975 \labelwidthstring 00.00.0000
987 -exec_prefix see table below
989 \labelwidthstring 00.00.0000
1001 -bindir see table below
1003 \labelwidthstring 00.00.0000
1015 -datadir see table below
1017 \labelwidthstring 00.00.0000
1019 docdir environment variable, see table below
1021 \labelwidthstring 00.00.0000
1023 include_dir_suffix environment variable, see table below
1025 \labelwidthstring 00.00.0000
1027 lib_dir_suffix environment variable, see table below
1029 \labelwidthstring 00.00.0000
1031 sdccconf_h_dir_separator environment variable, either / or
1036 This character will only be used in sdccconf.h; don't forget it's a C-header,
1037 therefore a double-backslash is needed there.
1039 \labelwidthstring 00.00.0000
1051 -disable-mcs51-port Excludes the Intel mcs51 port
1053 \labelwidthstring 00.00.0000
1065 -disable-gbz80-port Excludes the Gameboy gbz80 port
1067 \labelwidthstring 00.00.0000
1079 -disable-z80-port Excludes the z80 port
1081 \labelwidthstring 00.00.0000
1093 -disable-avr-port Excludes the AVR port
1095 \labelwidthstring 00.00.0000
1107 -disable-ds390-port Excludes the DS390 port
1109 \labelwidthstring 00.00.0000
1121 -disable-hc08-port Excludes the HC08 port
1123 \labelwidthstring 00.00.0000
1135 -disable-pic-port Excludes the PIC port
1137 \labelwidthstring 00.00.0000
1149 -disable-xa51-port Excludes the XA51 port
1151 \labelwidthstring 00.00.0000
1163 -disable-ucsim Disables configuring and building of ucsim
1165 \labelwidthstring 00.00.0000
1177 -disable-device-lib Disables automatically building device libraries
1179 \labelwidthstring 00.00.0000
1191 -disable-packihx Disables building packihx
1193 \labelwidthstring 00.00.0000
1205 -enable-doc Build pdf, html and txt files from the lyx sources
1207 \labelwidthstring 00.00.0000
1219 -enable-libgc Use the Bohem memory allocator.
1220 Lower runtime footprint.
1223 Furthermore the environment variables CC, CFLAGS, ...
1224 the tools and their arguments can be influenced.
1225 Please see `configure -
1235 -help` and the man/info pages of `configure` for details.
1239 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
1240 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
1241 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
1242 At the moment it's not possible to change the default settings (it was
1243 simply never required).
1247 These configure options are compiled into the binaries, and can only be
1248 changed by rerunning 'configure' and recompiling SDCC.
1249 The configure options are written in
1253 to distinguish them from run time environment variables (see section search
1259 \begin_inset Quotes sld
1263 \begin_inset Quotes srd
1266 are used by the SDCC team to build the official Win32 binaries.
1267 The SDCC team uses Mingw32 to build the official Windows binaries, because
1274 a gcc compiler and last but not least
1277 the binaries can be built by cross compiling on Sourceforge's compile farm.
1280 See the examples, how to pass the Win32 settings to 'configure'.
1281 The other Win32 builds using Borland, VC or whatever don't use 'configure',
1282 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
1293 \begin_inset Tabular
1294 <lyxtabular version="3" rows="8" columns="3">
1296 <column alignment="block" valignment="top" leftline="true" width="0in">
1297 <column alignment="block" valignment="top" leftline="true" width="0in">
1298 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1299 <row topline="true" bottomline="true">
1300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1325 <row topline="true">
1326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1344 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1355 <row topline="true">
1356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1366 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1376 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1387 <row topline="true">
1388 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1398 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1410 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1425 <row topline="true">
1426 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1436 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1448 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1459 <row topline="true">
1460 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1470 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1482 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1497 <row topline="true">
1498 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1516 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1525 <row topline="true" bottomline="true">
1526 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1536 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1544 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1562 'configure' also computes relative paths.
1563 This is needed for full relocatability of a binary package and to complete
1564 search paths (see section search paths below):
1570 \begin_inset Tabular
1571 <lyxtabular version="3" rows="4" columns="3">
1573 <column alignment="block" valignment="top" leftline="true" width="0in">
1574 <column alignment="block" valignment="top" leftline="true" width="0in">
1575 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1576 <row topline="true" bottomline="true">
1577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1585 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1593 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1602 <row topline="true" bottomline="true">
1603 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1630 <row bottomline="true">
1631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1641 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1649 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1658 <row bottomline="true">
1659 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1669 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1677 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1710 \begin_inset Quotes srd
1714 \begin_inset Quotes srd
1728 \begin_inset Quotes srd
1732 \begin_inset Quotes srd
1760 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1769 \begin_inset Quotes srd
1772 i586-mingw32msvc-gcc
1773 \begin_inset Quotes srd
1777 \begin_inset Quotes srd
1780 i586-mingw32msvc-g++
1781 \begin_inset Quotes srd
1789 \begin_inset Quotes srd
1792 i586-mingw32msvc-ranlib
1793 \begin_inset Quotes srd
1801 \begin_inset Quotes srd
1804 i586-mingw32msvc-strip
1805 \begin_inset Quotes srd
1823 \begin_inset Quotes srd
1827 \begin_inset Quotes srd
1845 \begin_inset Quotes srd
1849 \begin_inset Quotes srd
1857 \begin_inset Quotes srd
1861 \begin_inset Quotes srd
1869 \begin_inset Quotes srd
1873 \begin_inset Quotes srd
1881 \begin_inset Quotes srd
1885 \begin_inset Quotes srd
1892 sdccconf_h_dir_separator=
1893 \begin_inset Quotes srd
1905 \begin_inset Quotes srd
1950 -host=i586-mingw32msvc -
1960 -build=unknown-unknown-linux-gnu
1964 \begin_inset Quotes sld
1968 \begin_inset Quotes srd
1971 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1980 \begin_inset Quotes srd
1984 \begin_inset Quotes srd
1992 \begin_inset Quotes srd
1996 \begin_inset Quotes srd
2014 \begin_inset Quotes srd
2018 \begin_inset Quotes srd
2036 \begin_inset Quotes srd
2040 \begin_inset Quotes srd
2048 \begin_inset Quotes srd
2052 \begin_inset Quotes srd
2060 \begin_inset Quotes srd
2064 \begin_inset Quotes srd
2072 \begin_inset Quotes srd
2076 \begin_inset Quotes srd
2083 sdccconf_h_dir_separator=
2084 \begin_inset Quotes srd
2096 \begin_inset Quotes srd
2116 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2127 -C' turns on caching, which gives a little bit extra speed.
2128 However if options are changed, it can be necessary to delete the config.cache
2133 \begin_inset LatexCommand \label{sub:Install-paths}
2138 \begin_inset LatexCommand \index{Install paths}
2144 \added_space_top medskip \align center
2146 \begin_inset Tabular
2147 <lyxtabular version="3" rows="5" columns="4">
2149 <column alignment="center" valignment="top" leftline="true" width="0">
2150 <column alignment="center" valignment="top" leftline="true" width="0">
2151 <column alignment="center" valignment="top" leftline="true" width="0">
2152 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
2153 <row topline="true" bottomline="true">
2154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2195 <row topline="true">
2196 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2214 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2222 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2235 <row topline="true">
2236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2251 $DATADIR/ $INCLUDE_DIR_SUFFIX
2254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2259 /usr/local/share/sdcc/include
2262 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2275 <row topline="true">
2276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2291 $DATADIR/$LIB_DIR_SUFFIX
2294 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2299 /usr/local/share/sdcc/lib
2302 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2315 <row topline="true" bottomline="true">
2316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2339 /usr/local/share/sdcc/doc
2342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2364 *compiler, preprocessor, assembler, and linker
2370 is auto-appended by the compiler, e.g.
2371 small, large, z80, ds390 etc
2374 The install paths can still be changed during `make install` with e.g.:
2377 make install prefix=$(HOME)/local/sdcc
2380 Of course this doesn't change the search paths compiled into the binaries.
2384 Moreover the install path can be changed by defining DESTDIR
2385 \begin_inset LatexCommand \index{DESTDIR}
2392 make install DESTDIR=$(HOME)/sdcc.rpm/
2395 Please note that DESTDIR must have a trailing slash!
2399 \begin_inset LatexCommand \label{sub:Search-Paths}
2404 \begin_inset LatexCommand \index{Search path}
2411 Some search paths or parts of them are determined by configure variables
2416 , see section above).
2417 Further search paths are determined by environment variables during runtime.
2420 The paths searched when running the compiler are as follows (the first catch
2426 Binary files (preprocessor, assembler and linker)
2432 \begin_inset Tabular
2433 <lyxtabular version="3" rows="4" columns="3">
2435 <column alignment="block" valignment="top" leftline="true" width="0in">
2436 <column alignment="block" valignment="top" leftline="true" width="0in">
2437 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2438 <row topline="true" bottomline="true">
2439 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2447 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2455 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2464 <row topline="true">
2465 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2494 <row topline="true">
2495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2500 Path of argv[0] (if available)
2503 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2511 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2520 <row topline="true" bottomline="true">
2521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2529 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2562 \begin_inset Tabular
2563 <lyxtabular version="3" rows="6" columns="3">
2565 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2566 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2567 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2568 <row topline="true" bottomline="true">
2569 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2594 <row topline="true">
2595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2631 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2650 <row topline="true">
2651 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2659 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2676 <row topline="true">
2677 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2691 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2703 <cell alignment="center" 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">
2733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2796 <row topline="true" bottomline="true">
2797 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2813 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2818 /usr/local/share/sdcc/
2823 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2851 -nostdinc disables the last two search paths.
2861 With the exception of
2862 \begin_inset Quotes sld
2876 \begin_inset Quotes srd
2883 is auto-appended by the compiler (e.g.
2884 small, large, z80, ds390 etc.).
2891 \begin_inset Tabular
2892 <lyxtabular version="3" rows="6" columns="3">
2894 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2895 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2896 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2897 <row topline="true" bottomline="true">
2898 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2906 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2923 <row topline="true">
2924 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2979 <row topline="true">
2980 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2992 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3019 <row topline="true">
3020 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3031 $LIB_DIR_SUFFIX/<model>
3034 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3048 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3065 <row topline="true">
3066 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3081 $LIB_DIR_SUFFIX/<model>
3084 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3137 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3193 <row topline="true" bottomline="true">
3194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3203 $LIB_DIR_SUFFIX/<model>
3206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3211 /usr/local/share/sdcc/
3218 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3236 Don't delete any of the stray spaces in the table above without checking
3237 the HTML output (last line)!
3253 -nostdlib disables the last two search paths.
3257 \begin_inset LatexCommand \index{Building SDCC}
3264 Building SDCC on Linux
3265 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3274 Download the source package
3276 either from the SDCC Subversion repository or from the nightly snapshots
3278 , it will be named something like sdcc
3289 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3298 Bring up a command line terminal, such as xterm.
3303 Unpack the file using a command like:
3306 "tar -xvzf sdcc.src.tar.gz
3311 , this will create a sub-directory called sdcc with all of the sources.
3314 Change directory into the main SDCC directory, for example type:
3331 This configures the package for compilation on your system.
3347 All of the source packages will compile, this can take a while.
3363 This copies the binary executables, the include files, the libraries and
3364 the documentation to the install directories.
3365 Proceed with section
3366 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3373 Building SDCC on OSX 2.x
3376 Follow the instruction for Linux.
3380 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3381 )) fails to compile SDCC.
3382 Fortunately there's also gcc 2.9.x installed, which works fine.
3383 This compiler can be selected by running 'configure' with:
3386 ./configure CC=gcc2 CXX=g++2
3389 Cross compiling SDCC on Linux for Windows
3392 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3393 See section 'Configure Options'.
3396 Building SDCC on Windows
3399 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3401 They use Unix-sockets, which are not available on Win32.
3404 Building SDCC using Cygwin and Mingw32
3407 For building and installing a Cygwin executable follow the instructions
3413 \begin_inset Quotes sld
3417 \begin_inset Quotes srd
3420 Win32-binary can be built, which will not need the Cygwin-DLL.
3421 For the necessary 'configure' options see section 'configure options' or
3422 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3426 In order to install Cygwin on Windows download setup.exe from
3427 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3433 \begin_inset Quotes sld
3436 default text file type
3437 \begin_inset Quotes srd
3441 \begin_inset Quotes sld
3445 \begin_inset Quotes srd
3448 and download/install at least the following packages.
3449 Some packages are selected by default, others will be automatically selected
3450 because of dependencies with the manually selected packages.
3451 Never deselect these packages!
3460 gcc ; version 3.x is fine, no need to use the old 2.9x
3463 binutils ; selected with gcc
3469 rxvt ; a nice console, which makes life much easier under windoze (see below)
3472 man ; not really needed for building SDCC, but you'll miss it sooner or
3476 less ; not really needed for building SDCC, but you'll miss it sooner or
3480 svn ; only if you use Subversion access
3483 If you want to develop something you'll need:
3486 python ; for the regression tests
3489 gdb ; the gnu debugger, together with the nice GUI
3490 \begin_inset Quotes sld
3494 \begin_inset Quotes srd
3500 openssh ; to access the CF or commit changes
3503 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3504 use autoconf-stable!
3507 rxvt is a nice console with history.
3508 Replace in your cygwin.bat the line
3527 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3530 -bg black -fg white -geometry 100x65 -e bash -
3543 Text selected with the mouse is automatically copied to the clipboard, pasting
3544 works with shift-insert.
3548 The other good tip is to make sure you have no //c/-style paths anywhere,
3549 use /cygdrive/c/ instead.
3550 Using // invokes a network lookup which is very slow.
3552 \begin_inset Quotes sld
3556 \begin_inset Quotes srd
3559 is too long, you can change it with e.g.
3565 SDCC sources use the unix line ending LF.
3566 Life is much easier, if you store the source tree on a drive which is mounted
3568 And use an editor which can handle LF-only line endings.
3569 Make sure not to commit files with windows line endings.
3570 The tabulator spacing
3571 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3575 used in the project is 8.
3576 Although a tabulator spacing of 8 is a sensible choice for programmers
3577 (it's a power of 2 and allows to display 8/16 bit signed variables without
3578 loosing columns) the plan is to move towards using only spaces in the source.
3581 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3586 Download the source package
3588 either from the SDCC Subversion repository or from the
3589 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3595 , it will be named something like sdcc
3602 SDCC is distributed with all the projects, workspaces, and files you need
3603 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3604 doesn't build under MSVC).
3605 The workspace name is 'sdcc.dsw'.
3606 Please note that as it is now, all the executables are created in a folder
3610 Once built you need to copy the executables from sdcc
3614 bin before running SDCC.
3619 WARNING: Visual studio is very picky with line terminations; it expects
3620 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3621 When using the Subversion repository it's easiest to configure the svn
3622 client to convert automatically for you.
3623 If however you are getting a message such as "This makefile was not generated
3624 by Developer Studio etc.
3626 \begin_inset Quotes srd
3629 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3630 need to convert the Unix style line endings to DOS style line endings.
3631 To do so you can use the
3632 \begin_inset Quotes sld
3636 \begin_inset Quotes srd
3639 utility freely available on the internet.
3640 Doug Hawkins reported in the sdcc-user list that this works:
3648 SDCC> unix2dos sdcc.dsw
3654 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3658 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3659 flex.exe, and gawk.exe.
3660 One good place to get them is
3661 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3669 Download the file UnxUtils
3670 \begin_inset LatexCommand \index{UnxUtils}
3675 Now you have to install the utilities and setup MSVC so it can locate the
3677 Here there are two alternatives (choose one!):
3684 a) Extract UnxUtils.zip to your C:
3686 hard disk PRESERVING the original paths, otherwise bison won't work.
3687 (If you are using WinZip make certain that 'Use folder names' is selected)
3691 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3692 in 'Show directories for:' select 'Executable files', and in the directories
3693 window add a new path: 'C:
3703 (As a side effect, you get a bunch of Unix utilities that could be useful,
3704 such as diff and patch.)
3711 This one avoids extracting a bunch of files you may not use, but requires
3716 a) Create a directory were to put the tools needed, or use a directory already
3724 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3725 to such directory WITHOUT preserving the original paths.
3726 (If you are using WinZip make certain that 'Use folder names' is not selected)
3730 c) Rename bison.exe to '_bison.exe'.
3734 d) Create a batch file 'bison.bat' in 'C:
3738 ' and add these lines:
3758 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3762 Steps 'c' and 'd' are needed because bison requires by default that the
3763 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3764 '/usr/local/share/' I think.
3765 So it is necessary to tell bison where those files are located if they
3766 are not in such directory.
3767 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3771 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3772 in 'Show directories for:' select 'Executable files', and in the directories
3773 window add a new path: 'c:
3776 Note that you can use any other path instead of 'c:
3778 util', even the path where the Visual C++ tools are, probably: 'C:
3782 Microsoft Visual Studio
3787 So you don't have to execute step 'e' :)
3791 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3792 the executables from sdcc
3796 bin, and you can compile using SDCC.
3799 Building SDCC Using Borland
3802 From the sdcc directory, run the command "make -f Makefile.bcc".
3803 This should regenerate all the .exe files in the bin directory except for
3804 sdcdb.exe (which currently doesn't build under Borland C++).
3807 If you modify any source files and need to rebuild, be aware that the dependenci
3808 es may not be correctly calculated.
3809 The safest option is to delete all .obj files and run the build again.
3810 From a Cygwin BASH prompt, this can easily be done with the command (be
3811 sure you are in the sdcc directory):
3821 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3823 ) -print -exec rm {}
3832 or on Windows NT/2000/XP from the command prompt with the command:
3839 del /s *.obj *.lib *.rul
3842 from the sdcc directory.
3845 Windows Install Using a ZIP Package
3848 Download the binary zip package from
3849 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3853 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3854 This should unpack to a group of sub-directories.
3855 An example directory structure after unpacking the mingw32 package is:
3860 bin for the executables, c:
3868 lib for the include and libraries.
3871 Adjust your environment variable PATH to include the location of the bin
3872 directory or start sdcc using the full path.
3875 Windows Install Using the Setup Program
3876 \begin_inset LatexCommand \label{sub:Windows-Install}
3883 Download the setup program
3885 sdcc-x.y.z-setup.exe
3887 for an official release from
3890 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3894 or a setup program for one of the snapshots
3896 sdcc_yyyymmdd_setup.exe
3899 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3904 A windows typical installer will guide you through the installation process.
3908 \begin_inset LatexCommand \index{VPATH}
3915 SDCC supports the VPATH feature provided by configure and make.
3916 It allows to separate the source and build trees.
3948 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3949 # extract source to directory sdcc
3954 mkdir sdcc.build\SpecialChar ~
3963 # put output in sdcc.build
3973 ../sdcc/configure\SpecialChar ~
3981 # configure is doing all the magic!
3993 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
3994 It automagically computes the variables srcdir, top_srcdir and top_buildir
4000 the generated files will be in ~/sdcc.build, while the source files stay
4003 This is not only usefull for building different binaries, e.g.
4004 when cross compiling.
4005 It also gives you a much better overview in the source tree when all the
4006 generated files are not scattered between the source files.
4007 And the best thing is: if you want to change a file you can leave the original
4008 file untouched in the source directory.
4009 Simply copy it to the build directory, edit it, enter `make clean`, `rm
4010 Makefile.dep` and `make`.
4015 will do the rest for you!
4018 Building the Documentation
4031 -enable-doc to the configure arguments to build the documentation together
4032 with all the other stuff.
4033 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4034 dvips and makeindex) to get the job done.
4035 Another possibility is to change to the doc directory and to type
4039 \begin_inset Quotes srd
4043 \begin_inset Quotes srd
4050 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4053 \begin_inset LatexCommand \url{http://www.lyx.org}
4057 as editor is straightforward.
4058 Prebuilt documentation in html and pdf format is available from
4059 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4066 Reading the Documentation
4069 Currently reading the document in pdf format is recommended, as for unknown
4070 reason the hyperlinks are working there whereas in the html version they
4077 If you should know why please drop us a note
4083 You'll find the pdf version
4084 \begin_inset LatexCommand \index{PDF version of this document}
4089 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4097 \begin_inset LatexCommand \index{HTML version of this document}
4102 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4108 This documentation is in some aspects different from a commercial documentation:
4112 It tries to document SDCC for several processor architectures in one document
4113 (commercially these probably would be separate documents/products).
4115 \begin_inset LatexCommand \index{Status of documentation}
4119 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4121 Z80, PIC14, PIC16 and HC08.
4124 There are many references pointing away from this documentation.
4125 Don't let this distract you.
4127 was a reference like
4128 \begin_inset LatexCommand \url{http://www.opencores.org}
4132 together with a statement
4133 \begin_inset Quotes sld
4136 some processors which are targetted by SDCC can be implemented in a
4153 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4158 \begin_inset Quotes srd
4162 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4167 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4172 \begin_inset Quotes sld
4175 have you ever heard of an open source compiler that compiles a subset of
4177 \begin_inset Quotes srd
4180 we expect you to have a quick look there and come back.
4181 If you read this you are on the right track.
4184 Some sections attribute more space to problems, restrictions and warnings
4185 than to the solution.
4188 The installation section and the section about the debugger is intimidating.
4191 There are still lots of typos and there are more different writing styles
4195 Testing the SDCC Compiler
4196 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4203 The first thing you should do after installing your SDCC compiler is to
4219 \begin_inset LatexCommand \index{version}
4226 at the prompt, and the program should run and tell you the version.
4227 If it doesn't run, or gives a message about not finding sdcc program, then
4228 you need to check over your installation.
4229 Make sure that the sdcc bin directory is in your executable search path
4230 defined by the PATH environment setting (
4235 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4242 Install trouble-shooting for suggestions
4245 Make sure that the sdcc program is in the bin folder, if not perhaps something
4246 did not install correctly.
4254 is commonly installed as described in section
4255 \begin_inset Quotes sld
4258 Install and search paths
4259 \begin_inset Quotes srd
4268 Make sure the compiler works on a very simple example.
4269 Type in the following test.c program using your favorite
4295 Compile this using the following command:
4304 If all goes well, the compiler will generate a test.asm and test.rel file.
4305 Congratulations, you've just compiled your first program with SDCC.
4306 We used the -c option to tell SDCC not to link the generated code, just
4307 to keep things simple for this step.
4315 The next step is to try it with the linker.
4325 If all goes well the compiler will link with the libraries and produce
4326 a test.ihx output file.
4331 (no test.ihx, and the linker generates warnings), then the problem is most
4340 usr/local/share/sdcc/lib directory
4347 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4354 Install trouble-shooting for suggestions).
4362 The final test is to ensure
4370 header files and libraries.
4371 Edit test.c and change it to the following:
4388 strcpy(str1, "testing");
4395 Compile this by typing
4402 This should generate a test.ihx output file, and it should give no warnings
4403 such as not finding the string.h file.
4404 If it cannot find the string.h file, then the problem is that
4408 cannot find the /usr/local/share/sdcc/include directory
4415 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4422 Install trouble-shooting section for suggestions).
4440 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4444 to find exactly where SDCC is looking for the include and lib files.
4447 Install Trouble-shooting
4448 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4453 \begin_inset LatexCommand \index{Install trouble-shooting}
4460 If SDCC does not build correctly
4463 A thing to try is starting from scratch by unpacking the .tgz source package
4464 again in an empty directory.
4472 ./configure 2>&1 | tee configure.log
4486 make 2>&1 | tee make.log
4493 If anything goes wrong, you can review the log files to locate the problem.
4494 Or a relevant part of this can be attached to an email that could be helpful
4495 when requesting help from the mailing list.
4499 \begin_inset Quotes sld
4503 \begin_inset Quotes srd
4510 \begin_inset Quotes sld
4514 \begin_inset Quotes srd
4517 command is a script that analyzes your system and performs some configuration
4518 to ensure the source package compiles on your system.
4519 It will take a few minutes to run, and will compile a few tests to determine
4520 what compiler features are installed.
4524 \begin_inset Quotes sld
4528 \begin_inset Quotes srd
4534 This runs the GNU make tool, which automatically compiles all the source
4535 packages into the final installed binary executables.
4539 \begin_inset Quotes sld
4543 \begin_inset Quotes erd
4549 This will install the compiler, other executables libraries and include
4550 files into the appropriate directories.
4552 \begin_inset LatexCommand \ref{sub:Install-paths}
4558 \begin_inset LatexCommand \ref{sub:Search-Paths}
4563 about install and search paths.
4565 On most systems you will need super-user privileges to do this.
4571 SDCC is not just a compiler, but a collection of tools by various developers.
4572 These include linkers, assemblers, simulators and other components.
4573 Here is a summary of some of the components.
4574 Note that the included simulator and assembler have separate documentation
4575 which you can find in the source package in their respective directories.
4576 As SDCC grows to include support for other processors, other packages from
4577 various developers are included and may have their own sets of documentation.
4581 You might want to look at the files which are installed in <installdir>.
4582 At the time of this writing, we find the following programs for gcc-builds:
4586 In <installdir>/bin:
4589 sdcc - The compiler.
4592 sdcpp - The C preprocessor.
4595 asx8051 - The assembler for 8051 type processors.
4602 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4605 aslink -The linker for 8051 type processors.
4612 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4615 s51 - The ucSim 8051 simulator.
4616 Not available on Win32 platforms.
4619 sdcdb - The source debugger.
4620 Not available on Win32 platforms.
4623 packihx - A tool to pack (compress) Intel hex files.
4626 In <installdir>/share/sdcc/include
4632 In <installdir>/share/sdcc/lib
4635 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4639 In <installdir>/share/sdcc/doc
4645 As development for other processors proceeds, this list will expand to include
4646 executables to support processors like AVR, PIC, etc.
4652 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4653 the assembler and linkage editor.
4656 sdcpp - The C-Preprocessor
4660 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4664 is a modified version of the GNU preprocessor.
4665 The C preprocessor is used to pull in #include sources, process #ifdef
4666 statements, #defines and so on.
4677 - The Assemblers and Linkage Editors
4680 This is retargettable assembler & linkage editor, it was developed by Alan
4682 John Hartman created the version for 8051, and I (Sandeep) have made some
4683 enhancements and bug fixes for it to work properly with SDCC.
4690 \begin_inset LatexCommand \index{s51}
4694 is a freeware, opensource simulator developed by Daniel Drotos.
4695 The simulator is built as part of the build process.
4696 For more information visit Daniel's web site at:
4697 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4702 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4704 S51 is currently not available on Win32 platfors.
4707 sdcdb - Source Level Debugger
4711 \begin_inset LatexCommand \index{sdcdb (debugger)}
4715 is the companion source level debugger.
4716 More about sdcdb in section
4717 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4722 The current version of the debugger uses Daniel's Simulator S51
4723 \begin_inset LatexCommand \index{s51}
4727 , but can be easily changed to use other simulators.
4728 Sdcdb is currently not available on Win32 platfors.
4737 Single Source File Projects
4740 For single source file 8051 projects the process is very simple.
4741 Compile your programs with the following command
4744 "sdcc sourcefile.c".
4748 This will compile, assemble and link your source file.
4749 Output files are as follows:
4753 \begin_inset LatexCommand \index{<file>.asm}
4758 \begin_inset LatexCommand \index{Assembler source}
4762 file created by the compiler
4766 \begin_inset LatexCommand \index{<file>.lst}
4771 \begin_inset LatexCommand \index{Assembler listing}
4775 file created by the Assembler
4779 \begin_inset LatexCommand \index{<file>.rst}
4784 \begin_inset LatexCommand \index{Assembler listing}
4788 file updated with linkedit information, created by linkage editor
4792 \begin_inset LatexCommand \index{<file>.sym}
4797 \begin_inset LatexCommand \index{Symbol listing}
4801 for the sourcefile, created by the assembler
4805 \begin_inset LatexCommand \index{<file>.rel}
4810 \begin_inset LatexCommand \index{<file>.o}
4815 \begin_inset LatexCommand \index{Object file}
4819 created by the assembler, input to Linkage editor
4823 \begin_inset LatexCommand \index{<file>.map}
4828 \begin_inset LatexCommand \index{Memory map}
4832 for the load module, created by the Linker
4836 \begin_inset LatexCommand \index{<file>.mem}
4840 - A file with a summary of the memory usage
4844 \begin_inset LatexCommand \index{<file>.ihx}
4848 - The load module in Intel hex format
4849 \begin_inset LatexCommand \index{Intel hex format}
4853 (you can select the Motorola S19 format
4854 \begin_inset LatexCommand \index{Motorola S19 format}
4869 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4874 If you need another format you might want to use
4881 \begin_inset LatexCommand \index{objdump (tool)}
4892 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4897 Both formats are documented in the documentation of srecord
4898 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4906 \begin_inset LatexCommand \index{<file>.adb}
4910 - An intermediate file containing debug information needed to create the
4922 \begin_inset LatexCommand \index{-\/-debug}
4930 \begin_inset LatexCommand \index{<file>.cdb}
4934 - An optional file (with -
4944 -debug) containing debug information.
4945 The format is documented in cdbfileformat.pdf
4950 \begin_inset LatexCommand \index{<file> (no extension)}
4954 An optional AOMF or AOMF51
4955 \begin_inset LatexCommand \index{AOMF, AOMF51}
4959 file containing debug information (generated with option -
4986 ormat is commonly used by third party tools (debuggers
4987 \begin_inset LatexCommand \index{Debugger}
4991 , simulators, emulators)
4995 \begin_inset LatexCommand \index{<file>.dump*}
4999 - Dump file to debug the compiler it self (generated with option -
5009 -dumpall) (see section
5010 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5016 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5022 \begin_inset Quotes sld
5025 Anatomy of the compiler
5026 \begin_inset Quotes srd
5032 Projects with Multiple Source Files
5035 SDCC can compile only ONE file at a time.
5036 Let us for example assume that you have a project containing the following
5041 foo1.c (contains some functions)
5043 foo2.c (contains some more functions)
5045 foomain.c (contains more functions and the function main)
5053 The first two files will need to be compiled separately with the commands:
5085 Then compile the source file containing the
5090 \begin_inset LatexCommand \index{Linker}
5094 the files together with the following command:
5102 foomain.c\SpecialChar ~
5103 foo1.rel\SpecialChar ~
5108 \begin_inset LatexCommand \index{<file>.rel}
5120 can be separately compiled as well:
5131 sdcc foomain.rel foo1.rel foo2.rel
5138 The file containing the
5153 file specified in the command line, since the linkage editor processes
5154 file in the order they are presented to it.
5155 The linker is invoked from SDCC using a script file with extension .lnk
5156 \begin_inset LatexCommand \index{<file>.lnk}
5161 You can view this file to troubleshoot linking problems such as those arising
5162 from missing libraries.
5165 Projects with Additional Libraries
5166 \begin_inset LatexCommand \index{Libraries}
5173 Some reusable routines may be compiled into a library, see the documentation
5174 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5178 \begin_inset LatexCommand \index{<file>.lib}
5185 Libraries created in this manner can be included in the command line.
5186 Make sure you include the -L <library-path> option to tell the linker where
5187 to look for these files if they are not in the current directory.
5188 Here is an example, assuming you have the source file
5200 (if that is not the same as your current project):
5207 sdcc foomain.c foolib.lib -L mylib
5218 must be an absolute path name.
5222 The most efficient way to use libraries is to keep separate modules in separate
5224 The lib file now should name all the modules.rel
5225 \begin_inset LatexCommand \index{<file>.rel}
5230 For an example see the standard library file
5234 in the directory <installdir>/share/lib/small.
5237 Using sdcclib to Create and Manage Libraries
5238 \begin_inset LatexCommand \index{sdcclib}
5245 Alternatively, instead of having a .rel file for each entry on the library
5246 file as described in the preceding section, sdcclib can be used to embed
5247 all the modules belonging to such library in the library file itself.
5248 This results in a larger library file, but it greatly reduces the number
5249 of disk files accessed by the linker.
5250 Additionally, the packed library file contains an index of all include
5251 modules and symbols that significantly speeds up the linking process.
5252 To display a list of options supported by sdcclib type:
5261 \begin_inset LatexCommand \index{sdcclib}
5272 To create a new library file, start by compiling all the required modules.
5310 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5312 The next step is to add the .rel files to the library file:
5320 sdcclib libint.lib _divsint.rel
5323 \begin_inset LatexCommand \index{sdcclib}
5333 sdcclib libint.lib _divuint.rel
5339 sdcclib libint.lib _modsint.rel
5345 sdcclib libint.lib _moduint.rel
5351 sdcclib libint.lib _mulint.rel
5358 If the file already exists in the library, it will be replaced.
5359 To see what modules and symbols are included in the library, options -s
5360 and -m are available.
5368 sdcclib -s libint.lib
5371 \begin_inset LatexCommand \index{sdcclib}
5481 If the source files are compiled using -
5492 \begin_inset LatexCommand \index{-\/-debug}
5496 , the corresponding debug information file .adb will be include in the library
5498 The library files created with sdcclib are plain text files, so they can
5499 be viewed with a text editor.
5500 It is not recomended to modify a library file created with sdcclib using
5501 a text editor, as there are file indexes numbers located accross the file
5502 used by the linker to quickly locate the required module to link.
5503 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5504 it can be safely deleted, since all the information required for linking
5505 is embedded in the library file itself.
5506 Library files created using sdcclib are used as described in the preceding
5510 Command Line Options
5511 \begin_inset LatexCommand \index{Command Line Options}
5518 Processor Selection Options
5519 \begin_inset LatexCommand \index{Options processor selection}
5524 \begin_inset LatexCommand \index{Processor selection options}
5530 \labelwidthstring 00.00.0000
5535 \begin_inset LatexCommand \index{-mmcs51}
5541 Generate code for the Intel MCS51
5542 \begin_inset LatexCommand \index{MCS51}
5546 family of processors.
5547 This is the default processor target.
5549 \labelwidthstring 00.00.0000
5554 \begin_inset LatexCommand \index{-mds390}
5560 Generate code for the Dallas DS80C390
5561 \begin_inset LatexCommand \index{DS80C390}
5567 \labelwidthstring 00.00.0000
5572 \begin_inset LatexCommand \index{-mds400}
5578 Generate code for the Dallas DS80C400
5579 \begin_inset LatexCommand \index{DS80C400}
5585 \labelwidthstring 00.00.0000
5590 \begin_inset LatexCommand \index{-mhc08}
5596 Generate code for the Freescale/Motorola HC08
5597 \begin_inset LatexCommand \index{HC08}
5601 family of processors.
5603 \labelwidthstring 00.00.0000
5608 \begin_inset LatexCommand \index{-mz80}
5614 Generate code for the Zilog Z80
5615 \begin_inset LatexCommand \index{Z80}
5619 family of processors.
5621 \labelwidthstring 00.00.0000
5626 \begin_inset LatexCommand \index{-mgbz80}
5632 Generate code for the GameBoy Z80
5633 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5637 processor (Not actively maintained).
5639 \labelwidthstring 00.00.0000
5644 \begin_inset LatexCommand \index{-mavr}
5650 Generate code for the Atmel AVR
5651 \begin_inset LatexCommand \index{AVR}
5655 processor (In development, not complete).
5656 AVR users should probably have a look at winavr
5657 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5662 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5669 I think it is fair to direct users there for now.
5670 Open source is also about avoiding unnecessary work .
5671 But I didn't find the 'official' link.
5673 \labelwidthstring 00.00.0000
5678 \begin_inset LatexCommand \index{-mpic14}
5684 Generate code for the Microchip PIC 14
5685 \begin_inset LatexCommand \index{PIC14}
5689 -bit processors (p16f84 and variants.
5690 In development, not complete).
5693 p16f627 p16f628 p16f84 p16f873 p16f877?
5695 \labelwidthstring 00.00.0000
5700 \begin_inset LatexCommand \index{-mpic16}
5706 Generate code for the Microchip PIC 16
5707 \begin_inset LatexCommand \index{PIC16}
5711 -bit processors (p18f452 and variants.
5712 In development, not complete).
5714 \labelwidthstring 00.00.0000
5720 Generate code for the Toshiba TLCS-900H
5721 \begin_inset LatexCommand \index{TLCS-900H}
5725 processor (Not maintained, not complete).
5727 \labelwidthstring 00.00.0000
5732 \begin_inset LatexCommand \index{-mxa51}
5738 Generate code for the Phillips XA51
5739 \begin_inset LatexCommand \index{XA51}
5743 processor (Not maintained, not complete).
5746 Preprocessor Options
5747 \begin_inset LatexCommand \index{Options preprocessor}
5752 \begin_inset LatexCommand \index{Preprocessor options}
5757 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5763 \labelwidthstring 00.00.0000
5768 \begin_inset LatexCommand \index{-I<path>}
5774 The additional location where the pre processor will look for <..h> or
5775 \begin_inset Quotes eld
5779 \begin_inset Quotes erd
5784 \labelwidthstring 00.00.0000
5789 \begin_inset LatexCommand \index{-D<macro[=value]>}
5795 Command line definition of macros.
5796 Passed to the preprocessor.
5798 \labelwidthstring 00.00.0000
5803 \begin_inset LatexCommand \index{-M}
5809 Tell the preprocessor to output a rule suitable for make describing the
5810 dependencies of each object file.
5811 For each source file, the preprocessor outputs one make-rule whose target
5812 is the object file name for that source file and whose dependencies are
5813 all the files `#include'd in it.
5814 This rule may be a single line or may be continued with `
5816 '-newline if it is long.
5817 The list of rules is printed on standard output instead of the preprocessed
5820 \begin_inset LatexCommand \index{-E}
5826 \labelwidthstring 00.00.0000
5831 \begin_inset LatexCommand \index{-C}
5837 Tell the preprocessor not to discard comments.
5838 Used with the `-E' option.
5840 \labelwidthstring 00.00.0000
5845 \begin_inset LatexCommand \index{-MM}
5856 Like `-M' but the output mentions only the user header files included with
5858 \begin_inset Quotes eld
5862 System header files included with `#include <file>' are omitted.
5864 \labelwidthstring 00.00.0000
5869 \begin_inset LatexCommand \index{-Aquestion(answer)}
5875 Assert the answer answer for question, in case it is tested with a preprocessor
5876 conditional such as `#if #question(answer)'.
5877 `-A-' disables the standard assertions that normally describe the target
5880 \labelwidthstring 00.00.0000
5885 \begin_inset LatexCommand \index{-Umacro}
5891 Undefine macro macro.
5892 `-U' options are evaluated after all `-D' options, but before any `-include'
5893 and `-imacros' options.
5895 \labelwidthstring 00.00.0000
5900 \begin_inset LatexCommand \index{-dM}
5906 Tell the preprocessor to output only a list of the macro definitions that
5907 are in effect at the end of preprocessing.
5908 Used with the `-E' option.
5910 \labelwidthstring 00.00.0000
5915 \begin_inset LatexCommand \index{-dD}
5921 Tell the preprocessor to pass all macro definitions into the output, in
5922 their proper sequence in the rest of the output.
5924 \labelwidthstring 00.00.0000
5929 \begin_inset LatexCommand \index{-dN}
5940 Like `-dD' except that the macro arguments and contents are omitted.
5941 Only `#define name' is included in the output.
5943 \labelwidthstring 00.00.0000
5948 preprocessorOption[,preprocessorOption]
5951 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5956 Pass the preprocessorOption to the preprocessor
5961 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5966 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5967 Collection (gcc), if you need more dedicated options please refer to the
5969 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5977 \begin_inset LatexCommand \index{Options linker}
5982 \begin_inset LatexCommand \index{Linker options}
5988 \labelwidthstring 00.00.0000
6008 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6013 \begin_inset LatexCommand \index{-L -\/-lib-path}
6020 <absolute path to additional libraries> This option is passed to the linkage
6021 editor's additional libraries
6022 \begin_inset LatexCommand \index{Libraries}
6027 The path name must be absolute.
6028 Additional library files may be specified in the command line.
6029 See section Compiling programs for more details.
6031 \labelwidthstring 00.00.0000
6048 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6053 <Value> The start location of the external ram
6054 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6058 , default value is 0.
6059 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6069 -xram-loc 0x8000 or -
6081 \labelwidthstring 00.00.0000
6098 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6103 <Value> The start location of the code
6104 \begin_inset LatexCommand \index{code}
6108 segment, default value 0.
6109 Note when this option is used the interrupt vector table is also relocated
6110 to the given address.
6111 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6121 -code-loc 0x8000 or -
6133 \labelwidthstring 00.00.0000
6150 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6155 <Value> By default the stack
6156 \begin_inset LatexCommand \index{stack}
6160 is placed after the data segment.
6161 Using this option the stack can be placed anywhere in the internal memory
6163 The value entered can be in Hexadecimal or Decimal format, e.g.
6174 -stack-loc 0x20 or -
6185 Since the sp register is incremented before a push or call, the initial
6186 sp will be set to one byte prior the provided value.
6187 The provided value should not overlap any other memory areas such as used
6188 register banks or the data segment and with enough space for the current
6206 \begin_inset LatexCommand \index{-\/-pack-iram}
6210 option (which is now a default setting) will override this setting, so
6211 you should also specify the
6227 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6231 option if you need to manually place the stack.
6233 \labelwidthstring 00.00.0000
6250 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6255 <Value> By default the external stack
6256 \begin_inset LatexCommand \index{xstack}
6260 is placed after the pdata segment.
6261 Using this option the xstack can be placed anywhere in the external memory
6263 The value entered can be in Hexadecimal or Decimal format, e.g.
6274 -xstack-loc 0x8000 or -
6285 The provided value should not overlap any other memory areas such as the
6286 pdata or xdata segment and with enough space for the current application.
6288 \labelwidthstring 00.00.0000
6305 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6310 <Value> The start location of the internal ram data
6311 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6316 The value entered can be in Hexadecimal or Decimal format, eg.
6338 (By default, the start location of the internal ram data segment is set
6339 as low as possible in memory, taking into account the used register banks
6340 and the bit segment at address 0x20.
6341 For example if register banks 0 and 1 are used without bit variables, the
6342 data segment will be set, if -
6352 -data-loc is not used, to location 0x10.)
6354 \labelwidthstring 00.00.0000
6371 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6376 <Value> The start location of the indirectly addressable internal ram
6377 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6381 of the 8051, default value is 0x80.
6382 The value entered can be in Hexadecimal or Decimal format, eg.
6393 -idata-loc 0x88 or -
6405 \labelwidthstring 00.00.0000
6422 <Value> The start location of the bit
6423 \begin_inset LatexCommand \index{bit}
6427 addressable internal ram of the 8051.
6433 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6436 \labelwidthstring 00.00.0000
6451 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6460 The linker output (final object code) is in Intel Hex format.
6461 \begin_inset LatexCommand \index{Intel hex format}
6465 This is the default option.
6466 The format itself is documented in the documentation of srecord
6467 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6473 \labelwidthstring 00.00.0000
6488 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6497 The linker output (final object code) is in Motorola S19 format
6498 \begin_inset LatexCommand \index{Motorola S19 format}
6503 The format itself is documented in the documentation of srecord.
6505 \labelwidthstring 00.00.0000
6520 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6529 The linker output (final object code) is in ELF format
6530 \begin_inset LatexCommand \index{ELF format}
6535 (Currently only supported for the HC08 processors)
6537 \labelwidthstring 00.00.0000
6542 linkOption[,linkOption]
6545 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6550 Pass the linkOption to the linker.
6551 See file sdcc/as/doc/asxhtm.html for more on linker options.
6555 \begin_inset LatexCommand \index{Options MCS51}
6560 \begin_inset LatexCommand \index{MCS51 options}
6566 \labelwidthstring 00.00.0000
6581 \begin_inset LatexCommand \index{-\/-model-small}
6592 Generate code for Small Model programs, see section Memory Models for more
6594 This is the default model.
6596 \labelwidthstring 00.00.0000
6611 \begin_inset LatexCommand \index{-\/-model-medium}
6617 Generate code for Medium model programs, see section Memory Models for
6619 If this option is used all source files in the project have to be compiled
6621 It must also be used when invoking the linker.
6623 \labelwidthstring 00.00.0000
6638 \begin_inset LatexCommand \index{-\/-model-large}
6644 Generate code for Large model programs, see section Memory Models for more
6646 If this option is used all source files in the project have to be compiled
6648 It must also be used when invoking the linker.
6650 \labelwidthstring 00.00.0000
6665 \begin_inset LatexCommand \index{-\/-xstack}
6671 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6672 variables and passing parameters.
6674 \begin_inset LatexCommand \ref{sub:External-Stack}
6679 External Stack for more details.
6681 \labelwidthstring 00.00.0000
6699 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6703 Causes the linker to check if the internal ram usage is within limits of
6706 \labelwidthstring 00.00.0000
6724 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6728 Causes the linker to check if the external ram usage is within limits of
6731 \labelwidthstring 00.00.0000
6749 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6753 Causes the linker to check if the code memory usage is within limits of
6756 \labelwidthstring 00.00.0000
6774 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6778 Causes the linker to check if there is at minimum <Value> bytes for stack.
6780 \labelwidthstring 00.00.0000
6798 \begin_inset LatexCommand \index{-\/-pack-iram}
6802 Causes the linker to use unused register banks for data variables and pack
6803 data, idata and stack together.
6804 This is the default now.
6806 \labelwidthstring 00.00.0000
6824 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6828 Causes the linker to use old style for allocating memory areas.
6831 DS390 / DS400 Options
6832 \begin_inset LatexCommand \index{Options DS390}
6837 \begin_inset LatexCommand \index{DS390 options}
6843 \labelwidthstring 00.00.0000
6860 \begin_inset LatexCommand \index{-\/-model-flat24}
6870 Generate 24-bit flat mode code.
6871 This is the one and only that the ds390 code generator supports right now
6872 and is default when using
6877 See section Memory Models for more details.
6879 \labelwidthstring 00.00.0000
6894 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6900 disable interrupts during ESP:SP updates.
6902 \labelwidthstring 00.00.0000
6919 \begin_inset LatexCommand \index{-\/-stack-10bit}
6923 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6924 This is the one and only that the ds390 code generator supports right now
6925 and is default when using
6930 In this mode, the stack is located in the lower 1K of the internal RAM,
6931 which is mapped to 0x400000.
6932 Note that the support is incomplete, since it still uses a single byte
6933 as the stack pointer.
6934 This means that only the lower 256 bytes of the potential 1K stack space
6935 will actually be used.
6936 However, this does allow you to reclaim the precious 256 bytes of low RAM
6937 for use for the DATA and IDATA segments.
6938 The compiler will not generate any code to put the processor into 10 bit
6940 It is important to ensure that the processor is in this mode before calling
6941 any re-entrant functions compiled with this option.
6942 In principle, this should work with the
6955 \begin_inset LatexCommand \index{-\/-stack-auto}
6961 option, but that has not been tested.
6962 It is incompatible with the
6975 \begin_inset LatexCommand \index{-\/-xstack}
6982 It also only makes sense if the processor is in 24 bit contiguous addressing
6995 -model-flat24 option
6999 \labelwidthstring 00.00.0000
7014 \begin_inset LatexCommand \index{-\/-stack-probe}
7020 insert call to function __stack_probe at each function prologue.
7022 \labelwidthstring 00.00.0000
7037 \begin_inset LatexCommand \index{-\/-tini-libid}
7043 <nnnn> LibraryID used in -mTININative.
7046 \labelwidthstring 00.00.0000
7061 \begin_inset LatexCommand \index{-\/-use-accelerator}
7067 generate code for DS390 Arithmetic Accelerator.
7072 \begin_inset LatexCommand \index{Options Z80}
7077 \begin_inset LatexCommand \index{Z80 options}
7083 \labelwidthstring 00.00.0000
7100 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
7110 Force a called function to always save BC.
7112 \labelwidthstring 00.00.0000
7129 \begin_inset LatexCommand \index{-\/-no-std-crt0}
7133 When linking, skip the standard crt0.o object file.
7134 You must provide your own crt0.o for your system when linking.
7138 Optimization Options
7139 \begin_inset LatexCommand \index{Options optimization}
7144 \begin_inset LatexCommand \index{Optimization options}
7150 \labelwidthstring 00.00.0000
7165 \begin_inset LatexCommand \index{-\/-nogcse}
7171 Will not do global subexpression elimination, this option may be used when
7172 the compiler creates undesirably large stack/data spaces to store compiler
7182 \begin_inset LatexCommand \index{sloc (spill location)}
7187 A warning message will be generated when this happens and the compiler
7188 will indicate the number of extra bytes it allocated.
7189 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7191 \begin_inset LatexCommand \index{\#pragma nogcse}
7195 can be used to turn off global subexpression elimination
7196 \begin_inset LatexCommand \index{Subexpression elimination}
7200 for a given function only.
7202 \labelwidthstring 00.00.0000
7217 \begin_inset LatexCommand \index{-\/-noinvariant}
7223 Will not do loop invariant optimizations, this may be turned off for reasons
7224 explained for the previous option.
7225 For more details of loop optimizations performed see Loop Invariants in
7227 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7232 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7234 \begin_inset LatexCommand \index{\#pragma noinvariant}
7238 can be used to turn off invariant optimizations for a given function only.
7240 \labelwidthstring 00.00.0000
7255 \begin_inset LatexCommand \index{-\/-noinduction}
7261 Will not do loop induction optimizations, see section strength reduction
7263 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7265 \begin_inset LatexCommand \index{\#pragma noinduction}
7269 can be used to turn off induction optimizations for a given function only.
7271 \labelwidthstring 00.00.0000
7286 \begin_inset LatexCommand \index{-\/-nojtbound}
7297 Will not generate boundary condition check when switch statements
7298 \begin_inset LatexCommand \index{switch statement}
7302 are implemented using jump-tables.
7304 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7309 Switch Statements for more details.
7310 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7312 \begin_inset LatexCommand \index{\#pragma nojtbound}
7316 can be used to turn off boundary checking for jump tables for a given function
7319 \labelwidthstring 00.00.0000
7334 \begin_inset LatexCommand \index{-\/-noloopreverse}
7343 Will not do loop reversal
7344 \begin_inset LatexCommand \index{Loop reversing}
7350 \labelwidthstring 00.00.0000
7367 \begin_inset LatexCommand \index{-\/-nolabelopt }
7371 Will not optimize labels (makes the dumpfiles more readable).
7373 \labelwidthstring 00.00.0000
7388 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7394 Will not memcpy initialized data from code space into xdata space.
7395 This saves a few bytes in code space if you don't have initialized data
7396 \begin_inset LatexCommand \index{Variable initialization}
7402 \labelwidthstring 00.00.0000
7417 \begin_inset LatexCommand \index{-\/-nooverlay}
7423 The compiler will not overlay parameters and local variables of any function,
7424 see section Parameters and local variables for more details.
7426 \labelwidthstring 00.00.0000
7441 \begin_inset LatexCommand \index{-\/-no-peep}
7447 Disable peep-hole optimization with built-in rules.
7449 \labelwidthstring 00.00.0000
7466 \begin_inset LatexCommand \index{-\/-peep-file}
7471 <filename> This option can be used to use additional rules to be used by
7472 the peep hole optimizer.
7474 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7479 Peep Hole optimizations for details on how to write these rules.
7481 \labelwidthstring 00.00.0000
7496 \begin_inset LatexCommand \index{-\/-peep-asm}
7502 Pass the inline assembler code through the peep hole optimizer.
7503 This can cause unexpected changes to inline assembler code, please go through
7504 the peephole optimizer
7505 \begin_inset LatexCommand \index{Peephole optimizer}
7509 rules defined in the source file tree '<target>/peeph.def' before using
7512 \labelwidthstring 00.00.0000
7527 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7533 The compiler will optimize code generation towards fast code, possibly
7534 at the expense of code size.
7536 \labelwidthstring 00.00.0000
7551 \begin_inset LatexCommand \index{-\/-opt-code-size}
7557 The compiler will optimize code generation towards compact code, possibly
7558 at the expense of code speed.
7562 \begin_inset LatexCommand \index{Options other}
7568 \labelwidthstring 00.00.0000
7584 \begin_inset LatexCommand \index{-\/-compile-only}
7589 \begin_inset LatexCommand \index{-c -\/-compile-only}
7595 will compile and assemble the source, but will not call the linkage editor.
7597 \labelwidthstring 00.00.0000
7616 \begin_inset LatexCommand \index{-\/-c1mode}
7622 reads the preprocessed source from standard input and compiles it.
7623 The file name for the assembler output must be specified using the -o option.
7625 \labelwidthstring 00.00.0000
7630 \begin_inset LatexCommand \index{-E}
7636 Run only the C preprocessor.
7637 Preprocess all the C source files specified and output the results to standard
7640 \labelwidthstring 00.00.0000
7646 \begin_inset LatexCommand \index{-o <path/file>}
7652 The output path resp.
7653 file where everything will be placed.
7654 If the parameter is a path, it must have a trailing slash (or backslash
7655 for the Windows binaries) to be recognized as a path.
7658 \labelwidthstring 00.00.0000
7673 \begin_inset LatexCommand \index{-\/-stack-auto}
7684 All functions in the source file will be compiled as
7689 \begin_inset LatexCommand \index{reentrant}
7694 the parameters and local variables will be allocated on the stack
7695 \begin_inset LatexCommand \index{stack}
7701 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7705 Parameters and Local Variables for more details.
7706 If this option is used all source files in the project should be compiled
7708 It automatically implies --int-long-reent and --float-reent.
7711 \labelwidthstring 00.00.0000
7726 \begin_inset LatexCommand \index{-\/-callee-saves}
7730 function1[,function2][,function3]....
7733 The compiler by default uses a caller saves convention for register saving
7734 across function calls, however this can cause unnecessary register pushing
7735 & popping when calling small functions from larger functions.
7736 This option can be used to switch the register saving convention for the
7737 function names specified.
7738 The compiler will not save registers when calling these functions, no extra
7739 code will be generated at the entry & exit (function prologue
7742 \begin_inset LatexCommand \index{function prologue}
7751 \begin_inset LatexCommand \index{function epilogue}
7757 ) for these functions to save & restore the registers used by these functions,
7758 this can SUBSTANTIALLY reduce code & improve run time performance of the
7760 In the future the compiler (with inter procedural analysis) will be able
7761 to determine the appropriate scheme to use for each function call.
7762 DO NOT use this option for built-in functions such as _mulint..., if this
7763 option is used for a library function the appropriate library function
7764 needs to be recompiled with the same option.
7765 If the project consists of multiple source files then all the source file
7766 should be compiled with the same -
7776 -callee-saves option string.
7777 Also see #pragma\SpecialChar ~
7779 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7785 \labelwidthstring 00.00.0000
7800 \begin_inset LatexCommand \index{-\/-debug}
7809 When this option is used the compiler will generate debug information.
7810 The debug information collected in a file with .cdb extension can be used
7812 For more information see documentation for SDCDB.
7813 Another file with no extension contains debug information in AOMF or AOMF51
7814 \begin_inset LatexCommand \index{AOMF, AOMF51}
7818 format which is commonly used by third party tools.
7820 \labelwidthstring 00.00.0000
7825 \begin_inset LatexCommand \index{-S}
7836 Stop after the stage of compilation proper; do not assemble.
7837 The output is an assembler code file for the input file specified.
7839 \labelwidthstring 00.00.0000
7854 \begin_inset LatexCommand \index{-\/-int-long-reent}
7860 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7861 Note by default these libraries are compiled as non-reentrant.
7862 See section Installation for more details.
7864 \labelwidthstring 00.00.0000
7879 \begin_inset LatexCommand \index{-\/-cyclomatic}
7888 This option will cause the compiler to generate an information message for
7889 each function in the source file.
7890 The message contains some
7894 information about the function.
7895 The number of edges and nodes the compiler detected in the control flow
7896 graph of the function, and most importantly the
7898 cyclomatic complexity
7899 \begin_inset LatexCommand \index{Cyclomatic complexity}
7905 see section on Cyclomatic Complexity for more details.
7907 \labelwidthstring 00.00.0000
7922 \begin_inset LatexCommand \index{-\/-float-reent}
7928 Floating point library is compiled as reentrant
7929 \begin_inset LatexCommand \index{reentrant}
7934 See section Installation for more details.
7936 \labelwidthstring 00.00.0000
7951 \begin_inset LatexCommand \index{-\/-main-return}
7957 This option can be used if the code generated is called by a monitor program
7958 or if the main routine includes an endless loop.
7959 This option might result in slightly smaller code and save two bytes of
7961 The return from the 'main'
7962 \begin_inset LatexCommand \index{main return}
7966 function will return to the function calling main.
7967 The default setting is to lock up i.e.
7974 \labelwidthstring 00.00.0000
7989 \begin_inset LatexCommand \index{-\/-nostdinc}
7995 This will prevent the compiler from passing on the default include path
7996 to the preprocessor.
7998 \labelwidthstring 00.00.0000
8013 \begin_inset LatexCommand \index{-\/-nostdlib}
8019 This will prevent the compiler from passing on the default library
8020 \begin_inset LatexCommand \index{Libraries}
8026 \labelwidthstring 00.00.0000
8041 \begin_inset LatexCommand \index{-\/-verbose}
8047 Shows the various actions the compiler is performing.
8049 \labelwidthstring 00.00.0000
8054 \begin_inset LatexCommand \index{-V}
8060 Shows the actual commands the compiler is executing.
8062 \labelwidthstring 00.00.0000
8077 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8083 Hides your ugly and inefficient c-code from the asm file, so you can always
8084 blame the compiler :)
8086 \labelwidthstring 00.00.0000
8101 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8107 Will not include peep-hole comments in the generated files.
8109 \labelwidthstring 00.00.0000
8124 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8130 Include i-codes in the asm file.
8131 Sounds like noise but is most helpful for debugging the compiler itself.
8133 \labelwidthstring 00.00.0000
8148 \begin_inset LatexCommand \index{-\/-less-pedantic}
8154 Disable some of the more pedantic warnings
8155 \begin_inset LatexCommand \index{Warnings}
8159 (jwk burps: please be more specific here, please!).
8161 \labelwidthstring 00.00.0000
8175 -disable-warning\SpecialChar ~
8177 \begin_inset LatexCommand \index{-\/-disable-warning}
8183 Disable specific warning with number <nnnn>.
8185 \labelwidthstring 00.00.0000
8200 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8206 Display the directories in the compiler's search path
8208 \labelwidthstring 00.00.0000
8223 \begin_inset LatexCommand \index{-\/-vc}
8229 Display errors and warnings using MSVC style, so you can use SDCC with
8232 \labelwidthstring 00.00.0000
8247 \begin_inset LatexCommand \index{-\/-use-stdout}
8253 Send errors and warnings to stdout instead of stderr.
8255 \labelwidthstring 00.00.0000
8260 asmOption[,asmOption]
8263 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8268 Pass the asmOption to the assembler
8269 \begin_inset LatexCommand \index{Options assembler}
8274 \begin_inset LatexCommand \index{Assembler options}
8279 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8281 \labelwidthstring 00.00.0000
8296 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8302 Generally follow the C89 standard, but allow SDCC features that conflict
8303 with the standard (default).
8305 \labelwidthstring 00.00.0000
8320 \begin_inset LatexCommand \index{-\/-std-c89}
8326 Follow the C89 standard and disable SDCC features that conflict with the
8329 \labelwidthstring 00.00.0000
8344 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8350 Generally follow the C99 standard, but allow SDCC features that conflict
8351 with the standard (incomplete support).
8353 \labelwidthstring 00.00.0000
8368 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8374 Follow the C99 standard and disable SDCC features that conflict with the
8375 standard (incomplete support).
8377 \labelwidthstring 00.00.0000
8394 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8399 <Name> The name to be used for the code
8400 \begin_inset LatexCommand \index{code}
8404 segment, default CSEG.
8405 This is useful if you need to tell the compiler to put the code in a special
8406 segment so you can later on tell the linker to put this segment in a special
8408 Can be used for instance when using bank switching to put the code in a
8411 \labelwidthstring 00.00.0000
8428 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8433 <Name> The name to be used for the const
8434 \begin_inset LatexCommand \index{code}
8438 segment, default CONST.
8439 This is useful if you need to tell the compiler to put the const data in
8440 a special segment so you can later on tell the linker to put this segment
8441 in a special place in memory.
8442 Can be used for instance when using bank switching to put the const data
8445 \labelwidthstring 00.00.0000
8457 a SDCC compiler option but if you want
8461 warnings you can use a separate tool dedicated to syntax checking like
8463 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8468 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8473 \begin_inset LatexCommand \url{http://www.splint.org}
8478 To make your source files parseable by splint you will have to include
8484 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8488 in your source file and add brackets around extended keywords (like
8491 \begin_inset Quotes sld
8504 \begin_inset Quotes srd
8512 \begin_inset Quotes sld
8515 __interrupt\SpecialChar ~
8517 \begin_inset Quotes srd
8525 Splint has an excellent on line manual at
8526 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8530 and it's capabilities go beyond pure syntax checking.
8531 You'll need to tell splint the location of SDCC's include files so a typical
8532 command line could look like this:
8536 splint\SpecialChar ~
8538 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8543 Intermediate Dump Options
8544 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8549 \begin_inset LatexCommand \index{Options intermediate dump}
8554 \begin_inset LatexCommand \index{Intermediate dump options}
8561 The following options are provided for the purpose of retargetting and debugging
8563 They provide a means to dump the intermediate code (iCode
8564 \begin_inset LatexCommand \index{iCode}
8568 ) generated by the compiler in human readable form at various stages of
8569 the compilation process.
8570 More on iCodes see chapter
8571 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8576 \begin_inset Quotes srd
8579 The anatomy of the compiler
8580 \begin_inset Quotes srd
8585 \labelwidthstring 00.00.0000
8600 \begin_inset LatexCommand \index{-\/-dumpraw}
8606 This option will cause the compiler to dump the intermediate code into
8609 <source filename>.dumpraw
8611 just after the intermediate code has been generated for a function, i.e.
8612 before any optimizations are done.
8614 \begin_inset LatexCommand \index{Basic blocks}
8618 at this stage ordered in the depth first number, so they may not be in
8619 sequence of execution.
8621 \labelwidthstring 00.00.0000
8636 \begin_inset LatexCommand \index{-\/-dumpgcse}
8642 Will create a dump of iCode's, after global subexpression elimination
8643 \begin_inset LatexCommand \index{Global subexpression elimination}
8649 <source filename>.dumpgcse.
8651 \labelwidthstring 00.00.0000
8666 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8672 Will create a dump of iCode's, after deadcode elimination
8673 \begin_inset LatexCommand \index{Dead-code elimination}
8679 <source filename>.dumpdeadcode.
8681 \labelwidthstring 00.00.0000
8696 \begin_inset LatexCommand \index{-\/-dumploop}
8705 Will create a dump of iCode's, after loop optimizations
8706 \begin_inset LatexCommand \index{Loop optimization}
8712 <source filename>.dumploop.
8714 \labelwidthstring 00.00.0000
8729 \begin_inset LatexCommand \index{-\/-dumprange}
8738 Will create a dump of iCode's, after live range analysis
8739 \begin_inset LatexCommand \index{Live range analysis}
8745 <source filename>.dumprange.
8747 \labelwidthstring 00.00.0000
8762 \begin_inset LatexCommand \index{-\/-dumlrange}
8768 Will dump the life ranges
8769 \begin_inset LatexCommand \index{Live range analysis}
8775 \labelwidthstring 00.00.0000
8790 \begin_inset LatexCommand \index{-\/-dumpregassign}
8799 Will create a dump of iCode's, after register assignment
8800 \begin_inset LatexCommand \index{Register assignment}
8806 <source filename>.dumprassgn.
8808 \labelwidthstring 00.00.0000
8823 \begin_inset LatexCommand \index{-\/-dumplrange}
8829 Will create a dump of the live ranges of iTemp's
8831 \labelwidthstring 00.00.0000
8846 \begin_inset LatexCommand \index{-\/-dumpall}
8857 Will cause all the above mentioned dumps to be created.
8860 Redirecting output on Windows Shells
8863 By default SDCC writes it's error messages to
8864 \begin_inset Quotes sld
8868 \begin_inset Quotes srd
8872 To force all messages to
8873 \begin_inset Quotes sld
8877 \begin_inset Quotes srd
8901 \begin_inset LatexCommand \index{-\/-use-stdout}
8906 Additionally, if you happen to have visual studio installed in your windows
8907 machine, you can use it to compile your sources using a custom build and
8923 \begin_inset LatexCommand \index{-\/-vc}
8928 Something like this should work:
8972 -model-large -c $(InputPath)
8975 Environment variables
8976 \begin_inset LatexCommand \index{Environment variables}
8983 SDCC recognizes the following environment variables:
8985 \labelwidthstring 00.00.0000
8990 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8996 SDCC installs a signal handler
8997 \begin_inset LatexCommand \index{signal handler}
9001 to be able to delete temporary files after an user break (^C) or an exception.
9002 If this environment variable is set, SDCC won't install the signal handler
9003 in order to be able to debug SDCC.
9005 \labelwidthstring 00.00.0000
9012 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9018 Path, where temporary files will be created.
9019 The order of the variables is the search order.
9020 In a standard *nix environment these variables are not set, and there's
9021 no need to set them.
9022 On Windows it's recommended to set one of them.
9024 \labelwidthstring 00.00.0000
9029 \begin_inset LatexCommand \index{SDCC\_HOME}
9036 \begin_inset LatexCommand \ref{sub:Install-paths}
9042 \begin_inset Quotes sld
9046 \begin_inset Quotes srd
9051 \labelwidthstring 00.00.0000
9056 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9063 \begin_inset LatexCommand \ref{sub:Search-Paths}
9069 \begin_inset Quotes sld
9073 \begin_inset Quotes srd
9078 \labelwidthstring 00.00.0000
9083 \begin_inset LatexCommand \index{SDCC\_LIB}
9090 \begin_inset LatexCommand \ref{sub:Search-Paths}
9096 \begin_inset Quotes sld
9100 \begin_inset Quotes srd
9106 There are some more environment variables recognized by SDCC, but these
9107 are solely used for debugging purposes.
9108 They can change or disappear very quickly, and will never be documented.
9111 Storage Class Language Extensions
9114 MCS51/DS390 Storage Class
9115 \begin_inset LatexCommand \index{Storage class}
9122 In addition to the ANSI storage classes SDCC allows the following MCS51
9123 specific storage classes:
9124 \layout Subsubsection
9127 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9132 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9137 \begin_inset LatexCommand \index{near (storage class)}
9142 \begin_inset LatexCommand \index{\_\_near (storage class)}
9153 storage class for the Small Memory model (
9161 can be used synonymously).
9162 Variables declared with this storage class will be allocated in the directly
9163 addressable portion of the internal RAM of a 8051, e.g.:
9168 data unsigned char test_data;
9171 Writing 0x01 to this variable generates the assembly code:
9176 75*00 01\SpecialChar ~
9182 \layout Subsubsection
9185 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9190 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9195 \begin_inset LatexCommand \index{far (storage class)}
9200 \begin_inset LatexCommand \index{\_\_far (storage class)}
9207 Variables declared with this storage class will be placed in the external
9213 storage class for the Large Memory model, e.g.:
9218 xdata unsigned char test_xdata;
9221 Writing 0x01 to this variable generates the assembly code:
9226 90s00r00\SpecialChar ~
9255 \layout Subsubsection
9258 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9263 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9270 Variables declared with this storage class will be allocated into the indirectly
9271 addressable portion of the internal ram of a 8051, e.g.:
9276 idata unsigned char test_idata;
9279 Writing 0x01 to this variable generates the assembly code:
9308 Please note, the first 128 byte of idata physically access the same RAM
9310 The original 8051 had 128 byte idata memory, nowadays most devices have
9311 256 byte idata memory.
9313 \begin_inset LatexCommand \index{stack}
9317 is located in idata memory.
9318 \layout Subsubsection
9321 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9326 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9333 Paged xdata access is just as straightforward as using the other addressing
9335 It is typically located at the start of xdata and has a maximum size of
9337 The following example writes 0x01 to the pdata variable.
9338 Please note, pdata access physically accesses xdata memory.
9339 The high byte of the address is determined by port P2
9340 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9344 (or in case of some 8051 variants by a separate Special Function Register,
9346 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9355 storage class for the Medium Memory model, e.g.:
9360 pdata unsigned char test_pdata;
9363 Writing 0x01 to this variable generates the assembly code:
9407 \begin_inset LatexCommand \index{-\/-xstack}
9411 option is used the pdata memory area is followed by the xstack memory area
9412 and the sum of their sizes is limited to 256 bytes.
9413 \layout Subsubsection
9416 \begin_inset LatexCommand \index{code}
9421 \begin_inset LatexCommand \index{\_\_code}
9428 'Variables' declared with this storage class will be placed in the code
9434 code unsigned char test_code;
9437 Read access to this variable generates the assembly code:
9442 90s00r6F\SpecialChar ~
9445 mov dptr,#_test_code
9474 indexed arrays of characters in code memory can be accessed efficiently:
9479 code char test_array[] = {'c','h','e','a','p'};
9482 Read access to this array using an 8-bit unsigned index generates the assembly
9499 90s00r41\SpecialChar ~
9502 mov dptr,#_test_array
9517 \layout Subsubsection
9520 \begin_inset LatexCommand \index{bit}
9525 \begin_inset LatexCommand \index{\_\_bit}
9532 This is a data-type and a storage class specifier.
9533 When a variable is declared as a bit, it is allocated into the bit addressable
9534 memory of 8051, e.g.:
9542 Writing 1 to this variable generates the assembly code:
9558 The bit addressable memory consists of 128 bits which are located from 0x20
9559 to 0x2f in data memory.
9562 Apart from this 8051 specific storage class most architectures support ANSI-C
9564 \begin_inset LatexCommand \index{bitfields}
9574 Not really meant as examples, but nevertheless showing what bitfields are
9575 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9579 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9580 signed modifier are implemented as unsigned.
9581 \layout Subsubsection
9584 \begin_inset LatexCommand \index{sfr}
9589 \begin_inset LatexCommand \index{\_\_sfr}
9594 \begin_inset LatexCommand \index{sfr16}
9599 \begin_inset LatexCommand \index{\_\_sfr16}
9604 \begin_inset LatexCommand \index{sfr32}
9609 \begin_inset LatexCommand \index{\_\_sfr32}
9614 \begin_inset LatexCommand \index{\_\_sbit}
9621 Like the bit keyword,
9623 sfr / sfr16 / sfr32 / sbit
9625 signify both a data-type and storage class, they are used to describe the
9646 variables of a 8051, eg:
9652 \begin_inset LatexCommand \index{at}
9657 \begin_inset LatexCommand \index{\_\_at}
9661 0x80 P0;\SpecialChar ~
9662 /* special function register P0 at location 0x80 */
9664 /* 16 bit special function register combination for timer 0 */
9666 /* with the high byte at location 0x8C and the low byte at location 0x8A
9670 \begin_inset LatexCommand \index{at}
9675 \begin_inset LatexCommand \index{\_\_at}
9681 sbit at 0xd7 CY; /* CY (Carry Flag
9682 \begin_inset LatexCommand \index{Flags}
9687 \begin_inset LatexCommand \index{Carry flag}
9694 Special function registers which are located on an address dividable by
9695 8 are bit-addressable, an
9699 addresses a specific bit within these sfr.
9701 16 Bit and 32 bit special function register combinations which require a
9702 certain access order are better not declared using
9711 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9712 this is not guaranteed.
9713 \layout Subsubsection
9716 \begin_inset LatexCommand \index{Pointer}
9720 to MCS51/DS390 specific memory spaces
9723 SDCC allows (via language extensions) pointers to explicitly point to any
9724 of the memory spaces
9725 \begin_inset LatexCommand \index{Memory model}
9730 In addition to the explicit pointers, the compiler uses (by default) generic
9731 pointers which can be used to point to any of the memory spaces.
9735 Pointer declaration examples:
9740 /* pointer physically in internal ram pointing to object in external ram
9743 xdata unsigned char * data p;
9747 /* pointer physically in external ram pointing to object in internal ram
9750 data unsigned char * xdata p;
9754 /* pointer physically in code rom pointing to data in xdata space */
9756 xdata unsigned char * code p;
9760 /* pointer physically in code space pointing to data in code space */
9762 code unsigned char * code p;
9766 /* the following is a generic pointer physically located in xdata space
9773 /* the following is a function pointer physically located in data space
9776 char (* data fp)(void);
9779 Well you get the idea.
9784 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9797 The highest order byte of the
9801 pointers contains the data space information.
9802 Assembler support routines are called whenever data is stored or retrieved
9808 These are useful for developing reusable library
9809 \begin_inset LatexCommand \index{Libraries}
9814 Explicitly specifying the pointer type will generate the most efficient
9816 \layout Subsubsection
9818 Notes on MCS51 memory
9819 \begin_inset LatexCommand \index{MCS51 memory}
9826 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9827 RAM memory which is structured as follows:
9831 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9834 - Bytes 20-2F - 16 bytes to hold 128 bit
9835 \begin_inset LatexCommand \index{bit}
9841 - Bytes 30-7F - 80 bytes for general purpose use.
9846 Additionally some members of the MCS51 family may have up to 128 bytes of
9847 additional, indirectly addressable, internal RAM memory (
9852 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9857 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9862 Furthermore, some chips may have some built in external memory (
9867 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9872 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9876 ) which should not be confused with the internal, directly addressable RAM
9882 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9887 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9892 Sometimes this built in
9896 memory has to be activated before using it (you can probably find this
9897 information on the datasheet of the microcontroller your are using, see
9899 \begin_inset LatexCommand \ref{sub:Startup-Code}
9907 Normally SDCC will only use the first bank
9908 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9912 of registers (register bank 0), but it is possible to specify that other
9913 banks of registers (keyword
9920 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9925 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9931 ) should be used in interrupt
9932 \begin_inset LatexCommand \index{interrupt}
9937 \begin_inset LatexCommand \index{\_\_interrupt}
9942 By default, the compiler will place the stack after the last byte of allocated
9943 memory for variables.
9944 For example, if the first 2 banks of registers are used, and only four
9949 variables, it will position the base of the internal stack at address 20
9951 This implies that as the stack
9952 \begin_inset LatexCommand \index{stack}
9956 grows, it will use up the remaining register banks, and the 16 bytes used
9957 by the 128 bit variables, and 80 bytes for general purpose use.
9958 If any bit variables are used, the data variables will be placed in unused
9959 register banks and after the byte holding the last bit variable.
9960 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9965 variables will be placed starting from address 0x10 to 0x20 and continue
9978 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9982 to specify the start address of the
9997 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10001 to specify the size of the total internal RAM (
10013 By default the 8051 linker will place the stack after the last byte of (i)data
10026 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10030 allows you to specify the start of the stack, i.e.
10031 you could start it after any data in the general purpose area.
10032 If your microcontroller has additional indirectly addressable internal
10037 ) you can place the stack on it.
10038 You may also need to use -
10049 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10053 to set the start address of the external RAM (
10068 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10072 to specify its size.
10073 Same goes for the code memory, using -
10084 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10099 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10104 If in doubt, don't specify any options and see if the resulting memory
10105 layout is appropriate, then you can adjust it.
10108 The linker generates two files with memory allocation information.
10109 The first, with extension .map
10110 \begin_inset LatexCommand \index{<file>.map}
10114 shows all the variables and segments.
10115 The second with extension .mem
10116 \begin_inset LatexCommand \index{<file>.mem}
10120 shows the final memory layout.
10121 The linker will complain either if memory segments overlap, there is not
10122 enough memory, or there is not enough space for stack.
10123 If you get any linking warnings and/or errors related to stack or segments
10124 allocation, take a look at either the .map or .mem files to find out what
10126 The .mem file may even suggest a solution to the problem.
10129 Z80/Z180 Storage Class
10130 \begin_inset LatexCommand \index{Storage class}
10134 Language Extensions
10135 \layout Subsubsection
10138 \begin_inset LatexCommand \index{sfr}
10143 \begin_inset LatexCommand \index{\_\_sfr}
10147 (in/out to 8-bit addresses)
10151 \begin_inset LatexCommand \index{Z80}
10155 family has separate address spaces for memory and
10165 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10169 is accessed with special instructions, e.g.:
10174 sfr at 0x78 IoPort;\SpecialChar ~
10176 /* define a var in I/O space at 78h called IoPort */
10180 Writing 0x01 to this variable generates the assembly code:
10185 3E 01\SpecialChar ~
10193 D3 78\SpecialChar ~
10200 \layout Subsubsection
10203 \begin_inset LatexCommand \index{sfr}
10208 \begin_inset LatexCommand \index{\_\_sfr}
10212 (in/out to 16-bit addresses)
10219 is used to support 16 bit addresses in I/O memory e.g.:
10225 \begin_inset LatexCommand \index{at}
10230 \begin_inset LatexCommand \index{\_\_at}
10237 Writing 0x01 to this variable generates the assembly code:
10242 01 23 01\SpecialChar ~
10247 3E 01\SpecialChar ~
10255 ED 79\SpecialChar ~
10262 \layout Subsubsection
10265 \begin_inset LatexCommand \index{sfr}
10270 \begin_inset LatexCommand \index{\_\_sfr}
10274 (in0/out0 to 8 bit addresses on Z180
10275 \begin_inset LatexCommand \index{Z180}
10280 \begin_inset LatexCommand \index{HD64180}
10287 The compiler option -
10297 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
10299 \begin_inset LatexCommand \index{\#pragma portmode}
10303 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10313 If you include the file z180.h this will be set automatically.
10317 \begin_inset LatexCommand \index{Storage class}
10321 Language Extensions
10322 \layout Subsubsection
10325 \begin_inset LatexCommand \index{data (hc08 storage class)}
10330 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10337 The data storage class declares a variable that resides in the first 256
10338 bytes of memory (the direct page).
10339 The HC08 is most efficient at accessing variables (especially pointers)
10341 \layout Subsubsection
10344 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10349 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10356 The xdata storage class declares a variable that can reside anywhere in
10358 This is the default if no storage class is specified.
10362 Absolute Addressing
10363 \begin_inset LatexCommand \index{Absolute addressing}
10370 Data items can be assigned an absolute address with the
10373 \begin_inset LatexCommand \index{at}
10378 \begin_inset LatexCommand \index{\_\_at}
10384 keyword, in addition to a storage class, e.g.:
10390 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10395 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10400 \begin_inset LatexCommand \index{at}
10405 \begin_inset LatexCommand \index{\_\_at}
10409 0x7ffe unsigned int chksum;
10412 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10413 of the external ram.
10418 reserve any space for variables declared in this way (they are implemented
10419 with an equate in the assembler).
10420 Thus it is left to the programmer to make sure there are no overlaps with
10421 other variables that are declared without the absolute address.
10422 The assembler listing file (.lst
10423 \begin_inset LatexCommand \index{<file>.lst}
10427 ) and the linker output files (.rst
10428 \begin_inset LatexCommand \index{<file>.rst}
10433 \begin_inset LatexCommand \index{<file>.map}
10437 ) are good places to look for such overlaps.
10438 Variables with an absolute address are
10443 \begin_inset LatexCommand \index{Variable initialization}
10450 In case of memory mapped I/O devices the keyword
10454 has to be used to tell the compiler that accesses might not be removed:
10460 \begin_inset LatexCommand \index{volatile}
10465 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10470 \begin_inset LatexCommand \index{at}
10474 0x8000 unsigned char PORTA_8255;
10477 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10482 \begin_inset LatexCommand \index{Aligned array}
10489 starts at a block (256 byte) boundary
10490 \begin_inset LatexCommand \index{block boundary}
10495 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10501 Absolute addresses can be specified for variables in all storage classes,
10508 \begin_inset LatexCommand \index{bit}
10513 \begin_inset LatexCommand \index{at}
10520 The above example will allocate the variable at offset 0x02 in the bit-addressab
10522 There is no real advantage to assigning absolute addresses to variables
10523 in this manner, unless you want strict control over all the variables allocated.
10524 One possible use would be to write hardware portable code.
10525 For example, if you have a routine that uses one or more of the microcontroller
10526 I/O pins, and such pins are different for two different hardwares, you
10527 can declare the I/O pins in your routine using:
10533 \begin_inset LatexCommand \index{volatile}
10537 bit MOSI;\SpecialChar ~
10541 /* master out, slave in */
10543 extern volatile bit MISO;\SpecialChar ~
10547 /* master in, slave out */
10549 extern volatile bit MCLK;\SpecialChar ~
10557 /* Input and Output of a byte on a 3-wire serial bus.
10562 If needed adapt polarity of clock, polarity of data and bit order
10567 unsigned char spi_io(unsigned char out_byte)
10591 MOSI = out_byte & 0x80;
10621 /* _asm nop _endasm; */\SpecialChar ~
10629 /* for slow peripherals */
10680 Then, someplace in the code for the first hardware you would use
10686 \begin_inset LatexCommand \index{at}
10691 \begin_inset LatexCommand \index{\_\_at}
10695 0x80 MOSI;\SpecialChar ~
10699 /* I/O port 0, bit 0 */
10701 bit at 0x81 MISO;\SpecialChar ~
10705 /* I/O port 0, bit 1 */
10707 bit at 0x82 MCLK;\SpecialChar ~
10711 /* I/O port 0, bit 2 */
10714 Similarly, for the second hardware you would use
10719 bit at 0x83 MOSI;\SpecialChar ~
10723 /* I/O port 0, bit 3 */
10725 bit at 0x91 MISO;\SpecialChar ~
10729 /* I/O port 1, bit 1 */
10732 \begin_inset LatexCommand \index{bit}
10736 at 0x92 MCLK;\SpecialChar ~
10740 /* I/O port 1, bit 2 */
10743 and you can use the same hardware dependent routine without changes, as
10744 for example in a library.
10745 This is somehow similar to sbit, but only one absolute address has to be
10746 specified in the whole project.
10750 \begin_inset LatexCommand \index{Parameters}
10755 \begin_inset LatexCommand \index{function parameter}
10760 \begin_inset LatexCommand \index{local variables}
10765 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10772 Automatic (local) variables and parameters to functions can either be placed
10773 on the stack or in data-space.
10774 The default action of the compiler is to place these variables in the internal
10775 RAM (for small model) or external RAM (for large model).
10776 This in fact makes them similar to
10779 \begin_inset LatexCommand \index{static}
10785 so by default functions are non-reentrant
10786 \begin_inset LatexCommand \index{reentrant}
10795 They can be placed on the stack
10796 \begin_inset LatexCommand \index{stack}
10813 \begin_inset LatexCommand \index{-\/-stack-auto}
10821 #pragma\SpecialChar ~
10825 \begin_inset LatexCommand \index{\#pragma stackauto}
10832 \begin_inset LatexCommand \index{reentrant}
10838 keyword in the function declaration, e.g.:
10843 unsigned char foo(char i) reentrant
10857 Since stack space on 8051 is limited, the
10875 option should be used sparingly.
10876 Note that the reentrant keyword just means that the parameters & local
10877 variables will be allocated to the stack, it
10881 mean that the function is register bank
10882 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10891 \begin_inset LatexCommand \index{local variables}
10895 can be assigned storage classes and absolute
10896 \begin_inset LatexCommand \index{Absolute addressing}
10905 unsigned char foo()
10913 xdata unsigned char i;
10926 \begin_inset LatexCommand \index{at}
10930 0x31 unsigned char j;
10942 In the above example the variable
10946 will be allocated in the external ram,
10950 in bit addressable space and
10969 or when a function is declared as
10973 this should only be done for static variables.
10977 \begin_inset LatexCommand \index{function parameter}
10981 however are not allowed any storage class
10982 \begin_inset LatexCommand \index{Storage class}
10986 , (storage classes for parameters will be ignored), their allocation is
10987 governed by the memory model in use, and the reentrancy options.
10990 It is however allowed to use bit parameters in reentrant functions and also
10991 non-static local bit variables are supported.
10992 Efficient use is limited to 8 semi-bitregisters in bit space.
10993 They are pushed and popped to stack as a single byte just like the normal
10998 \begin_inset LatexCommand \label{sub:Overlaying}
11003 \begin_inset LatexCommand \index{Overlaying}
11011 \begin_inset LatexCommand \index{reentrant}
11015 functions SDCC will try to reduce internal ram space usage by overlaying
11016 parameters and local variables of a function (if possible).
11017 Parameters and local variables
11018 \begin_inset LatexCommand \index{local variables}
11022 of a function will be allocated to an overlayable segment if the function
11025 no other function calls and the function is non-reentrant and the memory
11027 \begin_inset LatexCommand \index{Memory model}
11034 If an explicit storage class
11035 \begin_inset LatexCommand \index{Storage class}
11039 is specified for a local variable, it will NOT be overlayed.
11042 Note that the compiler (not the linkage editor) makes the decision for overlayin
11044 Functions that are called from an interrupt service routine should be preceded
11045 by a #pragma\SpecialChar ~
11047 \begin_inset LatexCommand \index{\#pragma nooverlay}
11051 if they are not reentrant.
11054 Also note that the compiler does not do any processing of inline assembler
11055 code, so the compiler might incorrectly assign local variables and parameters
11056 of a function into the overlay segment if the inline assembler code calls
11057 other c-functions that might use the overlay.
11058 In that case the #pragma\SpecialChar ~
11059 nooverlay should be used.
11062 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11064 \begin_inset LatexCommand \index{Multiplication}
11069 \begin_inset LatexCommand \index{Division}
11073 will NOT be overlayed since these are implemented using external functions,
11082 \begin_inset LatexCommand \index{\#pragma nooverlay}
11088 void set_error(unsigned char errcd)
11104 void some_isr () interrupt
11105 \begin_inset LatexCommand \index{interrupt}
11135 In the above example the parameter
11143 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11145 not present, this could cause unpredictable runtime behavior when called
11146 from an interrupt service routine.
11147 The #pragma\SpecialChar ~
11148 nooverlay ensures that the parameters and local variables for
11149 the function are NOT overlayed.
11152 Interrupt Service Routines
11153 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11160 General Information
11175 outines to be coded in C, with some extended keywords.
11180 void timer_isr (void) interrupt 1 using 1
11194 The optional number following the
11197 \begin_inset LatexCommand \index{interrupt}
11202 \begin_inset LatexCommand \index{\_\_interrupt}
11208 keyword is the interrupt number this routine will service.
11209 When present, the compiler will insert a call to this routine in the interrupt
11210 vector table for the interrupt number specified.
11211 If you have multiple source files in your project, interrupt service routines
11212 can be present in any of them, but a prototype of the isr MUST be present
11213 or included in the file that contains the function
11221 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11226 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11232 keyword can be used to tell the compiler to use the specified register
11233 bank (8051 specific) when generating code for this function.
11239 Interrupt service routines open the door for some very interesting bugs:
11241 If an interrupt service routine changes variables which are accessed by
11242 other functions these variables have to be declared
11247 \begin_inset LatexCommand \index{volatile}
11255 If the access to these variables is not
11258 \begin_inset LatexCommand \index{atomic}
11265 the processor needs more than one instruction for the access and could
11266 be interrupted while accessing the variable) the interrupt must be disabled
11267 during the access to avoid inconsistent data.
11268 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
11269 and should be protected by disabling interrupts.
11270 You're not automatically on the safe side if you use 8 bit variables though.
11271 We need an example here: f.e.
11272 on the 8051 the harmless looking
11273 \begin_inset Quotes srd
11278 flags\SpecialChar ~
11283 \begin_inset Quotes sld
11292 \begin_inset Quotes srd
11297 flags\SpecialChar ~
11302 \begin_inset Quotes sld
11305 from within an interrupt routine might get lost if the interrupt occurs
11308 \begin_inset Quotes sld
11313 counter\SpecialChar ~
11318 \begin_inset Quotes srd
11321 is not atomic on the 8051 even if
11325 is located in data memory.
11326 Bugs like these are hard to reproduce and can cause a lot of trouble.
11330 The return address and the registers used in the interrupt service routine
11331 are saved on the stack
11332 \begin_inset LatexCommand \index{stack}
11336 so there must be sufficient stack space.
11337 If there isn't variables or registers (or even the return address itself)
11344 \begin_inset LatexCommand \index{stack overflow}
11348 is most likely to happen if the interrupt occurs during the
11349 \begin_inset Quotes sld
11353 \begin_inset Quotes srd
11356 subroutine when the stack is already in use for f.e.
11357 many return addresses.
11360 A special note here, int (16 bit) and long (32 bit) integer division
11361 \begin_inset LatexCommand \index{Division}
11366 \begin_inset LatexCommand \index{Multiplication}
11371 \begin_inset LatexCommand \index{Modulus}
11376 \begin_inset LatexCommand \index{Floating point support}
11380 operations are implemented using external support routines developed in
11382 If an interrupt service routine needs to do any of these operations then
11383 the support routines (as mentioned in a following section) will have to
11384 be recompiled using the
11397 \begin_inset LatexCommand \index{-\/-stack-auto}
11403 option and the source file will need to be compiled using the
11418 \begin_inset LatexCommand \index{-\/-int-long-reent}
11425 Calling other functions from an interrupt service routine is not recommended,
11426 avoid it if possible.
11427 Note that when some function is called from an interrupt service routine
11428 it should be preceded by a #pragma\SpecialChar ~
11430 \begin_inset LatexCommand \index{\#pragma nooverlay}
11434 if it is not reentrant.
11435 Furthermore nonreentrant functions should not be called from the main program
11436 while the interrupt service routine might be active.
11437 They also must not be called from low priority interrupt service routines
11438 while a high priority interrupt service routine might be active.
11439 You could use semaphores or make the function
11443 if all parameters are passed in registers.
11448 \begin_inset LatexCommand \ref{sub:Overlaying}
11453 about Overlaying and section
11454 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11459 about Functions using private register banks.
11462 MCS51/DS390 Interrupt Service Routines
11465 Interrupt numbers and the corresponding address & descriptions for the Standard
11466 8051/8052 are listed below.
11467 SDCC will automatically adjust the interrupt vector table to the maximum
11468 interrupt number specified.
11474 \begin_inset Tabular
11475 <lyxtabular version="3" rows="7" columns="3">
11477 <column alignment="center" valignment="top" leftline="true" width="0in">
11478 <column alignment="center" valignment="top" leftline="true" width="0in">
11479 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
11480 <row topline="true" bottomline="true">
11481 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11489 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11497 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11506 <row topline="true">
11507 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11532 <row topline="true">
11533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11558 <row topline="true">
11559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11567 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11575 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11584 <row topline="true">
11585 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11601 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11610 <row topline="true">
11611 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11636 <row topline="true" bottomline="true">
11637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11671 If the interrupt service routine is defined without
11674 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11679 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11685 a register bank or with register bank 0 (
11689 0), the compiler will save the registers used by itself on the stack upon
11690 entry and restore them at exit, however if such an interrupt service routine
11691 calls another function then the entire register bank will be saved on the
11693 This scheme may be advantageous for small interrupt service routines which
11694 have low register usage.
11697 If the interrupt service routine is defined to be using a specific register
11702 & psw are saved and restored, if such an interrupt service routine calls
11703 another function (using another register bank) then the entire register
11704 bank of the called function will be saved on the stack.
11705 This scheme is recommended for larger interrupt service routines.
11708 HC08 Interrupt Service Routines
11711 Since the number of interrupts available is chip specific and the interrupt
11712 vector table always ends at the last byte of memory, the interrupt numbers
11713 corresponds to the interrupt vectors in reverse order of address.
11714 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11715 2 will use the interrupt vector at 0xfffa, and so on.
11716 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11717 this way; instead see section
11718 \begin_inset LatexCommand \ref{sub:Startup-Code}
11722 for details on customizing startup.
11725 Z80 Interrupt Service Routines
11728 The Z80 uses several different methods for determining the correct interrupt
11729 vector depending on the hardware implementation.
11730 Therefore, SDCC ignores the optional interrupt number and does not attempt
11731 to generate an interrupt vector table.
11734 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11735 instruction to return from the interrupt.
11736 To write an interrupt handler for the non-maskable interrupt, which needs
11737 a RETN instruction instead, add the
11746 void nmi_isr (void) critical interrupt
11760 However if you need to create a non-interruptable interrupt service routine
11761 you would also require the
11766 To distinguish between this and an nmi_isr you must provide an interrupt
11770 Enabling and Disabling Interrupts
11773 Critical Functions and Critical Statements
11776 A special keyword may be associated with a block or a function declaring
11782 SDCC will generate code to disable all interrupts
11783 \begin_inset LatexCommand \index{interrupt}
11787 upon entry to a critical function and restore the interrupt enable to the
11788 previous state before returning.
11789 Nesting critical functions will need one additional byte on the stack
11790 \begin_inset LatexCommand \index{stack}
11799 int foo () critical
11800 \begin_inset LatexCommand \index{critical}
11805 \begin_inset LatexCommand \index{\_\_critical}
11830 The critical attribute maybe used with other attributes like
11840 may also be used to disable interrupts more locally:
11848 More than one statement could have been included in the block.
11851 Enabling and Disabling Interrupts directly
11855 \begin_inset LatexCommand \index{interrupt}
11859 can also be disabled and enabled directly (8051):
11864 EA = 0;\SpecialChar ~
11927 EA = 1;\SpecialChar ~
11994 On other architectures which have seperate opcodes for enabling and disabling
11995 interrupts you might want to make use of defines with inline assembly
11996 \begin_inset LatexCommand \index{Assembler routines}
12006 \begin_inset LatexCommand \index{\_asm}
12015 \begin_inset LatexCommand \index{\_endasm}
12024 #define SEI _asm\SpecialChar ~
12036 Note: it is sometimes sufficient to disable only a specific interrupt source
12038 a timer or serial interrupt by manipulating an
12041 \begin_inset LatexCommand \index{interrupt mask}
12051 Usually the time during which interrupts are disabled should be kept as
12053 This minimizes both
12058 \begin_inset LatexCommand \index{interrupt latency}
12062 (the time between the occurrence of the interrupt and the execution of
12063 the first code in the interrupt routine) and
12068 \begin_inset LatexCommand \index{interrupt jitter}
12072 (the difference between the shortest and the longest interrupt latency).
12073 These really are something different, f.e.
12074 a serial interrupt has to be served before its buffer overruns so it cares
12075 for the maximum interrupt latency, whereas it does not care about jitter.
12076 On a loudspeaker driven via a digital to analog converter which is fed
12077 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12078 a much smaller jitter will be very audible.
12081 You can reenable interrupts within an interrupt routine and on some architecture
12082 s you can make use of two (or more) levels of
12084 interrupt priorities
12087 \begin_inset LatexCommand \index{interrupt priority}
12092 On some architectures which don't support interrupt priorities these can
12093 be implemented by manipulating the interrupt mask and reenabling interrupts
12094 within the interrupt routine.
12095 Check there is sufficient space on the stack
12096 \begin_inset LatexCommand \index{stack}
12100 and don't add complexity unless you have to.
12105 \begin_inset LatexCommand \index{semaphore}
12109 locking (mcs51/ds390)
12112 Some architectures (mcs51/ds390) have an atomic
12113 \begin_inset LatexCommand \index{atomic}
12126 These type of instructions are typically used in preemptive multitasking
12127 systems, where a routine f.e.
12128 claims the use of a data structure ('acquires a lock
12129 \begin_inset LatexCommand \index{lock}
12133 on it'), makes some modifications and then releases the lock when the data
12134 structure is consistent again.
12135 The instruction may also be used if interrupt and non-interrupt code have
12136 to compete for a resource.
12137 With the atomic bit test and clear instruction interrupts
12138 \begin_inset LatexCommand \index{interrupt}
12142 don't have to be disabled for the locking operation.
12146 SDCC generates this instruction if the source follows this pattern:
12152 \begin_inset LatexCommand \index{volatile}
12156 bit resource_is_free;
12160 if (resource_is_free)
12170 resource_is_free=0;
12183 resource_is_free=1;
12190 Note, mcs51 and ds390 support only an atomic
12191 \begin_inset LatexCommand \index{atomic}
12199 instruction (as opposed to atomic bit test and
12204 Functions using private register banks
12205 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12212 Some architectures have support for quickly changing register sets.
12213 SDCC supports this feature with the
12216 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12221 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12227 attribute (which tells the compiler to use a register bank
12228 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12232 other than the default bank zero).
12233 It should only be applied to
12236 \begin_inset LatexCommand \index{interrupt}
12242 functions (see footnote below).
12243 This will in most circumstances make the generated ISR code more efficient
12244 since it will not have to save registers on the stack.
12251 attribute will have no effect on the generated code for a
12255 function (but may occasionally be useful anyway
12261 possible exception: if a function is called ONLY from 'interrupt' functions
12262 using a particular bank, it can be declared with the same 'using' attribute
12263 as the calling 'interrupt' functions.
12264 For instance, if you have several ISRs using bank one, and all of them
12265 call memcpy(), it might make sense to create a specialized version of memcpy()
12266 'using 1', since this would prevent the ISR from having to save bank zero
12267 to the stack on entry and switch to bank zero before calling the function
12274 (pending: I don't think this has been done yet)
12281 function using a non-zero bank will assume that it can trash that register
12282 bank, and will not save it.
12283 Since high-priority interrupts
12284 \begin_inset LatexCommand \index{interrupts}
12289 \begin_inset LatexCommand \index{interrupt priority}
12293 can interrupt low-priority ones on the 8051 and friends, this means that
12294 if a high-priority ISR
12298 a particular bank occurs while processing a low-priority ISR
12302 the same bank, terrible and bad things can happen.
12303 To prevent this, no single register bank should be
12307 by both a high priority and a low priority ISR.
12308 This is probably most easily done by having all high priority ISRs use
12309 one bank and all low priority ISRs use another.
12310 If you have an ISR which can change priority at runtime, you're on your
12311 own: I suggest using the default bank zero and taking the small performance
12315 It is most efficient if your ISR calls no other functions.
12316 If your ISR must call other functions, it is most efficient if those functions
12317 use the same bank as the ISR (see note 1 below); the next best is if the
12318 called functions use bank zero.
12319 It is very inefficient to call a function using a different, non-zero bank
12325 \begin_inset LatexCommand \label{sub:Startup-Code}
12330 \begin_inset LatexCommand \index{Startup code}
12337 MCS51/DS390 Startup Code
12340 The compiler inserts a call to the C routine
12342 _sdcc_external_startup()
12343 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12352 at the start of the CODE area.
12353 This routine is in the runtime library
12354 \begin_inset LatexCommand \index{Runtime library}
12359 By default this routine returns 0, if this routine returns a non-zero value,
12360 the static & global variable initialization will be skipped and the function
12361 main will be invoked.
12362 Otherwise static & global variables will be initialized before the function
12366 _sdcc_external_startup()
12368 routine to your program to override the default if you need to setup hardware
12369 or perform some other critical operation prior to static & global variable
12371 \begin_inset LatexCommand \index{Variable initialization}
12376 On some mcs51 variants xdata
12377 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12381 memory has to be explicitly enabled before it can be accessed or if the
12382 watchdog needs to be disabled, this is the place to do it.
12383 The startup code clears all internal data memory, 256 bytes by default,
12384 but from 0 to n-1 if
12397 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12404 (recommended for Chipcon CC1010).
12407 See also the compiler option
12426 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12431 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12436 about MCS51-variants.
12442 The HC08 startup code follows the same scheme as the MCS51 startup code.
12448 On the Z80 the startup code is inserted by linking with crt0.o which is generated
12449 from sdcc/device/lib/z80/crt0.s.
12450 If you need a different startup code you can use the compiler option
12471 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12475 and provide your own crt0.o.
12479 Inline Assembler Code
12480 \begin_inset LatexCommand \index{Assembler routines}
12487 A Step by Step Introduction
12488 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12495 Starting from a small snippet of c-code this example shows for the MCS51
12496 how to use inline assembly, access variables, a function parameter and
12497 an array in xdata memory.
12498 The example uses an MCS51 here but is easily adapted for other architectures.
12499 This is a buffer routine which should be optimized:
12506 \begin_inset LatexCommand \index{far (storage class)}
12511 \begin_inset LatexCommand \index{\_\_far (storage class)}
12516 \begin_inset LatexCommand \index{at}
12521 \begin_inset LatexCommand \index{\_\_at}
12526 \begin_inset LatexCommand \index{Aligned array}
12532 unsigned char head,tail;
12536 void to_buffer( unsigned char c )
12544 if( head != tail-1 )
12554 buf[ head++ ] = c;\SpecialChar ~
12558 /* access to a 256 byte aligned array */
12563 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12564 then a corresponding buffer.asm file is generated.
12565 We define a new function
12569 in file buffer.c in which we cut and paste the generated code, removing
12570 unwanted comments and some ':'.
12572 \begin_inset Quotes sld
12576 \begin_inset Quotes srd
12580 \begin_inset Quotes sld
12584 \begin_inset Quotes srd
12587 to the beginning and the end of the function body:
12593 /* With a cut and paste from the .asm file, we have something to start with.
12598 The function is not yet OK! (registers aren't saved) */
12600 void to_buffer_asm( unsigned char c )
12609 \begin_inset LatexCommand \index{\_asm}
12614 \begin_inset LatexCommand \index{\_\_asm}
12628 ;buffer.c if( head != tail-1 )
12676 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12677 \begin_inset LatexCommand \index{Aligned array}
12742 \begin_inset LatexCommand \index{\_endasm}
12747 \begin_inset LatexCommand \index{\_\_endasm}
12756 The new file buffer.c should compile with only one warning about the unreferenced
12757 function argument 'c'.
12758 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12759 (1) and finally have:
12765 unsigned char far at 0x7f00 buf[0x100];
12767 unsigned char head,tail;
12769 #define USE_ASSEMBLY (1)
12777 void to_buffer( unsigned char c )
12785 if( head != tail-1 )
12805 void to_buffer( unsigned char c )
12813 c; // to avoid warning: unreferenced function argument
12820 \begin_inset LatexCommand \index{\_asm}
12825 \begin_inset LatexCommand \index{\_\_asm}
12839 ; save used registers here.
12850 ; If we were still using r2,r3 we would have to push them here.
12853 ; if( head != tail-1 )
12896 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12920 ; buf[ head++ ] = c;
12931 a,dpl \SpecialChar ~
12938 ; dpl holds lower byte of function argument
12949 dpl,_head \SpecialChar ~
12952 ; buf is 0x100 byte aligned so head can be used directly
12994 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13006 ; restore used registers here
13013 \begin_inset LatexCommand \index{\_endasm}
13018 \begin_inset LatexCommand \index{\_\_endasm}
13029 The inline assembler code can contain any valid code understood by the assembler
13030 , this includes any assembler directives and comment lines
13036 The assembler does not like some characters like ':' or ''' in comments.
13037 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13038 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13043 \begin_inset LatexCommand \index{Assembler documentation}
13051 The compiler does not do any validation of the code within the
13054 \begin_inset LatexCommand \index{\_asm}
13059 \begin_inset LatexCommand \index{\_\_asm}
13067 \begin_inset LatexCommand \index{\_endasm}
13072 \begin_inset LatexCommand \index{\_\_endasm}
13081 Specifically it will not know which registers are used and thus register
13083 \begin_inset LatexCommand \index{push/pop}
13087 has to be done manually.
13091 It is recommended that each assembly instruction (including labels) be placed
13092 in a separate line (as the example shows).
13106 \begin_inset LatexCommand \index{-\/-peep-asm}
13112 command line option is used, the inline assembler code will be passed through
13113 the peephole optimizer
13114 \begin_inset LatexCommand \index{Peephole optimizer}
13119 There are only a few (if any) cases where this option makes sense, it might
13120 cause some unexpected changes in the inline assembler code.
13121 Please go through the peephole optimizer rules defined in file
13125 before using this option.
13129 \begin_inset LatexCommand \label{sub:Naked-Functions}
13134 \begin_inset LatexCommand \index{Naked functions}
13141 A special keyword may be associated with a function declaring it as
13144 \begin_inset LatexCommand \index{\_naked}
13149 \begin_inset LatexCommand \index{\_\_naked}
13160 function modifier attribute prevents the compiler from generating prologue
13161 \begin_inset LatexCommand \index{function prologue}
13166 \begin_inset LatexCommand \index{function epilogue}
13170 code for that function.
13171 This means that the user is entirely responsible for such things as saving
13172 any registers that may need to be preserved, selecting the proper register
13173 bank, generating the
13177 instruction at the end, etc.
13178 Practically, this means that the contents of the function must be written
13179 in inline assembler.
13180 This is particularly useful for interrupt functions, which can have a large
13181 (and often unnecessary) prologue/epilogue.
13182 For example, compare the code generated by these two functions:
13188 \begin_inset LatexCommand \index{volatile}
13192 data unsigned char counter;
13196 void simpleInterrupt(void) interrupt
13197 \begin_inset LatexCommand \index{interrupt}
13202 \begin_inset LatexCommand \index{\_\_interrupt}
13220 void nakedInterrupt(void) interrupt 2 _naked
13229 \begin_inset LatexCommand \index{\_asm}
13234 \begin_inset LatexCommand \index{\_\_asm}
13251 _counter ; does not change flags, no need to save psw
13263 ; MUST explicitly include ret or reti in _naked function.
13270 \begin_inset LatexCommand \index{\_endasm}
13275 \begin_inset LatexCommand \index{\_\_endasm}
13284 For an 8051 target, the generated simpleInterrupt looks like:
13425 whereas nakedInterrupt looks like:
13440 _counter ; does not change flags, no need to save psw
13458 ; MUST explicitly include ret or reti in _naked function
13461 The related directive #pragma exclude
13462 \begin_inset LatexCommand \index{\#pragma exclude}
13466 allows a more fine grained control over pushing & popping
13467 \begin_inset LatexCommand \index{push/pop}
13474 While there is nothing preventing you from writing C code inside a
13478 function, there are many ways to shoot yourself in the foot doing this,
13479 and it is recommended that you stick to inline assembler.
13482 Use of Labels within Inline Assembler
13485 SDCC allows the use of in-line assembler with a few restrictions regarding
13487 In older versions of the compiler all labels defined within inline assembler
13496 where nnnn is a number less than 100 (which implies a limit of utmost 100
13497 inline assembler labels
13511 \begin_inset LatexCommand \index{\_asm}
13516 \begin_inset LatexCommand \index{\_\_asm}
13546 \begin_inset LatexCommand \index{\_endasm}
13551 \begin_inset LatexCommand \index{\_\_endasm}
13558 Inline assembler code cannot reference any C-Labels, however it can reference
13560 \begin_inset LatexCommand \index{Labels}
13564 defined by the inline assembler, e.g.:
13589 ; some assembler code
13609 /* some more c code */
13611 clabel:\SpecialChar ~
13613 /* inline assembler cannot reference this label */
13625 $0003: ;label (can be referenced by inline assembler only)
13632 \begin_inset LatexCommand \index{\_endasm}
13637 \begin_inset LatexCommand \index{\_\_endasm}
13647 /* some more c code */
13652 In other words inline assembly code can access labels defined in inline
13653 assembly within the scope of the function.
13654 The same goes the other way, i.e.
13655 labels defines in inline assembly can not be accessed by C statements.
13658 Interfacing with Assembler Code
13659 \begin_inset LatexCommand \index{Assembler routines}
13666 Global Registers used for Parameter Passing
13667 \begin_inset LatexCommand \index{Parameter passing}
13674 The compiler always uses the global registers
13677 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13682 \begin_inset LatexCommand \index{DPTR}
13687 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13696 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13702 to pass the first parameter to a routine.
13703 The second parameter onwards is either allocated on the stack (for reentrant
13714 -stack-auto is used) or in data / xdata memory (depending on the memory
13719 Assembler Routine (non-reentrant)
13722 In the following example
13723 \begin_inset LatexCommand \index{reentrant}
13728 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13732 the function c_func calls an assembler routine asm_func, which takes two
13734 \begin_inset LatexCommand \index{function parameter}
13743 extern int asm_func(unsigned char, unsigned char);
13747 int c_func (unsigned char i, unsigned char j)
13755 return asm_func(i,j);
13769 return c_func(10,9);
13774 The corresponding assembler function is:
13779 .globl _asm_func_PARM_2
13880 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13897 Note here that the return values
13898 \begin_inset LatexCommand \index{return value}
13902 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13904 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13905 b' & 'acc' for four byte values.
13908 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13909 the parameter number starting from 1, and counting from the left.
13910 The first parameter is passed in
13911 \begin_inset Quotes eld
13915 \begin_inset Quotes erd
13918 for a one byte parameter,
13919 \begin_inset Quotes eld
13923 \begin_inset Quotes erd
13927 \begin_inset Quotes eld
13931 \begin_inset Quotes erd
13934 for three bytes and
13935 \begin_inset Quotes eld
13939 \begin_inset Quotes erd
13942 for a four bytes parameter.
13943 The variable name for the second parameter will be _<function_name>_PARM_2.
13947 Assemble the assembler routine with the following command:
13954 asx8051 -losg asmfunc.asm
13961 Then compile and link the assembler routine to the C source file with the
13969 sdcc cfunc.c asmfunc.rel
13972 Assembler Routine (reentrant)
13976 \begin_inset LatexCommand \index{reentrant}
13981 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13985 the second parameter
13986 \begin_inset LatexCommand \index{function parameter}
13990 onwards will be passed on the stack, the parameters are pushed from right
13992 after the call the leftmost parameter will be on the top of the stack.
13993 Here is an example:
13998 extern int asm_func(unsigned char, unsigned char);
14002 int c_func (unsigned char i, unsigned char j) reentrant
14010 return asm_func(i,j);
14024 return c_func(10,9);
14029 The corresponding assembler routine is:
14129 The compiling and linking procedure remains the same, however note the extra
14130 entry & exit linkage required for the assembler code, _bp is the stack
14131 frame pointer and is used to compute the offset into the stack for parameters
14132 and local variables.
14136 \begin_inset LatexCommand \index{int (16 bit)}
14141 \begin_inset LatexCommand \index{long (32 bit)}
14148 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14149 multiplication and modulus operations are implemented by support routines.
14150 These support routines are all developed in ANSI-C to facilitate porting
14151 to other MCUs, although some model specific assembler optimizations are
14153 The following files contain the described routines, all of them can be
14154 found in <installdir>/share/sdcc/lib.
14160 \begin_inset Tabular
14161 <lyxtabular version="3" rows="11" columns="2">
14163 <column alignment="center" valignment="top" leftline="true" width="0">
14164 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14165 <row topline="true" bottomline="true">
14166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14176 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14187 <row topline="true">
14188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14196 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14201 16 bit multiplication
14205 <row topline="true">
14206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14214 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14219 signed 16 bit division (calls _divuint)
14223 <row topline="true">
14224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14237 unsigned 16 bit division
14241 <row topline="true">
14242 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14250 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14255 signed 16 bit modulus (calls _moduint)
14259 <row topline="true">
14260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14268 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14273 unsigned 16 bit modulus
14277 <row topline="true">
14278 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14286 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14291 32 bit multiplication
14295 <row topline="true">
14296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14304 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14309 signed 32 division (calls _divulong)
14313 <row topline="true">
14314 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14322 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14327 unsigned 32 division
14331 <row topline="true">
14332 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14340 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14345 signed 32 bit modulus (calls _modulong)
14349 <row topline="true" bottomline="true">
14350 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14358 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14363 unsigned 32 bit modulus
14376 Since they are compiled as
14381 \begin_inset LatexCommand \index{reentrant}
14386 \begin_inset LatexCommand \index{interrupt}
14390 service routines should not do any of the above operations.
14391 If this is unavoidable then the above routines will need to be compiled
14405 \begin_inset LatexCommand \index{-\/-stack-auto}
14411 option, after which the source program will have to be compiled with
14424 \begin_inset LatexCommand \index{-\/-int-long-reent}
14431 Notice that you don't have to call these routines directly.
14432 The compiler will use them automatically every time an integer operation
14436 Floating Point Support
14437 \begin_inset LatexCommand \index{Floating point support}
14444 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
14445 floating point support routines are derived from gcc's floatlib.c and consist
14446 of the following routines:
14454 \begin_inset Tabular
14455 <lyxtabular version="3" rows="17" columns="2">
14457 <column alignment="center" valignment="top" leftline="true" width="0">
14458 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14459 <row topline="true" bottomline="true">
14460 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14477 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14486 <row topline="true">
14487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14504 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14518 add floating point numbers
14522 <row topline="true">
14523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14554 subtract floating point numbers
14558 <row topline="true">
14559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14576 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14590 divide floating point numbers
14594 <row topline="true">
14595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14612 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14626 multiply floating point numbers
14630 <row topline="true">
14631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14648 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14662 convert floating point to unsigned char
14666 <row topline="true">
14667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14698 convert floating point to signed char
14702 <row topline="true">
14703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14734 convert floating point to unsigned int
14738 <row topline="true">
14739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14770 convert floating point to signed int
14774 <row topline="true">
14775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14815 convert floating point to unsigned long
14819 <row topline="true">
14820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14837 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14851 convert floating point to signed long
14855 <row topline="true">
14856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14873 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14887 convert unsigned char to floating point
14891 <row topline="true">
14892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14909 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14923 convert char to floating point number
14927 <row topline="true">
14928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14945 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14959 convert unsigned int to floating point
14963 <row topline="true">
14964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14981 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14995 convert int to floating point numbers
14999 <row topline="true">
15000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15017 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15031 convert unsigned long to floating point number
15035 <row topline="true" bottomline="true">
15036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15053 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15067 convert long to floating point number
15080 These support routines are developed in ANSI-C so there is room for space
15081 and speed improvement
15087 The floating point routines for the mcs51 are implemented in assembler
15091 Note if all these routines are used simultaneously the data space might
15093 For serious floating point usage the large model might be needed.
15094 Also notice that you don't have to call this routines directly.
15095 The compiler will use them automatically every time a floating point operation
15100 \begin_inset LatexCommand \index{Libraries}
15109 <pending: this is messy and incomplete - a little more information is in
15110 sdcc/doc/libdoc.txt
15115 Compiler support routines (_gptrget, _mulint etc.)
15118 Stdclib functions (puts, printf, strcat etc.)
15119 \layout Subsubsection
15125 \begin_inset LatexCommand \index{<stdio.h>}
15129 As usual on embedded systems you have to provide your own
15132 \begin_inset LatexCommand \index{getchar()}
15141 \begin_inset LatexCommand \index{putchar()}
15148 SDCC does not know whether the system connects to a serial line with or
15149 without handshake, LCD, keyboard or other device.
15150 You'll find examples for serial routines f.e.
15151 in sdcc/device/lib.
15157 \begin_inset LatexCommand \index{printf()}
15167 does not support float (except on ds390).
15168 To enable this recompile it with the option
15181 \begin_inset LatexCommand \index{USE\_FLOATS}
15187 on the command line.
15201 \begin_inset LatexCommand \index{-\/-model-large}
15207 for the mcs51 port, since this uses a lot of memory.
15210 If you're short on memory you might want to use
15213 \begin_inset LatexCommand \index{printf\_small()}
15228 For the mcs51 there additionally are assembly versions
15231 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15240 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15249 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15255 which should fit the requirements of many embedded systems (printf_fast()
15256 can be customized by unsetting #defines to
15260 support long variables and field widths).
15263 Math functions (sin, pow, sqrt etc.)
15270 \begin_inset LatexCommand \index{Libraries}
15274 included in SDCC should have a license at least as liberal as the GNU Lesser
15275 General Public License
15276 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
15287 license statements for the libraries are missing.
15288 sdcc/device/lib/ser_ir.c
15292 come with a GPL (as opposed to LGPL) License - this will not be liberal
15293 enough for many embedded programmers.
15296 If you have ported some library or want to share experience about some code
15298 falls into any of these categories Busses (I
15299 \begin_inset Formula $^{\textrm{2}}$
15302 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
15303 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
15304 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
15305 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
15310 would certainly like to hear about it.
15311 Programmers coding for embedded systems are not especially famous for being
15312 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
15313 e these references are very valuable.
15314 Let's help to create a climate where information is shared.
15320 MCS51 Memory Models
15321 \begin_inset LatexCommand \index{Memory model}
15326 \begin_inset LatexCommand \index{MCS51 memory model}
15331 \layout Subsubsection
15333 Small, Medium and Large
15336 SDCC allows three memory models for MCS51 code,
15345 Modules compiled with different memory models should
15349 be combined together or the results would be unpredictable.
15350 The library routines supplied with the compiler are compiled as small,
15352 The compiled library modules are contained in separate directories as small,
15353 medium and large so that you can link to the appropriate set.
15356 When the medium or large model is used all variables declared without a
15357 storage class will be allocated into the external ram, this includes all
15358 parameters and local variables (for non-reentrant
15359 \begin_inset LatexCommand \index{reentrant}
15364 When the small model is used variables without storage class are allocated
15365 in the internal ram.
15368 Judicious usage of the processor specific storage classes
15369 \begin_inset LatexCommand \index{Storage class}
15373 and the 'reentrant' function type will yield much more efficient code,
15374 than using the large model.
15375 Several optimizations are disabled when the program is compiled using the
15376 large model, it is therefore recommended that the small model be used unless
15377 absolutely required.
15378 \layout Subsubsection
15381 \begin_inset LatexCommand \label{sub:External-Stack}
15386 \begin_inset LatexCommand \index{stack}
15391 \begin_inset LatexCommand \index{External stack (mcs51)}
15398 The external stack (-
15409 \begin_inset LatexCommand \index{-\/-xstack}
15413 ) is located in pdata
15414 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15418 memory (usually at the start of the external ram segment) and uses all
15419 unused space in pdata (max.
15431 -xstack option is used to compile the program, the parameters and local
15433 \begin_inset LatexCommand \index{local variables}
15437 of all reentrant functions are allocated in this area.
15438 This option is provided for programs with large stack space requirements.
15439 When used with the -
15450 \begin_inset LatexCommand \index{-\/-stack-auto}
15454 option, all parameters and local variables are allocated on the external
15455 stack (note: support libraries will need to be recompiled with the same
15457 There is a predefined target in the library makefile).
15460 The compiler outputs the higher order address byte of the external ram segment
15462 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15467 \begin_inset LatexCommand \ref{sub:MCS51-variants}
15471 ), therefore when using the External Stack option, this port
15475 be used by the application program.
15479 \begin_inset LatexCommand \index{Memory model}
15484 \begin_inset LatexCommand \index{DS390 memory model}
15491 The only model supported is Flat 24
15492 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15497 This generates code for the 24 bit contiguous addressing mode of the Dallas
15499 In this mode, up to four meg of external RAM or code space can be directly
15501 See the data sheets at www.dalsemi.com for further information on this part.
15505 Note that the compiler does not generate any code to place the processor
15506 into 24 bitmode (although
15510 in the ds390 libraries will do that for you).
15516 \begin_inset LatexCommand \index{Tinibios (DS390)}
15520 , the boot loader or similar code must ensure that the processor is in 24
15521 bit contiguous addressing mode before calling the SDCC startup code.
15539 option, variables will by default be placed into the XDATA segment.
15544 Segments may be placed anywhere in the 4 meg address space using the usual
15556 Note that if any segments are located above 64K, the -r flag must be passed
15557 to the linker to generate the proper segment relocations, and the Intel
15558 HEX output format must be used.
15559 The -r flag can be passed to the linker by using the option
15563 on the SDCC command line.
15564 However, currently the linker can not handle code segments > 64k.
15568 \begin_inset LatexCommand \index{Pragmas}
15575 SDCC supports the following #pragma directives:
15579 \begin_inset LatexCommand \index{\#pragma save}
15583 - this will save all current options to the save/restore stack.
15584 See #pragma\SpecialChar ~
15589 \begin_inset LatexCommand \index{\#pragma restore}
15593 - will restore saved options from the last save.
15594 saves & restores can be nested.
15595 SDCC uses a save/restore stack: save pushes current options to the stack,
15596 restore pulls current options from the stack.
15597 See #pragma\SpecialChar ~
15604 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15609 \begin_inset LatexCommand \index{function prologue}
15613 function1[,function2[,function3...]] - The compiler by default uses a caller
15614 saves convention for register saving across function calls, however this
15615 can cause unnecessary register pushing & popping
15616 \begin_inset LatexCommand \index{push/pop}
15620 when calling small functions from larger functions.
15621 This option can be used to switch off the register saving convention for
15622 the function names specified.
15623 The compiler will not save registers when calling these functions, extra
15624 code need to be manually inserted at the entry & exit for these functions
15625 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15626 reduce code & improve run time performance of the generated code.
15627 In the future the compiler (with inter procedural analysis) may be able
15628 to determine the appropriate scheme to use for each function call.
15639 -callee-saves command line option is used, the function names specified
15640 in #pragma\SpecialChar ~
15642 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15646 is appended to the list of functions specified in the command line.
15650 \begin_inset LatexCommand \index{\#pragma exclude}
15654 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15655 of pairs of push/pop
15656 \begin_inset LatexCommand \index{push/pop}
15665 \begin_inset LatexCommand \index{interrupt}
15678 The directive should be placed immediately before the ISR function definition
15679 and it affects ALL ISR functions following it.
15680 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15681 exclude\SpecialChar ~
15683 \begin_inset LatexCommand \index{\#pragma exclude}
15688 See also the related keyword _naked
15689 \begin_inset LatexCommand \index{\_naked}
15694 \begin_inset LatexCommand \index{\_\_naked}
15702 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15706 - the compiler will not warn you anymore for obvious mistakes, you'r on
15710 disable_warning <nnnn>
15711 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15715 - the compiler will not warn you anymore about warning number <nnnn>.
15719 \begin_inset LatexCommand \index{\#pragma nogcse}
15723 - will stop global common subexpression elimination.
15727 \begin_inset LatexCommand \index{\#pragma noinduction}
15731 - will stop loop induction optimizations.
15735 \begin_inset LatexCommand \index{\#pragma noinvariant}
15739 - will not do loop invariant optimizations.
15740 For more details see Loop Invariants in section
15741 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15749 \begin_inset LatexCommand \index{\#pragma noiv}
15753 - Do not generate interrupt
15754 \begin_inset LatexCommand \index{interrupt}
15758 vector table entries for all ISR functions defined after the pragma.
15759 This is useful in cases where the interrupt vector table must be defined
15760 manually, or when there is a secondary, manually defined interrupt vector
15762 for the autovector feature of the Cypress EZ-USB FX2).
15763 More elegantly this can be achieved by obmitting the optional interrupt
15764 number after the interrupt keyword, see section
15765 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15774 \begin_inset LatexCommand \index{\#pragma nojtbound}
15778 - will not generate code for boundary value checking, when switch statements
15779 are turned into jump-tables (dangerous).
15780 For more details see section
15781 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15789 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15793 - Will not do loop reversal optimization
15797 \begin_inset LatexCommand \index{\#pragma nooverlay}
15801 - the compiler will not overlay the parameters and local variables of a
15806 \begin_inset LatexCommand \index{\#pragma stackauto}
15821 \begin_inset LatexCommand \index{-\/-stack-auto}
15826 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15830 Parameters and Local Variables.
15834 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15838 - The compiler will optimize code generation towards fast code, possibly
15839 at the expense of code size.
15843 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15847 - The compiler will optimize code generation towards compact code, possibly
15848 at the expense of code speed.
15852 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15856 - The compiler will attempt to generate code that is both compact and fast,
15857 as long as meeting one goal is not a detriment to the other (this is the
15863 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15867 - Generally follow the C89 standard, but allow SDCC features that conflict
15868 with the standard (default).
15872 \begin_inset LatexCommand \index{\#pragma std\_c89}
15876 - Follow the C89 standard and disable SDCC features that conflict with the
15881 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15885 - Generally follow the C99 standard, but allow SDCC features that conflict
15886 with the standard (incomplete support).
15890 \begin_inset LatexCommand \index{\#pragma std\_c99}
15894 - Follow the C99 standard and disable SDCC features that conflict with the
15895 standard (incomplete support).
15899 \begin_inset LatexCommand \index{\#pragma codeseg}
15903 - Use this name (max.
15904 8 characters) for the code segment.
15908 \begin_inset LatexCommand \index{\#pragma constseg}
15912 - Use this name (max.
15913 8 characters) for the const segment.
15916 SDCPP supports the following #pragma directives:
15920 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15924 (+ | -) - switch _asm _endasm block preprocessing on / off.
15928 The pragma's are intended to be used to turn-on or off certain optimizations
15929 which might cause the compiler to generate extra stack / data space to
15930 store compiler generated temporary variables.
15931 This usually happens in large functions.
15932 Pragma directives should be used as shown in the following example, they
15933 are used to control options & optimizations for a given function; pragmas
15934 should be placed before and/or after a function, placing pragma's inside
15935 a function body could have unpredictable results.
15941 \begin_inset LatexCommand \index{\#pragma save}
15952 /* save the current settings */
15955 \begin_inset LatexCommand \index{\#pragma nogcse}
15964 /* turnoff global subexpression elimination */
15966 #pragma noinduction
15967 \begin_inset LatexCommand \index{\#pragma noinduction}
15971 /* turn off induction optimizations */
15994 \begin_inset LatexCommand \index{\#pragma restore}
15998 /* turn the optimizations back on */
16001 The compiler will generate a warning message when extra space is allocated.
16002 It is strongly recommended that the save and restore pragma's be used when
16003 changing options for a function.
16006 Defines Created by the Compiler
16009 The compiler creates the following #defines
16010 \begin_inset LatexCommand \index{\#defines}
16015 \begin_inset LatexCommand \index{Defines created by the compiler}
16025 \begin_inset Tabular
16026 <lyxtabular version="3" rows="11" columns="2">
16028 <column alignment="center" valignment="top" leftline="true" width="0">
16029 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16030 <row topline="true" bottomline="true">
16031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16052 <row topline="true">
16053 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16059 \begin_inset LatexCommand \index{SDCC}
16066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16071 this Symbol is always defined.
16072 Since version 2.5.6 it's this version number as an int (ex.
16077 <row topline="true">
16078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16084 \begin_inset LatexCommand \index{SDCC\_mcs51}
16089 \begin_inset LatexCommand \index{SDCC\_ds390}
16094 \begin_inset LatexCommand \index{SDCC\_z80}
16101 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16106 depending on the model used (e.g.: -mds390
16110 <row topline="true">
16111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16117 \begin_inset LatexCommand \index{\_\_mcs51}
16122 \begin_inset LatexCommand \index{\_\_ds390}
16127 \begin_inset LatexCommand \index{\_\_hc08}
16132 \begin_inset LatexCommand \index{\_\_z80}
16139 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16144 depending on the model used (e.g.
16149 <row topline="true">
16150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16156 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16186 <row topline="true">
16187 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16193 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16223 <row topline="true">
16224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16230 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
16237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16260 <row topline="true">
16261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16267 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
16274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16297 <row topline="true">
16298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16304 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
16311 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16334 <row topline="true">
16335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16341 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16348 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16361 <row topline="true" bottomline="true">
16362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16368 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16395 Notes on supported Processors
16399 \begin_inset LatexCommand \label{sub:MCS51-variants}
16404 \begin_inset LatexCommand \index{MCS51 variants}
16411 MCS51 processors are available from many vendors and come in many different
16413 While they might differ considerably in respect to Special Function Registers
16414 the core MCS51 is usually not modified or is kept compatible.
16418 pdata access by SFR
16421 With the upcome of devices with internal xdata and flash memory devices
16423 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16427 as dedicated I/O port is becoming more popular.
16428 Switching the high byte for pdata
16429 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16433 access which was formerly done by port P2 is then achieved by a Special
16435 \begin_inset LatexCommand \index{sfr}
16440 In well-established MCS51 tradition the address of this
16444 is where the chip designers decided to put it.
16445 Needless to say that they didn't agree on a common name either.
16446 So that the startup code can correctly initialize xdata variables, you
16447 should define an sfr with the name _XPAGE
16450 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16456 at the appropriate location if the default, port P2, is not used for this.
16462 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16467 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16472 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16475 For more exotic implementations further customizations may be needed.
16477 \begin_inset LatexCommand \ref{sub:Startup-Code}
16481 for other possibilities.
16484 Other Features available by SFR
16487 Some MCS51 variants offer features like Double DPTR
16488 \begin_inset LatexCommand \index{DPTR}
16492 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16493 These are currently not used for the MCS51 port.
16494 If you absolutely need them you can fall back to inline assembly or submit
16501 The DS80C400 microcontroller has a rich set of peripherals.
16502 In its built-in ROM library it includes functions to access some of the
16503 features, among them is a TCP stack with IP4 and IP6 support.
16504 Library headers (currently in beta status) and other files are provided
16508 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16516 The Z80 and gbz80 port
16519 SDCC can target both the Zilog
16520 \begin_inset LatexCommand \index{Z80}
16524 and the Nintendo Gameboy's Z80-like gbz80
16525 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16530 The Z80 port is passed through the same
16533 \begin_inset LatexCommand \index{Regression test}
16539 as the MCS51 and DS390 ports, so floating point support, support for long
16540 variables and bitfield support is fine.
16541 See mailing lists and forums about interrupt routines.
16544 As always, the code is the authoritative reference - see z80/ralloc.c and
16547 \begin_inset LatexCommand \index{stack}
16551 frame is similar to that generated by the IAR Z80 compiler.
16552 IX is used as the base pointer, HL and IY are used as a temporary registers,
16553 and BC and DE are available for holding variables.
16555 \begin_inset LatexCommand \index{return value}
16559 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16561 The gbz80 port use the same set of registers for the return values, but
16562 in a different order of significance: E (one byte), DE (two bytes), or
16569 The port to the Motorola HC08
16570 \begin_inset LatexCommand \index{HC08}
16574 family has been added in October 2003, and is still undergoing some basic
16576 The code generator is complete, but the register allocation is still quite
16578 Some of the SDCC's standard C library functions have embedded non-HC08
16579 inline assembly and so are not yet usable.
16590 \begin_inset LatexCommand \index{PIC14}
16594 port still requires a major effort from the development community.
16595 However it can work for very simple code.
16598 C code and 14bit PIC code page
16599 \begin_inset LatexCommand \index{code page (pic14)}
16604 \begin_inset LatexCommand \index{RAM bank (pic14)}
16611 The linker organizes allocation for the code page and RAM banks.
16612 It does not have intimate knowledge of the code flow.
16613 It will put all the code section of a single asm file into a single code
16615 In order to make use of multiple code pages, separate asm files must be
16617 The compiler treats all functions of a single C file as being in the same
16618 code page unless it is non static.
16619 The compiler treats all local variables of a single C file as being in
16620 the same RAM bank unless it is an extern.
16624 To get the best follow these guide lines:
16627 make local functions static, as non static functions require code page selection
16631 Make local variables static as extern variables require RAM bank selection
16635 For devices that have multiple code pages it is more efficient to use the
16636 same number of files as pages, i.e.
16637 for the 16F877 use 4 separate files and i.e.
16638 for the 16F874 use 2 separate files.
16639 This way the linker can put the code for each file into different code
16640 pages and the compiler can allocate reusable variables more efficiently
16641 and there's less page selection overhead.
16642 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16643 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16646 Creating a device include file
16649 For generating a device include file use the support perl script inc2h.pl
16650 kept in directory support/script.
16656 For the interrupt function, use the keyword 'interrupt'
16657 \begin_inset LatexCommand \index{interrupt}
16661 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16662 there to avoid a syntax error - it ought to be fixed).
16668 void Intr(void) interrupt 0
16674 T0IF = 0; /* Clear timer interrupt */
16679 Linking and assembling
16682 For assembling you can use either GPUTILS'
16683 \begin_inset LatexCommand \index{gputils (pic tools)}
16687 gpasm.exe or MPLAB's mpasmwin.exe.
16688 GPUTILS is available from
16689 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16694 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16695 If you use MPLAB and an interrupt function then the linker script file
16696 vectors section will need to be enlarged to link with mplink.
16719 sdcc -S -V -mpic14 -p16F877 $<
16733 $(PRJ).hex: $(OBJS)
16743 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16765 sdcc -S -V -mpic14 -p16F877 $<
16775 mpasmwin /q /o $*.asm
16779 $(PRJ).hex: $(OBJS)
16789 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16792 Please note that indentations within a
16796 have to be done with a tabulator character.
16799 Command-line options
16802 Besides the switches common to all SDCC backends, the PIC14 port accepts
16803 the following options (for an updated list see sdcc -
16815 \labelwidthstring 00.00.0000
16827 -debug-extra emit debug info in assembly output
16829 \labelwidthstring 00.00.0000
16841 -no-pcode-opt disable (slightly faulty) optimization on pCode
16845 \layout Subsubsection
16847 error: missing definition for symbol
16848 \begin_inset Quotes sld
16852 \begin_inset Quotes srd
16858 The PIC14 port uses library routines to provide more complex operations
16859 like multiplication, division/modulus and (generic) pointer dereferencing.
16860 In order to add these routines to your project, you must link with PIC14's
16866 For single source file projects this is done automatically, more complex
16871 to the linker's arguments.
16872 Make sure you also add an include path for the library (using the -I switch
16874 \layout Subsubsection
16876 Processor mismatch in file
16877 \begin_inset Quotes sld
16881 \begin_inset Quotes srd
16887 This warning can usually be ignored due to the very good compatibility amongst
16888 14 bit PIC devices.
16891 You might also consider recompiling the library for your specific device
16892 by changing the ARCH=p16f877 (default target) entry in
16894 device/lib/pic/Makefile.in
16898 device/lib/pic/Makefile
16900 to reflect your device.
16901 This might even improve performance for smaller devices as unneccesary
16902 BANKSELs migth be removed.
16906 \layout Subsubsection
16911 Currently, data can only be initialized if it resides in the source file
16917 Data in other source files will silently
16925 \begin_inset LatexCommand \index{PIC16}
16933 \begin_inset LatexCommand \index{PIC16}
16937 port is the portion of SDCC that is responsible to produce code for the
16939 \begin_inset LatexCommand \index{Microchip}
16943 (TM) microcontrollers with 16 bit core.
16944 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16945 Currently supported devices are:
16949 \begin_inset Tabular
16950 <lyxtabular version="3" rows="4" columns="6">
16952 <column alignment="center" valignment="top" leftline="true" width="0">
16953 <column alignment="center" valignment="top" leftline="true" width="0">
16954 <column alignment="center" valignment="top" leftline="true" width="0">
16955 <column alignment="center" valignment="top" leftline="true" width="0">
16956 <column alignment="center" valignment="top" leftline="true" width="0">
16957 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16958 <row topline="true">
16959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16975 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16983 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16991 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16999 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17008 <row topline="true">
17009 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17033 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17049 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17058 <row topline="true">
17059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17067 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17083 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17091 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17099 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17108 <row topline="true" bottomline="true">
17109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17165 PIC16 port supports the standard command line arguments as supposed, with
17166 the exception of certain cases that will be mentioned in the following
17169 \labelwidthstring 00.00.0000
17181 -callee-saves See -
17193 \labelwidthstring 00.00.0000
17205 -all-callee-saves All function arguments are passed on stack by default.
17208 There is no need to specify this in the command line.
17210 \labelwidthstring 00.00.0000
17222 -fommit-frame-pointer Frame pointer will be omitted when the function uses
17223 no local variables.
17226 Port Specific Options
17227 \begin_inset LatexCommand \index{Options PIC16}
17234 The port specific options appear after the global options in the sdcc --help
17236 \layout Subsubsection
17241 General options enable certain port features and optimizations.
17243 \labelwidthstring 00.00.0000
17255 -stack-model=[model] Used in conjuction with the command above.
17256 Defines the stack model to be used, valid stack models are :
17259 \labelwidthstring 00.00.0000
17265 Selects small stack model.
17266 8 bit stack and frame pointers.
17267 Supports 256 bytes stack size.
17269 \labelwidthstring 00.00.0000
17275 Selects large stack model.
17276 16 bit stack and frame pointers.
17277 Supports 65536 bytes stack size.
17280 \labelwidthstring 00.00.0000
17292 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
17293 unitialized data variables with [kword].
17294 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
17296 \labelwidthstring 00.00.0000
17308 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
17309 Useful for bootloaders.
17311 \labelwidthstring 00.00.0000
17323 -asm= sets the full path and name of an external assembler to call.
17325 \labelwidthstring 00.00.0000
17337 -link= sets the full path and name of an external linker to call.
17339 \labelwidthstring 00.00.0000
17351 -mplab-comp MPLAB compatibility option.
17352 Currently only suppresses special gpasm directives.
17353 \layout Subsubsection
17355 Optimization Options
17357 \labelwidthstring 00.00.0000
17369 -optimize-goto Try to use (conditional) BRA instead of GOTO
17371 \labelwidthstring 00.00.0000
17383 -optimize-cmp Try to optimize some compares.
17385 \labelwidthstring 00.00.0000
17397 -optimize-df Analyze the dataflow of the generated code and improve it.
17399 \labelwidthstring 00.00.0000
17411 -obanksel=nn Set optimization level for inserting BANKSELs.
17416 \labelwidthstring 00.00.0000
17420 \labelwidthstring 00.00.0000
17422 1 checks previous used register and if it is the same then does not emit
17423 BANKSEL, accounts only for labels.
17425 \labelwidthstring 00.00.0000
17427 2 tries to check the location of (even different) symbols and removes BANKSELs
17428 if they are in the same bank.
17433 Important: There might be problems if the linker script has data sections
17434 across bank borders!
17436 \layout Subsubsection
17440 \labelwidthstring 00.00.0000
17452 -nodefaultlibs do not link default libraries when linking
17454 \labelwidthstring 00.00.0000
17466 -no-crt Don't link the default run-time modules
17468 \labelwidthstring 00.00.0000
17480 -use-crt= Use a custom run-time module instead of the defaults.
17481 \layout Subsubsection
17486 Debugging options enable extra debugging information in the output files.
17488 \labelwidthstring 00.00.0000
17500 -debug-xtra Similar to -
17511 \begin_inset LatexCommand \index{-\/-debug}
17515 , but dumps more information.
17517 \labelwidthstring 00.00.0000
17529 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17531 <source> is the name of the file compiled.
17533 \labelwidthstring 00.00.0000
17545 -pcode-verbose Enable pcode debugging information in translation.
17547 \labelwidthstring 00.00.0000
17559 -denable-peeps Force the usage of peepholes.
17562 \labelwidthstring 00.00.0000
17574 -gstack Trace push/pops for stack pointer overflow
17576 \labelwidthstring 00.00.0000
17588 -call-tree dump call tree in .calltree file
17591 Enviromental Variables
17594 There is a number of enviromental variables that can be used when running
17595 SDCC to enable certain optimizations or force a specific program behaviour.
17596 these variables are primarily for debugging purposes so they can be enabled/dis
17600 Currently there is only two such variables available:
17602 \labelwidthstring 00.00.0000
17604 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17605 bitfields is optimized by directly loading FSR0 with the address of the
17606 bitfield structure.
17607 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17609 This step saves data ram and code space for functions that perform heavy
17612 80 bytes of code space are saved when compiling malloc.c with this option).
17615 \labelwidthstring 00.00.0000
17617 NO_REG_OPT do not perform pCode registers optimization.
17618 This should be used for debugging purposes.
17619 In some where bugs in the pcode optimizer are found, users can benefit
17620 from temporarily disabling the optimizer until the bug is fixed.
17623 Preprocessor Macros
17626 PIC16 port defines the following preprocessor macros while translating a
17631 \begin_inset Tabular
17632 <lyxtabular version="3" rows="6" columns="2">
17634 <column alignment="center" valignment="top" leftline="true" width="0">
17635 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17636 <row topline="true" bottomline="true">
17637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17645 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17654 <row topline="true">
17655 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17663 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17668 Port identification
17672 <row topline="true">
17673 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17691 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17696 Port identification (same as above)
17700 <row topline="true">
17701 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17714 MCU Identification.
17719 is the microcontrol identification number, i.e.
17724 <row topline="true">
17725 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17743 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17748 MCU Identification (same as above)
17752 <row topline="true" bottomline="true">
17753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17761 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17766 nnn = SMALL or LARGE respectively according to the stack model used
17777 In addition the following macros are defined when calling assembler:
17781 \begin_inset Tabular
17782 <lyxtabular version="3" rows="4" columns="2">
17784 <column alignment="center" valignment="top" leftline="true" width="0">
17785 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17786 <row topline="true" bottomline="true">
17787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17795 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17804 <row topline="true">
17805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17813 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17818 MCU Identification.
17823 is the microcontrol identification number, i.e.
17828 <row topline="true">
17829 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17837 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17842 nnn = SMALL or LARGE respectively according to the memory model used for
17847 <row topline="true" bottomline="true">
17848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17861 nnn = SMALL or LARGE respectively according to the stack model used
17876 \begin_inset LatexCommand \index{PIC16}
17880 port uses the following directories for searching header files and libraries.
17884 \begin_inset Tabular
17885 <lyxtabular version="3" rows="3" columns="4">
17887 <column alignment="center" valignment="top" leftline="true" width="0">
17888 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17889 <column alignment="center" valignment="top" width="0">
17890 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17891 <row topline="true" bottomline="true">
17892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17900 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17925 <row topline="true">
17926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17931 PREFIX/sdcc/include/pic16
17934 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17939 PIC16 specific headers
17942 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17950 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17959 <row topline="true" bottomline="true">
17960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17965 PREFIX/sdcc/lib/pic16
17968 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17973 PIC16 specific libraries
17976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18001 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18008 PIC16 port currently supports the following pragmas:
18010 \labelwidthstring 00.00.0000
18012 stack pragma stack forces the code generator to initialize the stack & frame
18013 pointers at a specific address.
18014 This is an adhoc solution for cases where no STACK directive is available
18015 in the linker script or gplink is not instructed to create a stack section.
18017 The stack pragma should be used only once in a project.
18018 Multiple pragmas may result in indeterminate behaviour of the program.
18024 The old format (ie.
18025 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18026 cross page boundaries (or even exceed the available data RAM) and crash
18028 Make sure that stack does not cross page boundaries when using the SMALL
18034 The format is as follows:
18037 #pragma stack bottom_address [stack_size]
18044 is the lower bound of the stack section.
18045 The stack pointer initially will point at address (bottom_address+stack_size-1).
18053 /* initializes stack of 100 bytes at RAM address 0x200 */
18056 #pragma stack 0x200 100
18059 If the stack_size field is omitted then a stack is created with the default
18061 This size might be enough for most programs, but its not enough for operations
18062 with deep function nesting or excessive stack usage.
18064 \labelwidthstring 00.00.0000
18068 This pragma is deprecated.
18069 Its use will cause a warning message to be issued.
18075 \labelwidthstring 00.00.0000
18077 code place a function symbol at static FLASH address
18085 /* place function test_func at 0x4000 */
18088 #pragma code test_func 0x4000
18092 \labelwidthstring 00.00.0000
18094 library instructs the linker to use a library module.
18099 #pragma library module_name
18106 can be any library or object file (including its path).
18107 Note that there are four reserved keywords which have special meaning.
18112 \begin_inset Tabular
18113 <lyxtabular version="3" rows="6" columns="3">
18115 <column alignment="center" valignment="top" leftline="true" width="0">
18116 <column alignment="block" valignment="top" leftline="true" width="20page%">
18117 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18118 <row topline="true" bottomline="true">
18119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18144 <row topline="true">
18145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18160 ignore all library pragmas
18163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18174 <row topline="true">
18175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18193 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18206 <row topline="true">
18207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18222 link the Math libarary
18225 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18238 <row topline="true">
18239 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18254 link the I/O library
18257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18270 <row topline="true" bottomline="true">
18271 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18286 link the debug library
18289 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18308 * is the device number, i.e.
18309 452 for PIC18F452 MCU.
18312 This feature allows for linking with specific libraries withoug having to
18313 explicit name them in the command line.
18318 keyword will reject all modules specified by the library pragma.
18320 \labelwidthstring 00.00.0000
18322 udata pragma udata instructs the compiler to emit code so that linker will
18323 place a variable at a specific memory bank
18331 /* places variable foo at bank2 */
18334 #pragma udata bank2 foo
18340 In order for this pragma to work extra SECTION directives should be added
18341 in the .lkr script.
18342 In the following example a sample .lkr file is shown:
18347 // Sample linker script for the PIC18F452 processor
18353 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18356 CODEPAGE NAME=page START=0x2A END=0x7FFF
18359 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18362 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18365 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18368 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18371 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18376 DATABANK NAME=gpr0 START=0x80 END=0xFF
18379 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18382 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18385 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18388 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18391 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18394 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18399 SECTION NAME=CONFIG ROM=config
18404 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18407 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18410 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18413 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18416 SECTION NAME=bank4 RAM=gpr4
18419 SECTION NAME=bank5 RAM=gpr5
18422 The linker will recognise the section name set in the pragma statement and
18423 will position the variable at the memory bank set with the RAM field at
18424 the SECTION line in the linker script file.
18428 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18435 There is one main header file that can be included to the source files using
18442 This header file contains the definitions for the processor special registers,
18443 so it is necessary if the source accesses them.
18444 It can be included by adding the following line in the beginning of the
18448 #include <pic18fregs.h>
18451 The specific microcontroller is selected within the pic18fregs.h automatically,
18452 so the same source can be used with a variety of devices.
18458 The libraries that PIC16
18459 \begin_inset LatexCommand \index{PIC16}
18463 port depends on are the microcontroller device libraries which contain
18464 the symbol definitions for the microcontroller special function registers.
18465 These libraries have the format pic18fxxxx.lib, where
18469 is the microcontroller identification number.
18470 The specific library is selected automatically by the compiler at link
18471 stage according to the selected device.
18474 Libraries are created with gplib which is part of the gputils package
18475 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18480 \layout Subsubsection*
18482 Building the libraries
18485 Before using SDCC/pic16 there are some libraries that need to be compiled.
18486 This process is not done automatically by SDCC since not all users use
18487 SDCC for pic16 projects.
18488 So each user should compile the libraries separately.
18491 The steps to compile the pic16 libraries under Linux are:
18494 cd device/lib/pic16
18509 su -c 'make install' # install the libraries, you need the root password
18512 If you need to install the headers too, do:
18518 su -c 'make install' # install the headers, you need the root password
18521 There exist a special target to build the I/O libraries.
18522 This target is not automatically build because it will build the I/O library
18528 This way building will take quite a lot of time.
18529 Users are advised to edit the
18531 device/lib/pic16/pics.build
18533 file and then execute:
18542 The following memory models are supported by the PIC16 port:
18551 Memory model affects the default size of pointers within the source.
18552 The sizes are shown in the next table:
18556 \begin_inset Tabular
18557 <lyxtabular version="3" rows="3" columns="3">
18559 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18560 <column alignment="center" valignment="top" leftline="true" width="0">
18561 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18562 <row topline="true" bottomline="true">
18563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18568 Pointer sizes according to memory model
18571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18588 <row topline="true" bottomline="true">
18589 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18597 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18605 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18614 <row topline="true" bottomline="true">
18615 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18623 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18631 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18647 It is advisable that all sources within a project are compiled with the
18649 If one wants to override the default memory model, this can be done by
18650 declaring a pointer as
18659 Far selects large memory model's pointers, while near selects small memory
18663 The standard device libraries (see
18664 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18668 ) contain no reference to pointers, so they can be used with both memory
18675 The stack implementation for the PIC16 port uses two indirect registers,
18678 \labelwidthstring 00.00.0000
18680 FSR1 is assigned as stack pointer
18682 \labelwidthstring 00.00.0000
18684 FSR2 is assigned as frame pointer
18687 The following stack models are supported by the PIC16 port
18708 model means that only the FSRxL byte is used to access stack and frame,
18715 uses both FSRxL and FSRxH registers.
18716 The following table shows the stack/frame pointers sizes according to stack
18717 model and the maximum space they can address:
18721 \begin_inset Tabular
18722 <lyxtabular version="3" rows="3" columns="3">
18724 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18725 <column alignment="center" valignment="top" leftline="true" width="0">
18726 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18727 <row topline="true" bottomline="true">
18728 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18733 Stack & Frame pointer sizes according to stack model
18736 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18753 <row topline="true">
18754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18779 <row topline="true" bottomline="true">
18780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18816 stack model is currently not working properly throughout the code generator.
18817 So its use is not advised.
18818 Also there are some other points that need special care:
18823 Do not create stack sections with size more than one physical bank (that
18827 Stack sections should no cross physical bank limits (i.e.
18828 #pragma stack 0x50 0x100)
18831 These limitations are caused by the fact that only FSRxL is modified when
18832 using SMALL stack model, so no more than 256 bytes of stack can be used.
18833 This problem will disappear after LARGE model is fully implemented.
18839 In addition to the standard SDCC function keywords, PIC16 port makes available
18842 \labelwidthstring 00.00.0000
18844 wparam Use the WREG to pass one byte of the first function argument.
18845 This improves speed but you may not use this for functions with arguments
18846 that are called via function pointers, otherwise the first byte of the
18847 first parameter will get lost.
18851 void func_wparam(int a) wparam
18857 /* WREG hold the lower part of a */
18860 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18870 This keyword replaces the deprecated wparam pragma.
18872 \labelwidthstring 00.00.0000
18874 shadowregs When entering/exiting an ISR, it is possible to take advantage
18875 of the PIC18F hardware shadow registers which hold the values of WREG,
18876 STATUS and BSR registers.
18877 This can be done by adding the keyword
18885 keyword in the function's header.
18888 void isr_shadow(void) shadowregs interrupt 1
18904 instructs the code generator not to store/restore WREG, STATUS, BSR when
18905 entering/exiting the ISR.
18908 Function return values
18911 Return values from functions are placed to the appropriate registers following
18912 a modified Microchip policy optimized for SDCC.
18913 The following table shows these registers:
18917 \begin_inset Tabular
18918 <lyxtabular version="3" rows="6" columns="2">
18920 <column alignment="center" valignment="top" leftline="true" width="0">
18921 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18922 <row topline="true" bottomline="true">
18923 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18931 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18936 destination register
18940 <row topline="true">
18941 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18949 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18958 <row topline="true">
18959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18967 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18976 <row topline="true">
18977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18985 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18994 <row topline="true">
18995 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19003 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19008 FSR0L:PRODH:PRODL:WREG
19012 <row topline="true" bottomline="true">
19013 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19026 on stack, FSR0 points to the beginning
19040 An interrupt servive routine (ISR) is declared using the
19047 void isr(void) interrupt
19065 is the interrupt number, which for PIC18F devices can be:
19069 \begin_inset Tabular
19070 <lyxtabular version="3" rows="4" columns="3">
19072 <column alignment="center" valignment="top" leftline="true" width="0">
19073 <column alignment="center" valignment="top" leftline="true" width="0">
19074 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19075 <row topline="true" bottomline="true">
19076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19099 Interrupt Vector Address
19103 <row topline="true">
19104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19120 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19129 <row topline="true">
19130 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19161 HIGH priority interrupts
19164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19173 <row topline="true" bottomline="true">
19174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19187 LOW priority interrupts
19190 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19206 When generating assembly code for ISR the code generator places a
19212 Interrupt Vector Address
19214 which points at the genetated ISR.
19215 This single GOTO instruction is part of an automatically generated
19217 interrupt entry point
19220 The actuall ISR code is placed as normally would in the code space.
19221 Upon interrupt request, the GOTO instruction is executed which jumps to
19223 When declaring interrupt functions as _naked this GOTO instruction is
19228 The whole interrupt functions is therefore placed at the Interrupt Vector
19229 Address of the specific interrupt.
19230 This is not a problem for the LOW priority interrupts, but it is a problem
19231 for the RESET and the HIGH priority interrupts because code may be written
19232 at the next interrupt´s vector address and cause undeterminate program
19233 behaviour if that interrupt is raised.
19239 This is not a problem when
19242 this is a HIGH interrupt ISR and LOW interrupts are
19249 when the ISR is small enough not to reach the next interrupt´s vector address.
19259 is possible to be omitted.
19260 This way a function is generated similar to an ISR, but it is not assigned
19264 When entering an interrupt, currently the PIC16
19265 \begin_inset LatexCommand \index{PIC16}
19269 port automatically saves the following registers:
19281 PROD (PRODL and PRODH)
19284 FSR0 (FSR0L and FSR0H)
19287 These registers are restored upon return from the interrupt routine.
19293 NOTE that when the _naked attribute is specified for an interrupt routine,
19294 then NO registers are stored or restored.
19303 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
19304 There are 3 types of generic pointers currently implemented data, code
19305 and eeprom pointers.
19306 They are differentiated by the value of the 7th and 6th bits of the upper
19311 \begin_inset Tabular
19312 <lyxtabular version="3" rows="5" columns="5">
19314 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19315 <column alignment="center" valignment="top" width="0">
19316 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19317 <column alignment="center" valignment="top" width="0">
19318 <column alignment="left" valignment="top" rightline="true" width="0">
19319 <row topline="true" bottomline="true">
19320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19336 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19344 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19349 rest of the pointer
19352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19361 <row topline="true" bottomline="true">
19362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19378 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19386 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19395 uuuuuu uuuuxxxx xxxxxxxx
19398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19403 a 12-bit data pointer in data RAM memory
19407 <row bottomline="true">
19408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19416 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19441 uxxxxx xxxxxxxx xxxxxxxx
19444 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19449 a 21-bit code pointer in FLASH memory
19453 <row bottomline="true">
19454 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19470 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19478 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19487 uuuuuu uuuuuuxx xxxxxxxx
19490 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19495 a 10-bit eeprom pointer in EEPROM memory
19499 <row bottomline="true">
19500 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19516 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19524 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19533 xxxxxx xxxxxxxx xxxxxxxx
19536 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19541 unimplemented pointer type
19552 Generic pointer are read and written with a set of library functions which
19553 read/write 1, 2, 3, 4 bytes.
19557 \layout Subsubsection
19559 Standard I/O Streams
19566 the type FILE is defined as:
19569 typedef char * FILE;
19572 This type is the stream type implemented I/O in the PIC18F devices.
19573 Also the standard input and output streams are declared in stdio.h:
19576 extern FILE * stdin;
19579 extern FILE * stdout;
19582 The FILE type is actually a generic pointer which defines one more type
19583 of generic pointers, the
19588 This new type has the format:
19592 \begin_inset Tabular
19593 <lyxtabular version="3" rows="2" columns="7">
19595 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19596 <column alignment="center" valignment="top" width="0">
19597 <column alignment="center" valignment="top" leftline="true" width="0">
19598 <column alignment="center" valignment="top" leftline="true" width="0">
19599 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19600 <column alignment="center" valignment="top" width="0">
19601 <column alignment="left" valignment="top" rightline="true" width="0">
19602 <row topline="true" bottomline="true">
19603 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19611 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19619 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19635 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19648 rest of the pointer
19651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19660 <row topline="true" bottomline="true">
19661 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19669 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19677 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19685 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19713 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19718 upper byte high nubble is 0x2n, the rest are zeroes
19729 Currently implemented there are 3 types of streams defined:
19733 \begin_inset Tabular
19734 <lyxtabular version="3" rows="4" columns="4">
19736 <column alignment="center" valignment="top" leftline="true" width="0">
19737 <column alignment="center" valignment="top" leftline="true" width="0">
19738 <column alignment="center" valignment="top" leftline="true" width="0">
19739 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19740 <row topline="true" bottomline="true">
19741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19774 <row topline="true">
19775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19806 Writes/Reads characters via the USART peripheral
19810 <row topline="true">
19811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19819 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19829 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19837 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19842 Writes/Reads characters via the MSSP peripheral
19846 <row topline="true" bottomline="true">
19847 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19865 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19873 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19878 Writes/Reads characters via used defined functions
19889 The stream identifiers are declared as macros in the stdio.h header.
19892 In the libc library there exist the functions that are used to write to
19893 each of the above streams.
19896 \labelwidthstring 00.00.0000
19908 _stream_usart_putchar writes a character at the USART stream
19910 \labelwidthstring 00.00.0000
19922 _stream_mssp_putchar writes a character at the MSSP stream
19924 \labelwidthstring 00.00.0000
19926 putchar dummy function.
19927 This writes a character to a user specified manner.
19930 In order to increase performance
19934 is declared in stdio.h as having its parameter in WREG (it has the wparam
19936 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19937 in a user-friendly way.
19942 is the name of the variable that holds the character to print.
19943 An example follows:
19946 #include <pic18fregs.h>
19958 PORTA = c; /* dump character c to PORTA */
19971 stdout = STREAM_USER; /* this is not necessery, since stdout points
19974 * by default to STREAM_USER */
19977 printf (¨This is a printf test
19985 \layout Subsubsection
19990 PIC16 contains an implementation of the printf-family of functions.
19991 There exist the following functions:
19994 extern unsigned int sprintf(char *buf, char *fmt, ...);
19997 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20002 extern unsigned int printf(char *fmt, ...);
20005 extern unsigned int vprintf(char *fmt, va_lista ap);
20010 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20013 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20016 For sprintf and vsprintf
20020 should normally be a data pointer where the resulting string will be placed.
20021 No range checking is done so the user should allocate the necessery buffer.
20022 For fprintf and vfprintf
20026 should be a stream pointer (i.e.
20027 stdout, STREAM_MSSP, etc...).
20028 \layout Subsubsection
20033 The PIC18F family of microcontrollers supports a number of interrupt sources.
20034 A list of these interrupts is shown in the following table:
20038 \begin_inset Tabular
20039 <lyxtabular version="3" rows="11" columns="4">
20041 <column alignment="left" valignment="top" leftline="true" width="0">
20042 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20043 <column alignment="left" valignment="top" leftline="true" width="0">
20044 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20045 <row topline="true" bottomline="true">
20046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20062 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20070 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20079 <row topline="true">
20080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20088 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20093 PORTB change interrupt
20096 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20109 EEPROM/FLASH write complete interrupt
20113 <row topline="true">
20114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20127 INT0 external interrupt
20130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20138 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20143 Bus collision interrupt
20147 <row topline="true">
20148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20156 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20161 INT1 external interrupt
20164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20177 Low voltage detect interrupt
20181 <row topline="true">
20182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20190 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20195 INT2 external interrupt
20198 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20211 Parallel slave port interrupt
20215 <row topline="true">
20216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20229 CCP1 module interrupt
20232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20240 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20245 AD convertion complete interrupt
20249 <row topline="true">
20250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20258 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20263 CCP2 module interrupt
20266 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20279 USART receive interrupt
20283 <row topline="true">
20284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20292 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20297 TMR0 overflow interrupt
20300 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20308 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20313 USART transmit interrupt
20317 <row topline="true">
20318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20331 TMR1 overflow interrupt
20334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20347 SSP receive/transmit interrupt
20351 <row topline="true">
20352 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20360 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20365 TMR2 matches PR2 interrupt
20368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20383 <row topline="true" bottomline="true">
20384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20397 TMR3 overflow interrupt
20400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20422 The prototypes for these names are defined in the header file
20429 In order to simplify signal handling, a number of macros is provided:
20431 \labelwidthstring 00.00.0000
20433 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20434 high priority interrupts.
20439 is the function name to use.
20441 \labelwidthstring 00.00.0000
20443 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20444 low priority interrupt.
20449 is the function name to use.
20451 \labelwidthstring 00.00.0000
20453 DEF_HANDLER(sig,handler) define a handler for signal
20457 \labelwidthstring 00.00.0000
20459 END_DEF end the declaration of the dispatch table.
20462 Additionally there are two more macros to simplify the declaration of the
20465 \labelwidthstring 00.00.0000
20469 SIGHANDLER(handler)
20471 this declares the function prototype for the
20477 \labelwidthstring 00.00.0000
20479 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20482 An example of using the macros above is shown below:
20485 #include <pic18fregs.h>
20488 #include <signal.h>
20492 DEF_INTHIGH(high_int)
20495 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20498 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20505 SIGHANDLER(_tmr0_handler)
20511 /* action to be taken when timer 0 overflows */
20518 SIGHANDLERNAKED(_bcol_handler)
20527 /* action to be taken when bus collision occurs */
20543 Special care should be taken when using the above scheme:
20546 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20549 when declaring SIGHANDLERNAKED handler never forget to use
20553 for proper returning.
20559 Here you can find some general tips for compiling programs with SDCC/pic16.
20560 \layout Subsubsection
20565 The default stack size (that is 64 bytes) probably is enough for many programs.
20566 One must take care that when there are many levels of function nesting,
20567 or there is excessive usage of stack, its size should be extended.
20568 An example of such a case is the printf/sprintf family of functions.
20569 If you encounter problems like not being able to print integers, then you
20570 need to set the stack size around the maximum (256 for small stack model).
20571 The following diagram shows what happens when calling printf to print an
20575 printf () --> ltoa () --> ultoa () --> divschar ()
20578 It is should be understood that stack is easily consumed when calling complicate
20580 Using command line arguments like -
20590 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20592 Other ways to reduce stack usage may exist.
20595 Debugging with SDCDB
20596 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20601 \begin_inset LatexCommand \index{sdcdb (debugger)}
20608 SDCC is distributed with a source level debugger
20609 \begin_inset LatexCommand \index{Debugger}
20614 The debugger uses a command line interface, the command repertoire of the
20615 debugger has been kept as close to gdb
20616 \begin_inset LatexCommand \index{gdb}
20620 (the GNU debugger) as possible.
20621 The configuration and build process is part of the standard compiler installati
20622 on, which also builds and installs the debugger in the target directory
20623 specified during configuration.
20624 The debugger allows you debug BOTH at the C source and at the ASM source
20626 Sdcdb is currently not available on Win32 platforms.
20629 Compiling for Debugging
20643 \begin_inset LatexCommand \index{-\/-debug}
20647 option must be specified for all files for which debug information is to
20649 The complier generates a .adb file for each of these files.
20650 The linker creates the .cdb
20651 \begin_inset LatexCommand \index{<file>.cdb}
20656 \begin_inset LatexCommand \index{<file>.adb}
20660 files and the address information.
20661 This .cdb is used by the debugger.
20664 How the Debugger Works
20677 -debug option is specified the compiler generates extra symbol information
20678 some of which are put into the assembler source and some are put into the
20680 Then the linker creates the .cdb file from the individual .adb files with
20681 the address information for the symbols.
20682 The debugger reads the symbolic information generated by the compiler &
20683 the address information generated by the linker.
20684 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20685 execution is controlled by the debugger.
20686 When a command is issued for the debugger, it translates it into appropriate
20687 commands for the simulator.
20690 Starting the Debugger
20693 The debugger can be started using the following command line.
20694 (Assume the file you are debugging has the file name foo).
20708 The debugger will look for the following files.
20711 foo.c - the source file.
20714 foo.cdb - the debugger symbol information file.
20717 foo.ihx - the Intel hex format
20718 \begin_inset LatexCommand \index{Intel hex format}
20725 Command Line Options
20738 -directory=<source file directory> this option can used to specify the directory
20740 The debugger will look into the directory list specified for source, cdb
20742 The items in the directory list must be separated by ':', e.g.
20743 if the source files can be in the directories /home/src1 and /home/src2,
20754 -directory option should be -
20764 -directory=/home/src1:/home/src2.
20765 Note there can be no spaces in the option.
20769 -cd <directory> - change to the <directory>.
20772 -fullname - used by GUI front ends.
20775 -cpu <cpu-type> - this argument is passed to the simulator please see the
20776 simulator docs for details.
20779 -X <Clock frequency > this options is passed to the simulator please see
20780 the simulator docs for details.
20783 -s <serial port file> passed to simulator see the simulator docs for details.
20786 -S <serial in,out> passed to simulator see the simulator docs for details.
20789 -k <port number> passed to simulator see the simulator docs for details.
20795 As mentioned earlier the command interface for the debugger has been deliberatel
20796 y kept as close the GNU debugger gdb, as possible.
20797 This will help the integration with existing graphical user interfaces
20798 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20799 If you use a graphical user interface for the debugger you can skip this
20801 \layout Subsubsection*
20803 break [line | file:line | function | file:function]
20806 Set breakpoint at specified line or function:
20815 sdcdb>break foo.c:100
20817 sdcdb>break funcfoo
20819 sdcdb>break foo.c:funcfoo
20820 \layout Subsubsection*
20822 clear [line | file:line | function | file:function ]
20825 Clear breakpoint at specified line or function:
20834 sdcdb>clear foo.c:100
20836 sdcdb>clear funcfoo
20838 sdcdb>clear foo.c:funcfoo
20839 \layout Subsubsection*
20844 Continue program being debugged, after breakpoint.
20845 \layout Subsubsection*
20850 Execute till the end of the current function.
20851 \layout Subsubsection*
20856 Delete breakpoint number 'n'.
20857 If used without any option clear ALL user defined break points.
20858 \layout Subsubsection*
20860 info [break | stack | frame | registers ]
20863 info break - list all breakpoints
20866 info stack - show the function call stack.
20869 info frame - show information about the current execution frame.
20872 info registers - show content of all registers.
20873 \layout Subsubsection*
20878 Step program until it reaches a different source line.
20879 Note: pressing <return> repeats the last command.
20880 \layout Subsubsection*
20885 Step program, proceeding through subroutine calls.
20886 \layout Subsubsection*
20891 Start debugged program.
20892 \layout Subsubsection*
20897 Print type information of the variable.
20898 \layout Subsubsection*
20903 print value of variable.
20904 \layout Subsubsection*
20909 load the given file name.
20910 Note this is an alternate method of loading file for debugging.
20911 \layout Subsubsection*
20916 print information about current frame.
20917 \layout Subsubsection*
20922 Toggle between C source & assembly source.
20923 \layout Subsubsection*
20925 ! simulator command
20928 Send the string following '!' to the simulator, the simulator response is
20930 Note the debugger does not interpret the command being sent to the simulator,
20931 so if a command like 'go' is sent the debugger can loose its execution
20932 context and may display incorrect values.
20933 \layout Subsubsection*
20940 My name is Bobby Brown"
20943 Interfacing with DDD
20946 The screenshot was converted from png to eps with:
20947 \begin_inset Quotes sld
20950 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20951 \begin_inset Quotes srd
20954 which produces a pretty compact eps file which is free from compression
20958 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20959 as this broke the build system on Sourceforge (pdf-file was broken).
20965 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
20971 shows a screenshot of a debugging session with DDD
20972 \begin_inset LatexCommand \index{DDD (debugger)}
20976 (Unix only) on a simulated 8032.
20977 The debugging session might not run as smoothly as the screenshot suggests.
20978 The debugger allows setting of breakpoints, displaying and changing variables,
20979 single stepping through C and assembler code.
20982 The source was compiled with
21005 -debug ddd_example.c
21018 and DDD was invoked with
21025 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
21028 Interfacing with XEmacs
21029 \begin_inset LatexCommand \index{XEmacs}
21034 \begin_inset LatexCommand \index{Emacs}
21041 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
21042 sdcdb.el and sdcdbsrc.el.
21043 These two files can be found in the $(prefix)/bin directory after the installat
21045 These files need to be loaded into XEmacs for the interface to work.
21046 This can be done at XEmacs startup time by inserting the following into
21047 your '.xemacs' file (which can be found in your HOME directory):
21053 (load-file sdcdbsrc.el)
21059 .xemacs is a lisp file so the () around the command is REQUIRED.
21060 The files can also be loaded dynamically while XEmacs is running, set the
21061 environment variable 'EMACSLOADPATH' to the installation bin directory
21062 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
21063 To start the interface enter the following command:
21077 You will prompted to enter the file name to be debugged.
21082 The command line options that are passed to the simulator directly are bound
21083 to default values in the file sdcdbsrc.el.
21084 The variables are listed below, these values maybe changed as required.
21087 sdcdbsrc-cpu-type '51
21090 sdcdbsrc-frequency '11059200
21093 sdcdbsrc-serial nil
21096 The following is a list of key mapping for the debugger interface.
21107 ;;key\SpecialChar ~
21121 binding\SpecialChar ~
21145 ;;---\SpecialChar ~
21159 -------\SpecialChar ~
21201 sdcdb-next-from-src\SpecialChar ~
21229 sdcdb-back-from-src\SpecialChar ~
21257 sdcdb-cont-from-src\SpecialChar ~
21267 SDCDB continue command
21285 sdcdb-step-from-src\SpecialChar ~
21313 sdcdb-whatis-c-sexp\SpecialChar ~
21323 SDCDB ptypecommand for data at
21390 sdcdbsrc-delete\SpecialChar ~
21404 SDCDB Delete all breakpoints if no arg
21453 given or delete arg (C-u arg x)
21471 sdcdbsrc-frame\SpecialChar ~
21486 SDCDB Display current frame if no arg,
21535 given or display frame arg
21602 sdcdbsrc-goto-sdcdb\SpecialChar ~
21612 Goto the SDCDB output buffer
21630 sdcdb-print-c-sexp\SpecialChar ~
21641 SDCDB print command for data at
21708 sdcdbsrc-goto-sdcdb\SpecialChar ~
21718 Goto the SDCDB output buffer
21736 sdcdbsrc-mode\SpecialChar ~
21752 Toggles Sdcdbsrc mode (turns it off)
21767 sdcdb-finish-from-src\SpecialChar ~
21775 SDCDB finish command
21790 sdcdb-break\SpecialChar ~
21808 Set break for line with point
21823 sdcdbsrc-mode\SpecialChar ~
21839 Toggle Sdcdbsrc mode
21854 sdcdbsrc-srcmode\SpecialChar ~
21877 Here are a few guidelines that will help the compiler generate more efficient
21878 code, some of the tips are specific to this compiler others are generally
21879 good programming practice.
21882 Use the smallest data type to represent your data-value.
21883 If it is known in advance that the value is going to be less than 256 then
21884 use an 'unsigned char' instead of a 'short' or 'int'.
21885 Please note, that ANSI C requires both signed and unsigned chars to be
21886 promoted to 'signed int' before doing any operation.
21888 \begin_inset LatexCommand \index{type promotion}
21892 can be omitted, if the result is the same.
21893 The effect of the promotion rules together with the sign-extension is often
21900 unsigned char uc = 0xfe;
21902 if (uc * uc < 0) /* this is true! */
21921 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21931 (unsigned char) -12 / (signed char) -3 = ...
21934 No, the result is not 4:
21939 (int) (unsigned char) -12 / (int) (signed char) -3 =
21941 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21943 (int) 0x00f4 / (int) 0xfffd =
21945 (int) 0x00f4 / (int) 0xfffd =
21947 (int) 244 / (int) -3 =
21949 (int) -81 = (int) 0xffaf;
21952 Don't complain, that gcc gives you a different result.
21953 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21954 Therefore the results are different.
21957 \begin_inset Quotes sld
21961 \begin_inset Quotes srd
21967 If well-defined overflow characteristics are important and negative values
21968 are not, or if you want to steer clear of sign-extension problems when
21969 manipulating bits or bytes, use one of the corresponding unsigned types.
21970 (Beware when mixing signed and unsigned values in expressions, though.)
21972 Although character types (especially unsigned char) can be used as "tiny"
21973 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21974 ble sign extension and increased code size.
21978 Use unsigned when it is known in advance that the value is not going to
21980 This helps especially if you are doing division or multiplication, bit-shifting
21981 or are using an array index.
21984 NEVER jump into a LOOP.
21987 Declare the variables to be local
21988 \begin_inset LatexCommand \index{local variables}
21992 whenever possible, especially loop control variables (induction).
21995 Since the compiler does not always do implicit integral promotion, the programme
21996 r should do an explicit cast when integral promotion is required.
21999 Reducing the size of division, multiplication & modulus operations can reduce
22000 code size substantially.
22001 Take the following code for example.
22007 foobar(unsigned int p1, unsigned char ch)
22015 unsigned char ch1 = p1 % ch ;
22026 For the modulus operation the variable ch will be promoted to unsigned int
22027 first then the modulus operation will be performed (this will lead to a
22028 call to support routine _moduint()), and the result will be casted to a
22030 If the code is changed to
22035 foobar(unsigned int p1, unsigned char ch)
22043 unsigned char ch1 = (unsigned char)p1 % ch ;
22054 It would substantially reduce the code generated (future versions of the
22055 compiler will be smart enough to detect such optimization opportunities).
22059 Have a look at the assembly listing to get a
22060 \begin_inset Quotes sld
22064 \begin_inset Quotes srd
22067 for the code generation.
22071 \begin_inset LatexCommand \index{Tools}
22075 included in the distribution
22079 \begin_inset Tabular
22080 <lyxtabular version="3" rows="12" columns="3">
22082 <column alignment="center" valignment="top" leftline="true" width="0pt">
22083 <column alignment="center" valignment="top" leftline="true" width="0pt">
22084 <column alignment="left" 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">
22120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22125 Simulator for various architectures
22128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22137 <row topline="true">
22138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22151 header file conversion
22154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22159 sdcc/support/scripts
22163 <row topline="true">
22164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22177 header file conversion
22180 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22185 sdcc/support/scripts
22189 <row topline="true">
22190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22224 <row topline="true">
22225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22259 <row topline="true">
22260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22268 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22276 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22294 <row topline="true">
22295 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22311 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22329 <row topline="true">
22330 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22346 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22364 <row topline="true">
22365 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22399 <row topline="true">
22400 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22434 <row topline="true" bottomline="true">
22435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22451 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22479 \begin_inset LatexCommand \index{Documentation}
22483 included in the distribution
22487 \begin_inset Tabular
22488 <lyxtabular version="3" rows="10" columns="2">
22490 <column alignment="left" valignment="top" leftline="true" width="0">
22491 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22492 <row topline="true" bottomline="true">
22493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22501 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22506 Where to get / filename
22510 <row topline="true">
22511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22516 SDCC Compiler User Guide
22519 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22524 You're reading it right now
22528 <row topline="true">
22529 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22546 <row topline="true">
22547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22553 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
22558 \begin_inset LatexCommand \index{Assembler documentation}
22562 Assemblers and ASLINK
22563 \begin_inset LatexCommand \index{aslink}
22568 \begin_inset LatexCommand \index{Linker documentation}
22575 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22580 sdcc/as/doc/asxhtm.html
22584 <row topline="true">
22585 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22590 SDCC regression test
22591 \begin_inset LatexCommand \index{Regression test}
22598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22603 sdcc/doc/test_suite_spec.pdf
22607 <row topline="true">
22608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22625 <row topline="true">
22626 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22631 Notes on debugging with sdcdb
22632 \begin_inset LatexCommand \index{sdcdb (debugger)}
22639 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22644 sdcc/debugger/README
22648 <row topline="true">
22649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22654 Software simulator for microcontrollers
22657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22684 <row topline="true">
22685 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22690 Temporary notes on the pic16
22691 \begin_inset LatexCommand \index{PIC16}
22698 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22703 sdcc/src/pic16/NOTES
22707 <row topline="true" bottomline="true">
22708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22713 SDCC internal documentation (debugging file format)
22716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22752 Related open source tools
22753 \begin_inset LatexCommand \index{Related tools}
22761 \begin_inset Tabular
22762 <lyxtabular version="3" rows="11" columns="3">
22764 <column alignment="center" valignment="top" leftline="true" width="0pt">
22765 <column alignment="block" valignment="top" leftline="true" width="30line%">
22766 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22767 <row topline="true" bottomline="true">
22768 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22784 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22793 <row topline="true">
22794 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22800 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22807 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22821 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22829 <row topline="true">
22830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22836 \begin_inset LatexCommand \index{gputils (pic tools)}
22843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22851 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22857 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22865 <row topline="true">
22866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22888 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22896 <row topline="true">
22897 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22903 \begin_inset LatexCommand \index{indent (source formatting tool)}
22910 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22915 Formats C source - Master of the white spaces
22918 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22924 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22932 <row topline="true">
22933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22939 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22951 Object file conversion, checksumming, ...
22954 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22960 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22968 <row topline="true">
22969 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22975 \begin_inset LatexCommand \index{objdump (tool)}
22982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22987 Object file conversion, ...
22990 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22995 Part of binutils (should be there anyway)
22999 <row topline="true">
23000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23006 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
23013 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23018 Source code documentation system
23021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23027 \begin_inset LatexCommand \url{http://www.doxygen.org}
23035 <row topline="true">
23036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23049 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
23052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23058 \begin_inset LatexCommand \url{http://www.kdevelop.org}
23066 <row topline="true">
23067 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23073 \begin_inset LatexCommand \index{splint (syntax checking tool)}
23080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23085 Statically checks c sources (see
23086 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
23093 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23099 \begin_inset LatexCommand \url{http://www.splint.org}
23107 <row topline="true" bottomline="true">
23108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23114 \begin_inset LatexCommand \index{ddd (debugger)}
23121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23126 Debugger, serves nicely as GUI to sdcdb
23127 \begin_inset LatexCommand \index{sdcdb (debugger)}
23134 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23140 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
23157 Related documentation / recommended reading
23161 \begin_inset Tabular
23162 <lyxtabular version="3" rows="6" columns="3">
23164 <column alignment="center" valignment="top" leftline="true" width="0pt">
23165 <column alignment="block" valignment="top" leftline="true" width="30line%">
23166 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
23167 <row topline="true" bottomline="true">
23168 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23193 <row topline="true">
23194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23217 \begin_inset LatexCommand \index{C Reference card}
23224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23230 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
23238 <row topline="true">
23239 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23247 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23255 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23261 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
23269 <row topline="true">
23270 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23282 Latest datasheet of the target CPU
23285 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23294 <row topline="true">
23295 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23307 Revision history of datasheet
23310 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23319 <row topline="true" bottomline="true">
23320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23330 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23335 Advanced Compiler Design and Implementation
23338 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23343 bookstore (very dedicated, probably read other books first)
23359 Some questions answered, some pointers given - it might be time to in turn
23367 can you solve your project with the selected microcontroller? Would you
23368 find out early or rather late that your target is too small/slow/whatever?
23369 Can you switch to a slightly better device if it doesn't fit?
23372 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
23373 and/or another programming language be more adequate? Would an operating
23374 system on the target device help?
23377 if you solved the problem, will the marketing department be happy?
23380 if the marketing department is happy, will customers be happy?
23383 if you're the project manager, marketing department and maybe even the customer
23384 in one person, have you tried to see the project from the outside?
23387 is the project done if you think it is done? Or is just that other interface/pro
23388 tocol/feature/configuration/option missing? How about website, manual(s),
23389 internationali(z|s)ation, packaging, labels, 2nd source for components,
23390 electromagnetic compatability/interference, documentation for production,
23391 production test software, update mechanism, patent issues?
23394 is your project adequately positioned in that magic triangle: fame, fortune,
23398 Maybe not all answers to these questions are known and some answers may
23403 , nevertheless knowing these questions may help you to avoid burnout
23409 burnout is bad for electronic devices, programmers and motorcycle tyres
23413 Chances are you didn't want to hear some of them...
23417 \begin_inset LatexCommand \index{Support}
23424 SDCC has grown to be a large project.
23425 The compiler alone (without the preprocessor, assembler and linker) is
23426 well over 100,000 lines of code (blank stripped).
23427 The open source nature of this project is a key to its continued growth
23429 You gain the benefit and support of many active software developers and
23431 Is SDCC perfect? No, that's why we need your help.
23432 The developers take pride in fixing reported bugs.
23433 You can help by reporting the bugs and helping other SDCC users.
23434 There are lots of ways to contribute, and we encourage you to take part
23435 in making SDCC a great software package.
23439 The SDCC project is hosted on the SDCC sourceforge site at
23440 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
23445 You'll find the complete set of mailing lists
23446 \begin_inset LatexCommand \index{Mailing list(s)}
23450 , forums, bug reporting system, patch submission
23451 \begin_inset LatexCommand \index{Patch submission}
23456 \begin_inset LatexCommand \index{download}
23460 area and Subversion code repository
23461 \begin_inset LatexCommand \index{Subversion code repository}
23469 \begin_inset LatexCommand \index{Bug reporting}
23474 \begin_inset LatexCommand \index{Reporting bugs}
23481 The recommended way of reporting bugs is using the infrastructure of the
23483 You can follow the status of bug reports there and have an overview about
23487 Bug reports are automatically forwarded to the developer mailing list and
23488 will be fixed ASAP.
23489 When reporting a bug, it is very useful to include a small test program
23490 (the smaller the better) which reproduces the problem.
23491 If you can isolate the problem by looking at the generated assembly code,
23492 this can be very helpful.
23493 Compiling your program with the -
23504 \begin_inset LatexCommand \index{-\/-dumpall}
23508 option can sometimes be useful in locating optimization problems.
23509 When reporting a bug please maker sure you:
23512 Attach the code you are compiling with SDCC.
23516 Specify the exact command you use to run SDCC, or attach your Makefile.
23520 Specify the SDCC version (type "
23526 "), your platform, and operating system.
23530 Provide an exact copy of any error message or incorrect output.
23534 Put something meaningful in the subject of your message.
23537 Please attempt to include these 5 important parts, as applicable, in all
23538 requests for support or when reporting any problems or bugs with SDCC.
23539 Though this will make your message lengthy, it will greatly improve your
23540 chance that SDCC users and developers will be able to help you.
23541 Some SDCC developers are frustrated by bug reports without code provided
23542 that they can use to reproduce and ultimately fix the problem, so please
23543 be sure to provide sample code if you are reporting a bug!
23546 Please have a short check that you are using a recent version of SDCC and
23547 the bug is not yet known.
23548 This is the link for reporting bugs:
23549 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
23556 Requesting Features
23557 \begin_inset LatexCommand \label{sub:Requesting-Features}
23562 \begin_inset LatexCommand \index{Feature request}
23567 \begin_inset LatexCommand \index{Requesting features}
23574 Like bug reports feature requests are forwarded to the developer mailing
23576 This is the link for requesting features:
23577 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23587 Like bug reports contributed patches are forwarded to the developer mailing
23589 This is the link for submitting patches
23590 \begin_inset LatexCommand \index{Patch submission}
23595 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23602 You need to specify some parameters to the
23606 command for the patches to be useful.
23607 If you modified more than one file a patch created f.e.
23612 \begin_inset Quotes sld
23615 diff -Naur unmodified_directory modified_directory >my_changes.patch
23616 \begin_inset Quotes srd
23622 will be fine, otherwise
23626 \begin_inset Quotes sld
23629 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23630 \begin_inset Quotes srd
23643 These links should take you directly to the
23644 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23654 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23655 automated messages (mid 2003)
23659 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23664 \begin_inset LatexCommand \index{Mailing list(s)}
23668 and forums are archived and searchable so if you are lucky someone already
23669 had a similar problem.
23670 While mails to the lists themselves are delivered promptly their web front
23671 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23672 if you're seriously using SDCC please consider subscribing to the lists.
23678 You can follow the status of the Subversion version
23679 \begin_inset LatexCommand \index{version}
23683 of SDCC by watching the Changelog
23684 \begin_inset LatexCommand \index{Changelog}
23688 in the Subversion repository
23693 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
23701 \begin_inset LatexCommand \index{Release policy}
23708 Historically there often were long delays between official releases and
23709 the sourceforge download area tends to get not updated at all.
23710 Excuses in the past might have referred to problems with live range analysis,
23711 but as this was fixed a while ago, the current problem is that another
23712 excuse has to be found.
23713 Kidding aside, we have to get better there! On the other hand there are
23714 daily snapshots available at
23715 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23719 , and you can always build the very last version (hopefully with many bugs
23720 fixed, and features added) from the source code available at
23721 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23729 \begin_inset LatexCommand \index{Examples}
23736 You'll find some small examples in the directory
23738 sdcc/device/examples/.
23741 More examples and libraries are available at
23743 The SDCC Open Knowledge Resource
23744 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23751 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23758 I did insert a reference to Paul's web site here although it seems rather
23759 dedicated to a specific 8032 board (I think it's okay because it f.e.
23760 shows LCD/Harddisc interface and has a free 8051 monitor.
23761 Independent 8032 board vendors face hard competition of heavily subsidized
23762 development boards anyway).
23765 Maybe we should include some links to real world applications.
23766 Preferably pointer to pointers (one for each architecture) so this stays
23771 \begin_inset LatexCommand \index{Quality control}
23778 The compiler is passed through nightly compile and build checks.
23784 \begin_inset LatexCommand \index{Regression test}
23788 check that SDCC itself compiles flawlessly on several platforms and checks
23789 the quality of the code generated by SDCC by running the code through simulator
23791 There is a separate document
23794 \begin_inset LatexCommand \index{Test suite}
23803 You'll find the test code in the directory
23805 sdcc/support/regression
23808 You can run these tests manually by running
23812 in this directory (or f.e.
23817 \begin_inset Quotes sld
23821 \begin_inset Quotes srd
23827 if you don't want to run the complete tests).
23828 The test code might also be interesting if you want to look for examples
23829 \begin_inset LatexCommand \index{Examples}
23833 checking corner cases of SDCC or if you plan to submit patches
23834 \begin_inset LatexCommand \index{Patch submission}
23841 The pic port uses a different set of regression tests, you'll find them
23844 sdcc/src/regression
23849 Use of SDCC in Education
23862 the phrase "use in education" might evoke the association "
23866 fit for use in education".
23867 This connotation is not intended but nevertheless risked as the licensing
23868 of SDCC makes it difficult to offer educational discounts
23872 If your rationales are to:
23875 give students a chance to understand the
23879 steps of code generation
23882 have a curriculum that can be extended for years.
23883 Then you could use an fpga board as target and your curriculum will seamlessly
23884 extend from logic synthesis (
23885 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
23890 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
23894 ), over assembly programming, to C to FPGA compilers (
23895 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
23902 be able to insert excursions about skills like using a revision control
23903 system, submitting/applying patches, using a type-setting (as opposed to
23904 word-processing) engine LyX/LaTeX, using
23905 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
23910 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
23914 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
23915 Source Software, CPU simulation, compiler regression tests
23916 \begin_inset LatexCommand \index{Regression test}
23923 And if there should be a shortage of ideas then you can always point students
23924 to the ever-growing feature request list
23925 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23932 not tie students to a specific host platform and instead allow them to use
23937 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
23939 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
23946 not encourage students to use illegal copies of educational software
23949 be immune to licensing/availability/price changes of the chosen tool chain
23952 be able to change to a new target platform without having to adopt a new
23956 have complete control over and insight into the tool chain
23959 make your students aware about the pros and cons of open source software
23963 give back to the public as you are probably at least partially publically
23967 give students a chance to publically prove their skills and to possibly
23968 see a world wide impact
23971 then SDCC is probably among the first choices.
23972 Well, probably SDCC might be the only choice.
23975 SDCC Technical Data
23979 \begin_inset LatexCommand \index{Optimizations}
23986 SDCC performs a host of standard optimizations in addition to some MCU specific
23991 Sub-expression Elimination
23992 \begin_inset LatexCommand \index{Subexpression elimination}
23999 The compiler does local and
24025 will be translated to
24037 Some subexpressions are not as obvious as the above example, e.g.:
24047 In this case the address arithmetic a->b[i] will be computed only once;
24048 the equivalent code in C would be.
24060 The compiler will try to keep these temporary variables in registers.
24063 Dead-Code Elimination
24064 \begin_inset LatexCommand \index{Dead-code elimination}
24085 i = 1; \SpecialChar ~
24094 global = 1;\SpecialChar ~
24107 global = 3;\SpecialChar ~
24132 \begin_inset LatexCommand \index{Copy propagation}
24188 Note: the dead stores created by this copy propagation will be eliminated
24189 by dead-code elimination.
24193 \begin_inset LatexCommand \index{Loop optimization}
24198 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
24205 Two types of loop optimizations are done by SDCC
24213 of loop induction variables.
24214 In addition to the strength reduction the optimizer marks the induction
24215 variables and the register allocator tries to keep the induction variables
24216 in registers for the duration of the loop.
24217 Because of this preference of the register allocator
24218 \begin_inset LatexCommand \index{Register allocation}
24222 , loop induction optimization causes an increase in register pressure, which
24223 may cause unwanted spilling of other temporary variables into the stack
24224 \begin_inset LatexCommand \index{stack}
24229 The compiler will generate a warning message when it is forced to allocate
24230 extra space either on the stack or data space.
24231 If this extra space allocation is undesirable then induction optimization
24232 can be eliminated either for the entire source file (with -
24242 -noinduction option) or for a given function only using #pragma\SpecialChar ~
24244 \begin_inset LatexCommand \index{\#pragma noinduction}
24257 for (i = 0 ; i < 100 ; i ++)
24273 for (i = 0; i < 100; i++)
24282 As mentioned previously some loop invariants are not as apparent, all static
24283 address computations are also moved out of the loop.
24288 \begin_inset LatexCommand \index{Strength reduction}
24292 , this optimization substitutes an expression by a cheaper expression:
24297 for (i=0;i < 100; i++)
24315 for (i=0;i< 100;i++) {
24321 ar[itemp1] = itemp2;
24338 The more expensive multiplication
24339 \begin_inset LatexCommand \index{Multiplication}
24343 is changed to a less expensive addition.
24347 \begin_inset LatexCommand \index{Loop reversing}
24354 This optimization is done to reduce the overhead of checking loop boundaries
24355 for every iteration.
24356 Some simple loops can be reversed and implemented using a
24357 \begin_inset Quotes eld
24360 decrement and jump if not zero
24361 \begin_inset Quotes erd
24365 SDCC checks for the following criterion to determine if a loop is reversible
24366 (note: more sophisticated compilers use data-dependency analysis to make
24367 this determination, SDCC uses a more simple minded analysis).
24370 The 'for' loop is of the form
24376 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
24386 The <for body> does not contain
24387 \begin_inset Quotes eld
24391 \begin_inset Quotes erd
24395 \begin_inset Quotes erd
24401 All goto's are contained within the loop.
24404 No function calls within the loop.
24407 The loop control variable <sym> is not assigned any value within the loop
24410 The loop control variable does NOT participate in any arithmetic operation
24414 There are NO switch statements in the loop.
24417 Algebraic Simplifications
24420 SDCC does numerous algebraic simplifications, the following is a small sub-set
24421 of these optimizations.
24426 i = j + 0;\SpecialChar ~
24430 /* changed to: */\SpecialChar ~
24436 i /= 2;\SpecialChar ~
24443 /* changed to: */\SpecialChar ~
24449 i = j - j;\SpecialChar ~
24453 /* changed to: */\SpecialChar ~
24459 i = j / 1;\SpecialChar ~
24463 /* changed to: */\SpecialChar ~
24470 Note the subexpressions
24471 \begin_inset LatexCommand \index{Subexpression}
24475 given above are generally introduced by macro expansions or as a result
24476 of copy/constant propagation.
24479 'switch' Statements
24480 \begin_inset LatexCommand \label{sub:'switch'-Statements}
24485 \begin_inset LatexCommand \index{switch statement}
24492 SDCC can optimize switch statements to jump tables
24493 \begin_inset LatexCommand \index{jump tables}
24498 It makes the decision based on an estimate of the generated code size.
24499 SDCC is quite liberal in the requirements for jump table generation:
24502 The labels need not be in order, and the starting number need not be one
24503 or zero, the case labels are in numerical sequence or not too many case
24504 labels are missing.
24510 switch(i) {\SpecialChar ~
24541 case 4: ...\SpecialChar ~
24573 case 5: ...\SpecialChar ~
24605 case 3: ...\SpecialChar ~
24636 case 6: ...\SpecialChar ~
24668 case 7: ...\SpecialChar ~
24700 case 8: ...\SpecialChar ~
24732 case 9: ...\SpecialChar ~
24764 case 10: ...\SpecialChar ~
24795 case 11: ...\SpecialChar ~
24862 Both the above switch statements will be implemented using a jump-table.
24863 The example to the right side is slightly more efficient as the check for
24864 the lower boundary of the jump-table is not needed.
24868 The number of case labels is not larger than supported by the target architectur
24872 If the case labels are not in numerical sequence ('gaps' between cases)
24873 SDCC checks whether a jump table with additionally inserted dummy cases
24874 is still attractive.
24878 If the starting number is not zero and a check for the lower boundary of
24879 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24884 Switch statements which have large gaps in the numeric sequence or those
24885 that have too many case labels can be split into more than one switch statement
24886 for efficient code generation, e.g.:
24966 If the above switch statement is broken down into two switch statements
25056 then both the switch statements will be implemented using jump-tables whereas
25057 the unmodified switch statement will not be.
25060 There might be reasons which SDCC cannot know about to either favour or
25061 not favour jump tables.
25062 If the target system has to be as quick for the last switch case as for
25063 the first (pro jump table), or if the switch argument is known to be zero
25064 in the majority of the cases (contra jump table).
25067 The pragma nojtbound
25068 \begin_inset LatexCommand \index{\#pragma nojtbound}
25072 can be used to turn off checking the
25085 It has no effect if a default label is supplied.
25086 Use of this pragma is dangerous: if the switch
25087 \begin_inset LatexCommand \index{switch statement}
25091 argument is not matched by a case statement the processor will happily
25095 Bit-shifting Operations
25096 \begin_inset LatexCommand \index{Bit shifting}
25103 Bit shifting is one of the most frequently used operation in embedded programmin
25105 SDCC tries to implement bit-shift operations in the most efficient way
25121 generates the following code:
25138 In general SDCC will never setup a loop if the shift count is known.
25181 \begin_inset LatexCommand \index{Bit rotation}
25188 A special case of the bit-shift operation is bit rotation
25189 \begin_inset LatexCommand \index{rotating bits}
25193 , SDCC recognizes the following expression to be a left bit-rotation:
25203 char i;\SpecialChar ~
25214 /* unsigned is needed for rotation */
25219 i = ((i << 1) | (i >> 7));
25228 will generate the following code:
25247 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
25248 ns of this case will also be recognized as bit-rotation, i.e.:
25253 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
25256 Nibble and Byte Swapping
25259 Other special cases of the bit-shift operations are nibble or byte swapping
25260 \begin_inset LatexCommand \index{swapping nibbles/bytes}
25264 , SDCC recognizes the following expressions:
25287 i = ((i << 4) | (i >> 4));
25293 j = ((j << 8) | (j >> 8));
25296 and generates a swap instruction for the nibble swapping
25297 \begin_inset LatexCommand \index{Nibble swapping}
25301 or move instructions for the byte swapping
25302 \begin_inset LatexCommand \index{Byte swapping}
25308 \begin_inset Quotes sld
25312 \begin_inset Quotes srd
25315 example can be used to convert from little to big-endian or vice versa.
25316 If you want to change the endianness of a
25320 integer you have to cast to
25327 Note that SDCC stores numbers in little-endian
25333 Usually 8-bit processors don't care much about endianness.
25334 This is not the case for the standard 8051 which only has an instruction
25340 \begin_inset LatexCommand \index{DPTR}
25348 so little-endian is the more efficient byte order.
25352 \begin_inset LatexCommand \index{little-endian}
25357 \begin_inset LatexCommand \index{Endianness}
25362 lowest order first).
25366 \begin_inset LatexCommand \index{Highest Order Bit}
25371 \begin_inset LatexCommand \index{Any Order Bit}
25378 It is frequently required to obtain the highest order bit of an integral
25379 type (long, int, short or char types).
25380 Also obtaining any other order bit is not uncommon.
25381 SDCC recognizes the following expressions to yield the highest order bit
25382 and generates optimized code for it, e.g.:
25395 unsigned char hob1, aob1;
25399 bit hob2, hob3, aob2, aob3;
25408 hob1 = (gint >> 15) & 1;
25412 hob2 = (gint >> 15) & 1;
25416 hob3 = gint & 0x8000;
25420 aob1 = (gint >> 9) & 1;
25424 aob2 = (gint >> 8) & 1;
25428 aob3 = gint & 0x0800;
25438 will generate the following code:
25471 000A E5*01\SpecialChar ~
25498 000C 23\SpecialChar ~
25529 000D 54 01\SpecialChar ~
25556 000F F5*02\SpecialChar ~
25611 0011 E5*01\SpecialChar ~
25638 0013 33\SpecialChar ~
25668 0014 92*00\SpecialChar ~
25723 0016 E5*01\SpecialChar ~
25750 0018 33\SpecialChar ~
25780 0019 92*01\SpecialChar ~
25835 001B E5*01\SpecialChar ~
25862 001D 03\SpecialChar ~
25893 001E 54 01\SpecialChar ~
25920 0020 F5*03\SpecialChar ~
25975 0022 E5*01\SpecialChar ~
26002 0024 13\SpecialChar ~
26032 0025 92*02\SpecialChar ~
26087 0027 E5*01\SpecialChar ~
26114 0029 A2 E3\SpecialChar ~
26141 002B 92*03\SpecialChar ~
26169 Other variations of these cases however will
26174 They are standard C expressions, so I heartily recommend these be the only
26175 way to get the highest order bit, (it is portable).
26176 Of course it will be recognized even if it is embedded in other expressions,
26182 xyz = gint + ((gint >> 15) & 1);
26185 will still be recognized.
26189 \begin_inset LatexCommand \index{Higher Order Byte}
26193 / Higher Order Word
26194 \begin_inset LatexCommand \index{Higher Order Word}
26201 It is also frequently required to obtain a higher order byte or word of
26202 a larger integral type (long, int or short types).
26203 SDCC recognizes the following expressions to yield the higher order byte
26204 or word and generates optimized code for it, e.g.:
26211 unsigned long int glong;
26219 unsigned char hob1, hob2;
26223 unsigned int how1, how2;
26232 hob1 = (gint >> 8) & 0xFF;
26236 hob2 = glong >> 24;
26240 how1 = (glong >> 16) & 0xFFFF;
26254 will generate the following code:
26287 0037 85*01*06\SpecialChar ~
26309 _foo_hob1_1_1,(_gint + 1)
26339 003A 85*05*07\SpecialChar ~
26361 _foo_hob2_1_1,(_glong + 3)
26391 003D 85*04*08\SpecialChar ~
26413 _foo_how1_1_1,(_glong + 2)
26415 0040 85*05*09\SpecialChar ~
26437 (_foo_how1_1_1 + 1),(_glong + 3)
26439 0043 85*03*0A\SpecialChar ~
26461 _foo_how2_1_1,(_glong + 1)
26463 0046 85*04*0B\SpecialChar ~
26485 (_foo_how2_1_1 + 1),(_glong + 2)
26488 Again, variations of these cases may
26493 They are standard C expressions, so I heartily recommend these be the only
26494 way to get the higher order byte/word, (it is portable).
26495 Of course it will be recognized even if it is embedded in other expressions,
26501 xyz = gint + ((gint >> 8) & 0xFF);
26504 will still be recognized.
26508 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
26513 \begin_inset LatexCommand \index{Peephole optimizer}
26520 The compiler uses a rule based, pattern matching and re-writing mechanism
26521 for peep-hole optimization.
26526 a peep-hole optimizer by Christopher W.
26527 Fraser (cwfraser\SpecialChar ~
26530 A default set of rules are compiled into the compiler, additional rules
26531 may be added with the
26544 \begin_inset LatexCommand \index{-\/-peep-file}
26551 The rule language is best illustrated with examples.
26575 The above rule will change the following assembly
26576 \begin_inset LatexCommand \index{Assembler routines}
26598 Note: All occurrences of a
26602 (pattern variable) must denote the same string.
26603 With the above rule, the assembly sequence:
26613 will remain unmodified.
26617 Other special case optimizations may be added by the user (via
26633 some variants of the 8051 MCU
26634 \begin_inset LatexCommand \index{MCS51 variants}
26647 The following two rules will change all
26666 replace { lcall %1 } by { acall %1 }
26668 replace { ljmp %1 } by { ajmp %1 }
26673 inline-assembler code
26675 is also passed through the peep hole optimizer, thus the peephole optimizer
26676 can also be used as an assembly level macro expander.
26677 The rules themselves are MCU dependent whereas the rule language infra-structur
26678 e is MCU independent.
26679 Peephole optimization rules for other MCU can be easily programmed using
26684 The syntax for a rule is as follows:
26689 rule := replace [ restart ] '{' <assembly sequence> '
26727 <assembly sequence> '
26745 '}' [if <functionName> ] '
26750 <assembly sequence> := assembly instruction (each instruction including
26751 labels must be on a separate line).
26755 The optimizer will apply to the rules one by one from the top in the sequence
26756 of their appearance, it will terminate when all rules are exhausted.
26757 If the 'restart' option is specified, then the optimizer will start matching
26758 the rules again from the top, this option for a rule is expensive (performance)
26759 , it is intended to be used in situations where a transformation will trigger
26760 the same rule again.
26761 An example of this (not a good one, it has side effects) is the following
26784 Note that the replace pattern cannot be a blank, but can be a comment line.
26785 Without the 'restart' option only the innermost 'pop' 'push' pair would
26786 be eliminated, i.e.:
26816 the restart option the rule will be applied again to the resulting code
26817 and then all the pop-push pairs will be eliminated to yield:
26827 A conditional function can be attached to a rule.
26828 Attaching rules are somewhat more involved, let me illustrate this with
26855 The optimizer does a look-up of a function name table defined in function
26860 in the source file SDCCpeeph.c, with the name
26865 If it finds a corresponding entry the function is called.
26866 Note there can be no parameters specified for these functions, in this
26871 is crucial, since the function
26875 expects to find the label in that particular variable (the hash table containin
26876 g the variable bindings is passed as a parameter).
26877 If you want to code more such functions, take a close look at the function
26878 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26879 Currently implemented are
26881 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26882 24bitMode, portIsDS390, 24bitModeAndPortDS390
26891 I know this whole thing is a little kludgey, but maybe some day we will
26892 have some better means.
26893 If you are looking at this file, you will see the default rules that are
26894 compiled into the compiler, you can add your own rules in the default set
26895 there if you get tired of specifying the -
26909 \begin_inset LatexCommand \index{ANSI-compliance}
26914 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26921 Deviations from the compliance:
26924 functions are not reentrant
26925 \begin_inset LatexCommand \index{reentrant}
26929 unless explicitly declared as such or the
26942 \begin_inset LatexCommand \index{-\/-stack-auto}
26948 command line option is specified.
26951 structures and unions cannot be assigned values directly, cannot be passed
26952 as function parameters or assigned to each other and cannot be a return
26953 value from a function, e.g.:
26979 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26990 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
27012 return rets;/* is invalid in SDCC although allowed in ANSI */
27018 initialization of structure arrays must be fully braced.
27024 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
27026 struct s { char x } a[] = {{1}, {2}}; /* OK */
27031 \begin_inset LatexCommand \index{long long (not supported)}
27036 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
27044 \begin_inset LatexCommand \index{double (not supported)}
27048 ' precision floating point
27049 \begin_inset LatexCommand \index{Floating point support}
27057 \begin_inset LatexCommand \index{K\&R style}
27061 function declarations are NOT allowed.
27067 foo(i,j) /* this old style of function declarations */
27069 int i,j; /* are valid in ANSI but not valid in SDCC */
27084 Most enhancements in C99 are not supported, f.e.:
27093 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
27100 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
27104 Certain words that are valid identifiers in the standard may be reserved
27105 words in SDCC unless the
27118 \begin_inset LatexCommand \index{-\/-std-c89}
27133 \begin_inset LatexCommand \index{-\/-std-c99}
27139 command line options are used.
27140 These may include (depending on the selected processor): 'at', 'banked',
27141 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
27142 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
27143 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
27145 Compliant equivalents of these keywords are always available in a form
27146 that begin with two underscores
27147 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
27152 '__data' instead of 'data'.
27155 Cyclomatic Complexity
27156 \begin_inset LatexCommand \index{Cyclomatic complexity}
27163 Cyclomatic complexity of a function is defined as the number of independent
27164 paths the program can take during execution of the function.
27165 This is an important number since it defines the number test cases you
27166 have to generate to validate the function.
27167 The accepted industry standard for complexity number is 10, if the cyclomatic
27168 complexity reported by SDCC exceeds 10 you should think about simplification
27169 of the function logic.
27170 Note that the complexity level is not related to the number of lines of
27171 code in a function.
27172 Large functions can have low complexity, and small functions can have large
27178 SDCC uses the following formula to compute the complexity:
27183 complexity = (number of edges in control flow graph) - (number of nodes
27184 in control flow graph) + 2;
27188 Having said that the industry standard is 10, you should be aware that in
27189 some cases it be may unavoidable to have a complexity level of less than
27191 For example if you have switch statement with more than 10 case labels,
27192 each case label adds one to the complexity level.
27193 The complexity level is by no means an absolute measure of the algorithmic
27194 complexity of the function, it does however provide a good starting point
27195 for which functions you might look at for further optimization.
27198 Retargetting for other Processors
27201 The issues for retargetting the compiler are far too numerous to be covered
27203 What follows is a brief description of each of the seven phases of the
27204 compiler and its MCU dependency.
27207 Parsing the source and building the annotated parse tree.
27208 This phase is largely MCU independent (except for the language extensions).
27209 Syntax & semantic checks are also done in this phase, along with some initial
27210 optimizations like back patching labels and the pattern matching optimizations
27211 like bit-rotation etc.
27214 The second phase involves generating an intermediate code which can be easy
27215 manipulated during the later phases.
27216 This phase is entirely MCU independent.
27217 The intermediate code generation assumes the target machine has unlimited
27218 number of registers, and designates them with the name iTemp.
27219 The compiler can be made to dump a human readable form of the code generated
27233 This phase does the bulk of the standard optimizations and is also MCU independe
27235 This phase can be broken down into several sub-phases:
27239 Break down intermediate code (iCode) into basic blocks.
27241 Do control flow & data flow analysis on the basic blocks.
27243 Do local common subexpression elimination, then global subexpression elimination
27245 Dead code elimination
27249 If loop optimizations caused any changes then do 'global subexpression eliminati
27250 on' and 'dead code elimination' again.
27253 This phase determines the live-ranges; by live range I mean those iTemp
27254 variables defined by the compiler that still survive after all the optimization
27256 Live range analysis
27257 \begin_inset LatexCommand \index{Live range analysis}
27261 is essential for register allocation, since these computation determines
27262 which of these iTemps will be assigned to registers, and for how long.
27265 Phase five is register allocation.
27266 There are two parts to this process.
27270 The first part I call 'register packing' (for lack of a better term).
27271 In this case several MCU specific expression folding is done to reduce
27276 The second part is more MCU independent and deals with allocating registers
27277 to the remaining live ranges.
27278 A lot of MCU specific code does creep into this phase because of the limited
27279 number of index registers available in the 8051.
27282 The Code generation phase is (unhappily), entirely MCU dependent and very
27283 little (if any at all) of this code can be reused for other MCU.
27284 However the scheme for allocating a homogenized assembler operand for each
27285 iCode operand may be reused.
27288 As mentioned in the optimization section the peep-hole optimizer is rule
27289 based system, which can reprogrammed for other MCUs.
27293 \begin_inset LatexCommand \index{Compiler internals}
27300 The anatomy of the compiler
27301 \begin_inset LatexCommand \label{sub:The-anatomy-of}
27310 This is an excerpt from an article published in Circuit Cellar Magazine
27316 It's a little outdated (the compiler is much more efficient now and user/develo
27317 per friendly), but pretty well exposes the guts of it all.
27323 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
27324 It is fairly easy to retarget for other 8-bit MCU.
27325 Here we take a look at some of the internals of the compiler.
27330 \begin_inset LatexCommand \index{Parsing}
27337 Parsing the input source file and creating an AST (Annotated Syntax Tree
27338 \begin_inset LatexCommand \index{Annotated syntax tree}
27343 This phase also involves propagating types (annotating each node of the
27344 parse tree with type information) and semantic analysis.
27345 There are some MCU specific parsing rules.
27346 For example the storage classes, the extended storage classes are MCU specific
27347 while there may be a xdata storage class for 8051 there is no such storage
27348 class for z80 or Atmel AVR.
27349 SDCC allows MCU specific storage class extensions, i.e.
27350 xdata will be treated as a storage class specifier when parsing 8051 C
27351 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
27356 \begin_inset LatexCommand \index{iCode}
27363 Intermediate code generation.
27364 In this phase the AST is broken down into three-operand form (iCode).
27365 These three operand forms are represented as doubly linked lists.
27366 ICode is the term given to the intermediate form generated by the compiler.
27367 ICode example section shows some examples of iCode generated for some simple
27368 C source functions.
27372 \begin_inset LatexCommand \index{Optimizations}
27379 Bulk of the target independent optimizations is performed in this phase.
27380 The optimizations include constant propagation, common sub-expression eliminati
27381 on, loop invariant code movement, strength reduction of loop induction variables
27382 and dead-code elimination.
27385 Live range analysis
27386 \begin_inset LatexCommand \index{Live range analysis}
27393 During intermediate code generation phase, the compiler assumes the target
27394 machine has infinite number of registers and generates a lot of temporary
27396 The live range computation determines the lifetime of each of these compiler-ge
27397 nerated temporaries.
27398 A picture speaks a thousand words.
27399 ICode example sections show the live range annotations for each of the
27401 It is important to note here, each iCode is assigned a number in the order
27402 of its execution in the function.
27403 The live ranges are computed in terms of these numbers.
27404 The from number is the number of the iCode which first defines the operand
27405 and the to number signifies the iCode which uses this operand last.
27408 Register Allocation
27409 \begin_inset LatexCommand \index{Register allocation}
27416 The register allocation determines the type and number of registers needed
27418 In most MCUs only a few registers can be used for indirect addressing.
27419 In case of 8051 for example the registers R0 & R1 can be used to indirectly
27420 address the internal ram and DPTR to indirectly address the external ram.
27421 The compiler will try to allocate the appropriate register to pointer variables
27423 ICode example section shows the operands annotated with the registers assigned
27425 The compiler will try to keep operands in registers as much as possible;
27426 there are several schemes the compiler uses to do achieve this.
27427 When the compiler runs out of registers the compiler will check to see
27428 if there are any live operands which is not used or defined in the current
27429 basic block being processed, if there are any found then it will push that
27430 operand and use the registers in this block, the operand will then be popped
27431 at the end of the basic block.
27435 There are other MCU specific considerations in this phase.
27436 Some MCUs have an accumulator; very short-lived operands could be assigned
27437 to the accumulator instead of a general-purpose register.
27443 Figure II gives a table of iCode operations supported by the compiler.
27444 The code generation involves translating these operations into corresponding
27445 assembly code for the processor.
27446 This sounds overly simple but that is the essence of code generation.
27447 Some of the iCode operations are generated on a MCU specific manner for
27448 example, the z80 port does not use registers to pass parameters so the
27449 SEND and RECV iCode operations will not be generated, and it also does
27450 not support JUMPTABLES.
27457 <Where is Figure II?>
27460 In the original article Figure II was announced to be downloadable on
27465 Unfortunately it never seemed to have shown up there, so: where is Figure
27470 \begin_inset LatexCommand \index{iCode}
27477 This section shows some details of iCode.
27478 The example C code does not do anything useful; it is used as an example
27479 to illustrate the intermediate code generated by the compiler.
27491 /* This function does nothing useful.
27498 for the purpose of explaining iCode */
27501 short function (data int *x)
27509 short i=10; \SpecialChar ~
27511 /* dead initialization eliminated */
27516 short sum=10; /* dead initialization eliminated */
27529 while (*x) *x++ = *p++;
27543 /* compiler detects i,j to be induction variables */
27547 for (i = 0, j = 10 ; i < 10 ; i++, j
27573 mul += i * 3; \SpecialChar ~
27575 /* this multiplication remains */
27581 gint += j * 3;\SpecialChar ~
27583 /* this multiplication changed to addition */
27597 In addition to the operands each iCode contains information about the filename
27598 and line it corresponds to in the source file.
27599 The first field in the listing should be interpreted as follows:
27604 Filename(linenumber: iCode Execution sequence number : ICode hash table
27605 key : loop depth of the iCode).
27610 Then follows the human readable form of the ICode operation.
27611 Each operand of this triplet form can be of three basic types a) compiler
27612 generated temporary b) user defined variable c) a constant value.
27613 Note that local variables and parameters are replaced by compiler generated
27616 \begin_inset LatexCommand \index{Live range analysis}
27620 are computed only for temporaries (i.e.
27621 live ranges are not computed for global variables).
27623 \begin_inset LatexCommand \index{Register allocation}
27627 are allocated for temporaries only.
27628 Operands are formatted in the following manner:
27633 Operand Name [lr live-from : live-to ] { type information } [ registers
27639 As mentioned earlier the live ranges are computed in terms of the execution
27640 sequence number of the iCodes, for example
27642 the iTemp0 is live from (i.e.
27643 first defined in iCode with execution sequence number 3, and is last used
27644 in the iCode with sequence number 5).
27645 For induction variables such as iTemp21 the live range computation extends
27646 the lifetime from the start to the end of the loop.
27648 The register allocator used the live range information to allocate registers,
27649 the same registers may be used for different temporaries if their live
27650 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
27651 iTemp17 since their live ranges do not overlap.
27652 In addition the allocator also takes into consideration the type and usage
27653 of a temporary, for example itemp6 is a pointer to near space and is used
27654 as to fetch data from (i.e.
27655 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
27656 Some short lived temporaries are allocated to special registers which have
27657 meaning to the code generator e.g.
27658 iTemp13 is allocated to a pseudo register CC which tells the back end that
27659 the temporary is used only for a conditional jump the code generation makes
27660 use of this information to optimize a compare and jump ICode.
27662 There are several loop optimizations
27663 \begin_inset LatexCommand \index{Loop optimization}
27667 performed by the compiler.
27668 It can detect induction variables iTemp21(i) and iTemp23(j).
27669 Also note the compiler does selective strength reduction
27670 \begin_inset LatexCommand \index{Strength reduction}
27675 the multiplication of an induction variable in line 18 (gint = j * 3) is
27676 changed to addition, a new temporary iTemp17 is allocated and assigned
27677 a initial value, a constant 3 is then added for each iteration of the loop.
27678 The compiler does not change the multiplication
27679 \begin_inset LatexCommand \index{Multiplication}
27683 in line 17 however since the processor does support an 8 * 8 bit multiplication.
27685 Note the dead code elimination
27686 \begin_inset LatexCommand \index{Dead-code elimination}
27690 optimization eliminated the dead assignments in line 7 & 8 to I and sum
27698 Sample.c (5:1:0:0) _entry($9) :
27703 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27708 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27713 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27718 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27724 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27729 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27735 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27740 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27746 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27752 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27758 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27764 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27765 * int}[r0] + 0x2 {short}
27770 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27775 Sample.c(11:17:21:0)_whilebreak_0($3) :
27780 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27785 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27790 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27795 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27800 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27805 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27810 Sample.c(15:24:26:1)_forcond_0($4) :
27815 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27821 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27826 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27827 + ITemp21 [lr21:38]{short}[r4]
27832 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27838 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27839 + iTemp15 [lr29:30]{short}[r1]
27844 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27850 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27856 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27862 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27868 Sample.c(19:38:47:1) goto _forcond_0($4)
27873 Sample.c(19:39:48:0)_forbreak_0($7) :
27878 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27879 + ITemp11 [lr19:40]{short}[r3]
27884 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27889 Sample.c(20:42:51:0)_return($8) :
27894 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27900 Finally the code generated for this function:
27941 ; ----------------------------------------------
27946 ; function function
27951 ; ----------------------------------------------
27961 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27973 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27985 ;_whilecontinue_0($1) :
27995 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
28000 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28059 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
28078 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
28125 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
28165 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
28191 ; iTemp6 [lr5:16]{_near * int}[r0] =
28196 ; iTemp6 [lr5:16]{_near * int}[r0] +
28213 ; goto _whilecontinue_0($1)
28225 ; _whilebreak_0($3) :
28235 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
28247 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
28259 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
28271 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
28290 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
28319 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
28324 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28369 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
28374 ; iTemp21 [lr21:38]{short}[r4]
28400 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
28433 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
28438 ; iTemp15 [lr29:30]{short}[r1]
28457 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
28504 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
28551 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
28563 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
28577 cjne r5,#0xff,00104$
28589 ; goto _forcond_0($4)
28601 ; _forbreak_0($7) :
28611 ; ret iTemp24 [lr40:41]{short}
28654 A few words about basic block successors, predecessors and dominators
28657 Successors are basic blocks
28658 \begin_inset LatexCommand \index{Basic blocks}
28662 that might execute after this basic block.
28664 Predecessors are basic blocks that might execute before reaching this basic
28667 Dominators are basic blocks that WILL execute before reaching this basic
28701 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28704 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28707 c) domVect of [BB4] = BB1 ...
28708 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28716 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28726 Thanks to all the other volunteer developers who have helped with coding,
28727 testing, web-page creation, distribution sets, etc.
28728 You know who you are :-)
28735 This document was initially written by Sandeep Dutta
28738 All product names mentioned herein may be trademarks
28739 \begin_inset LatexCommand \index{Trademarks}
28743 of their respective companies.
28750 To avoid confusion, the installation and building options for SDCC itself
28751 (chapter 2) are not part of the index.
28755 \begin_inset LatexCommand \printindex{}