1 #LyX 1.2 created this file. For more info see http://www.lyx.org/
5 \usepackage[colorlinks=true,linkcolor=blue]{hyperref}
11 \paperfontsize default
18 \use_numerical_citations 0
19 \paperorientation portrait
22 \paragraph_separation indent
24 \quotes_language swedish
32 Please note: double dashed longoptions (e.g.
33 --version) need three dashes in this document to be visable in html and
37 SDCC Compiler User Guide
41 \begin_inset LatexCommand \tableofcontents{}
58 is a Freeware, retargettable, optimizing ANSI-C compiler by
62 designed for 8 bit Microprocessors.
63 The current version targets Intel MCS51 based Microprocessors(8051,8052,
64 etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
65 It can be retargetted for other microprocessors, support for PIC, AVR and
66 186 is under development.
67 The entire source code for the compiler is distributed under GPL.
68 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
69 SDCC has extensive language extensions suitable for utilizing various microcont
70 rollers and underlying hardware effectively.
75 In addition to the MCU specific optimizations SDCC also does a host of standard
79 global sub expression elimination,
82 loop optimizations (loop invariant, strength reduction of induction variables
86 constant folding & propagation,
102 For the back-end SDCC uses a global register allocation scheme which should
103 be well suited for other 8 bit MCUs.
108 The peep hole optimizer uses a rule based substitution mechanism which is
114 Supported data-types are:
117 char (8 bits, 1 byte),
120 short and int (16 bits, 2 bytes),
123 long (32 bit, 4 bytes)
130 The compiler also allows
132 inline assembler code
134 to be embedded anywhere in a function.
135 In addition, routines developed in assembly can also be called.
139 SDCC also provides an option (--cyclomatic) to report the relative complexity
141 These functions can then be further optimized, or hand coded in assembly
147 SDCC also comes with a companion source level debugger SDCDB, the debugger
148 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
153 The latest version can be downloaded from
154 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
166 All packages used in this compiler system are
174 ; source code for all the sub-packages (pre-processor, assemblers, linkers
175 etc) is distributed with the package.
176 This documentation is maintained using a freeware word processor (LyX).
178 This program is free software; you can redistribute it and/or modify it
179 under the terms of the GNU General Public License
180 \begin_inset LatexCommand \index{GNU General Public License}
185 \begin_inset LatexCommand \index{GPL}
189 as published by the Free Software Foundation; either version 2, or (at
190 your option) any later version.
191 This program is distributed in the hope that it will be useful, but WITHOUT
192 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
193 FOR A PARTICULAR PURPOSE.
194 See the GNU General Public License for more details.
195 You should have received a copy of the GNU General Public License along
196 with this program; if not, write to the Free Software Foundation, 59 Temple
197 Place - Suite 330, Boston, MA 02111-1307, USA.
198 In other words, you are welcome to use, share and improve this program.
199 You are forbidden to forbid anyone else to use, share and improve what
201 Help stamp out software-hoarding!
204 Typographic conventions
207 Throughout this manual, we will use the following convention.
208 Commands you have to type in are printed in
216 Code samples are printed in
221 Interesting items and new terms are printed in
226 Compatibility with previous versions
229 This version has numerous bug fixes compared with the previous version.
230 But we also introduced some incompatibilities with older versions.
231 Not just for the fun of it, but to make the compiler more stable, efficient
238 short is now equivalent to int (16 bits), it used to be equivalent to char
239 (8 bits) which is not ANSI compliant
242 the default directory for gcc-builds where include, library and documention
243 files are stored is now in /usr/local/share
246 char type parameters to vararg functions are casted to int unless explicitly
263 will push a as an int and as a char resp.
266 option ---regextend has been removed
269 option ---noregparms has been removed
272 option ---stack-after-data has been removed
277 <pending: more incompatibilities?>
283 What do you need before you start installation of SDCC? A computer, and
285 The preferred method of installation is to compile SDCC from source using
287 For Windows some pre-compiled binary distributions are available for your
289 You should have some experience with command line tools and compiler use.
295 The SDCC home page at
296 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
300 is a great place to find distribution sets.
301 You can also find links to the user mailing lists that offer help or discuss
302 SDCC with other SDCC users.
303 Web links to other SDCC related sites can also be found here.
304 This document can be found in the DOC directory of the source package as
306 Some of the other tools (simulator and assembler) included with SDCC contain
307 their own documentation and can be found in the source distribution.
308 If you want the latest unreleased software, the complete source package
309 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
312 Wishes for the future
315 There are (and always will be) some things that could be done.
316 Here are some I can think of:
323 char KernelFunction3(char p) at 0x340;
329 If you can think of some more, please send them to the list.
335 \begin_inset LatexCommand \index{Installation}
343 \begin_inset LatexCommand \index{Configure Options}
350 The install paths, search paths and other options are defined when running
352 The defaults can be overriden by:
354 \labelwidthstring 00.00.0000
356 ---prefix see tabel below
358 \labelwidthstring 00.00.0000
360 ---exec_prefix see tabel below
362 \labelwidthstring 00.00.0000
364 ---bindir see tabel below
366 \labelwidthstring 00.00.0000
368 ---datadir see tabel below
370 \labelwidthstring 00.00.0000
372 docdir environment variable, see tabel below
374 \labelwidthstring 00.00.0000
376 include_dir_suffix environment variable, see tabel below
378 \labelwidthstring 00.00.0000
380 lib_dir_suffix environment variable, see tabel below
382 \labelwidthstring 00.00.0000
384 sdccconf_h_dir_separator environment variable, either / or
389 This character will only be used in sdccconf.h; don't forget it's a C-header,
390 therefore a double-backslash is needed there.
392 \labelwidthstring 00.00.0000
394 ---disable-mcs51-port Excludes the Intel mcs51 port
396 \labelwidthstring 00.00.0000
398 ---disable-gbz80-port Excludes the Gameboy gbz80 port
400 \labelwidthstring 00.00.0000
402 ---disable-z80-port Excludes the z80 port
404 \labelwidthstring 00.00.0000
406 ---disable-avr-port Excludes the AVR port
408 \labelwidthstring 00.00.0000
410 ---disable-ds390-port Excludes the DS390 port
412 \labelwidthstring 00.00.0000
414 ---disable-pic-port Excludes the PIC port
416 \labelwidthstring 00.00.0000
418 ---disable-xa51-port Excludes the XA51 port
420 \labelwidthstring 00.00.0000
422 ---disable-ucsim Disables configuring and building of ucsim
424 \labelwidthstring 00.00.0000
426 ---disable-device-lib-build Disables automatically building device libraries
428 \labelwidthstring 00.00.0000
430 ---disable-packihx Disables building packihx
432 \labelwidthstring 00.00.0000
434 ---enable-libgc Use the Bohem memory allocator.
435 Lower runtime footprint.
438 Furthermore the environment variables CC, CFLAGS, ...
439 the tools and their arguments can be influenced.
440 Please see `configure ---help` and the man/info pages of `configure` for
445 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
446 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
447 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
448 At the moment it's not possible to change the default settings (it was
449 simply never required.
453 These configure options are compiled into the binaries, and can only be
454 changed by rerunning 'configure' and recompiling SDCC.
455 The configure options are written in
459 to distinguish them from run time environment variables (see section search
465 \begin_inset Quotes sld
469 \begin_inset Quotes srd
472 are used by the SDCC team to build the official Win32 binaries.
473 The SDCC team uses Mingw32 to build the official Windows binaries, because
480 a gcc compiler and last but not least
483 the binaries can be built by cross compiling on Sourceforge's compile farm.
486 See the examples, how to pass the Win32 settings to 'configure'.
487 The other Win32 builds using Borland, VC or whatever don't use 'configure',
488 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
499 <lyxtabular version="3" rows="8" columns="3">
501 <column alignment="left" valignment="top" leftline="true" width="0in">
502 <column alignment="left" valignment="top" leftline="true" width="0in">
503 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
504 <row topline="true" bottomline="true">
505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
521 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
531 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
581 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
603 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
641 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
665 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
687 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
713 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
730 <row topline="true" bottomline="true">
731 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
749 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
768 'configure' also computes relative paths.
769 This is needed for full relocatability of a binary package and to complete
770 search paths (see section search paths below):
776 <lyxtabular version="3" rows="4" columns="3">
778 <column alignment="left" valignment="top" leftline="true" width="0in">
779 <column alignment="left" valignment="top" leftline="true" width="0in">
780 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
781 <row topline="true" bottomline="true">
782 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
798 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
807 <row topline="true" bottomline="true">
808 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
837 <row bottomline="true">
838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
865 <row bottomline="true">
866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
876 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
906 ./configure ---prefix=
907 \begin_inset Quotes srd
911 \begin_inset Quotes srd
915 \begin_inset Quotes srd
919 \begin_inset Quotes srd
925 ./configure ---disable-avr-port ---disable-xa51-port
928 To crosscompile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw3
938 \begin_inset Quotes srd
942 \begin_inset Quotes srd
946 \begin_inset Quotes srd
950 \begin_inset Quotes srd
959 \begin_inset Quotes srd
962 i586-mingw32msvc-ranlib
963 \begin_inset Quotes srd
972 \begin_inset Quotes srd
975 i586-mingw32msvc-strip
976 \begin_inset Quotes srd
985 \begin_inset Quotes srd
989 \begin_inset Quotes srd
998 \begin_inset Quotes srd
1002 \begin_inset Quotes srd
1011 \begin_inset Quotes srd
1015 \begin_inset Quotes srd
1024 \begin_inset Quotes srd
1028 \begin_inset Quotes srd
1037 \begin_inset Quotes srd
1041 \begin_inset Quotes srd
1049 sdccconf_h_dir_separator=
1050 \begin_inset Quotes srd
1062 \begin_inset Quotes srd
1070 ---disable-device-lib-build
1080 ---host=i586-mingw32msvc ---build=unknown-unknown-linux-gnu
1084 \begin_inset Quotes sld
1088 \begin_inset Quotes srd
1091 compile on Cygwin for Mingw32(see also sdcc/support/scripts/sdcc_cygwin_mingw32)
1101 \begin_inset Quotes srd
1105 \begin_inset Quotes srd
1114 \begin_inset Quotes srd
1118 \begin_inset Quotes srd
1127 \begin_inset Quotes srd
1131 \begin_inset Quotes srd
1140 \begin_inset Quotes srd
1144 \begin_inset Quotes srd
1153 \begin_inset Quotes srd
1157 \begin_inset Quotes srd
1166 \begin_inset Quotes srd
1170 \begin_inset Quotes srd
1179 \begin_inset Quotes srd
1183 \begin_inset Quotes srd
1191 sdccconf_h_dir_separator=
1192 \begin_inset Quotes srd
1204 \begin_inset Quotes srd
1215 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1216 The option '--C' turns on caching, which gives a little bit extra speed.
1217 However if options are changed, it can be necessary to delete the config.cache
1222 \begin_inset LatexCommand \index{Install paths}
1230 Binary files (preprocessor, assembler and linker)
1234 \begin_inset Tabular
1235 <lyxtabular version="3" rows="2" columns="3">
1237 <column alignment="left" valignment="top" leftline="true" width="0in">
1238 <column alignment="left" valignment="top" leftline="true" width="0in">
1239 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1240 <row topline="true" bottomline="true">
1241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1266 <row topline="true" bottomline="true">
1267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1285 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1311 \begin_inset Tabular
1312 <lyxtabular version="3" rows="2" columns="3">
1314 <column alignment="block" valignment="top" leftline="true" width="1.6in">
1315 <column alignment="left" valignment="top" leftline="true" width="0in">
1316 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
1317 <row topline="true" bottomline="true">
1318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1343 <row topline="true" bottomline="true">
1344 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1361 /usr/local/share/sdcc/include
1364 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1390 is auto-appended by the compiler, e.g.
1391 small, large, z80, ds390 etc.)
1395 \begin_inset Tabular
1396 <lyxtabular version="3" rows="2" columns="3">
1398 <column alignment="left" valignment="top" leftline="true" width="0in">
1399 <column alignment="left" valignment="top" leftline="true" width="0in">
1400 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1401 <row topline="true" bottomline="true">
1402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1427 <row topline="true" bottomline="true">
1428 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1435 $DATADIR/$LIB_DIR_SUFFIX
1438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1443 /usr/local/share/sdcc/lib
1446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1472 \begin_inset Tabular
1473 <lyxtabular version="3" rows="2" columns="3">
1475 <column alignment="left" valignment="top" leftline="true" width="0in">
1476 <column alignment="left" valignment="top" leftline="true" width="0in">
1477 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1478 <row topline="true" bottomline="true">
1479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1504 <row topline="true" bottomline="true">
1505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1520 /usr/local/share/sdcc/doc
1523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1544 The install paths can still be changed during `make install` with e.g.:
1547 make install prefix=$(HOME)/local/sdcc
1550 Of course this doesn't change the search paths compiled into the binaries.
1554 \begin_inset LatexCommand \index{Search Paths}
1561 Some search paths or parts of them are determined by configure variables
1566 , see section above).
1567 Further search paths are determined by environment variables during runtime.
1570 The paths searched when running the compiler are as follows (the first catch
1576 Binary files (preprocessor, assembler and linker)
1579 \begin_inset Tabular
1580 <lyxtabular version="3" rows="4" columns="3">
1582 <column alignment="left" valignment="top" leftline="true" width="0in">
1583 <column alignment="left" valignment="top" leftline="true" width="0in">
1584 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1585 <row topline="true" bottomline="true">
1586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1602 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1611 <row topline="true">
1612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1622 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1630 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1641 <row topline="true">
1642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1647 Path of argv[0] (if available)
1650 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1658 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1667 <row topline="true" bottomline="true">
1668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1705 \begin_inset Tabular
1706 <lyxtabular version="3" rows="6" columns="3">
1708 <column alignment="block" valignment="top" leftline="true" width="1.5in">
1709 <column alignment="block" valignment="top" leftline="true" width="1.5in">
1710 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1711 <row topline="true" bottomline="true">
1712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1737 <row topline="true">
1738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1763 <row topline="true">
1764 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1789 <row topline="true">
1790 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1804 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1816 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1827 <row topline="true">
1828 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1896 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1909 <row topline="true" bottomline="true">
1910 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1926 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1931 /usr/local/share/sdcc/
1936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1953 The option ---nostdinc disables the last two search paths.
1960 With the exception of
1961 \begin_inset Quotes sld
1965 \begin_inset Quotes srd
1972 is auto-appended by the compiler (e.g.
1973 small, large, z80, ds390 etc.).
1977 \begin_inset Tabular
1978 <lyxtabular version="3" rows="6" columns="3">
1980 <column alignment="block" valignment="top" leftline="true" width="1.7in">
1981 <column alignment="left" valignment="top" leftline="true" width="1.2in">
1982 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
1983 <row topline="true" bottomline="true">
1984 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1992 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2009 <row topline="true">
2010 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2035 <row topline="true">
2036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2048 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2075 <row topline="true">
2076 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2087 $LIB_DIR_SUFFIX/<model>
2090 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2104 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2121 <row topline="true">
2122 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2137 $LIB_DIR_SUFFIX/<model>
2140 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2193 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2249 <row topline="true" bottomline="true">
2250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2259 $LIB_DIR_SUFFIX/<model>
2262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2267 /usr/local/share/sdcc/
2274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2290 Don't delete any of the stray spaces in the table above without checking
2291 the HTML output (last line)!
2297 The option ---nostdlib disables the last two search paths.
2301 \begin_inset LatexCommand \index{Building SDCC}
2306 \layout Subsubsection
2308 Building SDCC on Linux
2313 Download the source package
2315 either from the SDCC CVS repository or from the
2316 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2322 , it will be named something like sdcc
2335 Bring up a command line terminal, such as xterm.
2340 Unpack the file using a command like:
2343 "tar -xzf sdcc.src.tar.gz
2348 , this will create a sub-directory called sdcc with all of the sources.
2351 Change directory into the main SDCC directory, for example type:
2368 This configures the package for compilation on your system.
2384 All of the source packages will compile, this can take a while.
2400 This copies the binary executables, the include files, the libraries and
2401 the documentation to the install directories.
2402 \layout Subsubsection
2404 Building SDCC on OSX 2.x
2407 Follow the instruction for Linux.
2411 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2412 )) fails to compile SDCC.
2413 Fortunately there's also gcc 2.9.x installed, which works fine.
2414 This compiler can be selected by running 'configure' with:
2417 ./configure CC=gcc2 CXX=g++2
2418 \layout Subsubsection
2420 Crosscompiling SDCC on Linux for Windows
2423 With the Mingw32 gcc crosscompiler it's easy to compile SDCC for Win32.
2424 See section 'Configure Options'.
2425 \layout Subsubsection
2427 Building SDCC on Windows
2430 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2432 They use Unix-sockets, which are not available on Win32.
2433 \layout Subsubsection
2435 Windows Install Using a Binary Package
2438 Download the binary package and unpack it using your favorite unpacking
2439 tool (gunzip, WinZip, etc).
2440 This should unpack to a group of sub-directories.
2441 An example directory structure after unpacking the mingw32 package is:
2446 bin for the executables, c:
2454 lib for the include and libraries.
2457 Adjust your environment variable PATH to include the location of the bin
2458 directory or start sdcc using the full path.
2459 \layout Subsubsection
2461 Building SDCC using Cygwin and Mingw32
2464 For building and installing a Cygwin executable follow the instructions
2470 \begin_inset Quotes sld
2474 \begin_inset Quotes srd
2477 Win32-binary can be built, which will not need the Cygwin-DLL.
2478 For the necessary 'configure' options see section 'configure options' or
2479 the script 'sdcc/support/scripts/sdcc_cygwinmingw32'.
2483 In order to install Cygwin on Windows download setup.exe from
2484 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2490 \begin_inset Quotes sld
2493 default text file type
2494 \begin_inset Quotes srd
2498 \begin_inset Quotes sld
2502 \begin_inset Quotes srd
2505 and download/install at least the following packages.
2506 Some packages are selected by default, others will be automatically selected
2507 because of dependencies with the manually selected packages.
2508 Never deselect these packages!
2517 gcc ; version 3.x is fine, no need to use the old 2.9x
2520 binutils ; selected with gcc
2526 rxvt ; a nice console, which makes life much easier under windoze (see below)
2529 man ; not really needed for building SDCC, but you'll miss it sooner or
2533 less ; not really needed for building SDCC, but you'll miss it sooner or
2537 cvs ; only if you use CVS access
2540 If you want to develop something you'll need:
2543 python ; for the regression tests
2546 gdb ; the gnu debugger, together with the nice GUI
2547 \begin_inset Quotes sld
2551 \begin_inset Quotes srd
2557 openssh ; to access the CF or commit changes
2560 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
2561 use autoconf-stable!
2564 rxvt is a nice console with history.
2565 Replace in your cygwin.bat the line
2574 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
2577 -bg black -fg white -geometry 100x65 -e bash --login
2580 Text selected with the mouse is automatically copied to the clipboard, pasting
2581 works with shift-insert.
2585 The other good tip is to make sure you have no //c/-style paths anywhere,
2586 use /cygdrive/c/ instead.
2587 Using // invokes a network lookup which is very slow.
2589 \begin_inset Quotes sld
2593 \begin_inset Quotes srd
2596 is too long, you can change it with e.g.
2602 SDCC sources use the unix line ending LF.
2603 Life is much easier, if you store the source tree on a drive, which is
2604 mount in binary mode.
2605 And use an editor which can handle LF-only line endings.
2606 Make sure not to commit files with windows line endings.
2607 \layout Subsubsection
2609 Windows Install Using Microsoft Visual C++ 6.0/NET
2614 Download the source package
2616 either from the SDCC CVS repository or from the
2617 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2623 , it will be named something like sdcc
2630 SDCC is distributed with all the projects, workspaces, and files you need
2631 to build it using Visual C++ 6.0/NET.
2632 The workspace name is 'sdcc.dsw'.
2633 Please note that as it is now, all the executables are created in a folder
2637 Once built you need to copy the executables from sdcc
2641 bin before runnng SDCC.
2646 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
2647 of bison.exe, flex.exe, and gawk.exe.
2648 One good place to get them is
2649 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
2657 Download the file UnxUtils
2658 \begin_inset LatexCommand \index{UnxUtils}
2663 Now you have to install the utilities and setup Visual C++ so it can locate
2664 the required programs.
2665 Here there are two alternatives (choose one!):
2672 a) Extract UnxUtils.zip to your C:
2674 hard disk PRESERVING the original paths, otherwise bison won't work.
2675 (If you are using WinZip make certain that 'Use folder names' is selected)
2679 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
2680 in 'Show directories for:' select 'Executable files', and in the directories
2681 window add a new path: 'C:
2691 (As a side effect, you get a bunch of Unix utilities that could be useful,
2692 such as diff and patch.)
2699 This one avoids extracting a bunch of files you may not use, but requires
2704 a) Create a directory were to put the tools needed, or use a directory already
2712 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
2713 to such directory WITHOUT preserving the original paths.
2714 (If you are using WinZip make certain that 'Use folder names' is not selected)
2718 c) Rename bison.exe to '_bison.exe'.
2722 d) Create a batch file 'bison.bat' in 'C:
2726 ' and add these lines:
2746 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
2750 Steps 'c' and 'd' are needed because bison requires by default that the
2751 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
2752 '/usr/local/share/' I think.
2753 So it is necessary to tell bison where those files are located if they
2754 are not in such directory.
2755 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
2759 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
2760 in 'Show directories for:' select 'Executable files', and in the directories
2761 window add a new path: 'c:
2764 Note that you can use any other path instead of 'c:
2766 util', even the path where the Visual C++ tools are, probably: 'C:
2770 Microsoft Visual Studio
2775 So you don't have to execute step 'e' :)
2779 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
2780 the executables from sdcc
2784 bin, and you can compile using sdcc.
2785 \layout Subsubsection
2787 Windows Install Using Borland
2790 From the sdcc directory, run the command "make -f Makefile.bcc".
2791 This should regenerate all the .exe files in the bin directory except for
2792 sdcdb.exe (which currently doesn't build under Borland C++).
2795 If you modify any source files and need to rebuild, be aware that the dependanci
2796 es may not be correctly calculated.
2797 The safest option is to delete all .obj files and run the build again.
2798 From a Cygwin BASH prompt, this can easily be done with the commmand:
2808 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
2810 ) -print -exec rm {}
2819 or on Windows NT/2000/XP from the command prompt with the commmand:
2826 del /s *.obj *.lib *.rul
2829 from the sdcc directory.
2832 Building the Documentation
2839 Testing out the SDCC Compiler
2842 The first thing you should do after installing your SDCC compiler is to
2848 \begin_inset LatexCommand \index{version}
2855 at the prompt, and the program should run and tell you the version.
2856 If it doesn't run, or gives a message about not finding sdcc program, then
2857 you need to check over your installation.
2858 Make sure that the sdcc bin directory is in your executable search path
2859 defined by the PATH environment setting (see the Trouble-shooting section
2861 Make sure that the sdcc program is in the bin folder, if not perhaps something
2862 did not install correctly.
2870 is commonly installed as described in section
2871 \begin_inset Quotes sld
2874 Install and search paths
2875 \begin_inset Quotes srd
2884 Make sure the compiler works on a very simple example.
2885 Type in the following test.c program using your favorite
2920 Compile this using the following command:
2929 If all goes well, the compiler will generate a test.asm and test.rel file.
2930 Congratulations, you've just compiled your first program with SDCC.
2931 We used the -c option to tell SDCC not to link the generated code, just
2932 to keep things simple for this step.
2940 The next step is to try it with the linker.
2950 If all goes well the compiler will link with the libraries and produce
2951 a test.ihx output file.
2956 (no test.ihx, and the linker generates warnings), then the problem is most
2957 likely that sdcc cannot find the
2961 usr/local/share/sdcc/lib directory
2965 (see the Install trouble-shooting section for suggestions).
2973 The final test is to ensure sdcc can use the
2977 header files and libraries.
2978 Edit test.c and change it to the following:
2998 strcpy(str1, "testing");
3007 Compile this by typing
3014 This should generate a test.ihx output file, and it should give no warnings
3015 such as not finding the string.h file.
3016 If it cannot find the string.h file, then the problem is that sdcc cannot
3017 find the /usr/local/share/sdcc/include directory
3021 (see the Install trouble-shooting section for suggestions).
3024 Install Trouble-shooting
3025 \begin_inset LatexCommand \index{Install Trouble-shooting}
3030 \layout Subsubsection
3032 SDCC does not build correctly.
3035 A thing to try is starting from scratch by unpacking the .tgz source package
3036 again in an empty directory.
3044 ./configure 2>&1 | tee configure.log
3058 make 2>&1 | tee make.log
3065 If anything goes wrong, you can review the log files to locate the problem.
3066 Or a relevant part of this can be attached to an email that could be helpful
3067 when requesting help from the mailing list.
3068 \layout Subsubsection
3071 \begin_inset Quotes sld
3075 \begin_inset Quotes srd
3082 \begin_inset Quotes sld
3086 \begin_inset Quotes srd
3089 command is a script that analyzes your system and performs some configuration
3090 to ensure the source package compiles on your system.
3091 It will take a few minutes to run, and will compile a few tests to determine
3092 what compiler features are installed.
3093 \layout Subsubsection
3096 \begin_inset Quotes sld
3100 \begin_inset Quotes srd
3106 This runs the GNU make tool, which automatically compiles all the source
3107 packages into the final installed binary executables.
3108 \layout Subsubsection
3111 \begin_inset Quotes sld
3115 \begin_inset Quotes erd
3121 This will install the compiler, other executables libraries and include
3122 files in to the appropriate directories.
3124 \begin_inset Quotes sld
3127 Install and Search PATHS
3128 \begin_inset Quotes srd
3133 On most systems you will need super-user privilages to do this.
3139 SDCC is not just a compiler, but a collection of tools by various developers.
3140 These include linkers, assemblers, simulators and other components.
3141 Here is a summary of some of the components.
3142 Note that the included simulator and assembler have separate documentation
3143 which you can find in the source package in their respective directories.
3144 As SDCC grows to include support for other processors, other packages from
3145 various developers are included and may have their own sets of documentation.
3149 You might want to look at the files which are installed in <installdir>.
3150 At the time of this writing, we find the following programs for gcc-builds:
3154 In <installdir>/bin:
3157 sdcc - The compiler.
3160 sdcpp - The C preprocessor.
3163 asx8051 - The assembler for 8051 type processors.
3170 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3173 aslink -The linker for 8051 type processors.
3180 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3183 s51 - The ucSim 8051 simulator.
3186 sdcdb - The source debugger.
3189 packihx - A tool to pack (compress) Intel hex files.
3192 In <installdir>/share/sdcc/include
3198 In <installdir>/share/sdcc/lib
3201 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3205 In <installdir>/share/sdcc/doc
3211 As development for other processors proceeds, this list will expand to include
3212 executables to support processors like AVR, PIC, etc.
3213 \layout Subsubsection
3218 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3219 the assembler and linkage editor.
3220 \layout Subsubsection
3222 sdcpp - The C-Preprocessor
3225 The preprocessor is a modified version of the GNU preprocessor.
3226 The C preprocessor is used to pull in #include sources, process #ifdef
3227 statements, #defines and so on.
3228 \layout Subsubsection
3230 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3234 This is retargettable assembler & linkage editor, it was developed by Alan
3236 John Hartman created the version for 8051, and I (Sandeep) have made some
3237 enhancements and bug fixes for it to work properly with the SDCC.
3238 \layout Subsubsection
3241 \begin_inset LatexCommand \index{s51 - The Simulator}
3248 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3249 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3254 The simulator is built as part of the build process.
3255 For more information visit Daniel's website at:
3256 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3261 It currently support the core mcs51, the Dallas DS80C390 and the Philips
3263 \layout Subsubsection
3265 sdcdb - Source Level Debugger
3271 <todo: is this thing still alive?>
3278 Sdcdb is the companion source level debugger.
3279 The current version of the debugger uses Daniel's Simulator S51, but can
3280 be easily changed to use other simulators.
3287 \layout Subsubsection
3289 Single Source File Projects
3292 For single source file 8051 projects the process is very simple.
3293 Compile your programs with the following command
3296 "sdcc sourcefile.c".
3300 This will compile, assemble and link your source file.
3301 Output files are as follows
3306 \begin_inset LatexCommand \index{.asm}
3310 - Assembler source file created by the compiler
3313 \begin_inset LatexCommand \index{.lst}
3317 - Assembler listing file created by the Assembler
3320 \begin_inset LatexCommand \index{.rst}
3324 - Assembler listing file updated with linkedit information, created by
3328 \begin_inset LatexCommand \index{.sym}
3332 - symbol listing for the sourcefile, created by the assembler
3335 \begin_inset LatexCommand \index{.rel}
3339 - Object file created by the assembler, input to Linkage editor
3342 \begin_inset LatexCommand \index{.map}
3346 - The memory map for the load module, created by the Linker
3349 \begin_inset LatexCommand \index{.mem}
3353 - A file with a summary of the memory ussage
3356 \begin_inset LatexCommand \index{.ihx}
3360 - The load module in Intel hex format (you can select the Motorola S19
3361 format with ---out-fmt-s19)
3364 \begin_inset LatexCommand \index{.adb}
3368 - An intermediate file containing debug information needed to create the
3369 .cdb file (with ---debug)
3372 \begin_inset LatexCommand \index{.cdb}
3376 - An optional file (with ---debug) containing debug information
3380 \begin_inset LatexCommand \index{. (no extension)}
3384 An optional AOMF51 file containing debug information (with ---debug)
3387 \begin_inset LatexCommand \index{.dump*}
3391 - Dump file to debug the compiler it self (with ---dumpall) (see section
3393 \begin_inset Quotes sld
3396 Anatomy of the compiler
3397 \begin_inset Quotes srd
3401 \layout Subsubsection
3403 Projects with Multiple Source Files
3406 SDCC can compile only ONE file at a time.
3407 Let us for example assume that you have a project containing the following
3412 foo1.c (contains some functions)
3414 foo2.c (contains some more functions)
3416 foomain.c (contains more functions and the function main)
3424 The first two files will need to be compiled separately with the commands:
3456 Then compile the source file containing the
3460 function and link the files together with the following command:
3468 foomain.c\SpecialChar ~
3469 foo1.rel\SpecialChar ~
3481 can be separately compiled as well:
3492 sdcc foomain.rel foo1.rel foo2.rel
3499 The file containing the
3514 file specified in the command line, since the linkage editor processes
3515 file in the order they are presented to it.
3516 \layout Subsubsection
3518 Projects with Additional Libraries
3519 \begin_inset LatexCommand \index{Libraries}
3526 Some reusable routines may be compiled into a library, see the documentation
3527 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
3531 \begin_inset LatexCommand \index{.lib}
3538 Libraries created in this manner can be included in the command line.
3539 Make sure you include the -L <library-path> option to tell the linker where
3540 to look for these files if they are not in the current directory.
3541 Here is an example, assuming you have the source file
3553 (if that is not the same as your current project):
3560 sdcc foomain.c foolib.lib -L mylib
3571 must be an absolute path name.
3575 The most efficient way to use libraries is to keep seperate modules in seperate
3577 The lib file now should name all the modules.rel files.
3578 For an example see the standard library file
3582 in the directory <installdir>/share/lib/small.
3585 Command Line Options
3586 \begin_inset LatexCommand \index{Command Line Options}
3591 \layout Subsubsection
3593 Processor Selection Options
3594 \begin_inset LatexCommand \index{Processor Selection Options}
3600 \labelwidthstring 00.00.0000
3605 \begin_inset LatexCommand \index{-mmcs51}
3611 Generate code for the MCS51 (8051) family of processors.
3612 This is the default processor target.
3614 \labelwidthstring 00.00.0000
3619 \begin_inset LatexCommand \index{-mds390}
3625 Generate code for the DS80C390 processor.
3627 \labelwidthstring 00.00.0000
3632 \begin_inset LatexCommand \index{-mz80}
3638 Generate code for the Z80 family of processors.
3640 \labelwidthstring 00.00.0000
3645 \begin_inset LatexCommand \index{-mgbz80}
3651 Generate code for the GameBoy Z80 processor.
3653 \labelwidthstring 00.00.0000
3658 \begin_inset LatexCommand \index{-mavr}
3664 Generate code for the Atmel AVR processor (In development, not complete).
3666 \labelwidthstring 00.00.0000
3671 \begin_inset LatexCommand \index{-mpic14}
3677 Generate code for the PIC 14-bit processors (In development, not complete).
3679 \labelwidthstring 00.00.0000
3685 Generate code for the Toshiba TLCS-900H processor (In development, not
3688 \labelwidthstring 00.00.0000
3693 \begin_inset LatexCommand \index{-mxa51}
3699 Generate code for the Philips XA51 processor (In development, not complete).
3700 \layout Subsubsection
3702 Preprocessor Options
3703 \begin_inset LatexCommand \index{Options Preprocessor}
3708 \begin_inset LatexCommand \index{Preprocessor Options}
3714 \labelwidthstring 00.00.0000
3719 \begin_inset LatexCommand \index{-I<path>}
3725 The additional location where the pre processor will look for <..h> or
3726 \begin_inset Quotes eld
3730 \begin_inset Quotes erd
3735 \labelwidthstring 00.00.0000
3740 \begin_inset LatexCommand \index{-D<macro[=value]>}
3746 Command line definition of macros.
3747 Passed to the pre processor.
3749 \labelwidthstring 00.00.0000
3754 \begin_inset LatexCommand \index{-M}
3760 Tell the preprocessor to output a rule suitable for make describing the
3761 dependencies of each object file.
3762 For each source file, the preprocessor outputs one make-rule whose target
3763 is the object file name for that source file and whose dependencies are
3764 all the files `#include'd in it.
3765 This rule may be a single line or may be continued with `
3767 '-newline if it is long.
3768 The list of rules is printed on standard output instead of the preprocessed
3772 \labelwidthstring 00.00.0000
3777 \begin_inset LatexCommand \index{-C}
3783 Tell the preprocessor not to discard comments.
3784 Used with the `-E' option.
3786 \labelwidthstring 00.00.0000
3791 \begin_inset LatexCommand \index{-MM}
3802 Like `-M' but the output mentions only the user header files included with
3804 \begin_inset Quotes eld
3808 System header files included with `#include <file>' are omitted.
3810 \labelwidthstring 00.00.0000
3815 \begin_inset LatexCommand \index{-Aquestion(answer)}
3821 Assert the answer answer for question, in case it is tested with a preprocessor
3822 conditional such as `#if #question(answer)'.
3823 `-A-' disables the standard assertions that normally describe the target
3826 \labelwidthstring 00.00.0000
3832 (answer) Assert the answer answer for question, in case it is tested with
3833 a preprocessor conditional such as `#if #question(answer)'.
3834 `-A-' disables the standard assertions that normally describe the target
3837 \labelwidthstring 00.00.0000
3842 \begin_inset LatexCommand \index{-Umacro}
3848 Undefine macro macro.
3849 `-U' options are evaluated after all `-D' options, but before any `-include'
3850 and `-imacros' options.
3852 \labelwidthstring 00.00.0000
3857 \begin_inset LatexCommand \index{-dM}
3863 Tell the preprocessor to output only a list of the macro definitions that
3864 are in effect at the end of preprocessing.
3865 Used with the `-E' option.
3867 \labelwidthstring 00.00.0000
3872 \begin_inset LatexCommand \index{-dD}
3878 Tell the preprocessor to pass all macro definitions into the output, in
3879 their proper sequence in the rest of the output.
3881 \labelwidthstring 00.00.0000
3886 \begin_inset LatexCommand \index{-dN}
3897 Like `-dD' except that the macro arguments and contents are omitted.
3898 Only `#define name' is included in the output.
3899 \layout Subsubsection
3902 \begin_inset LatexCommand \index{Options Linker}
3907 \begin_inset LatexCommand \index{Linker Options}
3913 \labelwidthstring 00.00.0000
3919 \begin_inset LatexCommand \index{-L ---lib-path}
3928 <absolute path to additional libraries> This option is passed to the linkage
3929 editor's additional libraries search path.
3930 The path name must be absolute.
3931 Additional library files may be specified in the command line.
3932 See section Compiling programs for more details.
3934 \labelwidthstring 00.00.0000
3941 \begin_inset LatexCommand \index{---xram-loc}
3945 <Value> The start location of the external ram, default value is 0.
3946 The value entered can be in Hexadecimal or Decimal format, e.g.: ---xram-loc
3947 0x8000 or ---xram-loc 32768.
3949 \labelwidthstring 00.00.0000
3956 \begin_inset LatexCommand \index{---code-loc}
3960 <Value> The start location of the code segment, default value 0.
3961 Note when this option is used the interrupt vector table is also relocated
3962 to the given address.
3963 The value entered can be in Hexadecimal or Decimal format, e.g.: ---code-loc
3964 0x8000 or ---code-loc 32768.
3966 \labelwidthstring 00.00.0000
3973 \begin_inset LatexCommand \index{---stack-loc}
3977 <Value> By default the stack is placed after the data segment.
3978 Using this option the stack can be placed anywhere in the internal memory
3980 The value entered can be in Hexadecimal or Decimal format, e.g.
3981 ---stack-loc 0x20 or ---stack-loc 32.
3982 Since the sp register is incremented before a push or call, the initial
3983 sp will be set to one byte prior the provided value.
3984 The provided value should not overlap any other memory areas such as used
3985 register banks or the data segment and with enough space for the current
3988 \labelwidthstring 00.00.0000
3995 \begin_inset LatexCommand \index{---data-loc}
3999 <Value> The start location of the internal ram data segment.
4000 The value entered can be in Hexadecimal or Decimal format, eg.
4001 ---data-loc 0x20 or ---data-loc 32.
4002 (By default, the start location of the internal ram data segment is set
4003 as low as possible in memory, taking into account the used register banks
4004 and the bit segment at address 0x20.
4005 For example if register banks 0 and 1 are used without bit variables, the
4006 data segment will be set, if ---data-loc is not used, to location 0x10.)
4008 \labelwidthstring 00.00.0000
4015 \begin_inset LatexCommand \index{---idata-loc}
4019 <Value> The start location of the indirectly addressable internal ram, default
4021 The value entered can be in Hexadecimal or Decimal format, eg.
4022 ---idata-loc 0x88 or ---idata-loc 136.
4024 \labelwidthstring 00.00.0000
4029 \begin_inset LatexCommand \index{---out-fmt-ihx}
4038 The linker output (final object code) is in Intel Hex format.
4039 (This is the default option).
4041 \labelwidthstring 00.00.0000
4046 \begin_inset LatexCommand \index{---out-fmt-s19}
4055 The linker output (final object code) is in Motorola S19 format.
4056 \layout Subsubsection
4059 \begin_inset LatexCommand \index{Options MCS51}
4064 \begin_inset LatexCommand \index{MCS51 Options}
4070 \labelwidthstring 00.00.0000
4075 \begin_inset LatexCommand \index{---model-large}
4081 Generate code for Large model programs see section Memory Models for more
4083 If this option is used all source files in the project should be compiled
4085 In addition the standard library routines are compiled with small model,
4086 they will need to be recompiled.
4088 \labelwidthstring 00.00.0000
4093 \begin_inset LatexCommand \index{---model-small}
4104 Generate code for Small Model programs see section Memory Models for more
4106 This is the default model.
4107 \layout Subsubsection
4110 \begin_inset LatexCommand \index{Options DS390}
4115 \begin_inset LatexCommand \index{DS390 Options}
4121 \labelwidthstring 00.00.0000
4128 \begin_inset LatexCommand \index{---model-flat24}
4138 Generate 24-bit flat mode code.
4139 This is the one and only that the ds390 code generator supports right now
4140 and is default when using
4145 See section Memory Models for more details.
4147 \labelwidthstring 00.00.0000
4154 \begin_inset LatexCommand \index{--stack-10bit}
4158 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
4159 This is the one and only that the ds390 code generator supports right now
4160 and is default when using
4165 In this mode, the stack is located in the lower 1K of the internal RAM,
4166 which is mapped to 0x400000.
4167 Note that the support is incomplete, since it still uses a single byte
4168 as the stack pointer.
4169 This means that only the lower 256 bytes of the potential 1K stack space
4170 will actually be used.
4171 However, this does allow you to reclaim the precious 256 bytes of low RAM
4172 for use for the DATA and IDATA segments.
4173 The compiler will not generate any code to put the processor into 10 bit
4175 It is important to ensure that the processor is in this mode before calling
4176 any re-entrant functions compiled with this option.
4177 In principle, this should work with the
4181 option, but that has not been tested.
4182 It is incompatible with the
4187 It also only makes sense if the processor is in 24 bit contiguous addressing
4190 ---model-flat24 option
4193 \layout Subsubsection
4195 Optimization Options
4196 \begin_inset LatexCommand \index{Options Optimization}
4201 \begin_inset LatexCommand \index{Optimization Options}
4207 \labelwidthstring 00.00.0000
4212 \begin_inset LatexCommand \index{---nogcse}
4218 Will not do global subexpression elimination, this option may be used when
4219 the compiler creates undesirably large stack/data spaces to store compiler
4221 A warning message will be generated when this happens and the compiler
4222 will indicate the number of extra bytes it allocated.
4223 It recommended that this option NOT be used, #pragma\SpecialChar ~
4225 to turn off global subexpression elimination for a given function only.
4227 \labelwidthstring 00.00.0000
4232 \begin_inset LatexCommand \index{---noinvariant}
4238 Will not do loop invariant optimizations, this may be turned off for reasons
4239 explained for the previous option.
4240 For more details of loop optimizations performed see section Loop Invariants.It
4241 recommended that this option NOT be used, #pragma\SpecialChar ~
4242 NOINVARIANT can be used
4243 to turn off invariant optimizations for a given function only.
4245 \labelwidthstring 00.00.0000
4250 \begin_inset LatexCommand \index{---noinduction}
4256 Will not do loop induction optimizations, see section strength reduction
4257 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
4259 ION can be used to turn off induction optimizations for a given function
4262 \labelwidthstring 00.00.0000
4267 \begin_inset LatexCommand \index{---nojtbound}
4278 Will not generate boundary condition check when switch statements are implement
4279 ed using jump-tables.
4280 See section Switch Statements for more details.
4281 It is recommended that this option is NOT used, #pragma\SpecialChar ~
4283 used to turn off boundary checking for jump tables for a given function
4286 \labelwidthstring 00.00.0000
4291 \begin_inset LatexCommand \index{---noloopreverse}
4300 Will not do loop reversal optimization.
4302 \labelwidthstring 00.00.0000
4309 \begin_inset LatexCommand \index{---nolabelopt }
4313 Will not optimize labels (makes the dumpfiles more readable).
4315 \labelwidthstring 00.00.0000
4320 \begin_inset LatexCommand \index{---no-xinit-opt}
4326 Will not memcpy initialized data in far space from code space.
4327 This saves a few bytes in code space if you don't have initialized data.
4328 \layout Subsubsection
4331 \begin_inset LatexCommand \index{Options other}
4336 \begin_inset LatexCommand \index{Other Options}
4342 \labelwidthstring 00.00.0000
4348 \begin_inset LatexCommand \index{---compile-only}
4353 \begin_inset LatexCommand \index{-c ---compile-only}
4359 will compile and assemble the source, but will not call the linkage editor.
4361 \labelwidthstring 00.00.0000
4366 \begin_inset LatexCommand \index{--c1mode}
4372 reads the preprocessed source from standard input and compiles it.
4373 The file name for the assembler output must be specified using the -o option.
4375 \labelwidthstring 00.00.0000
4380 \begin_inset LatexCommand \index{-E}
4386 Run only the C preprocessor.
4387 Preprocess all the C source files specified and output the results to standard
4390 \labelwidthstring 00.00.0000
4396 \begin_inset LatexCommand \index{-o <path/file>}
4402 The output path resp.
4403 file where everything will be placed.
4404 If the parameter is a path, it must have a trailing slash (or backslash
4405 for the Windows binaries) to be recognized as a path.
4408 \labelwidthstring 00.00.0000
4413 \begin_inset LatexCommand \index{---stack-auto}
4424 All functions in the source file will be compiled as
4429 the parameters and local variables will be allocated on the stack.
4430 see section Parameters and Local Variables for more details.
4431 If this option is used all source files in the project should be compiled
4435 \labelwidthstring 00.00.0000
4440 \begin_inset LatexCommand \index{---xstack}
4446 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
4447 variables and passing parameters.
4448 See section on external stack for more details.
4450 \labelwidthstring 00.00.0000
4455 \begin_inset LatexCommand \index{---callee-saves}
4459 function1[,function2][,function3]....
4462 The compiler by default uses a caller saves convention for register saving
4463 across function calls, however this can cause unneccessary register pushing
4464 & popping when calling small functions from larger functions.
4465 This option can be used to switch the register saving convention for the
4466 function names specified.
4467 The compiler will not save registers when calling these functions, no extra
4468 code will be generated at the entry & exit for these functions to save
4469 & restore the registers used by these functions, this can SUBSTANTIALLY
4470 reduce code & improve run time performance of the generated code.
4471 In the future the compiler (with interprocedural analysis) will be able
4472 to determine the appropriate scheme to use for each function call.
4473 DO NOT use this option for built-in functions such as _muluint..., if this
4474 option is used for a library function the appropriate library function
4475 needs to be recompiled with the same option.
4476 If the project consists of multiple source files then all the source file
4477 should be compiled with the same ---callee-saves option string.
4478 Also see #pragma\SpecialChar ~
4480 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
4486 \labelwidthstring 00.00.0000
4491 \begin_inset LatexCommand \index{---debug}
4500 When this option is used the compiler will generate debug information, that
4501 can be used with the SDCDB.
4502 The debug information is collected in a file with .cdb extension.
4503 For more information see documentation for SDCDB.
4505 \labelwidthstring 00.00.0000
4512 \begin_inset LatexCommand \index{---peep-file}
4516 <filename> This option can be used to use additional rules to be used by
4517 the peep hole optimizer.
4518 See section Peep Hole optimizations for details on how to write these rules.
4520 \labelwidthstring 00.00.0000
4525 \begin_inset LatexCommand \index{-S}
4536 Stop after the stage of compilation proper; do not assemble.
4537 The output is an assembler code file for the input file specified.
4539 \labelwidthstring 00.00.0000
4543 -Wa_asmOption[,asmOption]
4546 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
4551 Pass the asmOption to the assembler.
4553 \labelwidthstring 00.00.0000
4557 -Wl_linkOption[,linkOption]
4560 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
4565 Pass the linkOption to the linker.
4567 \labelwidthstring 00.00.0000
4572 \begin_inset LatexCommand \index{---int-long-reent}
4578 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
4579 Note by default these libraries are compiled as non-reentrant.
4580 See section Installation for more details.
4582 \labelwidthstring 00.00.0000
4587 \begin_inset LatexCommand \index{---cyclomatic}
4596 This option will cause the compiler to generate an information message for
4597 each function in the source file.
4598 The message contains some
4602 information about the function.
4603 The number of edges and nodes the compiler detected in the control flow
4604 graph of the function, and most importantly the
4606 cyclomatic complexity
4607 \begin_inset LatexCommand \index{cyclomatic complexity}
4613 see section on Cyclomatic Complexity for more details.
4615 \labelwidthstring 00.00.0000
4620 \begin_inset LatexCommand \index{---float-reent}
4629 Floating point library is compiled as reentrant
4630 \begin_inset LatexCommand \index{ reentrant}
4634 .See section Installation for more details.
4636 \labelwidthstring 00.00.0000
4641 \begin_inset LatexCommand \index{---nooverlay}
4647 The compiler will not overlay parameters and local variables of any function,
4648 see section Parameters and local variables for more details.
4650 \labelwidthstring 00.00.0000
4655 \begin_inset LatexCommand \index{---main-return}
4661 This option can be used when the code generated is called by a monitor
4663 The compiler will generate a 'ret' upon return from the 'main'
4664 \begin_inset LatexCommand \index{main return}
4669 The default option is to lock up i.e.
4672 \labelwidthstring 00.00.0000
4677 \begin_inset LatexCommand \index{---no-peep}
4683 Disable peep-hole optimization.
4685 \labelwidthstring 00.00.0000
4690 \begin_inset LatexCommand \index{---peep-asm}
4696 Pass the inline assembler code through the peep hole optimizer.
4697 This can cause unexpected changes to inline assembler code, please go through
4698 the peephole optimizer
4699 \begin_inset LatexCommand \index{Peephole optimizer}
4703 rules defined in the source file tree '<target>/peeph.def' before using
4706 \labelwidthstring 00.00.0000
4713 \begin_inset LatexCommand \index{---iram-size<Value>}
4717 Causes the linker to check if the internal ram usage is within limits of
4720 \labelwidthstring 00.00.0000
4727 \begin_inset LatexCommand \index{---xram-size<Value>}
4731 Causes the linker to check if the external ram usage is within limits of
4734 \labelwidthstring 00.00.0000
4741 \begin_inset LatexCommand \index{---code-size<Value>}
4745 Causes the linker to check if the code usage is within limits of the given
4748 \labelwidthstring 00.00.0000
4753 \begin_inset LatexCommand \index{---nostdincl}
4759 This will prevent the compiler from passing on the default include path
4760 to the preprocessor.
4762 \labelwidthstring 00.00.0000
4767 \begin_inset LatexCommand \index{---nostdlib}
4773 This will prevent the compiler from passing on the default library path
4776 \labelwidthstring 00.00.0000
4781 \begin_inset LatexCommand \index{---verbose}
4787 Shows the various actions the compiler is performing.
4789 \labelwidthstring 00.00.0000
4794 \begin_inset LatexCommand \index{-V}
4800 Shows the actual commands the compiler is executing.
4802 \labelwidthstring 00.00.0000
4807 \begin_inset LatexCommand \index{---no-c-code-in-asm}
4813 Hides your ugly and inefficient c-code from the asm file, so you can always
4814 blame the compiler :).
4816 \labelwidthstring 00.00.0000
4821 \begin_inset LatexCommand \index{---i-code-in-asm}
4827 Include i-codes in the asm file.
4828 Sounds like noise but is most helpfull for debugging the compiler itself.
4830 \labelwidthstring 00.00.0000
4835 \begin_inset LatexCommand \index{---less-pedantic}
4841 Disable some of the more pedantic warnings (jwk burps: please be more specific
4844 \labelwidthstring 00.00.0000
4848 ---print-search-dirs
4849 \begin_inset LatexCommand \index{---print-search-dirs}
4855 Display the directories in the compiler's search path
4856 \layout Subsubsection
4858 Intermediate Dump Options
4859 \begin_inset LatexCommand \index{Options Intermediate Dump}
4864 \begin_inset LatexCommand \index{Intermediate Dump Options}
4871 The following options are provided for the purpose of retargetting and debugging
4873 These provided a means to dump the intermediate code (iCode
4874 \begin_inset LatexCommand \index{iCode}
4878 ) generated by the compiler in human readable form at various stages of
4879 the compilation process.
4882 \labelwidthstring 00.00.0000
4887 \begin_inset LatexCommand \index{---dumpraw}
4893 This option will cause the compiler to dump the intermediate code into
4896 <source filename>.dumpraw
4898 just after the intermediate code has been generated for a function, i.e.
4899 before any optimizations are done.
4901 \begin_inset LatexCommand \index{basic blocks}
4905 at this stage ordered in the depth first number, so they may not be in
4906 sequence of execution.
4908 \labelwidthstring 00.00.0000
4913 \begin_inset LatexCommand \index{---dumpgcse}
4919 Will create a dump of iCode's, after global subexpression elimination
4920 \begin_inset LatexCommand \index{global subexpression elimination}
4926 <source filename>.dumpgcse.
4928 \labelwidthstring 00.00.0000
4933 \begin_inset LatexCommand \index{---dumpdeadcode}
4939 Will create a dump of iCode's, after deadcode elimination
4940 \begin_inset LatexCommand \index{deadcode elimination}
4946 <source filename>.dumpdeadcode.
4948 \labelwidthstring 00.00.0000
4953 \begin_inset LatexCommand \index{---dumploop}
4962 Will create a dump of iCode's, after loop optimizations
4963 \begin_inset LatexCommand \index{loop optimizations}
4969 <source filename>.dumploop.
4971 \labelwidthstring 00.00.0000
4976 \begin_inset LatexCommand \index{---dumprange}
4985 Will create a dump of iCode's, after live range analysis
4986 \begin_inset LatexCommand \index{live range analysis}
4992 <source filename>.dumprange.
4994 \labelwidthstring 00.00.0000
4999 \begin_inset LatexCommand \index{---dumlrange}
5005 Will dump the life ranges
5006 \begin_inset LatexCommand \index{life ranges}
5012 \labelwidthstring 00.00.0000
5017 \begin_inset LatexCommand \index{---dumpregassign}
5026 Will create a dump of iCode's, after register assignment
5027 \begin_inset LatexCommand \index{register assignment}
5033 <source filename>.dumprassgn.
5035 \labelwidthstring 00.00.0000
5040 \begin_inset LatexCommand \index{---dumplrange}
5046 Will create a dump of the live ranges of iTemp's
5048 \labelwidthstring 00.00.0000
5053 \begin_inset LatexCommand \index{---dumpall}
5064 Will cause all the above mentioned dumps to be created.
5067 Environment variables
5068 \begin_inset LatexCommand \index{Environment variables}
5075 SDCC recognizes the following environment variables:
5077 \labelwidthstring 00.00.0000
5082 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
5088 SDCC installs a signal handler
5089 \begin_inset LatexCommand \index{ signal handler}
5093 to be able to delete temporary files after an user break (^C) or an exception.
5094 If this environment variable is set, SDCC won't install the signal handler
5095 in order to be able to debug SDCC.
5097 \labelwidthstring 00.00.0000
5102 \begin_inset LatexCommand \index{TMP}
5108 \begin_inset LatexCommand \index{TEMP}
5114 \begin_inset LatexCommand \index{TMPDIR}
5120 Path, where temporary files will be created.
5121 The order of the variables is the search order.
5122 In a standard *nix environment these variables are not set, and there's
5123 no need to set them.
5124 On Windows it's recommended to set one of them.
5126 \labelwidthstring 00.00.0000
5131 \begin_inset LatexCommand \index{SDCC\_HOME}
5138 \begin_inset Quotes sld
5141 2.3 Install and search paths
5142 \begin_inset Quotes srd
5147 \labelwidthstring 00.00.0000
5152 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
5159 \begin_inset Quotes sld
5162 2.3 Install and search paths
5163 \begin_inset Quotes srd
5168 \labelwidthstring 00.00.0000
5173 \begin_inset LatexCommand \index{SDCC\_LIB}
5180 \begin_inset Quotes sld
5183 2.3 Install and search paths
5184 \begin_inset Quotes srd
5190 There are some more environment variables recognized by SDCC, but these
5191 are solely used for debugging purposes.
5192 They can change or disappear very quickly, and will never be documentated.
5195 MCS51/DS390 Storage Class
5196 \begin_inset LatexCommand \index{Storage Class}
5203 In addition to the ANSI storage classes SDCC allows the following MCS51
5204 specific storage classes.
5205 \layout Subsubsection
5208 \begin_inset LatexCommand \index{xdata}
5215 Variables declared with this storage class will be placed in the extern
5221 storage class for Large Memory model, e.g.:
5227 xdata unsigned char xduc;
5228 \layout Subsubsection
5231 \begin_inset LatexCommand \index{data}
5242 storage class for Small Memory model.
5243 Variables declared with this storage class will be allocated in the internal
5251 \layout Subsubsection
5254 \begin_inset LatexCommand \index{idata}
5261 Variables declared with this storage class will be allocated into the indirectly
5262 addressable portion of the internal ram of a 8051, e.g.:
5269 \layout Subsubsection
5272 \begin_inset LatexCommand \index{bit}
5279 This is a data-type and a storage class specifier.
5280 When a variable is declared as a bit, it is allocated into the bit addressable
5281 memory of 8051, e.g.:
5288 \layout Subsubsection
5291 \begin_inset LatexCommand \index{sfr}
5296 \begin_inset LatexCommand \index{sbit}
5303 Like the bit keyword,
5307 signifies both a data-type and storage class, they are used to describe
5308 the special function registers and special bit variables of a 8051, eg:
5314 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
5316 sbit at 0xd7 CY; /* CY (Carry Flag
5317 \begin_inset LatexCommand \index{Carry flag}
5325 \begin_inset LatexCommand \index{Pointers}
5332 SDCC allows (via language extensions) pointers to explicitly point to any
5333 of the memory spaces
5334 \begin_inset LatexCommand \index{memory spaces}
5339 In addition to the explicit pointers, the compiler uses (by default) generic
5340 pointers which can be used to point to any of the memory spaces.
5344 Pointer declaration examples:
5353 /* pointer physically in xternal ram pointing to object in internal ram
5356 data unsigned char * xdata p;
5360 /* pointer physically in code rom pointing to data in xdata space */
5362 xdata unsigned char * code p;
5366 /* pointer physically in code space pointing to data in code space */
5368 code unsigned char * code p;
5372 /* the folowing is a generic pointer physically located in xdata space */
5383 Well you get the idea.
5388 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
5401 The highest order byte of the
5405 pointers contains the data space information.
5406 Assembler support routines are called whenever data is stored or retrieved
5412 These are useful for developing reusable library routines.
5413 Explicitly specifying the pointer type will generate the most efficient
5418 \begin_inset LatexCommand \index{Parameters}
5423 \begin_inset LatexCommand \index{Local Variables}
5430 Automatic (local) variables and parameters to functions can either be placed
5431 on the stack or in data-space.
5432 The default action of the compiler is to place these variables in the internal
5433 RAM (for small model) or external RAM (for large model).
5434 This in fact makes them
5437 \begin_inset LatexCommand \index{static}
5443 so by default functions are non-reentrant
5444 \begin_inset LatexCommand \index{non-reentrant}
5452 They can be placed on the stack
5453 \begin_inset LatexCommand \index{stack}
5460 \begin_inset LatexCommand \index{--stack-auto}
5466 option or by using the
5469 \begin_inset LatexCommand \index{reentrant}
5475 keyword in the function declaration, e.g.:
5484 unsigned char foo(char i) reentrant
5497 Since stack space on 8051 is limited, the
5505 option should be used sparingly.
5506 Note that the reentrant keyword just means that the parameters & local
5507 variables will be allocated to the stack, it
5511 mean that the function is register bank independent.
5515 Local variables can be assigned storage classes and absolute
5516 \begin_inset LatexCommand \index{absolute}
5526 unsigned char foo() {
5532 xdata unsigned char i;
5544 data at 0x31 unsiged char j;
5559 In the above example the variable
5563 will be allocated in the external ram,
5567 in bit addressable space and
5576 or when a function is declared as
5580 this should only be done for static variables.
5583 Parameters however are not allowed any storage class, (storage classes for
5584 parameters will be ignored), their allocation is governed by the memory
5585 model in use, and the reentrancy options.
5589 \begin_inset LatexCommand \index{Overlaying}
5596 For non-reentrant functions SDCC will try to reduce internal ram space usage
5597 by overlaying parameters and local variables of a function (if possible).
5598 Parameters and local variables of a function will be allocated to an overlayabl
5599 e segment if the function has
5601 no other function calls and the function is non-reentrant and the memory
5603 \begin_inset LatexCommand \index{memory model}
5610 If an explicit storage class is specified for a local variable, it will
5614 Note that the compiler (not the linkage editor) makes the decision for overlayin
5616 Functions that are called from an interrupt service routine should be preceded
5617 by a #pragma\SpecialChar ~
5619 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
5623 if they are not reentrant.
5626 Also note that the compiler does not do any processing of inline
5627 \begin_inset LatexCommand \index{inline}
5631 assembler code, so the compiler might incorrectly assign local variables
5632 and parameters of a function into the overlay segment if the inline assembler
5633 code calls other c-functions that might use the overlay.
5634 In that case the #pragma\SpecialChar ~
5635 NOOVERLAY should be used.
5638 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
5640 \begin_inset LatexCommand \index{multiplication}
5645 \begin_inset LatexCommand \index{division}
5649 will NOT be overlayed since these are implemented using external functions,
5659 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
5665 void set_error(unsigned char errcd)
5681 void some_isr () interrupt
5682 \begin_inset LatexCommand \index{interrupt}
5687 \begin_inset LatexCommand \index{using}
5720 In the above example the parameter
5728 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
5730 not present, this could cause unpredictable runtime behavior when called
5732 The #pragma\SpecialChar ~
5733 NOOVERLAY ensures that the parameters and local variables for
5734 the function are NOT overlayed.
5737 Interrupt Service Routines
5738 \begin_inset LatexCommand \index{Interrupt Service Routines}
5745 SDCC allows interrupt service routines to be coded in C, with some extended
5752 void timer_isr (void) interrupt 2 using 1
5765 The number following the
5768 \begin_inset LatexCommand \index{interrupt}
5774 keyword is the interrupt number this routine will service.
5775 The compiler will insert a call to this routine in the interrupt vector
5776 table for the interrupt number specified.
5781 keyword is used to tell the compiler to use the specified register bank
5782 (8051 specific) when generating code for this function.
5783 Note that when some function is called from an interrupt service routine
5784 it should be preceded by a #pragma\SpecialChar ~
5786 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
5790 if it is not reentrant.
5791 A special note here, int (16 bit) and long (32 bit) integer division, multiplic
5792 ation & modulus operations are implemented using external support routines
5793 developed in ANSI-C, if an interrupt service routine needs to do any of
5794 these operations then the support routines (as mentioned in a following
5795 section) will have to be recompiled using the
5798 \begin_inset LatexCommand \index{---stack-auto}
5804 option and the source file will need to be compiled using the
5809 \begin_inset LatexCommand \index{---int-long-rent}
5816 If you have multiple source files in your project, interrupt service routines
5817 can be present in any of them, but a prototype of the isr MUST be present
5818 or included in the file that contains the function
5825 Interrupt Numbers and the corresponding address & descriptions for the Standard
5826 8051 are listed below.
5827 SDCC will automatically adjust the interrupt vector table
5828 \begin_inset LatexCommand \index{ interrupt vector table}
5832 to the maximum interrupt number specified.
5838 \begin_inset Tabular
5839 <lyxtabular version="3" rows="6" columns="3">
5841 <column alignment="center" valignment="top" leftline="true" width="0in">
5842 <column alignment="center" valignment="top" leftline="true" width="0in">
5843 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
5844 <row topline="true" bottomline="true">
5845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5853 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5861 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
5870 <row topline="true">
5871 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5879 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5887 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
5896 <row topline="true">
5897 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5913 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
5922 <row topline="true">
5923 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5939 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
5948 <row topline="true">
5949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5965 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
5974 <row topline="true" bottomline="true">
5975 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5983 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
5991 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
6008 If the interrupt service routine is defined without
6011 \begin_inset LatexCommand \index{using}
6017 a register bank or with register bank 0 (using 0), the compiler will save
6018 the registers used by itself on the stack upon entry and restore them at
6019 exit, however if such an interrupt service routine calls another function
6020 then the entire register bank will be saved on the stack.
6021 This scheme may be advantageous for small interrupt service routines which
6022 have low register usage.
6025 If the interrupt service routine is defined to be using a specific register
6030 are save and restored, if such an interrupt service routine calls another
6031 function (using another register bank) then the entire register bank of
6032 the called function will be saved on the stack.
6033 This scheme is recommended for larger interrupt service routines.
6036 Calling other functions from an interrupt service routine is not recommended,
6037 avoid it if possible.
6041 Also see the _naked modifier.
6049 <TODO: this isn't implemented at all!>
6055 A special keyword may be associated with a function declaring it as
6060 SDCC will generate code to disable all interrupts upon entry to a critical
6061 function and enable them back before returning.
6062 Note that nesting critical functions may cause unpredictable results.
6087 The critical attribute maybe used with other attributes like
6093 \begin_inset LatexCommand \index{Naked Functions}
6100 A special keyword may be associated with a function declaring it as
6103 \begin_inset LatexCommand \index{\_naked}
6114 function modifier attribute prevents the compiler from generating prologue
6115 \begin_inset LatexCommand \index{function prologue}
6120 \begin_inset LatexCommand \index{function epilogue}
6124 code for that function.
6125 This means that the user is entirely responsible for such things as saving
6126 any registers that may need to be preserved, selecting the proper register
6127 bank, generating the
6131 instruction at the end, etc.
6132 Practically, this means that the contents of the function must be written
6133 in inline assembler.
6134 This is particularly useful for interrupt functions, which can have a large
6135 (and often unnecessary) prologue/epilogue.
6136 For example, compare the code generated by these two functions:
6142 data unsigned char counter;
6144 void simpleInterrupt(void) interrupt
6145 \begin_inset LatexCommand \index{interrupt}
6163 void nakedInterrupt(void) interrupt 2 _naked
6196 ; MUST explicitly include ret in _naked function.
6210 For an 8051 target, the generated simpleInterrupt looks like:
6355 whereas nakedInterrupt looks like:
6380 ; MUST explicitly include ret(i) in _naked function.
6386 While there is nothing preventing you from writing C code inside a _naked
6387 function, there are many ways to shoot yourself in the foot doing this,
6388 and it is recommended that you stick to inline assembler.
6391 Functions using private banks
6392 \begin_inset LatexCommand \index{banks}
6402 \begin_inset LatexCommand \index{using}
6408 attribute (which tells the compiler to use a register bank other than the
6409 default bank zero) should only be applied to
6412 \begin_inset LatexCommand \index{interrupt}
6418 functions (see note 1 below).
6419 This will in most circumstances make the generated ISR code more efficient
6420 since it will not have to save registers on the stack.
6427 attribute will have no effect on the generated code for a
6431 function (but may occasionally be useful anyway
6437 possible exception: if a function is called ONLY from 'interrupt' functions
6438 using a particular bank, it can be declared with the same 'using' attribute
6439 as the calling 'interrupt' functions.
6440 For instance, if you have several ISRs using bank one, and all of them
6441 call memcpy(), it might make sense to create a specialized version of memcpy()
6442 'using 1', since this would prevent the ISR from having to save bank zero
6443 to the stack on entry and switch to bank zero before calling the function
6450 (pending: I don't think this has been done yet)
6457 function using a non-zero bank will assume that it can trash that register
6458 bank, and will not save it.
6459 Since high-priority interrupts
6460 \begin_inset LatexCommand \index{interrupt priority}
6464 can interrupt low-priority ones on the 8051 and friends, this means that
6465 if a high-priority ISR
6469 a particular bank occurs while processing a low-priority ISR
6473 the same bank, terrible and bad things can happen.
6474 To prevent this, no single register bank should be
6478 by both a high priority and a low priority ISR.
6479 This is probably most easily done by having all high priority ISRs use
6480 one bank and all low priority ISRs use another.
6481 If you have an ISR which can change priority at runtime, you're on your
6482 own: I suggest using the default bank zero and taking the small performance
6486 It is most efficient if your ISR calls no other functions.
6487 If your ISR must call other functions, it is most efficient if those functions
6488 use the same bank as the ISR (see note 1 below); the next best is if the
6489 called functions use bank zero.
6490 It is very inefficient to call a function using a different, non-zero bank
6496 \begin_inset LatexCommand \index{Absolute Addressing}
6503 Data items can be assigned an absolute address with the
6506 \begin_inset LatexCommand \index{at}
6512 keyword, in addition to a storage class, e.g.:
6519 \begin_inset LatexCommand \index{xdata}
6524 \begin_inset LatexCommand \index{at}
6528 0x8000 unsigned char PORTA_8255 ;
6534 In the above example the PORTA_8255 will be allocated to the location 0x8000
6535 of the external ram.
6536 Note that this feature is provided to give the programmer access to
6540 devices attached to the controller.
6541 The compiler does not actually reserve any space for variables declared
6542 in this way (they are implemented with an equate in the assembler).
6543 Thus it is left to the programmer to make sure there are no overlaps with
6544 other variables that are declared without the absolute address.
6545 The assembler listing file (.lst
6546 \begin_inset LatexCommand \index{.lst}
6550 ) and the linker output files (.rst
6551 \begin_inset LatexCommand \index{.rst}
6556 \begin_inset LatexCommand \index{.map}
6560 ) are a good places to look for such overlaps.
6564 Absolute address can be specified for variables in all storage classes,
6572 \begin_inset LatexCommand \index{bit}
6577 \begin_inset LatexCommand \index{at}
6587 The above example will allocate the variable at offset 0x02 in the bit-addressab
6589 There is no real advantage to assigning absolute addresses to variables
6590 in this manner, unless you want strict control over all the variables allocated.
6594 \begin_inset LatexCommand \index{Startup Code}
6601 The compiler inserts a call to the C routine
6603 _sdcc_external_startup()
6604 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
6613 at the start of the CODE area.
6614 This routine is in the runtime library.
6615 By default this routine returns 0, if this routine returns a non-zero value,
6616 the static & global variable initialization will be skipped and the function
6617 main will be invoked Other wise static & global variables will be initialized
6618 before the function main is invoked.
6621 _sdcc_external_startup()
6623 routine to your program to override the default if you need to setup hardware
6624 or perform some other critical operation prior to static & global variable
6628 Inline Assembler Code
6629 \begin_inset LatexCommand \index{Inline Assembler Code}
6636 SDCC allows the use of in-line assembler with a few restriction as regards
6638 All labels defined within inline assembler code
6646 where nnnn is a number less than 100 (which implies a limit of utmost 100
6647 inline assembler labels
6655 It is strongly recommended that each assembly instruction (including labels)
6656 be placed in a separate line (as the example shows).
6660 \begin_inset LatexCommand \index{--peep-asm}
6666 command line option is used, the inline assembler code will be passed through
6667 the peephole optimizer
6668 \begin_inset LatexCommand \index{Peephole optimizer}
6673 This might cause some unexpected changes in the inline assembler code.
6674 Please go throught the peephole optimizer rules defined in file
6678 carefully before using this option.
6685 \begin_inset LatexCommand \index{\_asm}
6715 \begin_inset LatexCommand \index{\_endasm}
6728 The inline assembler code can contain any valid code understood by the assembler
6729 , this includes any assembler directives and comment lines.
6730 The compiler does not do any validation of the code within the
6740 Inline assembler code cannot reference any C-Labels, however it can reference
6742 \begin_inset LatexCommand \index{Labels}
6746 defined by the inline assembler, e.g.:
6772 ; some assembler code
6792 /* some more c code */
6794 clabel:\SpecialChar ~
6796 /* inline assembler cannot reference this label */
6808 $0003: ;label (can be reference by inline assembler only)
6820 /* some more c code */
6828 In other words inline assembly code can access labels defined in inline
6829 assembly within the scope of the funtion.
6833 The same goes the other way, ie.
6834 labels defines in inline assembly CANNOT be accessed by C statements.
6838 \begin_inset LatexCommand \index{int (16 bit)}
6843 \begin_inset LatexCommand \index{long (32 bit)}
6850 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
6851 multiplication and modulus operations are implemented by support routines.
6852 These support routines are all developed in ANSI-C to facilitate porting
6853 to other MCUs, although some model specific assembler optimations are used.
6854 The following files contain the described routine, all of them can be found
6855 in <installdir>/share/sdcc/lib.
6861 <pending: tabularise this>
6867 _mulsint.c - signed 16 bit multiplication (calls _muluint)
6869 _muluint.c - unsigned 16 bit multiplication
6871 _divsint.c - signed 16 bit division (calls _divuint)
6873 _divuint.c - unsigned 16 bit division
6875 _modsint.c - signed 16 bit modulus (call _moduint)
6877 _moduint.c - unsigned 16 bit modulus
6879 _mulslong.c - signed 32 bit multiplication (calls _mululong)
6881 _mululong.c - unsigned32 bit multiplication
6883 _divslong.c - signed 32 division (calls _divulong)
6885 _divulong.c - unsigned 32 division
6887 _modslong.c - signed 32 bit modulus (calls _modulong)
6889 _modulong.c - unsigned 32 bit modulus
6897 Since they are compiled as
6902 \begin_inset LatexCommand \index{reentrant}
6907 \begin_inset LatexCommand \index{non-reentrant}
6912 \begin_inset LatexCommand \index{interrupt}
6916 service routines should not do any of the above operations.
6917 If this is unavoidable then the above routines will need to be compiled
6921 \begin_inset LatexCommand \index{---stack-auto}
6927 option, after which the source program will have to be compiled with
6930 \begin_inset LatexCommand \index{---int-long-rent}
6939 Floating Point Support
6940 \begin_inset LatexCommand \index{Floating Point Support}
6947 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
6948 point support routines are derived from gcc's floatlib.c and consists of
6949 the following routines:
6955 <pending: tabularise this>
6961 _fsadd.c - add floating point numbers
6963 _fssub.c - subtract floating point numbers
6965 _fsdiv.c - divide floating point numbers
6967 _fsmul.c - multiply floating point numbers
6969 _fs2uchar.c - convert floating point to unsigned char
6971 _fs2char.c - convert floating point to signed char
6973 _fs2uint.c - convert floating point to unsigned int
6975 _fs2int.c - convert floating point to signed int
6977 _fs2ulong.c - convert floating point to unsigned long
6979 _fs2long.c - convert floating point to signed long
6981 _uchar2fs.c - convert unsigned char to floating point
6983 _char2fs.c - convert char to floating point number
6985 _uint2fs.c - convert unsigned int to floating point
6987 _int2fs.c - convert int to floating point numbers
6989 _ulong2fs.c - convert unsigned long to floating point number
6991 _long2fs.c - convert long to floating point number
6999 Note if all these routines are used simultaneously the data space might
7001 For serious floating point usage it is strongly recommended that the large
7006 \begin_inset LatexCommand \index{Memory Models}
7011 \begin_inset LatexCommand \index{MCS51 Memory Models}
7018 SDCC allows two memory models for MCS51 code, small and large.
7019 Modules compiled with different memory models should
7023 be combined together or the results would be unpredictable.
7024 The library routines supplied with the compiler are compiled as both small
7026 The compiled library modules are contained in seperate directories as small
7027 and large so that you can link to either set.
7031 When the large model is used all variables declared without a storage class
7032 will be allocated into the external ram, this includes all parameters and
7033 local variables (for non-reentrant functions).
7034 When the small model is used variables without storage class are allocated
7035 in the internal ram.
7038 Judicious usage of the processor specific storage classes
7039 \begin_inset LatexCommand \index{storage classes}
7043 and the 'reentrant' function type will yield much more efficient code,
7044 than using the large model.
7045 Several optimizations are disabled when the program is compiled using the
7046 large model, it is therefore strongly recommdended that the small model
7047 be used unless absolutely required.
7051 \begin_inset LatexCommand \index{Memory Models}
7056 \begin_inset LatexCommand \index{DS390 Memory Models}
7063 The only model supported is Flat 24
7064 \begin_inset LatexCommand \index{Flat 24}
7069 This generates code for the 24 bit contiguous addressing mode of the Dallas
7071 In this mode, up to four meg of external RAM or code space can be directly
7073 See the data sheets at www.dalsemi.com for further information on this part.
7077 In older versions of the compiler, this option was used with the MCS51 code
7083 Now, however, the '390 has it's own code generator, selected by the
7092 Note that the compiler does not generate any code to place the processor
7093 into 24 bitmode (although
7097 in the ds390 libraries will do that for you).
7103 \begin_inset LatexCommand \index{tinibios}
7107 , the boot loader or similar code must ensure that the processor is in 24
7108 bit contiguous addressing mode before calling the SDCC startup code.
7116 option, variables will by default be placed into the XDATA segment.
7121 Segments may be placed anywhere in the 4 meg address space using the usual
7123 Note that if any segments are located above 64K, the -r flag must be passed
7124 to the linker to generate the proper segment relocations, and the Intel
7125 HEX output format must be used.
7126 The -r flag can be passed to the linker by using the option
7130 on the sdcc command line.
7131 However, currently the linker can not handle code segments > 64k.
7134 Defines Created by the Compiler
7135 \begin_inset LatexCommand \index{Defines Created by the Compiler}
7142 The compiler creates the following #defines
7143 \begin_inset LatexCommand \index{\#defines}
7151 \begin_inset LatexCommand \index{SDCC}
7155 - this Symbol is always defined.
7159 \begin_inset LatexCommand \index{SDCC\_mcs51}
7164 \begin_inset LatexCommand \index{SDCC\_ds390}
7169 \begin_inset LatexCommand \index{SDCC\_z80}
7173 , etc - depending on the model used (e.g.: -mds390)
7177 \begin_inset LatexCommand \index{\_\_mcs51}
7182 \begin_inset LatexCommand \index{\_\_ds390}
7187 \begin_inset LatexCommand \index{\_\_z80}
7191 , etc - depending on the model used (e.g.
7196 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
7200 - this symbol is defined when
7208 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
7220 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
7232 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
7244 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
7256 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
7271 \begin_inset LatexCommand \index{Optimizations}
7278 SDCC performs a host of standard optimizations in addition to some MCU specific
7281 \layout Subsubsection
7283 Sub-expression Elimination
7284 \begin_inset LatexCommand \index{Sub-expression Elimination}
7291 The compiler does local and global common subexpression elimination, e.g.:
7306 will be translated to
7322 Some subexpressions are not as obvious as the above example, e.g.:
7336 In this case the address arithmetic a->b[i] will be computed only once;
7337 the equivalent code in C would be.
7353 The compiler will try to keep these temporary variables in registers.
7354 \layout Subsubsection
7356 Dead-Code Elimination
7357 \begin_inset LatexCommand \index{Dead-Code Elimination}
7376 i = 1; \SpecialChar ~
7381 global = 1;\SpecialChar ~
7394 global = 3;\SpecialChar ~
7409 int global; void f ()
7422 \layout Subsubsection
7425 \begin_inset LatexCommand \index{Copy-Propagation}
7488 Note: the dead stores created by this copy propagation will be eliminated
7489 by dead-code elimination.
7490 \layout Subsubsection
7493 \begin_inset LatexCommand \index{Loop Optimizations}
7500 Two types of loop optimizations are done by SDCC loop invariant lifting
7501 and strength reduction of loop induction variables.
7502 In addition to the strength reduction the optimizer marks the induction
7503 variables and the register allocator tries to keep the induction variables
7504 in registers for the duration of the loop.
7505 Because of this preference of the register allocator
7506 \begin_inset LatexCommand \index{Register allocator}
7510 , loop induction optimization causes an increase in register pressure, which
7511 may cause unwanted spilling of other temporary variables into the stack
7512 \begin_inset LatexCommand \index{stack}
7517 The compiler will generate a warning message when it is forced to allocate
7518 extra space either on the stack or data space.
7519 If this extra space allocation is undesirable then induction optimization
7520 can be eliminated either for the entire source file (with ---noinduction
7521 option) or for a given function only using #pragma\SpecialChar ~
7523 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
7537 for (i = 0 ; i < 100 ; i ++)
7555 for (i = 0; i < 100; i++)
7565 As mentioned previously some loop invariants are not as apparent, all static
7566 address computations are also moved out of the loop.
7571 \begin_inset LatexCommand \index{Strength Reduction}
7575 , this optimization substitutes an expression by a cheaper expression:
7581 for (i=0;i < 100; i++)
7601 for (i=0;i< 100;i++) {
7605 ar[itemp1] = itemp2;
7621 The more expensive multiplication
7622 \begin_inset LatexCommand \index{multiplication}
7626 is changed to a less expensive addition.
7627 \layout Subsubsection
7630 \begin_inset LatexCommand \index{Loop Reversing}
7637 This optimization is done to reduce the overhead of checking loop boundaries
7638 for every iteration.
7639 Some simple loops can be reversed and implemented using a
7640 \begin_inset Quotes eld
7643 decrement and jump if not zero
7644 \begin_inset Quotes erd
7648 SDCC checks for the following criterion to determine if a loop is reversible
7649 (note: more sophisticated compilers use data-dependency analysis to make
7650 this determination, SDCC uses a more simple minded analysis).
7653 The 'for' loop is of the form
7659 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
7669 The <for body> does not contain
7670 \begin_inset Quotes eld
7674 \begin_inset Quotes erd
7678 \begin_inset Quotes erd
7684 All goto's are contained within the loop.
7687 No function calls within the loop.
7690 The loop control variable <sym> is not assigned any value within the loop
7693 The loop control variable does NOT participate in any arithmetic operation
7697 There are NO switch statements in the loop.
7698 \layout Subsubsection
7700 Algebraic Simplifications
7703 SDCC does numerous algebraic simplifications, the following is a small sub-set
7704 of these optimizations.
7710 i = j + 0 ; /* changed to */ i = j;
7712 i /= 2; /* changed to */ i >>= 1;
7714 i = j - j ; /* changed to */ i = 0;
7716 i = j / 1 ; /* changed to */ i = j;
7722 Note the subexpressions
7723 \begin_inset LatexCommand \index{subexpressions}
7727 given above are generally introduced by macro expansions or as a result
7728 of copy/constant propagation.
7729 \layout Subsubsection
7732 \begin_inset LatexCommand \index{switch Statements}
7739 SDCC changes switch statements to jump tables
7740 \begin_inset LatexCommand \index{jump tables}
7744 when the following conditions are true.
7748 The case labels are in numerical sequence, the labels need not be in order,
7749 and the starting number need not be one or zero.
7755 switch(i) {\SpecialChar ~
7862 Both the above switch statements will be implemented using a jump-table.
7865 The number of case labels is at least three, since it takes two conditional
7866 statements to handle the boundary conditions.
7869 The number of case labels is less than 84, since each label takes 3 bytes
7870 and a jump-table can be utmost 256 bytes long.
7874 Switch statements which have gaps in the numeric sequence or those that
7875 have more that 84 case labels can be split into more than one switch statement
7876 for efficient code generation, e.g.:
7914 If the above switch statement is broken down into two switch statements
7948 case 9: \SpecialChar ~
7958 case 12:\SpecialChar ~
7968 then both the switch statements will be implemented using jump-tables whereas
7969 the unmodified switch statement will not be.
7970 \layout Subsubsection
7972 Bit-shifting Operations
7973 \begin_inset LatexCommand \index{Bit-shifting Operations}
7980 Bit shifting is one of the most frequently used operation in embedded programmin
7982 SDCC tries to implement bit-shift operations in the most efficient way
8002 generates the following code:
8020 In general SDCC will never setup a loop if the shift count is known.
8060 Note that SDCC stores numbers in little-endian format (i.e.
8061 lowest order first).
8062 \layout Subsubsection
8065 \begin_inset LatexCommand \index{Bit-rotation}
8072 A special case of the bit-shift operation is bit rotation, SDCC recognizes
8073 the following expression to be a left bit-rotation:
8084 i = ((i << 1) | (i >> 7));
8092 will generate the following code:
8108 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
8109 ns of this case will also be recognized as bit-rotation, i.e.:
8115 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
8116 \layout Subsubsection
8119 \begin_inset LatexCommand \index{Highest Order Bit}
8126 It is frequently required to obtain the highest order bit of an integral
8127 type (long, int, short or char types).
8128 SDCC recognizes the following expression to yield the highest order bit
8129 and generates optimized code for it, e.g.:
8150 hob = (gint >> 15) & 1;
8163 will generate the following code:
8202 000A E5*01\SpecialChar ~
8230 000C 33\SpecialChar ~
8261 000D E4\SpecialChar ~
8292 000E 13\SpecialChar ~
8323 000F F5*02\SpecialChar ~
8353 Variations of this case however will
8358 It is a standard C expression, so I heartily recommend this be the only
8359 way to get the highest order bit, (it is portable).
8360 Of course it will be recognized even if it is embedded in other expressions,
8367 xyz = gint + ((gint >> 15) & 1);
8373 will still be recognized.
8374 \layout Subsubsection
8377 \begin_inset LatexCommand \index{Peephole optimizer}
8384 The compiler uses a rule based, pattern matching and re-writing mechanism
8385 for peep-hole optimization.
8390 a peep-hole optimizer by Christopher W.
8391 Fraser (cwfraser@microsoft.com).
8392 A default set of rules are compiled into the compiler, additional rules
8393 may be added with the
8396 \begin_inset LatexCommand \index{---peep-file}
8403 The rule language is best illustrated with examples.
8431 The above rule will change the following assembly sequence:
8461 Note: All occurrences of a
8465 (pattern variable) must denote the same string.
8466 With the above rule, the assembly sequence:
8484 will remain unmodified.
8488 Other special case optimizations may be added by the user (via
8494 some variants of the 8051 MCU allow only
8503 The following two rules will change all
8525 replace { lcall %1 } by { acall %1 }
8527 replace { ljmp %1 } by { ajmp %1 }
8535 inline-assembler code
8537 is also passed through the peep hole optimizer, thus the peephole optimizer
8538 can also be used as an assembly level macro expander.
8539 The rules themselves are MCU dependent whereas the rule language infra-structur
8540 e is MCU independent.
8541 Peephole optimization rules for other MCU can be easily programmed using
8546 The syntax for a rule is as follows:
8552 rule := replace [ restart ] '{' <assembly sequence> '
8590 <assembly sequence> '
8608 '}' [if <functionName> ] '
8616 <assembly sequence> := assembly instruction (each instruction including
8617 labels must be on a separate line).
8621 The optimizer will apply to the rules one by one from the top in the sequence
8622 of their appearance, it will terminate when all rules are exhausted.
8623 If the 'restart' option is specified, then the optimizer will start matching
8624 the rules again from the top, this option for a rule is expensive (performance)
8625 , it is intended to be used in situations where a transformation will trigger
8626 the same rule again.
8627 An example of this (not a good one, it has side effects) is the following
8654 Note that the replace pattern cannot be a blank, but can be a comment line.
8655 Without the 'restart' option only the inner most 'pop' 'push' pair would
8656 be eliminated, i.e.:
8708 the restart option the rule will be applied again to the resulting code
8709 and then all the pop-push pairs will be eliminated to yield:
8727 A conditional function can be attached to a rule.
8728 Attaching rules are somewhat more involved, let me illustrate this with
8759 The optimizer does a look-up of a function name table defined in function
8764 in the source file SDCCpeeph.c, with the name
8769 If it finds a corresponding entry the function is called.
8770 Note there can be no parameters specified for these functions, in this
8775 is crucial, since the function
8779 expects to find the label in that particular variable (the hash table containin
8780 g the variable bindings is passed as a parameter).
8781 If you want to code more such functions, take a close look at the function
8782 labelInRange and the calling mechanism in source file SDCCpeeph.c.
8783 I know this whole thing is a little kludgey, but maybe some day we will
8784 have some better means.
8785 If you are looking at this file, you will also see the default rules that
8786 are compiled into the compiler, you can add your own rules in the default
8787 set there if you get tired of specifying the ---peep-file option.
8791 \begin_inset LatexCommand \index{Pragmas}
8798 SDCC supports the following #pragma directives.
8802 \begin_inset LatexCommand \index{\#pragma SAVE}
8806 - this will save all current options to the SAVE/RESTORE stack.
8811 \begin_inset LatexCommand \index{\#pragma RESTORE}
8815 - will restore saved options from the last save.
8816 SAVEs & RESTOREs can be nested.
8817 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
8818 RESTORE pulls current options from the stack.
8823 \begin_inset LatexCommand \index{\#pragma NOGCSE}
8827 - will stop global subexpression elimination.
8831 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
8835 - will stop loop induction optimizations.
8839 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
8843 - will not generate code for boundary value checking, when switch statements
8844 are turned into jump-tables.
8848 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8852 - the compiler will not overlay the parameters and local variables of a
8857 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
8861 - the compiler will not warn you anymore for obvious mistakes, you'r on
8866 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
8870 - Will not do loop reversal optimization
8874 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
8878 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
8879 pair of push/pop instruction in ISR function (using interrupt keyword).
8880 The directive should be placed immediately before the ISR function definition
8881 and it affects ALL ISR functions following it.
8882 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
8883 EXCLUDE\SpecialChar ~
8888 \begin_inset LatexCommand \index{\#pragma NOIV}
8892 - Do not generate interrupt vector table entries for all ISR functions
8893 defined after the pragma.
8894 This is useful in cases where the interrupt vector table must be defined
8895 manually, or when there is a secondary, manually defined interrupt vector
8897 for the autovector feature of the Cypress EZ-USB FX2).
8901 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
8905 function1[,function2[,function3...]] - The compiler by default uses a caller
8906 saves convention for register saving across function calls, however this
8907 can cause unneccessary register pushing & popping when calling small functions
8908 from larger functions.
8909 This option can be used to switch off the register saving convention for
8910 the function names specified.
8911 The compiler will not save registers when calling these functions, extra
8912 code need to be manually inserted at the entry & exit for these functions
8913 to save & restore the registers used by these functions, this can SUBSTANTIALLY
8914 reduce code & improve run time performance of the generated code.
8915 In the future the compiler (with interprocedural analysis) may be able
8916 to determine the appropriate scheme to use for each function call.
8917 If ---callee-saves command line option is used, the function names specified
8918 in #pragma\SpecialChar ~
8919 CALLEE-SAVES is appended to the list of functions specified in
8923 The pragma's are intended to be used to turn-off certain optimizations which
8924 might cause the compiler to generate extra stack / data space to store
8925 compiler generated temporary variables.
8926 This usually happens in large functions.
8927 Pragma directives should be used as shown in the following example, they
8928 are used to control options & optimizations for a given function; pragmas
8929 should be placed before and/or after a function, placing pragma's inside
8930 a function body could have unpredictable results.
8937 \begin_inset LatexCommand \index{\#pragma SAVE}
8941 /* save the current settings */
8944 \begin_inset LatexCommand \index{\#pragma NOGCSE}
8948 /* turnoff global subexpression elimination */
8951 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
8955 /* turn off induction optimizations */
8978 \begin_inset LatexCommand \index{\#pragma RESTORE}
8982 /* turn the optimizations back on */
8988 The compiler will generate a warning message when extra space is allocated.
8989 It is strongly recommended that the SAVE and RESTORE pragma's be used when
8990 changing options for a function.
8995 <pending: this is messy and incomplete>
9000 Compiler support routines (_gptrget, _mulint etc)
9003 Stdclib functions (puts, printf, strcat etc)
9006 Math functions (sin, pow, sqrt etc)
9009 Interfacing with Assembly Routines
9010 \begin_inset LatexCommand \index{Assembly Routines}
9015 \layout Subsubsection
9017 Global Registers used for Parameter Passing
9018 \begin_inset LatexCommand \index{Parameter Passing}
9025 The compiler always uses the global registers
9028 \begin_inset LatexCommand \index{DPL}
9033 \begin_inset LatexCommand \index{DPH}
9038 \begin_inset LatexCommand \index{DPTR}
9043 \begin_inset LatexCommand \index{B}
9052 \begin_inset LatexCommand \index{ACC}
9058 to pass the first parameter to a routine.
9059 The second parameter onwards is either allocated on the stack (for reentrant
9060 routines or if ---stack-auto is used) or in the internal / external ram
9061 (depending on the memory model).
9063 \layout Subsubsection
9065 Assembler Routine(non-reentrant)
9066 \begin_inset LatexCommand \index{Assembler Routine(non-reentrant)}
9073 In the following example the function cfunc calls an assembler routine asm_func,
9074 which takes two parameters.
9080 extern int asm_func(unsigned char, unsigned char);
9084 int c_func (unsigned char i, unsigned char j)
9092 return asm_func(i,j);
9106 return c_func(10,9);
9114 The corresponding assembler function is:
9120 .globl _asm_func_PARM_2
9184 add a,_asm_func_PARM_2
9220 Note here that the return values are placed in 'dpl' - One byte return value,
9221 'dpl' LSB & 'dph' MSB for two byte values.
9222 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
9223 b' & 'acc' for four byte values.
9226 The parameter naming convention is _<function_name>_PARM_<n>, where n is
9227 the parameter number starting from 1, and counting from the left.
9228 The first parameter is passed in
9229 \begin_inset Quotes eld
9233 \begin_inset Quotes erd
9236 for One bye parameter,
9237 \begin_inset Quotes eld
9241 \begin_inset Quotes erd
9245 \begin_inset Quotes eld
9249 \begin_inset Quotes erd
9253 \begin_inset Quotes eld
9257 \begin_inset Quotes erd
9260 for four bytes, the varible name for the second parameter will be _<function_na
9265 Assemble the assembler routine with the following command:
9272 asx8051 -losg asmfunc.asm
9279 Then compile and link the assembler routine to the C source file with the
9287 sdcc cfunc.c asmfunc.rel
9288 \layout Subsubsection
9290 Assembler Routine(reentrant)
9291 \begin_inset LatexCommand \index{Assembler Routine(reentrant)}
9298 In this case the second parameter onwards will be passed on the stack, the
9299 parameters are pushed from right to left i.e.
9300 after the call the left most parameter will be on the top of the stack.
9307 extern int asm_func(unsigned char, unsigned char);
9311 int c_func (unsigned char i, unsigned char j) reentrant
9319 return asm_func(i,j);
9333 return c_func(10,9);
9341 The corresponding assembler routine is:
9451 The compiling and linking procedure remains the same, however note the extra
9452 entry & exit linkage required for the assembler code, _bp is the stack
9453 frame pointer and is used to compute the offset into the stack for parameters
9454 and local variables.
9458 \begin_inset LatexCommand \index{External Stack}
9465 The external stack is located at the start of the external ram segment,
9466 and is 256 bytes in size.
9467 When ---xstack option is used to compile the program, the parameters and
9468 local variables of all reentrant functions are allocated in this area.
9469 This option is provided for programs with large stack space requirements.
9470 When used with the ---stack-auto option, all parameters and local variables
9471 are allocated on the external stack (note support libraries will need to
9472 be recompiled with the same options).
9475 The compiler outputs the higher order address byte of the external ram segment
9476 into PORT P2, therefore when using the External Stack option, this port
9477 MAY NOT be used by the application program.
9481 \begin_inset LatexCommand \index{ANSI-Compliance}
9488 Deviations from the compliancy.
9491 functions are not always reentrant.
9494 structures cannot be assigned values directly, cannot be passed as function
9495 parameters or assigned to each other and cannot be a return value from
9522 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
9533 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
9555 return rets;/* is invalid in SDCC although allowed in ANSI */
9560 'long long' (64 bit integers) not supported.
9563 'double' precision floating point not supported.
9566 No support for setjmp and longjmp (for now).
9569 Old K&R style function declarations are NOT allowed.
9575 foo(i,j) /* this old style of function declarations */
9577 int i,j; /* are valid in ANSI but not valid in SDCC */
9591 functions declared as pointers must be dereferenced during the call.
9602 /* has to be called like this */
9604 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
9607 Cyclomatic Complexity
9608 \begin_inset LatexCommand \index{Cyclomatic Complexity}
9615 Cyclomatic complexity of a function is defined as the number of independent
9616 paths the program can take during execution of the function.
9617 This is an important number since it defines the number test cases you
9618 have to generate to validate the function.
9619 The accepted industry standard for complexity number is 10, if the cyclomatic
9620 complexity reported by SDCC exceeds 10 you should think about simplification
9621 of the function logic.
9622 Note that the complexity level is not related to the number of lines of
9624 Large functions can have low complexity, and small functions can have large
9630 SDCC uses the following formula to compute the complexity:
9635 complexity = (number of edges in control flow graph) - (number of nodes
9636 in control flow graph) + 2;
9640 Having said that the industry standard is 10, you should be aware that in
9641 some cases it be may unavoidable to have a complexity level of less than
9643 For example if you have switch statement with more than 10 case labels,
9644 each case label adds one to the complexity level.
9645 The complexity level is by no means an absolute measure of the algorithmic
9646 complexity of the function, it does however provide a good starting point
9647 for which functions you might look at for further optimization.
9653 Here are a few guidelines that will help the compiler generate more efficient
9654 code, some of the tips are specific to this compiler others are generally
9655 good programming practice.
9658 Use the smallest data type to represent your data-value.
9659 If it is known in advance that the value is going to be less than 256 then
9660 use an 'unsigned char' instead of a 'short' or 'int'.
9663 Use unsigned when it is known in advance that the value is not going to
9665 This helps especially if you are doing division or multiplication.
9668 NEVER jump into a LOOP.
9671 Declare the variables to be local whenever possible, especially loop control
9672 variables (induction).
9675 Since the compiler does not always do implicit integral promotion, the programme
9676 r should do an explicit cast when integral promotion is required.
9679 Reducing the size of division, multiplication & modulus operations can reduce
9680 code size substantially.
9681 Take the following code for example.
9687 foobar(unsigned int p1, unsigned char ch)
9691 unsigned char ch1 = p1 % ch ;
9702 For the modulus operation the variable ch will be promoted to unsigned int
9703 first then the modulus operation will be performed (this will lead to a
9704 call to support routine _moduint()), and the result will be casted to a
9706 If the code is changed to
9712 foobar(unsigned int p1, unsigned char ch)
9716 unsigned char ch1 = (unsigned char)p1 % ch ;
9727 It would substantially reduce the code generated (future versions of the
9728 compiler will be smart enough to detect such optimization oppurtunities).
9731 Notes on MCS51 memory
9732 \begin_inset LatexCommand \index{MCS51 memory}
9739 The 8051 family of micro controller have a minimum of 128 bytes of internal
9740 memory which is structured as follows
9744 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
9747 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
9749 - Bytes 30-7F - 60 bytes for general purpose use.
9753 Normally the SDCC compiler will only utilise the first bank
9754 \begin_inset LatexCommand \index{bank}
9758 of registers, but it is possible to specify that other banks of registers
9759 should be used in interrupt
9760 \begin_inset LatexCommand \index{interrupt}
9765 By default, the compiler will place the stack after the last bank of used
9767 if the first 2 banks of registers are used, it will position the base of
9768 the internal stack at address 16 (0X10).
9769 This implies that as the stack
9770 \begin_inset LatexCommand \index{stack}
9774 grows, it will use up the remaining register banks, and the 16 bytes used
9775 by the 128 bit variables, and 60 bytes for general purpose use.
9778 By default, the compiler uses the 60 general purpose bytes to hold "near
9780 The compiler/optimiser may also declare some Local Variables in this area
9785 If any of the 128 bit variables are used, or near data is being used then
9786 care needs to be taken to ensure that the stack does not grow so much that
9787 it starts to over write either your bit variables or "near data".
9788 There is no runtime checking to prevent this from happening.
9791 The amount of stack being used is affected by the use of the "internal stack"
9792 to save registers before a subroutine call is made (---stack-auto
9793 \begin_inset LatexCommand \index{---stack-auto}
9797 will declare parameters and local variables on the stack) and the number
9798 of nested subroutines.
9801 If you detect that the stack is over writing you data, then the following
9803 ---xstack will cause an external stack to be used for saving registers
9804 and (if ---stack-auto is being used) storing parameters and local variables.
9805 However this will produce more code which will be slower to execute.
9810 \begin_inset LatexCommand \index{---stack-loc}
9814 will allow you specify the start of the stack, i.e.
9815 you could start it after any data in the general purpose area.
9816 However this may waste the memory not used by the register banks and if
9817 the size of the "near data" increases, it may creep into the bottom of
9822 \begin_inset LatexCommand \index{---stack-after-data}
9826 , similar to the ---stack-loc, but it automatically places the stack after
9827 the end of the "near data".
9828 Again this could waste any spare register space.
9832 \begin_inset LatexCommand \index{---data-loc}
9836 allows you to specify the start address of the near data.
9837 This could be used to move the "near data" further away from the stack
9838 giving it more room to grow.
9839 This will only work if no bit variables are being used and the stack can
9840 grow to use the bit variable space.
9848 If you find that the stack is over writing your bit variables or "near data"
9849 then the approach which best utilised the internal memory is to position
9850 the "near data" after the last bank of used registers or, if you use bit
9851 variables, after the last bit variable by using the ---data-loc, e.g.
9852 if two register banks are being used and no bit variables, ---data-loc
9853 16, and use the ---stack-after-data option.
9856 If bit variables are being used, another method would be to try and squeeze
9857 the data area in the unused register banks if it will fit, and start the
9858 stack after the last bit variable.
9861 Retargetting for other MCUs.
9864 The issues for retargetting the compiler are far too numerous to be covered
9866 What follows is a brief description of each of the seven phases of the
9867 compiler and its MCU dependency.
9870 Parsing the source and building the annotated parse tree.
9871 This phase is largely MCU independent (except for the language extensions).
9872 Syntax & semantic checks are also done in this phase, along with some initial
9873 optimizations like back patching labels and the pattern matching optimizations
9874 like bit-rotation etc.
9877 The second phase involves generating an intermediate code which can be easy
9878 manipulated during the later phases.
9879 This phase is entirely MCU independent.
9880 The intermediate code generation assumes the target machine has unlimited
9881 number of registers, and designates them with the name iTemp.
9882 The compiler can be made to dump a human readable form of the code generated
9883 by using the ---dumpraw option.
9886 This phase does the bulk of the standard optimizations and is also MCU independe
9888 This phase can be broken down into several sub-phases:
9892 Break down intermediate code (iCode) into basic blocks.
9894 Do control flow & data flow analysis on the basic blocks.
9896 Do local common subexpression elimination, then global subexpression elimination
9898 Dead code elimination
9902 If loop optimizations caused any changes then do 'global subexpression eliminati
9903 on' and 'dead code elimination' again.
9906 This phase determines the live-ranges; by live range I mean those iTemp
9907 variables defined by the compiler that still survive after all the optimization
9909 Live range analysis is essential for register allocation, since these computati
9910 on determines which of these iTemps will be assigned to registers, and for
9914 Phase five is register allocation.
9915 There are two parts to this process.
9919 The first part I call 'register packing' (for lack of a better term).
9920 In this case several MCU specific expression folding is done to reduce
9925 The second part is more MCU independent and deals with allocating registers
9926 to the remaining live ranges.
9927 A lot of MCU specific code does creep into this phase because of the limited
9928 number of index registers available in the 8051.
9931 The Code generation phase is (unhappily), entirely MCU dependent and very
9932 little (if any at all) of this code can be reused for other MCU.
9933 However the scheme for allocating a homogenized assembler operand for each
9934 iCode operand may be reused.
9937 As mentioned in the optimization section the peep-hole optimizer is rule
9938 based system, which can reprogrammed for other MCUs.
9942 \begin_inset LatexCommand \index{SDCDB}
9946 - Source Level Debugger
9947 \begin_inset LatexCommand \index{Debugger}
9954 SDCC is distributed with a source level debugger.
9955 The debugger uses a command line interface, the command repertoire of the
9956 debugger has been kept as close to gdb (the GNU debugger) as possible.
9957 The configuration and build process is part of the standard compiler installati
9958 on, which also builds and installs the debugger in the target directory
9959 specified during configuration.
9960 The debugger allows you debug BOTH at the C source and at the ASM source
9964 Compiling for Debugging
9969 debug option must be specified for all files for which debug information
9971 The complier generates a .adb file for each of these files.
9972 The linker creates the .cdb file from the .adb files and the address information.
9973 This .cdb is used by the debugger.
9976 How the Debugger Works
9979 When the ---debug option is specified the compiler generates extra symbol
9980 information some of which are put into the the assembler source and some
9981 are put into the .adb file.
9982 Then the linker creates the .cdb file from the individual .adb files with
9983 the address information for the symbols.
9984 The debugger reads the symbolic information generated by the compiler &
9985 the address information generated by the linker.
9986 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
9987 execution is controlled by the debugger.
9988 When a command is issued for the debugger, it translates it into appropriate
9989 commands for the simulator.
9992 Starting the Debugger
9995 The debugger can be started using the following command line.
9996 (Assume the file you are debugging has the file name foo).
10010 The debugger will look for the following files.
10013 foo.c - the source file.
10016 foo.cdb - the debugger symbol information file.
10019 foo.ihx - the intel hex format object file.
10022 Command Line Options.
10025 ---directory=<source file directory> this option can used to specify the
10026 directory search list.
10027 The debugger will look into the directory list specified for source, cdb
10029 The items in the directory list must be separated by ':', e.g.
10030 if the source files can be in the directories /home/src1 and /home/src2,
10031 the ---directory option should be ---directory=/home/src1:/home/src2.
10032 Note there can be no spaces in the option.
10036 -cd <directory> - change to the <directory>.
10039 -fullname - used by GUI front ends.
10042 -cpu <cpu-type> - this argument is passed to the simulator please see the
10043 simulator docs for details.
10046 -X <Clock frequency > this options is passed to the simulator please see
10047 the simulator docs for details.
10050 -s <serial port file> passed to simulator see the simulator docs for details.
10053 -S <serial in,out> passed to simulator see the simulator docs for details.
10059 As mention earlier the command interface for the debugger has been deliberately
10060 kept as close the GNU debugger gdb, as possible.
10061 This will help the integration with existing graphical user interfaces
10062 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
10063 \layout Subsubsection
10065 break [line | file:line | function | file:function]
10068 Set breakpoint at specified line or function:
10077 sdcdb>break foo.c:100
10079 sdcdb>break funcfoo
10081 sdcdb>break foo.c:funcfoo
10082 \layout Subsubsection
10084 clear [line | file:line | function | file:function ]
10087 Clear breakpoint at specified line or function:
10096 sdcdb>clear foo.c:100
10098 sdcdb>clear funcfoo
10100 sdcdb>clear foo.c:funcfoo
10101 \layout Subsubsection
10106 Continue program being debugged, after breakpoint.
10107 \layout Subsubsection
10112 Execute till the end of the current function.
10113 \layout Subsubsection
10118 Delete breakpoint number 'n'.
10119 If used without any option clear ALL user defined break points.
10120 \layout Subsubsection
10122 info [break | stack | frame | registers ]
10125 info break - list all breakpoints
10128 info stack - show the function call stack.
10131 info frame - show information about the current execution frame.
10134 info registers - show content of all registers.
10135 \layout Subsubsection
10140 Step program until it reaches a different source line.
10141 \layout Subsubsection
10146 Step program, proceeding through subroutine calls.
10147 \layout Subsubsection
10152 Start debugged program.
10153 \layout Subsubsection
10158 Print type information of the variable.
10159 \layout Subsubsection
10164 print value of variable.
10165 \layout Subsubsection
10170 load the given file name.
10171 Note this is an alternate method of loading file for debugging.
10172 \layout Subsubsection
10177 print information about current frame.
10178 \layout Subsubsection
10183 Toggle between C source & assembly source.
10184 \layout Subsubsection
10186 ! simulator command
10189 Send the string following '!' to the simulator, the simulator response is
10191 Note the debugger does not interpret the command being sent to the simulator,
10192 so if a command like 'go' is sent the debugger can loose its execution
10193 context and may display incorrect values.
10194 \layout Subsubsection
10201 My name is Bobby Brown"
10204 Interfacing with XEmacs
10205 \begin_inset LatexCommand \index{XEmacs}
10210 \begin_inset LatexCommand \index{Emacs}
10217 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
10218 sdcdb.el and sdcdbsrc.el.
10219 These two files can be found in the $(prefix)/bin directory after the installat
10221 These files need to be loaded into XEmacs for the interface to work.
10222 This can be done at XEmacs startup time by inserting the following into
10223 your '.xemacs' file (which can be found in your HOME directory):
10229 (load-file sdcdbsrc.el)
10235 .xemacs is a lisp file so the () around the command is REQUIRED.
10236 The files can also be loaded dynamically while XEmacs is running, set the
10237 environment variable 'EMACSLOADPATH' to the installation bin directory
10238 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
10239 To start the interface enter the following command:
10253 You will prompted to enter the file name to be debugged.
10258 The command line options that are passed to the simulator directly are bound
10259 to default values in the file sdcdbsrc.el.
10260 The variables are listed below, these values maybe changed as required.
10263 sdcdbsrc-cpu-type '51
10266 sdcdbsrc-frequency '11059200
10269 sdcdbsrc-serial nil
10272 The following is a list of key mapping for the debugger interface.
10280 ;; Current Listing ::
10282 ;;key\SpecialChar ~
10297 binding\SpecialChar ~
10321 ;;---\SpecialChar ~
10336 ------\SpecialChar ~
10376 sdcdb-next-from-src\SpecialChar ~
10402 sdcdb-back-from-src\SpecialChar ~
10428 sdcdb-cont-from-src\SpecialChar ~
10438 SDCDB continue command
10454 sdcdb-step-from-src\SpecialChar ~
10480 sdcdb-whatis-c-sexp\SpecialChar ~
10490 SDCDB ptypecommand for data at
10554 sdcdbsrc-delete\SpecialChar ~
10568 SDCDB Delete all breakpoints if no arg
10616 given or delete arg (C-u arg x)
10632 sdcdbsrc-frame\SpecialChar ~
10647 SDCDB Display current frame if no arg,
10696 given or display frame arg
10761 sdcdbsrc-goto-sdcdb\SpecialChar ~
10771 Goto the SDCDB output buffer
10787 sdcdb-print-c-sexp\SpecialChar ~
10798 SDCDB print command for data at
10862 sdcdbsrc-goto-sdcdb\SpecialChar ~
10872 Goto the SDCDB output buffer
10888 sdcdbsrc-mode\SpecialChar ~
10904 Toggles Sdcdbsrc mode (turns it off)
10908 ;; C-c C-f\SpecialChar ~
10916 sdcdb-finish-from-src\SpecialChar ~
10924 SDCDB finish command
10928 ;; C-x SPC\SpecialChar ~
10936 sdcdb-break\SpecialChar ~
10954 Set break for line with point
10956 ;; ESC t\SpecialChar ~
10966 sdcdbsrc-mode\SpecialChar ~
10982 Toggle Sdcdbsrc mode
10984 ;; ESC m\SpecialChar ~
10994 sdcdbsrc-srcmode\SpecialChar ~
11018 The Z80 and gbz80 port
11021 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
11022 The port is incomplete - long support is incomplete (mul, div and mod are
11023 unimplimented), and both float and bitfield support is missing.
11024 Apart from that the code generated is correct.
11027 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
11028 The stack frame is similar to that generated by the IAR Z80 compiler.
11029 IX is used as the base pointer, HL is used as a temporary register, and
11030 BC and DE are available for holding varibles.
11031 IY is currently unusued.
11032 Return values are stored in HL.
11033 One bad side effect of using IX as the base pointer is that a functions
11034 stack frame is limited to 127 bytes - this will be fixed in a later version.
11038 \begin_inset LatexCommand \index{Support}
11045 SDCC has grown to be a large project.
11046 The compiler alone (without the preprocessor, assembler and linker) is
11047 about 40,000 lines of code (blank stripped).
11048 The open source nature of this project is a key to its continued growth
11050 You gain the benefit and support of many active software developers and
11052 Is SDCC perfect? No, that's why we need your help.
11053 The developers take pride in fixing reported bugs.
11054 You can help by reporting the bugs and helping other SDCC users.
11055 There are lots of ways to contribute, and we encourage you to take part
11056 in making SDCC a great software package.
11060 \begin_inset LatexCommand \index{Bugs}
11065 \begin_inset LatexCommand \index{Reporting Bugs}
11072 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
11073 cc@sdcc.sourceforge.net'.
11074 Bugs will be fixed ASAP.
11075 When reporting a bug, it is very useful to include a small test program
11076 which reproduces the problem.
11077 If you can isolate the problem by looking at the generated assembly code,
11078 this can be very helpful.
11079 Compiling your program with the ---dumpall option can sometimes be useful
11080 in locating optimization problems.
11086 The anatomy of the compiler
11091 This is an excerpt from an atricle published in Circuit Cellar MagaZine
11093 It's a little outdated (the compiler is much more efficient now and user/devell
11094 oper friendly), but pretty well exposes the guts of it all.
11100 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
11101 It is fairly easy to retarget for other 8-bit MCU.
11102 Here we take a look at some of the internals of the compiler.
11107 \begin_inset LatexCommand \index{Parsing}
11114 Parsing the input source file and creating an AST (Annotated Syntax Tree
11115 \begin_inset LatexCommand \index{Annotated Syntax Tree}
11120 This phase also involves propagating types (annotating each node of the
11121 parse tree with type information) and semantic analysis.
11122 There are some MCU specific parsing rules.
11123 For example the storage classes, the extended storage classes are MCU specific
11124 while there may be a xdata storage class for 8051 there is no such storage
11125 class for z80 or Atmel AVR.
11126 SDCC allows MCU specific storage class extensions, i.e.
11127 xdata will be treated as a storage class specifier when parsing 8051 C
11128 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
11133 \begin_inset LatexCommand \index{iCode}
11140 Intermediate code generation.
11141 In this phase the AST is broken down into three-operand form (iCode).
11142 These three operand forms are represented as doubly linked lists.
11143 ICode is the term given to the intermediate form generated by the compiler.
11144 ICode example section shows some examples of iCode generated for some simple
11145 C source functions.
11149 \begin_inset LatexCommand \index{Optimizations}
11156 Bulk of the target independent optimizations is performed in this phase.
11157 The optimizations include constant propagation, common sub-expression eliminati
11158 on, loop invariant code movement, strength reduction of loop induction variables
11159 and dead-code elimination.
11162 Live range analysis
11163 \begin_inset LatexCommand \index{Live range analysis}
11170 During intermediate code generation phase, the compiler assumes the target
11171 machine has infinite number of registers and generates a lot of temporary
11173 The live range computation determines the lifetime of each of these compiler-ge
11174 nerated temporaries.
11175 A picture speaks a thousand words.
11176 ICode example sections show the live range annotations for each of the
11178 It is important to note here, each iCode is assigned a number in the order
11179 of its execution in the function.
11180 The live ranges are computed in terms of these numbers.
11181 The from number is the number of the iCode which first defines the operand
11182 and the to number signifies the iCode which uses this operand last.
11185 Register Allocation
11186 \begin_inset LatexCommand \index{Register Allocation}
11193 The register allocation determines the type and number of registers needed
11195 In most MCUs only a few registers can be used for indirect addressing.
11196 In case of 8051 for example the registers R0 & R1 can be used to indirectly
11197 address the internal ram and DPTR to indirectly address the external ram.
11198 The compiler will try to allocate the appropriate register to pointer variables
11200 ICode example section shows the operands annotated with the registers assigned
11202 The compiler will try to keep operands in registers as much as possible;
11203 there are several schemes the compiler uses to do achieve this.
11204 When the compiler runs out of registers the compiler will check to see
11205 if there are any live operands which is not used or defined in the current
11206 basic block being processed, if there are any found then it will push that
11207 operand and use the registers in this block, the operand will then be popped
11208 at the end of the basic block.
11212 There are other MCU specific considerations in this phase.
11213 Some MCUs have an accumulator; very short-lived operands could be assigned
11214 to the accumulator instead of general-purpose register.
11220 Figure II gives a table of iCode operations supported by the compiler.
11221 The code generation involves translating these operations into corresponding
11222 assembly code for the processor.
11223 This sounds overly simple but that is the essence of code generation.
11224 Some of the iCode operations are generated on a MCU specific manner for
11225 example, the z80 port does not use registers to pass parameters so the
11226 SEND and RECV iCode operations will not be generated, and it also does
11227 not support JUMPTABLES.
11234 <Where is Figure II ?>
11238 \begin_inset LatexCommand \index{ICode Example}
11245 This section shows some details of iCode.
11246 The example C code does not do anything useful; it is used as an example
11247 to illustrate the intermediate code generated by the compiler.
11260 /* This function does nothing useful.
11267 for the purpose of explaining iCode */
11270 short function (data int *x)
11278 short i=10; /* dead initialization eliminated */
11283 short sum=10; /* dead initialization eliminated */
11296 while (*x) *x++ = *p++;
11310 /* compiler detects i,j to be induction variables */
11314 for (i = 0, j = 10 ; i < 10 ; i++, j---) {
11326 mul += i * 3; /* this multiplication remains */
11332 gint += j * 3;/* this multiplication changed to addition */
11349 In addition to the operands each iCode contains information about the filename
11350 and line it corresponds to in the source file.
11351 The first field in the listing should be interpreted as follows:
11356 Filename(linenumber: iCode Execution sequence number : ICode hash table
11357 key : loop depth of the iCode).
11362 Then follows the human readable form of the ICode operation.
11363 Each operand of this triplet form can be of three basic types a) compiler
11364 generated temporary b) user defined variable c) a constant value.
11365 Note that local variables and parameters are replaced by compiler generated
11367 Live ranges are computed only for temporaries (i.e.
11368 live ranges are not computed for global variables).
11369 Registers are allocated for temporaries only.
11370 Operands are formatted in the following manner:
11375 Operand Name [lr live-from : live-to ] { type information } [ registers
11381 As mentioned earlier the live ranges are computed in terms of the execution
11382 sequence number of the iCodes, for example
11384 the iTemp0 is live from (i.e.
11385 first defined in iCode with execution sequence number 3, and is last used
11386 in the iCode with sequence number 5).
11387 For induction variables such as iTemp21 the live range computation extends
11388 the lifetime from the start to the end of the loop.
11390 The register allocator used the live range information to allocate registers,
11391 the same registers may be used for different temporaries if their live
11392 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
11393 iTemp17 since their live ranges do not overlap.
11394 In addition the allocator also takes into consideration the type and usage
11395 of a temporary, for example itemp6 is a pointer to near space and is used
11396 as to fetch data from (i.e.
11397 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
11398 Some short lived temporaries are allocated to special registers which have
11399 meaning to the code generator e.g.
11400 iTemp13 is allocated to a pseudo register CC which tells the back end that
11401 the temporary is used only for a conditional jump the code generation makes
11402 use of this information to optimize a compare and jump ICode.
11404 There are several loop optimizations performed by the compiler.
11405 It can detect induction variables iTemp21(i) and iTemp23(j).
11406 Also note the compiler does selective strength reduction, i.e.
11407 the multiplication of an induction variable in line 18 (gint = j * 3) is
11408 changed to addition, a new temporary iTemp17 is allocated and assigned
11409 a initial value, a constant 3 is then added for each iteration of the loop.
11410 The compiler does not change the multiplication in line 17 however since
11411 the processor does support an 8 * 8 bit multiplication.
11413 Note the dead code elimination optimization eliminated the dead assignments
11414 in line 7 & 8 to I and sum respectively.
11421 Sample.c (5:1:0:0) _entry($9) :
11426 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
11431 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
11436 Sample.c(11:4:53:0) preHeaderLbl0($11) :
11441 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
11447 Sample.c(11:6:5:1) _whilecontinue_0($1) :
11452 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
11458 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
11463 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
11469 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
11475 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
11481 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
11487 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
11488 * int}[r0] + 0x2 {short}
11493 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
11498 Sample.c(11:17:21:0)_whilebreak_0($3) :
11503 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
11508 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
11513 Sample.c(15:20:54:0)preHeaderLbl1($13) :
11518 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
11523 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
11528 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
11533 Sample.c(15:24:26:1)_forcond_0($4) :
11538 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
11544 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
11549 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
11550 + ITemp21 [lr21:38]{short}[r4]
11555 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
11561 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
11562 + iTemp15 [lr29:30]{short}[r1]
11567 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
11573 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
11579 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
11585 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
11591 Sample.c(19:38:47:1) goto _forcond_0($4)
11596 Sample.c(19:39:48:0)_forbreak_0($7) :
11601 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
11602 + ITemp11 [lr19:40]{short}[r3]
11607 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
11612 Sample.c(20:42:51:0)_return($8) :
11617 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
11623 Finally the code generated for this function:
11664 ; ----------------------------------------------
11669 ; function function
11674 ; ----------------------------------------------
11684 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
11696 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
11708 ;_whilecontinue_0($1) :
11718 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
11723 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
11782 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
11801 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
11848 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
11888 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
11914 ; iTemp6 [lr5:16]{_near * int}[r0] =
11919 ; iTemp6 [lr5:16]{_near * int}[r0] +
11936 ; goto _whilecontinue_0($1)
11948 ; _whilebreak_0($3) :
11958 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
11970 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
11982 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
11994 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
12013 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
12042 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
12047 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
12092 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
12097 ; iTemp21 [lr21:38]{short}[r4]
12123 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
12156 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
12161 ; iTemp15 [lr29:30]{short}[r1]
12180 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
12227 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
12274 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
12286 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
12300 cjne r5,#0xff,00104$
12312 ; goto _forcond_0($4)
12324 ; _forbreak_0($7) :
12334 ; ret iTemp24 [lr40:41]{short}
12377 A few words about basic block successors, predecessors and dominators
12380 Successors are basic blocks that might execute after this basic block.
12382 Predecessors are basic blocks that might execute before reaching this basic
12385 Dominators are basic blocks that WILL execute before reaching this basic
12411 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
12414 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
12417 c) domVect of [BB4] = BB1 ...
12418 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
12426 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
12436 Thanks to all the other volunteer developers who have helped with coding,
12437 testing, web-page creation, distribution sets, etc.
12438 You know who you are :-)
12445 This document was initially written by Sandeep Dutta
12448 All product names mentioned herein may be trademarks of their respective
12456 To avoid confusion, the installation and building options for sdcc itself
12457 (chapter 2) are not part of the index.
12461 \begin_inset LatexCommand \printindex{}