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