Added Sandeep's anatomy lecture
[fw/sdcc] / doc / sdccman.lyx
1 #LyX 1.2 created this file. For more info see http://www.lyx.org/
2 \lyxformat 220
3 \textclass article
4 \language english
5 \inputencoding default
6 \fontscheme pslatex
7 \graphics default
8 \paperfontsize default
9 \spacing single 
10 \papersize Default
11 \paperpackage a4
12 \use_geometry 0
13 \use_amsmath 0
14 \use_natbib 0
15 \use_numerical_citations 0
16 \paperorientation portrait
17 \secnumdepth 3
18 \tocdepth 3
19 \paragraph_separation indent
20 \defskip medskip
21 \quotes_language swedish
22 \quotes_times 2
23 \papercolumns 1
24 \papersides 1
25 \paperpagestyle fancy
26
27 \layout Title
28
29
30 \begin_inset ERT
31 status Open
32
33 \layout Standard
34
35 \backslash 
36 NotSpecial{
37 \backslash 
38 do
39 \backslash 
40 -}
41 \end_inset 
42
43 SDCC Compiler User Guide
44 \layout Standard
45
46
47 \begin_inset LatexCommand \tableofcontents{}
48
49 \end_inset 
50
51
52 \layout Section
53
54 Introduction
55 \layout Subsection
56
57 About SDCC
58 \layout Standard
59
60
61 \series bold 
62 SDCC
63 \series default 
64  is a Freeware, retargettable, optimizing ANSI-C compiler by 
65 \series bold 
66 Sandeep Dutta
67 \series default 
68  designed for 8 bit Microprocessors.
69  The current version targets Intel MCS51 based Microprocessors(8051,8052,
70  etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
71  It can be retargetted for other microprocessors, support for PIC, AVR and
72  186 is under development.
73  The entire source code for the compiler is distributed under GPL.
74  SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
75  SDCC has extensive language extensions suitable for utilizing various microcont
76 rollers and underlying hardware effectively.
77  
78 \newline 
79
80 \newline 
81 In addition to the MCU specific optimizations SDCC also does a host of standard
82  optimizations like:
83 \layout Itemize
84
85 global sub expression elimination, 
86 \layout Itemize
87
88 loop optimizations (loop invariant, strength reduction of induction variables
89  and loop reversing), 
90 \layout Itemize
91
92 constant folding & propagation, 
93 \layout Itemize
94
95 copy propagation, 
96 \layout Itemize
97
98 dead code elimination 
99 \layout Itemize
100
101 jumptables for 
102 \emph on 
103 switch
104 \emph default 
105  statements.
106 \layout Standard
107
108 For the back-end SDCC uses a global register allocation scheme which should
109  be well suited for other 8 bit MCUs.
110  
111 \newline 
112
113 \newline 
114 The peep hole optimizer uses a rule based substitution mechanism which is
115  MCU independent.
116  
117 \newline 
118
119 \newline 
120 Supported data-types are:
121 \layout Itemize
122
123 char (8 bits, 1 byte), 
124 \layout Itemize
125
126 short and int (16 bits, 2 bytes), 
127 \layout Itemize
128
129 long (32 bit, 4 bytes)
130 \layout Itemize
131
132 float (4 byte IEEE).
133  
134 \layout Standard
135
136 The compiler also allows 
137 \emph on 
138 inline assembler code
139 \emph default 
140  to be embedded anywhere in a function.
141  In addition, routines developed in assembly can also be called.
142 \newline 
143
144 \newline 
145 SDCC also provides an option (---cyclomatic) to report the relative complexity
146  of a function.
147  These functions can then be further optimized, or hand coded in assembly
148  if needed.
149  
150 \newline 
151
152 \newline 
153 SDCC also comes with a companion source level debugger SDCDB, the debugger
154  currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
155  
156 \newline 
157
158 \newline 
159 The latest version can be downloaded from 
160 \begin_inset LatexCommand \htmlurl{http://sdcc.sourceforge.net/}
161
162 \end_inset 
163
164
165 \series bold 
166 .
167 \layout Subsection
168
169 Open Source
170 \layout Standard
171
172 All packages used in this compiler system are 
173 \emph on 
174 opensource
175 \emph default 
176  and 
177 \emph on 
178 freeware
179 \emph default 
180 ; source code for all the sub-packages (pre-processor, assemblers, linkers
181  etc) is distributed with the package.
182  This documentation is maintained using a freeware word processor (LyX).
183 \newline 
184 This program is free software; you can redistribute it and/or modify it
185  under the terms of the GNU General Public License as published by the Free
186  Software Foundation; either version 2, or (at your option) any later version.
187  This program is distributed in the hope that it will be useful, but WITHOUT
188  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
189  FOR A PARTICULAR PURPOSE.
190  See the GNU General Public License for more details.
191  You should have received a copy of the GNU General Public License along
192  with this program; if not, write to the Free Software Foundation, 59 Temple
193  Place - Suite 330, Boston, MA 02111-1307, USA.
194  In other words, you are welcome to use, share and improve this program.
195  You are forbidden to forbid anyone else to use, share and improve what
196  you give them.
197  Help stamp out software-hoarding! 
198 \layout Subsection
199
200 Typographic conventions
201 \layout Standard
202
203 Throughout this manual, we will use the following convention.
204  Commands you have to type in are printed in 
205 \family sans 
206 \series bold 
207 "sans serif"
208 \series default 
209 .
210
211 \family default 
212  Code samples are printed in 
213 \family typewriter 
214 typewriter font.
215
216 \family default 
217  Interesting items and new terms are printed in 
218 \emph on 
219 italic.
220 \layout Subsection
221
222 Compatibility with previous versions
223 \layout Standard
224
225 This version has numerous bug fixes compared with the previous version.
226  But we also introduced some incompatibilities with older versions.
227  Not just for the fun of it, but to make the compiler more stable, efficient
228  and ANSI compliant.
229  
230 \newline 
231
232 \layout Itemize
233
234 short is now equivalent to int (16 bits), it used to be equivalent to char
235  (8 bits) which is not ANSI compliant
236 \layout Itemize
237
238 the default directory where include, library and documention files are stored
239  is now in /usr/local/share
240 \layout Itemize
241
242 char type parameters to vararg functions are casted to int unless explicitly
243  casted, e.g.: 
244 \newline 
245
246 \family typewriter 
247 \SpecialChar ~
248 \SpecialChar ~
249 char a=3;
250 \newline 
251 \SpecialChar ~
252 \SpecialChar ~
253 printf ("%d %c
254 \backslash 
255 n", a, (char)a);
256 \family default 
257
258 \newline 
259  will push a as an int and as a char resp.
260 \layout Itemize
261
262 option 
263 \family typewriter 
264 --
265 \family default 
266 regextend has been removed
267 \layout Itemize
268
269 option --noregparms has been removed
270 \layout Itemize
271
272 option --stack-after-data has been removed
273 \layout Standard
274
275
276 \emph on 
277 <pending: more incompatibilities?>
278 \layout Subsection
279
280 System Requirements
281 \layout Standard
282
283 What do you need before you start installation of SDCC? A computer, and
284  a desire to compute.
285  The preferred method of installation is to compile SDCC from source using
286  GNU gcc and make.
287  For Windows some pre-compiled binary distributions are available for your
288  convenience.
289  You should have some experience with command line tools and compiler use.
290 \layout Subsection
291
292 Other Resources
293 \layout Standard
294
295 The SDCC home page at 
296 \begin_inset LatexCommand \htmlurl{http://sdcc.sourceforge.net/}
297
298 \end_inset 
299
300  is a great place to find distribution sets.
301  You can also find links to the user mailing lists that offer help or discuss
302  SDCC with other SDCC users.
303  Web links to other SDCC related sites can also be found here.
304  This document can be found in the DOC directory of the source package as
305  a text or HTML file.
306  Some of the other tools (simulator and assembler) included with SDCC contain
307  their own documentation and can be found in the source distribution.
308  If you want the latest unreleased software, the complete source package
309  is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
310 \layout Subsection
311
312 Wishes for the future
313 \layout Standard
314
315 There are (and always will be) some things that could be done.
316  Here are some I can think of:
317 \newline 
318
319 \layout Standard
320
321
322 \family typewriter 
323 char KernelFunction3(char p) at 0x340;
324 \newline 
325
326 \newline 
327
328 \family default 
329 If you can think of some more, please send them to the list.
330 \newline 
331
332 \newline 
333
334 \emph on 
335 <pending: And then of course a proper index-table
336 \begin_inset LatexCommand \index{index}
337
338 \end_inset 
339
340 >
341 \layout Section
342
343 Installation
344 \layout Subsection
345
346 Install and search paths
347 \layout Standard
348
349 Linux (and other gcc-builds like Solaris, Cygwin, Mingw and OSX) by default
350  install in /usr/local.
351  You can override this when configuring with --prefix-path.
352  Subdirs used will be bin, share/sdcc/include, share/sdcc/lib and share/sdcc/doc.
353 \newline 
354 Windows MSVC and Borland builds will install in one single tree (e.g.
355  /sdcc) with subdirs bin, lib, include and doc.
356 \newline 
357
358 \newline 
359 The paths searched when running the compiler are as follows (the first catch
360  wins):
361 \layout Enumerate
362
363 Binary files (preprocessor, assembler and linker):
364 \newline 
365 - the path of argv[0] (if available)
366 \newline 
367 - getenv(
368 \begin_inset Quotes sld
369 \end_inset 
370
371 SDCC_BIN_PATH
372 \begin_inset Quotes srd
373 \end_inset 
374
375 )
376 \newline 
377 - getenv(
378 \begin_inset Quotes sld
379 \end_inset 
380
381 SDCCHOME
382 \begin_inset Quotes srd
383 \end_inset 
384
385 )/bin
386 \newline 
387 - $PATH
388 \layout Enumerate
389
390 Include files:
391 \newline 
392 - -I dir
393 \newline 
394 - getenv(
395 \begin_inset Quotes sld
396 \end_inset 
397
398 SDCC_INCLUDE_PATH
399 \begin_inset Quotes srd
400 \end_inset 
401
402 )
403 \newline 
404 - getenv(
405 \begin_inset Quotes sld
406 \end_inset 
407
408 SDCCHOME)/include
409 \newline 
410 - /usr/local/share/sdcc/include (gcc builds)
411 \newline 
412 - path(arv[0])/../include and then /sdcc/include (windoze msvc and borland
413  builds)
414 \layout Enumerate
415
416 Library files (the 
417 \shape italic 
418 model
419 \shape default 
420  is auto-appended by the compiler, e.g.
421  small, large, z80, ds390 etc.):
422 \newline 
423 - -L dir
424 \newline 
425 - getenv(
426 \begin_inset Quotes sld
427 \end_inset 
428
429 SDCC_LIB_PATH
430 \begin_inset Quotes srd
431 \end_inset 
432
433 )/
434 \shape italic 
435 model
436 \shape default 
437
438 \newline 
439 - getenv(
440 \begin_inset Quotes sld
441 \end_inset 
442
443 SDCCHOME
444 \begin_inset Quotes srd
445 \end_inset 
446
447 )/lib/
448 \shape italic 
449 model
450 \shape default 
451
452 \newline 
453 - /usr/local/share/sdcc/lib/
454 \shape italic 
455 model
456 \shape default 
457  (gcc builds)
458 \newline 
459 - path(argv[0])/../lib/
460 \shape italic 
461 model
462 \shape default 
463  and then /sdcc/lib/
464 \shape italic 
465 model
466 \shape default 
467  (windoze msvc and borland builds)
468 \layout Enumerate
469
470 Documentation (although never really searched for, you have to do that yourself
471  :):
472 \newline 
473 - getenv(
474 \begin_inset Quotes sld
475 \end_inset 
476
477 SDCCHOME
478 \begin_inset Quotes srd
479 \end_inset 
480
481 )/doc
482 \newline 
483 - /usr/local/share/sdcc/doc (gcc builds)
484 \newline 
485 - /sdcc/doc (windoze msvc and borland builds)
486 \layout Standard
487
488 So, for windoze it is highly recommended to set the environment variable
489  SDCCHOME to prevent needless usage of -I and -L.
490 \layout Subsection
491
492 Linux and other gcc-based systems (cygwin, mingw, osx)
493 \layout Enumerate
494
495
496 \series medium 
497 Download the source package
498 \series default 
499  either from the SDCC CVS repository or from the 
500 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
501
502 \end_inset 
503
504
505 \series medium 
506 , it will be named something like sdcc
507 \series default 
508 .src
509 \series medium 
510 .tgz.
511 \layout Enumerate
512
513
514 \series medium 
515 Bring up a command line terminal, such as xterm.
516 \layout Enumerate
517
518
519 \series medium 
520 Unpack the file using a command like: 
521 \family sans 
522 \series bold 
523 "tar -xzf sdcc.src.tgz
524 \family default 
525 \series default 
526 "
527 \series medium 
528 , this will create a sub-directory called sdcc with all of the sources.
529 \layout Enumerate
530
531 Change directory into the main SDCC directory, for example type: 
532 \family sans 
533 \series bold 
534 "cd sdcc
535 \series default 
536 ".
537 \layout Enumerate
538
539
540 \series medium 
541 Type 
542 \family sans 
543 \series bold 
544 "./configure
545 \family default 
546 \series default 
547 ".
548  This configures the package for compilation on your system.
549 \layout Enumerate
550
551
552 \series medium 
553 Type 
554 \family sans 
555 \series bold 
556 "make
557 \family default 
558 \series default 
559 "
560 \series medium 
561 .
562
563 \series default 
564  All of the source packages will compile, this can take a while.
565 \layout Enumerate
566
567
568 \series medium 
569 Type 
570 \family sans 
571 \series bold 
572 "make install"
573 \family default 
574 \series default 
575  as root
576 \series medium 
577 .
578
579 \series default 
580  This copies the binary executables, the include files, the libraries and
581  the documentation to the install directories.
582 \layout Subsection
583
584 Windows 
585 \layout Subsubsection
586
587 Windows Install Using a Binary Package
588 \layout Enumerate
589
590 Download the binary package and unpack it using your favorite unpacking
591  tool (gunzip, WinZip, etc).
592  This should unpack to a group of sub-directories.
593  An example directory structure after unpacking the mingw package is: c:
594 \backslash 
595 usr
596 \backslash 
597 local
598 \backslash 
599 bin for the executables, c:
600 \backslash 
601 usr
602 \backslash 
603 local
604 \backslash 
605 share
606 \backslash 
607 sdcc
608 \backslash 
609 include and c:
610 \backslash 
611 usr
612 \backslash 
613 local
614 \backslash 
615 share
616 \backslash 
617 sdcc
618 \backslash 
619 lib for the include and libraries.
620 \layout Enumerate
621
622 Adjust your environment variable PATH to include the location of the bin
623  directory or start sdcc using the full path.
624 \layout Subsubsection
625
626 Windows Install Using Cygwin and Mingw
627 \layout Standard
628
629 Follow the instruction in 
630 \series bold 
631 Linux and other gcc-based systems
632 \series default 
633 .
634 \layout Subsubsection
635
636 Windows Install Using Microsoft Visual C++ 6.0/NET
637 \layout Standard
638
639
640 \series medium 
641 Download the source package
642 \series default 
643  either from the SDCC CVS repository or from the 
644 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
645
646 \end_inset 
647
648
649 \series medium 
650 , it will be named something like sdcc
651 \series default 
652 .src
653 \series medium 
654 .tgz.
655
656 \series default 
657  SDCC is distributed with all the projects, workspaces, and files you need
658  to build it using Visual C++ 6.0/NET.
659  The workspace name is 'sdcc.dsw'.
660  Please note that as it is now, all the executables are created in a folder
661  called sdcc
662 \backslash 
663 bin_vc.
664  Once built you need to copy the executables from sdcc
665 \backslash 
666 bin_vc to sdcc
667 \backslash 
668 bin before runnng SDCC.
669  
670 \newline 
671
672 \newline 
673 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
674  of bison.exe, flex.exe, and gawk.exe.
675  One good place to get them is 
676 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
677
678 \end_inset 
679
680
681 \newline 
682
683 \newline 
684 Download the file UnxUtils.zip.
685  Now you have to install the utilities and setup Visual C++ so it can locate
686  the required programs.
687  Here there are two alternatives (choose one!):
688 \layout Enumerate
689
690 The easy way:
691 \newline 
692
693 \newline 
694 a) Extract UnxUtils.zip to your C:
695 \backslash 
696  hard disk PRESERVING the original paths, otherwise bison won't work.
697  (If you are using WinZip make certain that 'Use folder names' is selected)
698 \newline 
699
700 \newline 
701 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
702  in 'Show directories for:' select 'Executable files', and in the directories
703  window add a new path: 'C:
704 \backslash 
705 user
706 \backslash 
707 local
708 \backslash 
709 wbin', click ok.
710 \newline 
711
712 \newline 
713 (As a side effect, you get a bunch of Unix utilities that could be useful,
714  such as diff and patch.)
715 \layout Enumerate
716
717 A more compact way:
718 \newline 
719
720 \newline 
721 This one avoids extracting a bunch of files you may not use, but requires
722  some extra work:
723 \newline 
724
725 \newline 
726 a) Create a directory were to put the tools needed, or use a directory already
727  present.
728  Say for example 'C:
729 \backslash 
730 util'.
731 \newline 
732
733 \newline 
734 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
735  to such directory WITHOUT preserving the original paths.
736  (If you are using WinZip make certain that 'Use folder names' is not selected)
737 \newline 
738
739 \newline 
740 c) Rename bison.exe to '_bison.exe'.
741 \newline 
742
743 \newline 
744 d) Create a batch file 'bison.bat' in 'C:
745 \backslash 
746 util
747 \backslash 
748 ' and add these lines: 
749 \newline 
750 \SpecialChar ~
751 \SpecialChar ~
752 set BISON_SIMPLE=C:
753 \backslash 
754 util
755 \backslash 
756 bison.simple 
757 \newline 
758 \SpecialChar ~
759 \SpecialChar ~
760 set BISON_HAIRY=C:
761 \backslash 
762 util
763 \backslash 
764 bison.hairy
765 \newline 
766 \SpecialChar ~
767 \SpecialChar ~
768 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
769 \newline 
770
771 \newline 
772 Steps 'c' and 'd' are needed because bison requires by default that the
773  files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
774  '/usr/local/share/' I think.
775  So it is necessary to tell bison where those files are located if they
776  are not in such directory.
777  That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
778 \newline 
779
780 \newline 
781 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
782  in 'Show directories for:' select 'Executable files', and in the directories
783  window add a new path: 'c:
784 \backslash 
785 util', click ok.
786  Note that you can use any other path instead of 'c:
787 \backslash 
788 util', even the path where the Visual C++ tools are, probably: 'C:
789 \backslash 
790 Program Files
791 \backslash 
792 Microsoft Visual Studio
793 \backslash 
794 Common
795 \backslash 
796 Tools'.
797  So you don't have to execute step 'e' :)
798 \layout Standard
799
800 That is it.
801  Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
802  the executables from sdcc
803 \backslash 
804 bin_vc to sdcc
805 \backslash 
806 bin, and you can compile using sdcc.
807 \layout Subsubsection
808
809 Windows Install Using Borland ......
810 \layout Standard
811
812
813 \shape italic 
814 <pending>
815 \layout Subsection
816
817 Testing out the SDCC Compiler
818 \layout Standard
819
820 The first thing you should do after installing your SDCC compiler is to
821  see if it runs.
822  Type 
823 \family sans 
824 \series bold 
825 "sdcc --version"
826 \family default 
827 \series default 
828  at the prompt, and the program should run and tell you the version.
829  If it doesn't run, or gives a message about not finding sdcc program, then
830  you need to check over your installation.
831  Make sure that the sdcc bin directory is in your executable search path
832  defined by the PATH environment setting (see the Trouble-shooting section
833  for suggestions).
834  Make sure that the sdcc program is in the bin folder, if not perhaps something
835  did not install correctly.
836 \newline 
837
838 \newline 
839
840 \series medium 
841 SDCC binaries are commonly installed in a directory arrangement like this:
842 \series default 
843
844 \newline 
845
846 \newline 
847
848 \begin_inset  Tabular
849 <lyxtabular version="3" rows="3" columns="2">
850 <features>
851 <column alignment="left" valignment="top" leftline="true" width="0(null)">
852 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0(null)">
853 <row topline="true" bottomline="true">
854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
855 \begin_inset Text
856
857 \layout Standard
858
859 /
860 \series medium 
861 usr/local/bin
862 \end_inset 
863 </cell>
864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
865 \begin_inset Text
866
867 \layout Standard
868
869
870 \series medium 
871 Holds executables(sdcc, s51, aslink, 
872 \series default 
873 ...
874 \series medium 
875 )
876 \end_inset 
877 </cell>
878 </row>
879 <row topline="true">
880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
881 \begin_inset Text
882
883 \layout Standard
884
885 /
886 \series medium 
887 usr/local/share/sdcc/lib 
888 \end_inset 
889 </cell>
890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
891 \begin_inset Text
892
893 \layout Standard
894
895
896 \series medium 
897 Holds common C 
898 \series default 
899 libraries
900 \end_inset 
901 </cell>
902 </row>
903 <row topline="true" bottomline="true">
904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
905 \begin_inset Text
906
907 \layout Standard
908
909 /
910 \series medium 
911 usr/local/share/sdcc/include
912 \end_inset 
913 </cell>
914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
915 \begin_inset Text
916
917 \layout Standard
918
919
920 \series medium 
921 Holds common C header files
922 \end_inset 
923 </cell>
924 </row>
925 </lyxtabular>
926
927 \end_inset 
928
929
930 \newline 
931
932 \newline 
933
934 \series medium 
935 Make sure the compiler works on a very simple example.
936  Type in the following test.c program using your favorite editor:
937 \series default 
938
939 \newline 
940
941 \family typewriter 
942
943 \newline 
944 char test;
945 \newline 
946
947 \newline 
948 void main(void) {
949 \newline 
950 \SpecialChar ~
951 \SpecialChar ~
952 \SpecialChar ~
953 \SpecialChar ~
954 test=0;
955 \newline 
956 }
957 \family default 
958
959 \newline 
960
961 \emph on 
962
963 \newline 
964
965 \series medium 
966 \emph default 
967 Compile this using the following command: 
968 \family sans 
969 \series bold 
970 "sdcc -c test.c".
971
972 \family default 
973 \series default 
974  
975 \series medium 
976 If all goes well, the compiler will generate a test.asm and test.rel file.
977  Congratulations, you've just compiled your first program with SDCC.
978  We used the -c option to tell SDCC not to link the generated code, just
979  to keep things simple for this step.
980 \series default 
981
982 \newline 
983
984 \newline 
985
986 \series medium 
987 The next step is to try it with the linker.
988  Type in 
989 \family sans 
990 \series bold 
991 "sdcc test.c
992 \family default 
993 \series default 
994 "
995 \series medium 
996 .
997  If all goes well the compiler will link with the libraries and produce
998  a test.ihx output file.
999  If this step fails
1000 \series default 
1001  
1002 \series medium 
1003 (no test.ihx, and the linker generates warnings), then the problem is most
1004  likely that sdcc cannot find the 
1005 \series default 
1006 /
1007 \series medium 
1008 usr/local/share/sdcc/lib directory
1009 \series default 
1010  
1011 \series medium 
1012 (see the Install trouble-shooting section for suggestions).
1013 \series default 
1014
1015 \newline 
1016
1017 \newline 
1018
1019 \series medium 
1020 The final test is to ensure sdcc can use the 
1021 \series default 
1022 standard
1023 \series medium 
1024  header files and libraries.
1025  Edit test.c and change it to the following:
1026 \series default 
1027
1028 \newline 
1029
1030 \newline 
1031
1032 \family typewriter 
1033 #include <string.h>
1034 \newline 
1035
1036 \newline 
1037 char str1[10];
1038 \newline 
1039
1040 \newline 
1041 void main(void) {
1042 \newline 
1043 \SpecialChar ~
1044 \SpecialChar ~
1045 strcpy(str1, "testing");
1046 \newline 
1047 }
1048 \newline 
1049
1050 \newline 
1051
1052 \family default 
1053 \series medium 
1054 Compile this by typing 
1055 \family sans 
1056 \series bold 
1057 "sdcc test.c"
1058 \family default 
1059 \series medium 
1060 .
1061  This should generate a test.ihx output file, and it should give no warnings
1062  such as not finding the string.h file.
1063  If it cannot find the string.h file, then the problem is that sdcc cannot
1064  find the /usr/local/share/sdcc/include directory
1065 \series default 
1066  
1067 \series medium 
1068 (see the Install trouble-shooting section for suggestions).
1069 \layout Subsection
1070
1071 Install Trouble-shooting
1072 \layout Subsubsection
1073
1074 SDCC does not build correctly.
1075 \layout Standard
1076
1077 A thing to try is starting from scratch by unpacking the .tgz source package
1078  again in an empty directory.
1079  Confure it like:
1080 \newline 
1081
1082 \newline 
1083
1084 \family typewriter 
1085 ./configure 2>&1 | tee configure.log
1086 \family default 
1087
1088 \newline 
1089
1090 \newline 
1091 and build it like:
1092 \newline 
1093
1094 \newline 
1095
1096 \family typewriter 
1097 make 2>&1 | tee make.log
1098 \family default 
1099
1100 \newline 
1101
1102 \newline 
1103 If anything goes wrong, you can review the log files to locate the problem.
1104  Or a relevant part of this can be attached to an email that could be helpful
1105  when requesting help from the mailing list.
1106 \layout Subsubsection
1107
1108 What the 
1109 \begin_inset Quotes sld
1110 \end_inset 
1111
1112 ./configure
1113 \begin_inset Quotes srd
1114 \end_inset 
1115
1116  does
1117 \layout Standard
1118
1119 The 
1120 \begin_inset Quotes sld
1121 \end_inset 
1122
1123 ./configure
1124 \begin_inset Quotes srd
1125 \end_inset 
1126
1127  command is a script that analyzes your system and performs some configuration
1128  to ensure the source package compiles on your system.
1129  It will take a few minutes to run, and will compile a few tests to determine
1130  what compiler features are installed.
1131 \layout Subsubsection
1132
1133 What the 
1134 \begin_inset Quotes sld
1135 \end_inset 
1136
1137 make
1138 \begin_inset Quotes srd
1139 \end_inset 
1140
1141  does.
1142 \layout Standard
1143
1144 This runs the GNU make tool, which automatically compiles all the source
1145  packages into the final installed binary executables.
1146 \layout Subsubsection
1147
1148 What the 
1149 \begin_inset Quotes sld
1150 \end_inset 
1151
1152 make install
1153 \begin_inset Quotes erd
1154 \end_inset 
1155
1156  command does.
1157 \layout Standard
1158
1159 This will install the compiler, other executables and libraries in to the
1160  appropriate system directories.
1161  The default is to copy the executables to /usr/local/bin and the libraries
1162  and header files to /usr/local/share/sdcc/lib and /usr/local/share/sdcc/include.
1163  On most systems you will need super-user privilages to do this.
1164 \layout Subsection
1165
1166 Advanced Install Options
1167 \layout Standard
1168
1169 The 
1170 \begin_inset Quotes eld
1171 \end_inset 
1172
1173 configure
1174 \begin_inset Quotes erd
1175 \end_inset 
1176
1177  command has several options.
1178  The most commonly used option is --prefix=<directory name>, where <directory
1179  name> is the final location for the sdcc executables and libraries, (default
1180  location is /usr/local).
1181  The installation process will create the following directory structure
1182  under the <directory name> specified (if they do not already exist).
1183  
1184 \newline 
1185
1186 \newline 
1187 bin/ - binary exectables (add to PATH environment variable)
1188 \newline 
1189 bin/share/
1190 \newline 
1191 bin/share/sdcc/include/ - include header files
1192 \newline 
1193 bin/share/sdcc/lib/
1194 \newline 
1195 bin/share/sdcc/lib/small/ - Object & library files for small model library
1196 \newline 
1197 bin/share/sdcc/lib/large/ - Object & library files for large model library
1198 \newline 
1199 bin/share/sdcc/lib/ds390/ - Object & library files for DS80C390 library
1200 \newline 
1201 bin/share/sdcc/lib/z80/ - Object & library files for Z80 library
1202 \newline 
1203
1204 \newline 
1205 The command 
1206 \family sans 
1207 \series bold 
1208
1209 \begin_inset Quotes sld
1210 \end_inset 
1211
1212 ./configure --prefix=/usr/local
1213 \begin_inset Quotes erd
1214 \end_inset 
1215
1216  
1217 \family default 
1218 \series default 
1219 will configure the compiler to be installed in directory /usr/local.
1220 \layout Subsection
1221
1222 Components of SDCC
1223 \layout Standard
1224
1225 SDCC is not just a compiler, but a collection of tools by various developers.
1226  These include linkers, assemblers, simulators and other components.
1227  Here is a summary of some of the components.
1228  Note that the included simulator and assembler have separate documentation
1229  which you can find in the source package in their respective directories.
1230  As SDCC grows to include support for other processors, other packages from
1231  various developers are included and may have their own sets of documentation.
1232 \newline 
1233
1234 \newline 
1235 You might want to look at the files which are installed in <installdir>.
1236  At the time of this writing, we find the following programs:
1237 \newline 
1238  
1239 \newline 
1240 In <installdir>/bin:
1241 \layout Itemize
1242
1243 sdcc - The compiler.
1244 \layout Itemize
1245
1246 sdcpp - The C preprocessor.
1247 \layout Itemize
1248
1249 asx8051 - The assembler for 8051 type processors.
1250 \layout Itemize
1251
1252 as-z80
1253 \series bold 
1254
1255 \series default 
1256 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
1257 \layout Itemize
1258
1259 aslink -The linker for 8051 type processors.
1260 \layout Itemize
1261
1262 link-z80
1263 \series bold 
1264
1265 \series default 
1266 link-gbz80 - The Z80 and GameBoy Z80 linkers.
1267 \layout Itemize
1268
1269 s51 - The ucSim 8051 simulator.
1270 \layout Itemize
1271
1272 sdcdb - The source debugger.
1273 \layout Itemize
1274
1275 packihx - A tool to pack (compress) Intel hex files.
1276 \layout Standard
1277
1278 In <installdir>/share/sdcc/include
1279 \layout Itemize
1280
1281 the include files
1282 \layout Standard
1283
1284 In <installdir>/share/sdcc/lib
1285 \layout Itemize
1286
1287 the sources of the runtime library and the subdirs small large and ds390
1288  with the precompiled relocatables.
1289 \layout Standard
1290
1291 In <installdir>/share/sdcc/doc
1292 \layout Itemize
1293
1294 the documentation
1295 \layout Standard
1296
1297 As development for other processors proceeds, this list will expand to include
1298  executables to support processors like AVR, PIC, etc.
1299 \layout Subsubsection
1300
1301 sdcc - The Compiler
1302 \layout Standard
1303
1304 This is the actual compiler, it in turn uses the c-preprocessor and invokes
1305  the assembler and linkage editor.
1306 \layout Subsubsection
1307
1308 sdcpp - The C-Preprocessor
1309 \layout Standard
1310
1311 The preprocessor is a modified version of the GNU preprocessor.
1312  The C preprocessor is used to pull in #include sources, process #ifdef
1313  statements, #defines and so on.
1314 \layout Subsubsection
1315
1316 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
1317  and Linkage Editors
1318 \layout Standard
1319
1320 This is retargettable assembler & linkage editor, it was developed by Alan
1321  Baldwin.
1322  John Hartman created the version for 8051, and I (Sandeep) have made some
1323  enhancements and bug fixes for it to work properly with the SDCC.
1324 \layout Subsubsection
1325
1326 s51 - The Simulator
1327 \layout Standard
1328
1329 S51 is a freeware, opensource simulator developed by Daniel Drotos (
1330 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
1331
1332 \end_inset 
1333
1334 ).
1335  The simulator is built as part of the build process.
1336  For more information visit Daniel's website at: 
1337 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
1338
1339 \end_inset 
1340
1341 .
1342  It currently support the core mcs51, the Dallas DS80C390 and the Philips
1343  XA51 family.
1344 \layout Subsubsection
1345
1346 sdcdb - Source Level Debugger
1347 \layout Standard
1348
1349
1350 \family typewriter 
1351 \shape italic 
1352 <todo: is this thing alive?>
1353 \newline 
1354
1355 \newline 
1356
1357 \family default 
1358 \shape default 
1359 Sdcdb is the companion source level debugger.
1360  The current version of the debugger uses Daniel's Simulator S51, but can
1361  be easily changed to use other simulators.
1362 \layout Section
1363
1364 Using SDCC
1365 \layout Subsection
1366
1367 Compiling
1368 \layout Subsubsection
1369
1370 Single Source File Projects
1371 \layout Standard
1372
1373 For single source file 8051 projects the process is very simple.
1374  Compile your programs with the following command 
1375 \family sans 
1376 \series bold 
1377 "sdcc sourcefile.c".
1378
1379 \family default 
1380 \series default 
1381  This will compile, assemble and link your source file.
1382  Output files are as follows
1383 \newline 
1384
1385 \newline 
1386 sourcefile.asm - Assembler source file created by the compiler
1387 \newline 
1388 sourcefile.lst - Assembler listing file created by the Assembler
1389 \newline 
1390 sourcefile.rst - Assembler listing file updated with linkedit information,
1391  created by linkage editor
1392 \newline 
1393 sourcefile.sym - symbol listing for the sourcefile, created by the assembler
1394 \newline 
1395 sourcefile.rel - Object file created by the assembler, input to Linkage editor
1396 \newline 
1397 sourcefile.map - The memory map for the load module, created by the Linker
1398 \newline 
1399 sourcefile.ihx - The load module in Intel hex format (you can select the
1400  Motorola S19 format with --out-fmt-s19)
1401 \newline 
1402 sourcefile.cdb - An optional file (with --debug) containing debug information
1403 \newline 
1404
1405 \layout Subsubsection
1406
1407 Projects with Multiple Source Files
1408 \layout Standard
1409
1410 SDCC can compile only ONE file at a time.
1411  Let us for example assume that you have a project containing the following
1412  files:
1413 \newline 
1414
1415 \newline 
1416 foo1.c (contains some functions)
1417 \newline 
1418 foo2.c (contains some more functions)
1419 \newline 
1420 foomain.c (contains more functions and the function main)
1421 \newline 
1422
1423 \size footnotesize 
1424
1425 \newline 
1426
1427 \size default 
1428 The first two files will need to be compiled separately with the commands:
1429 \size footnotesize 
1430  
1431 \size default 
1432
1433 \newline 
1434
1435 \newline 
1436
1437 \family sans 
1438 \series bold 
1439 sdcc\SpecialChar ~
1440 -c\SpecialChar ~
1441 foo1.c
1442 \family default 
1443 \series default 
1444 \size footnotesize 
1445
1446 \newline 
1447
1448 \family sans 
1449 \series bold 
1450 \size default 
1451 sdcc\SpecialChar ~
1452 -c\SpecialChar ~
1453 foo2.c
1454 \family default 
1455 \series default 
1456
1457 \newline 
1458
1459 \newline 
1460 Then compile the source file containing the 
1461 \emph on 
1462 main()
1463 \emph default 
1464  function and link the files together with the following command: 
1465 \newline 
1466
1467 \newline 
1468
1469 \family sans 
1470 \series bold 
1471 sdcc\SpecialChar ~
1472 foomain.c\SpecialChar ~
1473 foo1.rel\SpecialChar ~
1474 foo2.rel
1475 \family default 
1476 \series default 
1477
1478 \newline 
1479
1480 \newline 
1481 Alternatively, 
1482 \emph on 
1483 foomain.c 
1484 \emph default 
1485 can be separately compiled as well: 
1486 \family sans 
1487 \series bold 
1488
1489 \newline 
1490
1491 \newline 
1492 sdcc\SpecialChar ~
1493 -c\SpecialChar ~
1494 foomain.c
1495 \newline 
1496 sdcc foomain.rel foo1.rel foo2.rel
1497 \newline 
1498
1499 \newline 
1500
1501 \family default 
1502 \series default 
1503 The file containing the 
1504 \emph on 
1505 main()
1506 \emph default 
1507  function
1508 \emph on 
1509  
1510 \emph default 
1511 \noun on 
1512 must
1513 \noun default 
1514  be the 
1515 \noun on 
1516 first
1517 \noun default 
1518  file specified in the command line, since the linkage editor processes
1519  file in the order they are presented to it.
1520 \layout Subsubsection
1521
1522 Projects with Additional Libraries
1523 \layout Standard
1524
1525 Some reusable routines may be compiled into a library, see the documentation
1526  for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
1527  for how to create a 
1528 \emph on 
1529 .lib
1530 \emph default 
1531  library file.
1532  Libraries created in this manner can be included in the command line.
1533  Make sure you include the -L <library-path> option to tell the linker where
1534  to look for these files if they are not in the current directory.
1535  Here is an example, assuming you have the source file 
1536 \emph on 
1537 foomain.c
1538 \emph default 
1539  and a library
1540 \emph on 
1541  foolib.lib
1542 \emph default 
1543  in the directory 
1544 \emph on 
1545 mylib
1546 \emph default 
1547  (if that is not the same as your current project):
1548 \newline 
1549
1550 \newline 
1551
1552 \family sans 
1553 \series bold 
1554 sdcc foomain.c foolib.lib -L mylib
1555 \newline 
1556
1557 \newline 
1558
1559 \family default 
1560 \series default 
1561 Note here that
1562 \emph on 
1563  mylib
1564 \emph default 
1565  must be an absolute path name.
1566 \newline 
1567
1568 \newline 
1569 The most efficient way to use libraries is to keep seperate modules in seperate
1570  source files.
1571  The lib file now should name all the modules.rel files.
1572  For an example see the standard library file 
1573 \emph on 
1574 libsdcc.lib
1575 \emph default 
1576  in the directory <installdir>/share/lib/small.
1577 \layout Subsection
1578
1579 Command Line Options
1580 \layout Subsubsection
1581
1582 Processor Selection Options
1583 \layout List
1584 \labelwidthstring 00.00.0000
1585
1586
1587 \series bold 
1588 -mmcs51
1589 \series default 
1590  Generate code for the MCS51 (8051) family of processors.
1591  This is the default processor target.
1592 \layout List
1593 \labelwidthstring 00.00.0000
1594
1595
1596 \series bold 
1597 -mds390
1598 \series default 
1599  Generate code for the DS80C390 processor.
1600 \layout List
1601 \labelwidthstring 00.00.0000
1602
1603
1604 \series bold 
1605 -mz80
1606 \series default 
1607  Generate code for the Z80 family of processors.
1608 \layout List
1609 \labelwidthstring 00.00.0000
1610
1611
1612 \series bold 
1613 -mgbz80
1614 \series default 
1615  Generate code for the GameBoy Z80 processor.
1616 \layout List
1617 \labelwidthstring 00.00.0000
1618
1619
1620 \series bold 
1621 -mavr
1622 \series default 
1623  Generate code for the Atmel AVR processor (In development, not complete).
1624 \layout List
1625 \labelwidthstring 00.00.0000
1626
1627
1628 \series bold 
1629 -mpic14
1630 \series default 
1631  Generate code for the PIC 14-bit processors (In development, not complete).
1632 \layout List
1633 \labelwidthstring 00.00.0000
1634
1635
1636 \series bold 
1637 -mtlcs900h
1638 \series default 
1639  Generate code for the Toshiba TLCS-900H processor (In development, not
1640  complete).
1641 \layout List
1642 \labelwidthstring 00.00.0000
1643
1644
1645 \series bold 
1646 -mxa51
1647 \series default 
1648  Generate code for the Philips XA51 processor (In development, not complete).
1649 \layout Subsubsection
1650
1651 Preprocessor Options
1652 \layout List
1653 \labelwidthstring 00.00.0000
1654
1655
1656 \series bold 
1657 -I<path>
1658 \series default 
1659  The additional location where the pre processor will look for <..h> or 
1660 \begin_inset Quotes eld
1661 \end_inset 
1662
1663 ..h
1664 \begin_inset Quotes erd
1665 \end_inset 
1666
1667  files.
1668 \layout List
1669 \labelwidthstring 00.00.0000
1670
1671
1672 \series bold 
1673 -D<macro[=value]>
1674 \series default 
1675  Command line definition of macros.
1676  Passed to the pre processor.
1677 \layout List
1678 \labelwidthstring 00.00.0000
1679
1680
1681 \series bold 
1682 -M
1683 \series default 
1684  Tell the preprocessor to output a rule suitable for make describing the
1685  dependencies of each object file.
1686  For each source file, the preprocessor outputs one make-rule whose target
1687  is the object file name for that source file and whose dependencies are
1688  all the files `#include'd in it.
1689  This rule may be a single line or may be continued with `
1690 \backslash 
1691 '-newline if it is long.
1692  The list of rules is printed on standard output instead of the preprocessed
1693  C program.
1694  `-M' implies `-E'.
1695 \layout List
1696 \labelwidthstring 00.00.0000
1697
1698
1699 \series bold 
1700 -C
1701 \series default 
1702  Tell the preprocessor not to discard comments.
1703  Used with the `-E' option.
1704 \layout List
1705 \labelwidthstring 00.00.0000
1706
1707
1708 \series bold 
1709 -MM
1710 \size large 
1711 \bar under 
1712  
1713 \series default 
1714 \size default 
1715 \bar default 
1716 Like `-M' but the output mentions only the user header files included with
1717  `#include 
1718 \begin_inset Quotes eld
1719 \end_inset 
1720
1721 file"'.
1722  System header files included with `#include <file>' are omitted.
1723 \layout List
1724 \labelwidthstring 00.00.0000
1725
1726
1727 \series bold 
1728 -Aquestion(answer)
1729 \series default 
1730  Assert the answer answer for question, in case it is tested with a preprocessor
1731  conditional such as `#if #question(answer)'.
1732  `-A-' disables the standard assertions that normally describe the target
1733  machine.
1734 \layout List
1735 \labelwidthstring 00.00.0000
1736
1737
1738 \series bold 
1739 -Aquestion
1740 \series default 
1741  (answer) Assert the answer answer for question, in case it is tested with
1742  a preprocessor conditional such as `#if #question(answer)'.
1743  `-A-' disables the standard assertions that normally describe the target
1744  machine.
1745 \layout List
1746 \labelwidthstring 00.00.0000
1747
1748
1749 \series bold 
1750 -Umacro
1751 \series default 
1752  Undefine macro macro.
1753  `-U' options are evaluated after all `-D' options, but before any `-include'
1754  and `-imacros' options.
1755 \layout List
1756 \labelwidthstring 00.00.0000
1757
1758
1759 \series bold 
1760 -dM
1761 \series default 
1762  Tell the preprocessor to output only a list of the macro definitions that
1763  are in effect at the end of preprocessing.
1764  Used with the `-E' option.
1765 \layout List
1766 \labelwidthstring 00.00.0000
1767
1768
1769 \series bold 
1770 -dD
1771 \series default 
1772  Tell the preprocessor to pass all macro definitions into the output, in
1773  their proper sequence in the rest of the output.
1774 \layout List
1775 \labelwidthstring 00.00.0000
1776
1777
1778 \series bold 
1779 -dN
1780 \size large 
1781 \bar under 
1782  
1783 \series default 
1784 \size default 
1785 \bar default 
1786 Like `-dD' except that the macro arguments and contents are omitted.
1787  Only `#define name' is included in the output.
1788 \layout Subsubsection
1789
1790 Linker Options
1791 \layout List
1792 \labelwidthstring 00.00.0000
1793
1794
1795 \series bold 
1796 -o\SpecialChar ~
1797 <path/file> 
1798 \series default 
1799 the output path resp.
1800  file where everything will be placed
1801 \layout List
1802 \labelwidthstring 00.00.0000
1803
1804
1805 \series bold 
1806 -L\SpecialChar ~
1807 --lib-path
1808 \bar under 
1809  
1810 \series default 
1811 \bar default 
1812 <absolute path to additional libraries> This option is passed to the linkage
1813  editor's additional libraries search path.
1814  The path name must be absolute.
1815  Additional library files may be specified in the command line.
1816  See section Compiling programs for more details.
1817 \layout List
1818 \labelwidthstring 00.00.0000
1819
1820
1821 \series bold 
1822 --xram-loc
1823 \series default 
1824 <Value> The start location of the external ram, default value is 0.
1825  The value entered can be in Hexadecimal or Decimal format, e.g.: --xram-loc
1826  0x8000 or --xram-loc 32768.
1827 \layout List
1828 \labelwidthstring 00.00.0000
1829
1830
1831 \series bold 
1832 --code-loc
1833 \series default 
1834 <Value> The start location of the code segment, default value 0.
1835  Note when this option is used the interrupt vector table is also relocated
1836  to the given address.
1837  The value entered can be in Hexadecimal or Decimal format, e.g.: --code-loc
1838  0x8000 or --code-loc 32768.
1839 \layout List
1840 \labelwidthstring 00.00.0000
1841
1842
1843 \series bold 
1844 --stack-loc
1845 \series default 
1846 <Value> By default the stack is placed after the data segment.
1847  Using this option the stack can be placed anywhere in the internal memory
1848  space of the 8051.
1849  The value entered can be in Hexadecimal or Decimal format, e.g.
1850  --stack-loc 0x20 or --stack-loc 32.
1851  Since the sp register is incremented before a push or call, the initial
1852  sp will be set to one byte prior the provided value.
1853  The provided value should not overlap any other memory areas such as used
1854  register banks or the data segment and with enough space for the current
1855  application.
1856 \layout List
1857 \labelwidthstring 00.00.0000
1858
1859
1860 \series bold 
1861 --data-loc
1862 \series default 
1863 <Value> The start location of the internal ram data segment.
1864  The value entered can be in Hexadecimal or Decimal format, eg.
1865  --data-loc 0x20 or --data-loc 32.
1866  (By default, the start location of the internal ram data segment  is set
1867  as low as possible in memory, taking into account the used register banks
1868  and the bit segment at address 0x20.
1869  For example if register banks 0 and 1 are used without bit variables, the
1870  data segment will be set, if --data-loc is not used, to location 0x10.)
1871 \layout List
1872 \labelwidthstring 00.00.0000
1873
1874
1875 \series bold 
1876 --idata-loc
1877 \series default 
1878 <Value> The start location of the indirectly addressable internal ram, default
1879  value is 0x80.
1880  The value entered can be in Hexadecimal or Decimal format, eg.
1881  --idata-loc 0x88 or --idata-loc 136.
1882 \layout List
1883 \labelwidthstring 00.00.0000
1884
1885
1886 \series bold 
1887 --out-fmt-ihx
1888 \bar under 
1889  
1890 \series default 
1891 \bar default 
1892 The linker output (final object code) is in Intel Hex format.
1893  (This is the default option).
1894 \layout List
1895 \labelwidthstring 00.00.0000
1896
1897
1898 \series bold 
1899 --out-fmt-s19
1900 \bar under 
1901  
1902 \series default 
1903 \bar default 
1904 The linker output (final object code) is in Motorola S19 format.
1905 \layout Subsubsection
1906
1907 MCS51 Options
1908 \layout List
1909 \labelwidthstring 00.00.0000
1910
1911
1912 \series bold 
1913 --model-large
1914 \series default 
1915  Generate code for Large model programs see section Memory Models for more
1916  details.
1917  If this option is used all source files in the project should be compiled
1918  with this option.
1919  In addition the standard library routines are compiled with small model,
1920  they will need to be recompiled.
1921 \layout List
1922 \labelwidthstring 00.00.0000
1923
1924
1925 \series bold 
1926 --model-small
1927 \series default 
1928 \size large 
1929 \emph on 
1930  
1931 \size default 
1932 \emph default 
1933 Generate code for Small Model programs see section Memory Models for more
1934  details.
1935  This is the default model.
1936 \layout Subsubsection
1937
1938 DS390 Options
1939 \layout List
1940 \labelwidthstring 00.00.0000
1941
1942
1943 \series bold 
1944 --model-flat24
1945 \series default 
1946 \size large 
1947 \emph on 
1948  
1949 \size default 
1950 \emph default 
1951 Generate 24-bit flat mode code.
1952  This is the one and only that the ds390 code generator supports right now
1953  and is default when using 
1954 \emph on 
1955 -mds390
1956 \emph default 
1957 .
1958  See section Memory Models for more details.
1959 \layout List
1960 \labelwidthstring 00.00.0000
1961
1962
1963 \series bold 
1964 --stack-10bit
1965 \series default 
1966  Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
1967  This is the one and only that the ds390 code generator supports right now
1968  and is default when using 
1969 \emph on 
1970 -mds390
1971 \emph default 
1972 .
1973  In this mode, the stack is located in the lower 1K of the internal RAM,
1974  which is mapped to 0x400000.
1975  Note that the support is incomplete, since it still uses a single byte
1976  as the stack pointer.
1977  This means that only the lower 256 bytes of the potential 1K stack space
1978  will actually be used.
1979  However, this does allow you to reclaim the precious 256 bytes of low RAM
1980  for use for the DATA and IDATA segments.
1981  The compiler will not generate any code to put the processor into 10 bit
1982  stack mode.
1983  It is important to ensure that the processor is in this mode before calling
1984  any re-entrant functions compiled with this option.
1985  In principle, this should work with the 
1986 \emph on 
1987 --stack-auto
1988 \emph default 
1989  option, but that has not been tested.
1990  It is incompatible with the 
1991 \emph on 
1992 --xstack
1993 \emph default 
1994  option.
1995  It also only makes sense if the processor is in 24 bit contiguous addressing
1996  mode (see the 
1997 \emph on 
1998 --model-flat24 option
1999 \emph default 
2000 ).
2001 \layout Subsubsection
2002
2003 Optimization Options
2004 \layout List
2005 \labelwidthstring 00.00.0000
2006
2007
2008 \series bold 
2009 --nogcse
2010 \series default 
2011  Will not do global subexpression elimination, this option may be used when
2012  the compiler creates undesirably large stack/data spaces to store compiler
2013  temporaries.
2014  A warning message will be generated when this happens and the compiler
2015  will indicate the number of extra bytes it allocated.
2016  It recommended that this option NOT be used, #pragma\SpecialChar ~
2017 NOGCSE can be used
2018  to turn off global subexpression elimination for a given function only.
2019 \layout List
2020 \labelwidthstring 00.00.0000
2021
2022
2023 \series bold 
2024 --noinvariant
2025 \series default 
2026  Will not do loop invariant optimizations, this may be turned off for reasons
2027  explained for the previous option.
2028  For more details of loop optimizations performed see section Loop Invariants.It
2029  recommended that this option NOT be used, #pragma\SpecialChar ~
2030 NOINVARIANT can be used
2031  to turn off invariant optimizations for a given function only.
2032 \layout List
2033 \labelwidthstring 00.00.0000
2034
2035
2036 \series bold 
2037 --noinduction
2038 \series default 
2039  Will not do loop induction optimizations, see section strength reduction
2040  for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
2041 NOINDUCT
2042 ION can be used to turn off induction optimizations for a given function
2043  only.
2044 \layout List
2045 \labelwidthstring 00.00.0000
2046
2047
2048 \series bold 
2049 --nojtbound
2050 \size large 
2051 \bar under 
2052  
2053 \series default 
2054 \size default 
2055 \bar default 
2056  Will not generate boundary condition check when switch statements are implement
2057 ed using jump-tables.
2058  See section Switch Statements for more details.
2059  It is recommended that this option is NOT used, #pragma\SpecialChar ~
2060 NOJTBOUND can be
2061  used to turn off boundary checking for jump tables for a given function
2062  only.
2063 \layout List
2064 \labelwidthstring 00.00.0000
2065
2066
2067 \series bold 
2068 --noloopreverse
2069 \series default 
2070 \size large 
2071  
2072 \size default 
2073 Will not do loop reversal optimization.
2074 \layout List
2075 \labelwidthstring 00.00.0000
2076
2077
2078 \series bold 
2079 --no-xinit-opt
2080 \series default 
2081  This will disable the memcpy of initialized data in far space from code
2082  space
2083 \layout Subsubsection
2084
2085 Other Options
2086 \layout List
2087 \labelwidthstring 00.00.0000
2088
2089
2090 \series bold 
2091 -c\SpecialChar ~
2092 --compile-only
2093 \series default 
2094  will compile and assemble the source, but will not call the linkage editor.
2095 \layout List
2096 \labelwidthstring 00.00.0000
2097
2098
2099 \series bold 
2100 -E
2101 \series default 
2102  Run only the C preprocessor.
2103  Preprocess all the C source files specified and output the results to standard
2104  output.
2105 \layout List
2106 \labelwidthstring 00.00.0000
2107
2108
2109 \series bold 
2110 --stack-auto
2111 \series default 
2112 \size large 
2113 \emph on 
2114  
2115 \size default 
2116 \emph default 
2117 All functions in the source file will be compiled as 
2118 \emph on 
2119 reentrant
2120 \emph default 
2121 , i.e.
2122  the parameters and local variables will be allocated on the stack.
2123  see section Parameters and Local Variables for more details.
2124  If this option is used all source files in the project should be compiled
2125  with this option.
2126  
2127 \layout List
2128 \labelwidthstring 00.00.0000
2129
2130
2131 \series bold 
2132 --xstack
2133 \series default 
2134  Uses a pseudo stack in the first 256 bytes in the external ram for allocating
2135  variables and passing parameters.
2136  See section on external stack for more details.
2137 \layout List
2138 \labelwidthstring 00.00.0000
2139
2140
2141 \series bold 
2142 --callee-saves function1[,function2][,function3]....
2143
2144 \series default 
2145  The compiler by default uses a caller saves convention for register saving
2146  across function calls, however this can cause unneccessary register pushing
2147  & popping when calling small functions from larger functions.
2148  This option can be used to switch the register saving convention for the
2149  function names specified.
2150  The compiler will not save registers when calling these functions, no extra
2151  code will be generated at the entry & exit for these functions to save
2152  & restore the registers used by these functions, this can SUBSTANTIALLY
2153  reduce code & improve run time performance of the generated code.
2154  In the future the compiler (with interprocedural analysis) will be able
2155  to determine the appropriate scheme to use for each function call.
2156  DO NOT use this option for built-in functions such as _muluint..., if this
2157  option is used for a library function the appropriate library function
2158  needs to be recompiled with the same option.
2159  If the project consists of multiple source files then all the source file
2160  should be compiled with the same --callee-saves option string.
2161  Also see #pragma\SpecialChar ~
2162 CALLEE-SAVES.
2163 \layout List
2164 \labelwidthstring 00.00.0000
2165
2166
2167 \series bold 
2168 --debug
2169 \bar under 
2170  
2171 \series default 
2172 \bar default 
2173 When this option is used the compiler will generate debug information, that
2174  can be used with the SDCDB.
2175  The debug information is collected in a file with .cdb extension.
2176  For more information see documentation for SDCDB.
2177 \layout List
2178 \labelwidthstring 00.00.0000
2179
2180
2181 \series bold 
2182 --peep-file
2183 \series default 
2184 <filename> This option can be used to use additional rules to be used by
2185  the peep hole optimizer.
2186  See section Peep Hole optimizations for details on how to write these rules.
2187 \layout List
2188 \labelwidthstring 00.00.0000
2189
2190
2191 \series bold 
2192 -S
2193 \size large 
2194 \bar under 
2195  
2196 \series default 
2197 \size default 
2198 \bar default 
2199 Stop after the stage of compilation proper; do not assemble.
2200  The output is an assembler code file for the input file specified.
2201 \layout List
2202 \labelwidthstring 00.00.0000
2203
2204
2205 \series bold 
2206 -Wa_asmOption[,asmOption]
2207 \series default 
2208 ...
2209  Pass the asmOption to the assembler.
2210 \layout List
2211 \labelwidthstring 00.00.0000
2212
2213
2214 \series bold 
2215 -Wl_linkOption[,linkOption]
2216 \series default 
2217 ...
2218  Pass the linkOption to the linker.
2219 \layout List
2220 \labelwidthstring 00.00.0000
2221
2222
2223 \series bold 
2224 --int-long-reent
2225 \series default 
2226 \size large 
2227  
2228 \size default 
2229  Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
2230  Note by default these libraries are compiled as non-reentrant.
2231  See section Installation for more details.
2232 \layout List
2233 \labelwidthstring 00.00.0000
2234
2235
2236 \series bold 
2237 --cyclomatic
2238 \bar under 
2239  
2240 \series default 
2241 \bar default 
2242 This option will cause the compiler to generate an information message for
2243  each function in the source file.
2244  The message contains some 
2245 \emph on 
2246 important
2247 \emph default 
2248  information about the function.
2249  The number of edges and nodes the compiler detected in the control flow
2250  graph of the function, and most importantly the 
2251 \emph on 
2252 cyclomatic complexity
2253 \emph default 
2254  see section on Cyclomatic Complexity for more details.
2255 \layout List
2256 \labelwidthstring 00.00.0000
2257
2258
2259 \series bold 
2260 --float-reent
2261 \bar under 
2262  
2263 \series default 
2264 \bar default 
2265  Floating point library is compiled as reentrant.See section Installation
2266  for more details.
2267 \layout List
2268 \labelwidthstring 00.00.0000
2269
2270
2271 \series bold 
2272 --nooverlay
2273 \series default 
2274   The compiler will not overlay parameters and local variables of any function,
2275  see section Parameters and local variables for more details.
2276 \layout List
2277 \labelwidthstring 00.00.0000
2278
2279
2280 \series bold 
2281 --main-return
2282 \series default 
2283  This option can be used when the code generated is called by a monitor
2284  program.
2285  The compiler will generate a 'ret' upon return from the 'main' function.
2286  The default option is to lock up i.e.
2287  generate a 'ljmp '.
2288 \layout List
2289 \labelwidthstring 00.00.0000
2290
2291
2292 \series bold 
2293 --no-peep
2294 \series default 
2295   Disable peep-hole optimization.
2296 \layout List
2297 \labelwidthstring 00.00.0000
2298
2299
2300 \series bold 
2301 --peep-asm
2302 \series default 
2303   Pass the inline assembler code through the peep hole optimizer.
2304  This can cause unexpected changes to inline assembler code, please go through
2305  the peephole optimizer rules defined in the source file tree '<target>/peeph.def
2306 ' before using this option.
2307 \layout List
2308 \labelwidthstring 00.00.0000
2309
2310
2311 \series bold 
2312 --iram-size
2313 \series default 
2314 <Value> Causes the linker to check if the internal ram usage is within limits
2315  of the given value.
2316 \layout List
2317 \labelwidthstring 00.00.0000
2318
2319
2320 \series bold 
2321 --xram-size
2322 \series default 
2323 <Value> Causes the linker to check if the external ram usage is within limits
2324  of the given value.
2325 \layout List
2326 \labelwidthstring 00.00.0000
2327
2328
2329 \series bold 
2330 --code-size
2331 \series default 
2332 <Value> Causes the linker to check if the code usage is within limits of
2333  the given value.
2334 \layout List
2335 \labelwidthstring 00.00.0000
2336
2337
2338 \series bold 
2339 --nostdincl
2340 \series default 
2341  This will prevent the compiler from passing on the default include path
2342  to the preprocessor.
2343 \layout List
2344 \labelwidthstring 00.00.0000
2345
2346
2347 \series bold 
2348 --nostdlib
2349 \series default 
2350  This will prevent the compiler from passing on the default library path
2351  to the linker.
2352 \layout List
2353 \labelwidthstring 00.00.0000
2354
2355
2356 \series bold 
2357 --verbose
2358 \series default 
2359  Shows the various actions the compiler is performing.
2360 \layout List
2361 \labelwidthstring 00.00.0000
2362
2363
2364 \series bold 
2365 -V
2366 \series default 
2367  Shows the actual commands the compiler is executing.
2368 \layout Subsubsection
2369
2370 Intermediate Dump Options
2371 \layout Standard
2372
2373 The following options are provided for the purpose of retargetting and debugging
2374  the compiler.
2375  These provided a means to dump the intermediate code (iCode) generated
2376  by the compiler in human readable form at various stages of the compilation
2377  process.
2378  
2379 \layout List
2380 \labelwidthstring 00.00.0000
2381
2382
2383 \series bold 
2384 --dumpraw
2385 \series default 
2386  This option will cause the compiler to dump the intermediate code into
2387  a file of named 
2388 \emph on 
2389 <source filename>.dumpraw
2390 \emph default 
2391  just after the intermediate code has been generated for a function, i.e.
2392  before any optimizations are done.
2393  The basic blocks at this stage ordered in the depth first number, so they
2394  may not be in sequence of execution.
2395 \layout List
2396 \labelwidthstring 00.00.0000
2397
2398
2399 \series bold 
2400 --dumpgcse
2401 \series default 
2402  Will create a dump of iCode's, after global subexpression elimination,
2403  into a file named 
2404 \emph on 
2405 <source filename>.dumpgcse.
2406 \layout List
2407 \labelwidthstring 00.00.0000
2408
2409
2410 \series bold 
2411 --dumpdeadcode
2412 \series default 
2413  Will create a dump of iCode's, after deadcode elimination, into a file
2414  named 
2415 \emph on 
2416 <source filename>.dumpdeadcode.
2417 \layout List
2418 \labelwidthstring 00.00.0000
2419
2420
2421 \series bold 
2422 --dumploop
2423 \series default 
2424 \size large 
2425  
2426 \size default 
2427 Will create a dump of iCode's, after loop optimizations, into a file named
2428  
2429 \emph on 
2430 <source filename>.dumploop.
2431 \layout List
2432 \labelwidthstring 00.00.0000
2433
2434
2435 \series bold 
2436 --dumprange
2437 \series default 
2438 \size large 
2439  
2440 \size default 
2441 Will create a dump of iCode's, after live range analysis, into a file named
2442  
2443 \emph on 
2444 <source filename>.dumprange.
2445 \layout List
2446 \labelwidthstring 00.00.0000
2447
2448
2449 \series bold 
2450 --dumlrange
2451 \series default 
2452  Will dump the life ranges for all symbols.
2453 \layout List
2454 \labelwidthstring 00.00.0000
2455
2456
2457 \series bold 
2458 --dumpregassign
2459 \bar under 
2460  
2461 \series default 
2462 \bar default 
2463 Will create a dump of iCode's, after register assignment, into a file named
2464  
2465 \emph on 
2466 <source filename>.dumprassgn.
2467 \layout List
2468 \labelwidthstring 00.00.0000
2469
2470
2471 \series bold 
2472 --dumplrange
2473 \series default 
2474  Will create a dump of the live ranges of iTemp's
2475 \layout List
2476 \labelwidthstring 00.00.0000
2477
2478
2479 \series bold 
2480 --dumpall
2481 \size large 
2482 \bar under 
2483  
2484 \series default 
2485 \size default 
2486 \bar default 
2487 Will cause all the above mentioned dumps to be created.
2488 \layout Subsection
2489
2490 MCS51/DS390 Storage Class Language Extensions
2491 \layout Standard
2492
2493 In addition to the ANSI storage classes SDCC allows the following MCS51
2494  specific storage classes.
2495 \layout Subsubsection
2496
2497 xdata
2498 \layout Standard
2499
2500 Variables declared with this storage class will be placed in the extern
2501  RAM.
2502  This is the 
2503 \series bold 
2504 default
2505 \series default 
2506  storage class for Large Memory model, e.g.:
2507 \newline 
2508
2509 \newline 
2510
2511 \family typewriter 
2512 xdata unsigned char xduc;
2513 \layout Subsubsection
2514
2515 data
2516 \layout Standard
2517
2518 This is the 
2519 \series bold 
2520 default
2521 \series default 
2522  storage class for Small Memory model.
2523  Variables declared with this storage class will be allocated in the internal
2524  RAM, e.g.:
2525 \newline 
2526
2527 \newline 
2528
2529 \family typewriter 
2530 data int iramdata;
2531 \layout Subsubsection
2532
2533 idata
2534 \layout Standard
2535
2536 Variables declared with this storage class will be allocated into the indirectly
2537  addressable portion of the internal ram of a 8051, e.g.:
2538 \newline 
2539
2540 \newline 
2541
2542 \family typewriter 
2543 idata int idi;
2544 \layout Subsubsection
2545
2546 bit
2547 \layout Standard
2548
2549 This is a data-type and a storage class specifier.
2550  When a variable is declared as a bit, it is allocated into the bit addressable
2551  memory of 8051, e.g.:
2552 \newline 
2553
2554 \newline 
2555
2556 \family typewriter 
2557 bit iFlag;
2558 \layout Subsubsection
2559
2560 sfr / sbit
2561 \layout Standard
2562
2563 Like the bit keyword, 
2564 \emph on 
2565 sfr / sbit 
2566 \emph default 
2567 signifies both a data-type and storage class, they are used to describe
2568  the special function registers and special bit variables of a 8051, eg:
2569 \newline 
2570
2571 \newline 
2572
2573 \family typewriter 
2574 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
2575 \newline 
2576 sbit at 0xd7 CY; /* CY (Carry Flag) */
2577 \layout Subsection
2578
2579 Pointers
2580 \layout Standard
2581
2582 SDCC allows (via language extensions) pointers to explicitly point to any
2583  of the memory spaces of the 8051.
2584  In addition to the explicit pointers, the compiler uses (by default) generic
2585  pointers which can be used to point to any of the memory spaces.
2586 \newline 
2587
2588 \newline 
2589 Pointer declaration examples:
2590 \newline 
2591
2592 \size small 
2593
2594 \newline 
2595
2596 \family typewriter 
2597 \size default 
2598 /* pointer physically in xternal ram pointing to object in internal ram
2599  */ 
2600 \newline 
2601 data unsigned char * xdata p;
2602 \newline 
2603
2604 \newline 
2605 /* pointer physically in code rom pointing to data in xdata space */ 
2606 \newline 
2607 xdata unsigned char * code p;
2608 \newline 
2609
2610 \newline 
2611 /* pointer physically in code space pointing to data in code space */ 
2612 \newline 
2613 code unsigned char * code p;
2614 \newline 
2615
2616 \newline 
2617 /* the folowing is a generic pointer physically located in xdata space */
2618 \newline 
2619 char * xdata p;
2620 \family default 
2621 \size small 
2622
2623 \newline 
2624
2625 \newline 
2626
2627 \size default 
2628 Well you get the idea.
2629  
2630 \newline 
2631
2632 \newline 
2633 All unqualified pointers are treated as 3-byte (4-byte for the ds390) 
2634 \emph on 
2635 generic
2636 \emph default 
2637  pointers.
2638  
2639 \size small 
2640
2641 \newline 
2642
2643 \newline 
2644
2645 \size default 
2646 The highest order byte of the 
2647 \emph on 
2648 generic
2649 \emph default 
2650  pointers contains the data space information.
2651  Assembler support routines are called whenever data is stored or retrieved
2652  using 
2653 \emph on 
2654 generic
2655 \emph default 
2656  pointers.
2657  These are useful for developing reusable library routines.
2658  Explicitly specifying the pointer type will generate the most efficient
2659  code.
2660 \layout Subsection
2661
2662 Parameters & Local Variables
2663 \layout Standard
2664
2665 Automatic (local) variables and parameters to functions can either be placed
2666  on the stack or in data-space.
2667  The default action of the compiler is to place these variables in the internal
2668  RAM (for small model) or external RAM (for large model).
2669  This in fact makes them 
2670 \emph on 
2671 static
2672 \emph default 
2673  so by default functions are non-reentrant.
2674 \newline 
2675
2676 \newline 
2677 They can be placed on the stack either by using the
2678 \emph on 
2679  --stack-auto
2680 \emph default 
2681  option or by using the 
2682 \emph on 
2683 reentrant
2684 \emph default 
2685  keyword in the function declaration, e.g.:
2686 \newline 
2687
2688 \size small 
2689
2690 \newline 
2691
2692 \family typewriter 
2693 \size default 
2694 unsigned char foo(char i) reentrant 
2695 \newline 
2696
2697 \newline 
2698 ...
2699  
2700 \newline 
2701 }
2702 \newline 
2703
2704 \family default 
2705
2706 \newline 
2707 Since stack space on 8051 is limited, the 
2708 \emph on 
2709 reentrant 
2710 \emph default 
2711 keyword or the
2712 \emph on 
2713  --stack-auto
2714 \emph default 
2715  option should be used sparingly.
2716  Note that the reentrant keyword just means that the parameters & local
2717  variables will be allocated to the stack, it 
2718 \emph on 
2719 does not
2720 \emph default 
2721  mean that the function is register bank independent.
2722 \newline 
2723
2724 \newline 
2725 Local variables can be assigned storage classes and absolute addresses,
2726  e.g.: 
2727 \newline 
2728
2729 \newline 
2730
2731 \family typewriter 
2732 unsigned char foo() {
2733 \newline 
2734 \SpecialChar ~
2735 \SpecialChar ~
2736 \SpecialChar ~
2737 \SpecialChar ~
2738 xdata unsigned char i;
2739 \newline 
2740 \SpecialChar ~
2741 \SpecialChar ~
2742 \SpecialChar ~
2743 \SpecialChar ~
2744 bit bvar;
2745 \newline 
2746 \SpecialChar ~
2747 \SpecialChar ~
2748 \SpecialChar ~
2749 \SpecialChar ~
2750 data at 0x31 unsiged char j;
2751 \newline 
2752 \SpecialChar ~
2753 \SpecialChar ~
2754 \SpecialChar ~
2755 \SpecialChar ~
2756 ...
2757  
2758 \newline 
2759 }
2760 \newline 
2761
2762 \newline 
2763
2764 \family default 
2765 In the above example the variable 
2766 \emph on 
2767 i
2768 \emph default 
2769  will be allocated in the external ram, 
2770 \emph on 
2771 bvar
2772 \emph default 
2773  in bit addressable space and
2774 \emph on 
2775  j
2776 \emph default 
2777  in internal ram.
2778  When compiled with 
2779 \emph on 
2780 --stack-auto
2781 \emph default 
2782  or when a function is declared as 
2783 \emph on 
2784 reentrant
2785 \emph default 
2786  this should only be done for static variables.
2787 \layout Standard
2788
2789 Parameters however are not allowed any storage class, (storage classes for
2790  parameters will be ignored), their allocation is governed by the memory
2791  model in use, and the reentrancy options.
2792 \layout Subsection
2793
2794 Overlaying
2795 \layout Standard
2796
2797 For non-reentrant functions SDCC will try to reduce internal ram space usage
2798  by overlaying parameters and local variables of a function (if possible).
2799  Parameters and local variables of a function will be allocated to an overlayabl
2800 e segment if the function has 
2801 \emph on 
2802 no other function calls and the function is non-reentrant and the memory
2803  model is small.
2804
2805 \emph default 
2806  If an explicit storage class is specified for a local variable, it will
2807  NOT be overlayed.
2808 \layout Standard
2809
2810 Note that the compiler (not the linkage editor) makes the decision for overlayin
2811 g the data items.
2812  Functions that are called from an interrupt service routine should be preceded
2813  by a #pragma\SpecialChar ~
2814 NOOVERLAY if they are not reentrant.
2815 \layout Standard
2816
2817 Also note that the compiler does not do any processing of inline assembler
2818  code, so the compiler might incorrectly assign local variables and parameters
2819  of a function into the overlay segment if the inline assembler code calls
2820  other c-functions that might use the overlay.
2821  In that case the #pragma\SpecialChar ~
2822 NOOVERLAY should be used.
2823 \layout Standard
2824
2825 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
2826 tion or division will NOT be overlayed since these are implemented using
2827  external functions, e.g.:
2828 \newline 
2829
2830 \newline 
2831
2832 \family typewriter 
2833 #pragma SAVE 
2834 \newline 
2835 #pragma NOOVERLAY 
2836 \newline 
2837 void set_error(unsigned char errcd) 
2838 \newline 
2839 {
2840 \newline 
2841 \SpecialChar ~
2842 \SpecialChar ~
2843 \SpecialChar ~
2844 \SpecialChar ~
2845 P3 = errcd;
2846 \newline 
2847
2848 \newline 
2849 #pragma RESTORE 
2850 \newline 
2851
2852 \newline 
2853 void some_isr () interrupt 2 using 1 
2854 \newline 
2855 {
2856 \newline 
2857 \SpecialChar ~
2858 \SpecialChar ~
2859 \SpecialChar ~
2860 \SpecialChar ~
2861 ...
2862 \newline 
2863 \SpecialChar ~
2864 \SpecialChar ~
2865 \SpecialChar ~
2866 \SpecialChar ~
2867 set_error(10);
2868 \newline 
2869 \SpecialChar ~
2870 \SpecialChar ~
2871 \SpecialChar ~
2872 \SpecialChar ~
2873 ...
2874  
2875 \newline 
2876 }
2877 \newline 
2878
2879 \newline 
2880
2881 \family default 
2882 In the above example the parameter 
2883 \emph on 
2884 errcd
2885 \emph default 
2886  for the function 
2887 \emph on 
2888 set_error
2889 \emph default 
2890  would be assigned to the overlayable segment if the #pragma\SpecialChar ~
2891 NOOVERLAY was
2892  not present, this could cause unpredictable runtime behavior when called
2893  from an ISR.
2894  The #pragma\SpecialChar ~
2895 NOOVERLAY ensures that the parameters and local variables for
2896  the function are NOT overlayed.
2897 \layout Subsection
2898
2899 Interrupt Service Routines
2900 \layout Standard
2901
2902 SDCC allows interrupt service routines to be coded in C, with some extended
2903  keywords.
2904 \newline 
2905
2906 \newline 
2907
2908 \family typewriter 
2909 void timer_isr (void) interrupt 2 using 1 
2910 \newline 
2911
2912 \newline 
2913 ..
2914  
2915 \newline 
2916 }
2917 \newline 
2918
2919 \newline 
2920
2921 \family default 
2922 The number following the 
2923 \emph on 
2924 interrupt
2925 \emph default 
2926  keyword is the interrupt number this routine will service.
2927  The compiler will insert a call to this routine in the interrupt vector
2928  table for the interrupt number specified.
2929  The 
2930 \emph on 
2931 using
2932 \emph default 
2933  keyword is used to tell the compiler to use the specified register bank
2934  (8051 specific) when generating code for this function.
2935  Note that when some function is called from an interrupt service routine
2936  it should be preceded by a #pragma\SpecialChar ~
2937 NOOVERLAY if it is not reentrant.
2938  A special note here, int (16 bit) and long (32 bit) integer division, multiplic
2939 ation & modulus operations are implemented using external support routines
2940  developed in ANSI-C, if an interrupt service routine needs to do any of
2941  these operations then the support routines (as mentioned in a following
2942  section) will have to be recompiled using the
2943 \emph on 
2944  --stack-auto
2945 \emph default 
2946  option and the source file will need to be compiled using the 
2947 \emph on 
2948 --int-long-ren
2949 \emph default 
2950 t compiler option.
2951 \layout Standard
2952
2953 If you have multiple source files in your project, interrupt service routines
2954  can be present in any of them, but a prototype of the isr MUST be present
2955  or included in the file that contains the function 
2956 \emph on 
2957 main
2958 \emph default 
2959 .
2960 \layout Standard
2961
2962 Interrupt Numbers and the corresponding address & descriptions for the Standard
2963  8051 are listed below.
2964  SDCC will automatically adjust the interrupt vector table to the maximum
2965  interrupt number specified.
2966 \newline 
2967
2968 \layout Standard
2969
2970
2971 \begin_inset  Tabular
2972 <lyxtabular version="3" rows="6" columns="3">
2973 <features>
2974 <column alignment="center" valignment="top" leftline="true" width="0(null)">
2975 <column alignment="center" valignment="top" leftline="true" width="0(null)">
2976 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0(null)">
2977 <row topline="true" bottomline="true">
2978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2979 \begin_inset Text
2980
2981 \layout Standard
2982
2983 Interrupt #
2984 \end_inset 
2985 </cell>
2986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2987 \begin_inset Text
2988
2989 \layout Standard
2990
2991 Description
2992 \end_inset 
2993 </cell>
2994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2995 \begin_inset Text
2996
2997 \layout Standard
2998
2999 Vector Address
3000 \end_inset 
3001 </cell>
3002 </row>
3003 <row topline="true">
3004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3005 \begin_inset Text
3006
3007 \layout Standard
3008
3009 0
3010 \end_inset 
3011 </cell>
3012 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3013 \begin_inset Text
3014
3015 \layout Standard
3016
3017 External 0
3018 \end_inset 
3019 </cell>
3020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3021 \begin_inset Text
3022
3023 \layout Standard
3024
3025 0x0003
3026 \end_inset 
3027 </cell>
3028 </row>
3029 <row topline="true">
3030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3031 \begin_inset Text
3032
3033 \layout Standard
3034
3035 1
3036 \end_inset 
3037 </cell>
3038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3039 \begin_inset Text
3040
3041 \layout Standard
3042
3043 Timer 0
3044 \end_inset 
3045 </cell>
3046 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3047 \begin_inset Text
3048
3049 \layout Standard
3050
3051 0x000B
3052 \end_inset 
3053 </cell>
3054 </row>
3055 <row topline="true">
3056 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3057 \begin_inset Text
3058
3059 \layout Standard
3060
3061 2
3062 \end_inset 
3063 </cell>
3064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3065 \begin_inset Text
3066
3067 \layout Standard
3068
3069 External 1
3070 \end_inset 
3071 </cell>
3072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3073 \begin_inset Text
3074
3075 \layout Standard
3076
3077 0x0013
3078 \end_inset 
3079 </cell>
3080 </row>
3081 <row topline="true">
3082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3083 \begin_inset Text
3084
3085 \layout Standard
3086
3087 3
3088 \end_inset 
3089 </cell>
3090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3091 \begin_inset Text
3092
3093 \layout Standard
3094
3095 Timer 1
3096 \end_inset 
3097 </cell>
3098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3099 \begin_inset Text
3100
3101 \layout Standard
3102
3103 0x001B
3104 \end_inset 
3105 </cell>
3106 </row>
3107 <row topline="true" bottomline="true">
3108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3109 \begin_inset Text
3110
3111 \layout Standard
3112
3113 4
3114 \end_inset 
3115 </cell>
3116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3117 \begin_inset Text
3118
3119 \layout Standard
3120
3121 Serial
3122 \end_inset 
3123 </cell>
3124 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3125 \begin_inset Text
3126
3127 \layout Standard
3128
3129 0x0023
3130 \end_inset 
3131 </cell>
3132 </row>
3133 </lyxtabular>
3134
3135 \end_inset 
3136
3137
3138 \newline 
3139
3140 \newline 
3141 If the interrupt service routine is defined without 
3142 \emph on 
3143 using
3144 \emph default 
3145  a register bank or with register bank 0 (using 0), the compiler will save
3146  the registers used by itself on the stack upon entry and restore them at
3147  exit, however if such an interrupt service routine calls another function
3148  then the entire register bank will be saved on the stack.
3149  This scheme may be advantageous for small interrupt service routines which
3150  have low register usage.
3151 \layout Standard
3152
3153 If the interrupt service routine is defined to be using a specific register
3154  bank then only 
3155 \emph on 
3156 a, b & dptr
3157 \emph default 
3158  are save and restored, if such an interrupt service routine calls another
3159  function (using another register bank) then the entire register bank of
3160  the called function will be saved on the stack.
3161  This scheme is recommended for larger interrupt service routines.
3162 \layout Standard
3163
3164 Calling other functions from an interrupt service routine is not recommended,
3165  avoid it if possible.
3166 \newline 
3167
3168 \newline 
3169 Also see the _naked modifier.
3170 \layout Subsection
3171
3172 Critical Functions
3173 \layout Standard
3174
3175
3176 \shape italic 
3177 <TODO: this isn't implemented at all!>
3178 \shape default 
3179
3180 \newline 
3181
3182 \newline 
3183 A special keyword may be associated with a function declaring it as 
3184 \emph on 
3185 critical
3186 \emph default 
3187 .
3188  SDCC will generate code to disable all interrupts upon entry to a critical
3189  function and enable them back before returning.
3190  Note that nesting critical functions may cause unpredictable results.
3191 \newline 
3192
3193 \size small 
3194
3195 \newline 
3196
3197 \family typewriter 
3198 \size default 
3199 int foo () critical 
3200 \newline 
3201
3202 \newline 
3203 ...
3204  
3205 \newline 
3206 ...
3207  
3208 \newline 
3209 }
3210 \newline 
3211
3212 \family default 
3213
3214 \newline 
3215 The critical attribute maybe used with other attributes like 
3216 \emph on 
3217 reentrant.
3218 \layout Subsection
3219
3220 Naked Functions
3221 \layout Standard
3222
3223 A special keyword may be associated with a function declaring it as 
3224 \emph on 
3225 _naked.
3226  
3227 \emph default 
3228 The 
3229 \emph on 
3230 _naked
3231 \emph default 
3232  function modifier attribute prevents the compiler from generating prologue
3233  and epilogue code for that function.
3234  This means that the user is entirely responsible for such things as saving
3235  any registers that may need to be preserved, selecting the proper register
3236  bank, generating the 
3237 \emph on 
3238 return
3239 \emph default 
3240  instruction at the end, etc.
3241  Practically, this means that the contents of the function must be written
3242  in inline assembler.
3243  This is particularly useful for interrupt functions, which can have a large
3244  (and often unnecessary) prologue/epilogue.
3245  For example, compare the code generated by these two functions:
3246 \newline 
3247
3248 \newline 
3249
3250 \family typewriter 
3251 data unsigned char counter;
3252 \newline 
3253 void simpleInterrupt(void) interrupt 1
3254 \newline 
3255 {
3256 \newline 
3257 \SpecialChar ~
3258 \SpecialChar ~
3259 \SpecialChar ~
3260 \SpecialChar ~
3261 counter++;
3262 \newline 
3263 }
3264 \newline 
3265
3266 \newline 
3267 void nakedInterrupt(void) interrupt 2 _naked
3268 \newline 
3269 {
3270 \newline 
3271 \SpecialChar ~
3272 \SpecialChar ~
3273 \SpecialChar ~
3274 \SpecialChar ~
3275 _asm
3276 \newline 
3277 \SpecialChar ~
3278 \SpecialChar ~
3279 \SpecialChar ~
3280 \SpecialChar ~
3281 \SpecialChar ~
3282 \SpecialChar ~
3283 inc\SpecialChar ~
3284 \SpecialChar ~
3285 \SpecialChar ~
3286 \SpecialChar ~
3287 \SpecialChar ~
3288 _counter
3289 \newline 
3290 \SpecialChar ~
3291 \SpecialChar ~
3292 \SpecialChar ~
3293 \SpecialChar ~
3294 \SpecialChar ~
3295 \SpecialChar ~
3296 reti\SpecialChar ~
3297 \SpecialChar ~
3298 \SpecialChar ~
3299 \SpecialChar ~
3300 ; MUST explicitly include ret in _naked function.
3301 \newline 
3302 \SpecialChar ~
3303 \SpecialChar ~
3304 \SpecialChar ~
3305 \SpecialChar ~
3306 _endasm;
3307 \newline 
3308 }
3309 \family default 
3310
3311 \newline 
3312
3313 \newline 
3314 For an 8051 target, the generated simpleInterrupt looks like:
3315 \newline 
3316
3317 \newline 
3318
3319 \family typewriter 
3320 _simpleIterrupt:
3321 \newline 
3322 \SpecialChar ~
3323 \SpecialChar ~
3324 \SpecialChar ~
3325 \SpecialChar ~
3326 push\SpecialChar ~
3327 \SpecialChar ~
3328 \SpecialChar ~
3329 \SpecialChar ~
3330 acc
3331 \newline 
3332 \SpecialChar ~
3333 \SpecialChar ~
3334 \SpecialChar ~
3335 \SpecialChar ~
3336 push\SpecialChar ~
3337 \SpecialChar ~
3338 \SpecialChar ~
3339 \SpecialChar ~
3340 b
3341 \newline 
3342 \SpecialChar ~
3343 \SpecialChar ~
3344 \SpecialChar ~
3345 \SpecialChar ~
3346 push\SpecialChar ~
3347 \SpecialChar ~
3348 \SpecialChar ~
3349 \SpecialChar ~
3350 dpl
3351 \newline 
3352 \SpecialChar ~
3353 \SpecialChar ~
3354 \SpecialChar ~
3355 \SpecialChar ~
3356 push\SpecialChar ~
3357 \SpecialChar ~
3358 \SpecialChar ~
3359 \SpecialChar ~
3360 dph
3361 \newline 
3362 \SpecialChar ~
3363 \SpecialChar ~
3364 \SpecialChar ~
3365 \SpecialChar ~
3366 push\SpecialChar ~
3367 \SpecialChar ~
3368 \SpecialChar ~
3369 \SpecialChar ~
3370 psw
3371 \newline 
3372 \SpecialChar ~
3373 \SpecialChar ~
3374 \SpecialChar ~
3375 \SpecialChar ~
3376 mov\SpecialChar ~
3377 \SpecialChar ~
3378 \SpecialChar ~
3379 \SpecialChar ~
3380 \SpecialChar ~
3381 psw,#0x00
3382 \newline 
3383 \SpecialChar ~
3384 \SpecialChar ~
3385 \SpecialChar ~
3386 \SpecialChar ~
3387 inc\SpecialChar ~
3388 \SpecialChar ~
3389 \SpecialChar ~
3390 \SpecialChar ~
3391 \SpecialChar ~
3392 _counter
3393 \newline 
3394 \SpecialChar ~
3395 \SpecialChar ~
3396 \SpecialChar ~
3397 \SpecialChar ~
3398 pop\SpecialChar ~
3399 \SpecialChar ~
3400 \SpecialChar ~
3401 \SpecialChar ~
3402 \SpecialChar ~
3403 psw
3404 \newline 
3405 \SpecialChar ~
3406 \SpecialChar ~
3407 \SpecialChar ~
3408 \SpecialChar ~
3409 pop\SpecialChar ~
3410 \SpecialChar ~
3411 \SpecialChar ~
3412 \SpecialChar ~
3413 \SpecialChar ~
3414 dph
3415 \newline 
3416 \SpecialChar ~
3417 \SpecialChar ~
3418 \SpecialChar ~
3419 \SpecialChar ~
3420 pop\SpecialChar ~
3421 \SpecialChar ~
3422 \SpecialChar ~
3423 \SpecialChar ~
3424 \SpecialChar ~
3425 dpl
3426 \newline 
3427 \SpecialChar ~
3428 \SpecialChar ~
3429 \SpecialChar ~
3430 \SpecialChar ~
3431 pop\SpecialChar ~
3432 \SpecialChar ~
3433 \SpecialChar ~
3434 \SpecialChar ~
3435 \SpecialChar ~
3436 b
3437 \newline 
3438 \SpecialChar ~
3439 \SpecialChar ~
3440 \SpecialChar ~
3441 \SpecialChar ~
3442 pop\SpecialChar ~
3443 \SpecialChar ~
3444 \SpecialChar ~
3445 \SpecialChar ~
3446 \SpecialChar ~
3447 acc
3448 \newline 
3449 \SpecialChar ~
3450 \SpecialChar ~
3451 \SpecialChar ~
3452 \SpecialChar ~
3453 reti
3454 \family default 
3455
3456 \newline 
3457
3458 \newline 
3459 whereas nakedInterrupt looks like:
3460 \newline 
3461
3462 \newline 
3463
3464 \family typewriter 
3465 _nakedInterrupt:
3466 \newline 
3467 \SpecialChar ~
3468 \SpecialChar ~
3469 \SpecialChar ~
3470 \SpecialChar ~
3471 inc\SpecialChar ~
3472 \SpecialChar ~
3473 \SpecialChar ~
3474 \SpecialChar ~
3475 _counter
3476 \newline 
3477 \SpecialChar ~
3478 \SpecialChar ~
3479 \SpecialChar ~
3480 \SpecialChar ~
3481 reti\SpecialChar ~
3482 \SpecialChar ~
3483 \SpecialChar ~
3484 ; MUST explicitly include ret(i) in _naked function.
3485 \family default 
3486
3487 \newline 
3488
3489 \newline 
3490 While there is nothing preventing you from writing C code inside a _naked
3491  function, there are many ways to shoot yourself in the foot doing this,
3492  and it is recommended that you stick to inline assembler.
3493 \layout Subsection
3494
3495 Functions using private banks
3496 \layout Standard
3497
3498 The 
3499 \emph on 
3500 using
3501 \emph default 
3502  attribute (which tells the compiler to use a register bank other than the
3503  default bank zero) should only be applied to 
3504 \emph on 
3505 interrupt
3506 \emph default 
3507  functions (see note 1 below).
3508  This will in most circumstances make the generated ISR code more efficient
3509  since it will not have to save registers on the stack.
3510 \layout Standard
3511
3512 The 
3513 \emph on 
3514 using
3515 \emph default 
3516  attribute will have no effect on the generated code for a 
3517 \emph on 
3518 non-interrupt
3519 \emph default 
3520  function (but may occasionally be useful anyway
3521 \begin_inset Foot
3522 collapsed true
3523
3524 \layout Standard
3525
3526 possible exception: if a function is called ONLY from 'interrupt' functions
3527  using a particular bank, it can be declared with the same 'using' attribute
3528  as the calling 'interrupt' functions.
3529  For instance, if you have several ISRs using bank one, and all of them
3530  call memcpy(), it might make sense to create a specialized version of memcpy()
3531  'using 1', since this would prevent the ISR from having to save bank zero
3532  to the stack on entry and switch to bank zero before calling the function
3533 \end_inset 
3534
3535 ).
3536 \newline 
3537
3538 \emph on 
3539 (pending: I don't think this has been done yet)
3540 \layout Standard
3541
3542 An 
3543 \emph on 
3544 interrupt
3545 \emph default 
3546  function using a non-zero bank will assume that it can trash that register
3547  bank, and will not save it.
3548  Since high-priority interrupts can interrupt low-priority ones on the 8051
3549  and friends, this means that if a high-priority ISR 
3550 \emph on 
3551 using
3552 \emph default 
3553  a particular bank occurs while processing a low-priority ISR 
3554 \emph on 
3555 using
3556 \emph default 
3557  the same bank, terrible and bad things can happen.
3558  To prevent this, no single register bank should be 
3559 \emph on 
3560 used
3561 \emph default 
3562  by both a high priority and a low priority ISR.
3563  This is probably most easily done by having all high priority ISRs use
3564  one bank and all low priority ISRs use another.
3565  If you have an ISR which can change priority at runtime, you're on your
3566  own: I suggest using the default bank zero and taking the small performance
3567  hit.
3568 \layout Standard
3569
3570 It is most efficient if your ISR calls no other functions.
3571  If your ISR must call other functions, it is most efficient if those functions
3572  use the same bank as the ISR (see note 1 below); the next best is if the
3573  called functions use bank zero.
3574  It is very inefficient to call a function using a different, non-zero bank
3575  from an ISR.
3576  
3577 \layout Subsection
3578
3579 Absolute Addressing
3580 \layout Standard
3581
3582 Data items can be assigned an absolute address with the 
3583 \emph on 
3584 at <address>
3585 \emph default 
3586  keyword, in addition to a storage class, e.g.:
3587 \newline 
3588
3589 \newline 
3590
3591 \family typewriter 
3592 xdata at 0x8000 unsigned char PORTA_8255 ;
3593 \newline 
3594
3595 \family default 
3596
3597 \newline 
3598 In the above example the PORTA_8255 will be allocated to the location 0x8000
3599  of the external ram.
3600  Note that this feature is provided to give the programmer access to 
3601 \emph on 
3602 memory mapped
3603 \emph default 
3604  devices attached to the controller.
3605  The compiler does not actually reserve any space for variables declared
3606  in this way (they are implemented with an equate in the assembler).
3607  Thus it is left to the programmer to make sure there are no overlaps with
3608  other variables that are declared without the absolute address.
3609  The assembler listing file (.lst) and the linker output files (.rst) and
3610  (.map) are a good places to look for such overlaps.
3611 \newline 
3612
3613 \newline 
3614 Absolute address can be specified for variables in all storage classes,
3615  e.g.:
3616 \newline 
3617
3618 \newline 
3619
3620 \family typewriter 
3621 bit at 0x02 bvar;
3622 \newline 
3623
3624 \newline 
3625
3626 \family default 
3627 The above example will allocate the variable at offset 0x02 in the bit-addressab
3628 le space.
3629  There is no real advantage to assigning absolute addresses to variables
3630  in this manner, unless you want strict control over all the variables allocated.
3631 \layout Subsection
3632
3633 Startup Code
3634 \layout Standard
3635
3636 The compiler inserts a call to the C routine 
3637 \emph on 
3638 _sdcc__external__startup()
3639 \series bold 
3640 \emph default 
3641  
3642 \series default 
3643 at the start of the CODE area.
3644  This routine is in the runtime library.
3645  By default this routine returns 0, if this routine returns a non-zero value,
3646  the static & global variable initialization will be skipped and the function
3647  main will be invoked Other wise static & global variables will be initialized
3648  before the function main is invoked.
3649  You could add a 
3650 \emph on 
3651 _sdcc__external__startup()
3652 \emph default 
3653  routine to your program to override the default if you need to setup hardware
3654  or perform some other critical operation prior to static & global variable
3655  initialization.
3656 \layout Subsection
3657
3658 Inline Assembler Code
3659 \layout Standard
3660
3661 SDCC allows the use of in-line assembler with a few restriction as regards
3662  labels.
3663  All labels defined within inline assembler code 
3664 \emph on 
3665 has to be
3666 \emph default 
3667  of the form 
3668 \emph on 
3669 nnnnn$
3670 \emph default 
3671  where nnnn is a number less than 100 (which implies a limit of utmost 100
3672  inline assembler labels 
3673 \emph on 
3674 per function
3675 \emph default 
3676 \noun on 
3677 )
3678 \noun default 
3679 .
3680  It is strongly recommended that each assembly instruction (including labels)
3681  be placed in a separate line (as the example shows).
3682  When the 
3683 \emph on 
3684 --peep-asm
3685 \emph default 
3686  command line option is used, the inline assembler code will be passed through
3687  the peephole optimizer.
3688  This might cause some unexpected changes in the inline assembler code.
3689  Please go throught the peephole optimizer rules defined in file 
3690 \emph on 
3691 SDCCpeeph.def
3692 \emph default 
3693  carefully before using this option.
3694 \newline 
3695
3696 \newline 
3697
3698 \family typewriter 
3699 _asm 
3700 \newline 
3701 \SpecialChar ~
3702 \SpecialChar ~
3703 \SpecialChar ~
3704 \SpecialChar ~
3705 mov\SpecialChar ~
3706 \SpecialChar ~
3707 \SpecialChar ~
3708 \SpecialChar ~
3709 \SpecialChar ~
3710 b,#10 
3711 \newline 
3712 00001$: 
3713 \newline 
3714 \SpecialChar ~
3715 \SpecialChar ~
3716 \SpecialChar ~
3717 \SpecialChar ~
3718 djnz\SpecialChar ~
3719 \SpecialChar ~
3720 \SpecialChar ~
3721 \SpecialChar ~
3722 b,00001$ 
3723 \newline 
3724 _endasm ;
3725 \family default 
3726 \size small 
3727
3728 \newline 
3729
3730 \newline 
3731
3732 \size default 
3733 The inline assembler code can contain any valid code understood by the assembler
3734 , this includes any assembler directives and comment lines.
3735  The compiler does not do any validation of the code within the 
3736 \family typewriter 
3737 _asm ...
3738  _endasm;
3739 \family default 
3740  keyword pair.
3741  
3742 \newline 
3743
3744 \newline 
3745 Inline assembler code cannot reference any C-Labels, however it can reference
3746  labels defined by the inline assembler, e.g.:
3747 \newline 
3748
3749 \newline 
3750
3751 \family typewriter 
3752 foo() { 
3753 \newline 
3754 \SpecialChar ~
3755 \SpecialChar ~
3756 \SpecialChar ~
3757 \SpecialChar ~
3758 /* some c code */ 
3759 \newline 
3760 \SpecialChar ~
3761 \SpecialChar ~
3762 \SpecialChar ~
3763 \SpecialChar ~
3764 _asm 
3765 \newline 
3766 \SpecialChar ~
3767 \SpecialChar ~
3768 \SpecialChar ~
3769 \SpecialChar ~
3770 \SpecialChar ~
3771 \SpecialChar ~
3772 ; some assembler code 
3773 \newline 
3774 \SpecialChar ~
3775 \SpecialChar ~
3776 \SpecialChar ~
3777 \SpecialChar ~
3778 \SpecialChar ~
3779 \SpecialChar ~
3780 ljmp $0003 
3781 \newline 
3782 \SpecialChar ~
3783 \SpecialChar ~
3784 \SpecialChar ~
3785 \SpecialChar ~
3786 _endasm; 
3787 \newline 
3788 \SpecialChar ~
3789 \SpecialChar ~
3790 \SpecialChar ~
3791 \SpecialChar ~
3792 /* some more c code */ 
3793 \newline 
3794 clabel:\SpecialChar ~
3795 \SpecialChar ~
3796 /* inline assembler cannot reference this label */ 
3797 \newline 
3798 \SpecialChar ~
3799 \SpecialChar ~
3800 \SpecialChar ~
3801 \SpecialChar ~
3802 _asm
3803 \newline 
3804 \SpecialChar ~
3805 \SpecialChar ~
3806 \SpecialChar ~
3807 \SpecialChar ~
3808 $0003: ;label (can be reference by inline assembler only) 
3809 \newline 
3810 \SpecialChar ~
3811 \SpecialChar ~
3812 \SpecialChar ~
3813 \SpecialChar ~
3814 _endasm ; 
3815 \newline 
3816 \SpecialChar ~
3817 \SpecialChar ~
3818 \SpecialChar ~
3819 \SpecialChar ~
3820 /* some more c code */
3821 \newline 
3822 }
3823 \newline 
3824
3825 \newline 
3826
3827 \family default 
3828 In other words inline assembly code can access labels defined in inline
3829  assembly within the scope of the funtion.
3830  
3831 \layout Standard
3832
3833 The same goes the other way, ie.
3834  labels defines in inline assembly CANNOT be accessed by C statements.
3835 \layout Subsection
3836
3837 int(16 bit) and long (32 bit) Support
3838 \layout Standard
3839
3840 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
3841  multiplication and modulus operations are implemented by support routines.
3842  These support routines are all developed in ANSI-C to facilitate porting
3843  to other MCUs, although some model specific assembler optimations are used.
3844  The following files contain the described routine, all of them can be found
3845  in <installdir>/share/sdcc/lib.
3846 \newline 
3847
3848 \newline 
3849
3850 \emph on 
3851 <pending: tabularise this>
3852 \emph default 
3853
3854 \newline 
3855
3856 \newline 
3857 _mulsint.c - signed 16 bit multiplication (calls _muluint)
3858 \newline 
3859 _muluint.c - unsigned 16 bit multiplication
3860 \newline 
3861 _divsint.c - signed 16 bit division (calls _divuint)
3862 \newline 
3863 _divuint.c - unsigned 16 bit division
3864 \newline 
3865 _modsint.c - signed 16 bit modulus (call _moduint)
3866 \newline 
3867 _moduint.c - unsigned 16 bit modulus
3868 \newline 
3869 _mulslong.c - signed 32 bit multiplication (calls _mululong)
3870 \newline 
3871 _mululong.c - unsigned32 bit multiplication
3872 \newline 
3873 _divslong.c - signed 32 division (calls _divulong)
3874 \newline 
3875 _divulong.c - unsigned 32 division
3876 \newline 
3877 _modslong.c - signed 32 bit modulus (calls _modulong)
3878 \newline 
3879 _modulong.c - unsigned 32 bit modulus 
3880 \size footnotesize 
3881
3882 \newline 
3883
3884 \newline 
3885
3886 \size default 
3887 Since they are compiled as 
3888 \emph on 
3889 non-reentrant
3890 \emph default 
3891 , interrupt service routines should not do any of the above operations.
3892  If this is unavoidable then the above routines will need to be compiled
3893  with the 
3894 \emph on 
3895 --stack-auto
3896 \emph default 
3897  option, after which the source program will have to be compiled with 
3898 \emph on 
3899 --int-long-rent
3900 \emph default 
3901  option.
3902 \layout Subsection
3903
3904 Floating Point Support
3905 \layout Standard
3906
3907 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
3908  point support routines are derived from gcc's floatlib.c and consists of
3909  the following routines:
3910 \newline 
3911
3912 \newline 
3913
3914 \emph on 
3915 <pending: tabularise this>
3916 \emph default 
3917
3918 \newline 
3919
3920 \newline 
3921 _fsadd.c - add floating point numbers
3922 \newline 
3923 _fssub.c - subtract floating point numbers
3924 \newline 
3925 _fsdiv.c - divide floating point numbers
3926 \newline 
3927 _fsmul.c - multiply floating point numbers
3928 \newline 
3929 _fs2uchar.c - convert floating point to unsigned char
3930 \newline 
3931 _fs2char.c - convert floating point to signed char
3932 \newline 
3933 _fs2uint.c - convert floating point to unsigned int
3934 \newline 
3935 _fs2int.c - convert floating point to signed int
3936 \newline 
3937 _fs2ulong.c - convert floating point to unsigned long
3938 \newline 
3939 _fs2long.c - convert floating point to signed long
3940 \newline 
3941 _uchar2fs.c - convert unsigned char to floating point
3942 \newline 
3943 _char2fs.c - convert char to floating point number
3944 \newline 
3945 _uint2fs.c - convert unsigned int to floating point
3946 \newline 
3947 _int2fs.c - convert int to floating point numbers
3948 \newline 
3949 _ulong2fs.c - convert unsigned long to floating point number
3950 \newline 
3951 _long2fs.c - convert long to floating point number
3952 \size footnotesize 
3953
3954 \newline 
3955
3956 \newline 
3957
3958 \size default 
3959 Note if all these routines are used simultaneously the data space might
3960  overflow.
3961  For serious floating point usage it is strongly recommended that the large
3962  model be used.
3963 \layout Subsection
3964
3965 MCS51 Memory Models
3966 \layout Standard
3967
3968 SDCC allows two memory models for MCS51 code, small and large.
3969  Modules compiled with different memory models should 
3970 \emph on 
3971 never
3972 \emph default 
3973  be combined together or the results would be unpredictable.
3974  The library routines supplied with the compiler are compiled as both small
3975  and large.
3976  The compiled library modules are contained in seperate directories as small
3977  and large so that you can link to either set.
3978  
3979 \layout Standard
3980
3981 When the large model is used all variables declared without a storage class
3982  will be allocated into the external ram, this includes all parameters and
3983  local variables (for non-reentrant functions).
3984  When the small model is used variables without storage class are allocated
3985  in the internal ram.
3986 \layout Standard
3987
3988 Judicious usage of the processor specific storage classes and the 'reentrant'
3989  function type will yield much more efficient code, than using the large
3990  model.
3991  Several optimizations are disabled when the program is compiled using the
3992  large model, it is therefore strongly recommdended that the small model
3993  be used unless absolutely required.
3994 \layout Subsection
3995
3996 DS390 Memory Models
3997 \layout Standard
3998
3999 The only model supported is Flat 24.
4000  This generates code for the 24 bit contiguous addressing mode of the Dallas
4001  DS80C390 part.
4002  In this mode, up to four meg of external RAM or code space can be directly
4003  addressed.
4004  See the data sheets at www.dalsemi.com for further information on this part.
4005 \newline 
4006
4007 \newline 
4008 In older versions of the compiler, this option was used with the MCS51 code
4009  generator (
4010 \emph on 
4011 -mmcs51
4012 \emph default 
4013 ).
4014  Now, however, the '390 has it's own code generator, selected by the 
4015 \emph on 
4016 -mds390
4017 \emph default 
4018  switch.
4019  
4020 \newline 
4021
4022 \newline 
4023 Note that the compiler does not generate any code to place the processor
4024  into 24 bitmode (although 
4025 \emph on 
4026 tinibios
4027 \emph default 
4028  in the ds390 libraries will do that for you).
4029  If you don't use 
4030 \emph on 
4031 tinibios
4032 \emph default 
4033 , the boot loader or similar code must ensure that the processor is in 24
4034  bit contiguous addressing mode before calling the SDCC startup code.
4035 \newline 
4036
4037 \newline 
4038 Like the 
4039 \emph on 
4040 --model-large
4041 \emph default 
4042  option, variables will by default be placed into the XDATA segment.
4043  
4044 \newline 
4045
4046 \newline 
4047 Segments may be placed anywhere in the 4 meg address space using the usual
4048  --*-loc options.
4049  Note that if any segments are located above 64K, the -r flag must be passed
4050  to the linker to generate the proper segment relocations, and the Intel
4051  HEX output format must be used.
4052  The -r flag can be passed to the linker by using the option 
4053 \emph on 
4054 -Wl-r
4055 \emph default 
4056  on the sdcc command line.
4057  However, currently the linker can not handle code segments > 64k.
4058 \layout Subsection
4059
4060 Defines Created by the Compiler
4061 \layout Standard
4062
4063 The compiler creates the following #defines.
4064 \layout Itemize
4065
4066 SDCC - this Symbol is always defined.
4067 \layout Itemize
4068
4069 SDCC_mcs51 or SDCC_ds390 or SDCC_z80, etc - depending on the model used
4070  (e.g.: -mds390)
4071 \layout Itemize
4072
4073 __mcs51 or __ds390 or __z80, etc - depending on the model used (e.g.
4074  -mz80)
4075 \layout Itemize
4076
4077 SDCC_STACK_AUTO - this symbol is defined when 
4078 \emph on 
4079 --stack-auto
4080 \emph default 
4081  option is used.
4082 \layout Itemize
4083
4084 SDCC_MODEL_SMALL - when 
4085 \emph on 
4086 --model-small
4087 \emph default 
4088  is used.
4089 \layout Itemize
4090
4091 SDCC_MODEL_LARGE - when 
4092 \emph on 
4093 --model-large
4094 \emph default 
4095  is used.
4096 \layout Itemize
4097
4098 SDCC_USE_XSTACK - when 
4099 \emph on 
4100 --xstack
4101 \emph default 
4102  option is used.
4103 \layout Itemize
4104
4105 SDCC_STACK_TENBIT - when 
4106 \emph on 
4107 -mds390
4108 \emph default 
4109  is used
4110 \layout Itemize
4111
4112 SDCC_MODEL_FLAT24 - when 
4113 \emph on 
4114 -mds390
4115 \emph default 
4116  is used
4117 \layout Section
4118
4119 SDCC Technical Data
4120 \layout Subsection
4121
4122 Optimizations
4123 \layout Standard
4124
4125 SDCC performs a host of standard optimizations in addition to some MCU specific
4126  optimizations.
4127  
4128 \layout Subsubsection
4129
4130 Sub-expression Elimination
4131 \layout Standard
4132
4133 The compiler does local and global common subexpression elimination, e.g.:
4134  
4135 \newline 
4136
4137 \newline 
4138
4139 \family typewriter 
4140 i = x + y + 1; 
4141 \newline 
4142 j = x + y;
4143 \family default 
4144
4145 \newline 
4146
4147 \newline 
4148 will be translated to
4149 \newline 
4150
4151 \newline 
4152
4153 \family typewriter 
4154 iTemp = x + y 
4155 \newline 
4156 i = iTemp + 1 
4157 \newline 
4158 j = iTemp
4159 \newline 
4160
4161 \family default 
4162
4163 \newline 
4164 Some subexpressions are not as obvious as the above example, e.g.:
4165 \newline 
4166
4167 \newline 
4168
4169 \family typewriter 
4170 a->b[i].c = 10; 
4171 \newline 
4172 a->b[i].d = 11;
4173 \family default 
4174
4175 \newline 
4176
4177 \newline 
4178 In this case the address arithmetic a->b[i] will be computed only once;
4179  the equivalent code in C would be.
4180 \newline 
4181
4182 \newline 
4183
4184 \family typewriter 
4185 iTemp = a->b[i]; 
4186 \newline 
4187 iTemp.c = 10; 
4188 \newline 
4189 iTemp.d = 11;
4190 \family default 
4191
4192 \newline 
4193
4194 \newline 
4195 The compiler will try to keep these temporary variables in registers.
4196 \layout Subsubsection
4197
4198 Dead-Code Elimination
4199 \layout Standard
4200
4201
4202 \family typewriter 
4203 int global; 
4204 \newline 
4205 void f () { 
4206 \newline 
4207 \SpecialChar ~
4208 \SpecialChar ~
4209 int i; 
4210 \newline 
4211 \SpecialChar ~
4212 \SpecialChar ~
4213 i = 1; \SpecialChar ~
4214 /* dead store */ 
4215 \newline 
4216 \SpecialChar ~
4217 \SpecialChar ~
4218 global = 1;\SpecialChar ~
4219 /* dead store */ 
4220 \newline 
4221 \SpecialChar ~
4222 \SpecialChar ~
4223 global = 2; 
4224 \newline 
4225 \SpecialChar ~
4226 \SpecialChar ~
4227 return; 
4228 \newline 
4229 \SpecialChar ~
4230 \SpecialChar ~
4231 global = 3;\SpecialChar ~
4232 /* unreachable */ 
4233 \newline 
4234 }
4235 \family default 
4236
4237 \newline 
4238
4239 \newline 
4240 will be changed to
4241 \newline 
4242
4243 \newline 
4244
4245 \family typewriter 
4246 int global; void f () 
4247 \newline 
4248 {
4249 \newline 
4250 \SpecialChar ~
4251 \SpecialChar ~
4252 global = 2; 
4253 \newline 
4254 \SpecialChar ~
4255 \SpecialChar ~
4256 return; 
4257 \newline 
4258 }
4259 \layout Subsubsection
4260
4261 Copy-Propagation
4262 \layout Standard
4263
4264
4265 \family typewriter 
4266 int f() { 
4267 \newline 
4268 \SpecialChar ~
4269 \SpecialChar ~
4270 int i, j; 
4271 \newline 
4272 \SpecialChar ~
4273 \SpecialChar ~
4274 i = 10; 
4275 \newline 
4276 \SpecialChar ~
4277 \SpecialChar ~
4278 j = i; 
4279 \newline 
4280 \SpecialChar ~
4281 \SpecialChar ~
4282 return j; 
4283 \newline 
4284 }
4285 \family default 
4286
4287 \newline 
4288
4289 \newline 
4290 will be changed to 
4291 \newline 
4292
4293 \newline 
4294
4295 \family typewriter 
4296 int f() { 
4297 \newline 
4298 \SpecialChar ~
4299  \SpecialChar ~
4300  int i,j; 
4301 \newline 
4302 \SpecialChar ~
4303  \SpecialChar ~
4304  i = 10; 
4305 \newline 
4306 \SpecialChar ~
4307  \SpecialChar ~
4308  j = 10; 
4309 \newline 
4310 \SpecialChar ~
4311  \SpecialChar ~
4312  return 10; 
4313 \newline 
4314 }
4315 \newline 
4316
4317 \newline 
4318
4319 \family default 
4320 Note: the dead stores created by this copy propagation will be eliminated
4321  by dead-code elimination.
4322 \layout Subsubsection
4323
4324 Loop Optimizations
4325 \layout Standard
4326
4327 Two types of loop optimizations are done by SDCC loop invariant lifting
4328  and strength reduction of loop induction variables.
4329  In addition to the strength reduction the optimizer marks the induction
4330  variables and the register allocator tries to keep the induction variables
4331  in registers for the duration of the loop.
4332  Because of this preference of the register allocator, loop induction optimizati
4333 on causes an increase in register pressure, which may cause unwanted spilling
4334  of other temporary variables into the stack / data space.
4335  The compiler will generate a warning message when it is forced to allocate
4336  extra space either on the stack or data space.
4337  If this extra space allocation is undesirable then induction optimization
4338  can be eliminated either for the entire source file (with --noinduction
4339  option) or for a given function only using #pragma\SpecialChar ~
4340 NOINDUCTION.
4341 \newline 
4342
4343 \newline 
4344 Loop Invariant:
4345 \newline 
4346
4347 \newline 
4348
4349 \family typewriter 
4350 for (i = 0 ; i < 100 ; i ++) 
4351 \newline 
4352  \SpecialChar ~
4353  \SpecialChar ~
4354 f += k + l;
4355 \family default 
4356
4357 \newline 
4358
4359 \newline 
4360 changed to
4361 \newline 
4362
4363 \newline 
4364
4365 \family typewriter 
4366 itemp = k + l; 
4367 \newline 
4368 for (i = 0; i < 100; i++) 
4369 \newline 
4370 \SpecialChar ~
4371 \SpecialChar ~
4372 f += itemp;
4373 \family default 
4374
4375 \newline 
4376
4377 \newline 
4378 As mentioned previously some loop invariants are not as apparent, all static
4379  address computations are also moved out of the loop.
4380 \newline 
4381
4382 \newline 
4383 Strength Reduction, this optimization substitutes an expression by a cheaper
4384  expression:
4385 \newline 
4386
4387 \newline 
4388
4389 \family typewriter 
4390 for (i=0;i < 100; i++)
4391 \newline 
4392 \SpecialChar ~
4393 \SpecialChar ~
4394 ar[i*5] = i*3;
4395 \family default 
4396
4397 \newline 
4398
4399 \newline 
4400 changed to
4401 \newline 
4402
4403 \newline 
4404
4405 \family typewriter 
4406 itemp1 = 0; 
4407 \newline 
4408 itemp2 = 0; 
4409 \newline 
4410 for (i=0;i< 100;i++) { 
4411 \newline 
4412  \SpecialChar ~
4413  \SpecialChar ~
4414 ar[itemp1] = itemp2; 
4415 \newline 
4416  \SpecialChar ~
4417  \SpecialChar ~
4418 itemp1 += 5; 
4419 \newline 
4420  \SpecialChar ~
4421  \SpecialChar ~
4422 itemp2 += 3; 
4423 \newline 
4424 }
4425 \family default 
4426
4427 \newline 
4428
4429 \newline 
4430 The more expensive multiplication is changed to a less expensive addition.
4431 \layout Subsubsection
4432
4433 Loop Reversing
4434 \layout Standard
4435
4436 This optimization is done to reduce the overhead of checking loop boundaries
4437  for every iteration.
4438  Some simple loops can be reversed and implemented using a 
4439 \begin_inset Quotes eld
4440 \end_inset 
4441
4442 decrement and jump if not zero
4443 \begin_inset Quotes erd
4444 \end_inset 
4445
4446  instruction.
4447  SDCC checks for the following criterion to determine if a loop is reversible
4448  (note: more sophisticated compilers use data-dependency analysis to make
4449  this determination, SDCC uses a more simple minded analysis).
4450 \layout Itemize
4451
4452 The 'for' loop is of the form 
4453 \newline 
4454
4455 \newline 
4456
4457 \family typewriter 
4458 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
4459  <sym> += 1])
4460 \newline 
4461 \SpecialChar ~
4462 \SpecialChar ~
4463 \SpecialChar ~
4464 \SpecialChar ~
4465 <for body>
4466 \layout Itemize
4467
4468 The <for body> does not contain 
4469 \begin_inset Quotes eld
4470 \end_inset 
4471
4472 continue
4473 \begin_inset Quotes erd
4474 \end_inset 
4475
4476  or 'break
4477 \begin_inset Quotes erd
4478 \end_inset 
4479
4480 .
4481 \layout Itemize
4482
4483 All goto's are contained within the loop.
4484 \layout Itemize
4485
4486 No function calls within the loop.
4487 \layout Itemize
4488
4489 The loop control variable <sym> is not assigned any value within the loop
4490 \layout Itemize
4491
4492 The loop control variable does NOT participate in any arithmetic operation
4493  within the loop.
4494 \layout Itemize
4495
4496 There are NO switch statements in the loop.
4497 \layout Subsubsection
4498
4499 Algebraic Simplifications
4500 \layout Standard
4501
4502 SDCC does numerous algebraic simplifications, the following is a small sub-set
4503  of these optimizations.
4504 \newline 
4505
4506 \newline 
4507
4508 \family typewriter 
4509 i = j + 0 ; /* changed to */ i = j; 
4510 \newline 
4511 i /= 2; /* changed to */ i >>= 1; 
4512 \newline 
4513 i = j - j ; /* changed to */ i = 0; 
4514 \newline 
4515 i = j / 1 ; /* changed to */ i = j;
4516 \family default 
4517
4518 \newline 
4519
4520 \newline 
4521 Note the subexpressions given above are generally introduced by macro expansions
4522  or as a result of copy/constant propagation.
4523 \layout Subsubsection
4524
4525 'switch' Statements
4526 \layout Standard
4527
4528 SDCC changes switch statements to jump tables when the following conditions
4529  are true.
4530  
4531 \layout Itemize
4532
4533 The case labels are in numerical sequence, the labels need not be in order,
4534  and the starting number need not be one or zero.
4535 \newline 
4536
4537 \newline 
4538
4539 \family typewriter 
4540 switch(i) {\SpecialChar ~
4541  \SpecialChar ~
4542  \SpecialChar ~
4543  \SpecialChar ~
4544  \SpecialChar ~
4545  \SpecialChar ~
4546  \SpecialChar ~
4547  \SpecialChar ~
4548  \SpecialChar ~
4549  \SpecialChar ~
4550  \SpecialChar ~
4551  \SpecialChar ~
4552  \SpecialChar ~
4553 switch (i) { 
4554 \newline 
4555 case 4:...
4556  \SpecialChar ~
4557  \SpecialChar ~
4558  \SpecialChar ~
4559  \SpecialChar ~
4560  \SpecialChar ~
4561  \SpecialChar ~
4562  \SpecialChar ~
4563  \SpecialChar ~
4564  \SpecialChar ~
4565  \SpecialChar ~
4566  \SpecialChar ~
4567  \SpecialChar ~
4568  \SpecialChar ~
4569 case 1: ...
4570  
4571 \newline 
4572 case 5:...
4573  \SpecialChar ~
4574  \SpecialChar ~
4575  \SpecialChar ~
4576  \SpecialChar ~
4577  \SpecialChar ~
4578  \SpecialChar ~
4579  \SpecialChar ~
4580  \SpecialChar ~
4581  \SpecialChar ~
4582  \SpecialChar ~
4583  \SpecialChar ~
4584  \SpecialChar ~
4585  \SpecialChar ~
4586 case 2: ...
4587  
4588 \newline 
4589 case 3:...
4590  \SpecialChar ~
4591  \SpecialChar ~
4592  \SpecialChar ~
4593  \SpecialChar ~
4594  \SpecialChar ~
4595  \SpecialChar ~
4596  \SpecialChar ~
4597  \SpecialChar ~
4598  \SpecialChar ~
4599  \SpecialChar ~
4600  \SpecialChar ~
4601  \SpecialChar ~
4602  \SpecialChar ~
4603 case 3: ...
4604  
4605 \newline 
4606 case 6:...
4607  \SpecialChar ~
4608  \SpecialChar ~
4609  \SpecialChar ~
4610  \SpecialChar ~
4611  \SpecialChar ~
4612  \SpecialChar ~
4613  \SpecialChar ~
4614  \SpecialChar ~
4615  \SpecialChar ~
4616  \SpecialChar ~
4617  \SpecialChar ~
4618  \SpecialChar ~
4619  \SpecialChar ~
4620 case 4: ...
4621  
4622 \newline 
4623 }\SpecialChar ~
4624  \SpecialChar ~
4625  \SpecialChar ~
4626  \SpecialChar ~
4627  \SpecialChar ~
4628  \SpecialChar ~
4629  \SpecialChar ~
4630  \SpecialChar ~
4631  \SpecialChar ~
4632  \SpecialChar ~
4633  \SpecialChar ~
4634  \SpecialChar ~
4635  \SpecialChar ~
4636  \SpecialChar ~
4637  \SpecialChar ~
4638  \SpecialChar ~
4639  \SpecialChar ~
4640  \SpecialChar ~
4641 }
4642 \newline 
4643
4644 \newline 
4645
4646 \family default 
4647 Both the above switch statements will be implemented using a jump-table.
4648 \layout Itemize
4649
4650 The number of case labels is at least three, since it takes two conditional
4651  statements to handle the boundary conditions.
4652 \layout Itemize
4653
4654 The number of case labels is less than 84, since each label takes 3 bytes
4655  and a jump-table can be utmost 256 bytes long.
4656  
4657 \layout Standard
4658
4659 Switch statements which have gaps in the numeric sequence or those that
4660  have more that 84 case labels can be split into more than one switch statement
4661  for efficient code generation, e.g.:
4662 \newline 
4663
4664 \newline 
4665
4666 \family typewriter 
4667 switch (i) { 
4668 \newline 
4669 case 1: ...
4670  
4671 \newline 
4672 case 2: ...
4673  
4674 \newline 
4675 case 3: ...
4676  
4677 \newline 
4678 case 4: ...
4679  
4680 \newline 
4681 case 9: ...
4682  
4683 \newline 
4684 case 10: ...
4685  
4686 \newline 
4687 case 11: ...
4688  
4689 \newline 
4690 case 12: ...
4691  
4692 \newline 
4693 }
4694 \family default 
4695
4696 \newline 
4697
4698 \newline 
4699 If the above switch statement is broken down into two switch statements
4700 \newline 
4701
4702 \newline 
4703
4704 \family typewriter 
4705 switch (i) { 
4706 \newline 
4707 case 1: ...
4708  
4709 \newline 
4710 case 2: ...
4711  
4712 \newline 
4713 case 3: ...
4714  
4715 \newline 
4716 case 4: ...
4717  
4718 \newline 
4719 }
4720 \newline 
4721
4722 \newline 
4723
4724 \family default 
4725 and
4726 \family typewriter 
4727
4728 \newline 
4729
4730 \newline 
4731 switch (i) { 
4732 \newline 
4733 case 9: \SpecialChar ~
4734 ...
4735  
4736 \newline 
4737 case 10: ...
4738  
4739 \newline 
4740 case 11: ...
4741  
4742 \newline 
4743 case 12:\SpecialChar ~
4744 ...
4745  
4746 \newline 
4747 }
4748 \newline 
4749
4750 \newline 
4751
4752 \family default 
4753 then both the switch statements will be implemented using jump-tables whereas
4754  the unmodified switch statement will not be.
4755 \layout Subsubsection
4756
4757 Bit-shifting Operations.
4758 \layout Standard
4759
4760 Bit shifting is one of the most frequently used operation in embedded programmin
4761 g.
4762  SDCC tries to implement bit-shift operations in the most efficient way
4763  possible, e.g.:
4764 \newline 
4765
4766 \family typewriter 
4767
4768 \newline 
4769 unsigned char i;
4770 \newline 
4771 ...
4772  
4773 \newline 
4774 i>>= 4; 
4775 \newline 
4776 ...
4777 \newline 
4778
4779 \family default 
4780
4781 \newline 
4782 generates the following code:
4783 \newline 
4784
4785 \family typewriter 
4786
4787 \newline 
4788 mov a,_i 
4789 \newline 
4790 swap a 
4791 \newline 
4792 anl a,#0x0f 
4793 \newline 
4794 mov _i,a
4795 \family default 
4796
4797 \newline 
4798
4799 \newline 
4800 In general SDCC will never setup a loop if the shift count is known.
4801  Another example:
4802 \newline 
4803
4804 \newline 
4805
4806 \family typewriter 
4807 unsigned int i; 
4808 \newline 
4809 ...
4810  
4811 \newline 
4812 i >>= 9; 
4813 \newline 
4814 ...
4815 \family default 
4816
4817 \newline 
4818
4819 \newline 
4820 will generate:
4821 \newline 
4822
4823 \newline 
4824
4825 \family typewriter 
4826 mov a,(_i + 1) 
4827 \newline 
4828 mov (_i + 1),#0x00 
4829 \newline 
4830 clr c 
4831 \newline 
4832 rrc a 
4833 \newline 
4834 mov _i,a
4835 \family default 
4836
4837 \newline 
4838
4839 \newline 
4840 Note that SDCC stores numbers in little-endian format (i.e.
4841  lowest order first).
4842 \layout Subsubsection
4843
4844 Bit-rotation
4845 \layout Standard
4846
4847 A special case of the bit-shift operation is bit rotation, SDCC recognizes
4848  the following expression to be a left bit-rotation:
4849 \newline 
4850
4851 \newline 
4852
4853 \family typewriter 
4854 unsigned char i; 
4855 \newline 
4856 ...
4857  
4858 \newline 
4859 i = ((i << 1) | (i >> 7)); 
4860 \family default 
4861
4862 \newline 
4863 ...
4864 \newline 
4865
4866 \newline 
4867 will generate the following code:
4868 \newline 
4869
4870 \newline 
4871
4872 \family typewriter 
4873 mov a,_i 
4874 \newline 
4875 rl a 
4876 \newline 
4877 mov _i,a
4878 \family default 
4879
4880 \newline 
4881
4882 \newline 
4883 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
4884 ns of this case will also be recognized as bit-rotation, i.e.: 
4885 \newline 
4886
4887 \newline 
4888
4889 \family typewriter 
4890 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
4891 \layout Subsubsection
4892
4893 Highest Order Bit
4894 \layout Standard
4895
4896 It is frequently required to obtain the highest order bit of an integral
4897  type (long, int, short or char types).
4898  SDCC recognizes the following expression to yield the highest order bit
4899  and generates optimized code for it, e.g.:
4900 \newline 
4901
4902 \newline 
4903  
4904 \family typewriter 
4905 unsigned int gint; 
4906 \newline 
4907
4908 \newline 
4909 foo () { 
4910 \newline 
4911 unsigned char hob; 
4912 \newline 
4913 \SpecialChar ~
4914 \SpecialChar ~
4915 ...
4916  
4917 \newline 
4918 \SpecialChar ~
4919 \SpecialChar ~
4920 hob = (gint >> 15) & 1; 
4921 \newline 
4922 \SpecialChar ~
4923 \SpecialChar ~
4924 ..
4925  
4926 \newline 
4927 }
4928 \family default 
4929
4930 \newline 
4931
4932 \newline 
4933 will generate the following code:
4934 \newline 
4935
4936 \family typewriter 
4937
4938 \newline 
4939 \SpecialChar ~
4940 \SpecialChar ~
4941 \SpecialChar ~
4942 \SpecialChar ~
4943 \SpecialChar ~
4944 \SpecialChar ~
4945 \SpecialChar ~
4946 \SpecialChar ~
4947 \SpecialChar ~
4948 \SpecialChar ~
4949 \SpecialChar ~
4950 \SpecialChar ~
4951 \SpecialChar ~
4952 \SpecialChar ~
4953 \SpecialChar ~
4954 \SpecialChar ~
4955 \SpecialChar ~
4956 \SpecialChar ~
4957 \SpecialChar ~
4958 \SpecialChar ~
4959 \SpecialChar ~
4960 \SpecialChar ~
4961 \SpecialChar ~
4962 \SpecialChar ~
4963 \SpecialChar ~
4964 \SpecialChar ~
4965 \SpecialChar ~
4966 \SpecialChar ~
4967  61 ;\SpecialChar ~
4968  hob.c 7 
4969 \newline 
4970 \SpecialChar ~
4971 \SpecialChar ~
4972  000A E5*01\SpecialChar ~
4973 \SpecialChar ~
4974 \SpecialChar ~
4975 \SpecialChar ~
4976 \SpecialChar ~
4977 \SpecialChar ~
4978 \SpecialChar ~
4979 \SpecialChar ~
4980 \SpecialChar ~
4981 \SpecialChar ~
4982 \SpecialChar ~
4983 \SpecialChar ~
4984 \SpecialChar ~
4985 \SpecialChar ~
4986 \SpecialChar ~
4987  62\SpecialChar ~
4988 \SpecialChar ~
4989 \SpecialChar ~
4990 \SpecialChar ~
4991 \SpecialChar ~
4992 \SpecialChar ~
4993 \SpecialChar ~
4994 \SpecialChar ~
4995  mov\SpecialChar ~
4996  a,(_gint + 1) 
4997 \newline 
4998 \SpecialChar ~
4999 \SpecialChar ~
5000  000C 33\SpecialChar ~
5001 \SpecialChar ~
5002 \SpecialChar ~
5003 \SpecialChar ~
5004 \SpecialChar ~
5005 \SpecialChar ~
5006 \SpecialChar ~
5007 \SpecialChar ~
5008 \SpecialChar ~
5009 \SpecialChar ~
5010 \SpecialChar ~
5011 \SpecialChar ~
5012 \SpecialChar ~
5013 \SpecialChar ~
5014 \SpecialChar ~
5015 \SpecialChar ~
5016 \SpecialChar ~
5017 \SpecialChar ~
5018  63\SpecialChar ~
5019 \SpecialChar ~
5020 \SpecialChar ~
5021 \SpecialChar ~
5022 \SpecialChar ~
5023 \SpecialChar ~
5024 \SpecialChar ~
5025 \SpecialChar ~
5026  rlc\SpecialChar ~
5027  a 
5028 \newline 
5029 \SpecialChar ~
5030 \SpecialChar ~
5031  000D E4\SpecialChar ~
5032 \SpecialChar ~
5033 \SpecialChar ~
5034 \SpecialChar ~
5035 \SpecialChar ~
5036 \SpecialChar ~
5037 \SpecialChar ~
5038 \SpecialChar ~
5039 \SpecialChar ~
5040 \SpecialChar ~
5041 \SpecialChar ~
5042 \SpecialChar ~
5043 \SpecialChar ~
5044 \SpecialChar ~
5045 \SpecialChar ~
5046 \SpecialChar ~
5047 \SpecialChar ~
5048 \SpecialChar ~
5049  64\SpecialChar ~
5050 \SpecialChar ~
5051 \SpecialChar ~
5052 \SpecialChar ~
5053 \SpecialChar ~
5054 \SpecialChar ~
5055 \SpecialChar ~
5056 \SpecialChar ~
5057  clr\SpecialChar ~
5058  a 
5059 \newline 
5060 \SpecialChar ~
5061 \SpecialChar ~
5062  000E 13\SpecialChar ~
5063 \SpecialChar ~
5064 \SpecialChar ~
5065 \SpecialChar ~
5066 \SpecialChar ~
5067 \SpecialChar ~
5068 \SpecialChar ~
5069 \SpecialChar ~
5070 \SpecialChar ~
5071 \SpecialChar ~
5072 \SpecialChar ~
5073 \SpecialChar ~
5074 \SpecialChar ~
5075 \SpecialChar ~
5076 \SpecialChar ~
5077 \SpecialChar ~
5078 \SpecialChar ~
5079 \SpecialChar ~
5080  65\SpecialChar ~
5081 \SpecialChar ~
5082 \SpecialChar ~
5083 \SpecialChar ~
5084 \SpecialChar ~
5085 \SpecialChar ~
5086 \SpecialChar ~
5087 \SpecialChar ~
5088  rrc\SpecialChar ~
5089  a 
5090 \newline 
5091 \SpecialChar ~
5092 \SpecialChar ~
5093  000F F5*02\SpecialChar ~
5094 \SpecialChar ~
5095 \SpecialChar ~
5096 \SpecialChar ~
5097 \SpecialChar ~
5098 \SpecialChar ~
5099 \SpecialChar ~
5100 \SpecialChar ~
5101 \SpecialChar ~
5102 \SpecialChar ~
5103 \SpecialChar ~
5104 \SpecialChar ~
5105 \SpecialChar ~
5106 \SpecialChar ~
5107 \SpecialChar ~
5108  66\SpecialChar ~
5109 \SpecialChar ~
5110 \SpecialChar ~
5111 \SpecialChar ~
5112 \SpecialChar ~
5113 \SpecialChar ~
5114 \SpecialChar ~
5115 \SpecialChar ~
5116  mov\SpecialChar ~
5117  _foo_hob_1_1,a
5118 \newline 
5119
5120 \newline 
5121
5122 \family default 
5123 Variations of this case however will 
5124 \emph on 
5125 not
5126 \emph default 
5127  be recognized.
5128  It is a standard C expression, so I heartily recommend this be the only
5129  way to get the highest order bit, (it is portable).
5130  Of course it will be recognized even if it is embedded in other expressions,
5131  e.g.:
5132 \newline 
5133
5134 \newline 
5135
5136 \family typewriter 
5137 xyz = gint + ((gint >> 15) & 1);
5138 \family default 
5139
5140 \newline 
5141
5142 \newline 
5143 will still be recognized.
5144 \layout Subsubsection
5145
5146 Peep-hole Optimizer
5147 \layout Standard
5148
5149 The compiler uses a rule based, pattern matching and re-writing mechanism
5150  for peep-hole optimization.
5151  It is inspired by 
5152 \emph on 
5153 copt
5154 \emph default 
5155  a peep-hole optimizer by Christopher W.
5156  Fraser (cwfraser@microsoft.com).
5157  A default set of rules are compiled into the compiler, additional rules
5158  may be added with the 
5159 \emph on 
5160 --peep-file <filename>
5161 \emph default 
5162  option.
5163  The rule language is best illustrated with examples.
5164 \newline 
5165
5166 \newline 
5167
5168 \family typewriter 
5169 replace { 
5170 \newline 
5171 \SpecialChar ~
5172 \SpecialChar ~
5173 mov %1,a 
5174 \newline 
5175 \SpecialChar ~
5176 \SpecialChar ~
5177 mov a,%1
5178 \newline 
5179 } by {
5180 \newline 
5181 \SpecialChar ~
5182 \SpecialChar ~
5183 mov %1,a
5184 \newline 
5185 }
5186 \family default 
5187
5188 \newline 
5189
5190 \newline 
5191 The above rule will change the following assembly sequence:
5192 \newline 
5193
5194 \newline 
5195
5196 \family typewriter 
5197 \SpecialChar ~
5198 \SpecialChar ~
5199 mov r1,a 
5200 \newline 
5201 \SpecialChar ~
5202 \SpecialChar ~
5203 mov a,r1
5204 \family default 
5205
5206 \newline 
5207
5208 \newline 
5209 to
5210 \newline 
5211
5212 \newline 
5213
5214 \family typewriter 
5215 mov r1,a
5216 \family default 
5217
5218 \newline 
5219
5220 \newline 
5221 Note: All occurrences of a 
5222 \emph on 
5223 %n
5224 \emph default 
5225  (pattern variable) must denote the same string.
5226  With the above rule, the assembly sequence:
5227 \newline 
5228
5229 \newline 
5230
5231 \family typewriter 
5232 \SpecialChar ~
5233 \SpecialChar ~
5234 mov r1,a 
5235 \newline 
5236 \SpecialChar ~
5237 \SpecialChar ~
5238 mov a,r2
5239 \family default 
5240
5241 \newline 
5242
5243 \newline 
5244 will remain unmodified.
5245 \newline 
5246
5247 \newline 
5248 Other special case optimizations may be added by the user (via 
5249 \emph on 
5250 --peep-file option
5251 \emph default 
5252 ).
5253  E.g.
5254  some variants of the 8051 MCU allow only 
5255 \family typewriter 
5256 ajmp
5257 \family default 
5258  and 
5259 \family typewriter 
5260 acall
5261 \family default 
5262 .
5263  The following two rules will change all 
5264 \family typewriter 
5265 ljmp
5266 \family default 
5267  and 
5268 \family typewriter 
5269 lcall
5270 \family default 
5271  to 
5272 \family typewriter 
5273 ajmp
5274 \family default 
5275  and 
5276 \family typewriter 
5277 acall
5278 \family default 
5279
5280 \newline 
5281
5282 \newline 
5283
5284 \family typewriter 
5285 replace { lcall %1 } by { acall %1 } 
5286 \newline 
5287 replace { ljmp %1 } by { ajmp %1 }
5288 \family default 
5289
5290 \newline 
5291
5292 \newline 
5293 The 
5294 \emph on 
5295 inline-assembler code
5296 \emph default 
5297  is also passed through the peep hole optimizer, thus the peephole optimizer
5298  can also be used as an assembly level macro expander.
5299  The rules themselves are MCU dependent whereas the rule language infra-structur
5300 e is MCU independent.
5301  Peephole optimization rules for other MCU can be easily programmed using
5302  the rule language.
5303 \newline 
5304
5305 \newline 
5306 The syntax for a rule is as follows:
5307 \newline 
5308
5309 \newline 
5310
5311 \family typewriter 
5312 rule := replace [ restart ] '{' <assembly sequence> '
5313 \backslash 
5314 n' 
5315 \newline 
5316 \SpecialChar ~
5317  \SpecialChar ~
5318  \SpecialChar ~
5319  \SpecialChar ~
5320  \SpecialChar ~
5321  \SpecialChar ~
5322  \SpecialChar ~
5323  \SpecialChar ~
5324  \SpecialChar ~
5325  \SpecialChar ~
5326  \SpecialChar ~
5327  \SpecialChar ~
5328  \SpecialChar ~
5329  \SpecialChar ~
5330  '}' by '{' '
5331 \backslash 
5332 n' 
5333 \newline 
5334 \SpecialChar ~
5335  \SpecialChar ~
5336  \SpecialChar ~
5337  \SpecialChar ~
5338  \SpecialChar ~
5339  \SpecialChar ~
5340  \SpecialChar ~
5341  \SpecialChar ~
5342  \SpecialChar ~
5343  \SpecialChar ~
5344  \SpecialChar ~
5345  \SpecialChar ~
5346  \SpecialChar ~
5347  \SpecialChar ~
5348  \SpecialChar ~
5349  \SpecialChar ~
5350  <assembly sequence> '
5351 \backslash 
5352 n' 
5353 \newline 
5354 \SpecialChar ~
5355  \SpecialChar ~
5356  \SpecialChar ~
5357  \SpecialChar ~
5358  \SpecialChar ~
5359  \SpecialChar ~
5360  \SpecialChar ~
5361  \SpecialChar ~
5362  \SpecialChar ~
5363  \SpecialChar ~
5364  \SpecialChar ~
5365  \SpecialChar ~
5366  \SpecialChar ~
5367  \SpecialChar ~
5368  '}' [if <functionName> ] '
5369 \backslash 
5370 n' 
5371 \newline 
5372
5373 \family default 
5374
5375 \newline 
5376 <assembly sequence> := assembly instruction (each instruction including
5377  labels must be on a separate line).
5378 \newline 
5379
5380 \newline 
5381 The optimizer will apply to the rules one by one from the top in the sequence
5382  of their appearance, it will terminate when all rules are exhausted.
5383  If the 'restart' option is specified, then the optimizer will start matching
5384  the rules again from the top, this option for a rule is expensive (performance)
5385 , it is intended to be used in situations where a transformation will trigger
5386  the same rule again.
5387  An example of this (not a good one, it has side effects) is the following
5388  rule:
5389 \newline 
5390
5391 \newline 
5392
5393 \family typewriter 
5394 replace restart { 
5395 \newline 
5396 \SpecialChar ~
5397 \SpecialChar ~
5398 pop %1 
5399 \newline 
5400 \SpecialChar ~
5401 \SpecialChar ~
5402 push %1 } by { 
5403 \newline 
5404 \SpecialChar ~
5405 \SpecialChar ~
5406 ; nop 
5407 \newline 
5408 }
5409 \family default 
5410
5411 \newline 
5412
5413 \newline 
5414 Note that the replace pattern cannot be a blank, but can be a comment line.
5415  Without the 'restart' option only the inner most 'pop' 'push' pair would
5416  be eliminated, i.e.:
5417 \newline 
5418
5419 \newline 
5420
5421 \family typewriter 
5422 \SpecialChar ~
5423 \SpecialChar ~
5424 pop ar1 
5425 \newline 
5426 \SpecialChar ~
5427 \SpecialChar ~
5428 pop ar2 
5429 \newline 
5430 \SpecialChar ~
5431 \SpecialChar ~
5432 push ar2 
5433 \newline 
5434 \SpecialChar ~
5435 \SpecialChar ~
5436 push ar1
5437 \family default 
5438
5439 \newline 
5440
5441 \newline 
5442 would result in:
5443 \newline 
5444
5445 \newline 
5446
5447 \family typewriter 
5448 \SpecialChar ~
5449 \SpecialChar ~
5450 pop ar1 
5451 \newline 
5452 \SpecialChar ~
5453 \SpecialChar ~
5454 ; nop 
5455 \newline 
5456 \SpecialChar ~
5457 \SpecialChar ~
5458 push ar1
5459 \family default 
5460
5461 \newline 
5462
5463 \newline 
5464
5465 \emph on 
5466 with
5467 \emph default 
5468  the restart option the rule will be applied again to the resulting code
5469  and then all the pop-push pairs will be eliminated to yield:
5470 \newline 
5471
5472 \newline 
5473
5474 \family typewriter 
5475 \SpecialChar ~
5476 \SpecialChar ~
5477 ; nop 
5478 \newline 
5479 \SpecialChar ~
5480 \SpecialChar ~
5481 ; nop
5482 \family default 
5483
5484 \newline 
5485
5486 \newline 
5487 A conditional function can be attached to a rule.
5488  Attaching rules are somewhat more involved, let me illustrate this with
5489  an example.
5490 \newline 
5491
5492 \newline 
5493
5494 \family typewriter 
5495 replace { 
5496 \newline 
5497 \SpecialChar ~
5498  \SpecialChar ~
5499  \SpecialChar ~
5500 ljmp %5 
5501 \newline 
5502 %2:
5503 \newline 
5504 } by { 
5505 \newline 
5506 \SpecialChar ~
5507  \SpecialChar ~
5508  \SpecialChar ~
5509 sjmp %5 
5510 \newline 
5511 %2:
5512 \newline 
5513 } if labelInRange
5514 \family default 
5515
5516 \newline 
5517
5518 \newline 
5519 The optimizer does a look-up of a function name table defined in function
5520  
5521 \emph on 
5522 callFuncByName
5523 \emph default 
5524  in the source file SDCCpeeph.c, with the name 
5525 \emph on 
5526 labelInRange
5527 \emph default 
5528 .
5529  If it finds a corresponding entry the function is called.
5530  Note there can be no parameters specified for these functions, in this
5531  case the use of 
5532 \emph on 
5533 %5
5534 \emph default 
5535  is crucial, since the function 
5536 \emph on 
5537 labelInRange
5538 \emph default 
5539  expects to find the label in that particular variable (the hash table containin
5540 g the variable bindings is passed as a parameter).
5541  If you want to code more such functions, take a close look at the function
5542  labelInRange and the calling mechanism in source file SDCCpeeph.c.
5543  I know this whole thing is a little kludgey, but maybe some day we will
5544  have some better means.
5545  If you are looking at this file, you will also see the default rules that
5546  are compiled into the compiler, you can add your own rules in the default
5547  set there if you get tired of specifying the --peep-file option.
5548 \layout Subsection
5549
5550 Pragmas
5551 \layout Standard
5552
5553 SDCC supports the following #pragma directives.
5554  This directives are applicable only at a function level.
5555 \layout Itemize
5556
5557 SAVE - this will save all the current options.
5558 \layout Itemize
5559
5560 RESTORE - will restore the saved options from the last save.
5561  Note that SAVES & RESTOREs cannot be nested.
5562  SDCC uses the same buffer to save the options each time a SAVE is called.
5563 \layout Itemize
5564
5565 NOGCSE - will stop global subexpression elimination.
5566 \layout Itemize
5567
5568 NOINDUCTION - will stop loop induction optimizations.
5569 \layout Itemize
5570
5571 NOJTBOUND - will not generate code for boundary value checking, when switch
5572  statements are turned into jump-tables.
5573 \layout Itemize
5574
5575 NOOVERLAY - the compiler will not overlay the parameters and local variables
5576  of a function.
5577 \layout Itemize
5578
5579 NOLOOPREVERSE - Will not do loop reversal optimization
5580 \layout Itemize
5581
5582 EXCLUDE NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation
5583  of pair of push/pop instruction in ISR function (using interrupt keyword).
5584  The directive should be placed immediately before the ISR function definition
5585  and it affects ALL ISR functions following it.
5586  To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
5587 EXCLUDE\SpecialChar ~
5588 none.
5589 \layout Itemize
5590
5591 CALLEE-SAVES function1[,function2[,function3...]] - The compiler by default
5592  uses a caller saves convention for register saving across function calls,
5593  however this can cause unneccessary register pushing & popping when calling
5594  small functions from larger functions.
5595  This option can be used to switch the register saving convention for the
5596  function names specified.
5597  The compiler will not save registers when calling these functions, extra
5598  code will be generated at the entry & exit for these functions to save
5599  & restore the registers used by these functions, this can SUBSTANTIALLY
5600  reduce code & improve run time performance of the generated code.
5601  In future the compiler (with interprocedural analysis) will be able to
5602  determine the appropriate scheme to use for each function call.
5603  If --callee-saves command line option is used, the function names specified
5604  in #pragma\SpecialChar ~
5605 CALLEE-SAVES is appended to the list of functions specified inthe
5606  command line.
5607 \layout Standard
5608
5609 The pragma's are intended to be used to turn-off certain optimizations which
5610  might cause the compiler to generate extra stack / data space to store
5611  compiler generated temporary variables.
5612  This usually happens in large functions.
5613  Pragma directives should be used as shown in the following example, they
5614  are used to control options & optimizations for a given function; pragmas
5615  should be placed before and/or after a function, placing pragma's inside
5616  a function body could have unpredictable results.
5617 \newline 
5618
5619 \newline 
5620
5621 \family typewriter 
5622 #pragma SAVE /* save the current settings */ 
5623 \newline 
5624 #pragma NOGCSE /* turnoff global subexpression elimination */ 
5625 \newline 
5626 #pragma NOINDUCTION /* turn off induction optimizations */ 
5627 \newline 
5628 int foo () 
5629 \newline 
5630
5631 \newline 
5632 \SpecialChar ~
5633  \SpecialChar ~
5634  ...
5635  
5636 \newline 
5637 \SpecialChar ~
5638  \SpecialChar ~
5639  /* large code */ 
5640 \newline 
5641 \SpecialChar ~
5642  \SpecialChar ~
5643  ...
5644  
5645 \newline 
5646
5647 \newline 
5648 #pragma RESTORE /* turn the optimizations back on */
5649 \family default 
5650
5651 \newline 
5652
5653 \newline 
5654 The compiler will generate a warning message when extra space is allocated.
5655  It is strongly recommended that the SAVE and RESTORE pragma's be used when
5656  changing options for a function.
5657 \layout Subsection
5658
5659
5660 \emph on 
5661 <pending: this is messy and incomplete>
5662 \emph default 
5663  Library Routines
5664 \layout Enumerate
5665
5666 Compiler support routines (_gptrget, _mulint etc)
5667 \layout Enumerate
5668
5669 Stdclib functions (puts, printf, strcat etc)
5670 \layout Enumerate
5671
5672 Math functions (sin, pow, sqrt etc)
5673 \layout Subsection
5674
5675 Interfacing with Assembly Routines
5676 \layout Subsubsection
5677
5678 Global Registers used for Parameter Passing
5679 \layout Standard
5680
5681 The compiler always uses the global registers 
5682 \emph on 
5683 DPL,DPH,B 
5684 \emph default 
5685 and
5686 \emph on 
5687  ACC
5688 \emph default 
5689  to pass the first parameter to a routine.
5690  The second parameter onwards is either allocated on the stack (for reentrant
5691  routines or if --stack-auto is used) or in the internal / external ram
5692  (depending on the memory model).
5693  
5694 \layout Subsubsection
5695
5696 Assembler Routine(non-reentrant)
5697 \layout Standard
5698
5699 In the following example the function cfunc calls an assembler routine asm_func,
5700  which takes two parameters.
5701 \newline 
5702
5703 \newline 
5704
5705 \family typewriter 
5706 extern int asm_func(unsigned char, unsigned char);
5707 \newline 
5708
5709 \newline 
5710 int c_func (unsigned char i, unsigned char j)
5711 \newline 
5712 {
5713 \newline 
5714 \SpecialChar ~
5715 \SpecialChar ~
5716 \SpecialChar ~
5717 \SpecialChar ~
5718 return asm_func(i,j);
5719 \newline 
5720 }
5721 \newline 
5722
5723 \newline 
5724 int main()
5725 \newline 
5726 {
5727 \newline 
5728 \SpecialChar ~
5729 \SpecialChar ~
5730 \SpecialChar ~
5731 \SpecialChar ~
5732 return c_func(10,9);
5733 \newline 
5734 }
5735 \newline 
5736
5737 \newline 
5738
5739 \family default 
5740 The corresponding assembler function is:
5741 \newline 
5742
5743 \newline 
5744
5745 \family typewriter 
5746 .globl _asm_func_PARM_2 
5747 \newline 
5748 \SpecialChar ~
5749 \SpecialChar ~
5750 \SpecialChar ~
5751 \SpecialChar ~
5752 \SpecialChar ~
5753 \SpecialChar ~
5754 \SpecialChar ~
5755 \SpecialChar ~
5756 .globl _asm_func 
5757 \newline 
5758 \SpecialChar ~
5759 \SpecialChar ~
5760 \SpecialChar ~
5761 \SpecialChar ~
5762 \SpecialChar ~
5763 \SpecialChar ~
5764 \SpecialChar ~
5765 \SpecialChar ~
5766 .area OSEG 
5767 \newline 
5768 _asm_func_PARM_2:
5769 \newline 
5770 \SpecialChar ~
5771 \SpecialChar ~
5772 \SpecialChar ~
5773 \SpecialChar ~
5774 \SpecialChar ~
5775 \SpecialChar ~
5776 \SpecialChar ~
5777 \SpecialChar ~
5778 .ds      1 
5779 \newline 
5780 \SpecialChar ~
5781 \SpecialChar ~
5782 \SpecialChar ~
5783 \SpecialChar ~
5784 \SpecialChar ~
5785 \SpecialChar ~
5786 \SpecialChar ~
5787 \SpecialChar ~
5788 .area CSEG 
5789 \newline 
5790 _asm_func: 
5791 \newline 
5792 \SpecialChar ~
5793 \SpecialChar ~
5794 \SpecialChar ~
5795 \SpecialChar ~
5796 \SpecialChar ~
5797 \SpecialChar ~
5798 \SpecialChar ~
5799 \SpecialChar ~
5800 mov     a,dpl 
5801 \newline 
5802 \SpecialChar ~
5803 \SpecialChar ~
5804 \SpecialChar ~
5805 \SpecialChar ~
5806 \SpecialChar ~
5807 \SpecialChar ~
5808 \SpecialChar ~
5809 \SpecialChar ~
5810 add     a,_asm_func_PARM_2 
5811 \newline 
5812 \SpecialChar ~
5813 \SpecialChar ~
5814 \SpecialChar ~
5815 \SpecialChar ~
5816 \SpecialChar ~
5817 \SpecialChar ~
5818 \SpecialChar ~
5819 \SpecialChar ~
5820 mov     dpl,a 
5821 \newline 
5822 \SpecialChar ~
5823 \SpecialChar ~
5824 \SpecialChar ~
5825 \SpecialChar ~
5826 \SpecialChar ~
5827 \SpecialChar ~
5828 \SpecialChar ~
5829 \SpecialChar ~
5830 mov     dpl,#0x00 
5831 \newline 
5832 \SpecialChar ~
5833 \SpecialChar ~
5834 \SpecialChar ~
5835 \SpecialChar ~
5836 \SpecialChar ~
5837 \SpecialChar ~
5838 \SpecialChar ~
5839 \SpecialChar ~
5840 ret
5841 \newline 
5842
5843 \newline 
5844
5845 \family default 
5846 Note here that the return values are placed in 'dpl' - One byte return value,
5847  'dpl' LSB & 'dph' MSB for two byte values.
5848  'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
5849 b' & 'acc' for four byte values.
5850 \layout Standard
5851
5852 The parameter naming convention is _<function_name>_PARM_<n>, where n is
5853  the parameter number starting from 1, and counting from the left.
5854  The first parameter is passed in 
5855 \begin_inset Quotes eld
5856 \end_inset 
5857
5858 dpl
5859 \begin_inset Quotes erd
5860 \end_inset 
5861
5862  for One bye parameter, 
5863 \begin_inset Quotes eld
5864 \end_inset 
5865
5866 dptr
5867 \begin_inset Quotes erd
5868 \end_inset 
5869
5870  if two bytes, 
5871 \begin_inset Quotes eld
5872 \end_inset 
5873
5874 b,dptr
5875 \begin_inset Quotes erd
5876 \end_inset 
5877
5878  for three bytes and 
5879 \begin_inset Quotes eld
5880 \end_inset 
5881
5882 acc,b,dptr
5883 \begin_inset Quotes erd
5884 \end_inset 
5885
5886  for four bytes, the varible name for the second parameter will be _<function_na
5887 me>_PARM_2.
5888 \newline 
5889
5890 \newline 
5891 Assemble the assembler routine with the following command:
5892 \newline 
5893
5894 \newline 
5895
5896 \family sans 
5897 \series bold 
5898 asx8051 -losg asmfunc.asm
5899 \newline 
5900
5901 \newline 
5902
5903 \family default 
5904 \series default 
5905 Then compile and link the assembler routine to the C source file with the
5906  following command:
5907 \newline 
5908
5909 \newline 
5910
5911 \family sans 
5912 \series bold 
5913 sdcc cfunc.c asmfunc.rel
5914 \layout Subsubsection
5915
5916 Assembler Routine(reentrant)
5917 \layout Standard
5918
5919 In this case the second parameter onwards will be passed on the stack, the
5920  parameters are pushed from right to left i.e.
5921  after the call the left most parameter will be on the top of the stack.
5922  Here is an example:
5923 \newline 
5924
5925 \newline 
5926
5927 \family typewriter 
5928 extern int asm_func(unsigned char, unsigned char);
5929 \newline 
5930
5931 \newline 
5932 int c_func (unsigned char i, unsigned char j) reentrant 
5933 \newline 
5934
5935 \newline 
5936 \SpecialChar ~
5937 \SpecialChar ~
5938 \SpecialChar ~
5939 \SpecialChar ~
5940 return asm_func(i,j); 
5941 \newline 
5942
5943 \newline 
5944
5945 \newline 
5946 int main() 
5947 \newline 
5948
5949 \newline 
5950 \SpecialChar ~
5951 \SpecialChar ~
5952 \SpecialChar ~
5953 \SpecialChar ~
5954 return c_func(10,9); 
5955 \newline 
5956 }
5957 \newline 
5958
5959 \family default 
5960
5961 \newline 
5962 The corresponding assembler routine is:
5963 \newline 
5964
5965 \newline 
5966
5967 \family typewriter 
5968 .globl _asm_func 
5969 \newline 
5970 _asm_func: 
5971 \newline 
5972 \SpecialChar ~
5973 \SpecialChar ~
5974 \SpecialChar ~
5975 \SpecialChar ~
5976 push  _bp 
5977 \newline 
5978 \SpecialChar ~
5979 \SpecialChar ~
5980 \SpecialChar ~
5981 \SpecialChar ~
5982 mov  _bp,sp 
5983 \newline 
5984 \SpecialChar ~
5985 \SpecialChar ~
5986 \SpecialChar ~
5987 \SpecialChar ~
5988 mov  r2,dpl
5989 \newline 
5990 \SpecialChar ~
5991 \SpecialChar ~
5992 \SpecialChar ~
5993 \SpecialChar ~
5994 mov  a,_bp 
5995 \newline 
5996 \SpecialChar ~
5997 \SpecialChar ~
5998 \SpecialChar ~
5999 \SpecialChar ~
6000 clr  c 
6001 \newline 
6002 \SpecialChar ~
6003 \SpecialChar ~
6004 \SpecialChar ~
6005 \SpecialChar ~
6006 add  a,#0xfd 
6007 \newline 
6008 \SpecialChar ~
6009 \SpecialChar ~
6010 \SpecialChar ~
6011 \SpecialChar ~
6012 mov  r0,a 
6013 \newline 
6014 \SpecialChar ~
6015 \SpecialChar ~
6016 \SpecialChar ~
6017 \SpecialChar ~
6018 add  a,#0xfc
6019 \newline 
6020 \SpecialChar ~
6021 \SpecialChar ~
6022 \SpecialChar ~
6023 \SpecialChar ~
6024 mov  r1,a 
6025 \newline 
6026 \SpecialChar ~
6027 \SpecialChar ~
6028 \SpecialChar ~
6029 \SpecialChar ~
6030 mov  a,@r0 
6031 \newline 
6032 \SpecialChar ~
6033 \SpecialChar ~
6034 \SpecialChar ~
6035 \SpecialChar ~
6036 add  a,r2
6037 \newline 
6038 \SpecialChar ~
6039 \SpecialChar ~
6040 \SpecialChar ~
6041 \SpecialChar ~
6042 mov  dpl,a 
6043 \newline 
6044 \SpecialChar ~
6045 \SpecialChar ~
6046 \SpecialChar ~
6047 \SpecialChar ~
6048 mov  dph,#0x00 
6049 \newline 
6050 \SpecialChar ~
6051 \SpecialChar ~
6052 \SpecialChar ~
6053 \SpecialChar ~
6054 mov  sp,_bp 
6055 \newline 
6056 \SpecialChar ~
6057 \SpecialChar ~
6058 \SpecialChar ~
6059 \SpecialChar ~
6060 pop  _bp 
6061 \newline 
6062 \SpecialChar ~
6063 \SpecialChar ~
6064 \SpecialChar ~
6065 \SpecialChar ~
6066 ret
6067 \newline 
6068
6069 \newline 
6070
6071 \family default 
6072 The compiling and linking procedure remains the same, however note the extra
6073  entry & exit linkage required for the assembler code, _bp is the stack
6074  frame pointer and is used to compute the offset into the stack for parameters
6075  and local variables.
6076 \layout Subsection
6077
6078 External Stack
6079 \layout Standard
6080
6081 The external stack is located at the start of the external ram segment,
6082  and is 256 bytes in size.
6083  When --xstack option is used to compile the program, the parameters and
6084  local variables of all reentrant functions are allocated in this area.
6085  This option is provided for programs with large stack space requirements.
6086  When used with the --stack-auto option, all parameters and local variables
6087  are allocated on the external stack (note support libraries will need to
6088  be recompiled with the same options).
6089 \layout Standard
6090
6091 The compiler outputs the higher order address byte of the external ram segment
6092  into PORT P2, therefore when using the External Stack option, this port
6093  MAY NOT be used by the application program.
6094 \layout Subsection
6095
6096 ANSI-Compliance
6097 \layout Standard
6098
6099 Deviations from the compliancy.
6100 \layout Itemize
6101
6102 functions are not always reentrant.
6103 \layout Itemize
6104
6105 structures cannot be assigned values directly, cannot be passed as function
6106  parameters or assigned to each other and cannot be a return value from
6107  a function, e.g.:
6108 \family typewriter 
6109
6110 \newline 
6111
6112 \newline 
6113 struct s { ...
6114  }; 
6115 \newline 
6116 struct s s1, s2; 
6117 \newline 
6118 foo() 
6119 \newline 
6120
6121 \newline 
6122 \SpecialChar ~
6123 \SpecialChar ~
6124 \SpecialChar ~
6125 \SpecialChar ~
6126 ...
6127  
6128 \newline 
6129 \SpecialChar ~
6130 \SpecialChar ~
6131 \SpecialChar ~
6132 \SpecialChar ~
6133 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */ 
6134 \newline 
6135 \SpecialChar ~
6136 \SpecialChar ~
6137 \SpecialChar ~
6138 \SpecialChar ~
6139 ...
6140  
6141 \newline 
6142 }
6143 \newline 
6144 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
6145  ANSI */ 
6146 \newline 
6147
6148 \newline 
6149 \SpecialChar ~
6150 \SpecialChar ~
6151 \SpecialChar ~
6152 \SpecialChar ~
6153 struct s rets; 
6154 \newline 
6155 \SpecialChar ~
6156 \SpecialChar ~
6157 \SpecialChar ~
6158 \SpecialChar ~
6159 ...
6160  
6161 \newline 
6162 \SpecialChar ~
6163 \SpecialChar ~
6164 \SpecialChar ~
6165 \SpecialChar ~
6166 return rets;/* is invalid in SDCC although allowed in ANSI */ 
6167 \newline 
6168 }
6169 \layout Itemize
6170
6171 'long long' (64 bit integers) not supported.
6172 \layout Itemize
6173
6174 'double' precision floating point not supported.
6175 \layout Itemize
6176
6177 No support for setjmp and longjmp (for now).
6178 \layout Itemize
6179
6180 Old K&R style function declarations are NOT allowed.
6181 \newline 
6182
6183 \family typewriter 
6184
6185 \newline 
6186 foo(i,j) /* this old style of function declarations */ 
6187 \newline 
6188 int i,j; /* are valid in ANSI but not valid in SDCC */ 
6189 \newline 
6190
6191 \newline 
6192 \SpecialChar ~
6193 \SpecialChar ~
6194 \SpecialChar ~
6195 \SpecialChar ~
6196 ...
6197  
6198 \newline 
6199 }
6200 \layout Itemize
6201
6202 functions declared as pointers must be dereferenced during the call.
6203 \newline 
6204
6205 \family typewriter 
6206
6207 \newline 
6208 int (*foo)();
6209 \newline 
6210 ...
6211  
6212 \newline 
6213 /* has to be called like this */ 
6214 \newline 
6215 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
6216 \layout Subsection
6217
6218 Cyclomatic Complexity
6219 \layout Standard
6220
6221 Cyclomatic complexity of a function is defined as the number of independent
6222  paths the program can take during execution of the function.
6223  This is an important number since it defines the number test cases you
6224  have to generate to validate the function.
6225  The accepted industry standard for complexity number is 10, if the cyclomatic
6226  complexity reported by SDCC exceeds 10 you should think about simplification
6227  of the function logic.
6228  Note that the complexity level is not related to the number of lines of
6229  code in a function.
6230  Large functions can have low complexity, and small functions can have large
6231  complexity levels.
6232  
6233 \newline 
6234
6235 \newline 
6236 SDCC uses the following formula to compute the complexity:
6237 \newline 
6238
6239 \layout Standard
6240
6241 complexity = (number of edges in control flow graph) - (number of nodes
6242  in control flow graph) + 2;
6243 \newline 
6244
6245 \newline 
6246 Having said that the industry standard is 10, you should be aware that in
6247  some cases it be may unavoidable to have a complexity level of less than
6248  10.
6249  For example if you have switch statement with more than 10 case labels,
6250  each case label adds one to the complexity level.
6251  The complexity level is by no means an absolute measure of the algorithmic
6252  complexity of the function, it does however provide a good starting point
6253  for which functions you might look at for further optimization.
6254 \layout Section
6255
6256 TIPS
6257 \layout Standard
6258
6259 Here are a few guidelines that will help the compiler generate more efficient
6260  code, some of the tips are specific to this compiler others are generally
6261  good programming practice.
6262 \layout Itemize
6263
6264 Use the smallest data type to represent your data-value.
6265  If it is known in advance that the value is going to be less than 256 then
6266  use a 'char' instead of a 'short' or 'int'.
6267 \layout Itemize
6268
6269 Use unsigned when it is known in advance that the value is not going to
6270  be negative.
6271  This helps especially if you are doing division or multiplication.
6272 \layout Itemize
6273
6274 NEVER jump into a LOOP.
6275 \layout Itemize
6276
6277 Declare the variables to be local whenever possible, especially loop control
6278  variables (induction).
6279 \layout Itemize
6280
6281 Since the compiler does not do implicit integral promotion, the programmer
6282  should do an explicit cast when integral promotion is required.
6283 \layout Itemize
6284
6285 Reducing the size of division, multiplication & modulus operations can reduce
6286  code size substantially.
6287  Take the following code for example.
6288 \family typewriter 
6289
6290 \newline 
6291
6292 \newline 
6293 foobar(unsigned int p1, unsigned char ch)
6294 \newline 
6295 {
6296 \newline 
6297     unsigned char ch1 = p1 % ch ;
6298 \newline 
6299     ....
6300     
6301 \newline 
6302 }
6303 \newline 
6304
6305 \family default 
6306
6307 \newline 
6308 For the modulus operation the variable ch will be promoted to unsigned int
6309  first then the modulus operation will be performed (this will lead to a
6310  call to support routine _moduint()), and the result will be casted to a
6311  char.
6312  If the code is changed to 
6313 \newline 
6314
6315 \family typewriter 
6316
6317 \newline 
6318 foobar(unsigned int p1, unsigned char ch)
6319 \newline 
6320 {
6321 \newline 
6322     unsigned char ch1 = (unsigned char)p1 % ch ;
6323 \newline 
6324     ....
6325     
6326 \newline 
6327 }
6328 \newline 
6329
6330 \family default 
6331
6332 \newline 
6333 It would substantially reduce the code generated (future versions of the
6334  compiler will be smart enough to detect such optimization oppurtunities).
6335 \layout Subsection
6336
6337 Notes on MCS51 memory layout
6338 \layout Standard
6339
6340 The 8051 family of micro controller have a minimum of 128 bytes of internal
6341  memory which is structured as follows
6342 \newline 
6343
6344 \newline 
6345 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
6346  
6347 \newline 
6348 - Bytes 20-2F - 16 bytes to hold 128 bit variables and 
6349 \newline 
6350 - Bytes 30-7F - 60 bytes for general purpose use.
6351 \newline 
6352
6353 \newline 
6354 Normally the SDCC compiler will only utilise the first bank of registers,
6355  but it is possible to specify that other banks of registers should be used
6356  in interrupt routines.
6357  By default, the compiler will place the stack after the last bank of used
6358  registers, i.e.
6359  if the first 2 banks of registers are used, it will position the base of
6360  the internal stack at address 16 (0X10).
6361  This implies that as the stack grows, it will use up the remaining register
6362  banks, and the 16 bytes used by the 128 bit variables, and 60 bytes for
6363  general purpose use.
6364 \layout Standard
6365
6366 By default, the compiler uses the 60 general purpose bytes to hold "near
6367  data".
6368  The compiler/optimiser may also declare some Local Variables in this area
6369  to hold local data.
6370  
6371 \layout Standard
6372
6373 If any of the 128 bit variables are used, or near data is being used then
6374  care needs to be taken to ensure that the stack does not grow so much that
6375  it starts to over write either your bit variables or "near data".
6376  There is no runtime checking to prevent this from happening.
6377 \layout Standard
6378
6379 The amount of stack being used is affected by the use of the "internal stack"
6380  to save registers before a subroutine call is made (--stack-auto will declare
6381  parameters and local variables on the stack) and the number of nested subroutin
6382 es.
6383 \layout Standard
6384
6385 If you detect that the stack is over writing you data, then the following
6386  can be done.
6387  --xstack will cause an external stack to be used for saving registers and
6388  (if --stack-auto is being used) storing parameters and local variables.
6389  However this will produce more code which will be slower to execute.
6390  
6391 \layout Standard
6392
6393 --stack-loc will allow you specify the start of the stack, i.e.
6394  you could start it after any data in the general purpose area.
6395  However this may waste the memory not used by the register banks and if
6396  the size of the "near data" increases, it may creep into the bottom of
6397  the stack.
6398 \layout Standard
6399
6400 --stack-after-data, similar to the --stack-loc, but it automatically places
6401  the stack after the end of the "near data".
6402  Again this could waste any spare register space.
6403 \layout Standard
6404
6405 --data-loc allows you to specify the start address of the near data.
6406  This could be used to move the "near data" further away from the stack
6407  giving it more room to grow.
6408  This will only work if no bit variables are being used and the stack can
6409  grow to use the bit variable space.
6410 \newline 
6411
6412 \newline 
6413 Conclusion.
6414 \newline 
6415
6416 \newline 
6417 If you find that the stack is over writing your bit variables or "near data"
6418  then the approach which best utilised the internal memory is to position
6419  the "near data" after the last bank of used registers or, if you use bit
6420  variables, after the last bit variable by using the --data-loc, e.g.
6421  if two register banks are being used and no bit variables, --data-loc 16,
6422  and use the --stack-after-data option.
6423 \layout Standard
6424
6425 If bit variables are being used, another method would be to try and squeeze
6426  the data area in the unused register banks if it will fit, and start the
6427  stack after the last bit variable.
6428 \layout Section
6429
6430 Retargetting for other MCUs.
6431 \layout Standard
6432
6433 The issues for retargetting the compiler are far too numerous to be covered
6434  by this document.
6435  What follows is a brief description of each of the seven phases of the
6436  compiler and its MCU dependency.
6437 \layout Itemize
6438
6439 Parsing the source and building the annotated parse tree.
6440  This phase is largely MCU independent (except for the language extensions).
6441  Syntax & semantic checks are also done in this phase, along with some initial
6442  optimizations like back patching labels and the pattern matching optimizations
6443  like bit-rotation etc.
6444 \layout Itemize
6445
6446 The second phase involves generating an intermediate code which can be easy
6447  manipulated during the later phases.
6448  This phase is entirely MCU independent.
6449  The intermediate code generation assumes the target machine has unlimited
6450  number of registers, and designates them with the name iTemp.
6451  The compiler can be made to dump a human readable form of the code generated
6452  by using the --dumpraw option.
6453 \layout Itemize
6454
6455 This phase does the bulk of the standard optimizations and is also MCU independe
6456 nt.
6457  This phase can be broken down into several sub-phases:
6458 \newline 
6459
6460 \newline 
6461 Break down intermediate code (iCode) into basic blocks.
6462 \newline 
6463 Do control flow & data flow analysis on the basic blocks.
6464 \newline 
6465 Do local common subexpression elimination, then global subexpression elimination
6466 \newline 
6467 Dead code elimination
6468 \newline 
6469 Loop optimizations
6470 \newline 
6471 If loop optimizations caused any changes then do 'global subexpression eliminati
6472 on' and 'dead code elimination' again.
6473 \layout Itemize
6474
6475 This phase determines the live-ranges; by live range I mean those iTemp
6476  variables defined by the compiler that still survive after all the optimization
6477 s.
6478  Live range analysis is essential for register allocation, since these computati
6479 on determines which of these iTemps will be assigned to registers, and for
6480  how long.
6481 \layout Itemize
6482
6483 Phase five is register allocation.
6484  There are two parts to this process.
6485 \newline 
6486
6487 \newline 
6488 The first part I call 'register packing' (for lack of a better term).
6489  In this case several MCU specific expression folding is done to reduce
6490  register pressure.
6491 \newline 
6492
6493 \newline 
6494 The second part is more MCU independent and deals with allocating registers
6495  to the remaining live ranges.
6496  A lot of MCU specific code does creep into this phase because of the limited
6497  number of index registers available in the 8051.
6498 \layout Itemize
6499
6500 The Code generation phase is (unhappily), entirely MCU dependent and very
6501  little (if any at all) of this code can be reused for other MCU.
6502  However the scheme for allocating a homogenized assembler operand for each
6503  iCode operand may be reused.
6504 \layout Itemize
6505
6506 As mentioned in the optimization section the peep-hole optimizer is rule
6507  based system, which can reprogrammed for other MCUs.
6508 \layout Section
6509
6510 SDCDB - Source Level Debugger
6511 \layout Standard
6512
6513 SDCC is distributed with a source level debugger.
6514  The debugger uses a command line interface, the command repertoire of the
6515  debugger has been kept as close to gdb (the GNU debugger) as possible.
6516  The configuration and build process is part of the standard compiler installati
6517 on, which also builds and installs the debugger in the target directory
6518  specified during configuration.
6519  The debugger allows you debug BOTH at the C source and at the ASM source
6520  level.
6521 \layout Subsection
6522
6523 Compiling for Debugging
6524 \layout Standard
6525
6526 The \SpecialChar \-
6527 \SpecialChar \-
6528 debug option must be specified for all files for which debug information
6529  is to be generated.
6530  The complier generates a .cdb file for each of these files.
6531  The linker updates the .cdb file with the address information.
6532  This .cdb is used by the debugger.
6533 \layout Subsection
6534
6535 How the Debugger Works
6536 \layout Standard
6537
6538 When the --debug option is specified the compiler generates extra symbol
6539  information some of which are put into the the assembler source and some
6540  are put into the .cdb file, the linker updates the .cdb file with the address
6541  information for the symbols.
6542  The debugger reads the symbolic information generated by the compiler &
6543  the address information generated by the linker.
6544  It uses the SIMULATOR (Daniel's S51) to execute the program, the program
6545  execution is controlled by the debugger.
6546  When a command is issued for the debugger, it translates it into appropriate
6547  commands for the simulator.
6548 \layout Subsection
6549
6550 Starting the Debugger
6551 \layout Standard
6552
6553 The debugger can be started using the following command line.
6554  (Assume the file you are debugging has the file name foo).
6555 \newline 
6556
6557 \newline 
6558
6559 \family sans 
6560 \series bold 
6561 sdcdb foo
6562 \newline 
6563
6564 \family default 
6565 \series default 
6566
6567 \newline 
6568 The debugger will look for the following files.
6569 \layout Itemize
6570
6571 foo.c - the source file.
6572 \layout Itemize
6573
6574 foo.cdb - the debugger symbol information file.
6575 \layout Itemize
6576
6577 foo.ihx - the intel hex format object file.
6578 \layout Subsection
6579
6580 Command Line Options.
6581 \layout Itemize
6582
6583 --directory=<source file directory> this option can used to specify the
6584  directory search list.
6585  The debugger will look into the directory list specified for source, cdb
6586  & ihx files.
6587  The items in the directory list must be separated by ':', e.g.
6588  if the source files can be in the directories /home/src1 and /home/src2,
6589  the --directory option should be --directory=/home/src1:/home/src2.
6590  Note there can be no spaces in the option.
6591  
6592 \layout Itemize
6593
6594 -cd <directory> - change to the <directory>.
6595 \layout Itemize
6596
6597 -fullname - used by GUI front ends.
6598 \layout Itemize
6599
6600 -cpu <cpu-type> - this argument is passed to the simulator please see the
6601  simulator docs for details.
6602 \layout Itemize
6603
6604 -X <Clock frequency > this options is passed to the simulator please see
6605  the simulator docs for details.
6606 \layout Itemize
6607
6608 -s <serial port file> passed to simulator see the simulator docs for details.
6609 \layout Itemize
6610
6611 -S <serial in,out> passed to simulator see the simulator docs for details.
6612 \layout Subsection
6613
6614 Debugger Commands.
6615 \layout Standard
6616
6617 As mention earlier the command interface for the debugger has been deliberately
6618  kept as close the GNU debugger gdb, as possible.
6619  This will help the integration with existing graphical user interfaces
6620  (like ddd, xxgdb or xemacs) existing for the GNU debugger.
6621 \layout Subsubsection
6622
6623 break [line | file:line | function | file:function]
6624 \layout Standard
6625
6626 Set breakpoint at specified line or function:
6627 \newline 
6628
6629 \newline 
6630
6631 \family sans 
6632 \series bold 
6633 sdcdb>break 100 
6634 \newline 
6635 sdcdb>break foo.c:100
6636 \newline 
6637 sdcdb>break funcfoo
6638 \newline 
6639 sdcdb>break foo.c:funcfoo
6640 \layout Subsubsection
6641
6642 clear [line | file:line | function | file:function ]
6643 \layout Standard
6644
6645 Clear breakpoint at specified line or function:
6646 \newline 
6647
6648 \newline 
6649
6650 \family sans 
6651 \series bold 
6652 sdcdb>clear 100
6653 \newline 
6654 sdcdb>clear foo.c:100
6655 \newline 
6656 sdcdb>clear funcfoo
6657 \newline 
6658 sdcdb>clear foo.c:funcfoo
6659 \layout Subsubsection
6660
6661 continue
6662 \layout Standard
6663
6664 Continue program being debugged, after breakpoint.
6665 \layout Subsubsection
6666
6667 finish
6668 \layout Standard
6669
6670 Execute till the end of the current function.
6671 \layout Subsubsection
6672
6673 delete [n]
6674 \layout Standard
6675
6676 Delete breakpoint number 'n'.
6677  If used without any option clear ALL user defined break points.
6678 \layout Subsubsection
6679
6680 info [break | stack | frame | registers ]
6681 \layout Itemize
6682
6683 info break - list all breakpoints
6684 \layout Itemize
6685
6686 info stack - show the function call stack.
6687 \layout Itemize
6688
6689 info frame - show information about the current execution frame.
6690 \layout Itemize
6691
6692 info registers - show content of all registers.
6693 \layout Subsubsection
6694
6695 step
6696 \layout Standard
6697
6698 Step program until it reaches a different source line.
6699 \layout Subsubsection
6700
6701 next
6702 \layout Standard
6703
6704 Step program, proceeding through subroutine calls.
6705 \layout Subsubsection
6706
6707 run
6708 \layout Standard
6709
6710 Start debugged program.
6711 \layout Subsubsection
6712
6713 ptype variable 
6714 \layout Standard
6715
6716 Print type information of the variable.
6717 \layout Subsubsection
6718
6719 print variable
6720 \layout Standard
6721
6722 print value of variable.
6723 \layout Subsubsection
6724
6725 file filename
6726 \layout Standard
6727
6728 load the given file name.
6729  Note this is an alternate method of loading file for debugging.
6730 \layout Subsubsection
6731
6732 frame
6733 \layout Standard
6734
6735 print information about current frame.
6736 \layout Subsubsection
6737
6738 set srcmode
6739 \layout Standard
6740
6741 Toggle between C source & assembly source.
6742 \layout Subsubsection
6743
6744 ! simulator command
6745 \layout Standard
6746
6747 Send the string following '!' to the simulator, the simulator response is
6748  displayed.
6749  Note the debugger does not interpret the command being sent to the simulator,
6750  so if a command like 'go' is sent the debugger can loose its execution
6751  context and may display incorrect values.
6752 \layout Subsubsection
6753
6754 quit.
6755 \layout Standard
6756
6757 "Watch me now.
6758  Iam going Down.
6759  My name is Bobby Brown"
6760 \layout Subsection
6761
6762 Interfacing with XEmacs.
6763 \layout Standard
6764
6765 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
6766  sdcdb.el and sdcdbsrc.el.
6767  These two files can be found in the $(prefix)/bin directory after the installat
6768 ion is complete.
6769  These files need to be loaded into XEmacs for the interface to work.
6770  This can be done at XEmacs startup time by inserting the following into
6771  your '.xemacs' file (which can be found in your HOME directory): 
6772 \newline 
6773
6774 \newline 
6775
6776 \family typewriter 
6777 (load-file sdcdbsrc.el) 
6778 \family default 
6779
6780 \newline 
6781
6782 \newline 
6783 .xemacs is a lisp file so the () around the command is REQUIRED.
6784  The files can also be loaded dynamically while XEmacs is running, set the
6785  environment variable 'EMACSLOADPATH' to the installation bin directory
6786  (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
6787  To start the interface enter the following command: 
6788 \newline 
6789
6790 \newline 
6791
6792 \family sans 
6793 \series bold 
6794 ESC-x sdcdbsrc
6795 \family default 
6796 \series default 
6797
6798 \newline 
6799
6800 \newline 
6801 You will prompted to enter the file name to be debugged.
6802  
6803 \newline 
6804
6805 \newline 
6806 The command line options that are passed to the simulator directly are bound
6807  to default values in the file sdcdbsrc.el.
6808  The variables are listed below, these values maybe changed as required.
6809 \layout Itemize
6810
6811 sdcdbsrc-cpu-type '51
6812 \layout Itemize
6813
6814 sdcdbsrc-frequency '11059200
6815 \layout Itemize
6816
6817 sdcdbsrc-serial nil
6818 \layout Standard
6819
6820 The following is a list of key mapping for the debugger interface.
6821 \layout Standard
6822
6823 \SpecialChar ~
6824
6825 \family typewriter 
6826
6827 \newline 
6828 ;; Current Listing :: 
6829 \newline 
6830 ;;key\SpecialChar ~
6831 \SpecialChar ~
6832 \SpecialChar ~
6833 \SpecialChar ~
6834 \SpecialChar ~
6835 \SpecialChar ~
6836 \SpecialChar ~
6837 \SpecialChar ~
6838 \SpecialChar ~
6839 \SpecialChar ~
6840 \SpecialChar ~
6841 \SpecialChar ~
6842 \SpecialChar ~
6843 \SpecialChar ~
6844 \SpecialChar ~
6845 binding\SpecialChar ~
6846 \SpecialChar ~
6847 \SpecialChar ~
6848 \SpecialChar ~
6849 \SpecialChar ~
6850 \SpecialChar ~
6851 \SpecialChar ~
6852 \SpecialChar ~
6853 \SpecialChar ~
6854 \SpecialChar ~
6855 \SpecialChar ~
6856 \SpecialChar ~
6857 \SpecialChar ~
6858 \SpecialChar ~
6859 \SpecialChar ~
6860 \SpecialChar ~
6861 \SpecialChar ~
6862 \SpecialChar ~
6863 \SpecialChar ~
6864 \SpecialChar ~
6865 \SpecialChar ~
6866 \SpecialChar ~
6867 Comment 
6868 \newline 
6869 ;;---\SpecialChar ~
6870 \SpecialChar ~
6871 \SpecialChar ~
6872 \SpecialChar ~
6873 \SpecialChar ~
6874 \SpecialChar ~
6875 \SpecialChar ~
6876 \SpecialChar ~
6877 \SpecialChar ~
6878 \SpecialChar ~
6879 \SpecialChar ~
6880 \SpecialChar ~
6881 \SpecialChar ~
6882 \SpecialChar ~
6883 \SpecialChar ~
6884 -------\SpecialChar ~
6885 \SpecialChar ~
6886 \SpecialChar ~
6887 \SpecialChar ~
6888 \SpecialChar ~
6889 \SpecialChar ~
6890 \SpecialChar ~
6891 \SpecialChar ~
6892 \SpecialChar ~
6893 \SpecialChar ~
6894 \SpecialChar ~
6895 \SpecialChar ~
6896 \SpecialChar ~
6897 \SpecialChar ~
6898 \SpecialChar ~
6899 \SpecialChar ~
6900 \SpecialChar ~
6901 \SpecialChar ~
6902 \SpecialChar ~
6903 \SpecialChar ~
6904 \SpecialChar ~
6905 \SpecialChar ~
6906 ------- 
6907 \newline 
6908 ;; 
6909 \newline 
6910 ;; n\SpecialChar ~
6911 \SpecialChar ~
6912 \SpecialChar ~
6913 \SpecialChar ~
6914 \SpecialChar ~
6915 \SpecialChar ~
6916 \SpecialChar ~
6917 \SpecialChar ~
6918 \SpecialChar ~
6919 \SpecialChar ~
6920 \SpecialChar ~
6921 \SpecialChar ~
6922 \SpecialChar ~
6923 \SpecialChar ~
6924  sdcdb-next-from-src\SpecialChar ~
6925 \SpecialChar ~
6926 \SpecialChar ~
6927 \SpecialChar ~
6928 \SpecialChar ~
6929 \SpecialChar ~
6930 \SpecialChar ~
6931 \SpecialChar ~
6932 \SpecialChar ~
6933 \SpecialChar ~
6934 SDCDB next command 
6935 \newline 
6936 ;; b\SpecialChar ~
6937 \SpecialChar ~
6938 \SpecialChar ~
6939 \SpecialChar ~
6940 \SpecialChar ~
6941 \SpecialChar ~
6942 \SpecialChar ~
6943 \SpecialChar ~
6944 \SpecialChar ~
6945 \SpecialChar ~
6946 \SpecialChar ~
6947 \SpecialChar ~
6948 \SpecialChar ~
6949 \SpecialChar ~
6950  sdcdb-back-from-src\SpecialChar ~
6951 \SpecialChar ~
6952 \SpecialChar ~
6953 \SpecialChar ~
6954 \SpecialChar ~
6955 \SpecialChar ~
6956 \SpecialChar ~
6957 \SpecialChar ~
6958 \SpecialChar ~
6959 \SpecialChar ~
6960 SDCDB back command 
6961 \newline 
6962 ;; c\SpecialChar ~
6963 \SpecialChar ~
6964 \SpecialChar ~
6965 \SpecialChar ~
6966 \SpecialChar ~
6967 \SpecialChar ~
6968 \SpecialChar ~
6969 \SpecialChar ~
6970 \SpecialChar ~
6971 \SpecialChar ~
6972 \SpecialChar ~
6973 \SpecialChar ~
6974 \SpecialChar ~
6975 \SpecialChar ~
6976  sdcdb-cont-from-src\SpecialChar ~
6977 \SpecialChar ~
6978 \SpecialChar ~
6979 \SpecialChar ~
6980 \SpecialChar ~
6981 \SpecialChar ~
6982 \SpecialChar ~
6983 \SpecialChar ~
6984 \SpecialChar ~
6985 \SpecialChar ~
6986 SDCDB continue command
6987 \newline 
6988 ;; s\SpecialChar ~
6989 \SpecialChar ~
6990 \SpecialChar ~
6991 \SpecialChar ~
6992 \SpecialChar ~
6993 \SpecialChar ~
6994 \SpecialChar ~
6995 \SpecialChar ~
6996 \SpecialChar ~
6997 \SpecialChar ~
6998 \SpecialChar ~
6999 \SpecialChar ~
7000 \SpecialChar ~
7001 \SpecialChar ~
7002  sdcdb-step-from-src\SpecialChar ~
7003 \SpecialChar ~
7004 \SpecialChar ~
7005 \SpecialChar ~
7006 \SpecialChar ~
7007 \SpecialChar ~
7008 \SpecialChar ~
7009 \SpecialChar ~
7010 \SpecialChar ~
7011 \SpecialChar ~
7012 SDCDB step command 
7013 \newline 
7014 ;; ?\SpecialChar ~
7015 \SpecialChar ~
7016 \SpecialChar ~
7017 \SpecialChar ~
7018 \SpecialChar ~
7019 \SpecialChar ~
7020 \SpecialChar ~
7021 \SpecialChar ~
7022 \SpecialChar ~
7023 \SpecialChar ~
7024 \SpecialChar ~
7025 \SpecialChar ~
7026 \SpecialChar ~
7027 \SpecialChar ~
7028  sdcdb-whatis-c-sexp\SpecialChar ~
7029 \SpecialChar ~
7030 \SpecialChar ~
7031 \SpecialChar ~
7032 \SpecialChar ~
7033 \SpecialChar ~
7034 \SpecialChar ~
7035 \SpecialChar ~
7036 \SpecialChar ~
7037 \SpecialChar ~
7038 SDCDB ptypecommand for data at 
7039 \newline 
7040 ;;\SpecialChar ~
7041 \SpecialChar ~
7042 \SpecialChar ~
7043 \SpecialChar ~
7044 \SpecialChar ~
7045 \SpecialChar ~
7046 \SpecialChar ~
7047 \SpecialChar ~
7048 \SpecialChar ~
7049 \SpecialChar ~
7050 \SpecialChar ~
7051 \SpecialChar ~
7052 \SpecialChar ~
7053 \SpecialChar ~
7054 \SpecialChar ~
7055 \SpecialChar ~
7056 \SpecialChar ~
7057 \SpecialChar ~
7058 \SpecialChar ~
7059 \SpecialChar ~
7060 \SpecialChar ~
7061 \SpecialChar ~
7062 \SpecialChar ~
7063 \SpecialChar ~
7064 \SpecialChar ~
7065 \SpecialChar ~
7066 \SpecialChar ~
7067 \SpecialChar ~
7068 \SpecialChar ~
7069 \SpecialChar ~
7070 \SpecialChar ~
7071 \SpecialChar ~
7072 \SpecialChar ~
7073 \SpecialChar ~
7074 \SpecialChar ~
7075 \SpecialChar ~
7076 \SpecialChar ~
7077 \SpecialChar ~
7078 \SpecialChar ~
7079 \SpecialChar ~
7080 \SpecialChar ~
7081 \SpecialChar ~
7082 \SpecialChar ~
7083 \SpecialChar ~
7084 \SpecialChar ~
7085 \SpecialChar ~
7086  buffer point 
7087 \newline 
7088 ;; x\SpecialChar ~
7089 \SpecialChar ~
7090 \SpecialChar ~
7091 \SpecialChar ~
7092 \SpecialChar ~
7093 \SpecialChar ~
7094 \SpecialChar ~
7095 \SpecialChar ~
7096 \SpecialChar ~
7097 \SpecialChar ~
7098 \SpecialChar ~
7099 \SpecialChar ~
7100 \SpecialChar ~
7101 \SpecialChar ~
7102  sdcdbsrc-delete\SpecialChar ~
7103 \SpecialChar ~
7104 \SpecialChar ~
7105 \SpecialChar ~
7106 \SpecialChar ~
7107 \SpecialChar ~
7108 \SpecialChar ~
7109 \SpecialChar ~
7110 \SpecialChar ~
7111 \SpecialChar ~
7112 \SpecialChar ~
7113 \SpecialChar ~
7114 \SpecialChar ~
7115 \SpecialChar ~
7116 SDCDB Delete all breakpoints if no arg 
7117 \newline 
7118 ;;\SpecialChar ~
7119 \SpecialChar ~
7120 \SpecialChar ~
7121 \SpecialChar ~
7122 \SpecialChar ~
7123 \SpecialChar ~
7124 \SpecialChar ~
7125 \SpecialChar ~
7126 \SpecialChar ~
7127 \SpecialChar ~
7128 \SpecialChar ~
7129 \SpecialChar ~
7130 \SpecialChar ~
7131 \SpecialChar ~
7132 \SpecialChar ~
7133 \SpecialChar ~
7134 \SpecialChar ~
7135 \SpecialChar ~
7136 \SpecialChar ~
7137 \SpecialChar ~
7138 \SpecialChar ~
7139 \SpecialChar ~
7140 \SpecialChar ~
7141 \SpecialChar ~
7142 \SpecialChar ~
7143 \SpecialChar ~
7144 \SpecialChar ~
7145 \SpecialChar ~
7146 \SpecialChar ~
7147 \SpecialChar ~
7148 \SpecialChar ~
7149 \SpecialChar ~
7150 \SpecialChar ~
7151 \SpecialChar ~
7152 \SpecialChar ~
7153 \SpecialChar ~
7154 \SpecialChar ~
7155 \SpecialChar ~
7156 \SpecialChar ~
7157 \SpecialChar ~
7158 \SpecialChar ~
7159 \SpecialChar ~
7160 \SpecialChar ~
7161 \SpecialChar ~
7162 \SpecialChar ~
7163 \SpecialChar ~
7164 given or delete arg (C-u arg x) 
7165 \newline 
7166 ;; m\SpecialChar ~
7167 \SpecialChar ~
7168 \SpecialChar ~
7169 \SpecialChar ~
7170 \SpecialChar ~
7171 \SpecialChar ~
7172 \SpecialChar ~
7173 \SpecialChar ~
7174 \SpecialChar ~
7175 \SpecialChar ~
7176 \SpecialChar ~
7177 \SpecialChar ~
7178 \SpecialChar ~
7179 \SpecialChar ~
7180  sdcdbsrc-frame\SpecialChar ~
7181 \SpecialChar ~
7182 \SpecialChar ~
7183 \SpecialChar ~
7184 \SpecialChar ~
7185 \SpecialChar ~
7186 \SpecialChar ~
7187 \SpecialChar ~
7188 \SpecialChar ~
7189 \SpecialChar ~
7190 \SpecialChar ~
7191 \SpecialChar ~
7192 \SpecialChar ~
7193 \SpecialChar ~
7194 \SpecialChar ~
7195 SDCDB Display current frame if no arg, 
7196 \newline 
7197 ;;\SpecialChar ~
7198 \SpecialChar ~
7199 \SpecialChar ~
7200 \SpecialChar ~
7201 \SpecialChar ~
7202 \SpecialChar ~
7203 \SpecialChar ~
7204 \SpecialChar ~
7205 \SpecialChar ~
7206 \SpecialChar ~
7207 \SpecialChar ~
7208 \SpecialChar ~
7209 \SpecialChar ~
7210 \SpecialChar ~
7211 \SpecialChar ~
7212 \SpecialChar ~
7213 \SpecialChar ~
7214 \SpecialChar ~
7215 \SpecialChar ~
7216 \SpecialChar ~
7217 \SpecialChar ~
7218 \SpecialChar ~
7219 \SpecialChar ~
7220 \SpecialChar ~
7221 \SpecialChar ~
7222 \SpecialChar ~
7223 \SpecialChar ~
7224 \SpecialChar ~
7225 \SpecialChar ~
7226 \SpecialChar ~
7227 \SpecialChar ~
7228 \SpecialChar ~
7229 \SpecialChar ~
7230 \SpecialChar ~
7231 \SpecialChar ~
7232 \SpecialChar ~
7233 \SpecialChar ~
7234 \SpecialChar ~
7235 \SpecialChar ~
7236 \SpecialChar ~
7237 \SpecialChar ~
7238 \SpecialChar ~
7239 \SpecialChar ~
7240 \SpecialChar ~
7241 \SpecialChar ~
7242 \SpecialChar ~
7243 \SpecialChar ~
7244 given or display frame arg 
7245 \newline 
7246 ;;\SpecialChar ~
7247 \SpecialChar ~
7248 \SpecialChar ~
7249 \SpecialChar ~
7250 \SpecialChar ~
7251 \SpecialChar ~
7252 \SpecialChar ~
7253 \SpecialChar ~
7254 \SpecialChar ~
7255 \SpecialChar ~
7256 \SpecialChar ~
7257 \SpecialChar ~
7258 \SpecialChar ~
7259 \SpecialChar ~
7260 \SpecialChar ~
7261 \SpecialChar ~
7262 \SpecialChar ~
7263 \SpecialChar ~
7264 \SpecialChar ~
7265 \SpecialChar ~
7266 \SpecialChar ~
7267 \SpecialChar ~
7268 \SpecialChar ~
7269 \SpecialChar ~
7270 \SpecialChar ~
7271 \SpecialChar ~
7272 \SpecialChar ~
7273 \SpecialChar ~
7274 \SpecialChar ~
7275 \SpecialChar ~
7276 \SpecialChar ~
7277 \SpecialChar ~
7278 \SpecialChar ~
7279 \SpecialChar ~
7280 \SpecialChar ~
7281 \SpecialChar ~
7282 \SpecialChar ~
7283 \SpecialChar ~
7284 \SpecialChar ~
7285 \SpecialChar ~
7286 \SpecialChar ~
7287 \SpecialChar ~
7288 \SpecialChar ~
7289 \SpecialChar ~
7290 \SpecialChar ~
7291 \SpecialChar ~
7292 \SpecialChar ~
7293 buffer point 
7294 \newline 
7295 ;; !\SpecialChar ~
7296 \SpecialChar ~
7297 \SpecialChar ~
7298 \SpecialChar ~
7299 \SpecialChar ~
7300 \SpecialChar ~
7301 \SpecialChar ~
7302 \SpecialChar ~
7303 \SpecialChar ~
7304 \SpecialChar ~
7305 \SpecialChar ~
7306 \SpecialChar ~
7307 \SpecialChar ~
7308 \SpecialChar ~
7309  sdcdbsrc-goto-sdcdb\SpecialChar ~
7310 \SpecialChar ~
7311 \SpecialChar ~
7312 \SpecialChar ~
7313 \SpecialChar ~
7314 \SpecialChar ~
7315 \SpecialChar ~
7316 \SpecialChar ~
7317 \SpecialChar ~
7318 \SpecialChar ~
7319 Goto the SDCDB output buffer 
7320 \newline 
7321 ;; p\SpecialChar ~
7322 \SpecialChar ~
7323 \SpecialChar ~
7324 \SpecialChar ~
7325 \SpecialChar ~
7326 \SpecialChar ~
7327 \SpecialChar ~
7328 \SpecialChar ~
7329 \SpecialChar ~
7330 \SpecialChar ~
7331 \SpecialChar ~
7332 \SpecialChar ~
7333 \SpecialChar ~
7334 \SpecialChar ~
7335  sdcdb-print-c-sexp\SpecialChar ~
7336 \SpecialChar ~
7337 \SpecialChar ~
7338 \SpecialChar ~
7339 \SpecialChar ~
7340 \SpecialChar ~
7341 \SpecialChar ~
7342 \SpecialChar ~
7343 \SpecialChar ~
7344 \SpecialChar ~
7345 \SpecialChar ~
7346 SDCDB print command for data at 
7347 \newline 
7348 ;;\SpecialChar ~
7349 \SpecialChar ~
7350 \SpecialChar ~
7351 \SpecialChar ~
7352 \SpecialChar ~
7353 \SpecialChar ~
7354 \SpecialChar ~
7355 \SpecialChar ~
7356 \SpecialChar ~
7357 \SpecialChar ~
7358 \SpecialChar ~
7359 \SpecialChar ~
7360 \SpecialChar ~
7361 \SpecialChar ~
7362 \SpecialChar ~
7363 \SpecialChar ~
7364 \SpecialChar ~
7365 \SpecialChar ~
7366 \SpecialChar ~
7367 \SpecialChar ~
7368 \SpecialChar ~
7369 \SpecialChar ~
7370 \SpecialChar ~
7371 \SpecialChar ~
7372 \SpecialChar ~
7373 \SpecialChar ~
7374 \SpecialChar ~
7375 \SpecialChar ~
7376 \SpecialChar ~
7377 \SpecialChar ~
7378 \SpecialChar ~
7379 \SpecialChar ~
7380 \SpecialChar ~
7381 \SpecialChar ~
7382 \SpecialChar ~
7383 \SpecialChar ~
7384 \SpecialChar ~
7385 \SpecialChar ~
7386 \SpecialChar ~
7387 \SpecialChar ~
7388 \SpecialChar ~
7389 \SpecialChar ~
7390 \SpecialChar ~
7391 \SpecialChar ~
7392 \SpecialChar ~
7393 \SpecialChar ~
7394  buffer point 
7395 \newline 
7396 ;; g\SpecialChar ~
7397 \SpecialChar ~
7398 \SpecialChar ~
7399 \SpecialChar ~
7400 \SpecialChar ~
7401 \SpecialChar ~
7402 \SpecialChar ~
7403 \SpecialChar ~
7404 \SpecialChar ~
7405 \SpecialChar ~
7406 \SpecialChar ~
7407 \SpecialChar ~
7408 \SpecialChar ~
7409 \SpecialChar ~
7410  sdcdbsrc-goto-sdcdb\SpecialChar ~
7411 \SpecialChar ~
7412 \SpecialChar ~
7413 \SpecialChar ~
7414 \SpecialChar ~
7415 \SpecialChar ~
7416 \SpecialChar ~
7417 \SpecialChar ~
7418 \SpecialChar ~
7419 \SpecialChar ~
7420 Goto the SDCDB output buffer 
7421 \newline 
7422 ;; t\SpecialChar ~
7423 \SpecialChar ~
7424 \SpecialChar ~
7425 \SpecialChar ~
7426 \SpecialChar ~
7427 \SpecialChar ~
7428 \SpecialChar ~
7429 \SpecialChar ~
7430 \SpecialChar ~
7431 \SpecialChar ~
7432 \SpecialChar ~
7433 \SpecialChar ~
7434 \SpecialChar ~
7435 \SpecialChar ~
7436  sdcdbsrc-mode\SpecialChar ~
7437 \SpecialChar ~
7438 \SpecialChar ~
7439 \SpecialChar ~
7440 \SpecialChar ~
7441 \SpecialChar ~
7442 \SpecialChar ~
7443 \SpecialChar ~
7444 \SpecialChar ~
7445 \SpecialChar ~
7446 \SpecialChar ~
7447 \SpecialChar ~
7448 \SpecialChar ~
7449 \SpecialChar ~
7450 \SpecialChar ~
7451 \SpecialChar ~
7452 Toggles Sdcdbsrc mode (turns it off) 
7453 \newline 
7454 ;; 
7455 \newline 
7456 ;; C-c C-f\SpecialChar ~
7457 \SpecialChar ~
7458 \SpecialChar ~
7459 \SpecialChar ~
7460 \SpecialChar ~
7461 \SpecialChar ~
7462 \SpecialChar ~
7463 \SpecialChar ~
7464  sdcdb-finish-from-src\SpecialChar ~
7465 \SpecialChar ~
7466 \SpecialChar ~
7467 \SpecialChar ~
7468 \SpecialChar ~
7469 \SpecialChar ~
7470 \SpecialChar ~
7471 \SpecialChar ~
7472 SDCDB finish command 
7473 \newline 
7474 ;; 
7475 \newline 
7476 ;; C-x SPC\SpecialChar ~
7477 \SpecialChar ~
7478 \SpecialChar ~
7479 \SpecialChar ~
7480 \SpecialChar ~
7481 \SpecialChar ~
7482 \SpecialChar ~
7483 \SpecialChar ~
7484  sdcdb-break\SpecialChar ~
7485 \SpecialChar ~
7486 \SpecialChar ~
7487 \SpecialChar ~
7488 \SpecialChar ~
7489 \SpecialChar ~
7490 \SpecialChar ~
7491 \SpecialChar ~
7492 \SpecialChar ~
7493 \SpecialChar ~
7494 \SpecialChar ~
7495 \SpecialChar ~
7496 \SpecialChar ~
7497 \SpecialChar ~
7498 \SpecialChar ~
7499 \SpecialChar ~
7500 \SpecialChar ~
7501 \SpecialChar ~
7502 Set break for line with point 
7503 \newline 
7504 ;; ESC t\SpecialChar ~
7505 \SpecialChar ~
7506 \SpecialChar ~
7507 \SpecialChar ~
7508 \SpecialChar ~
7509 \SpecialChar ~
7510 \SpecialChar ~
7511 \SpecialChar ~
7512 \SpecialChar ~
7513 \SpecialChar ~
7514  sdcdbsrc-mode\SpecialChar ~
7515 \SpecialChar ~
7516 \SpecialChar ~
7517 \SpecialChar ~
7518 \SpecialChar ~
7519 \SpecialChar ~
7520 \SpecialChar ~
7521 \SpecialChar ~
7522 \SpecialChar ~
7523 \SpecialChar ~
7524 \SpecialChar ~
7525 \SpecialChar ~
7526 \SpecialChar ~
7527 \SpecialChar ~
7528 \SpecialChar ~
7529 \SpecialChar ~
7530 Toggle Sdcdbsrc mode 
7531 \newline 
7532 ;; ESC m\SpecialChar ~
7533 \SpecialChar ~
7534 \SpecialChar ~
7535 \SpecialChar ~
7536 \SpecialChar ~
7537 \SpecialChar ~
7538 \SpecialChar ~
7539 \SpecialChar ~
7540 \SpecialChar ~
7541 \SpecialChar ~
7542  sdcdbsrc-srcmode\SpecialChar ~
7543 \SpecialChar ~
7544 \SpecialChar ~
7545 \SpecialChar ~
7546 \SpecialChar ~
7547 \SpecialChar ~
7548 \SpecialChar ~
7549 \SpecialChar ~
7550 \SpecialChar ~
7551 \SpecialChar ~
7552 \SpecialChar ~
7553 \SpecialChar ~
7554  Toggle list mode 
7555 \newline 
7556 ;; 
7557 \family default 
7558
7559 \newline 
7560
7561 \layout Section
7562
7563 Other Processors
7564 \layout Subsection
7565
7566 The Z80 and gbz80 port
7567 \layout Standard
7568
7569 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
7570  The port is incomplete - long support is incomplete (mul, div and mod are
7571  unimplimented), and both float and bitfield support is missing.
7572  Apart from that the code generated is correct.
7573 \layout Standard
7574
7575 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
7576  The stack frame is similar to that generated by the IAR Z80 compiler.
7577  IX is used as the base pointer, HL is used as a temporary register, and
7578  BC and DE are available for holding varibles.
7579  IY is currently unusued.
7580  Return values are stored in HL.
7581  One bad side effect of using IX as the base pointer is that a functions
7582  stack frame is limited to 127 bytes - this will be fixed in a later version.
7583 \layout Section
7584
7585 Support
7586 \layout Standard
7587
7588 SDCC has grown to be a large project.
7589  The compiler alone (without the preprocessor, assembler and linker) is
7590  about 40,000 lines of code (blank stripped).
7591  The open source nature of this project is a key to its continued growth
7592  and support.
7593  You gain the benefit and support of many active software developers and
7594  end users.
7595  Is SDCC perfect? No, that's why we need your help.
7596  The developers take pride in fixing reported bugs.
7597  You can help by reporting the bugs and helping other SDCC users.
7598  There are lots of ways to contribute, and we encourage you to take part
7599  in making SDCC a great software package.
7600 \layout Subsection
7601
7602 Reporting Bugs
7603 \layout Standard
7604
7605 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
7606 cc@sdcc.sourceforge.net'.
7607  Bugs will be fixed ASAP.
7608  When reporting a bug, it is very useful to include a small test program
7609  which reproduces the problem.
7610  If you can isolate the problem by looking at the generated assembly code,
7611  this can be very helpful.
7612  Compiling your program with the --dumpall option can sometimes be useful
7613  in locating optimization problems.
7614 \layout Section
7615
7616 The anatomy of the compiler
7617 \layout Standard
7618
7619
7620 \shape italic 
7621 This is an excerpt from an atricle published in Circuit Cellar MagaZine
7622  in august 2000.
7623  It's a little outdated (the compiler is much more efficient now and user/devell
7624 oper friendly), but pretty well exposes the guts of it all.
7625 \shape default 
7626
7627 \newline 
7628
7629 \newline 
7630 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
7631  It is fairly easy to retarget for other 8-bit MCU.
7632  Here we take a look at some of the internals of the compiler.
7633  
7634 \layout Paragraph*
7635
7636 Parsing 
7637 \layout Standard
7638
7639 Parsing the input source file and creating an AST (Annotated Syntax Tree).
7640  This phase also involves propagating types (annotating each node of the
7641  parse tree with type information) and semantic analysis.
7642  There are some MCU specific parsing rules.
7643  For example the storage classes, the extended storage classes are MCU specific
7644  while there may be a xdata storage class for 8051 there is no such storage
7645  class for z80 or Atmel AVR.
7646  SDCC allows MCU specific storage class extensions, i.e.
7647  xdata will be treated as a storage class specifier when parsing 8051 C
7648  code but will be treated as a C identifier when parsing z80 or ATMEL AVR
7649  C code.
7650 \layout Paragraph
7651
7652 Generating iCode
7653 \layout Standard
7654
7655 Intermediate code generation.
7656  In this phase the AST is broken down into three-operand form (iCode).
7657  These three operand forms are represented as doubly linked lists.
7658  ICode is the term given to the intermediate form generated by the compiler.
7659  ICode example section shows some examples of iCode generated for some simple
7660  C source functions.
7661 \layout Paragraph
7662
7663 Optimizations.
7664 \layout Standard
7665
7666 Bulk of the target independent optimizations is performed in this phase.
7667  The optimizations include constant propagation, common sub-expression eliminati
7668 on, loop invariant code movement, strength reduction of loop induction variables
7669  and dead-code elimination.
7670 \layout Paragraph
7671
7672 Live range analysis
7673 \layout Standard
7674
7675 During intermediate code generation phase, the compiler assumes the target
7676  machine has infinite number of registers and generates a lot of temporary
7677  variables.
7678  The live range computation determines the lifetime of each of these compiler-ge
7679 nerated temporaries.
7680  A picture speaks a thousand words.
7681  ICode example sections show the live range annotations for each of the
7682  operand.
7683  It is important to note here, each iCode is assigned a number in the order
7684  of its execution in the function.
7685  The live ranges are computed in terms of these numbers.
7686  The from number is the number of the iCode which first defines the operand
7687  and the to number signifies the iCode which uses this operand last.
7688 \layout Paragraph
7689
7690 Register Allocation
7691 \layout Standard
7692
7693 The register allocation determines the type and number of registers needed
7694  by each operand.
7695  In most MCUs only a few registers can be used for indirect addressing.
7696  In case of 8051 for example the registers R0 & R1 can be used to indirectly
7697  address the internal ram and DPTR to indirectly address the external ram.
7698  The compiler will try to allocate the appropriate register to pointer variables
7699  if it can.
7700  ICode example section shows the operands annotated with the registers assigned
7701  to them.
7702  The compiler will try to keep operands in registers as much as possible;
7703  there are several schemes the compiler uses to do achieve this.
7704  When the compiler runs out of registers the compiler will check to see
7705  if there are any live operands which is not used or defined in the current
7706  basic block being processed, if there are any found then it will push that
7707  operand and use the registers in this block, the operand will then be popped
7708  at the end of the basic block.
7709  
7710 \layout Standard
7711
7712 There are other MCU specific considerations in this phase.
7713  Some MCUs have an accumulator; very short-lived operands could be assigned
7714  to the accumulator instead of general-purpose register.
7715 \layout Paragraph
7716
7717 Code generation
7718 \layout Standard
7719
7720 Figure II gives a table of iCode operations supported by the compiler.
7721  The code generation involves translating these operations into corresponding
7722  assembly code for the processor.
7723  This sounds overly simple but that is the essence of code generation.
7724  Some of the iCode operations are generated on a MCU specific manner for
7725  example, the z80 port does not use registers to pass parameters so the
7726  SEND and RECV iCode operations will not be generated, and it also does
7727  not support JUMPTABLES
7728 \layout Section*
7729
7730 <Where is Figure II ?>
7731 \layout Paragraph
7732
7733 ICode Example
7734 \layout Standard
7735
7736 This section shows some details of iCode.
7737  The example C code does not do anything useful; it is used as an example
7738  to illustrate the intermediate code generated by the compiler.
7739 \newline 
7740
7741 \newline 
7742
7743 \family typewriter 
7744 1.\SpecialChar ~
7745 xdata int * p;
7746 \newline 
7747 2.\SpecialChar ~
7748 int gint;
7749 \newline 
7750 3.\SpecialChar ~
7751 /* This function does nothing useful.
7752  It is used
7753 \newline 
7754 4.\SpecialChar ~
7755 \SpecialChar ~
7756 \SpecialChar ~
7757 \SpecialChar ~
7758 for the purpose of explaining iCode */
7759 \newline 
7760 5.\SpecialChar ~
7761 short function (data int *x)
7762 \newline 
7763 6.\SpecialChar ~
7764 {
7765 \newline 
7766 7.\SpecialChar ~
7767 \SpecialChar ~
7768 \SpecialChar ~
7769 short i=10; /* dead initialization eliminated */
7770 \newline 
7771 8.\SpecialChar ~
7772 \SpecialChar ~
7773 \SpecialChar ~
7774 short sum=10; /* dead initialization eliminated */
7775 \newline 
7776 9.\SpecialChar ~
7777 \SpecialChar ~
7778 \SpecialChar ~
7779 short mul;
7780 \newline 
7781 10.\SpecialChar ~
7782 \SpecialChar ~
7783 int j ;
7784 \newline 
7785 11.\SpecialChar ~
7786 \SpecialChar ~
7787 while (*x) *x++ = *p++; 
7788 \newline 
7789 12.\SpecialChar ~
7790 \SpecialChar ~
7791 \SpecialChar ~
7792 \SpecialChar ~
7793 sum = 0 ; 
7794 \newline 
7795 13.\SpecialChar ~
7796 \SpecialChar ~
7797 mul = 0;
7798 \newline 
7799 14.\SpecialChar ~
7800 \SpecialChar ~
7801 /* compiler detects i,j to be induction variables */
7802 \newline 
7803 15.\SpecialChar ~
7804 \SpecialChar ~
7805 for (i = 0, j = 10 ; i < 10 ; i++, j--) {
7806 \newline 
7807 16.\SpecialChar ~
7808 \SpecialChar ~
7809 \SpecialChar ~
7810 \SpecialChar ~
7811 sum += i;
7812 \newline 
7813 17.\SpecialChar ~
7814 \SpecialChar ~
7815 \SpecialChar ~
7816 \SpecialChar ~
7817 mul += i * 3; /* this multiplication remains */
7818 \newline 
7819 18.\SpecialChar ~
7820 \SpecialChar ~
7821 \SpecialChar ~
7822 \SpecialChar ~
7823 gint += j * 3;/* this multiplication changed to addition */
7824 \newline 
7825 19.\SpecialChar ~
7826 \SpecialChar ~
7827 }
7828 \newline 
7829 20.\SpecialChar ~
7830 \SpecialChar ~
7831 return sum+mul;
7832 \newline 
7833 21.\SpecialChar ~
7834 }
7835 \newline 
7836
7837 \newline 
7838
7839 \family default 
7840 In addition to the operands each iCode contains information about the filename
7841  and line it corresponds to in the source file.
7842  The first field in the listing should be interpreted as follows:
7843 \newline 
7844
7845 \shape italic 
7846 Filename(linenumber: iCode Execution sequence number : ICode hash table
7847  key : loop depth of the iCode).
7848 \newline 
7849
7850 \shape default 
7851 Then follows the human readable form of the ICode operation.
7852  Each operand of this triplet form can be of three basic types a) compiler
7853  generated temporary b) user defined variable c) a constant value.
7854  Note that local variables and parameters are replaced by compiler generated
7855  temporaries.
7856  Live ranges are computed only for temporaries (i.e.
7857  live ranges are not computed for global variables).
7858  Registers are allocated for temporaries only.
7859  Operands are formatted in the following manner:
7860 \newline 
7861
7862 \shape italic 
7863 Operand Name [lr live-from : live-to ] { type information } [ registers
7864  allocated ].
7865 \newline 
7866
7867 \shape default 
7868 As mentioned earlier the live ranges are computed in terms of the execution
7869  sequence number of the iCodes, for example 
7870 \newline 
7871 the iTemp0 is live from (i.e.
7872  first defined in iCode with execution sequence number 3, and is last used
7873  in the iCode with sequence number 5).
7874  For induction variables such as iTemp21 the live range computation extends
7875  the lifetime from the start to the end of the loop.
7876 \newline 
7877 The register allocator used the live range information to allocate registers,
7878  the same registers may be used for different temporaries if their live
7879  ranges do not overlap, for example r0 is allocated to both iTemp6 and to
7880  iTemp17 since their live ranges do not overlap.
7881  In addition the allocator also takes into consideration the type and usage
7882  of a temporary, for example itemp6 is a pointer to near space and is used
7883  as to fetch data from (i.e.
7884  used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
7885  Some short lived temporaries are allocated to special registers which have
7886  meaning to the code generator e.g.
7887  iTemp13 is allocated to a pseudo register CC which tells the back end that
7888  the temporary is used only for a conditional jump the code generation makes
7889  use of this information to optimize a compare and jump ICode.
7890 \newline 
7891 There are several loop optimizations performed by the compiler.
7892  It can detect induction variables iTemp21(i) and iTemp23(j).
7893  Also note the compiler does selective strength reduction, i.e.
7894  the multiplication of an induction variable in line 18 (gint = j * 3) is
7895  changed to addition, a new temporary iTemp17 is allocated and assigned
7896  a initial value, a constant 3 is then added for each iteration of the loop.
7897  The compiler does not change the multiplication in line 17 however since
7898  the processor does support an 8 * 8 bit multiplication.
7899 \newline 
7900 Note the dead code elimination optimization eliminated the dead assignments
7901  in line 7 & 8 to I and sum respectively.
7902 \newline 
7903
7904 \layout Standard
7905
7906
7907 \family typewriter 
7908 Sample.c (5:1:0:0) _entry($9) :
7909 \layout Standard
7910
7911
7912 \family typewriter 
7913 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
7914 \layout Standard
7915
7916
7917 \family typewriter 
7918 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv 
7919 \layout Standard
7920
7921
7922 \family typewriter 
7923 Sample.c(11:4:53:0) preHeaderLbl0($11) :
7924 \layout Standard
7925
7926
7927 \family typewriter 
7928 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
7929  * int}[r2]
7930 \layout Standard
7931
7932
7933 \family typewriter 
7934 Sample.c(11:6:5:1) _whilecontinue_0($1) :
7935 \layout Standard
7936
7937
7938 \family typewriter 
7939 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
7940  int}[r0]]
7941 \layout Standard
7942
7943
7944 \family typewriter 
7945 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
7946 \layout Standard
7947
7948
7949 \family typewriter 
7950 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
7951  * int}
7952 \layout Standard
7953
7954
7955 \family typewriter 
7956 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
7957  {short}
7958 \layout Standard
7959
7960
7961 \family typewriter 
7962 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
7963  * int}[DPTR]]
7964 \layout Standard
7965
7966
7967 \family typewriter 
7968 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
7969 }[r2 r3]
7970 \layout Standard
7971
7972
7973 \family typewriter 
7974 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
7975  * int}[r0] + 
7976 \layout Standard
7977
7978
7979 \family typewriter 
7980 0x2 {short}
7981 \layout Standard
7982
7983
7984 \family typewriter 
7985 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
7986 \layout Standard
7987
7988
7989 \family typewriter 
7990 Sample.c(11:17:21:0)_whilebreak_0($3) :
7991 \layout Standard
7992
7993
7994 \family typewriter 
7995 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
7996 \layout Standard
7997
7998
7999 \family typewriter 
8000 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
8001 \layout Standard
8002
8003
8004 \family typewriter 
8005 Sample.c(15:20:54:0)preHeaderLbl1($13) :
8006 \layout Standard
8007
8008
8009 \family typewriter 
8010 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
8011 \layout Standard
8012
8013
8014 \family typewriter 
8015 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
8016 \layout Standard
8017
8018
8019 \family typewriter 
8020 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
8021 \layout Standard
8022
8023
8024 \family typewriter 
8025 Sample.c(15:24:26:1)_forcond_0($4) :
8026 \layout Standard
8027
8028
8029 \family typewriter 
8030 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
8031  < 0xa {short}
8032 \layout Standard
8033
8034
8035 \family typewriter 
8036 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
8037 \layout Standard
8038
8039
8040 \family typewriter 
8041 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
8042  + 
8043 \layout Standard
8044
8045
8046 \family typewriter 
8047 ITemp21 [lr21:38]{short}[r4]
8048 \layout Standard
8049
8050
8051 \family typewriter 
8052 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
8053  * 0x3 {short}
8054 \layout Standard
8055
8056
8057 \family typewriter 
8058 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
8059  + 
8060 \layout Standard
8061
8062
8063 \family typewriter 
8064 iTemp15 [lr29:30]{short}[r1]
8065 \layout Standard
8066
8067
8068 \family typewriter 
8069 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
8070  r0]- 0x3 {short}
8071 \layout Standard
8072
8073
8074 \family typewriter 
8075 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
8076 int}[r7 r0]
8077 \layout Standard
8078
8079
8080 \family typewriter 
8081 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
8082  + 0x1 {short}
8083 \layout Standard
8084
8085
8086 \family typewriter 
8087 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
8088  r6]- 0x1 {short}
8089 \layout Standard
8090
8091
8092 \family typewriter 
8093 Sample.c(19:38:47:1) goto _forcond_0($4)
8094 \layout Standard
8095
8096
8097 \family typewriter 
8098 Sample.c(19:39:48:0)_forbreak_0($7) :
8099 \layout Standard
8100
8101
8102 \family typewriter 
8103 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
8104  + 
8105 \layout Standard
8106
8107
8108 \family typewriter 
8109 ITemp11 [lr19:40]{short}[r3]
8110 \layout Standard
8111
8112
8113 \family typewriter 
8114 sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
8115 \layout Standard
8116
8117
8118 \family typewriter 
8119 sample.c(20:42:51:0)_return($8) :
8120 \layout Standard
8121
8122
8123 \family typewriter 
8124 sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
8125 \newline 
8126
8127 \family default 
8128
8129 \newline 
8130 Finally the code generated for this function:
8131 \newline 
8132
8133 \layout Standard
8134
8135
8136 \family typewriter 
8137 .area DSEG (DATA)
8138 \layout Standard
8139
8140
8141 \family typewriter 
8142 _p::
8143 \layout Standard
8144
8145
8146 \family typewriter 
8147 \SpecialChar ~
8148 \SpecialChar ~
8149 .ds 2
8150 \layout Standard
8151
8152
8153 \family typewriter 
8154 _gint::
8155 \layout Standard
8156
8157
8158 \family typewriter 
8159 \SpecialChar ~
8160 \SpecialChar ~
8161 .ds 2
8162 \layout Standard
8163
8164
8165 \family typewriter 
8166 ; sample.c 5
8167 \layout Standard
8168
8169
8170 \family typewriter 
8171 ; -----------------------------------------
8172 \layout Standard
8173
8174
8175 \family typewriter 
8176 ; function function
8177 \layout Standard
8178
8179
8180 \family typewriter 
8181 ; -----------------------------------------
8182 \layout Standard
8183
8184
8185 \family typewriter 
8186 _function:
8187 \layout Standard
8188
8189
8190 \family typewriter 
8191 ; iTemp0 [lr3:5]{_near * int}[r2] = recv 
8192 \layout Standard
8193
8194
8195 \family typewriter 
8196 \SpecialChar ~
8197 \SpecialChar ~
8198 mov r2,dpl
8199 \layout Standard
8200
8201
8202 \family typewriter 
8203 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
8204 \layout Standard
8205
8206
8207 \family typewriter 
8208 \SpecialChar ~
8209 \SpecialChar ~
8210 mov ar0,r2
8211 \layout Standard
8212
8213
8214 \family typewriter 
8215 ;_whilecontinue_0($1) :
8216 \layout Standard
8217
8218
8219 \family typewriter 
8220 00101$:
8221 \layout Standard
8222
8223
8224 \family typewriter 
8225 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
8226 \layout Standard
8227
8228
8229 \family typewriter 
8230 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
8231 \layout Standard
8232
8233
8234 \family typewriter 
8235 \SpecialChar ~
8236 \SpecialChar ~
8237 mov ar2,@r0
8238 \layout Standard
8239
8240
8241 \family typewriter 
8242 \SpecialChar ~
8243 \SpecialChar ~
8244 inc r0
8245 \layout Standard
8246
8247
8248 \family typewriter 
8249 \SpecialChar ~
8250 \SpecialChar ~
8251 mov ar3,@r0
8252 \layout Standard
8253
8254
8255 \family typewriter 
8256 \SpecialChar ~
8257 \SpecialChar ~
8258 dec r0
8259 \layout Standard
8260
8261
8262 \family typewriter 
8263 \SpecialChar ~
8264 \SpecialChar ~
8265 mov a,r2
8266 \layout Standard
8267
8268
8269 \family typewriter 
8270 \SpecialChar ~
8271 \SpecialChar ~
8272 orl a,r3
8273 \layout Standard
8274
8275
8276 \family typewriter 
8277 \SpecialChar ~
8278 \SpecialChar ~
8279 jz 00103$
8280 \layout Standard
8281
8282
8283 \family typewriter 
8284 00114$:
8285 \layout Standard
8286
8287
8288 \family typewriter 
8289 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
8290 \layout Standard
8291
8292
8293 \family typewriter 
8294 \SpecialChar ~
8295 \SpecialChar ~
8296 mov dpl,_p
8297 \layout Standard
8298
8299
8300 \family typewriter 
8301 \SpecialChar ~
8302 \SpecialChar ~
8303 mov dph,(_p + 1)
8304 \layout Standard
8305
8306
8307 \family typewriter 
8308 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
8309 \layout Standard
8310
8311
8312 \family typewriter 
8313 \SpecialChar ~
8314 \SpecialChar ~
8315 mov a,#0x02
8316 \layout Standard
8317
8318
8319 \family typewriter 
8320 \SpecialChar ~
8321 \SpecialChar ~
8322 add a,_p
8323 \layout Standard
8324
8325
8326 \family typewriter 
8327 \SpecialChar ~
8328 \SpecialChar ~
8329 mov _p,a
8330 \layout Standard
8331
8332
8333 \family typewriter 
8334 \SpecialChar ~
8335 \SpecialChar ~
8336 clr a
8337 \layout Standard
8338
8339
8340 \family typewriter 
8341 \SpecialChar ~
8342 \SpecialChar ~
8343 addc a,(_p + 1)
8344 \layout Standard
8345
8346
8347 \family typewriter 
8348 \SpecialChar ~
8349 \SpecialChar ~
8350 mov (_p + 1),a
8351 \layout Standard
8352
8353
8354 \family typewriter 
8355 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
8356 \layout Standard
8357
8358
8359 \family typewriter 
8360 \SpecialChar ~
8361 \SpecialChar ~
8362 movx a,@dptr
8363 \layout Standard
8364
8365
8366 \family typewriter 
8367 \SpecialChar ~
8368 \SpecialChar ~
8369 mov r2,a
8370 \layout Standard
8371
8372
8373 \family typewriter 
8374 \SpecialChar ~
8375 \SpecialChar ~
8376 inc dptr
8377 \layout Standard
8378
8379
8380 \family typewriter 
8381 \SpecialChar ~
8382 \SpecialChar ~
8383 movx a,@dptr
8384 \layout Standard
8385
8386
8387 \family typewriter 
8388 \SpecialChar ~
8389 \SpecialChar ~
8390 mov r3,a
8391 \layout Standard
8392
8393
8394 \family typewriter 
8395 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
8396 \layout Standard
8397
8398
8399 \family typewriter 
8400 \SpecialChar ~
8401 \SpecialChar ~
8402 mov @r0,ar2
8403 \layout Standard
8404
8405
8406 \family typewriter 
8407 \SpecialChar ~
8408 \SpecialChar ~
8409 inc r0
8410 \layout Standard
8411
8412
8413 \family typewriter 
8414 \SpecialChar ~
8415 \SpecialChar ~
8416 mov @r0,ar3
8417 \layout Standard
8418
8419
8420 \family typewriter 
8421 ; iTemp6 [lr5:16]{_near * int}[r0] = 
8422 \layout Standard
8423
8424
8425 \family typewriter 
8426 ; iTemp6 [lr5:16]{_near * int}[r0] + 
8427 \layout Standard
8428
8429
8430 \family typewriter 
8431 ; 0x2 {short}
8432 \layout Standard
8433
8434
8435 \family typewriter 
8436 \SpecialChar ~
8437 \SpecialChar ~
8438 inc r0
8439 \layout Standard
8440
8441
8442 \family typewriter 
8443 ; goto _whilecontinue_0($1)
8444 \layout Standard
8445
8446
8447 \family typewriter 
8448 \SpecialChar ~
8449 \SpecialChar ~
8450 sjmp 00101$
8451 \layout Standard
8452
8453
8454 \family typewriter 
8455 ; _whilebreak_0($3) :
8456 \layout Standard
8457
8458
8459 \family typewriter 
8460 00103$:
8461 \layout Standard
8462
8463
8464 \family typewriter 
8465 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
8466 \layout Standard
8467
8468
8469 \family typewriter 
8470 \SpecialChar ~
8471 \SpecialChar ~
8472 mov r2,#0x00
8473 \layout Standard
8474
8475
8476 \family typewriter 
8477 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
8478 \layout Standard
8479
8480
8481 \family typewriter 
8482 \SpecialChar ~
8483 \SpecialChar ~
8484 mov r3,#0x00
8485 \layout Standard
8486
8487
8488 \family typewriter 
8489 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
8490 \layout Standard
8491
8492
8493 \family typewriter 
8494 \SpecialChar ~
8495 \SpecialChar ~
8496 mov r4,#0x00
8497 \layout Standard
8498
8499
8500 \family typewriter 
8501 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
8502 \layout Standard
8503
8504
8505 \family typewriter 
8506 \SpecialChar ~
8507 \SpecialChar ~
8508 mov r5,#0x0A
8509 \layout Standard
8510
8511
8512 \family typewriter 
8513 \SpecialChar ~
8514 \SpecialChar ~
8515 mov r6,#0x00
8516 \layout Standard
8517
8518
8519 \family typewriter 
8520 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
8521 \layout Standard
8522
8523
8524 \family typewriter 
8525 \SpecialChar ~
8526 \SpecialChar ~
8527 mov r7,#0x1E
8528 \layout Standard
8529
8530
8531 \family typewriter 
8532 \SpecialChar ~
8533 \SpecialChar ~
8534 mov r0,#0x00
8535 \layout Standard
8536
8537
8538 \family typewriter 
8539 ; _forcond_0($4) :
8540 \layout Standard
8541
8542
8543 \family typewriter 
8544 00104$:
8545 \layout Standard
8546
8547
8548 \family typewriter 
8549 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
8550 \layout Standard
8551
8552
8553 \family typewriter 
8554 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
8555 \layout Standard
8556
8557
8558 \family typewriter 
8559 \SpecialChar ~
8560 \SpecialChar ~
8561 clr c
8562 \layout Standard
8563
8564
8565 \family typewriter 
8566 \SpecialChar ~
8567 \SpecialChar ~
8568 mov a,r4
8569 \layout Standard
8570
8571
8572 \family typewriter 
8573 \SpecialChar ~
8574 \SpecialChar ~
8575 xrl a,#0x80
8576 \layout Standard
8577
8578
8579 \family typewriter 
8580 \SpecialChar ~
8581 \SpecialChar ~
8582 subb a,#0x8a
8583 \layout Standard
8584
8585
8586 \family typewriter 
8587 \SpecialChar ~
8588 \SpecialChar ~
8589 jnc 00107$
8590 \layout Standard
8591
8592
8593 \family typewriter 
8594 00115$:
8595 \layout Standard
8596
8597
8598 \family typewriter 
8599 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] + 
8600 \layout Standard
8601
8602
8603 \family typewriter 
8604 ; iTemp21 [lr21:38]{short}[r4]
8605 \layout Standard
8606
8607
8608 \family typewriter 
8609 \SpecialChar ~
8610 \SpecialChar ~
8611 mov a,r4
8612 \layout Standard
8613
8614
8615 \family typewriter 
8616 \SpecialChar ~
8617 \SpecialChar ~
8618 add a,r2
8619 \layout Standard
8620
8621
8622 \family typewriter 
8623 \SpecialChar ~
8624 \SpecialChar ~
8625 mov r2,a
8626 \layout Standard
8627
8628
8629 \family typewriter 
8630 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
8631 \layout Standard
8632
8633
8634 \family typewriter 
8635 \SpecialChar ~
8636 \SpecialChar ~
8637 mov b,#0x03
8638 \layout Standard
8639
8640
8641 \family typewriter 
8642 \SpecialChar ~
8643 \SpecialChar ~
8644 mov a,r4
8645 \layout Standard
8646
8647
8648 \family typewriter 
8649 \SpecialChar ~
8650 \SpecialChar ~
8651 mul ab
8652 \layout Standard
8653
8654
8655 \family typewriter 
8656 \SpecialChar ~
8657 \SpecialChar ~
8658 mov r1,a
8659 \layout Standard
8660
8661
8662 \family typewriter 
8663 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] + 
8664 \layout Standard
8665
8666
8667 \family typewriter 
8668 ; iTemp15 [lr29:30]{short}[r1]
8669 \layout Standard
8670
8671
8672 \family typewriter 
8673 \SpecialChar ~
8674 \SpecialChar ~
8675 add a,r3
8676 \layout Standard
8677
8678
8679 \family typewriter 
8680 \SpecialChar ~
8681 \SpecialChar ~
8682 mov r3,a
8683 \layout Standard
8684
8685
8686 \family typewriter 
8687 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
8688 \layout Standard
8689
8690
8691 \family typewriter 
8692 \SpecialChar ~
8693 \SpecialChar ~
8694 mov a,r7
8695 \layout Standard
8696
8697
8698 \family typewriter 
8699 \SpecialChar ~
8700 \SpecialChar ~
8701 add a,#0xfd
8702 \layout Standard
8703
8704
8705 \family typewriter 
8706 \SpecialChar ~
8707 \SpecialChar ~
8708 mov r7,a
8709 \layout Standard
8710
8711
8712 \family typewriter 
8713 \SpecialChar ~
8714 \SpecialChar ~
8715 mov a,r0
8716 \layout Standard
8717
8718
8719 \family typewriter 
8720 \SpecialChar ~
8721 \SpecialChar ~
8722 addc a,#0xff
8723 \layout Standard
8724
8725
8726 \family typewriter 
8727 \SpecialChar ~
8728 \SpecialChar ~
8729 mov r0,a
8730 \layout Standard
8731
8732
8733 \family typewriter 
8734 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
8735 \layout Standard
8736
8737
8738 \family typewriter 
8739 \SpecialChar ~
8740 \SpecialChar ~
8741 mov a,r7
8742 \layout Standard
8743
8744
8745 \family typewriter 
8746 \SpecialChar ~
8747 \SpecialChar ~
8748 add a,_gint
8749 \layout Standard
8750
8751
8752 \family typewriter 
8753 \SpecialChar ~
8754 \SpecialChar ~
8755 mov _gint,a
8756 \layout Standard
8757
8758
8759 \family typewriter 
8760 \SpecialChar ~
8761 \SpecialChar ~
8762 mov a,r0
8763 \layout Standard
8764
8765
8766 \family typewriter 
8767 \SpecialChar ~
8768 \SpecialChar ~
8769 addc a,(_gint + 1)
8770 \layout Standard
8771
8772
8773 \family typewriter 
8774 \SpecialChar ~
8775 \SpecialChar ~
8776 mov (_gint + 1),a
8777 \layout Standard
8778
8779
8780 \family typewriter 
8781 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
8782 \layout Standard
8783
8784
8785 \family typewriter 
8786 \SpecialChar ~
8787 \SpecialChar ~
8788 inc r4
8789 \layout Standard
8790
8791
8792 \family typewriter 
8793 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
8794 \layout Standard
8795
8796
8797 \family typewriter 
8798 \SpecialChar ~
8799 \SpecialChar ~
8800 dec r5
8801 \layout Standard
8802
8803
8804 \family typewriter 
8805 \SpecialChar ~
8806 \SpecialChar ~
8807 cjne r5,#0xff,00104$
8808 \layout Standard
8809
8810
8811 \family typewriter 
8812 \SpecialChar ~
8813 \SpecialChar ~
8814 dec r6
8815 \layout Standard
8816
8817
8818 \family typewriter 
8819 ; goto _forcond_0($4)
8820 \layout Standard
8821
8822
8823 \family typewriter 
8824 \SpecialChar ~
8825 \SpecialChar ~
8826 sjmp 00104$
8827 \layout Standard
8828
8829
8830 \family typewriter 
8831 ; _forbreak_0($7) :
8832 \layout Standard
8833
8834
8835 \family typewriter 
8836 00107$:
8837 \layout Standard
8838
8839
8840 \family typewriter 
8841 ; ret iTemp24 [lr40:41]{short}
8842 \layout Standard
8843
8844
8845 \family typewriter 
8846 \SpecialChar ~
8847 \SpecialChar ~
8848 mov a,r3
8849 \layout Standard
8850
8851
8852 \family typewriter 
8853 \SpecialChar ~
8854 \SpecialChar ~
8855 add a,r2
8856 \layout Standard
8857
8858
8859 \family typewriter 
8860 \SpecialChar ~
8861 \SpecialChar ~
8862 mov dpl,a
8863 \layout Standard
8864
8865
8866 \family typewriter 
8867 ; _return($8) :
8868 \layout Standard
8869
8870
8871 \family typewriter 
8872 00108$:
8873 \layout Standard
8874
8875
8876 \family typewriter 
8877 \SpecialChar ~
8878 \SpecialChar ~
8879 ret
8880 \family default 
8881
8882 \newline 
8883
8884 \layout Section
8885
8886 Acknowledgments
8887 \layout Standard
8888
8889
8890 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
8891
8892 \end_inset 
8893
8894
8895 \newline 
8896
8897 \newline 
8898
8899 \emph on 
8900 Thanks to all the other volunteer developers who have helped with coding,
8901  testing, web-page creation, distribution sets, etc.
8902  You know who you are :-)
8903 \emph default 
8904
8905 \newline 
8906
8907 \layout Standard
8908
8909 This document was initially written by Sandeep Dutta
8910 \layout Standard
8911
8912 All product names mentioned herein may be trademarks of their respective
8913  companies.
8914  
8915 \layout Standard
8916
8917
8918 \begin_inset LatexCommand \printindex{}
8919
8920 \end_inset 
8921
8922
8923 \the_end