1 #LyX 1.2 created this file. For more info see http://www.lyx.org/
5 \usepackage[colorlinks=true,linkcolor=blue]{hyperref}
12 \paperfontsize default
19 \use_numerical_citations 0
20 \paperorientation portrait
27 \paragraph_separation indent
29 \quotes_language swedish
37 Please note: double dashed longoptions (e.g.
38 --version) need three dashes in this document to be visable in html and
42 SDCC Compiler User Guide
49 \begin_inset LatexCommand \tableofcontents{}
66 is a Freeware, retargettable, optimizing ANSI-C compiler by
70 designed for 8 bit Microprocessors.
71 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
73 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052 CPU}
77 , etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
78 It can be retargetted for other microprocessors, support for PIC, AVR and
79 186 is under development.
80 The entire source code for the compiler is distributed under GPL.
81 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
82 SDCC has extensive language extensions suitable for utilizing various microcont
83 rollers and underlying hardware effectively.
88 In addition to the MCU specific optimizations SDCC also does a host of standard
92 global sub expression elimination,
95 loop optimizations (loop invariant, strength reduction of induction variables
99 constant folding & propagation,
105 dead code elimination
115 For the back-end SDCC uses a global register allocation scheme which should
116 be well suited for other 8 bit MCUs.
121 The peep hole optimizer uses a rule based substitution mechanism which is
127 Supported data-types are:
130 char (8 bits, 1 byte),
133 short and int (16 bits, 2 bytes),
136 long (32 bit, 4 bytes)
143 The compiler also allows
145 inline assembler code
147 to be embedded anywhere in a function.
148 In addition, routines developed in assembly can also be called.
152 SDCC also provides an option (--cyclomatic) to report the relative complexity
154 These functions can then be further optimized, or hand coded in assembly
160 SDCC also comes with a companion source level debugger SDCDB, the debugger
161 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
166 The latest version can be downloaded from
167 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
177 Please note: the compiler will probably always be some steps ahead of this
182 \begin_inset LatexCommand \index{Status of documentation}
192 Obviously this has pros and cons
201 All packages used in this compiler system are
209 ; source code for all the sub-packages (pre-processor, assemblers, linkers
210 etc) is distributed with the package.
211 This documentation is maintained using a freeware word processor (LyX).
213 This program is free software; you can redistribute it and/or modify it
214 under the terms of the GNU General Public License
215 \begin_inset LatexCommand \index{GNU General Public License, GPL}
219 as published by the Free Software Foundation; either version 2, or (at
220 your option) any later version.
221 This program is distributed in the hope that it will be useful, but WITHOUT
222 ANY WARRANTY; without even the implied warranty
223 \begin_inset LatexCommand \index{warranty}
227 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
228 See the GNU General Public License for more details.
229 You should have received a copy of the GNU General Public License along
230 with this program; if not, write to the Free Software Foundation, 59 Temple
231 Place - Suite 330, Boston, MA 02111-1307, USA.
232 In other words, you are welcome to use, share and improve this program.
233 You are forbidden to forbid anyone else to use, share and improve what
235 Help stamp out software-hoarding!
238 Typographic conventions
239 \begin_inset LatexCommand \index{Typographic conventions}
246 Throughout this manual, we will use the following convention.
247 Commands you have to type in are printed in
255 Code samples are printed in
260 Interesting items and new terms are printed in
265 Compatibility with previous versions
268 This version has numerous bug fixes compared with the previous version.
269 But we also introduced some incompatibilities with older versions.
270 Not just for the fun of it, but to make the compiler more stable, efficient
272 \begin_inset LatexCommand \index{ANSI-compliance}
282 short is now equivalent to int (16 bits), it used to be equivalent to char
283 (8 bits) which is not ANSI compliant
286 the default directory for gcc-builds where include, library and documention
287 files are stored is now in /usr/local/share
290 char type parameters to vararg functions are casted to int unless explicitly
307 will push a as an int and as a char resp.
310 option ---regextend has been removed
313 option ---noregparms has been removed
316 option ---stack-after-data has been removed
321 <pending: more incompatibilities?>
327 What do you need before you start installation of SDCC? A computer, and
329 The preferred method of installation is to compile SDCC from source using
331 For Windows some pre-compiled binary distributions are available for your
333 You should have some experience with command line tools and compiler use.
339 The SDCC home page at
340 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
344 is a great place to find distribution sets.
345 You can also find links to the user mailing lists that offer help or discuss
346 SDCC with other SDCC users.
347 Web links to other SDCC related sites can also be found here.
348 This document can be found in the DOC directory of the source package as
350 Some of the other tools (simulator and assembler) included with SDCC contain
351 their own documentation and can be found in the source distribution.
352 If you want the latest unreleased software, the complete source package
353 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
356 ANSI C reference / related tools / recommended reading / compiler building:
360 Wishes for the future
363 There are (and always will be) some things that could be done.
364 Here are some I can think of:
371 char KernelFunction3(char p) at 0x340;
379 \begin_inset LatexCommand \index{code banking (not supported)}
387 If you can think of some more, please see the chapter 9 about filing feature
389 \begin_inset LatexCommand \index{Requesting features}
394 \begin_inset LatexCommand \index{Feature requests}
404 \begin_inset LatexCommand \index{Installation}
412 \begin_inset LatexCommand \index{Options SDCC configuration}
419 The install paths, search paths and other options are defined when running
421 The defaults can be overriden by:
423 \labelwidthstring 00.00.0000
425 ---prefix see tabel below
427 \labelwidthstring 00.00.0000
429 ---exec_prefix see tabel below
431 \labelwidthstring 00.00.0000
433 ---bindir see tabel below
435 \labelwidthstring 00.00.0000
437 ---datadir see tabel below
439 \labelwidthstring 00.00.0000
441 docdir environment variable, see tabel below
443 \labelwidthstring 00.00.0000
445 include_dir_suffix environment variable, see tabel below
447 \labelwidthstring 00.00.0000
449 lib_dir_suffix environment variable, see tabel below
451 \labelwidthstring 00.00.0000
453 sdccconf_h_dir_separator environment variable, either / or
458 This character will only be used in sdccconf.h; don't forget it's a C-header,
459 therefore a double-backslash is needed there.
461 \labelwidthstring 00.00.0000
463 ---disable-mcs51-port Excludes the Intel mcs51 port
465 \labelwidthstring 00.00.0000
467 ---disable-gbz80-port Excludes the Gameboy gbz80 port
469 \labelwidthstring 00.00.0000
471 ---disable-z80-port Excludes the z80 port
473 \labelwidthstring 00.00.0000
475 ---disable-avr-port Excludes the AVR port
477 \labelwidthstring 00.00.0000
479 ---disable-ds390-port Excludes the DS390 port
481 \labelwidthstring 00.00.0000
483 ---disable-pic-port Excludes the PIC port
485 \labelwidthstring 00.00.0000
487 ---disable-xa51-port Excludes the XA51 port
489 \labelwidthstring 00.00.0000
491 ---disable-ucsim Disables configuring and building of ucsim
493 \labelwidthstring 00.00.0000
495 ---disable-device-lib-build Disables automatically building device libraries
497 \labelwidthstring 00.00.0000
499 ---disable-packihx Disables building packihx
501 \labelwidthstring 00.00.0000
503 ---enable-libgc Use the Bohem memory allocator.
504 Lower runtime footprint.
507 Furthermore the environment variables CC, CFLAGS, ...
508 the tools and their arguments can be influenced.
509 Please see `configure ---help` and the man/info pages of `configure` for
514 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
515 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
516 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
517 At the moment it's not possible to change the default settings (it was
518 simply never required.
522 These configure options are compiled into the binaries, and can only be
523 changed by rerunning 'configure' and recompiling SDCC.
524 The configure options are written in
528 to distinguish them from run time environment variables (see section search
534 \begin_inset Quotes sld
538 \begin_inset Quotes srd
541 are used by the SDCC team to build the official Win32 binaries.
542 The SDCC team uses Mingw32 to build the official Windows binaries, because
549 a gcc compiler and last but not least
552 the binaries can be built by cross compiling on Sourceforge's compile farm.
555 See the examples, how to pass the Win32 settings to 'configure'.
556 The other Win32 builds using Borland, VC or whatever don't use 'configure',
557 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
568 <lyxtabular version="3" rows="8" columns="3">
570 <column alignment="left" valignment="top" leftline="true" width="0in">
571 <column alignment="left" valignment="top" leftline="true" width="0in">
572 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
573 <row topline="true" bottomline="true">
574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
618 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
662 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
672 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
722 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
782 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
790 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
799 <row topline="true" bottomline="true">
800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
818 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
837 'configure' also computes relative paths.
838 This is needed for full relocatability of a binary package and to complete
839 search paths (see section search paths below):
845 <lyxtabular version="3" rows="4" columns="3">
847 <column alignment="left" valignment="top" leftline="true" width="0in">
848 <column alignment="left" valignment="top" leftline="true" width="0in">
849 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
850 <row topline="true" bottomline="true">
851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
876 <row topline="true" bottomline="true">
877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
895 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
906 <row bottomline="true">
907 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
917 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
925 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
934 <row bottomline="true">
935 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
953 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
975 ./configure ---prefix=
976 \begin_inset Quotes srd
980 \begin_inset Quotes srd
984 \begin_inset Quotes srd
988 \begin_inset Quotes srd
994 ./configure ---disable-avr-port ---disable-xa51-port
997 To crosscompile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw3
1007 \begin_inset Quotes srd
1010 i586-mingw32msvc-gcc
1011 \begin_inset Quotes srd
1015 \begin_inset Quotes srd
1018 i586-mingw32msvc-g++
1019 \begin_inset Quotes srd
1028 \begin_inset Quotes srd
1031 i586-mingw32msvc-ranlib
1032 \begin_inset Quotes srd
1041 \begin_inset Quotes srd
1044 i586-mingw32msvc-strip
1045 \begin_inset Quotes srd
1054 \begin_inset Quotes srd
1058 \begin_inset Quotes srd
1067 \begin_inset Quotes srd
1071 \begin_inset Quotes srd
1080 \begin_inset Quotes srd
1084 \begin_inset Quotes srd
1093 \begin_inset Quotes srd
1097 \begin_inset Quotes srd
1106 \begin_inset Quotes srd
1110 \begin_inset Quotes srd
1118 sdccconf_h_dir_separator=
1119 \begin_inset Quotes srd
1131 \begin_inset Quotes srd
1139 ---disable-device-lib-build
1149 ---host=i586-mingw32msvc ---build=unknown-unknown-linux-gnu
1153 \begin_inset Quotes sld
1157 \begin_inset Quotes srd
1160 compile on Cygwin for Mingw32(see also sdcc/support/scripts/sdcc_cygwin_mingw32)
1170 \begin_inset Quotes srd
1174 \begin_inset Quotes srd
1183 \begin_inset Quotes srd
1187 \begin_inset Quotes srd
1196 \begin_inset Quotes srd
1200 \begin_inset Quotes srd
1209 \begin_inset Quotes srd
1213 \begin_inset Quotes srd
1222 \begin_inset Quotes srd
1226 \begin_inset Quotes srd
1235 \begin_inset Quotes srd
1239 \begin_inset Quotes srd
1248 \begin_inset Quotes srd
1252 \begin_inset Quotes srd
1260 sdccconf_h_dir_separator=
1261 \begin_inset Quotes srd
1273 \begin_inset Quotes srd
1284 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1285 The option '--C' turns on caching, which gives a little bit extra speed.
1286 However if options are changed, it can be necessary to delete the config.cache
1291 \begin_inset LatexCommand \index{Install paths}
1299 Binary files (preprocessor, assembler and linker)
1303 \begin_inset Tabular
1304 <lyxtabular version="3" rows="2" columns="3">
1306 <column alignment="left" valignment="top" leftline="true" width="0in">
1307 <column alignment="left" valignment="top" leftline="true" width="0in">
1308 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1309 <row topline="true" bottomline="true">
1310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1335 <row topline="true" bottomline="true">
1336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1380 \begin_inset Tabular
1381 <lyxtabular version="3" rows="2" columns="3">
1383 <column alignment="block" valignment="top" leftline="true" width="1.6in">
1384 <column alignment="left" valignment="top" leftline="true" width="0in">
1385 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
1386 <row topline="true" bottomline="true">
1387 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1395 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1403 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1412 <row topline="true" bottomline="true">
1413 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1425 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1430 /usr/local/share/sdcc/include
1433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1459 is auto-appended by the compiler, e.g.
1460 small, large, z80, ds390 etc.)
1464 \begin_inset Tabular
1465 <lyxtabular version="3" rows="2" columns="3">
1467 <column alignment="left" valignment="top" leftline="true" width="0in">
1468 <column alignment="left" valignment="top" leftline="true" width="0in">
1469 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1470 <row topline="true" bottomline="true">
1471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1496 <row topline="true" bottomline="true">
1497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1504 $DATADIR/$LIB_DIR_SUFFIX
1507 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1512 /usr/local/share/sdcc/lib
1515 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1541 \begin_inset Tabular
1542 <lyxtabular version="3" rows="2" columns="3">
1544 <column alignment="left" valignment="top" leftline="true" width="0in">
1545 <column alignment="left" valignment="top" leftline="true" width="0in">
1546 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1547 <row topline="true" bottomline="true">
1548 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1556 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1573 <row topline="true" bottomline="true">
1574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1584 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1589 /usr/local/share/sdcc/doc
1592 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1613 The install paths can still be changed during `make install` with e.g.:
1616 make install prefix=$(HOME)/local/sdcc
1619 Of course this doesn't change the search paths compiled into the binaries.
1623 \begin_inset LatexCommand \index{Search path}
1630 Some search paths or parts of them are determined by configure variables
1635 , see section above).
1636 Further search paths are determined by environment variables during runtime.
1639 The paths searched when running the compiler are as follows (the first catch
1645 Binary files (preprocessor, assembler and linker)
1648 \begin_inset Tabular
1649 <lyxtabular version="3" rows="4" columns="3">
1651 <column alignment="left" valignment="top" leftline="true" width="0in">
1652 <column alignment="left" valignment="top" leftline="true" width="0in">
1653 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1654 <row topline="true" bottomline="true">
1655 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1680 <row topline="true">
1681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1699 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1710 <row topline="true">
1711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1716 Path of argv[0] (if available)
1719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1736 <row topline="true" bottomline="true">
1737 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1774 \begin_inset Tabular
1775 <lyxtabular version="3" rows="6" columns="3">
1777 <column alignment="block" valignment="top" leftline="true" width="1.5in">
1778 <column alignment="block" valignment="top" leftline="true" width="1.5in">
1779 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1780 <row topline="true" bottomline="true">
1781 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1797 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1806 <row topline="true">
1807 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1823 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1832 <row topline="true">
1833 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1841 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1849 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1858 <row topline="true">
1859 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1873 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1885 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1896 <row topline="true">
1897 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1965 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1978 <row topline="true" bottomline="true">
1979 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1995 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2000 /usr/local/share/sdcc/
2005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2022 The option ---nostdinc disables the last two search paths.
2029 With the exception of
2030 \begin_inset Quotes sld
2034 \begin_inset Quotes srd
2041 is auto-appended by the compiler (e.g.
2042 small, large, z80, ds390 etc.).
2046 \begin_inset Tabular
2047 <lyxtabular version="3" rows="6" columns="3">
2049 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2050 <column alignment="left" valignment="top" leftline="true" width="1.2in">
2051 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2052 <row topline="true" bottomline="true">
2053 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2061 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2069 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2078 <row topline="true">
2079 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2104 <row topline="true">
2105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2117 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2144 <row topline="true">
2145 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2156 $LIB_DIR_SUFFIX/<model>
2159 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2173 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2190 <row topline="true">
2191 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2206 $LIB_DIR_SUFFIX/<model>
2209 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2262 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2318 <row topline="true" bottomline="true">
2319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2328 $LIB_DIR_SUFFIX/<model>
2331 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2336 /usr/local/share/sdcc/
2343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2359 Don't delete any of the stray spaces in the table above without checking
2360 the HTML output (last line)!
2366 The option ---nostdlib disables the last two search paths.
2370 \begin_inset LatexCommand \index{Building SDCC}
2375 \layout Subsubsection
2377 Building SDCC on Linux
2382 Download the source package
2384 either from the SDCC CVS repository or from the
2385 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2391 , it will be named something like sdcc
2404 Bring up a command line terminal, such as xterm.
2409 Unpack the file using a command like:
2412 "tar -xzf sdcc.src.tar.gz
2417 , this will create a sub-directory called sdcc with all of the sources.
2420 Change directory into the main SDCC directory, for example type:
2437 This configures the package for compilation on your system.
2453 All of the source packages will compile, this can take a while.
2469 This copies the binary executables, the include files, the libraries and
2470 the documentation to the install directories.
2471 \layout Subsubsection
2473 Building SDCC on OSX 2.x
2476 Follow the instruction for Linux.
2480 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2481 )) fails to compile SDCC.
2482 Fortunately there's also gcc 2.9.x installed, which works fine.
2483 This compiler can be selected by running 'configure' with:
2486 ./configure CC=gcc2 CXX=g++2
2487 \layout Subsubsection
2489 Crosscompiling SDCC on Linux for Windows
2492 With the Mingw32 gcc crosscompiler it's easy to compile SDCC for Win32.
2493 See section 'Configure Options'.
2494 \layout Subsubsection
2496 Building SDCC on Windows
2499 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2501 They use Unix-sockets, which are not available on Win32.
2502 \layout Subsubsection
2504 Windows Install Using a Binary Package
2507 Download the binary package and unpack it using your favorite unpacking
2508 tool (gunzip, WinZip, etc).
2509 This should unpack to a group of sub-directories.
2510 An example directory structure after unpacking the mingw32 package is:
2515 bin for the executables, c:
2523 lib for the include and libraries.
2526 Adjust your environment variable PATH to include the location of the bin
2527 directory or start sdcc using the full path.
2528 \layout Subsubsection
2530 Building SDCC using Cygwin and Mingw32
2533 For building and installing a Cygwin executable follow the instructions
2539 \begin_inset Quotes sld
2543 \begin_inset Quotes srd
2546 Win32-binary can be built, which will not need the Cygwin-DLL.
2547 For the necessary 'configure' options see section 'configure options' or
2548 the script 'sdcc/support/scripts/sdcc_cygwinmingw32'.
2552 In order to install Cygwin on Windows download setup.exe from
2553 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2559 \begin_inset Quotes sld
2562 default text file type
2563 \begin_inset Quotes srd
2567 \begin_inset Quotes sld
2571 \begin_inset Quotes srd
2574 and download/install at least the following packages.
2575 Some packages are selected by default, others will be automatically selected
2576 because of dependencies with the manually selected packages.
2577 Never deselect these packages!
2586 gcc ; version 3.x is fine, no need to use the old 2.9x
2589 binutils ; selected with gcc
2595 rxvt ; a nice console, which makes life much easier under windoze (see below)
2598 man ; not really needed for building SDCC, but you'll miss it sooner or
2602 less ; not really needed for building SDCC, but you'll miss it sooner or
2606 cvs ; only if you use CVS access
2609 If you want to develop something you'll need:
2612 python ; for the regression tests
2615 gdb ; the gnu debugger, together with the nice GUI
2616 \begin_inset Quotes sld
2620 \begin_inset Quotes srd
2626 openssh ; to access the CF or commit changes
2629 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
2630 use autoconf-stable!
2633 rxvt is a nice console with history.
2634 Replace in your cygwin.bat the line
2643 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
2646 -bg black -fg white -geometry 100x65 -e bash --login
2649 Text selected with the mouse is automatically copied to the clipboard, pasting
2650 works with shift-insert.
2654 The other good tip is to make sure you have no //c/-style paths anywhere,
2655 use /cygdrive/c/ instead.
2656 Using // invokes a network lookup which is very slow.
2658 \begin_inset Quotes sld
2662 \begin_inset Quotes srd
2665 is too long, you can change it with e.g.
2671 SDCC sources use the unix line ending LF.
2672 Life is much easier, if you store the source tree on a drive, which is
2673 mount in binary mode.
2674 And use an editor which can handle LF-only line endings.
2675 Make sure not to commit files with windows line endings.
2676 \layout Subsubsection
2678 Windows Install Using Microsoft Visual C++ 6.0/NET
2683 Download the source package
2685 either from the SDCC CVS repository or from the
2686 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2692 , it will be named something like sdcc
2699 SDCC is distributed with all the projects, workspaces, and files you need
2700 to build it using Visual C++ 6.0/NET.
2701 The workspace name is 'sdcc.dsw'.
2702 Please note that as it is now, all the executables are created in a folder
2706 Once built you need to copy the executables from sdcc
2710 bin before runnng SDCC.
2715 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
2716 of bison.exe, flex.exe, and gawk.exe.
2717 One good place to get them is
2718 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
2726 Download the file UnxUtils
2727 \begin_inset LatexCommand \index{UnxUtils}
2732 Now you have to install the utilities and setup Visual C++ so it can locate
2733 the required programs.
2734 Here there are two alternatives (choose one!):
2741 a) Extract UnxUtils.zip to your C:
2743 hard disk PRESERVING the original paths, otherwise bison won't work.
2744 (If you are using WinZip make certain that 'Use folder names' is selected)
2748 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
2749 in 'Show directories for:' select 'Executable files', and in the directories
2750 window add a new path: 'C:
2760 (As a side effect, you get a bunch of Unix utilities that could be useful,
2761 such as diff and patch.)
2768 This one avoids extracting a bunch of files you may not use, but requires
2773 a) Create a directory were to put the tools needed, or use a directory already
2781 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
2782 to such directory WITHOUT preserving the original paths.
2783 (If you are using WinZip make certain that 'Use folder names' is not selected)
2787 c) Rename bison.exe to '_bison.exe'.
2791 d) Create a batch file 'bison.bat' in 'C:
2795 ' and add these lines:
2815 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
2819 Steps 'c' and 'd' are needed because bison requires by default that the
2820 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
2821 '/usr/local/share/' I think.
2822 So it is necessary to tell bison where those files are located if they
2823 are not in such directory.
2824 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
2828 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
2829 in 'Show directories for:' select 'Executable files', and in the directories
2830 window add a new path: 'c:
2833 Note that you can use any other path instead of 'c:
2835 util', even the path where the Visual C++ tools are, probably: 'C:
2839 Microsoft Visual Studio
2844 So you don't have to execute step 'e' :)
2848 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
2849 the executables from sdcc
2853 bin, and you can compile using sdcc.
2854 \layout Subsubsection
2856 Windows Install Using Borland
2859 From the sdcc directory, run the command "make -f Makefile.bcc".
2860 This should regenerate all the .exe files in the bin directory except for
2861 sdcdb.exe (which currently doesn't build under Borland C++).
2864 If you modify any source files and need to rebuild, be aware that the dependanci
2865 es may not be correctly calculated.
2866 The safest option is to delete all .obj files and run the build again.
2867 From a Cygwin BASH prompt, this can easily be done with the commmand:
2877 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
2879 ) -print -exec rm {}
2888 or on Windows NT/2000/XP from the command prompt with the commmand:
2895 del /s *.obj *.lib *.rul
2898 from the sdcc directory.
2901 Building the Documentation
2908 Testing out the SDCC Compiler
2911 The first thing you should do after installing your SDCC compiler is to
2917 \begin_inset LatexCommand \index{version}
2924 at the prompt, and the program should run and tell you the version.
2925 If it doesn't run, or gives a message about not finding sdcc program, then
2926 you need to check over your installation.
2927 Make sure that the sdcc bin directory is in your executable search path
2928 defined by the PATH environment setting (see the Trouble-shooting section
2930 Make sure that the sdcc program is in the bin folder, if not perhaps something
2931 did not install correctly.
2939 is commonly installed as described in section
2940 \begin_inset Quotes sld
2943 Install and search paths
2944 \begin_inset Quotes srd
2953 Make sure the compiler works on a very simple example.
2954 Type in the following test.c program using your favorite
2989 Compile this using the following command:
2998 If all goes well, the compiler will generate a test.asm and test.rel file.
2999 Congratulations, you've just compiled your first program with SDCC.
3000 We used the -c option to tell SDCC not to link the generated code, just
3001 to keep things simple for this step.
3009 The next step is to try it with the linker.
3019 If all goes well the compiler will link with the libraries and produce
3020 a test.ihx output file.
3025 (no test.ihx, and the linker generates warnings), then the problem is most
3026 likely that sdcc cannot find the
3030 usr/local/share/sdcc/lib directory
3034 (see the Install trouble-shooting section for suggestions).
3042 The final test is to ensure sdcc can use the
3046 header files and libraries.
3047 Edit test.c and change it to the following:
3067 strcpy(str1, "testing");
3076 Compile this by typing
3083 This should generate a test.ihx output file, and it should give no warnings
3084 such as not finding the string.h file.
3085 If it cannot find the string.h file, then the problem is that sdcc cannot
3086 find the /usr/local/share/sdcc/include directory
3090 (see the Install trouble-shooting section for suggestions).
3093 Install Trouble-shooting
3094 \begin_inset LatexCommand \index{Install trouble-shooting}
3099 \layout Subsubsection
3101 SDCC does not build correctly.
3104 A thing to try is starting from scratch by unpacking the .tgz source package
3105 again in an empty directory.
3113 ./configure 2>&1 | tee configure.log
3127 make 2>&1 | tee make.log
3134 If anything goes wrong, you can review the log files to locate the problem.
3135 Or a relevant part of this can be attached to an email that could be helpful
3136 when requesting help from the mailing list.
3137 \layout Subsubsection
3140 \begin_inset Quotes sld
3144 \begin_inset Quotes srd
3151 \begin_inset Quotes sld
3155 \begin_inset Quotes srd
3158 command is a script that analyzes your system and performs some configuration
3159 to ensure the source package compiles on your system.
3160 It will take a few minutes to run, and will compile a few tests to determine
3161 what compiler features are installed.
3162 \layout Subsubsection
3165 \begin_inset Quotes sld
3169 \begin_inset Quotes srd
3175 This runs the GNU make tool, which automatically compiles all the source
3176 packages into the final installed binary executables.
3177 \layout Subsubsection
3180 \begin_inset Quotes sld
3184 \begin_inset Quotes erd
3190 This will install the compiler, other executables libraries and include
3191 files in to the appropriate directories.
3193 \begin_inset Quotes sld
3196 Install and Search PATHS
3197 \begin_inset Quotes srd
3202 On most systems you will need super-user privilages to do this.
3208 SDCC is not just a compiler, but a collection of tools by various developers.
3209 These include linkers, assemblers, simulators and other components.
3210 Here is a summary of some of the components.
3211 Note that the included simulator and assembler have separate documentation
3212 which you can find in the source package in their respective directories.
3213 As SDCC grows to include support for other processors, other packages from
3214 various developers are included and may have their own sets of documentation.
3218 You might want to look at the files which are installed in <installdir>.
3219 At the time of this writing, we find the following programs for gcc-builds:
3223 In <installdir>/bin:
3226 sdcc - The compiler.
3229 sdcpp - The C preprocessor.
3232 asx8051 - The assembler for 8051 type processors.
3239 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3242 aslink -The linker for 8051 type processors.
3249 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3252 s51 - The ucSim 8051 simulator.
3255 sdcdb - The source debugger.
3258 packihx - A tool to pack (compress) Intel hex files.
3261 In <installdir>/share/sdcc/include
3267 In <installdir>/share/sdcc/lib
3270 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3274 In <installdir>/share/sdcc/doc
3280 As development for other processors proceeds, this list will expand to include
3281 executables to support processors like AVR, PIC, etc.
3282 \layout Subsubsection
3287 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3288 the assembler and linkage editor.
3289 \layout Subsubsection
3292 \begin_inset LatexCommand \index{sdcpp}
3296 - The C-Preprocessor
3299 The preprocessor is a modified version of the GNU preprocessor.
3300 The C preprocessor is used to pull in #include sources, process #ifdef
3301 statements, #defines and so on.
3302 \layout Subsubsection
3304 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3308 This is retargettable assembler & linkage editor, it was developed by Alan
3310 John Hartman created the version for 8051, and I (Sandeep) have made some
3311 enhancements and bug fixes for it to work properly with the SDCC.
3312 \layout Subsubsection
3315 \begin_inset LatexCommand \index{s51}
3322 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3323 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3328 The simulator is built as part of the build process.
3329 For more information visit Daniel's website at:
3330 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3335 It currently support the core mcs51, the Dallas DS80C390 and the Philips
3337 \layout Subsubsection
3340 \begin_inset LatexCommand \index{sdcdb}
3344 - Source Level Debugger
3347 Sdcdb is the companion source level debugger.
3348 The current version of the debugger uses Daniel's Simulator S51, but can
3349 be easily changed to use other simulators.
3356 \layout Subsubsection
3358 Single Source File Projects
3361 For single source file 8051 projects the process is very simple.
3362 Compile your programs with the following command
3365 "sdcc sourcefile.c".
3369 This will compile, assemble and link your source file.
3370 Output files are as follows
3374 \begin_inset LatexCommand \index{.asm}
3379 \begin_inset LatexCommand \index{Assembler source}
3383 file created by the compiler
3387 \begin_inset LatexCommand \index{.lst}
3392 \begin_inset LatexCommand \index{Assembler listing}
3396 file created by the Assembler
3400 \begin_inset LatexCommand \index{.rst}
3405 \begin_inset LatexCommand \index{Assembler listing}
3409 file updated with linkedit information, created by linkage editor
3413 \begin_inset LatexCommand \index{.sym}
3418 \begin_inset LatexCommand \index{Symbol listing}
3422 for the sourcefile, created by the assembler
3426 \begin_inset LatexCommand \index{.rel}
3431 \begin_inset LatexCommand \index{Object file}
3435 created by the assembler, input to Linkage editor
3439 \begin_inset LatexCommand \index{.map}
3444 \begin_inset LatexCommand \index{Memory map}
3448 for the load module, created by the Linker
3452 \begin_inset LatexCommand \index{.mem}
3456 - A file with a summary of the memory usage
3460 \begin_inset LatexCommand \index{.ihx}
3464 - The load module in Intel hex format
3465 \begin_inset LatexCommand \index{Intel hex format}
3469 (you can select the Motorola S19 format
3470 \begin_inset LatexCommand \index{Motorola S19 format}
3475 \begin_inset LatexCommand \index{---out-fmt-s19}
3480 If you need another format you might want to use objdump
3481 \begin_inset LatexCommand \index{objdump}
3486 \begin_inset LatexCommand \index{srecord}
3494 \begin_inset LatexCommand \index{.adb}
3498 - An intermediate file containing debug information needed to create the
3499 .cdb file (with ---debug
3500 \begin_inset LatexCommand \index{---debug}
3508 \begin_inset LatexCommand \index{.cdb}
3512 - An optional file (with ---debug) containing debug information
3517 \begin_inset LatexCommand \index{. (no extension)}
3521 An optional AOMF51 file containing debug information (with ---debug)
3525 \begin_inset LatexCommand \index{.dump*}
3529 - Dump file to debug the compiler it self (with ---dumpall) (see section
3531 \begin_inset Quotes sld
3534 Anatomy of the compiler
3535 \begin_inset Quotes srd
3539 \layout Subsubsection
3541 Projects with Multiple Source Files
3544 SDCC can compile only ONE file at a time.
3545 Let us for example assume that you have a project containing the following
3550 foo1.c (contains some functions)
3552 foo2.c (contains some more functions)
3554 foomain.c (contains more functions and the function main)
3562 The first two files will need to be compiled separately with the commands:
3594 Then compile the source file containing the
3599 \begin_inset LatexCommand \index{Linker}
3603 the files together with the following command:
3611 foomain.c\SpecialChar ~
3612 foo1.rel\SpecialChar ~
3617 \begin_inset LatexCommand \index{.rel}
3629 can be separately compiled as well:
3640 sdcc foomain.rel foo1.rel foo2.rel
3647 The file containing the
3662 file specified in the command line, since the linkage editor processes
3663 file in the order they are presented to it.
3664 \layout Subsubsection
3666 Projects with Additional Libraries
3667 \begin_inset LatexCommand \index{Libraries}
3674 Some reusable routines may be compiled into a library, see the documentation
3675 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
3679 \begin_inset LatexCommand \index{.lib}
3686 Libraries created in this manner can be included in the command line.
3687 Make sure you include the -L <library-path> option to tell the linker where
3688 to look for these files if they are not in the current directory.
3689 Here is an example, assuming you have the source file
3701 (if that is not the same as your current project):
3708 sdcc foomain.c foolib.lib -L mylib
3719 must be an absolute path name.
3723 The most efficient way to use libraries is to keep seperate modules in seperate
3725 The lib file now should name all the modules.rel files.
3726 For an example see the standard library file
3730 in the directory <installdir>/share/lib/small.
3733 Command Line Options
3734 \begin_inset LatexCommand \index{Command Line Options}
3739 \layout Subsubsection
3741 Processor Selection Options
3742 \begin_inset LatexCommand \index{Options processor selection}
3747 \begin_inset LatexCommand \index{Processor selection options}
3753 \labelwidthstring 00.00.0000
3758 \begin_inset LatexCommand \index{-mmcs51}
3764 Generate code for the MCS51
3765 \begin_inset LatexCommand \index{MCS51}
3769 family of processors.
3770 This is the default processor target.
3772 \labelwidthstring 00.00.0000
3777 \begin_inset LatexCommand \index{-mds390}
3783 Generate code for the DS80C390
3784 \begin_inset LatexCommand \index{DS80C390}
3790 \labelwidthstring 00.00.0000
3795 \begin_inset LatexCommand \index{-mds400}
3801 Generate code for the DS80C400
3802 \begin_inset LatexCommand \index{DS80C400}
3808 \labelwidthstring 00.00.0000
3813 \begin_inset LatexCommand \index{-mz80}
3819 Generate code for the Z80
3820 \begin_inset LatexCommand \index{Z80}
3824 family of processors.
3826 \labelwidthstring 00.00.0000
3831 \begin_inset LatexCommand \index{-mgbz80}
3837 Generate code for the GameBoy Z80
3838 \begin_inset LatexCommand \index{GameBoy Z80}
3844 \labelwidthstring 00.00.0000
3849 \begin_inset LatexCommand \index{-mavr}
3855 Generate code for the Atmel AVR
3856 \begin_inset LatexCommand \index{AVR}
3860 processor (In development, not complete).
3861 AVR users should probably have a look at avr-gcc
3862 \begin_inset LatexCommand \url[FIXME: official URL?]{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
3869 I think it is fair to direct users there for now.
3870 Open source is also about avoiding unnecessary work .
3871 But I didn't find the 'official' link.
3873 \labelwidthstring 00.00.0000
3878 \begin_inset LatexCommand \index{-mpic14}
3884 Generate code for the PIC 14
3885 \begin_inset LatexCommand \index{PIC14}
3889 -bit processors (In development, not complete).
3892 p16f627 p16f628 p16f84 p16f873 p16f877?
3894 \labelwidthstring 00.00.0000
3900 Generate code for the Toshiba TLCS-900H
3901 \begin_inset LatexCommand \index{TLCS-900H}
3905 processor (In development, not complete).
3907 \labelwidthstring 00.00.0000
3912 \begin_inset LatexCommand \index{-mxa51}
3918 Generate code for the Philips XA51
3919 \begin_inset LatexCommand \index{XA51}
3923 processor (In development, not complete).
3924 \layout Subsubsection
3926 Preprocessor Options
3927 \begin_inset LatexCommand \index{Options preprocessor}
3932 \begin_inset LatexCommand \index{Preprocessor options}
3938 \labelwidthstring 00.00.0000
3943 \begin_inset LatexCommand \index{-I<path>}
3949 The additional location where the pre processor will look for <..h> or
3950 \begin_inset Quotes eld
3954 \begin_inset Quotes erd
3959 \labelwidthstring 00.00.0000
3964 \begin_inset LatexCommand \index{-D<macro[=value]>}
3970 Command line definition of macros.
3971 Passed to the pre processor.
3973 \labelwidthstring 00.00.0000
3978 \begin_inset LatexCommand \index{-M}
3984 Tell the preprocessor to output a rule suitable for make describing the
3985 dependencies of each object file.
3986 For each source file, the preprocessor outputs one make-rule whose target
3987 is the object file name for that source file and whose dependencies are
3988 all the files `#include'd in it.
3989 This rule may be a single line or may be continued with `
3991 '-newline if it is long.
3992 The list of rules is printed on standard output instead of the preprocessed
3996 \labelwidthstring 00.00.0000
4001 \begin_inset LatexCommand \index{-C}
4007 Tell the preprocessor not to discard comments.
4008 Used with the `-E' option.
4010 \labelwidthstring 00.00.0000
4015 \begin_inset LatexCommand \index{-MM}
4026 Like `-M' but the output mentions only the user header files included with
4028 \begin_inset Quotes eld
4032 System header files included with `#include <file>' are omitted.
4034 \labelwidthstring 00.00.0000
4039 \begin_inset LatexCommand \index{-Aquestion(answer)}
4045 Assert the answer answer for question, in case it is tested with a preprocessor
4046 conditional such as `#if #question(answer)'.
4047 `-A-' disables the standard assertions that normally describe the target
4050 \labelwidthstring 00.00.0000
4056 (answer) Assert the answer answer for question, in case it is tested with
4057 a preprocessor conditional such as `#if #question(answer)'.
4058 `-A-' disables the standard assertions that normally describe the target
4061 \labelwidthstring 00.00.0000
4066 \begin_inset LatexCommand \index{-Umacro}
4072 Undefine macro macro.
4073 `-U' options are evaluated after all `-D' options, but before any `-include'
4074 and `-imacros' options.
4076 \labelwidthstring 00.00.0000
4081 \begin_inset LatexCommand \index{-dM}
4087 Tell the preprocessor to output only a list of the macro definitions that
4088 are in effect at the end of preprocessing.
4089 Used with the `-E' option.
4091 \labelwidthstring 00.00.0000
4096 \begin_inset LatexCommand \index{-dD}
4102 Tell the preprocessor to pass all macro definitions into the output, in
4103 their proper sequence in the rest of the output.
4105 \labelwidthstring 00.00.0000
4110 \begin_inset LatexCommand \index{-dN}
4121 Like `-dD' except that the macro arguments and contents are omitted.
4122 Only `#define name' is included in the output.
4123 \layout Subsubsection
4126 \begin_inset LatexCommand \index{Options linker}
4131 \begin_inset LatexCommand \index{Linker options}
4137 \labelwidthstring 00.00.0000
4143 \begin_inset LatexCommand \index{---lib-path}
4148 \begin_inset LatexCommand \index{-L ---lib-path}
4157 <absolute path to additional libraries> This option is passed to the linkage
4158 editor's additional libraries
4159 \begin_inset LatexCommand \index{Libraries}
4164 The path name must be absolute.
4165 Additional library files may be specified in the command line.
4166 See section Compiling programs for more details.
4168 \labelwidthstring 00.00.0000
4175 \begin_inset LatexCommand \index{---xram-loc}
4179 <Value> The start location of the external ram
4180 \begin_inset LatexCommand \index{xdata}
4184 , default value is 0.
4185 The value entered can be in Hexadecimal or Decimal format, e.g.: ---xram-loc
4186 0x8000 or ---xram-loc 32768.
4188 \labelwidthstring 00.00.0000
4195 \begin_inset LatexCommand \index{---code-loc}
4199 <Value> The start location of the code
4200 \begin_inset LatexCommand \index{code}
4204 segment, default value 0.
4205 Note when this option is used the interrupt vector table is also relocated
4206 to the given address.
4207 The value entered can be in Hexadecimal or Decimal format, e.g.: ---code-loc
4208 0x8000 or ---code-loc 32768.
4210 \labelwidthstring 00.00.0000
4217 \begin_inset LatexCommand \index{---stack-loc}
4221 <Value> By default the stack
4222 \begin_inset LatexCommand \index{stack}
4226 is placed after the data segment.
4227 Using this option the stack can be placed anywhere in the internal memory
4229 The value entered can be in Hexadecimal or Decimal format, e.g.
4230 ---stack-loc 0x20 or ---stack-loc 32.
4231 Since the sp register is incremented before a push or call, the initial
4232 sp will be set to one byte prior the provided value.
4233 The provided value should not overlap any other memory areas such as used
4234 register banks or the data segment and with enough space for the current
4237 \labelwidthstring 00.00.0000
4244 \begin_inset LatexCommand \index{---data-loc}
4248 <Value> The start location of the internal ram data
4249 \begin_inset LatexCommand \index{data}
4254 The value entered can be in Hexadecimal or Decimal format, eg.
4255 ---data-loc 0x20 or ---data-loc 32.
4256 (By default, the start location of the internal ram data segment is set
4257 as low as possible in memory, taking into account the used register banks
4258 and the bit segment at address 0x20.
4259 For example if register banks 0 and 1 are used without bit variables, the
4260 data segment will be set, if ---data-loc is not used, to location 0x10.)
4262 \labelwidthstring 00.00.0000
4269 \begin_inset LatexCommand \index{---idata-loc}
4273 <Value> The start location of the indirectly addressable internal ram
4274 \begin_inset LatexCommand \index{idata}
4278 , default value is 0x80.
4279 The value entered can be in Hexadecimal or Decimal format, eg.
4280 ---idata-loc 0x88 or ---idata-loc 136.
4282 \labelwidthstring 00.00.0000
4287 \begin_inset LatexCommand \index{---out-fmt-ihx}
4296 The linker output (final object code) is in Intel Hex format.
4297 \begin_inset LatexCommand \index{Intel hex format}
4301 (This is the default option).
4303 \labelwidthstring 00.00.0000
4308 \begin_inset LatexCommand \index{---out-fmt-s19}
4317 The linker output (final object code) is in Motorola S19 format
4318 \begin_inset LatexCommand \index{Motorola S19 format}
4323 \layout Subsubsection
4326 \begin_inset LatexCommand \index{Options MCS51}
4331 \begin_inset LatexCommand \index{MCS51 options}
4337 \labelwidthstring 00.00.0000
4342 \begin_inset LatexCommand \index{---model-large}
4348 Generate code for Large model programs see section Memory Models for more
4350 If this option is used all source files in the project should be compiled
4352 In addition the standard library routines are compiled with small model,
4353 they will need to be recompiled.
4355 \labelwidthstring 00.00.0000
4360 \begin_inset LatexCommand \index{---model-small}
4371 Generate code for Small Model programs see section Memory Models for more
4373 This is the default model.
4374 \layout Subsubsection
4377 \begin_inset LatexCommand \index{Options DS390}
4382 \begin_inset LatexCommand \index{DS390 options}
4388 \labelwidthstring 00.00.0000
4395 \begin_inset LatexCommand \index{---model-flat24}
4405 Generate 24-bit flat mode code.
4406 This is the one and only that the ds390 code generator supports right now
4407 and is default when using
4412 See section Memory Models for more details.
4414 \labelwidthstring 00.00.0000
4421 \begin_inset LatexCommand \index{--stack-10bit}
4425 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
4426 This is the one and only that the ds390 code generator supports right now
4427 and is default when using
4432 In this mode, the stack is located in the lower 1K of the internal RAM,
4433 which is mapped to 0x400000.
4434 Note that the support is incomplete, since it still uses a single byte
4435 as the stack pointer.
4436 This means that only the lower 256 bytes of the potential 1K stack space
4437 will actually be used.
4438 However, this does allow you to reclaim the precious 256 bytes of low RAM
4439 for use for the DATA and IDATA segments.
4440 The compiler will not generate any code to put the processor into 10 bit
4442 It is important to ensure that the processor is in this mode before calling
4443 any re-entrant functions compiled with this option.
4444 In principle, this should work with the
4447 \begin_inset LatexCommand \index{---stack-auto}
4453 option, but that has not been tested.
4454 It is incompatible with the
4457 \begin_inset LatexCommand \index{---xstack}
4464 It also only makes sense if the processor is in 24 bit contiguous addressing
4467 ---model-flat24 option
4470 \layout Subsubsection
4472 Optimization Options
4473 \begin_inset LatexCommand \index{Options optimization}
4478 \begin_inset LatexCommand \index{Optimization options}
4484 \labelwidthstring 00.00.0000
4489 \begin_inset LatexCommand \index{---nogcse}
4495 Will not do global subexpression elimination, this option may be used when
4496 the compiler creates undesirably large stack/data spaces to store compiler
4498 A warning message will be generated when this happens and the compiler
4499 will indicate the number of extra bytes it allocated.
4500 It recommended that this option NOT be used, #pragma\SpecialChar ~
4502 \begin_inset LatexCommand \index{\#pragma NOGCSE}
4506 can be used to turn off global subexpression elimination
4507 \begin_inset LatexCommand \index{Subexpression elimination}
4511 for a given function only.
4513 \labelwidthstring 00.00.0000
4518 \begin_inset LatexCommand \index{---noinvariant}
4524 Will not do loop invariant optimizations, this may be turned off for reasons
4525 explained for the previous option.
4526 For more details of loop optimizations performed see section Loop Invariants.It
4527 recommended that this option NOT be used, #pragma\SpecialChar ~
4529 \begin_inset LatexCommand \index{\#pragma NOINVARIANT}
4533 can be used to turn off invariant optimizations for a given function only.
4535 \labelwidthstring 00.00.0000
4540 \begin_inset LatexCommand \index{---noinduction}
4546 Will not do loop induction optimizations, see section strength reduction
4547 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
4550 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
4554 can be used to turn off induction optimizations for a given function only.
4556 \labelwidthstring 00.00.0000
4561 \begin_inset LatexCommand \index{---nojtbound}
4572 Will not generate boundary condition check when switch statements
4573 \begin_inset LatexCommand \index{switch statement}
4577 are implemented using jump-tables.
4578 See section Switch Statements for more details.
4579 It is recommended that this option is NOT used, #pragma\SpecialChar ~
4581 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
4585 can be used to turn off boundary checking for jump tables for a given function
4588 \labelwidthstring 00.00.0000
4593 \begin_inset LatexCommand \index{---noloopreverse}
4602 Will not do loop reversal
4603 \begin_inset LatexCommand \index{Loop reversing}
4609 \labelwidthstring 00.00.0000
4616 \begin_inset LatexCommand \index{---nolabelopt }
4620 Will not optimize labels (makes the dumpfiles more readable).
4622 \labelwidthstring 00.00.0000
4627 \begin_inset LatexCommand \index{---no-xinit-opt}
4633 Will not memcpy initialized data in far space from code space.
4634 This saves a few bytes in code space if you don't have initialized data.
4635 \layout Subsubsection
4638 \begin_inset LatexCommand \index{Options other}
4644 \labelwidthstring 00.00.0000
4650 \begin_inset LatexCommand \index{---compile-only}
4655 \begin_inset LatexCommand \index{-c ---compile-only}
4661 will compile and assemble the source, but will not call the linkage editor.
4663 \labelwidthstring 00.00.0000
4668 \begin_inset LatexCommand \index{--c1mode}
4674 reads the preprocessed source from standard input and compiles it.
4675 The file name for the assembler output must be specified using the -o option.
4677 \labelwidthstring 00.00.0000
4682 \begin_inset LatexCommand \index{-E}
4688 Run only the C preprocessor.
4689 Preprocess all the C source files specified and output the results to standard
4692 \labelwidthstring 00.00.0000
4698 \begin_inset LatexCommand \index{-o <path/file>}
4704 The output path resp.
4705 file where everything will be placed.
4706 If the parameter is a path, it must have a trailing slash (or backslash
4707 for the Windows binaries) to be recognized as a path.
4710 \labelwidthstring 00.00.0000
4715 \begin_inset LatexCommand \index{---stack-auto}
4726 All functions in the source file will be compiled as
4731 \begin_inset LatexCommand \index{reentrant}
4736 the parameters and local variables will be allocated on the stack
4737 \begin_inset LatexCommand \index{stack}
4742 see section Parameters and Local Variables for more details.
4743 If this option is used all source files in the project should be compiled
4747 \labelwidthstring 00.00.0000
4752 \begin_inset LatexCommand \index{---xstack}
4758 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
4759 variables and passing parameters.
4760 See section on external stack for more details.
4762 \labelwidthstring 00.00.0000
4767 \begin_inset LatexCommand \index{---callee-saves}
4772 \begin_inset LatexCommand \index{function prologue}
4776 function1[,function2][,function3]....
4779 The compiler by default uses a caller saves convention for register saving
4780 across function calls, however this can cause unneccessary register pushing
4781 & popping when calling small functions from larger functions.
4782 This option can be used to switch the register saving convention for the
4783 function names specified.
4784 The compiler will not save registers when calling these functions, no extra
4785 code will be generated at the entry & exit for these functions to save
4786 & restore the registers used by these functions, this can SUBSTANTIALLY
4787 reduce code & improve run time performance of the generated code.
4788 In the future the compiler (with interprocedural analysis) will be able
4789 to determine the appropriate scheme to use for each function call.
4790 DO NOT use this option for built-in functions such as _muluint..., if this
4791 option is used for a library function the appropriate library function
4792 needs to be recompiled with the same option.
4793 If the project consists of multiple source files then all the source file
4794 should be compiled with the same ---callee-saves option string.
4795 Also see #pragma\SpecialChar ~
4797 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
4803 \labelwidthstring 00.00.0000
4808 \begin_inset LatexCommand \index{---debug}
4817 When this option is used the compiler will generate debug information, that
4818 can be used with the SDCDB.
4819 The debug information is collected in a file with .cdb extension.
4820 For more information see documentation for SDCDB.
4822 \labelwidthstring 00.00.0000
4829 \begin_inset LatexCommand \index{---peep-file}
4833 <filename> This option can be used to use additional rules to be used by
4834 the peep hole optimizer.
4835 See section Peep Hole optimizations for details on how to write these rules.
4837 \labelwidthstring 00.00.0000
4842 \begin_inset LatexCommand \index{-S}
4853 Stop after the stage of compilation proper; do not assemble.
4854 The output is an assembler code file for the input file specified.
4856 \labelwidthstring 00.00.0000
4860 -Wa_asmOption[,asmOption]
4863 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
4868 Pass the asmOption to the assembler.
4870 \labelwidthstring 00.00.0000
4874 -Wl_linkOption[,linkOption]
4877 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
4882 Pass the linkOption to the linker.
4884 \labelwidthstring 00.00.0000
4889 \begin_inset LatexCommand \index{---int-long-reent}
4895 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
4896 Note by default these libraries are compiled as non-reentrant.
4897 See section Installation for more details.
4899 \labelwidthstring 00.00.0000
4904 \begin_inset LatexCommand \index{---cyclomatic}
4913 This option will cause the compiler to generate an information message for
4914 each function in the source file.
4915 The message contains some
4919 information about the function.
4920 The number of edges and nodes the compiler detected in the control flow
4921 graph of the function, and most importantly the
4923 cyclomatic complexity
4924 \begin_inset LatexCommand \index{Cyclomatic complexity}
4930 see section on Cyclomatic Complexity for more details.
4932 \labelwidthstring 00.00.0000
4937 \begin_inset LatexCommand \index{---float-reent}
4946 Floating point library is compiled as reentrant
4947 \begin_inset LatexCommand \index{reentrant}
4951 .See section Installation for more details.
4953 \labelwidthstring 00.00.0000
4958 \begin_inset LatexCommand \index{---nooverlay}
4964 The compiler will not overlay parameters and local variables of any function,
4965 see section Parameters and local variables for more details.
4967 \labelwidthstring 00.00.0000
4972 \begin_inset LatexCommand \index{---main-return}
4978 This option can be used when the code generated is called by a monitor
4980 The compiler will generate a 'ret' upon return from the 'main'
4981 \begin_inset LatexCommand \index{main return}
4986 The default option is to lock up i.e.
4989 \labelwidthstring 00.00.0000
4994 \begin_inset LatexCommand \index{---no-peep}
5000 Disable peep-hole optimization.
5002 \labelwidthstring 00.00.0000
5007 \begin_inset LatexCommand \index{---peep-asm}
5013 Pass the inline assembler code through the peep hole optimizer.
5014 This can cause unexpected changes to inline assembler code, please go through
5015 the peephole optimizer
5016 \begin_inset LatexCommand \index{Peephole optimizer}
5020 rules defined in the source file tree '<target>/peeph.def' before using
5023 \labelwidthstring 00.00.0000
5030 \begin_inset LatexCommand \index{---iram-size<Value>}
5034 Causes the linker to check if the internal ram usage is within limits of
5037 \labelwidthstring 00.00.0000
5044 \begin_inset LatexCommand \index{---xram-size<Value>}
5048 Causes the linker to check if the external ram usage is within limits of
5051 \labelwidthstring 00.00.0000
5058 \begin_inset LatexCommand \index{---code-size<Value>}
5062 Causes the linker to check if the code usage is within limits of the given
5065 \labelwidthstring 00.00.0000
5070 \begin_inset LatexCommand \index{---nostdincl}
5076 This will prevent the compiler from passing on the default include path
5077 to the preprocessor.
5079 \labelwidthstring 00.00.0000
5084 \begin_inset LatexCommand \index{---nostdlib}
5090 This will prevent the compiler from passing on the default library
5091 \begin_inset LatexCommand \index{Libraries}
5097 \labelwidthstring 00.00.0000
5102 \begin_inset LatexCommand \index{---verbose}
5108 Shows the various actions the compiler is performing.
5110 \labelwidthstring 00.00.0000
5115 \begin_inset LatexCommand \index{-V}
5121 Shows the actual commands the compiler is executing.
5123 \labelwidthstring 00.00.0000
5128 \begin_inset LatexCommand \index{---no-c-code-in-asm}
5134 Hides your ugly and inefficient c-code from the asm file, so you can always
5135 blame the compiler :).
5137 \labelwidthstring 00.00.0000
5142 \begin_inset LatexCommand \index{---i-code-in-asm}
5148 Include i-codes in the asm file.
5149 Sounds like noise but is most helpfull for debugging the compiler itself.
5151 \labelwidthstring 00.00.0000
5156 \begin_inset LatexCommand \index{---less-pedantic}
5162 Disable some of the more pedantic warnings (jwk burps: please be more specific
5165 \labelwidthstring 00.00.0000
5169 ---print-search-dirs
5170 \begin_inset LatexCommand \index{---print-search-dirs}
5176 Display the directories in the compiler's search path
5177 \layout Subsubsection
5179 Intermediate Dump Options
5180 \begin_inset LatexCommand \index{Options intermediate dump}
5185 \begin_inset LatexCommand \index{Intermediate dump options}
5192 The following options are provided for the purpose of retargetting and debugging
5194 These provided a means to dump the intermediate code (iCode
5195 \begin_inset LatexCommand \index{iCode}
5199 ) generated by the compiler in human readable form at various stages of
5200 the compilation process.
5203 \labelwidthstring 00.00.0000
5208 \begin_inset LatexCommand \index{---dumpraw}
5214 This option will cause the compiler to dump the intermediate code into
5217 <source filename>.dumpraw
5219 just after the intermediate code has been generated for a function, i.e.
5220 before any optimizations are done.
5222 \begin_inset LatexCommand \index{Basic blocks}
5226 at this stage ordered in the depth first number, so they may not be in
5227 sequence of execution.
5229 \labelwidthstring 00.00.0000
5234 \begin_inset LatexCommand \index{---dumpgcse}
5240 Will create a dump of iCode's, after global subexpression elimination
5241 \begin_inset LatexCommand \index{Global subexpression elimination}
5247 <source filename>.dumpgcse.
5249 \labelwidthstring 00.00.0000
5254 \begin_inset LatexCommand \index{---dumpdeadcode}
5260 Will create a dump of iCode's, after deadcode elimination
5261 \begin_inset LatexCommand \index{Dead-code elimination}
5267 <source filename>.dumpdeadcode.
5269 \labelwidthstring 00.00.0000
5274 \begin_inset LatexCommand \index{---dumploop}
5283 Will create a dump of iCode's, after loop optimizations
5284 \begin_inset LatexCommand \index{Loop optimization}
5290 <source filename>.dumploop.
5292 \labelwidthstring 00.00.0000
5297 \begin_inset LatexCommand \index{---dumprange}
5306 Will create a dump of iCode's, after live range analysis
5307 \begin_inset LatexCommand \index{Live range analysis}
5313 <source filename>.dumprange.
5315 \labelwidthstring 00.00.0000
5320 \begin_inset LatexCommand \index{---dumlrange}
5326 Will dump the life ranges
5327 \begin_inset LatexCommand \index{Live range analysis}
5333 \labelwidthstring 00.00.0000
5338 \begin_inset LatexCommand \index{---dumpregassign}
5347 Will create a dump of iCode's, after register assignment
5348 \begin_inset LatexCommand \index{Register assignment}
5354 <source filename>.dumprassgn.
5356 \labelwidthstring 00.00.0000
5361 \begin_inset LatexCommand \index{---dumplrange}
5367 Will create a dump of the live ranges of iTemp's
5369 \labelwidthstring 00.00.0000
5374 \begin_inset LatexCommand \index{---dumpall}
5385 Will cause all the above mentioned dumps to be created.
5388 Environment variables
5389 \begin_inset LatexCommand \index{Environment variables}
5396 SDCC recognizes the following environment variables:
5398 \labelwidthstring 00.00.0000
5403 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
5409 SDCC installs a signal handler
5410 \begin_inset LatexCommand \index{signal handler}
5414 to be able to delete temporary files after an user break (^C) or an exception.
5415 If this environment variable is set, SDCC won't install the signal handler
5416 in order to be able to debug SDCC.
5418 \labelwidthstring 00.00.0000
5423 \begin_inset LatexCommand \index{TMP}
5429 \begin_inset LatexCommand \index{TEMP}
5435 \begin_inset LatexCommand \index{TMPDIR}
5441 Path, where temporary files will be created.
5442 The order of the variables is the search order.
5443 In a standard *nix environment these variables are not set, and there's
5444 no need to set them.
5445 On Windows it's recommended to set one of them.
5447 \labelwidthstring 00.00.0000
5452 \begin_inset LatexCommand \index{SDCC\_HOME}
5459 \begin_inset Quotes sld
5462 2.3 Install and search paths
5463 \begin_inset Quotes srd
5468 \labelwidthstring 00.00.0000
5473 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
5480 \begin_inset Quotes sld
5483 2.3 Install and search paths
5484 \begin_inset Quotes srd
5489 \labelwidthstring 00.00.0000
5494 \begin_inset LatexCommand \index{SDCC\_LIB}
5501 \begin_inset Quotes sld
5504 2.3 Install and search paths
5505 \begin_inset Quotes srd
5511 There are some more environment variables recognized by SDCC, but these
5512 are solely used for debugging purposes.
5513 They can change or disappear very quickly, and will never be documentated.
5516 MCS51/DS390 Storage Class
5517 \begin_inset LatexCommand \index{Storage class}
5524 In addition to the ANSI storage classes SDCC allows the following MCS51
5525 specific storage classes.
5526 \layout Subsubsection
5529 \begin_inset LatexCommand \index{xdata}
5536 Variables declared with this storage class will be placed in the extern
5542 storage class for Large Memory model, e.g.:
5548 xdata unsigned char xduc;
5549 \layout Subsubsection
5552 \begin_inset LatexCommand \index{data}
5563 storage class for Small Memory model.
5564 Variables declared with this storage class will be allocated in the internal
5572 \layout Subsubsection
5575 \begin_inset LatexCommand \index{idata}
5582 Variables declared with this storage class will be allocated into the indirectly
5583 addressable portion of the internal ram of a 8051, e.g.:
5590 \layout Subsubsection
5593 \begin_inset LatexCommand \index{bit}
5600 This is a data-type and a storage class specifier.
5601 When a variable is declared as a bit, it is allocated into the bit addressable
5602 memory of 8051, e.g.:
5609 \layout Subsubsection
5612 \begin_inset LatexCommand \index{sfr}
5617 \begin_inset LatexCommand \index{sbit}
5624 Like the bit keyword,
5628 signifies both a data-type and storage class, they are used to describe
5629 the special function registers and special bit variables of a 8051, eg:
5635 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
5637 sbit at 0xd7 CY; /* CY (Carry Flag
5638 \begin_inset LatexCommand \index{Flags}
5643 \begin_inset LatexCommand \index{Carry flag}
5651 \begin_inset LatexCommand \index{Pointers}
5658 SDCC allows (via language extensions) pointers to explicitly point to any
5659 of the memory spaces
5660 \begin_inset LatexCommand \index{Memory model}
5665 In addition to the explicit pointers, the compiler uses (by default) generic
5666 pointers which can be used to point to any of the memory spaces.
5670 Pointer declaration examples:
5679 /* pointer physically in xternal ram pointing to object in internal ram
5682 data unsigned char * xdata p;
5686 /* pointer physically in code rom pointing to data in xdata space */
5688 xdata unsigned char * code p;
5692 /* pointer physically in code space pointing to data in code space */
5694 code unsigned char * code p;
5698 /* the folowing is a generic pointer physically located in xdata space */
5709 Well you get the idea.
5714 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
5727 The highest order byte of the
5731 pointers contains the data space information.
5732 Assembler support routines are called whenever data is stored or retrieved
5738 These are useful for developing reusable library
5739 \begin_inset LatexCommand \index{Libraries}
5744 Explicitly specifying the pointer type will generate the most efficient
5749 \begin_inset LatexCommand \index{Parameters}
5754 \begin_inset LatexCommand \index{Local variable}
5761 Automatic (local) variables and parameters to functions can either be placed
5762 on the stack or in data-space.
5763 The default action of the compiler is to place these variables in the internal
5764 RAM (for small model) or external RAM (for large model).
5765 This in fact makes them
5768 \begin_inset LatexCommand \index{static}
5774 so by default functions are non-reentrant
5775 \begin_inset LatexCommand \index{reentrant}
5783 They can be placed on the stack
5784 \begin_inset LatexCommand \index{stack}
5791 \begin_inset LatexCommand \index{--stack-auto}
5797 option or by using the
5800 \begin_inset LatexCommand \index{reentrant}
5806 keyword in the function declaration, e.g.:
5815 unsigned char foo(char i) reentrant
5828 Since stack space on 8051 is limited, the
5836 option should be used sparingly.
5837 Note that the reentrant keyword just means that the parameters & local
5838 variables will be allocated to the stack, it
5842 mean that the function is register bank independent.
5846 Local variables can be assigned storage classes and absolute
5847 \begin_inset LatexCommand \index{Absolute addressing}
5857 unsigned char foo() {
5863 xdata unsigned char i;
5875 data at 0x31 unsiged char j;
5890 In the above example the variable
5894 will be allocated in the external ram,
5898 in bit addressable space and
5907 or when a function is declared as
5911 this should only be done for static variables.
5914 Parameters however are not allowed any storage class, (storage classes for
5915 parameters will be ignored), their allocation is governed by the memory
5916 model in use, and the reentrancy options.
5920 \begin_inset LatexCommand \index{Overlaying}
5928 \begin_inset LatexCommand \index{reentrant}
5932 functions SDCC will try to reduce internal ram space usage by overlaying
5933 parameters and local variables of a function (if possible).
5934 Parameters and local variables of a function will be allocated to an overlayabl
5935 e segment if the function has
5937 no other function calls and the function is non-reentrant and the memory
5939 \begin_inset LatexCommand \index{Memory model}
5946 If an explicit storage class
5947 \begin_inset LatexCommand \index{Storage class}
5951 is specified for a local variable, it will NOT be overlayed.
5954 Note that the compiler (not the linkage editor) makes the decision for overlayin
5956 Functions that are called from an interrupt service routine should be preceded
5957 by a #pragma\SpecialChar ~
5959 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
5963 if they are not reentrant.
5966 Also note that the compiler does not do any processing of inline
5967 \begin_inset LatexCommand \index{inline}
5971 assembler code, so the compiler might incorrectly assign local variables
5972 and parameters of a function into the overlay segment if the inline assembler
5973 code calls other c-functions that might use the overlay.
5974 In that case the #pragma\SpecialChar ~
5975 NOOVERLAY should be used.
5978 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
5980 \begin_inset LatexCommand \index{Multiplication}
5985 \begin_inset LatexCommand \index{Division}
5989 will NOT be overlayed since these are implemented using external functions,
5999 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
6005 void set_error(unsigned char errcd)
6021 void some_isr () interrupt
6022 \begin_inset LatexCommand \index{interrupt}
6027 \begin_inset LatexCommand \index{using}
6060 In the above example the parameter
6068 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
6070 not present, this could cause unpredictable runtime behavior when called
6072 The #pragma\SpecialChar ~
6073 NOOVERLAY ensures that the parameters and local variables for
6074 the function are NOT overlayed.
6077 Interrupt Service Routines
6080 SDCC allows interrupt service routines to be coded in C, with some extended
6087 void timer_isr (void) interrupt 2 using 1
6100 The number following the
6103 \begin_inset LatexCommand \index{interrupt}
6109 keyword is the interrupt number this routine will service.
6110 The compiler will insert a call to this routine in the interrupt vector
6111 table for the interrupt number specified.
6116 keyword is used to tell the compiler to use the specified register bank
6117 (8051 specific) when generating code for this function.
6118 Note that when some function is called from an interrupt service routine
6119 it should be preceded by a #pragma\SpecialChar ~
6121 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
6125 if it is not reentrant.
6126 A special note here, int (16 bit) and long (32 bit) integer division
6127 \begin_inset LatexCommand \index{Division}
6132 \begin_inset LatexCommand \index{Multiplication}
6137 \begin_inset LatexCommand \index{Modulus}
6141 operations are implemented using external support routines developed in
6142 ANSI-C, if an interrupt service routine needs to do any of these operations
6143 then the support routines (as mentioned in a following section) will have
6144 to be recompiled using the
6147 \begin_inset LatexCommand \index{---stack-auto}
6153 option and the source file will need to be compiled using the
6158 \begin_inset LatexCommand \index{---int-long-rent}
6165 If you have multiple source files in your project, interrupt service routines
6166 can be present in any of them, but a prototype of the isr MUST be present
6167 or included in the file that contains the function
6174 Interrupt Numbers and the corresponding address & descriptions for the Standard
6175 8051 are listed below.
6176 SDCC will automatically adjust the interrupt vector table to the maximum
6177 interrupt number specified.
6183 \begin_inset Tabular
6184 <lyxtabular version="3" rows="6" columns="3">
6186 <column alignment="center" valignment="top" leftline="true" width="0in">
6187 <column alignment="center" valignment="top" leftline="true" width="0in">
6188 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
6189 <row topline="true" bottomline="true">
6190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6215 <row topline="true">
6216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6241 <row topline="true">
6242 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6258 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6267 <row topline="true">
6268 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6293 <row topline="true">
6294 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6310 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6319 <row topline="true" bottomline="true">
6320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
6336 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6353 If the interrupt service routine is defined without
6356 \begin_inset LatexCommand \index{using}
6362 a register bank or with register bank 0 (using 0), the compiler will save
6363 the registers used by itself on the stack upon entry and restore them at
6364 exit, however if such an interrupt service routine calls another function
6365 then the entire register bank will be saved on the stack.
6366 This scheme may be advantageous for small interrupt service routines which
6367 have low register usage.
6370 If the interrupt service routine is defined to be using a specific register
6375 are save and restored, if such an interrupt service routine calls another
6376 function (using another register bank) then the entire register bank of
6377 the called function will be saved on the stack.
6378 This scheme is recommended for larger interrupt service routines.
6381 Calling other functions from an interrupt service routine is not recommended,
6382 avoid it if possible.
6386 Also see the _naked modifier.
6394 <TODO: this isn't implemented at all!>
6400 A special keyword may be associated with a function declaring it as
6405 SDCC will generate code to disable all interrupts upon entry to a critical
6406 function and enable them back before returning.
6407 Note that nesting critical functions may cause unpredictable results.
6432 The critical attribute maybe used with other attributes like
6438 \begin_inset LatexCommand \index{Naked functions}
6445 A special keyword may be associated with a function declaring it as
6448 \begin_inset LatexCommand \index{\_naked}
6459 function modifier attribute prevents the compiler from generating prologue
6460 \begin_inset LatexCommand \index{function prologue}
6465 \begin_inset LatexCommand \index{function epilogue}
6469 code for that function.
6470 This means that the user is entirely responsible for such things as saving
6471 any registers that may need to be preserved, selecting the proper register
6472 bank, generating the
6476 instruction at the end, etc.
6477 Practically, this means that the contents of the function must be written
6478 in inline assembler.
6479 This is particularly useful for interrupt functions, which can have a large
6480 (and often unnecessary) prologue/epilogue.
6481 For example, compare the code generated by these two functions:
6487 data unsigned char counter;
6489 void simpleInterrupt(void) interrupt
6490 \begin_inset LatexCommand \index{interrupt}
6508 void nakedInterrupt(void) interrupt 2 _naked
6517 \begin_inset LatexCommand \index{\_asm}
6546 ; MUST explicitly include ret in _naked function.
6553 \begin_inset LatexCommand \index{\_endasm}
6565 For an 8051 target, the generated simpleInterrupt looks like:
6710 whereas nakedInterrupt looks like:
6735 ; MUST explicitly include ret(i) in _naked function.
6741 While there is nothing preventing you from writing C code inside a _naked
6742 function, there are many ways to shoot yourself in the foot doing this,
6743 and it is recommended that you stick to inline assembler.
6746 Functions using private banks
6747 \begin_inset LatexCommand \index{bank}
6757 \begin_inset LatexCommand \index{using}
6763 attribute (which tells the compiler to use a register bank other than the
6764 default bank zero) should only be applied to
6767 \begin_inset LatexCommand \index{interrupt}
6773 functions (see note 1 below).
6774 This will in most circumstances make the generated ISR code more efficient
6775 since it will not have to save registers on the stack.
6782 attribute will have no effect on the generated code for a
6786 function (but may occasionally be useful anyway
6792 possible exception: if a function is called ONLY from 'interrupt' functions
6793 using a particular bank, it can be declared with the same 'using' attribute
6794 as the calling 'interrupt' functions.
6795 For instance, if you have several ISRs using bank one, and all of them
6796 call memcpy(), it might make sense to create a specialized version of memcpy()
6797 'using 1', since this would prevent the ISR from having to save bank zero
6798 to the stack on entry and switch to bank zero before calling the function
6805 (pending: I don't think this has been done yet)
6812 function using a non-zero bank will assume that it can trash that register
6813 bank, and will not save it.
6814 Since high-priority interrupts
6815 \begin_inset LatexCommand \index{interrupt priority}
6819 can interrupt low-priority ones on the 8051 and friends, this means that
6820 if a high-priority ISR
6824 a particular bank occurs while processing a low-priority ISR
6828 the same bank, terrible and bad things can happen.
6829 To prevent this, no single register bank should be
6833 by both a high priority and a low priority ISR.
6834 This is probably most easily done by having all high priority ISRs use
6835 one bank and all low priority ISRs use another.
6836 If you have an ISR which can change priority at runtime, you're on your
6837 own: I suggest using the default bank zero and taking the small performance
6841 It is most efficient if your ISR calls no other functions.
6842 If your ISR must call other functions, it is most efficient if those functions
6843 use the same bank as the ISR (see note 1 below); the next best is if the
6844 called functions use bank zero.
6845 It is very inefficient to call a function using a different, non-zero bank
6851 \begin_inset LatexCommand \index{Absolute addressing}
6858 Data items can be assigned an absolute address with the
6861 \begin_inset LatexCommand \index{at}
6867 keyword, in addition to a storage class, e.g.:
6874 \begin_inset LatexCommand \index{xdata}
6879 \begin_inset LatexCommand \index{at}
6883 0x8000 unsigned char PORTA_8255 ;
6889 In the above example the PORTA_8255 will be allocated to the location 0x8000
6890 of the external ram.
6891 Note that this feature is provided to give the programmer access to
6895 devices attached to the controller.
6896 The compiler does not actually reserve any space for variables declared
6897 in this way (they are implemented with an equate in the assembler).
6898 Thus it is left to the programmer to make sure there are no overlaps with
6899 other variables that are declared without the absolute address.
6900 The assembler listing file (.lst
6901 \begin_inset LatexCommand \index{.lst}
6905 ) and the linker output files (.rst
6906 \begin_inset LatexCommand \index{.rst}
6911 \begin_inset LatexCommand \index{.map}
6915 ) are a good places to look for such overlaps.
6919 Absolute address can be specified for variables in all storage classes,
6927 \begin_inset LatexCommand \index{bit}
6932 \begin_inset LatexCommand \index{at}
6942 The above example will allocate the variable at offset 0x02 in the bit-addressab
6944 There is no real advantage to assigning absolute addresses to variables
6945 in this manner, unless you want strict control over all the variables allocated.
6949 \begin_inset LatexCommand \index{Startup code}
6956 The compiler inserts a call to the C routine
6958 _sdcc_external_startup()
6959 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
6968 at the start of the CODE area.
6969 This routine is in the runtime library
6970 \begin_inset LatexCommand \index{Runtime library}
6975 By default this routine returns 0, if this routine returns a non-zero value,
6976 the static & global variable initialization will be skipped and the function
6977 main will be invoked Other wise static & global variables will be initialized
6978 before the function main is invoked.
6981 _sdcc_external_startup()
6983 routine to your program to override the default if you need to setup hardware
6984 or perform some other critical operation prior to static & global variable
6988 Inline Assembler Code
6989 \begin_inset LatexCommand \index{Assembler routines}
6996 SDCC allows the use of in-line assembler with a few restriction as regards
6998 All labels defined within inline assembler code
7006 where nnnn is a number less than 100 (which implies a limit of utmost 100
7007 inline assembler labels
7015 It is strongly recommended that each assembly instruction (including labels)
7016 be placed in a separate line (as the example shows).
7020 \begin_inset LatexCommand \index{--peep-asm}
7026 command line option is used, the inline assembler code will be passed through
7027 the peephole optimizer
7028 \begin_inset LatexCommand \index{Peephole optimizer}
7033 This might cause some unexpected changes in the inline assembler code.
7034 Please go throught the peephole optimizer rules defined in file
7038 carefully before using this option.
7045 \begin_inset LatexCommand \index{\_asm}
7075 \begin_inset LatexCommand \index{\_endasm}
7088 The inline assembler code can contain any valid code understood by the assembler
7089 , this includes any assembler directives and comment lines.
7090 The compiler does not do any validation of the code within the
7100 Inline assembler code cannot reference any C-Labels, however it can reference
7102 \begin_inset LatexCommand \index{Labels}
7106 defined by the inline assembler, e.g.:
7132 ; some assembler code
7152 /* some more c code */
7154 clabel:\SpecialChar ~
7156 /* inline assembler cannot reference this label */
7168 $0003: ;label (can be reference by inline assembler only)
7180 /* some more c code */
7188 In other words inline assembly code can access labels defined in inline
7189 assembly within the scope of the funtion.
7193 The same goes the other way, ie.
7194 labels defines in inline assembly CANNOT be accessed by C statements.
7198 \begin_inset LatexCommand \index{int (16 bit)}
7203 \begin_inset LatexCommand \index{long (32 bit)}
7210 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
7211 multiplication and modulus operations are implemented by support routines.
7212 These support routines are all developed in ANSI-C to facilitate porting
7213 to other MCUs, although some model specific assembler optimations are used.
7214 The following files contain the described routine, all of them can be found
7215 in <installdir>/share/sdcc/lib.
7221 <pending: tabularise this>
7227 _mulsint.c - signed 16 bit multiplication (calls _muluint)
7229 _muluint.c - unsigned 16 bit multiplication
7231 _divsint.c - signed 16 bit division (calls _divuint)
7233 _divuint.c - unsigned 16 bit division
7235 _modsint.c - signed 16 bit modulus (call _moduint)
7237 _moduint.c - unsigned 16 bit modulus
7239 _mulslong.c - signed 32 bit multiplication (calls _mululong)
7241 _mululong.c - unsigned32 bit multiplication
7243 _divslong.c - signed 32 division (calls _divulong)
7245 _divulong.c - unsigned 32 division
7247 _modslong.c - signed 32 bit modulus (calls _modulong)
7249 _modulong.c - unsigned 32 bit modulus
7257 Since they are compiled as
7262 \begin_inset LatexCommand \index{reentrant}
7267 \begin_inset LatexCommand \index{interrupt}
7271 service routines should not do any of the above operations.
7272 If this is unavoidable then the above routines will need to be compiled
7276 \begin_inset LatexCommand \index{---stack-auto}
7282 option, after which the source program will have to be compiled with
7285 \begin_inset LatexCommand \index{---int-long-rent}
7294 Floating Point Support
7295 \begin_inset LatexCommand \index{Floating point support}
7302 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
7303 point support routines are derived from gcc's floatlib.c and consists of
7304 the following routines:
7310 <pending: tabularise this>
7316 _fsadd.c - add floating point numbers
7318 _fssub.c - subtract floating point numbers
7320 _fsdiv.c - divide floating point numbers
7322 _fsmul.c - multiply floating point numbers
7324 _fs2uchar.c - convert floating point to unsigned char
7326 _fs2char.c - convert floating point to signed char
7328 _fs2uint.c - convert floating point to unsigned int
7330 _fs2int.c - convert floating point to signed int
7332 _fs2ulong.c - convert floating point to unsigned long
7334 _fs2long.c - convert floating point to signed long
7336 _uchar2fs.c - convert unsigned char to floating point
7338 _char2fs.c - convert char to floating point number
7340 _uint2fs.c - convert unsigned int to floating point
7342 _int2fs.c - convert int to floating point numbers
7344 _ulong2fs.c - convert unsigned long to floating point number
7346 _long2fs.c - convert long to floating point number
7354 Note if all these routines are used simultaneously the data space might
7356 For serious floating point usage it is strongly recommended that the large
7361 \begin_inset LatexCommand \index{Memory model}
7366 \begin_inset LatexCommand \index{MCS51 memory}
7373 SDCC allows two memory models for MCS51 code, small and large.
7374 Modules compiled with different memory models should
7378 be combined together or the results would be unpredictable.
7379 The library routines supplied with the compiler are compiled as both small
7381 The compiled library modules are contained in seperate directories as small
7382 and large so that you can link to either set.
7386 When the large model is used all variables declared without a storage class
7387 will be allocated into the external ram, this includes all parameters and
7388 local variables (for non-reentrant
7389 \begin_inset LatexCommand \index{reentrant}
7394 When the small model is used variables without storage class are allocated
7395 in the internal ram.
7398 Judicious usage of the processor specific storage classes
7399 \begin_inset LatexCommand \index{Storage class}
7403 and the 'reentrant' function type will yield much more efficient code,
7404 than using the large model.
7405 Several optimizations are disabled when the program is compiled using the
7406 large model, it is therefore strongly recommdended that the small model
7407 be used unless absolutely required.
7411 \begin_inset LatexCommand \index{Memory model}
7416 \begin_inset LatexCommand \index{DS390 memory model}
7423 The only model supported is Flat 24
7424 \begin_inset LatexCommand \index{Flat 24 (memory model)}
7429 This generates code for the 24 bit contiguous addressing mode of the Dallas
7431 In this mode, up to four meg of external RAM or code space can be directly
7433 See the data sheets at www.dalsemi.com for further information on this part.
7437 In older versions of the compiler, this option was used with the MCS51 code
7443 Now, however, the '390 has it's own code generator, selected by the
7452 Note that the compiler does not generate any code to place the processor
7453 into 24 bitmode (although
7457 in the ds390 libraries will do that for you).
7463 \begin_inset LatexCommand \index{Tinibios (DS390)}
7467 , the boot loader or similar code must ensure that the processor is in 24
7468 bit contiguous addressing mode before calling the SDCC startup code.
7476 option, variables will by default be placed into the XDATA segment.
7481 Segments may be placed anywhere in the 4 meg address space using the usual
7483 Note that if any segments are located above 64K, the -r flag must be passed
7484 to the linker to generate the proper segment relocations, and the Intel
7485 HEX output format must be used.
7486 The -r flag can be passed to the linker by using the option
7490 on the sdcc command line.
7491 However, currently the linker can not handle code segments > 64k.
7494 Defines Created by the Compiler
7495 \begin_inset LatexCommand \index{Defines created by the compiler}
7502 The compiler creates the following #defines
7503 \begin_inset LatexCommand \index{\#defines}
7511 \begin_inset LatexCommand \index{SDCC}
7515 - this Symbol is always defined.
7519 \begin_inset LatexCommand \index{SDCC\_mcs51}
7524 \begin_inset LatexCommand \index{SDCC\_ds390}
7529 \begin_inset LatexCommand \index{SDCC\_z80}
7533 , etc - depending on the model used (e.g.: -mds390)
7537 \begin_inset LatexCommand \index{\_\_mcs51}
7542 \begin_inset LatexCommand \index{\_\_ds390}
7547 \begin_inset LatexCommand \index{\_\_z80}
7551 , etc - depending on the model used (e.g.
7556 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
7560 - this symbol is defined when
7568 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
7580 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
7592 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
7604 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
7616 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
7631 \begin_inset LatexCommand \index{Optimizations}
7638 SDCC performs a host of standard optimizations in addition to some MCU specific
7641 \layout Subsubsection
7643 Sub-expression Elimination
7644 \begin_inset LatexCommand \index{Subexpression elimination}
7651 The compiler does local and global common subexpression elimination, e.g.:
7666 will be translated to
7682 Some subexpressions are not as obvious as the above example, e.g.:
7696 In this case the address arithmetic a->b[i] will be computed only once;
7697 the equivalent code in C would be.
7713 The compiler will try to keep these temporary variables in registers.
7714 \layout Subsubsection
7716 Dead-Code Elimination
7717 \begin_inset LatexCommand \index{Dead-code elimination}
7736 i = 1; \SpecialChar ~
7741 global = 1;\SpecialChar ~
7754 global = 3;\SpecialChar ~
7769 int global; void f ()
7782 \layout Subsubsection
7785 \begin_inset LatexCommand \index{Copy propagation}
7848 Note: the dead stores created by this copy propagation will be eliminated
7849 by dead-code elimination.
7850 \layout Subsubsection
7853 \begin_inset LatexCommand \index{Loop optimization}
7860 Two types of loop optimizations are done by SDCC loop invariant lifting
7861 and strength reduction of loop induction variables.
7862 In addition to the strength reduction the optimizer marks the induction
7863 variables and the register allocator tries to keep the induction variables
7864 in registers for the duration of the loop.
7865 Because of this preference of the register allocator
7866 \begin_inset LatexCommand \index{Register allocation}
7870 , loop induction optimization causes an increase in register pressure, which
7871 may cause unwanted spilling of other temporary variables into the stack
7872 \begin_inset LatexCommand \index{stack}
7877 The compiler will generate a warning message when it is forced to allocate
7878 extra space either on the stack or data space.
7879 If this extra space allocation is undesirable then induction optimization
7880 can be eliminated either for the entire source file (with ---noinduction
7881 option) or for a given function only using #pragma\SpecialChar ~
7883 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
7897 for (i = 0 ; i < 100 ; i ++)
7915 for (i = 0; i < 100; i++)
7925 As mentioned previously some loop invariants are not as apparent, all static
7926 address computations are also moved out of the loop.
7931 \begin_inset LatexCommand \index{Strength reduction}
7935 , this optimization substitutes an expression by a cheaper expression:
7941 for (i=0;i < 100; i++)
7961 for (i=0;i< 100;i++) {
7965 ar[itemp1] = itemp2;
7981 The more expensive multiplication
7982 \begin_inset LatexCommand \index{Multiplication}
7986 is changed to a less expensive addition.
7987 \layout Subsubsection
7990 \begin_inset LatexCommand \index{Loop reversing}
7997 This optimization is done to reduce the overhead of checking loop boundaries
7998 for every iteration.
7999 Some simple loops can be reversed and implemented using a
8000 \begin_inset Quotes eld
8003 decrement and jump if not zero
8004 \begin_inset Quotes erd
8008 SDCC checks for the following criterion to determine if a loop is reversible
8009 (note: more sophisticated compilers use data-dependency analysis to make
8010 this determination, SDCC uses a more simple minded analysis).
8013 The 'for' loop is of the form
8019 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
8029 The <for body> does not contain
8030 \begin_inset Quotes eld
8034 \begin_inset Quotes erd
8038 \begin_inset Quotes erd
8044 All goto's are contained within the loop.
8047 No function calls within the loop.
8050 The loop control variable <sym> is not assigned any value within the loop
8053 The loop control variable does NOT participate in any arithmetic operation
8057 There are NO switch statements in the loop.
8058 \layout Subsubsection
8060 Algebraic Simplifications
8063 SDCC does numerous algebraic simplifications, the following is a small sub-set
8064 of these optimizations.
8070 i = j + 0 ; /* changed to */ i = j;
8072 i /= 2; /* changed to */ i >>= 1;
8074 i = j - j ; /* changed to */ i = 0;
8076 i = j / 1 ; /* changed to */ i = j;
8082 Note the subexpressions
8083 \begin_inset LatexCommand \index{Subexpression}
8087 given above are generally introduced by macro expansions or as a result
8088 of copy/constant propagation.
8089 \layout Subsubsection
8092 \begin_inset LatexCommand \index{switch statement}
8099 SDCC changes switch statements to jump tables
8100 \begin_inset LatexCommand \index{jump tables}
8104 when the following conditions are true.
8108 The case labels are in numerical sequence, the labels need not be in order,
8109 and the starting number need not be one or zero.
8115 switch(i) {\SpecialChar ~
8222 Both the above switch statements will be implemented using a jump-table.
8225 The number of case labels is at least three, since it takes two conditional
8226 statements to handle the boundary conditions.
8229 The number of case labels is less than 84, since each label takes 3 bytes
8230 and a jump-table can be utmost 256 bytes long.
8234 Switch statements which have gaps in the numeric sequence or those that
8235 have more that 84 case labels can be split into more than one switch statement
8236 for efficient code generation, e.g.:
8274 If the above switch statement is broken down into two switch statements
8308 case 9: \SpecialChar ~
8318 case 12:\SpecialChar ~
8328 then both the switch statements will be implemented using jump-tables whereas
8329 the unmodified switch statement will not be.
8330 \layout Subsubsection
8332 Bit-shifting Operations
8333 \begin_inset LatexCommand \index{Bit shifting}
8340 Bit shifting is one of the most frequently used operation in embedded programmin
8342 SDCC tries to implement bit-shift operations in the most efficient way
8362 generates the following code:
8380 In general SDCC will never setup a loop if the shift count is known.
8420 Note that SDCC stores numbers in little-endian format (i.e.
8421 lowest order first).
8422 \layout Subsubsection
8425 \begin_inset LatexCommand \index{Bit rotation}
8432 A special case of the bit-shift operation is bit rotation, SDCC recognizes
8433 the following expression to be a left bit-rotation:
8444 i = ((i << 1) | (i >> 7));
8452 will generate the following code:
8468 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
8469 ns of this case will also be recognized as bit-rotation, i.e.:
8475 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
8476 \layout Subsubsection
8479 \begin_inset LatexCommand \index{Highest Order Bit}
8486 It is frequently required to obtain the highest order bit of an integral
8487 type (long, int, short or char types).
8488 SDCC recognizes the following expression to yield the highest order bit
8489 and generates optimized code for it, e.g.:
8510 hob = (gint >> 15) & 1;
8523 will generate the following code:
8562 000A E5*01\SpecialChar ~
8590 000C 33\SpecialChar ~
8621 000D E4\SpecialChar ~
8652 000E 13\SpecialChar ~
8683 000F F5*02\SpecialChar ~
8713 Variations of this case however will
8718 It is a standard C expression, so I heartily recommend this be the only
8719 way to get the highest order bit, (it is portable).
8720 Of course it will be recognized even if it is embedded in other expressions,
8727 xyz = gint + ((gint >> 15) & 1);
8733 will still be recognized.
8734 \layout Subsubsection
8737 \begin_inset LatexCommand \index{Peephole optimizer}
8744 The compiler uses a rule based, pattern matching and re-writing mechanism
8745 for peep-hole optimization.
8750 a peep-hole optimizer by Christopher W.
8751 Fraser (cwfraser@microsoft.com).
8752 A default set of rules are compiled into the compiler, additional rules
8753 may be added with the
8756 \begin_inset LatexCommand \index{---peep-file}
8763 The rule language is best illustrated with examples.
8791 The above rule will change the following assembly
8792 \begin_inset LatexCommand \index{Assembler routines}
8826 Note: All occurrences of a
8830 (pattern variable) must denote the same string.
8831 With the above rule, the assembly sequence:
8849 will remain unmodified.
8853 Other special case optimizations may be added by the user (via
8859 some variants of the 8051 MCU allow only
8868 The following two rules will change all
8890 replace { lcall %1 } by { acall %1 }
8892 replace { ljmp %1 } by { ajmp %1 }
8900 inline-assembler code
8902 is also passed through the peep hole optimizer, thus the peephole optimizer
8903 can also be used as an assembly level macro expander.
8904 The rules themselves are MCU dependent whereas the rule language infra-structur
8905 e is MCU independent.
8906 Peephole optimization rules for other MCU can be easily programmed using
8911 The syntax for a rule is as follows:
8917 rule := replace [ restart ] '{' <assembly sequence> '
8955 <assembly sequence> '
8973 '}' [if <functionName> ] '
8981 <assembly sequence> := assembly instruction (each instruction including
8982 labels must be on a separate line).
8986 The optimizer will apply to the rules one by one from the top in the sequence
8987 of their appearance, it will terminate when all rules are exhausted.
8988 If the 'restart' option is specified, then the optimizer will start matching
8989 the rules again from the top, this option for a rule is expensive (performance)
8990 , it is intended to be used in situations where a transformation will trigger
8991 the same rule again.
8992 An example of this (not a good one, it has side effects) is the following
9019 Note that the replace pattern cannot be a blank, but can be a comment line.
9020 Without the 'restart' option only the inner most 'pop' 'push' pair would
9021 be eliminated, i.e.:
9073 the restart option the rule will be applied again to the resulting code
9074 and then all the pop-push pairs will be eliminated to yield:
9092 A conditional function can be attached to a rule.
9093 Attaching rules are somewhat more involved, let me illustrate this with
9124 The optimizer does a look-up of a function name table defined in function
9129 in the source file SDCCpeeph.c, with the name
9134 If it finds a corresponding entry the function is called.
9135 Note there can be no parameters specified for these functions, in this
9140 is crucial, since the function
9144 expects to find the label in that particular variable (the hash table containin
9145 g the variable bindings is passed as a parameter).
9146 If you want to code more such functions, take a close look at the function
9147 labelInRange and the calling mechanism in source file SDCCpeeph.c.
9148 I know this whole thing is a little kludgey, but maybe some day we will
9149 have some better means.
9150 If you are looking at this file, you will also see the default rules that
9151 are compiled into the compiler, you can add your own rules in the default
9152 set there if you get tired of specifying the ---peep-file option.
9156 \begin_inset LatexCommand \index{Pragmas}
9163 SDCC supports the following #pragma directives.
9167 \begin_inset LatexCommand \index{\#pragma SAVE}
9171 - this will save all current options to the SAVE/RESTORE stack.
9176 \begin_inset LatexCommand \index{\#pragma RESTORE}
9180 - will restore saved options from the last save.
9181 SAVEs & RESTOREs can be nested.
9182 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
9183 RESTORE pulls current options from the stack.
9188 \begin_inset LatexCommand \index{\#pragma NOGCSE}
9192 - will stop global subexpression elimination.
9196 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
9200 - will stop loop induction optimizations.
9204 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
9208 - will not generate code for boundary value checking, when switch statements
9209 are turned into jump-tables.
9213 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
9217 - the compiler will not overlay the parameters and local variables of a
9222 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
9226 - the compiler will not warn you anymore for obvious mistakes, you'r on
9231 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
9235 - Will not do loop reversal optimization
9239 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
9243 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
9245 \begin_inset LatexCommand \index{push/pop}
9249 instruction in ISR function (using interrupt
9250 \begin_inset LatexCommand \index{interrupt}
9255 The directive should be placed immediately before the ISR function definition
9256 and it affects ALL ISR functions following it.
9257 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
9258 EXCLUDE\SpecialChar ~
9260 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
9268 \begin_inset LatexCommand \index{\#pragma NOIV}
9272 - Do not generate interrupt vector table entries for all ISR functions
9273 defined after the pragma.
9274 This is useful in cases where the interrupt vector table must be defined
9275 manually, or when there is a secondary, manually defined interrupt vector
9277 for the autovector feature of the Cypress EZ-USB FX2).
9281 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
9286 \begin_inset LatexCommand \index{function prologue}
9290 function1[,function2[,function3...]] - The compiler by default uses a caller
9291 saves convention for register saving across function calls, however this
9292 can cause unneccessary register pushing & popping when calling small functions
9293 from larger functions.
9294 This option can be used to switch off the register saving convention for
9295 the function names specified.
9296 The compiler will not save registers when calling these functions, extra
9297 code need to be manually inserted at the entry & exit for these functions
9298 to save & restore the registers used by these functions, this can SUBSTANTIALLY
9299 reduce code & improve run time performance of the generated code.
9300 In the future the compiler (with interprocedural analysis) may be able
9301 to determine the appropriate scheme to use for each function call.
9302 If ---callee-saves command line option is used, the function names specified
9303 in #pragma\SpecialChar ~
9305 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
9309 is appended to the list of functions specified in the command line.
9312 The pragma's are intended to be used to turn-off certain optimizations which
9313 might cause the compiler to generate extra stack / data space to store
9314 compiler generated temporary variables.
9315 This usually happens in large functions.
9316 Pragma directives should be used as shown in the following example, they
9317 are used to control options & optimizations for a given function; pragmas
9318 should be placed before and/or after a function, placing pragma's inside
9319 a function body could have unpredictable results.
9326 \begin_inset LatexCommand \index{\#pragma SAVE}
9330 /* save the current settings */
9333 \begin_inset LatexCommand \index{\#pragma NOGCSE}
9337 /* turnoff global subexpression elimination */
9340 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
9344 /* turn off induction optimizations */
9367 \begin_inset LatexCommand \index{\#pragma RESTORE}
9371 /* turn the optimizations back on */
9377 The compiler will generate a warning message when extra space is allocated.
9378 It is strongly recommended that the SAVE and RESTORE pragma's be used when
9379 changing options for a function.
9384 <pending: this is messy and incomplete>
9389 Compiler support routines (_gptrget, _mulint etc)
9392 Stdclib functions (puts, printf, strcat etc)
9395 Math functions (sin, pow, sqrt etc)
9398 license statements for the libraries are missing
9401 Interfacing with Assembly Routines
9402 \begin_inset LatexCommand \index{Assembler routines}
9407 \layout Subsubsection
9409 Global Registers used for Parameter Passing
9410 \begin_inset LatexCommand \index{Parameter passing}
9417 The compiler always uses the global registers
9420 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
9425 \begin_inset LatexCommand \index{B (register)}
9434 \begin_inset LatexCommand \index{ACC}
9440 to pass the first parameter to a routine.
9441 The second parameter onwards is either allocated on the stack (for reentrant
9442 routines or if ---stack-auto is used) or in the internal / external ram
9443 (depending on the memory model).
9445 \layout Subsubsection
9447 Assembler Routine(non-reentrant
9448 \begin_inset LatexCommand \index{reentrant}
9453 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
9460 In the following example the function cfunc calls an assembler routine asm_func,
9461 which takes two parameters.
9467 extern int asm_func(unsigned char, unsigned char);
9471 int c_func (unsigned char i, unsigned char j)
9479 return asm_func(i,j);
9493 return c_func(10,9);
9501 The corresponding assembler function is:
9507 .globl _asm_func_PARM_2
9571 add a,_asm_func_PARM_2
9592 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
9612 Note here that the return values are placed in 'dpl' - One byte return value,
9613 'dpl' LSB & 'dph' MSB for two byte values.
9614 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
9615 b' & 'acc' for four byte values.
9618 The parameter naming convention is _<function_name>_PARM_<n>, where n is
9619 the parameter number starting from 1, and counting from the left.
9620 The first parameter is passed in
9621 \begin_inset Quotes eld
9625 \begin_inset Quotes erd
9628 for One bye parameter,
9629 \begin_inset Quotes eld
9633 \begin_inset Quotes erd
9637 \begin_inset Quotes eld
9641 \begin_inset Quotes erd
9645 \begin_inset Quotes eld
9649 \begin_inset Quotes erd
9652 for four bytes, the varible name for the second parameter will be _<function_na
9657 Assemble the assembler routine with the following command:
9664 asx8051 -losg asmfunc.asm
9671 Then compile and link the assembler routine to the C source file with the
9679 sdcc cfunc.c asmfunc.rel
9680 \layout Subsubsection
9682 Assembler Routine(reentrant
9683 \begin_inset LatexCommand \index{reentrant}
9688 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
9695 In this case the second parameter onwards will be passed on the stack, the
9696 parameters are pushed from right to left i.e.
9697 after the call the left most parameter will be on the top of the stack.
9704 extern int asm_func(unsigned char, unsigned char);
9708 int c_func (unsigned char i, unsigned char j) reentrant
9716 return asm_func(i,j);
9730 return c_func(10,9);
9738 The corresponding assembler routine is:
9848 The compiling and linking procedure remains the same, however note the extra
9849 entry & exit linkage required for the assembler code, _bp is the stack
9850 frame pointer and is used to compute the offset into the stack for parameters
9851 and local variables.
9855 \begin_inset LatexCommand \index{stack}
9860 \begin_inset LatexCommand \index{External stack}
9867 The external stack is located at the start of the external ram segment,
9868 and is 256 bytes in size.
9869 When ---xstack option is used to compile the program, the parameters and
9870 local variables of all reentrant functions are allocated in this area.
9871 This option is provided for programs with large stack space requirements.
9872 When used with the ---stack-auto option, all parameters and local variables
9873 are allocated on the external stack (note support libraries will need to
9874 be recompiled with the same options).
9877 The compiler outputs the higher order address byte of the external ram segment
9878 into PORT P2, therefore when using the External Stack option, this port
9879 MAY NOT be used by the application program.
9883 \begin_inset LatexCommand \index{ANSI-compliance}
9890 Deviations from the compliancy.
9893 functions are not always reentrant.
9896 structures cannot be assigned values directly, cannot be passed as function
9897 parameters or assigned to each other and cannot be a return value from
9924 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
9935 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
9957 return rets;/* is invalid in SDCC although allowed in ANSI */
9963 \begin_inset LatexCommand \index{long long (not supported)}
9968 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
9976 \begin_inset LatexCommand \index{double (not supported)}
9980 ' precision floating point
9981 \begin_inset LatexCommand \index{Floating point support}
9988 No support for setjmp and longjmp (for now).
9992 \begin_inset LatexCommand \index{K\&R style}
9996 function declarations are NOT allowed.
10002 foo(i,j) /* this old style of function declarations */
10004 int i,j; /* are valid in ANSI but not valid in SDCC */
10018 functions declared as pointers must be dereferenced during the call.
10029 /* has to be called like this */
10031 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
10034 Cyclomatic Complexity
10035 \begin_inset LatexCommand \index{Cyclomatic complexity}
10042 Cyclomatic complexity of a function is defined as the number of independent
10043 paths the program can take during execution of the function.
10044 This is an important number since it defines the number test cases you
10045 have to generate to validate the function.
10046 The accepted industry standard for complexity number is 10, if the cyclomatic
10047 complexity reported by SDCC exceeds 10 you should think about simplification
10048 of the function logic.
10049 Note that the complexity level is not related to the number of lines of
10050 code in a function.
10051 Large functions can have low complexity, and small functions can have large
10057 SDCC uses the following formula to compute the complexity:
10062 complexity = (number of edges in control flow graph) - (number of nodes
10063 in control flow graph) + 2;
10067 Having said that the industry standard is 10, you should be aware that in
10068 some cases it be may unavoidable to have a complexity level of less than
10070 For example if you have switch statement with more than 10 case labels,
10071 each case label adds one to the complexity level.
10072 The complexity level is by no means an absolute measure of the algorithmic
10073 complexity of the function, it does however provide a good starting point
10074 for which functions you might look at for further optimization.
10080 Here are a few guidelines that will help the compiler generate more efficient
10081 code, some of the tips are specific to this compiler others are generally
10082 good programming practice.
10085 Use the smallest data type to represent your data-value.
10086 If it is known in advance that the value is going to be less than 256 then
10087 use an 'unsigned char' instead of a 'short' or 'int'.
10090 Use unsigned when it is known in advance that the value is not going to
10092 This helps especially if you are doing division or multiplication.
10095 NEVER jump into a LOOP.
10098 Declare the variables to be local whenever possible, especially loop control
10099 variables (induction).
10102 Since the compiler does not always do implicit integral promotion, the programme
10103 r should do an explicit cast when integral promotion is required.
10106 Reducing the size of division, multiplication & modulus operations can reduce
10107 code size substantially.
10108 Take the following code for example.
10114 foobar(unsigned int p1, unsigned char ch)
10118 unsigned char ch1 = p1 % ch ;
10129 For the modulus operation the variable ch will be promoted to unsigned int
10130 first then the modulus operation will be performed (this will lead to a
10131 call to support routine _moduint()), and the result will be casted to a
10133 If the code is changed to
10139 foobar(unsigned int p1, unsigned char ch)
10143 unsigned char ch1 = (unsigned char)p1 % ch ;
10154 It would substantially reduce the code generated (future versions of the
10155 compiler will be smart enough to detect such optimization oppurtunities).
10158 Notes on MCS51 memory
10159 \begin_inset LatexCommand \index{MCS51 memory}
10166 The 8051 family of micro controller have a minimum of 128 bytes of internal
10167 memory which is structured as follows
10171 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
10174 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
10176 - Bytes 30-7F - 60 bytes for general purpose use.
10180 Normally the SDCC compiler will only utilise the first bank
10181 \begin_inset LatexCommand \index{bank}
10185 of registers, but it is possible to specify that other banks of registers
10186 should be used in interrupt
10187 \begin_inset LatexCommand \index{interrupt}
10192 By default, the compiler will place the stack after the last bank of used
10194 if the first 2 banks of registers are used, it will position the base of
10195 the internal stack at address 16 (0X10).
10196 This implies that as the stack
10197 \begin_inset LatexCommand \index{stack}
10201 grows, it will use up the remaining register banks, and the 16 bytes used
10202 by the 128 bit variables, and 60 bytes for general purpose use.
10205 By default, the compiler uses the 60 general purpose bytes to hold "near
10207 The compiler/optimiser may also declare some Local Variables in this area
10208 to hold local data.
10212 If any of the 128 bit variables are used, or near data is being used then
10213 care needs to be taken to ensure that the stack does not grow so much that
10214 it starts to over write either your bit variables or "near data".
10215 There is no runtime checking to prevent this from happening.
10218 The amount of stack being used is affected by the use of the "internal stack"
10219 to save registers before a subroutine call is made (---stack-auto
10220 \begin_inset LatexCommand \index{---stack-auto}
10224 will declare parameters and local variables on the stack) and the number
10225 of nested subroutines.
10228 If you detect that the stack is over writing you data, then the following
10230 ---xstack will cause an external stack to be used for saving registers
10231 and (if ---stack-auto is being used) storing parameters and local variables.
10232 However this will produce more code which will be slower to execute.
10237 \begin_inset LatexCommand \index{---stack-loc}
10241 will allow you specify the start of the stack, i.e.
10242 you could start it after any data in the general purpose area.
10243 However this may waste the memory not used by the register banks and if
10244 the size of the "near data" increases, it may creep into the bottom of
10248 ---stack-after-data
10249 \begin_inset LatexCommand \index{---stack-after-data}
10253 , similar to the ---stack-loc, but it automatically places the stack after
10254 the end of the "near data".
10255 Again this could waste any spare register space.
10259 \begin_inset LatexCommand \index{---data-loc}
10263 allows you to specify the start address of the near data.
10264 This could be used to move the "near data" further away from the stack
10265 giving it more room to grow.
10266 This will only work if no bit variables are being used and the stack can
10267 grow to use the bit variable space.
10275 If you find that the stack is over writing your bit variables or "near data"
10276 then the approach which best utilised the internal memory is to position
10277 the "near data" after the last bank of used registers or, if you use bit
10278 variables, after the last bit variable by using the ---data-loc, e.g.
10279 if two register banks are being used and no bit variables, ---data-loc
10280 16, and use the ---stack-after-data option.
10283 If bit variables are being used, another method would be to try and squeeze
10284 the data area in the unused register banks if it will fit, and start the
10285 stack after the last bit variable.
10289 \begin_inset LatexCommand \index{Tools}
10293 included in the distribution
10297 \begin_inset Tabular
10298 <lyxtabular version="3" rows="9" columns="3">
10300 <column alignment="center" valignment="top" leftline="true" width="0pt">
10301 <column alignment="center" valignment="top" leftline="true" width="0pt">
10302 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
10303 <row topline="true" bottomline="true">
10304 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10312 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10320 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10329 <row topline="true">
10330 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10343 Simulator for various architectures
10346 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10355 <row topline="true">
10356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10364 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10369 header file conversion
10372 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10377 sdcc/support/scripts
10381 <row topline="true">
10382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10395 header file conversion
10398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10403 sdcc/support/scripts
10407 <row topline="true">
10408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10416 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10442 <row topline="true">
10443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10477 <row topline="true">
10478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10494 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10512 <row topline="true">
10513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10547 <row topline="true" bottomline="true">
10548 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10555 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10562 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10577 Related open source tools
10578 \begin_inset LatexCommand \index{Related tools}
10586 \begin_inset Tabular
10587 <lyxtabular version="3" rows="5" columns="3">
10589 <column alignment="center" valignment="top" leftline="true" width="0pt">
10590 <column alignment="center" valignment="top" leftline="true" width="0pt">
10591 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
10592 <row topline="true" bottomline="true">
10593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10601 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10618 <row topline="true">
10619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10625 \begin_inset LatexCommand \index{gpsim}
10632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10646 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
10654 <row topline="true">
10655 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10661 \begin_inset LatexCommand \index{srecord}
10668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10673 Object file conversion, checksumming, ...
10676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10682 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
10690 <row topline="true">
10691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10697 \begin_inset LatexCommand \index{objdump}
10704 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10709 Object file conversion
10712 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10717 Part of binutils (should be there anyway)
10721 <row topline="true" bottomline="true">
10722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10728 \begin_inset LatexCommand \index{ddd}
10735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10740 Debugger, serves nicely as GUI to sdcdb
10741 \begin_inset LatexCommand \index{sdcdb}
10748 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10754 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
10769 Related documentation / recommended reading
10773 \begin_inset Tabular
10774 <lyxtabular version="3" rows="4" columns="3">
10776 <column alignment="center" valignment="top" leftline="true" width="0pt">
10777 <column alignment="center" valignment="top" leftline="true" width="0pt">
10778 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
10779 <row topline="true" bottomline="true">
10780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10805 <row topline="true">
10806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10818 Compiler architecture
10821 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10830 <row topline="true">
10831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10836 test_suite_spec.pdf
10839 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10844 sdcc regression test
10847 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10856 <row topline="true" bottomline="true">
10857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10883 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10888 sdcc internal documentation
10891 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10907 Retargetting for other MCUs.
10910 The issues for retargetting the compiler are far too numerous to be covered
10912 What follows is a brief description of each of the seven phases of the
10913 compiler and its MCU dependency.
10916 Parsing the source and building the annotated parse tree.
10917 This phase is largely MCU independent (except for the language extensions).
10918 Syntax & semantic checks are also done in this phase, along with some initial
10919 optimizations like back patching labels and the pattern matching optimizations
10920 like bit-rotation etc.
10923 The second phase involves generating an intermediate code which can be easy
10924 manipulated during the later phases.
10925 This phase is entirely MCU independent.
10926 The intermediate code generation assumes the target machine has unlimited
10927 number of registers, and designates them with the name iTemp.
10928 The compiler can be made to dump a human readable form of the code generated
10929 by using the ---dumpraw option.
10932 This phase does the bulk of the standard optimizations and is also MCU independe
10934 This phase can be broken down into several sub-phases:
10938 Break down intermediate code (iCode) into basic blocks.
10940 Do control flow & data flow analysis on the basic blocks.
10942 Do local common subexpression elimination, then global subexpression elimination
10944 Dead code elimination
10948 If loop optimizations caused any changes then do 'global subexpression eliminati
10949 on' and 'dead code elimination' again.
10952 This phase determines the live-ranges; by live range I mean those iTemp
10953 variables defined by the compiler that still survive after all the optimization
10955 Live range analysis
10956 \begin_inset LatexCommand \index{Live range analysis}
10960 is essential for register allocation, since these computation determines
10961 which of these iTemps will be assigned to registers, and for how long.
10964 Phase five is register allocation.
10965 There are two parts to this process.
10969 The first part I call 'register packing' (for lack of a better term).
10970 In this case several MCU specific expression folding is done to reduce
10975 The second part is more MCU independent and deals with allocating registers
10976 to the remaining live ranges.
10977 A lot of MCU specific code does creep into this phase because of the limited
10978 number of index registers available in the 8051.
10981 The Code generation phase is (unhappily), entirely MCU dependent and very
10982 little (if any at all) of this code can be reused for other MCU.
10983 However the scheme for allocating a homogenized assembler operand for each
10984 iCode operand may be reused.
10987 As mentioned in the optimization section the peep-hole optimizer is rule
10988 based system, which can reprogrammed for other MCUs.
10992 \begin_inset LatexCommand \index{sdcdb}
10996 - Source Level Debugger
10997 \begin_inset LatexCommand \index{Debugger}
11004 SDCC is distributed with a source level debugger.
11005 The debugger uses a command line interface, the command repertoire of the
11006 debugger has been kept as close to gdb
11007 \begin_inset LatexCommand \index{gdb}
11011 (the GNU debugger) as possible.
11012 The configuration and build process is part of the standard compiler installati
11013 on, which also builds and installs the debugger in the target directory
11014 specified during configuration.
11015 The debugger allows you debug BOTH at the C source and at the ASM source
11019 Compiling for Debugging
11022 The \SpecialChar \-
11024 debug option must be specified for all files for which debug information
11025 is to be generated.
11026 The complier generates a .adb file for each of these files.
11027 The linker creates the .cdb file from the .adb files and the address information.
11028 This .cdb is used by the debugger.
11031 How the Debugger Works
11034 When the ---debug option is specified the compiler generates extra symbol
11035 information some of which are put into the the assembler source and some
11036 are put into the .adb file.
11037 Then the linker creates the .cdb file from the individual .adb files with
11038 the address information for the symbols.
11039 The debugger reads the symbolic information generated by the compiler &
11040 the address information generated by the linker.
11041 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
11042 execution is controlled by the debugger.
11043 When a command is issued for the debugger, it translates it into appropriate
11044 commands for the simulator.
11047 Starting the Debugger
11050 The debugger can be started using the following command line.
11051 (Assume the file you are debugging has the file name foo).
11065 The debugger will look for the following files.
11068 foo.c - the source file.
11071 foo.cdb - the debugger symbol information file.
11074 foo.ihx - the intel hex format
11075 \begin_inset LatexCommand \index{Intel hex format}
11082 Command Line Options.
11085 ---directory=<source file directory> this option can used to specify the
11086 directory search list.
11087 The debugger will look into the directory list specified for source, cdb
11089 The items in the directory list must be separated by ':', e.g.
11090 if the source files can be in the directories /home/src1 and /home/src2,
11091 the ---directory option should be ---directory=/home/src1:/home/src2.
11092 Note there can be no spaces in the option.
11096 -cd <directory> - change to the <directory>.
11099 -fullname - used by GUI front ends.
11102 -cpu <cpu-type> - this argument is passed to the simulator please see the
11103 simulator docs for details.
11106 -X <Clock frequency > this options is passed to the simulator please see
11107 the simulator docs for details.
11110 -s <serial port file> passed to simulator see the simulator docs for details.
11113 -S <serial in,out> passed to simulator see the simulator docs for details.
11119 As mention earlier the command interface for the debugger has been deliberately
11120 kept as close the GNU debugger gdb, as possible.
11121 This will help the integration with existing graphical user interfaces
11122 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
11123 \layout Subsubsection
11125 break [line | file:line | function | file:function]
11128 Set breakpoint at specified line or function:
11137 sdcdb>break foo.c:100
11139 sdcdb>break funcfoo
11141 sdcdb>break foo.c:funcfoo
11142 \layout Subsubsection
11144 clear [line | file:line | function | file:function ]
11147 Clear breakpoint at specified line or function:
11156 sdcdb>clear foo.c:100
11158 sdcdb>clear funcfoo
11160 sdcdb>clear foo.c:funcfoo
11161 \layout Subsubsection
11166 Continue program being debugged, after breakpoint.
11167 \layout Subsubsection
11172 Execute till the end of the current function.
11173 \layout Subsubsection
11178 Delete breakpoint number 'n'.
11179 If used without any option clear ALL user defined break points.
11180 \layout Subsubsection
11182 info [break | stack | frame | registers ]
11185 info break - list all breakpoints
11188 info stack - show the function call stack.
11191 info frame - show information about the current execution frame.
11194 info registers - show content of all registers.
11195 \layout Subsubsection
11200 Step program until it reaches a different source line.
11201 \layout Subsubsection
11206 Step program, proceeding through subroutine calls.
11207 \layout Subsubsection
11212 Start debugged program.
11213 \layout Subsubsection
11218 Print type information of the variable.
11219 \layout Subsubsection
11224 print value of variable.
11225 \layout Subsubsection
11230 load the given file name.
11231 Note this is an alternate method of loading file for debugging.
11232 \layout Subsubsection
11237 print information about current frame.
11238 \layout Subsubsection
11243 Toggle between C source & assembly source.
11244 \layout Subsubsection
11246 ! simulator command
11249 Send the string following '!' to the simulator, the simulator response is
11251 Note the debugger does not interpret the command being sent to the simulator,
11252 so if a command like 'go' is sent the debugger can loose its execution
11253 context and may display incorrect values.
11254 \layout Subsubsection
11261 My name is Bobby Brown"
11264 Interfacing with XEmacs
11265 \begin_inset LatexCommand \index{XEmacs}
11270 \begin_inset LatexCommand \index{Emacs}
11277 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
11278 sdcdb.el and sdcdbsrc.el.
11279 These two files can be found in the $(prefix)/bin directory after the installat
11281 These files need to be loaded into XEmacs for the interface to work.
11282 This can be done at XEmacs startup time by inserting the following into
11283 your '.xemacs' file (which can be found in your HOME directory):
11289 (load-file sdcdbsrc.el)
11295 .xemacs is a lisp file so the () around the command is REQUIRED.
11296 The files can also be loaded dynamically while XEmacs is running, set the
11297 environment variable 'EMACSLOADPATH' to the installation bin directory
11298 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
11299 To start the interface enter the following command:
11313 You will prompted to enter the file name to be debugged.
11318 The command line options that are passed to the simulator directly are bound
11319 to default values in the file sdcdbsrc.el.
11320 The variables are listed below, these values maybe changed as required.
11323 sdcdbsrc-cpu-type '51
11326 sdcdbsrc-frequency '11059200
11329 sdcdbsrc-serial nil
11332 The following is a list of key mapping for the debugger interface.
11340 ;; Current Listing ::
11342 ;;key\SpecialChar ~
11357 binding\SpecialChar ~
11381 ;;---\SpecialChar ~
11396 ------\SpecialChar ~
11436 sdcdb-next-from-src\SpecialChar ~
11462 sdcdb-back-from-src\SpecialChar ~
11488 sdcdb-cont-from-src\SpecialChar ~
11498 SDCDB continue command
11514 sdcdb-step-from-src\SpecialChar ~
11540 sdcdb-whatis-c-sexp\SpecialChar ~
11550 SDCDB ptypecommand for data at
11614 sdcdbsrc-delete\SpecialChar ~
11628 SDCDB Delete all breakpoints if no arg
11676 given or delete arg (C-u arg x)
11692 sdcdbsrc-frame\SpecialChar ~
11707 SDCDB Display current frame if no arg,
11756 given or display frame arg
11821 sdcdbsrc-goto-sdcdb\SpecialChar ~
11831 Goto the SDCDB output buffer
11847 sdcdb-print-c-sexp\SpecialChar ~
11858 SDCDB print command for data at
11922 sdcdbsrc-goto-sdcdb\SpecialChar ~
11932 Goto the SDCDB output buffer
11948 sdcdbsrc-mode\SpecialChar ~
11964 Toggles Sdcdbsrc mode (turns it off)
11968 ;; C-c C-f\SpecialChar ~
11976 sdcdb-finish-from-src\SpecialChar ~
11984 SDCDB finish command
11988 ;; C-x SPC\SpecialChar ~
11996 sdcdb-break\SpecialChar ~
12014 Set break for line with point
12016 ;; ESC t\SpecialChar ~
12026 sdcdbsrc-mode\SpecialChar ~
12042 Toggle Sdcdbsrc mode
12044 ;; ESC m\SpecialChar ~
12054 sdcdbsrc-srcmode\SpecialChar ~
12078 The Z80 and gbz80 port
12081 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
12082 The port is incomplete - long support is incomplete (mul, div and mod are
12083 unimplimented), and both float and bitfield support is missing.
12084 Apart from that the code generated is correct.
12087 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
12088 The stack frame is similar to that generated by the IAR Z80 compiler.
12089 IX is used as the base pointer, HL is used as a temporary register, and
12090 BC and DE are available for holding varibles.
12091 IY is currently unusued.
12092 Return values are stored in HL.
12093 One bad side effect of using IX as the base pointer is that a functions
12094 stack frame is limited to 127 bytes - this will be fixed in a later version.
12098 \begin_inset LatexCommand \index{Support}
12105 SDCC has grown to be a large project.
12106 The compiler alone (without the preprocessor, assembler and linker) is
12107 about 40,000 lines of code (blank stripped).
12108 The open source nature of this project is a key to its continued growth
12110 You gain the benefit and support of many active software developers and
12112 Is SDCC perfect? No, that's why we need your help.
12113 The developers take pride in fixing reported bugs.
12114 You can help by reporting the bugs and helping other SDCC users.
12115 There are lots of ways to contribute, and we encourage you to take part
12116 in making SDCC a great software package.
12120 The SDCC project is hosted on the sdcc sourceforge site at
12121 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
12126 You'll find the complete set of mailing lists
12127 \begin_inset LatexCommand \index{Mailing list}
12131 , forums, bug reporting system, patch submission
12132 \begin_inset LatexCommand \index{Patch submission}
12137 \begin_inset LatexCommand \index{download}
12141 area and cvs code repository
12142 \begin_inset LatexCommand \index{cvs code repository}
12150 \begin_inset LatexCommand \index{Bugs}
12155 \begin_inset LatexCommand \index{Reporting bugs}
12162 The recommended way of reporting bugs is using the infrastructure of the
12164 You can follow the status of bug reports there and have an overview about
12168 Bug reports are automatically forwarded to the developer mailing list and
12169 will be fixed ASAP.
12170 When reporting a bug, it is very useful to include a small test program
12171 which reproduces the problem.
12172 If you can isolate the problem by looking at the generated assembly code,
12173 this can be very helpful.
12174 Compiling your program with the ---dumpall
12175 \begin_inset LatexCommand \index{---dumpall}
12179 option can sometimes be useful in locating optimization problems.
12182 Please have a short check that you are using a recent version of SDCC and
12183 the bug is not yet known.
12184 This is the link for reporting bugs:
12185 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
12192 Requesting Features
12193 \begin_inset LatexCommand \index{Feature request}
12198 \begin_inset LatexCommand \index{Requesting features}
12205 Like bug reports feature requests are forwarded to the developer mailing
12207 This is the link for requesting features:
12208 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
12218 These links should take you directly to the
12219 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
12229 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
12230 spam and automated messages (mid 2003)
12234 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
12238 , lists and forums are archived so if you are lucky someone already had
12243 \begin_inset LatexCommand \index{Changelog}
12250 You can follow the status of the cvs version
12251 \begin_inset LatexCommand \index{version}
12255 of SDCC by watching the file
12256 \begin_inset LatexCommand \htmlurl[ChangeLog]{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
12260 in the cvs-repository.
12264 \begin_inset LatexCommand \index{Release policy}
12271 Historically there often were long delays between official releases and
12272 the sourceforge download area tends to get not updated at all.
12273 Current excuses might refer to problems with live range analysis, but if
12274 this is fixed, the next problem rising is that another excuse will have
12276 Kidding aside, we have to get better there!
12280 \begin_inset LatexCommand \index{Examples}
12287 You'll find some small examples in the directory sdcc/device/examples/
12290 Maybe we should include some links to real world applications.
12291 Preferrably pointer to pointers (one for each architecture) so this stays
12296 \begin_inset LatexCommand \index{Quality control}
12303 The compiler is passed through nightly compile and build checks.
12309 \begin_inset LatexCommand \index{Regression test}
12313 check that SDCC itself compiles flawlessly on several platforms and checks
12314 the quality of the code generated by SDCC by running the code through simulator
12316 There is a separate document
12319 \begin_inset LatexCommand \index{Test suite}
12328 You'll find the test code in the directory
12330 sdcc/support/regression
12333 You can run these tests manually by running
12337 in this directory (or f.e.
12342 if you don't want to run the complete tests).
12343 The test code might also be interesting if you want to look for examples
12344 \begin_inset LatexCommand \index{Examples}
12348 checking corner cases of SDCC or if you plan to submit patches
12349 \begin_inset LatexCommand \index{Patch submission}
12356 The pic port uses a different set of regression tests, you'll find them
12359 sdcc/src/regression
12365 \begin_inset LatexCommand \index{Compiler internals}
12372 The anatomy of the compiler
12377 This is an excerpt from an atricle published in Circuit Cellar MagaZine
12379 It's a little outdated (the compiler is much more efficient now and user/devell
12380 oper friendly), but pretty well exposes the guts of it all.
12386 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
12387 It is fairly easy to retarget for other 8-bit MCU.
12388 Here we take a look at some of the internals of the compiler.
12393 \begin_inset LatexCommand \index{Parsing}
12400 Parsing the input source file and creating an AST (Annotated Syntax Tree
12401 \begin_inset LatexCommand \index{Annotated syntax tree}
12406 This phase also involves propagating types (annotating each node of the
12407 parse tree with type information) and semantic analysis.
12408 There are some MCU specific parsing rules.
12409 For example the storage classes, the extended storage classes are MCU specific
12410 while there may be a xdata storage class for 8051 there is no such storage
12411 class for z80 or Atmel AVR.
12412 SDCC allows MCU specific storage class extensions, i.e.
12413 xdata will be treated as a storage class specifier when parsing 8051 C
12414 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
12419 \begin_inset LatexCommand \index{iCode}
12426 Intermediate code generation.
12427 In this phase the AST is broken down into three-operand form (iCode).
12428 These three operand forms are represented as doubly linked lists.
12429 ICode is the term given to the intermediate form generated by the compiler.
12430 ICode example section shows some examples of iCode generated for some simple
12431 C source functions.
12435 \begin_inset LatexCommand \index{Optimizations}
12442 Bulk of the target independent optimizations is performed in this phase.
12443 The optimizations include constant propagation, common sub-expression eliminati
12444 on, loop invariant code movement, strength reduction of loop induction variables
12445 and dead-code elimination.
12448 Live range analysis
12449 \begin_inset LatexCommand \index{Live range analysis}
12456 During intermediate code generation phase, the compiler assumes the target
12457 machine has infinite number of registers and generates a lot of temporary
12459 The live range computation determines the lifetime of each of these compiler-ge
12460 nerated temporaries.
12461 A picture speaks a thousand words.
12462 ICode example sections show the live range annotations for each of the
12464 It is important to note here, each iCode is assigned a number in the order
12465 of its execution in the function.
12466 The live ranges are computed in terms of these numbers.
12467 The from number is the number of the iCode which first defines the operand
12468 and the to number signifies the iCode which uses this operand last.
12471 Register Allocation
12472 \begin_inset LatexCommand \index{Register allocation}
12479 The register allocation determines the type and number of registers needed
12481 In most MCUs only a few registers can be used for indirect addressing.
12482 In case of 8051 for example the registers R0 & R1 can be used to indirectly
12483 address the internal ram and DPTR to indirectly address the external ram.
12484 The compiler will try to allocate the appropriate register to pointer variables
12486 ICode example section shows the operands annotated with the registers assigned
12488 The compiler will try to keep operands in registers as much as possible;
12489 there are several schemes the compiler uses to do achieve this.
12490 When the compiler runs out of registers the compiler will check to see
12491 if there are any live operands which is not used or defined in the current
12492 basic block being processed, if there are any found then it will push that
12493 operand and use the registers in this block, the operand will then be popped
12494 at the end of the basic block.
12498 There are other MCU specific considerations in this phase.
12499 Some MCUs have an accumulator; very short-lived operands could be assigned
12500 to the accumulator instead of general-purpose register.
12506 Figure II gives a table of iCode operations supported by the compiler.
12507 The code generation involves translating these operations into corresponding
12508 assembly code for the processor.
12509 This sounds overly simple but that is the essence of code generation.
12510 Some of the iCode operations are generated on a MCU specific manner for
12511 example, the z80 port does not use registers to pass parameters so the
12512 SEND and RECV iCode operations will not be generated, and it also does
12513 not support JUMPTABLES.
12520 <Where is Figure II ?>
12524 \begin_inset LatexCommand \index{iCode}
12531 This section shows some details of iCode.
12532 The example C code does not do anything useful; it is used as an example
12533 to illustrate the intermediate code generated by the compiler.
12546 /* This function does nothing useful.
12553 for the purpose of explaining iCode */
12556 short function (data int *x)
12564 short i=10; /* dead initialization eliminated */
12569 short sum=10; /* dead initialization eliminated */
12582 while (*x) *x++ = *p++;
12596 /* compiler detects i,j to be induction variables */
12600 for (i = 0, j = 10 ; i < 10 ; i++, j---) {
12612 mul += i * 3; /* this multiplication remains */
12618 gint += j * 3;/* this multiplication changed to addition */
12635 In addition to the operands each iCode contains information about the filename
12636 and line it corresponds to in the source file.
12637 The first field in the listing should be interpreted as follows:
12642 Filename(linenumber: iCode Execution sequence number : ICode hash table
12643 key : loop depth of the iCode).
12648 Then follows the human readable form of the ICode operation.
12649 Each operand of this triplet form can be of three basic types a) compiler
12650 generated temporary b) user defined variable c) a constant value.
12651 Note that local variables and parameters are replaced by compiler generated
12654 \begin_inset LatexCommand \index{Live range analysis}
12658 are computed only for temporaries (i.e.
12659 live ranges are not computed for global variables).
12661 \begin_inset LatexCommand \index{Register allocation}
12665 are allocated for temporaries only.
12666 Operands are formatted in the following manner:
12671 Operand Name [lr live-from : live-to ] { type information } [ registers
12677 As mentioned earlier the live ranges are computed in terms of the execution
12678 sequence number of the iCodes, for example
12680 the iTemp0 is live from (i.e.
12681 first defined in iCode with execution sequence number 3, and is last used
12682 in the iCode with sequence number 5).
12683 For induction variables such as iTemp21 the live range computation extends
12684 the lifetime from the start to the end of the loop.
12686 The register allocator used the live range information to allocate registers,
12687 the same registers may be used for different temporaries if their live
12688 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
12689 iTemp17 since their live ranges do not overlap.
12690 In addition the allocator also takes into consideration the type and usage
12691 of a temporary, for example itemp6 is a pointer to near space and is used
12692 as to fetch data from (i.e.
12693 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
12694 Some short lived temporaries are allocated to special registers which have
12695 meaning to the code generator e.g.
12696 iTemp13 is allocated to a pseudo register CC which tells the back end that
12697 the temporary is used only for a conditional jump the code generation makes
12698 use of this information to optimize a compare and jump ICode.
12700 There are several loop optimizations
12701 \begin_inset LatexCommand \index{Loop optimization}
12705 performed by the compiler.
12706 It can detect induction variables iTemp21(i) and iTemp23(j).
12707 Also note the compiler does selective strength reduction
12708 \begin_inset LatexCommand \index{Strength reduction}
12713 the multiplication of an induction variable in line 18 (gint = j * 3) is
12714 changed to addition, a new temporary iTemp17 is allocated and assigned
12715 a initial value, a constant 3 is then added for each iteration of the loop.
12716 The compiler does not change the multiplication
12717 \begin_inset LatexCommand \index{Multiplication}
12721 in line 17 however since the processor does support an 8 * 8 bit multiplication.
12723 Note the dead code elimination
12724 \begin_inset LatexCommand \index{Dead-code elimination}
12728 optimization eliminated the dead assignments in line 7 & 8 to I and sum
12736 Sample.c (5:1:0:0) _entry($9) :
12741 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
12746 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
12751 Sample.c(11:4:53:0) preHeaderLbl0($11) :
12756 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
12762 Sample.c(11:6:5:1) _whilecontinue_0($1) :
12767 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
12773 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
12778 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
12784 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
12790 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
12796 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
12802 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
12803 * int}[r0] + 0x2 {short}
12808 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
12813 Sample.c(11:17:21:0)_whilebreak_0($3) :
12818 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
12823 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
12828 Sample.c(15:20:54:0)preHeaderLbl1($13) :
12833 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
12838 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
12843 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
12848 Sample.c(15:24:26:1)_forcond_0($4) :
12853 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
12859 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
12864 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
12865 + ITemp21 [lr21:38]{short}[r4]
12870 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
12876 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
12877 + iTemp15 [lr29:30]{short}[r1]
12882 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
12888 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
12894 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
12900 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
12906 Sample.c(19:38:47:1) goto _forcond_0($4)
12911 Sample.c(19:39:48:0)_forbreak_0($7) :
12916 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
12917 + ITemp11 [lr19:40]{short}[r3]
12922 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
12927 Sample.c(20:42:51:0)_return($8) :
12932 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
12938 Finally the code generated for this function:
12979 ; ----------------------------------------------
12984 ; function function
12989 ; ----------------------------------------------
12999 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
13011 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
13023 ;_whilecontinue_0($1) :
13033 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
13038 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
13097 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
13116 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
13163 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
13203 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
13229 ; iTemp6 [lr5:16]{_near * int}[r0] =
13234 ; iTemp6 [lr5:16]{_near * int}[r0] +
13251 ; goto _whilecontinue_0($1)
13263 ; _whilebreak_0($3) :
13273 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
13285 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
13297 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
13309 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
13328 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
13357 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
13362 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
13407 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
13412 ; iTemp21 [lr21:38]{short}[r4]
13438 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
13471 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
13476 ; iTemp15 [lr29:30]{short}[r1]
13495 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
13542 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
13589 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
13601 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
13615 cjne r5,#0xff,00104$
13627 ; goto _forcond_0($4)
13639 ; _forbreak_0($7) :
13649 ; ret iTemp24 [lr40:41]{short}
13692 A few words about basic block successors, predecessors and dominators
13695 Successors are basic blocks
13696 \begin_inset LatexCommand \index{Basic blocks}
13700 that might execute after this basic block.
13702 Predecessors are basic blocks that might execute before reaching this basic
13705 Dominators are basic blocks that WILL execute before reaching this basic
13731 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
13734 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
13737 c) domVect of [BB4] = BB1 ...
13738 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
13746 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
13756 Thanks to all the other volunteer developers who have helped with coding,
13757 testing, web-page creation, distribution sets, etc.
13758 You know who you are :-)
13765 This document was initially written by Sandeep Dutta
13768 All product names mentioned herein may be trademarks
13769 \begin_inset LatexCommand \index{Trademarks}
13773 of their respective companies.
13780 To avoid confusion, the installation and building options for sdcc itself
13781 (chapter 2) are not part of the index.
13785 \begin_inset LatexCommand \printindex{}