1 #LyX 1.1 created this file. For more info see http://www.lyx.org/
14 \paperorientation portrait
17 \paragraph_separation indent
19 \quotes_language swedish
23 \paperpagestyle default
27 lSDCC Compiler User Guide
31 \begin_inset LatexCommand \tableofcontents{}
48 is a Free ware, retargettable, optimizing ANSI-C compiler by
52 designed for 8 bit Microprocessors.
53 The current version targets Intel MCS51 based Microprocessors(8051,8052,
54 etc), Zilog Z80 based MCUs, and the Dallas 80C390 MCS51 variant.
55 It can be retargetted for other microprocessors, support for PIC, AVR and
56 186 is under development.
57 The entire source code for the compiler is distributed under GPL.
58 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
59 SDCC has extensive language extensions suitable for utilizing various microcont
60 rollers underlying hardware effectively.
61 In addition to the MCU specific optimizations SDCC also does a host of
62 standard optimizations like
64 global sub expression elimination, loop optimizations (loop invariant,
65 strength reduction of induction variables and loop reversing), constant
66 folding & propagation, copy propagation, dead code elimination and jumptables
67 for 'switch' statements.
70 For the back-end SDCC uses a global register allocation scheme which should
71 be well suited for other 8 bit MCUs.
72 The peep hole optimizer uses a rule based substitution mechanism which
74 Supported data-types are
76 char (8 bits, 1 byte), short and int (16 bits, 2 bytes ), long (32 bit,
84 The compiler also allows
88 to be embedded anywhere in a function.
89 In addition routines developed in assembly can also be called.
90 SDCC also provides an option to report the relative complexity of a function,
91 these functions can then be further optimized, or hand coded in assembly
93 SDCC also comes with a companion source level debugger SDCDB, the debugger
94 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
95 The latest version can be downloaded from
98 \begin_inset LatexCommand \htmlurl{http://sdcc.sourceforge.net/}
108 All packages used in this compiler system are
112 (freeware); source code for all the sub-packages (asxxxx assembler/linker,
113 pre-processor) are distributed with the package.
114 This documentation is maintained using a freeware word processor (LyX).
118 This program is free software; you can redistribute it and/or modify it
119 under the terms of the GNU General Public License as published by the Free
120 Software Foundation; either version 2, or (at your option) any later version.
121 This program is distributed in the hope that it will be useful, but WITHOUT
122 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
123 FOR A PARTICULAR PURPOSE.
124 See the GNU General Public License for more details.
125 You should have received a copy of the GNU General Public License along
126 with this program; if not, write to the Free Software Foundation, 59 Temple
127 Place - Suite 330, Boston, MA 02111-1307, USA.
128 In other words, you are welcome to use, share and improve this program.
129 You are forbidden to forbid anyone else to use, share and improve what
131 Help stamp out software-hoarding!
134 Typographic conventions
137 Throughout this manual, we will use the following convention.
138 Commands you have to type in are printed in
146 Code samples are printed in
151 Interesting items and new terms are printed in
159 What do you need before you start installation of SDCC? A computer, and
161 The preferred method of installation is to compile SDCC from source using
163 For Windows some pre-compiled binary distributions are available for your
165 You should have some experience with command line tools and compiler use.
171 The SDCC home page at
172 \begin_inset LatexCommand \htmlurl{http://sdcc.sourceforge.net/}
176 is a great place to find distribution sets.
177 You can also find links to the user mailing lists that offer help or discuss
178 SDCC with other SDCC users.
179 Web links to other SDCC related sites can also be found here.
180 This document can be found in the DOC directory of the source package as
182 Some of the other tools (simulator and assembler) included with SDCC contain
183 their own documentation and can be found in the source distribution.
184 If you want the latest unreleased software, the complete source package
185 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
191 Linux/Unix Installation
196 Download the source package, it will be named something like sdcc-2.x.x.tgz.
201 Bring up a command line terminal, such as xterm.
206 Unpack the file using a command like:
209 "tar -xzf sdcc-2.x.x.tgz"
212 , this will create a sub-directory called sdcc with all of the sources.
215 Change directory into the main SDCC directory, for example type:
232 This configures the package for compilation on your system.
246 All of the source packages will compile, this can take a while.
262 This copies the binary executables to the install directories.
268 For installation under Windows you first need to pick between a pre-compiled
269 binary package, or installing the source package along with the Cygwin
271 The binary package is the quickest to install, while the Cygwin package
272 includes all of the open source power tools used to compile the complete
273 SDCC source package in the Windows environment.
274 If you are not familiar with the Unix command line environment, you may
275 want to read the section on additional information for Windows users prior
276 to your initial installation.
277 \layout Subsubsection
279 Windows Install Using a Binary Package
282 Download the binary package and unpack it using your favorite unpacking
283 tool (gunzip, WinZip, etc).
284 This should unpack to a group of sub-directories.
285 An example directory structure after unpacking is: c:
291 bin for the executables, c:
311 lib for the include and libraries.
314 Adjust your environment PATH to include the location of the bin directory.
315 For example, make a setsdcc.bat file with the following: set PATH=c:
324 When you compile with sdcc, you may need to specify the location of the
325 lib and include folders.
326 For example, sdcc -I c:
349 \layout Subsubsection
351 Windows Install Using Cygwin
356 Download and install the cygwin package from the redhat site
360 \begin_inset LatexCommand \htmlurl{http://sources.redhat.com/cygwin/}
368 Currently, this involved downloading a small install program which then
369 automates downloading and installing
375 (a large 80M byte sized dowload for the whole thing)
389 command line terminal from the Cygwin menu.
394 Follow the instructions in the preceding Linux/Unix installation section.
397 Testing out the SDCC Compiler
400 The first thing you should do after installing your SDCC compiler is to
408 at the prompt, and the program should run and tell you the version.
409 If it doesn't run, or gives a message about not finding sdcc program, then
410 you need to check over your installation.
411 Make sure that the sdcc bin directory is in your executable search path
412 defined by the PATH environment setting (see the Trouble-shooting section
414 Make sure that the sdcc program is in the bin folder, if not perhaps something
415 did not install correctly.
421 SDCC binaries are commonly installed in a directory arrangement like this:
430 <lyxtabular version="2" rows="3" columns="2">
431 <features rotate="false" islongtable="false" endhead="0" endfirsthead="0" endfoot="0" endlastfoot="0">
432 <column alignment="left" valignment="top" leftline="true" rightline="false" width="" special="">
433 <column alignment="left" valignment="top" leftline="true" rightline="true" width="" special="">
434 <row topline="true" bottomline="true" newpage="false">
435 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
445 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
452 Holds executables(sdcc, s51, aslink,
460 <row topline="true" bottomline="false" newpage="false">
461 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
468 usr/local/share/sdcc/lib
471 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
484 <row topline="true" bottomline="true" newpage="false">
485 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
492 usr/local/share/sdcc/include
495 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
502 Holds common C header files
516 Make sure the compiler works on a very simple example.
517 Type in the following test.c program using your favorite editor:
528 Compile this using the following command:
536 If all goes well, the compiler will generate a test.asm and test.rel file.
537 Congratulations, you've just compiled your first program with SDCC.
538 We used the -c option to tell SDCC not to link the generated code, just
539 to keep things simple for this step.
547 The next step is to try it with the linker.
555 If all goes well the compiler will link with the libraries and produce
556 a test.ihx output file.
561 (no test.ihx, and the linker generates warnings), then the problem is most
562 likely that sdcc cannot find the
566 usr/local/share/sdcc/lib directory
570 (see the Install trouble-shooting section for suggestions).
578 The final test is to ensure sdcc can use the
582 header files and libraries.
583 Edit test.c and change it to the following:
599 strcpy(str1, "testing");
611 Compile this by typing
618 This should generate a test.ihx output file, and it should give no warnings
619 such as not finding the string.h file.
620 If it cannot find the string.h file, then the problem is that sdcc cannot
621 find the /usr/local/share/sdcc/include directory
625 (see the Install trouble-shooting section for suggestions).
628 Install Trouble-shooting
629 \layout Subsubsection
631 SDCC cannot find libraries or header files.
634 The default installation assumes the libraries and header files are located
636 \begin_inset Quotes eld
639 /usr/local/share/sdcc/lib
640 \begin_inset Quotes erd
644 \begin_inset Quotes eld
647 /usr/local/share/sdcc/include
648 \begin_inset Quotes erd
652 An alternative is to specify these locations as compiler options like this:
656 "sdcc -L /usr/local/sdcc/lib/small -I /usr/local/sdcc/include test.c"
660 \layout Subsubsection
662 SDCC does not compile correctly.
665 A thing to try is starting from scratch by unpacking the .tgz source package
666 again in an empty directory.
667 Confure it again and build like:
674 make 2&>1 | tee make.log
681 After this you can review the make.log file to locate the problem.
682 Or a relevant part of this be attached to an email that could be helpful
683 when requesting help from the mailing list.
684 \layout Subsubsection
687 \begin_inset Quotes sld
691 \begin_inset Quotes srd
698 \begin_inset Quotes sld
702 \begin_inset Quotes srd
705 command is a script that analyzes your system and performs some configuration
706 to ensure the source package compiles on your system.
707 It will take a few minutes to run, and will compile a few tests to determine
708 what compiler features are installed.
709 \layout Subsubsection
712 \begin_inset Quotes sld
716 \begin_inset Quotes srd
722 This runs the GNU make tool, which automatically compiles all the source
723 packages into the final installed binary executables.
724 \layout Subsubsection
727 \begin_inset Quotes sld
731 \begin_inset Quotes erd
737 This will install the compiler, other executables and libraries in to the
738 appropriate system directories.
739 The default is to copy the executables to /usr/local/bin and the libraries
740 and header files to /usr/local/share/sdcc/lib and /usr/local/share/sdcc/include.
743 Additional Information for Windows Users
746 The standard method of installing on a Unix system involves compiling the
748 This is easily done under Unix, but under Windows it can be a more difficult
750 The Cygwin is a large package to download, and the compilation runs considerabl
751 y slower under Windows due to the overhead of the Cygwin tool set.
752 An alternative is to install a pre-compiled Windows binary package.
753 There are various trade-offs between each of these methods.
757 The Cygwin package allows a Windows user to run a Unix command line interface
758 (bash shell) and also implements a Unix like file system on top of Windows.
759 Included are many of the famous GNU software development tools which can
760 augment the SDCC compiler.This is great if you have some experience with
761 Unix command line tools and file system conventions, if not you may find
762 it easier to start by installing a binary Windows package.
763 The binary packages work with the Windows file system conventions.
764 \layout Subsubsection
766 Getting started with Cygwin
769 SDCC is typically distributed as a tarred/gzipped file (.tgz).
770 This is a packed file similar to a .zip file.
771 Cygwin includes the tools you will need to unpack the SDCC distribution
773 To unpack it, simply follow the instructions under the Linux/Unix install
775 Before you do this you need to learn how to start a cygwin shell and some
776 of the basic commands used to move files, change directory, run commands
778 The change directory command is
782 \begin_inset Quotes eld
786 \begin_inset Quotes erd
792 , the move command is
796 \begin_inset Quotes eld
800 \begin_inset Quotes erd
807 To print the current working directory, type
811 \begin_inset Quotes eld
815 \begin_inset Quotes erd
822 To make a directory, use
826 \begin_inset Quotes eld
830 \begin_inset Quotes erd
839 There are some basic differences between Unix and Windows file systems you
841 When you type in directory paths, Unix and the Cygwin bash prompt uses
842 forward slashes '/' between directories while Windows traditionally uses
846 So when you work at the Cygwin bash prompt, you will need to use the forward
848 Unix does not have a concept of drive letters, such as
849 \begin_inset Quotes eld
853 \begin_inset Quotes eld
856 , instead all files systems attach and appear as directories.
857 \layout Subsubsection
859 Running SDCC as Native Compiled Executables
862 If you use the pre-compiled binaries, the install directories for the libraries
863 and header files may need to be specified on the sdcc command line like
888 if you are running outside of a Unix bash shell.
891 If you have successfully installed and compiled SDCC with the Cygwin package,
892 it is possible to compile into native .exe files by using the additional
893 makefiles included for this purpose.
894 For example, with the Borland 32-bit compiler you would run
897 "make -f Makefile.bcc"
901 A command line version of the Borland 32-bit compiler can be downloaded
902 from the Inprise web site.
905 SDCC on Other Platforms
910 FreeBSD and other non-GNU Unixes
912 - Make sure the GNU make is installed as the default make tool.
915 SDCC has been ported to run under a variety of operating systems and processors.
916 If you can run GNU GCC/make then chances are good SDCC can be compiled
917 and run on your system.
920 Advanced Install Options
924 \begin_inset Quotes eld
928 \begin_inset Quotes erd
931 command has several options.
932 The most commonly used option is --prefix=<directory name>, where <directory
933 name> is the final location for the sdcc executables and libraries, (default
934 location is /usr/local).
935 The installation process will create the following directory structure
936 under the <directory name> specified (if they do not already exist).
942 bin/ - binary exectables (add to PATH environment variable)
948 bin/share/sdcc/include/ - include header files
954 bin/share/sdcc/lib/small/ - Object & library files for small model library
957 bin/share/sdcc/lib/large/ - Object & library files for large model library
960 bin/share/sdcc/lib/ds390/ - Object & library files forDS80C390 library
968 \begin_inset Quotes sld
971 ./configure --prefix=/usr/local
972 \begin_inset Quotes erd
978 will configure the compiler to be installed in directory /usr/local/bin.
984 SDCC is not just a compiler, but a collection of tools by various developers.
985 These include linkers, assemblers, simulators and other components.
986 Here is a summary of some of the components.
987 Note that the included simulator and assembler have separate documentation
988 which you can find in the source package in their respective directories.
989 As SDCC grows to include support for other processors, other packages from
990 various developers are included and may have their own sets of documentation.
993 You might want to look at the various executables which are installed in
995 At the time of this writing, we find the following programs:
1011 -The linker for 8051 type processors.
1018 - The assembler for 8051 type processors.
1025 - The C preprocessor.
1032 - The source debugger.
1039 - The ucSim 8051 simulator.
1044 link-z80, link-gbz80
1046 - The Z80 and GameBoy Z80 linkers.
1053 - The Z80 and GameBoy Z80 assemblers.
1060 - A tool to pack Intel hex files.
1064 As development for other processors proceeds, this list will expand to include
1065 executables to support processors like AVR, PIC, etc.
1066 \layout Subsubsection
1068 cpp ( C-Preprocessor)
1071 The preprocessor is a modified version of the GNU preprocessor.
1072 The C preprocessor is used to pull in #include sources, process #ifdef
1073 statements, #defines and so on.
1074 \layout Subsubsection
1076 asxxxx & aslink ( The Assembler and Linkage Editor)
1079 This is retargettable assembler & linkage editor, it was developed by Alan
1081 John Hartman created the version for 8051, and I (Sandeep) have made some
1082 enhancements and bug fixes for it to work properly with the SDCC.
1083 \layout Subsubsection
1088 This is the actual compiler, it in turn uses the c-preprocessor and invokes
1089 the assembler and linkage editor.
1090 \layout Subsubsection
1095 S51 is a freeware, opensource simulator developed by Daniel Drotos
1096 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
1101 The executable is built as part of the build process.
1102 For more information visit Daniel's website at
1103 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
1108 \layout Subsubsection
1110 sdcdb - Source Level Debugger
1113 Sdcdb is the companion source level debugger.
1114 The current version of the debugger uses Daniel's Simulator S51, but can
1115 be easily changed to use other simulators.
1122 \layout Subsubsection
1124 Single Source File Projects
1127 For single source file 8051 projects the process is very simple.
1128 Compile your programs with the following command
1131 "sdcc sourcefile.c".
1135 This will compile, assemble and link your source file.
1136 Output files are as follows.
1141 sourcefile.asm - Assembler source file created by the compiler
1146 sourcefile.lst - Assembler listing file created by the Assembler
1151 sourcefile.rst - Assembler listing file updated with linkedit information
1152 , created by linkage editor
1157 sourcefile.sym - symbol listing for the sourcefile, created by the assembler.
1162 sourcefile.rel - Object file created by the assembler, input to Linkage editor.
1167 sourcefile.map - The memory map for the load module, created by the Linker.
1172 sourcefile.ihx - The load module in Intel hex format (you can select the
1173 Motorola S19 format with --out-fmt-s19)
1176 sourcefile.cdb - An optional file (with --debug) containing debug information.
1177 \layout Subsubsection
1179 Projects with Multiple Source Files
1182 SDCC can compile only ONE file at a time.
1183 Let us for example assume that you have a project containing the following
1191 foo1.c ( contains some functions )
1196 foo2.c (contains some more functions)
1201 foomain.c (contains more functions and the function main)
1207 The first two files will need to be compiled separately with the commands
1232 Then compile the source file containing the main() function and link the
1233 files together with the following command:
1237 foomain.c\SpecialChar ~
1238 foo1.rel\SpecialChar ~
1243 Alternatively, foomain.c
1247 can be separately compiled as well:
1261 \begin_inset Quotes sld
1264 sdcc foomain.rel foo1.rel foo2.rel"
1271 The file containing the main function
1279 file specified in the command line, since the linkage editor processes
1280 file in the order they are presented to it.
1281 \layout Subsubsection
1283 Projects with Additional Libraries
1286 Some reusable routines may be compiled into a library, see the documentation
1287 for the assembler and linkage editor in the directory
1289 SDCCDIR/asxxxx/asxhtm.htm
1291 this describes how to create a
1295 library file, the libraries created in this manner may be included using
1296 the command line, make sure you include the -L <library-path> option to
1297 tell the linker where to look for these files.
1298 Here is an example, assuming you have the source file
1310 ' (if that is not the same as your current project).
1315 sdcc foomain.c foolib.lib -L mylib
1322 ' must be an absolute path name.
1325 The view of the way the linkage editor processes the library files, it is
1326 recommended that you put each source routine in a separate file and combine
1327 them using the .lib file.
1328 For an example see the standard library file 'libsdcc.lib' in the directory
1332 Command Line Options
1333 \layout Subsubsection
1335 Processor Selection Options
1337 \labelwidthstring 00.00.0000
1343 Generate code for the MCS51 (8051) family of processors.
1344 This is the default processor target.
1346 \labelwidthstring 00.00.0000
1352 Generate code for the DS80C390 processor.
1354 \labelwidthstring 00.00.0000
1360 Generate code for the Z80 family of processors.
1362 \labelwidthstring 00.00.0000
1368 Generate code for the GameBoy Z80 processor.
1370 \labelwidthstring 00.00.0000
1376 Generate code for the Atmel AVR processor(In development, not complete).
1378 \labelwidthstring 00.00.0000
1384 Generate code for the PIC 14-bit processors(In development, not complete).
1386 \labelwidthstring 00.00.0000
1392 Generate code for the Toshiba TLCS-900H processor(In development, not complete).
1393 \layout Subsubsection
1395 Preprocessor Options
1397 \labelwidthstring 00.00.0000
1403 The additional location where the pre processor will look for <..h> or
1404 \begin_inset Quotes eld
1408 \begin_inset Quotes erd
1413 \labelwidthstring 00.00.0000
1419 Command line definition of macros.
1420 Passed to the pre processor.
1422 \labelwidthstring 00.00.0000
1428 Tell the preprocessor to output a rule suitable for make describing the
1429 dependencies of each object file.
1430 For each source file, the preprocessor outputs one make-rule whose target
1431 is the object file name for that source file and whose dependencies are
1432 all the files `#include'd in it.
1433 This rule may be a single line or may be continued with `
1435 '-newline if it is long.
1436 The list of rules is printed on standard output instead of the preprocessed
1440 \labelwidthstring 00.00.0000
1446 Tell the preprocessor not to discard comments.
1447 Used with the `-E' option.
1449 \labelwidthstring 00.00.0000
1460 Like `-M' but the output mentions only the user header files included with
1462 \begin_inset Quotes eld
1466 System header files included with `#include <file>' are omitted.
1468 \labelwidthstring 00.00.0000
1474 Assert the answer answer for question, in case it is tested with a preprocessor
1475 conditional such as `#if #question(answer)'.
1476 `-A-' disables the standard assertions that normally describe the target
1479 \labelwidthstring 00.00.0000
1485 (answer) Assert the answer answer for question, in case it is tested with
1486 a preprocessor conditional such as `#if #question(answer)'.
1487 `-A-' disables the standard assertions that normally describe the target
1490 \labelwidthstring 00.00.0000
1496 Undefine macro macro.
1497 `-U' options are evaluated after all `-D' options, but before any `-include'
1498 and `-imacros' options.
1500 \labelwidthstring 00.00.0000
1506 Tell the preprocessor to output only a list of the macro definitions that
1507 are in effect at the end of preprocessing.
1508 Used with the `-E' option.
1510 \labelwidthstring 00.00.0000
1516 Tell the preprocessor to pass all macro definitions into the output, in
1517 their proper sequence in the rest of the output.
1519 \labelwidthstring 00.00.0000
1530 Like `-dD' except that the macro arguments and contents are omitted.
1531 Only `#define name' is included in the output.
1532 \layout Subsubsection
1536 \labelwidthstring 00.00.0000
1546 <absolute path to additional libraries> This option is passed to the linkage
1547 editor's additional libraries search path.
1548 The path name must be absolute.
1549 Additional library files may be specified in the command line.
1550 See section Compiling programs for more details.
1552 \labelwidthstring 00.00.0000
1558 <Value> The start location of the external ram, default value is 0.
1559 The value entered can be in Hexadecimal or Decimal format, e.g.: --xram-loc
1560 0x8000 or --xram-loc 32768.
1562 \labelwidthstring 00.00.0000
1568 <Value> The start location of the code segment , default value 0.
1569 Note when this option is used the interrupt vector table is also relocated
1570 to the given address.
1571 The value entered can be in Hexadecimal or Decimal format, e.g.: --code-loc
1572 0x8000 or --code-loc 32768.
1574 \labelwidthstring 00.00.0000
1580 <Value> The initial value of the stack pointer.
1581 The default value of the stack pointer is 0x07 if only register bank 0
1582 is used, if other register banks are used then the stack pointer is initialized
1583 to the location above the highest register bank used.
1585 if register banks 1 & 2 are used the stack pointer will default to location
1587 The value entered can be in Hexadecimal or Decimal format, eg.
1588 --stack-loc 0x20 or --stack-loc 32.
1589 If all four register banks are used the stack will be placed after the
1590 data segment (equivalent to --stack-after-data)
1592 \labelwidthstring 00.00.0000
1598 This option will cause the stack to be located in the internal ram after
1601 \labelwidthstring 00.00.0000
1607 <Value> The start location of the internal ram data segment, the default
1608 value is 0x30.The value entered can be in Hexadecimal or Decimal format,
1610 --data-loc 0x20 or --data-loc 32.
1612 \labelwidthstring 00.00.0000
1618 <Value> The start location of the indirectly addressable internal ram, default
1620 The value entered can be in Hexadecimal or Decimal format, eg.
1621 --idata-loc 0x88 or --idata-loc 136.
1623 \labelwidthstring 00.00.0000
1632 The linker output (final object code) is in Intel Hex format.
1633 (This is the default option).
1635 \labelwidthstring 00.00.0000
1644 The linker output (final object code) is in Motorola S19 format.
1645 \layout Subsubsection
1649 \labelwidthstring 00.00.0000
1655 Generate code for Large model programs see section Memory Models for more
1657 If this option is used all source files in the project should be compiled
1659 In addition the standard library routines are compiled with small model
1660 , they will need to be recompiled.
1662 \labelwidthstring 00.00.0000
1673 Generate code for Small Model programs see section Memory Models for more
1675 This is the default model.
1677 \labelwidthstring 00.00.0000
1683 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
1684 variables and passing parameters.
1685 See section on external stack for more details.
1686 \layout Subsubsection
1688 Optimization Options
1690 \labelwidthstring 00.00.0000
1696 Will not do global subexpression elimination, this option may be used when
1697 the compiler creates undesirably large stack/data spaces to store compiler
1699 A warning message will be generated when this happens and the compiler
1700 will indicate the number of extra bytes it allocated.
1701 It recommended that this option NOT be used , #pragma NOGCSE can be used
1702 to turn off global subexpression elimination for a given function only.
1704 \labelwidthstring 00.00.0000
1710 Will not do loop invariant optimizations, this may be turned off for reasons
1711 explained for the previous option.
1712 For more details of loop optimizations performed see section Loop Invariants.It
1713 recommended that this option NOT be used , #pragma NOINVARIANT can be used
1714 to turn off invariant optimizations for a given function only.
1716 \labelwidthstring 00.00.0000
1722 Will not do loop induction optimizations, see section Strength reduction
1723 for more details.It recommended that this option NOT be used , #pragma NOINDUCTI
1724 ON can be used to turn off induction optimizations for given function only.
1726 \labelwidthstring 00.00.0000
1737 Will not generate boundary condition check when switch statements are implement
1738 ed using jump-tables.
1739 See section Switch Statements for more details.It recommended that this
1740 option NOT be used , #pragma NOJTBOUND can be used to turn off boundary
1741 checking for jump tables for a given function only.
1743 \labelwidthstring 00.00.0000
1752 Will not do loop reversal optimization
1753 \layout Subsubsection
1757 \labelwidthstring 00.00.0000
1768 Generate 24-bit flat mode code.
1769 This is the one and only that the ds390 code generator supports right now
1770 and is default when using -mds390.
1771 See section Memory Models for more details.
1773 \labelwidthstring 00.00.0000
1779 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
1780 This is the one and only that the ds390 code generator supports right now
1781 and is default when using -mds390.
1782 In this mode, the stack is located in the lower 1K of the internal RAM,
1783 which is mapped to 0x400000.
1784 Note that the support is incomplete, since it still uses a single byte
1785 as the stack pointer.
1786 This means that only the lower 256 bytes of the potential 1K stack space
1787 will actually be used.
1788 However, this does allow you to reclaim the precious 256 bytes of low RAM
1789 for use for the DATA and IDATA segments.
1790 The compiler will not generate any code to put the processor into 10 bit
1792 It is important to ensure that the processor is in this mode before calling
1793 any re-entrant functions compiled with this option.
1794 In principle, this should work with the --stack-auto option, but that has
1796 It is incompatible with the --xstack option.
1797 It also only makes sense if the processor is in 24 bit contiguous addressing
1798 mode (see the --model-flat24 option).
1799 \layout Subsubsection
1803 \labelwidthstring 00.00.0000
1810 will compile and assemble the source, but will not call the linkage editor.
1812 \labelwidthstring 00.00.0000
1818 Run only the C preprocessor.
1819 Preprocess all the C source files specified and output the results to standard
1822 \labelwidthstring 00.00.0000
1833 All functions in the source file will be compiled as
1838 the parameters and local variables will be allocated on the stack.
1839 see section Parameters and Local Variables for more details.
1840 If this option is used all source files in the project should be compiled
1844 \labelwidthstring 00.00.0000
1848 --callee-saves function1[,function2][,function3]....
1851 The compiler by default uses a caller saves convention for register saving
1852 across function calls, however this can cause unneccessary register pushing
1853 & popping when calling small functions from larger functions.
1854 This option can be used to switch the register saving convention for the
1855 function names specified.
1856 The compiler will not save registers when calling these functions, no extra
1857 code will be generated at the entry & exit for these functions to save
1858 & restore the registers used by these functions, this can SUBSTANTIALLY
1859 reduce code & improve run time performance of the generated code.
1860 In the future the compiler (with interprocedural analysis) will be able
1861 to determine the appropriate scheme to use for each function call.
1862 DO NOT use this option for built-in functions such as _muluint..., if this
1863 option is used for a library function the appropriate library function
1864 needs to be recompiled with the same option.
1865 If the project consists of multiple source files then all the source file
1866 should be compiled with the same --callee-saves option string.
1867 Also see Pragma Directive CALLEE-SAVES.
1869 \labelwidthstring 00.00.0000
1878 When this option is used the compiler will generate debug information ,
1879 that can be used with the SDCDB.
1880 The debug information is collected in a file with .cdb extension.
1881 For more information see documentation for SDCDB.
1883 \labelwidthstring 00.00.0000
1893 This option is obsolete and isn't supported anymore.
1895 \labelwidthstring 00.00.0000
1902 This option is obsolete and isn't supported anymore.
1904 \labelwidthstring 00.00.0000
1910 <filename> This option can be used to use additional rules to be used by
1911 the peep hole optimizer.
1912 See section Peep Hole optimizations for details on how to write these rules.
1914 \labelwidthstring 00.00.0000
1925 Stop after the stage of compilation proper; do not assemble.
1926 The output is an assembler code file for the input file specified.
1928 \labelwidthstring 00.00.0000
1932 -Wa_asmOption[,asmOption]
1935 Pass the asmOption to the assembler.
1937 \labelwidthstring 00.00.0000
1941 -Wl_linkOption[,linkOption]
1944 Pass the linkOption to the linker.
1946 \labelwidthstring 00.00.0000
1955 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
1956 Note by default these libraries are compiled as non-reentrant.
1957 See section Installation for more details.
1959 \labelwidthstring 00.00.0000
1968 This option will cause the compiler to generate an information message for
1969 each function in the source file.
1970 The message contains some
1974 information about the function.
1975 The number of edges and nodes the compiler detected in the control flow
1976 graph of the function, and most importantly the
1978 cyclomatic complexity
1980 see section on Cyclomatic Complexity for more details.
1982 \labelwidthstring 00.00.0000
1991 Floating point library is compiled as reentrant.See section Installation
1994 \labelwidthstring 00.00.0000
2000 The compiler will not overlay parameters and local variables of any function,
2001 see section Parameters and local variables for more details.
2003 \labelwidthstring 00.00.0000
2009 This option can be used when the code generated is called by a monitor
2011 The compiler will generate a 'ret' upon return from the 'main' function.
2012 The default option is to lock up i.e.
2015 \labelwidthstring 00.00.0000
2021 Disable peep-hole optimization.
2023 \labelwidthstring 00.00.0000
2029 Pass the inline assembler code through the peep hole optimizer.
2030 This can cause unexpected changes to inline assembler code, please go through
2031 the peephole optimizer rules defined in the source file tree '<target>/peeph.def
2032 ' before using this option.
2034 \labelwidthstring 00.00.0000
2040 <Value> Causes the linker to check if the interal ram usage is within limits
2043 \labelwidthstring 00.00.0000
2049 This will prevent the compiler from passing on the default include path
2050 to the preprocessor.
2052 \labelwidthstring 00.00.0000
2058 This will prevent the compiler from passing on the default library path
2061 \labelwidthstring 00.00.0000
2067 Shows the various actions the compiler is performing.
2069 \labelwidthstring 00.00.0000
2075 Shows the actual commands the compiler is executing.
2076 \layout Subsubsection
2078 Intermediate Dump Options
2081 The following options are provided for the purpose of retargetting and debugging
2083 These provided a means to dump the intermediate code (iCode) generated
2084 by the compiler in human readable form at various stages of the compilation
2088 \labelwidthstring 00.00.0000
2094 This option will cause the compiler to dump the intermediate code into
2097 <source filename>.dumpraw
2099 just after the intermediate code has been generated for a function, i.e.
2100 before any optimizations are done.
2101 The basic blocks at this stage ordered in the depth first number, so they
2102 may not be in sequence of execution.
2104 \labelwidthstring 00.00.0000
2110 Will create a dump of iCode's, after global subexpression elimination,
2113 <source filename>.dumpgcse.
2115 \labelwidthstring 00.00.0000
2121 Will create a dump of iCode's, after deadcode elimination, into a file
2124 <source filename>.dumpdeadcode.
2126 \labelwidthstring 00.00.0000
2135 Will create a dump of iCode's, after loop optimizations, into a file named
2138 <source filename>.dumploop.
2140 \labelwidthstring 00.00.0000
2149 Will create a dump of iCode's, after live range analysis, into a file named
2152 <source filename>.dumprange.
2154 \labelwidthstring 00.00.0000
2160 Will dump the life ranges for all symbols.
2162 \labelwidthstring 00.00.0000
2171 Will create a dump of iCode's, after register assignment , into a file named
2174 <source filename>.dumprassgn.
2176 \labelwidthstring 00.00.0000
2182 Will create a dump of the live ranges of iTemp's
2184 \labelwidthstring 00.00.0000
2195 Will cause all the above mentioned dumps to be created.
2198 MCS51/DS390 Storage Class Language Extensions
2201 In addition to the ANSI storage classes SDCC allows the following MCS51
2202 specific storage classes.
2203 \layout Subsubsection
2208 Variables declared with this storage class will be placed in the extern
2214 storage class for Large Memory model, e.g.:
2220 xdata unsigned char xduc;
2221 \layout Subsubsection
2230 storage class for Small Memory model.
2231 Variables declared with this storage class will be allocated in the internal
2239 \layout Subsubsection
2244 Variables declared with this storage class will be allocated into the indirectly
2245 addressable portion of the internal ram of a 8051, e.g.:
2252 \layout Subsubsection
2257 This is a data-type and a storage class specifier.
2258 When a variable is declared as a bit , it is allocated into the bit addressable
2259 memory of 8051, e.g.:
2266 \layout Subsubsection
2271 Like the bit keyword,
2275 signifies both a data-type and storage class, they are used to describe
2276 the special function registers and special bit variables of a 8051, eg:
2282 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
2284 sbit at 0xd7 CY; /* CY (Carry Flag) */
2290 SDCC allows (via language extensions) pointers to explicitly point to any
2291 of the memory spaces of the 8051.
2292 In addition to the explicit pointers, the compiler also allows a
2296 class of pointers which can be used to point to any of the memory spaces.
2300 Pointer declaration examples.
2309 /* pointer physically in xternal ram pointing to object in internal ram
2312 data unsigned char * xdata p;
2316 /* pointer physically in code rom pointing to data in xdata space */
2318 xdata unsigned char * code p;
2322 /* pointer physically in code space pointing to data in code space */
2324 code unsigned char * code p;
2328 /* the folowing is a generic pointer physically located in xdata space */
2339 Well you get the idea.
2340 For compatibility with the previous version of the compiler, the following
2341 syntax for pointer declaration is still supported but will disappear int
2349 unsigned char _xdata *ucxdp; /* pointer to data in external ram */
2351 unsigned char _data \SpecialChar ~
2352 *ucdp ; /* pointer to data in internal ram */
2354 unsigned char _code \SpecialChar ~
2355 *uccp ; /* pointer to data in R/O code space */
2357 unsigned char _idata *uccp; \SpecialChar ~
2358 /* pointer to upper 128 bytes of ram */
2367 All unqualified pointers are treated as 3-byte (4-byte for the ds390) '_generic'
2369 These type of pointers can also to be explicitly declared.
2375 unsigned char _generic *ucgp;
2384 The highest order byte of the generic pointers contains the data space informati
2386 Assembler support routines are called whenever data is stored or retrieved
2387 using _generic pointers.
2388 These are useful for developing reusable library routines.
2389 Explicitly specifying the pointer type will generate the most efficient
2391 Pointers declared using a mixture of OLD/NEW style could have unpredictable
2395 Parameters & Local Variables
2398 Automatic (local) variables and parameters to functions can either be placed
2399 on the stack or in data-space.
2400 The default action of the compiler is to place these variables in the internal
2401 RAM ( for small model) or external RAM (for Large model).
2402 They can be placed on the stack either by using the
2406 compiler option or by using the 'reentrant' keyword in the function declaration
2416 unsigned char foo( char i) reentrant
2429 Note that when the parameters & local variables are declared in the internal/ext
2430 ernal ram the functions are non-reentrant.
2431 Since stack space on 8051 is limited the
2439 option should be used sparingly.
2440 Note the reentrant keyword just means that the parameters & local variables
2441 will be allocated to the stack, it DOES NOT mean that the function is register
2445 When compiled with the default option (i.e.
2446 non-reentrant ), local variables can be assigned storage classes and absolute
2447 addresses, e.g.: (jwk: pending: this is obsolete and need a rewrite)
2453 unsigned char foo() {
2458 xdata unsigned char i;
2468 data at 0x31 unsiged char j;
2479 In the above example the variable
2483 will be allocated in the external ram,
2487 in bit addressable space and
2492 When compiled with the
2496 or when a function is declared as
2500 local variables cannot be assigned storage classes or absolute addresses.
2503 Parameters however are not allowed any storage class, (storage classes for
2504 parameters will be ignored), their allocation is governed by the memory
2505 model in use , and the reentrancy options.
2511 For non-reentrant functions SDCC will try to reduce internal ram space usage
2512 by overlaying parameters and local variables of a function (if possible).
2513 Parameters and local variables of a function will be allocated to an overlayabl
2514 e segment if the function has
2516 no other function calls and the function is non-reentrant and the memory
2520 If an explicit storage class is specified for a local variable , it will
2524 Note that the compiler (not the linkage editor) makes the decision for overlayin
2526 Functions that are called from an interrupt service routine should be preceded
2527 by a #pragma NOOVERLAY if they are not reentrant Along the same lines the
2528 compiler does not do any processing with the inline assembler code so the
2529 compiler might incorrectly assign local variables and parameters of a function
2530 into the overlay segment if the only function call from a function is from
2531 inline assembler code, it is safe to use the #pragma NOOVERLAY for functions
2532 which call other functions using inline assembler code.
2535 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
2536 tion or division will NOT be overlayed since these are implemented using
2537 external functions, e.g.:
2547 void set_error( unsigned char errcd)
2563 void some_isr () interrupt 2 using 1
2589 In the above example the parameter
2597 would be assigned to the overlayable segment (if the #pragma NOOVERLAY
2598 was not present) , this could cause unpredictable runtime behavior when
2600 The pragma NOOVERLAY ensures that the parameters and local variables for
2601 the function are NOT overlayed.
2604 Interrupt Service Routines
2607 SDCC allows interrupt service routines to be coded in C, with some extended
2614 void timer_isr (void) interrupt 2 using 1
2627 The number following the 'interrupt' keyword is the interrupt number this
2628 routine will service.
2629 The compiler will insert a call to this routine in the interrupt vector
2630 table for the interrupt number specified.
2631 The 'using' keyword is used to tell the compiler to use the specified register
2632 bank (8051 specific) when generating code for this function.
2633 Note that when some function is called from an interrupt service routine
2634 it should be preceded by a #pragma NOOVERLAY (if it is not reentrant).
2635 A special note here, int (16 bit) and long (32 bit) integer division, multiplic
2636 ation & modulus operations are implemented using external support routines
2637 developed in ANSI-C, if an interrupt service routine needs to do any of
2638 these operations then the support routines (as mentioned in a following
2639 section) will have to recompiled using the --stack-auto option and the
2640 source file will need to be compiled using the --int-long-rent compiler
2644 If you have multiple source files in your project, interrupt service routines
2645 can be present in any of them, but a prototype of the isr MUST be present
2646 in the file that contains the function
2653 Interrupt Numbers and the corresponding address & descriptions for the Standard
2654 8051 are listed below.
2655 SDCC will automatically adjust the interrupt vector table to the maximum
2656 interrupt number specified.
2662 \begin_inset Tabular
2663 <lyxtabular version="2" rows="6" columns="3">
2664 <features rotate="false" islongtable="false" endhead="0" endfirsthead="0" endfoot="0" endlastfoot="0">
2665 <column alignment="center" valignment="top" leftline="true" rightline="false" width="" special="">
2666 <column alignment="center" valignment="top" leftline="true" rightline="false" width="" special="">
2667 <column alignment="center" valignment="top" leftline="true" rightline="true" width="" special="">
2668 <row topline="true" bottomline="true" newpage="false">
2669 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2677 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2685 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
2694 <row topline="true" bottomline="false" newpage="false">
2695 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2703 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2711 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
2720 <row topline="true" bottomline="false" newpage="false">
2721 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2729 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2737 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
2746 <row topline="true" bottomline="false" newpage="false">
2747 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2755 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2763 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
2772 <row topline="true" bottomline="false" newpage="false">
2773 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2781 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2789 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
2798 <row topline="true" bottomline="true" newpage="false">
2799 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2807 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="false" rotate="false" usebox="none" width="" special="">
2815 <cell multicolumn="0" alignment="center" valignment="top" topline="true" bottomline="false" leftline="true" rightline="true" rotate="false" usebox="none" width="" special="">
2833 If the interrupt service routine is defined without
2837 a register bank or with register bank 0 (using 0), the compiler will save
2838 the registers used by itself on the stack (upon entry and restore them
2839 at exit), however if such an interrupt service routine calls another function
2840 then the entire register bank will be saved on the stack.
2841 This scheme may be advantageous for small interrupt service routines which
2842 have low register usage.
2845 If the interrupt service routine is defined to be using a specific register
2847 \begin_inset Quotes eld
2851 \begin_inset Quotes erd
2855 \begin_inset Quotes erd
2859 \begin_inset Quotes erd
2863 \begin_inset Quotes eld
2867 \begin_inset Quotes erd
2870 are save and restored, if such an interrupt service routine calls another
2871 function (using another register bank) then the entire register bank of
2872 the called function will be saved on the stack.
2873 This scheme is recommended for larger interrupt service routines.
2876 Calling other functions from an interrupt service routine is not recommended
2877 avoid it if possible.
2883 A special keyword may be associated with a function declaring it as '
2888 SDCC will generate code to disable all interrupts upon entry to a critical
2889 function and enable them back before returning.
2890 Note that nesting critical functions may cause unpredictable results.
2916 The critical attribute maybe used with other attributes like
2924 A special keyword may be associated with a function declaring it as
2933 function modifier attribute prevents the compiler from generating prologue
2934 and epilogue code for that function.
2935 This means that the user is entirely responsible for such things as saving
2936 any registers that may need to be preserved, selecting the proper register
2937 bank, generating the
2941 instruction at the end, etc.
2942 Practically, this means that the contents of the function must be written
2943 in inline assembler.
2944 This is particularly useful for interrupt functions, which can have a large
2945 (and often unnecessary) prologue/epilogue.
2946 For example, compare the code generated by these two functions:
2952 data unsigned char counter;
2954 void simpleIterrupt(void) interrupt 1
2968 void nakedInterrupt(void) interrupt 2 _naked
3001 ; MUST explicitly include ret in _naked function.
3015 For an 8051 target, the generated simpleInterrupt looks like:
3160 whereas nakedInterrupt looks like:
3185 ; MUST explicitly include ret(i) in _naked function.
3191 While there is nothing preventing you from writing C code inside a _naked
3192 function, there are many ways to shoot yourself in the foot doing this,
3193 and is is recommended that you stick to inline assembler.
3196 Functions using private banks
3203 attribute (which tells the compiler to use a register bank other than the
3204 default bank zero) should only be applied to 'interrupt' functions (see
3206 This will in most circumstances make the generated ISR code more efficient
3207 since it will not have to save registers on the stack.
3214 attribute will have no effect on the generated code for a
3218 function (but may occasionally be useful anyway
3219 \begin_float footnote
3222 possible exception: if a function is called ONLY from 'interrupt' functions
3223 using a particular bank, it can be declared with the same 'using' attribute
3224 as the calling 'interrupt' functions.
3225 For instance, if you have several ISRs using bank one, and all of them
3226 call memcpy(), it might make sense to create a specialized version of memcpy()
3227 'using 1', since this would prevent the ISR from having to save bank zero
3228 to the stack on entry and switch to bank zero before calling the function
3232 (jwk: todo: I don't think this has been done yet)
3235 An 'interrupt' function using a non-zero bank will assume that it can trash
3236 that register bank, and will not save it.
3237 Since high-priority interrupts can interrupt low-priority ones on the 8051
3238 and friends, this means that if a high-priority ISR 'using' a particular
3239 bank occurs while processing a low-priority ISR 'using' the same bank,
3240 terrible and bad things can happen.
3241 To prevent this, no single register bank should be 'used' by both a high
3242 priority and a low priority ISR.
3243 This is probably most easily done by having all high priority ISRs use
3244 one bank and all low priority ISRs use another.
3245 If you have an ISR which can change priority at runtime, you're on your
3246 own: I suggest using the default bank zero and taking the small performance
3250 It is most efficient if your ISR calls no other functions.
3251 If your ISR must call other functions, it is most efficient if those functions
3252 use the same bank as the ISR (see note A below); the next best is if the
3253 called functions use bank zero.
3254 It is very inefficient to call a function using a different, non-zero bank
3262 Data items can be assigned an absolute address with the
3266 keyword, in addition to a storage class, e.g.:
3272 xdata at 0x8000 unsigned char PORTA_8255 ;
3278 In the above example the
3282 will be allocated to the location 0x8000 of the external ram.
3283 Note that this feature is provided to give the programmer access to
3287 devices attached to the controller.
3288 The compiler does not actually reserve any space for variables declared
3289 in this way (they are implemented with an equate in the assembler), thus
3290 it is left to the programmer to make sure there are no overlaps with other
3291 variables that are declared without the absolute address, the assembler
3292 listing file (.lst) and the linker output files (<filename>.rst) and (<filename>.m
3293 ap) are a good places to look for such overlaps.
3297 Absolute address can be specified for variables in all storage classes,
3310 The above example will allocate the variable at offset 0x02 in the bit-addressab
3312 There is no real advantage to assigning absolute addresses to variables
3313 in this manner , unless you want strict control over all the variables
3320 The compiler inserts a jump to the C routine
3322 _sdcc__external__startup()
3324 at the start of the CODE area.
3325 This routine can be found in the file
3327 SDCCDIR/sdcc51lib/_startup.c
3329 , by default this routine returns 0, if this routine returns a non-zero
3330 value , the static & global variable initialization will be skipped and
3331 the function main will be invoked, other wise static & global variables
3332 will be initialized before the function main is invoked.
3335 _sdcc__external__startup()
3337 routine to your program to override the default if you needed to setup
3338 hardware or perform some other critical operation prior to static & global
3339 variable initialization.
3342 Inline Assembler Code
3345 SDCC allows the use of in-line assembler with a few restriction as regards
3347 All labels defined within inline assembler code HAS TO BE of the form
3351 where nnnn is a number less than 100 (which implies a limit of utmost 100
3352 inline assembler labels
3357 It is strongly recommended that each assembly instruction (including labels)
3358 be placed in a separate line (as the example shows).
3363 command line option is used, the inline assembler code will be passed through
3364 the peephole optimizer, this might cause some unexpected changes in the
3365 inline assembler code.
3366 Please go throught the peephole optimizer rules defined in file 'SDCCpeeph.def'
3367 carefully before using this option.
3394 The inline assembler code can contain any valid code understood by the assembler
3395 (this includes any assembler directives and comment lines).
3396 The compiler does not do any validation of the code within the
3406 Inline assembler code cannot reference any C-Labels, however it can reference
3407 labels defined by the inline assembler, e.g.:
3433 ; some assembler code
3453 /* some more c code */
3455 clabel:\SpecialChar ~
3457 /* inline assembler cannot reference this label */
3469 $0003: ;label (can be reference by inline assembler only)
3481 /* some more c code */
3489 In other words inline assembly code can access labels defined in inline
3491 The same goes the other way, ie.
3492 labels defines in inline assembly CANNOT be accessed by C statements.
3495 int(16 bit) and long (32 bit ) Support
3498 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
3499 multiplication and modulus operations are implemented by support routines.
3500 These support routines are all developed in ANSI-C to facilitate porting
3502 The following files contain the described routine, all of them can be found
3503 in the directory default SDCC library path.
3508 _mulsint.c - signed 16 bit multiplication (calls _muluint)
3513 _muluint.c - unsigned 16 bit multiplication
3518 _divsint.c - signed 16 bit division (calls _divuint)
3523 _divuint.c - unsigned 16 bit division.
3528 _modsint.c - signed 16 bit modulus (call _moduint)
3533 _moduint.c - unsigned 16 bit modulus.
3538 _mulslong.c - signed 32 bit multiplication (calls _mululong)
3543 _mululong.c - unsigned32 bit multiplication.
3548 _divslong.c - signed 32 division (calls _divulong)
3553 _divulong.c - unsigned 32 division.
3558 _modslong.c - signed 32 bit modulus (calls _modulong).
3563 _modulong.c - unsigned 32 bit modulus.
3566 Since they are compiled as non-reentrant, interrupt service routines should
3567 not do any of the above operations.
3568 If this unavoidable then the above routines will need to be compiled with
3569 the --stack-auto option, after which the source program will have to be
3570 compiled with --int-long-rent option.
3573 Floating Point Support
3576 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
3577 point support routines are derived from gcc's floatlib.c and consists of
3578 the following routines.
3584 _fsadd.c - add floating point numbers.
3589 _fssub.c - subtract floating point numbers
3594 _fsdiv.c - divide floating point numbers
3599 _fsmul.c - multiply floating point numbers
3604 _fs2uchar.c - convert floating point to unsigned char
3609 _fs2char.c - convert floating point to signed char.
3614 _fs2uint.c - convert floating point to unsigned int.
3619 _fs2int.c - convert floating point to signed int.
3624 _fs2ulong.c - convert floating point to unsigned long.
3629 _fs2long.c - convert floating point to signed long.
3634 _uchar2fs.c - convert unsigned char to floating point
3639 _char2fs.c - convert char to floating point number
3644 _uint2fs.c - convert unsigned int to floating point
3649 _int2fs.c - convert int to floating point numbers
3654 _ulong2fs.c - convert unsigned long to floating point number
3659 _long2fs.c - convert long to floating point number.
3662 Note if all these routines are used simultaneously the data space might
3664 For serious floating point usage it is strongly recommended that the Large
3665 model be used (in which case the floating point routines mentioned above
3666 will need to recompiled with the --model-large option)
3672 SDCC allows two memory models for MCS51 code, small and large.
3673 Modules compiled with different memory models should
3677 be combined together or the results would be unpredictable.
3678 The library routines supplied with the compiler are compiled as both small
3680 The compiled library modules are contained in seperate directories as small
3681 and large so that you can link to either set.
3685 When the large model is used all variables declared without a storage class
3686 will be allocated into the external ram, this includes all parameters and
3687 local variables (for non-reentrant functions).
3688 When the small model is used variables without storage class are allocated
3689 in the internal ram.
3692 Judicious usage of the processor specific storage classes and the 'reentrant'
3693 function type will yield much more efficient code, than using the large
3695 Several optimizations are disabled when the program is compiled using the
3696 large model, it is therefore strongly recommdended that the small model
3697 be used unless absolutely required.
3703 The only model supported is Flat 24.
3704 This generates code for the 24 bit contiguous addressing mode of the Dallas
3706 In this mode, up to four meg of external RAM or code space can be directly
3708 See the data sheets at www.dalsemi.com for further information on this part.
3712 In older versions of the compiler, this option was used with the MCS51 code
3713 generator (-mmcs51).
3714 Now, however, the '390 has it's own code generator, selected by the -mds390
3720 Note that the compiler does not generate any code to place the processor
3721 into 24 bitmode (although the tinibios in the ds390 libraries will do that
3723 If you don't use tinibios, the boot loader or similar code must ensure
3724 that the processor is in 24 bit contiguous addressing mode before calling
3725 the SDCC startup code.
3729 Like the --model-large option, variables will by default be placed into
3735 Segments may be placed anywhere in the 4 meg address space using the usual
3737 Note that if any segments are located above 64K, the -r flag must be passed
3738 to the linker to generate the proper segment relocations, and the Intel
3739 HEX output format must be used.
3740 The -r flag can be passed to the linker by using the option -Wl-r on the
3742 However, currently the linker can not handle code segments > 64k.
3745 Defines Created by the Compiler
3748 The compiler creates the following #defines.
3751 SDCC - this Symbol is always defined.
3754 SDCC_mcs51 or SDCC_ds390 or SDCC_z80, etc - depending on the model used
3758 __mcs51 or __ds390 or __z80, etc - depending on the model used (e.g.
3762 SDCC_STACK_AUTO - this symbol is defined when --stack-auto option is used.
3765 SDCC_MODEL_SMALL - when small model is used.
3768 SDCC_MODEL_LARGE - when --model-large is used.
3771 SDCC_USE_XSTACK - when --xstack option is used.
3774 SDCC_STACK_TENBIT - when -mds390 is used
3777 SDCC_MODEL_FLAT24 - when -mds390 is used
3786 SDCC performs a a host of standard optimizations in addition to some MCU
3787 specific optimizations.
3789 \layout Subsubsection
3791 Sub-expression Elimination
3798 common subexpression elimination.
3819 will be translated to
3831 Some subexpressions are not as obvious as the above example.
3844 In this case the address arithmetic
3848 will be computed only once; the equivalent code in C would be.
3860 The compiler will try to keep these temporary variables in registers.
3861 \layout Subsubsection
3863 Dead-Code Elimination
3881 i = 1; \SpecialChar ~
3888 global = 1; /* dead store */
3900 global = 3; /* unreachable */
3910 int global; void f ()
3917 global = 2; \SpecialChar ~
3925 \layout Subsubsection
3986 Note: the dead stores created by this copy propagation will be eliminated
3987 by dead-code elimination.
3988 \layout Subsubsection
3993 Two types of loop optimizations are done by SDCC loop invariant lifting
3994 and strength reduction of loop induction variables.In addition to the strength
3995 reduction the optimizer marks the induction variables and the register
3996 allocator tries to keep the induction variables in registers for the duration
3998 Because of this preference of the register allocator , loop induction optimizat
3999 ion causes an increase in register pressure, which may cause unwanted spilling
4000 of other temporary variables into the stack / data space.
4001 The compiler will generate a warning message when it is forced to allocate
4002 extra space either on the stack or data space.
4003 If this extra space allocation is undesirable then induction optimization
4004 can be eliminated either for the entire source file ( with --noinduction
4005 option) or for a given function only (#pragma NOINDUCTION).
4018 for (i = 0 ; i < 100 ; i ++)
4033 for ( i = 0; i < 100; i++ ) f += itemp;
4036 As mentioned previously some loop invariants are not as apparent, all static
4037 address computations are also moved out of the loop.
4042 Strength Reduction :
4045 This optimization substitutes an expression by a cheaper expression.
4053 for (i=0;i < 100; i++) ar[i*5] = i*3;
4065 for (i=0;i< 100;i++) {
4070 ar[itemp1] = itemp2;
4085 The more expensive multiplication is changed to a less expensive addition.
4086 \layout Subsubsection
4091 This optimization is done to reduce the overhead of checking loop boundaries
4092 for every iteration.
4093 Some simple loops can be reversed and implemented using a
4094 \begin_inset Quotes eld
4097 decrement and jump if not zero
4098 \begin_inset Quotes erd
4102 SDCC checks for the following criterion to determine if a loop is reversible
4103 (note: more sophisticated compiers use data-dependency analysis to make
4104 this determination, SDCC uses a more simple minded analysis).
4107 The 'for' loop is of the form
4110 \begin_inset Quotes eld
4113 for ( <symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
4124 \begin_inset Quotes erd
4130 The <for body> does not contain
4131 \begin_inset Quotes eld
4135 \begin_inset Quotes erd
4139 \begin_inset Quotes erd
4145 All goto's are contained within the loop.
4148 No function calls within the loop.
4151 The loop control variable <sym> is not assigned any value within the loop
4154 The loop control variable does NOT participate in any arithmetic operation
4158 There are NO switch statements in the loop.
4161 Note djnz instruction can be used for 8-bit values ONLY, therefore it is
4162 advantageous to declare loop control symbols as either 'char', ofcourse
4163 this may not be possible on all situations.
4164 \layout Subsubsection
4166 Algebraic Simplifications
4169 SDCC does numerous algebraic simplifications, the following is a small sub-set
4170 of these optimizations.
4181 i = j + 0 ; /* changed to */ i = j;
4183 i /= 2; /* changed to */ i >>= 1;
4185 i = j - j ; /* changed to */ i = 0;
4187 i = j / 1 ; /* changed to */ i = j;
4190 Note the subexpressions given above are generally introduced by macro expansions
4191 or as a result of copy/constant propagation.
4192 \layout Subsubsection
4197 SDCC changes switch statements to jump tables when the following conditions
4202 The case labels are in numerical sequence , the labels need not be in order,
4203 and the starting number need not be one or zero.
4211 switch(i) {\SpecialChar ~
4315 Both the above switch statements will be implemented using a jump-table.
4318 The number of case labels is at least three, since it takes two conditional
4319 statements to handle the boundary conditions.
4322 The number of case labels is less than 84, since each label takes 3 bytes
4323 and a jump-table can be utmost 256 bytes long.
4327 Switch statements which have gaps in the numeric sequence or those that
4328 have more that 84 case labels can be split into more than one switch statement
4329 for efficient code generation.
4366 If the above switch statement is broken down into two switch statements
4407 then both the switch statements will be implemented using jump-tables whereas
4408 the unmodified switch statement will not be.
4409 \layout Subsubsection
4411 Bit-shifting Operations.
4414 Bit shifting is one of the most frequently used operation in embedded programmin
4416 SDCC tries to implement bit-shift operations in the most efficient way
4438 generates the following code.
4452 In general SDCC will never setup a loop if the shift count is known.
4484 Note that SDCC stores numbers in
4490 \layout Subsubsection
4495 A special case of the bit-shift operation is bit rotation, SDCC recognizes
4496 the following expression to be a left bit-rotation.
4506 i = ( ( i << 1) | ( i >> 7));
4511 will generate the following code.
4523 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
4524 ns of this case will also be recognized as bit-rotation i.e
4526 i = ((i >> 7) | (i << 1));
4528 /* left-bit rotation */
4529 \layout Subsubsection
4534 It is frequently required to obtain the highest order bit of an integral
4535 type (long, int, short or char types).
4536 SDCC recognizes the following expression to yield the highest order bit
4537 and generates optimized code for it.
4559 hob = (gint >> 15) & 1;
4570 Will generate the following code.
4608 000A E5*01\SpecialChar ~
4636 000C 33\SpecialChar ~
4667 000D E4\SpecialChar ~
4698 000E 13\SpecialChar ~
4729 000F F5*02\SpecialChar ~
4756 Variations of this case however will NOT be recognized.
4757 It is a standard C expression , so I heartily recommend this be the only
4758 way to get the highest order bit, (it is portable).
4759 Of course it will be recognized even if it is embedded in other expressions.
4769 xyz = gint + ((gint >> 15) & 1);
4772 will still be recognized.
4773 \layout Subsubsection
4778 The compiler uses a rule based , pattern matching and re-writing mechanism
4779 for peep-hole optimization.
4784 a peep-hole optimizer by Christopher W.
4785 Fraser (cwfraser@microsoft.com).
4786 A default set of rules are compiled into the compiler, additional rules
4787 may be added with the --peep-file <filename> option.
4788 The rule language is best illustrated with examples.
4797 mov a,%1 } by { mov %1,a }
4800 The above rule will the following assembly sequence
4818 Note: All occurrences of a '%n' ( pattern variable ) must denote the same
4820 With the above rule, the assembly sequence
4830 will remain unmodified.
4831 Other special case optimizations may be added by the user (via --peep-file
4833 some variants of the 8051 MCU allow only 'AJMP' and 'ACALL' , the following
4834 two rules will change all 'LJMP' & 'LCALL' to 'AJMP' & 'ACALL'.
4839 replace { lcall %1 } by { acall %1 }
4841 replace { ljmp %1 } by { ajmp %1 }
4844 The inline-assembler' code is also passed through the peep hole optimizer,
4845 thus the peephole optimizer can also be used as an assembly level macro
4847 The rules themselves are MCU dependent whereas the rule language infra-structur
4848 e is MCU independent.
4849 Peephole optimization rules for other MCU can be easily programmed using
4853 The syntax for a rule is as follows ,
4858 rule := replace [ restart ] '{' <assembly sequence> '
4896 <assembly sequence> '
4914 '}' [if <functionName> ] '
4918 <assembly sequence> := assembly instruction (each instruction including
4919 labels must be on a separate line).\SpecialChar ~
4924 The optimizer will apply to the rules one by one from the top in the sequence
4925 of their appearance, it will terminate when all rules are exhausted.
4930 ' option is specified, then the optimizer will start matching the rules
4931 again from the top, this option for a rule is expensive (performance),
4932 it is intended to be used in situations where a transformation will trigger
4933 the same rule again.
4934 A good example of this the following rule.
4950 Note that the replace pattern cannot be a blank, but can be a comment line.
4955 ' option only the inner most 'pop' 'push' pair would be eliminated.
4986 ' option the rule will be applied again to the resulting code and the all
4991 pairs will be eliminated to yield
5001 A conditional function can be attached to a rule.
5002 Attaching rules are somewhat more involved, let me illustrate this with
5022 %2:} if labelInRange
5025 The optimizer does a look-up of a function name table defined in function
5038 ', if it finds a corresponding entry the function is called.
5039 Note there can be no parameters specified for these functions, in this
5044 ' is crucial, since the function
5048 expects to find the label in that particular variable (the hash table containin
5049 g the variable bindings is passed as a parameter).
5050 If you want to code more such functions , take a close look at the function
5055 and the calling mechanism in source file
5060 I know this whole thing is a little kludgey , may be some day we will have
5062 If you are looking at this file, you will also see the default rules that
5063 are compiled into the compiler, you can your own rules in the default set
5064 there if you get tired of specifying the
5074 SDCC supports the following
5079 This directives are applicable only at a function level.
5086 - this will save all the current options.
5093 - will restore the saved options from the last save.
5094 Note that SAVES & RESTOREs cannot be nested.
5095 SDCC uses the same buffer to save the options each time a SAVE is called.
5102 - will stop global subexpression elimination.
5109 - will stop loop induction optimizations.
5116 - will not generate code for boundary value checking , when switch statements
5117 are turned into jump-tables.
5124 - the compiler will not overlay the parameters and local variables of a
5132 - Will not do loop reversal optimization
5137 EXCLUDE NONE | {acc[,b[,dpl[,dph]]]
5139 - The exclude pragma disables generation of pair of push/pop instruction
5140 in ISR function (using interrupt keyword).
5141 The directive should be placed immediately before the ISR function definition
5142 and it affects ALL ISR functions following it.
5143 To enable the normal register saving for ISR functions use
5144 \begin_inset Quotes eld
5147 #pragma EXCLUDE none
5148 \begin_inset Quotes erd
5156 CALLEE-SAVES function1[,function2[,function3...]]
5158 - The compiler by default uses a caller saves convention for register saving
5159 across function calls, however this can cause unneccessary register pushing
5160 & popping when calling small functions from larger functions.
5161 This option can be used to switch the register saving convention for the
5162 function names specified.
5163 The compiler will not save registers when calling these functions, extra
5164 code will be generated at the entry & exit for these functions to save
5165 & restore the registers used by these functions, this can SUBSTANTIALLY
5166 reduce code & improve run time performance of the generated code.
5167 In future the compiler (with interprocedural analysis) will be able to
5168 determine the appropriate scheme to use for each function call.
5169 If --callee-saves command line option is used, the function names specified
5170 in #pragma CALLEE-SAVES is appended to the list of functions specified
5174 The pragma's are intended to be used to turn-off certain optimizations which
5175 might cause the compiler to generate extra stack / data space to store
5176 compiler generated temporary variables.
5177 This usually happens in large functions.
5178 Pragma directives should be used as shown in the following example, they
5179 are used to control options & optimizations for a given function; pragmas
5188 a function, placing pragma's inside a function body could have unpredictable
5199 #pragma SAVE \SpecialChar ~
5200 /* save the current settings */
5202 #pragma NOGCSE /* turnoff global subexpression elimination */
5204 #pragma NOINDUCTION /* turn off induction optimizations */
5226 #pragma RESTORE /* turn the optimizations back on */
5229 The compiler will generate a warning message when extra space is allocated.
5230 It is strongly recommended that the SAVE and RESTORE pragma's be used when
5231 changing options for a function.
5237 The following library routines are provided for your convenience.
5246 - Contains the following functions printf & sprintf these routines are developed
5249 Martijn van Balen <balen@natlab.research.philips.com>.
5255 %[flags][width][b|B|l|L]type
5270 flags: -\SpecialChar ~
5277 left justify output in specified field width
5302 prefix output with +/- sign if output is signed type
5323 prefix output with a blank if it's a signed positive value
5334 width:\SpecialChar ~
5343 specifies minimum number of characters outputted for numbers
5398 - For numbers, spaces are added on the left when needed.
5428 If width starts with a zero character, zeroes and used
5485 - For strings, spaces are are added on the left or right (when
5514 flag '-' is used) when needed.
5564 byte argument (used by d, u, o, x, X)
5586 long argument (used by d, u, o, x, X)
5630 unsigned decimal number
5655 unsigned octal number
5680 unsigned hexadecimal number (0-9, a-f)
5705 unsigned hexadecimal number (0-9, A-F)
5755 string (generic pointer)
5780 generic pointer (I:data/idata, C:code, X:xdata, P:paged)
5805 float (still to be implemented)
5808 Also contains a very simple version of printf (
5813 This simplified version of printf supports only the following formats.
5819 format\SpecialChar ~
5824 output\SpecialChar ~
5848 decimal \SpecialChar ~
5863 decimal\SpecialChar ~
5880 decimal\SpecialChar ~
5897 hexadecimal\SpecialChar ~
5910 hexadecimal\SpecialChar ~
5923 hexadecimal\SpecialChar ~
5995 character\SpecialChar ~
6011 character\SpecialChar ~
6023 , --stack-after-data parameter should be used when using this routine, the
6024 routine also takes about 1K of code space.
6025 It also expects an external function named
6029 to be present (this can be changed).
6030 When using the %s format the string / pointer should be cast to a generic
6038 \begin_inset Quotes eld
6041 my str %s, my int %d
6044 \begin_inset Quotes erd
6047 ,(char _generic *)mystr,myint);
6056 - contains definition for the following macros to be used for variable parameter
6057 list, note that a function can have a variable parameter list if and only
6058 if it is 'reentrant'
6064 va_list, va_start, va_arg, va_end.
6074 - contains defintion for ANSI
6083 Note in this case setjmp & longjmp can be used between functions executing
6084 within the same register bank, if long jmp is executed from a function
6085 that is using a different register bank from the function issuing the setjmp
6086 function, the results may be unpredictable.
6087 The jump buffer requires 3 bytes of data (the stack pointer & a 16 byte
6088 return address), and can be placed in any address space.
6097 - contains the following functions.
6113 - contains the following functions.
6119 strcpy, strncpy, strcat, strncat, strcmp, strncmp, strchr, strrchr, strspn,
6120 strcspn, strpbrk, strstr, strlen, strtok, memcpy, memcmp, memset.
6130 - contains the following routines.
6136 iscntrl, isdigit, isgraph, islower, isupper, isprint, ispunct, isspace,
6137 isxdigit, isalnum, isalpha.
6147 - The malloc routines are developed by Dmitry S.
6148 Obukhov (dso@usa.net).
6149 These routines will allocate memory from the external ram.
6150 Here is a description on how to use them (as described by the author).
6166 #define DYNAMIC_MEMORY_SIZE 0x2000
6187 unsigned char xdata dynamic_memory_pool[DYNAMIC_MEMORY_SIZE];
6197 unsigned char xdata * current_buffer;
6258 init_dynamic_memory(dynamic_memory_pool,DYNAMIC_MEMORY_SIZE);
6272 //Now it's possible to use malloc.
6302 current_buffer = malloc(0x100);
6318 - Serial IO routines are also developed by Dmitry S.
6319 Obukhov (dso@usa.net).
6320 These routines are interrupt driven with a 256 byte circular buffer, they
6321 also expect external ram to be present.
6322 Please see documentation in file SDCCDIR/sdcc51lib/serial.c.
6323 Note the header file
6324 \begin_inset Quotes eld
6328 \begin_inset Quotes erd
6331 MUST be included in the file containing the 'main' function.
6340 - Alternate serial routine provided by Wolfgang Esslinger <wolfgang@WiredMinds.co
6341 m> these routines are more compact and faster.
6342 Please see documentation in file SDCCDIR/sdcc51lib/ser.c
6351 - Another alternate set of serial routines provided by Josef Wolf <jw@raven.inka.d
6352 e> , these routines do not use the external ram.
6361 - contains register definitions for a standard 8051
6370 - contains min, max and other floating point related stuff.
6373 All library routines are compiled as --model-small , they are all non-reentrant,
6374 if you plan to use the large model or want to make these routines reentrant,
6375 then they will have to be recompiled with the appropriate compiler option.
6378 Have not had time to do the more involved routines like printf, will get
6382 Interfacing with Assembly Routines
6385 Global Registers used for Parameter Passing
6388 By default the compiler uses the global registers
6389 \begin_inset Quotes eld
6393 \begin_inset Quotes erd
6396 to pass the first parameter to a routine, the second parameter onwards
6397 is either allocated on the stack (for reentrant routines or --stack-auto
6398 is used) or in the internal / external ram (depending on the memory model).
6400 \layout Subsubsection
6402 Assembler Routine(non-reentrant)
6405 In the following example the function
6409 calls an assembler routine
6413 , which takes two parameters.
6418 extern int asm_func( unsigned char, unsigned char);
6426 int c_func (unsigned char i, unsigned char j)
6437 return asm_func(i,j);
6452 return c_func(10,9);
6457 The corresponding assembler function is:-
6469 .globl _asm_func_PARM_2
6489 _asm_func_PARM_2:\SpecialChar ~
6575 Note here that the return values are placed in 'dpl' - One byte return value,
6576 'dpl' LSB & 'dph' MSB for two byte values.
6577 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
6578 b' & 'acc' for four byte values.
6581 The parameter naming convention is
6583 _<function_name>_PARM_<n>,
6585 where n is the parameter number starting from 1, and counting from the
6587 The first parameter is passed in
6588 \begin_inset Quotes eld
6592 \begin_inset Quotes erd
6595 for One bye parameter,
6596 \begin_inset Quotes eld
6600 \begin_inset Quotes erd
6604 \begin_inset Quotes eld
6608 \begin_inset Quotes erd
6612 \begin_inset Quotes eld
6616 \begin_inset Quotes erd
6623 varaible name for the second parameter will be _<function_name>_PARM_2.
6626 Assemble the assembler routine with the following command.
6629 asx8051 -losg asmfunc.asm
6632 Then compile and link the assembler routine to the C source file with the
6636 sdcc cfunc.c asmfunc.rel
6637 \layout Subsubsection
6639 Assembler Routine(reentrant)
6642 In this case the second parameter onwards will be passed on the stack ,
6643 the parameters are pushed from right to left i.e.
6644 after the call the left most parameter will be on the top of the stack.
6650 extern int asm_func( unsigned char, unsigned char);
6661 int c_func (unsigned char i, unsigned char j) reentrant
6672 return asm_func(i,j);
6687 return c_func(10,9);
6692 The corresponding assembler routine is.
6869 The compiling and linking procedure remains the same, however note the extra
6870 entry & exit linkage required for the assembler code, _bp is the stack
6871 frame pointer and is used to compute the offset into the stack for parameters
6872 and local variables.
6878 The external stack is located at the start of the external ram segment ,
6879 and is 256 bytes in size.
6880 When --xstack option is used to compile the program, the parameters and
6881 local variables of all reentrant functions are allocated in this area.
6882 This option is provided for programs with large stack space requirements.
6883 When used with the --stack-auto option, all parameters and local variables
6884 are allocated on the external stack (note support libraries will need to
6885 be recompiled with the same options).
6888 The compiler outputs the higher order address byte of the external ram segment
6889 into PORT P2, therefore when using the External Stack option, this port
6890 MAY NOT be used by the application program.
6896 Deviations from the compliancy.
6899 functions are not always reentrant.
6902 structures cannot be assigned values directly, cannot be passed as function
6903 parameters or assigned to each other and cannot be a return value from
6928 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
6938 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
6948 return rets;/* is invalid in SDCC although allowed in ANSI */
6953 'long long' (64 bit integers) not supported.
6956 'double' precision floating point not supported.
6959 integral promotions are suppressed.
6960 What does this mean ? The compiler will not implicitly promote an integer
6961 expression to a higher order integer, exception is an assignment or parameter
6977 Old K&R style function declarations are NOT allowed.
6982 foo( i,j) /* this old style of function declarations */
6984 int i,j; /* are valid in ANSI ..
6985 not valid in SDCC */
6995 functions declared as pointers must be dereferenced during the call.
7014 /* has to be called like this */
7018 (*foo)();/* ansi standard allows calls to be made like 'foo()' */
7021 Cyclomatic Complexity
7024 Cyclomatic complexity of a function is defined as the number of independent
7025 paths the program can take during execution of the function.
7026 This is an important number since it defines the number test cases you
7027 have to generate to validate the function.
7028 The accepted industry standard for complexity number is 10, if the cyclomatic
7029 complexity reported by SDCC exceeds 10 you should think about simplification
7030 of the function logic.
7033 Note that the complexity level is not related to the number of lines of
7035 Large functions can have low complexity, and small functions can have large
7037 SDCC uses the following formula to compute the complexity.
7042 complexity = (number of edges in control flow graph) -
7051 (number of nodes in control flow graph) + 2;
7054 Having said that the industry standard is 10, you should be aware that in
7055 some cases it may unavoidable to have a complexity level of less than 10.
7056 For example if you have switch statement with more than 10 case labels,
7057 each case label adds one to the complexity level.
7058 The complexity level is by no means an absolute measure of the algorithmic
7059 complexity of the function, it does however provide a good starting point
7060 for which functions you might look at for further optimization.
7066 Here are a few guide-lines that will help the compiler generate more efficient
7067 code, some of the tips are specific to this compiler others are generally
7068 good programming practice.
7071 Use the smallest data type to represent your data-value.
7072 If it is known in advance that the value is going to be less than 256 then
7073 use a 'char' instead of a 'short' or 'int'.
7076 Use unsigned when it is known in advance that the value is not going to
7078 This helps especially if you are doing division or multiplication.
7081 NEVER jump into a LOOP.
7084 Declare the variables to be local whenever possible, especially loop control
7085 variables (induction).
7088 Since the compiler does not do implicit integral promotion, the programmer
7089 should do an explicit cast when integral promotion is required.
7092 Reducing the size of division , multiplication & modulus operations can
7093 reduce code size substantially.
7094 Take the following code for example.
7100 foobar( unsigned int p1, unsigned char ch)
7108 unsigned char ch1 = p1 % ch ;
7123 For the modulus operation the variable ch will be promoted to unsigned int
7124 first then the modulus operation will be performed (this will lead to a
7125 call to a support routine).
7126 If the code is changed to
7131 foobar( unsigned int p1, unsigned char ch)
7139 unsigned char ch1 = (unsigned char)p1 % ch ;
7154 It would substantially reduce the code generated (future versions of the
7155 compiler will be smart enough to detect such optimization oppurtunities).
7161 Notes on MCS51 memory layout(Trefor@magera.freeserve.co.uk)
7164 The 8051 family of micro controller have a minimum of 128 bytes of internal
7165 memory which is structured as follows
7168 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
7172 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
7175 - Bytes 30-7F - 60 bytes for general purpose use.
7178 Normally the SDCC compiler will only utilise the first bank of registers,
7179 but it is possible to specify that other banks of registers should be used
7180 in interrupt routines.
7181 By default, the compiler will place the stack after the last bank of used
7183 if the first 2 banks of registers are used, it will position the base of
7184 the internal stack at address 16 (0X10).
7185 This implies that as the stack grows, it will use up the remaining register
7186 banks, and the 16 bytes used by the 128 bit variables, and 60 bytes for
7187 general purpose use.
7190 By default, the compiler uses the 60 general purpose bytes to hold "near
7192 The compiler/optimiser may also declare some Local Variables in this area
7197 If any of the 128 bit variables are used, or near data is being used then
7198 care needs to be taken to ensure that the stack does not grow so much that
7199 it starts to over write either your bit variables or "near data".
7200 There is no runtime checking to prevent this from happening.
7203 The amount of stack being used is affected by the use of the "internal stack"
7204 to save registers before a subroutine call is made, - --stack-auto will
7205 declare parameters and local variables on the stack - the number of nested
7209 If you detect that the stack is over writing you data, then the following
7211 --xstack will cause an external stack to be used for saving registers and
7212 (if --stack-auto is being used) storing parameters and local variables.
7213 However this will produce more and code which will be slower to execute.
7217 --stack-loc will allow you specify the start of the stack, i.e.
7218 you could start it after any data in the general purpose area.
7219 However this may waste the memory not used by the register banks and if
7220 the size of the "near data" increases, it may creep into the bottom of
7224 --stack-after-data, similar to the --stack-loc, but it automatically places
7225 the stack after the end of the "near data".
7226 Again this could waste any spare register space.
7229 --data-loc allows you to specify the start address of the near data.
7230 This could be used to move the "near data" further away from the stack
7231 giving it more room to grow.
7232 This will only work if no bit variables are being used and the stack can
7233 grow to use the bit variable space.
7239 If you find that the stack is over writing your bit variables or "near data"
7240 then the approach which best utilised the internal memory is to position
7241 the "near data" after the last bank of used registers or, if you use bit
7242 variables, after the last bit variable by using the --data-loc, e.g.
7243 if two register banks are being used and no data variables, --data-loc
7244 16, and - use the --stack-after-data option.
7247 If bit variables are being used, another method would be to try and squeeze
7248 the data area in the unused register banks if it will fit, and start the
7249 stack after the last bit variable.
7252 Retargetting for other MCUs.
7255 The issues for retargetting the compiler are far too numerous to be covered
7257 What follows is a brief description of each of the seven phases of the
7258 compiler and its MCU dependency.
7261 Parsing the source and building the annotated parse tree.
7262 This phase is largely MCU independent (except for the language extensions).
7263 Syntax & semantic checks are also done in this phase , along with some
7264 initial optimizations like back patching labels and the pattern matching
7265 optimizations like bit-rotation etc.
7268 The second phase involves generating an intermediate code which can be easy
7269 manipulated during the later phases.
7270 This phase is entirely MCU independent.
7271 The intermediate code generation assumes the target machine has unlimited
7272 number of registers, and designates them with the name iTemp.
7273 The compiler can be made to dump a human readable form of the code generated
7274 by using the --dumpraw option.
7277 This phase does the bulk of the standard optimizations and is also MCU independe
7279 This phase can be broken down into several sub-phases.
7283 Break down intermediate code (iCode) into basic blocks.
7286 Do control flow & data flow analysis on the basic blocks.
7289 Do local common subexpression elimination, then global subexpression elimination
7292 dead code elimination
7298 if loop optimizations caused any changes then do 'global subexpression eliminati
7299 on' and 'dead code elimination' again.
7303 This phase determines the live-ranges; by live range I mean those iTemp
7304 variables defined by the compiler that still survive after all the optimization
7306 Live range analysis is essential for register allocation, since these computati
7307 on determines which of these iTemps will be assigned to registers, and for
7311 Phase five is register allocation.
7312 There are two parts to this process.
7316 The first part I call 'register packing' (for lack of a better term).
7317 In this case several MCU specific expression folding is done to reduce
7321 The second part is more MCU independent and deals with allocating registers
7322 to the remaining live ranges.
7323 A lot of MCU specific code does creep into this phase because of the limited
7324 number of index registers available in the 8051.
7328 The Code generation phase is (unhappily), entirely MCU dependent and very
7329 little (if any at all) of this code can be reused for other MCU.
7330 However the scheme for allocating a homogenized assembler operand for each
7331 iCode operand may be reused.
7334 As mentioned in the optimization section the peep-hole optimizer is rule
7335 based system, which can reprogrammed for other MCUs.
7338 SDCDB - Source Level Debugger
7341 SDCC is distributed with a source level debugger.
7342 The debugger uses a command line interface, the command repertoire of the
7343 debugger has been kept as close to gdb ( the GNU debugger) as possible.
7344 The configuration and build process is part of the standard compiler installati
7345 on, which also builds and installs the debugger in the target directory
7346 specified during configuration.
7347 The debugger allows you debug BOTH at the C source and at the ASM source
7351 Compiling for Debugging
7358 option must be specified for all files for which debug information is to
7360 The complier generates a
7364 file for each of these files.
7365 The linker updates the
7369 file with the address information.
7370 This .cdb is used by the debugger.
7373 How the Debugger Works
7380 option is specified the compiler generates extra symbol information some
7381 of which are put into the the assembler source and some are put into the
7382 .cdb file, the linker updates the .cdb file with the address information
7384 The debugger reads the symbolic information generated by the compiler &
7385 the address information generated by the linker.
7386 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
7387 execution is controlled by the debugger.
7388 When a command is issued for the debugger, it translates it into appropriate
7389 commands for the simulator.
7392 Starting the Debugger
7395 The debugger can be started using the following command line.
7396 (Assume the file you are debugging has
7405 The debugger will look for the following files.
7408 foo.c - the source file.
7411 foo.cdb - the debugger symbol information file.
7414 foo.ihx - the intel hex format object file.
7417 Command Line Options.
7420 --directory=<source file directory> this option can used to specify the
7421 directory search list.
7422 The debugger will look into the directory list specified for source , cdb
7424 The items in the directory list must be separated by ':' , e.g.
7425 if the source files can be in the directories /home/src1 and /home/src2,
7426 the --directory option should be --directory=/home/src1:/home/src2.
7427 Note there can be no spaces in the option.
7431 -cd <directory> - change to the <directory>.
7434 -fullname - used by GUI front ends.
7437 -cpu <cpu-type> - this argument is passed to the simulator please see the
7438 simulator docs for details.
7441 -X <Clock frequency > this options is passed to the simulator please see
7442 simulator docs for details.
7445 -s <serial port file> passed to simulator see simulator docs for details.
7448 -S <serial in,out> passed to simulator see simulator docs for details.
7454 As mention earlier the command interface for the debugger has been deliberately
7455 kept as close the GNU debugger gdb , as possible, this will help int integratio
7456 n with existing graphical user interfaces (like ddd, xxgdb or xemacs) existing
7457 for the GNU debugger.
7458 \layout Subsubsection
7460 break [line | file:line | function | file:function]
7463 Set breakpoint at specified line or function.
7468 sdcdb>break foo.c:100
7472 sdcdb>break foo.c:funcfoo
7473 \layout Subsubsection
7475 clear [line | file:line | function | file:function ]
7478 Clear breakpoint at specified line or function.
7483 sdcdb>clear foo.c:100
7487 sdcdb>clear foo.c:funcfoo
7488 \layout Subsubsection
7493 Continue program being debugged, after breakpoint.
7494 \layout Subsubsection
7499 Execute till the end of the current function.
7500 \layout Subsubsection
7505 Delete breakpoint number 'n'.
7506 If used without any option clear ALL user defined break points.
7507 \layout Subsubsection
7509 info [break | stack | frame | registers ]
7512 info break - list all breakpoints
7515 info stack - show the function call stack.
7518 info frame - show information about the current execution frame.
7521 info registers - show content of all registers.
7522 \layout Subsubsection
7527 Step program until it reaches a different source line.
7528 \layout Subsubsection
7533 Step program, proceeding through subroutine calls.
7534 \layout Subsubsection
7539 Start debugged program.
7540 \layout Subsubsection
7545 Print type information of the variable.
7546 \layout Subsubsection
7551 print value of variable.
7552 \layout Subsubsection
7557 load the given file name.
7558 Note this is an alternate method of loading file for debugging.
7559 \layout Subsubsection
7564 print information about current frame.
7565 \layout Subsubsection
7570 Toggle between C source & assembly source.
7571 \layout Subsubsection
7576 Send the string following '!' to the simulator, the simulator response is
7578 Note the debugger does not interpret the command being sent to the simulator,
7579 so if a command like 'go' is sent the debugger can loose its execution
7580 context and may display incorrect values.
7581 \layout Subsubsection
7588 My name is Bobby Brown"
7591 Interfacing with XEmacs.
7594 Two files are (in emacs lisp) are provided for the interfacing with XEmacs,
7603 These two files can be found in the $(prefix)/bin directory after the installat
7605 These files need to be loaded into XEmacs for the interface to work, this
7606 can be done at XEmacs startup time by inserting the following into your
7611 file (which can be found in your HOME directory)
7613 (load-file sdcdbsrc.el)
7615 [ .xemacs is a lisp file so the () around the command is REQUIRED), the files
7616 can also be loaded dynamically while XEmacs is running, set the environment
7621 to the installation bin directory [$(prefix)/bin], then enter the following
7624 ESC-x load-file sdcdbsrc.
7627 To start the interface enter the following command
7631 , you will prompted to enter the file name to be debugged.
7635 The command line options that are passed to the simulator directly are bound
7636 to default values in the file
7640 the variables are listed below these values maybe changed as required.
7643 sdcdbsrc-cpu-type '51
7646 sdcdbsrc-frequency '11059200
7652 The following is a list of key mapping for the debugger interface.
7660 ;; Current Listing ::
7677 binding\SpecialChar ~
7716 -------\SpecialChar ~
7756 sdcdb-next-from-src\SpecialChar ~
7782 sdcdb-back-from-src\SpecialChar ~
7808 sdcdb-cont-from-src\SpecialChar ~
7818 SDCDB continue command
7834 sdcdb-step-from-src\SpecialChar ~
7860 sdcdb-whatis-c-sexp\SpecialChar ~
7870 SDCDB ptypecommand for data at
7930 sdcdbsrc-delete\SpecialChar ~
7944 SDCDB Delete all breakpoints if no arg
7992 given or delete arg (C-u arg x)
8008 sdcdbsrc-frame\SpecialChar ~
8023 SDCDB Display current frame if no arg,
8071 given or display frame arg
8134 sdcdbsrc-goto-sdcdb\SpecialChar ~
8144 Goto the SDCDB output buffer
8160 sdcdb-print-c-sexp\SpecialChar ~
8171 SDCDB print command for data at
8231 sdcdbsrc-goto-sdcdb\SpecialChar ~
8241 Goto the SDCDB output buffer
8257 sdcdbsrc-mode\SpecialChar ~
8273 Toggles Sdcdbsrc mode (turns it off)
8277 ;; C-c C-f\SpecialChar ~
8285 sdcdb-finish-from-src\SpecialChar ~
8293 SDCDB finish command
8297 ;; C-x SPC\SpecialChar ~
8305 sdcdb-break\SpecialChar ~
8323 Set break for line with point
8325 ;; ESC t\SpecialChar ~
8335 sdcdbsrc-mode\SpecialChar ~
8351 Toggle Sdcdbsrc mode
8353 ;; ESC m\SpecialChar ~
8363 sdcdbsrc-srcmode\SpecialChar ~
8385 The Z80 and gbz80 port
8388 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
8389 The port is incomplete - long support is incomplete (mul, div and mod are
8390 unimplimented), and both float and bitfield support is missing, but apart
8391 from that the code generated is correct.
8394 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
8395 The stack frame is similar to that generated by the IAR Z80 compiler.
8396 IX is used as the base pointer, HL is used as a temporary register, and
8397 BC and DE are available for holding varibles.
8398 IY is currently unusued.
8399 Return values are stored in HL.
8400 One bad side effect of using IX as the base pointer is that a functions
8401 stack frame is limited to 127 bytes - this will be fixed in a later version.
8407 SDCC has grown to be large project, the compiler alone (without the Assembler
8408 Package, Preprocessor) is about 40,000 lines of code (blank stripped).
8409 The open source nature of this project is a key to its continued growth
8411 You gain the benefit and support of many active software developers and
8413 Is SDCC perfect? No, that's why we need your help.
8414 The developers take pride in fixing reported bugs.
8415 You can help by reporting the bugs and helping other SDCC users.
8416 There are lots of ways to contribute, and we encourage you to take part
8417 in making SDCC a great software package.
8423 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
8424 cc@sdcc.sourceforge.net'.
8425 Bugs will be fixed ASAP.
8426 When reporting a bug, it is very useful to include a small test program
8427 which reproduces the problem.
8428 If you can isolate the problem by looking at the generated assembly code,
8429 this can be very helpful.
8430 Compiling your program with the --dumpall option can sometimes be useful
8431 in locating optimization problems.
8437 Sandeep Dutta(sandeep.dutta@usa.net) - SDCC, the compiler, MCS51 code generator,
8440 Alan Baldwin (baldwin@shop-pdp.kent.edu) - Initial version of ASXXXX & ASLINK.
8443 John Hartman (jhartman@compuserve.com) - Porting ASXXX & ASLINK for 8051
8446 Obukhov (dso@usa.net) - malloc & serial i/o routines.
8449 Daniel Drotos <drdani@mazsola.iit.uni-miskolc.hu> - for his Freeware simulator
8451 Malini Dutta(malini_dutta@hotmail.com) - my wife for her patience and support.
8453 Unknown - for the GNU C - preprocessor.
8455 Michael Hope - The Z80 and Z80GB port, 186 development
8457 Kevin Vigor - The DS390 port.
8459 Johan Knol - DS390/TINI libs, lots of fixes and enhancements.
8461 Scott Datallo - PIC port.
8463 (Thanks to all the other volunteer developers who have helped with coding,
8464 testing, web-page creation, distribution sets, etc.
8465 You know who you are :-)
8470 This document initially written by Sandeep Dutta
8473 All product names mentioned herein may be trademarks of their respective
8479 \begin_inset LatexCommand \index{}