OSDN Git Service

gcc:
[pf3gnuchains/gcc-fork.git] / gcc / treelang / treelang.texi
1 \input texinfo  @c -*-texinfo-*-
2
3 @c NOTE THIS IS NOT A GOOD EXAMPLE OF HOW TO DO A MANUAL. FIXME!!!
4 @c NOTE THIS IS NOT A GOOD EXAMPLE OF HOW TO DO A MANUAL. FIXME!!!
5
6
7 @c %**start of header
8 @setfilename treelang.info
9
10 @include gcc-common.texi
11
12 @set copyrights-treelang 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005
13
14 @set email-general gcc@@gcc.gnu.org
15 @set email-bugs gcc-bugs@@gcc.gnu.org or bug-gcc@@gnu.org
16 @set email-patches gcc-patches@@gcc.gnu.org 
17 @set path-treelang gcc/gcc/treelang
18
19 @ifset VERSION_PACKAGE
20 @set which-treelang @value{version-GCC} @value{VERSION_PACKAGE}
21 @end ifset
22 @ifclear VERSION_PACKAGE
23 @set which-treelang @value{version-GCC}
24 @end ifclear
25 @set which-GCC GCC
26
27 @set email-josling tej@@melbpc.org.au
28 @set www-josling http://www.geocities.com/timjosling
29
30 @c This tells @include'd files that they're part of the overall TREELANG doc
31 @c set.  (They might be part of a higher-level doc set too.)
32 @set DOC-TREELANG
33
34 @c @setfilename usetreelang.info
35 @c @setfilename maintaintreelang.info
36 @c To produce the full manual, use the "treelang.info" setfilename, and
37 @c make sure the following do NOT begin with '@c' (and the @clear lines DO)
38 @set INTERNALS
39 @set USING
40 @c To produce a user-only manual, use the "usetreelang.info" setfilename, and
41 @c make sure the following does NOT begin with '@c':
42 @c @clear INTERNALS
43 @c To produce a maintainer-only manual, use the "maintaintreelang.info" setfilename,
44 @c and make sure the following does NOT begin with '@c':
45 @c @clear USING
46
47 @ifset INTERNALS
48 @ifset USING
49 @settitle Using and Maintaining GNU Treelang
50 @end ifset
51 @end ifset
52 @c seems reasonable to assume at least one of INTERNALS or USING is set...
53 @ifclear INTERNALS
54 @settitle Using GNU Treelang
55 @end ifclear
56 @ifclear USING
57 @settitle Maintaining GNU Treelang
58 @end ifclear
59 @c then again, have some fun
60 @ifclear INTERNALS
61 @ifclear USING
62 @settitle Doing Very Little at all with GNU Treelang
63 @end ifclear
64 @end ifclear
65
66 @syncodeindex fn cp
67 @syncodeindex vr cp
68 @c %**end of header
69
70 @c Cause even numbered pages to be printed on the left hand side of
71 @c the page and odd numbered pages to be printed on the right hand
72 @c side of the page.  Using this, you can print on both sides of a
73 @c sheet of paper and have the text on the same part of the sheet.
74
75 @c The text on right hand pages is pushed towards the right hand
76 @c margin and the text on left hand pages is pushed toward the left
77 @c hand margin.
78 @c (To provide the reverse effect, set bindingoffset to -0.75in.)
79
80 @c @tex
81 @c \global\bindingoffset=0.75in
82 @c \global\normaloffset =0.75in
83 @c @end tex
84
85 @copying
86 Copyright @copyright{} @value{copyrights-treelang} Free Software Foundation, Inc.
87
88 Permission is granted to copy, distribute and/or modify this document
89 under the terms of the GNU Free Documentation License, Version 1.2 or
90 any later version published by the Free Software Foundation; with the
91 Invariant Sections being ``GNU General Public License'', the Front-Cover
92 texts being (a) (see below), and with the Back-Cover Texts being (b)
93 (see below).  A copy of the license is included in the section entitled
94 ``GNU Free Documentation License''.
95
96 (a) The FSF's Front-Cover Text is:
97
98      A GNU Manual
99
100 (b) The FSF's Back-Cover Text is:
101
102      You have freedom to copy and modify this GNU Manual, like GNU
103      software.  Copies published by the Free Software Foundation raise
104      funds for GNU development.
105 @end copying
106
107 @ifnottex
108 @dircategory Software development
109 @direntry
110 * treelang: (treelang).                  The GNU Treelang compiler.
111 @end direntry
112 @ifset INTERNALS
113 @ifset USING
114 This file documents the use and the internals of the GNU Treelang
115 (@code{treelang}) compiler.  At the moment this manual is not
116 incorporated into the main GCC manual as it is incomplete.  It
117 corresponds to the @value{which-treelang} version of @code{treelang}.
118 @end ifset
119 @end ifset
120 @ifclear USING
121 This file documents the internals of the GNU Treelang (@code{treelang}) compiler.
122 It corresponds to the @value{which-treelang} version of @code{treelang}.
123 @end ifclear
124 @ifclear INTERNALS
125 This file documents the use of the GNU Treelang (@code{treelang}) compiler.
126 It corresponds to the @value{which-treelang} version of @code{treelang}.
127 @end ifclear
128
129 Published by the Free Software Foundation
130 51 Franklin Street, Fifth Floor
131 Boston, MA 02110-1301 USA
132
133 @insertcopying
134 @end ifnottex
135
136 @setchapternewpage odd
137 @c @finalout
138 @titlepage
139 @ifset INTERNALS
140 @ifset USING
141 @title Using and Maintaining GNU Treelang
142 @end ifset
143 @end ifset
144 @ifclear INTERNALS
145 @title Using GNU Treelang
146 @end ifclear
147 @ifclear USING
148 @title Maintaining GNU Treelang
149 @end ifclear
150 @versionsubtitle
151 @author Tim Josling
152 @page
153 @vskip 0pt plus 1filll
154 Published by the Free Software Foundation @*
155 51 Franklin Street, Fifth Floor@*
156 Boston, MA 02110-1301, USA@*
157 @c Last printed ??ber, 19??.@*
158 @c Printed copies are available for $? each.@*
159 @c ISBN ???
160 @sp 1
161 @insertcopying
162 @end titlepage
163 @page
164
165 @ifnottex
166
167 @node Top, Copying,, (dir)
168 @top Introduction
169 @cindex Introduction
170
171 @ifset INTERNALS
172 @ifset USING
173 This manual documents how to run, install and maintain @code{treelang}.
174 It also documents the features and incompatibilities in the @value{which-treelang}
175 version of @code{treelang}.
176 @end ifset
177 @end ifset
178
179 @ifclear INTERNALS
180 This manual documents how to run and install @code{treelang}.
181 It also documents the features and incompatibilities in the @value{which-treelang}
182 version of @code{treelang}.
183 @end ifclear
184 @ifclear USING
185 This manual documents how to maintain @code{treelang}.
186 It also documents the features and incompatibilities in the @value{which-treelang}
187 version of @code{treelang}.
188 @end ifclear
189
190 @end ifnottex
191
192 @menu
193 * Copying::                     
194 * Contributors::                
195 * GNU Free Documentation License::                
196 * Funding::                     
197 * Getting Started::             
198 * What is GNU Treelang?::       
199 * Lexical Syntax::              
200 * Parsing Syntax::              
201 * Compiler Overview::           
202 * TREELANG and GCC::            
203 * Compiler::                    
204 * Other Languages::             
205 * treelang internals::          
206 * Open Questions::              
207 * Bugs::                        
208 * Service::                     
209 * Projects::                    
210 * Index::                       
211
212 @detailmenu
213  --- The Detailed Node Listing ---
214
215 Other Languages
216
217 * Interoperating with C and C++::  
218
219 treelang internals
220
221 * treelang files::              
222 * treelang compiler interfaces::  
223 * Hints and tips::              
224
225 treelang compiler interfaces
226
227 * treelang driver::             
228 * treelang main compiler::      
229
230 treelang main compiler
231
232 * Interfacing to toplev.c::     
233 * Interfacing to the garbage collection::  
234 * Interfacing to the code generation code. ::  
235
236 Reporting Bugs
237
238 * Sending Patches::             
239
240 @end detailmenu
241 @end menu
242
243 @include gpl.texi
244
245 @include fdl.texi
246
247 @node Contributors
248
249 @unnumbered Contributors to GNU Treelang
250 @cindex contributors
251 @cindex credits
252
253 Treelang was based on 'toy' by Richard Kenner, and also uses code from
254 the GCC core code tree.  Tim Josling first created the language and
255 documentation, based on the GCC Fortran compiler's documentation
256 framework.  Treelang was updated to use the TreeSSA infrastructure by
257 James A. Morrison.
258
259 @itemize @bullet
260 @item
261 The packaging and compiler portions of GNU Treelang are based largely
262 on the GCC compiler.
263 @xref{Contributors,,Contributors to GCC,GCC,Using and Maintaining GCC},
264 for more information.
265
266 @item
267 There is no specific run-time library for treelang, other than the
268 standard C runtime.
269
270 @item
271 It would have been difficult to build treelang without access to Joachim
272 Nadler's guide to writing a front end to GCC (written in German).  A
273 translation of this document into English is available via the
274 CobolForGCC project or via the documentation links from the GCC home
275 page @uref{http://gcc.gnu.org}.
276 @end itemize
277
278 @include funding.texi
279
280 @node Getting Started
281 @chapter Getting Started
282 @cindex getting started
283 @cindex new users
284 @cindex newbies
285 @cindex beginners
286
287 Treelang is a sample language, useful only to help people understand how
288 to implement a new language front end to GCC.  It is not a useful
289 language in itself other than as an example or basis for building a new
290 language.  Therefore only language developers are likely to have an
291 interest in it.
292
293 This manual assumes familiarity with GCC, which you can obtain by using
294 it and by reading the manuals @samp{Using the GNU Compiler Collection (GCC)}
295 and @samp{GNU Compiler Collection (GCC) Internals}.
296
297 To install treelang, follow the GCC installation instructions, 
298 taking care to ensure you specify treelang in the configure step by adding
299 treelang to the list of languages specified by @option{--enable-languages}, 
300 e.g.@: @samp{--enable-languages=all,treelang}.
301
302 If you're generally curious about the future of
303 @code{treelang}, see @ref{Projects}.
304 If you're curious about its past,
305 see @ref{Contributors}.
306
307 To see a few of the questions maintainers of @code{treelang} have,
308 and that you might be able to answer,
309 see @ref{Open Questions}.
310
311 @ifset USING
312 @node What is GNU Treelang?, Lexical Syntax, Getting Started, Top
313 @chapter What is GNU Treelang?
314 @cindex concepts, basic
315 @cindex basic concepts
316
317 GNU Treelang, or @code{treelang}, is designed initially as a free
318 replacement for, or alternative to, the 'toy' language, but which is
319 amenable to inclusion within the GCC source tree. 
320
321 @code{treelang} is largely a cut down version of C, designed to showcase
322 the features of the GCC code generation back end.  Only those features
323 that are directly supported by the GCC code generation back end are
324 implemented.  Features are implemented in a manner which is easiest and
325 clearest to implement.  Not all or even most code generation back end
326 features are implemented.  The intention is to add features incrementally
327 until most features of the GCC back end are implemented in treelang.
328
329 The main features missing are structures, arrays and pointers.
330
331 A sample program follows:
332
333 @smallexample
334 // @r{function prototypes}
335 // @r{function 'add' taking two ints and returning an int}
336 external_definition int add(int arg1, int arg2);
337 external_definition int subtract(int arg3, int arg4);
338 external_definition int first_nonzero(int arg5, int arg6);
339 external_definition int double_plus_one(int arg7);
340
341 // @r{function definition}
342 add 
343 @{
344   // @r{return the sum of arg1 and arg2}
345   return arg1 + arg2;
346 @}
347
348         
349 subtract 
350 @{
351   return arg3 - arg4;
352 @}
353
354 double_plus_one
355 @{
356   // @r{aaa is a variable, of type integer and allocated at the start of}
357   // @r{the function}
358   automatic int aaa;
359   // @r{set aaa to the value returned from add, when passed arg7 and arg7 as}
360   // @r{the two parameters}
361   aaa=add(arg7, arg7);
362   aaa=add(aaa, aaa);
363   aaa=subtract(subtract(aaa, arg7), arg7) + 1;
364   return aaa;
365 @}
366
367 first_nonzero
368 @{
369   // @r{C-like if statement}
370   if (arg5)
371     @{
372       return arg5;
373     @}
374   else
375     @{
376     @}
377   return arg6;
378 @}
379 @end smallexample
380
381 @node Lexical Syntax, Parsing Syntax, What is GNU Treelang?, Top
382 @chapter Lexical Syntax
383 @cindex Lexical Syntax
384
385 Treelang programs consist of whitespace, comments, keywords and names.
386 @itemize @bullet
387
388 @item
389 Whitespace consists of the space character, a tab, and the end of line
390 character.  Line terminations are as defined by the
391 standard C library.  Whitespace is ignored except within comments,
392 and where it separates parts of the program.  In the example below, A and
393 B are two separate names separated by whitespace. 
394
395 @smallexample
396 A B
397 @end smallexample
398
399 @item
400 Comments consist of @samp{//} followed by any characters up to the end
401 of the line.  C style comments (/* */) are not supported.  For example,
402 the assignment below is followed by a not very helpful comment.
403
404 @smallexample
405 x = 1; // @r{Set X to 1}
406 @end smallexample
407
408 @item
409 Keywords consist of any of the following reserved words or symbols:
410
411 @itemize @bullet
412 @item @{
413 used to start the statements in a function
414 @item @}
415 used to end the statements in a function
416 @item (
417 start list of function arguments, or to change the precedence of operators in
418 an expression
419 @item )
420 end list or prioritized operators in expression
421 @item ,
422 used to separate parameters in a function prototype or in a function call 
423 @item ;
424 used to end a statement
425 @item +
426 addition, or unary plus for signed literals
427 @item - 
428 subtraction, or unary minus for signed literals
429 @item = 
430 assignment
431 @item ==
432 equality test
433 @item if
434 begin IF statement
435 @item else
436 begin 'else' portion of IF statement
437 @item static
438 indicate variable is permanent, or function has file scope only
439 @item automatic
440 indicate that variable is allocated for the life of the current scope
441 @item external_reference
442 indicate that variable or function is defined in another file
443 @item external_definition
444 indicate that variable or function is to be accessible from other files
445 @item int
446 variable is an integer (same as C int) 
447 @item char
448 variable is a character (same as C char)
449 @item unsigned
450 variable is unsigned. If this is not present, the variable is signed
451 @item return
452 start function return statement
453 @item void
454 used as function type to indicate function returns nothing
455 @end itemize
456
457
458 @item
459 Names consist of any letter or "_" followed by any number of letters,
460 numbers, or "_".  "$" is not allowed in a name.  All names must be globally
461 unique, i.e. may not be used twice in any context, and must
462 not be a keyword.  Names and keywords are case sensitive.  For example:
463
464 @smallexample
465 a A _a a_ IF_X
466 @end smallexample
467
468 are all different names.
469
470 @end itemize
471
472 @node Parsing Syntax, Compiler Overview, Lexical Syntax, Top
473 @chapter Parsing Syntax
474 @cindex Parsing Syntax
475
476 Declarations are built up from the lexical elements described above.  A
477 file may contain one of more declarations.
478
479 @itemize @bullet
480
481 @item
482 declaration: variable declaration OR function prototype OR function declaration
483
484 @item
485 Function Prototype: storage type NAME ( optional_parameter_list )
486
487 @smallexample
488 static int add (int a, int b)
489 @end smallexample
490
491 @item
492 variable_declaration: storage type NAME initial;
493
494 Example:
495
496 @smallexample
497 int temp1 = 1;
498 @end smallexample
499
500 A variable declaration can be outside a function, or at the start of a
501 function.
502
503 @item
504 storage: automatic OR static OR external_reference OR external_definition
505
506 This defines the scope, duration and visibility of a function or variable
507
508 @enumerate 1
509
510 @item 
511 automatic: This means a variable is allocated at start of the current scope and
512 released when the current scope is exited.  This can only be used for variables
513 within functions.  It cannot be used for functions.
514
515 @item 
516 static: This means a variable is allocated at start of program and
517 remains allocated until the program as a whole ends.  For a function, it
518 means that the function is only visible within the current file.
519
520 @item
521 external_definition: For a variable, which must be defined outside a
522 function, it means that the variable is visible from other files.  For a
523 function, it means that the function is visible from another file.
524
525 @item
526 external_reference: For a variable, which must be defined outside a
527 function, it means that the variable is defined in another file.  For a
528 function, it means that the function is defined in another file.
529
530 @end enumerate
531
532 @item
533 type: int OR unsigned int OR char OR unsigned char OR void
534
535 This defines the data type of a variable or the return type of a function.
536
537 @enumerate a
538
539 @item 
540 int: The variable is a signed integer.  The function returns a signed integer.
541
542 @item 
543 unsigned int: The variable is an unsigned integer.  The function returns an unsigned integer.
544
545 @item 
546 char: The variable is a signed character.  The function returns a signed character.
547
548 @item 
549 unsigned char: The variable is an unsigned character.  The function returns an unsigned character.
550
551 @end enumerate
552
553 @item
554 parameter_list OR parameter [, parameter]...
555
556 @item
557 parameter: variable_declaration ,
558
559 The variable declarations must not have initializations.
560
561 @item 
562 initial: = value
563
564 @item
565 value: integer_constant
566
567 Values without a unary plus or minus are considered to be unsigned.
568 @smallexample
569 e.g.@: 1 +2 -3
570 @end smallexample
571
572 @item
573 function_declaration: name @{ variable_declarations statements @}
574
575 A function consists of the function name then the declarations (if any)
576 and statements (if any) within one pair of braces.
577
578 The details of the function arguments come from the function
579 prototype.  The function prototype must precede the function declaration
580 in the file.
581
582 @item
583 statement: if_statement OR expression_statement OR return_statement
584
585 @item
586 if_statement: if ( expression ) @{ variable_declarations statements @}
587 else @{ variable_declarations statements @}
588
589 The first lot of statements is executed if the expression is
590 nonzero.  Otherwise the second lot of statements is executed.  Either
591 list of statements may be empty, but both sets of braces and the else must be present. 
592
593 @smallexample
594 if (a==b) 
595 @{
596 // @r{nothing}
597 @}
598 else
599 @{
600 a=b;
601 @}
602 @end smallexample
603
604 @item
605 expression_statement: expression;
606
607 The expression is executed, including any side effects.
608
609 @item
610 return_statement: return expression_opt;
611
612 Returns from the function. If the function is void, the expression must
613 be absent, and if the function is not void the expression must be
614 present.
615
616 @item
617 expression: variable OR integer_constant OR expression + expression
618 OR expression - expression OR expression == expression OR ( expression )
619 OR variable = expression OR function_call
620
621 An expression can be a constant or a variable reference or a
622 function_call.  Expressions can be combined as a sum of two expressions
623 or the difference of two expressions, or an equality test of two
624 expressions.  An assignment is also an expression.  Expressions and operator
625 precedence work as in C.
626
627 @item
628 function_call: function_name ( optional_comma_separated_expressions )
629
630 This invokes the function, passing to it the values of the expressions
631 as actual parameters.
632
633 @end itemize
634
635 @cindex compilers
636 @node Compiler Overview, TREELANG and GCC, Parsing Syntax, Top
637 @chapter Compiler Overview
638 treelang is run as part of the GCC compiler. 
639
640 @itemize @bullet
641 @cindex source code
642 @cindex file, source
643 @cindex code, source
644 @cindex source file
645 @item
646 It reads a user's program, stored in a file and containing instructions
647 written in the appropriate language (Treelang, C, and so on).  This file
648 contains @dfn{source code}.
649
650 @cindex translation of user programs
651 @cindex machine code
652 @cindex code, machine
653 @cindex mistakes
654 @item
655 It translates the user's program into instructions a computer can carry
656 out more quickly than it takes to translate the instructions in the
657 first place.  These instructions are called @dfn{machine code}---code
658 designed to be efficiently translated and processed by a machine such as
659 a computer.  Humans usually aren't as good writing machine code as they
660 are at writing Treelang or C, because it is easy to make tiny mistakes
661 writing machine code.  When writing Treelang or C, it is easy to make
662 big mistakes. But you can only make one mistake, because the compiler
663 stops after it finds any problem.
664
665 @cindex debugger
666 @cindex bugs, finding
667 @cindex @code{gdb}, command
668 @cindex commands, @code{gdb}
669 @item
670 It provides information in the generated machine code
671 that can make it easier to find bugs in the program
672 (using a debugging tool, called a @dfn{debugger},
673 such as @code{gdb}).
674
675 @cindex libraries
676 @cindex linking
677 @cindex @code{ld} command
678 @cindex commands, @code{ld}
679 @item
680 It locates and gathers machine code already generated to perform actions
681 requested by statements in the user's program.  This machine code is
682 organized into @dfn{libraries} and is located and gathered during the
683 @dfn{link} phase of the compilation process.  (Linking often is thought
684 of as a separate step, because it can be directly invoked via the
685 @code{ld} command.  However, the @code{gcc} command, as with most
686 compiler commands, automatically performs the linking step by calling on
687 @code{ld} directly, unless asked to not do so by the user.)
688
689 @cindex language, incorrect use of
690 @cindex incorrect use of language
691 @item
692 It attempts to diagnose cases where the user's program contains
693 incorrect usages of the language.  The @dfn{diagnostics} produced by the
694 compiler indicate the problem and the location in the user's source file
695 where the problem was first noticed.  The user can use this information
696 to locate and fix the problem.
697
698 The compiler stops after the first error.  There are no plans to fix
699 this, ever, as it would vastly complicate the implementation of treelang
700 to little or no benefit.
701
702 @cindex diagnostics, incorrect
703 @cindex incorrect diagnostics
704 @cindex error messages, incorrect
705 @cindex incorrect error messages
706 (Sometimes an incorrect usage of the language leads to a situation where
707 the compiler can not make any sense of what it reads---while a human
708 might be able to---and thus ends up complaining about an incorrect
709 ``problem'' it encounters that, in fact, reflects a misunderstanding of
710 the programmer's intention.)
711
712 @cindex warnings
713 @cindex questionable instructions
714 @item
715 There are a few warnings in treelang.  For example an unused static function
716 generate a warnings when -Wunused-function is specified, similarly an unused
717 static variable generates a warning when -Wunused-variable are specified.
718 The only treelang specific warning is a warning when an expression is in a
719 return statement for functions that return void.
720 @end itemize
721
722 @cindex components of treelang
723 @cindex @code{treelang}, components of
724 @code{treelang} consists of several components:
725
726 @cindex @code{gcc}, command
727 @cindex commands, @code{gcc}
728 @itemize @bullet
729 @item
730 A modified version of the @code{gcc} command, which also might be
731 installed as the system's @code{cc} command.
732 (In many cases, @code{cc} refers to the
733 system's ``native'' C compiler, which
734 might be a non-GNU compiler, or an older version
735 of @code{GCC} considered more stable or that is
736 used to build the operating system kernel.)
737
738 @cindex @code{treelang}, command
739 @cindex commands, @code{treelang}
740 @item
741 The @code{treelang} command itself.
742
743 @item
744 The @code{libc} run-time library.  This library contains the machine
745 code needed to support capabilities of the Treelang language that are
746 not directly provided by the machine code generated by the
747 @code{treelang} compilation phase.  This is the same library that the
748 main C compiler uses (libc).
749
750 @cindex @code{tree1}, program
751 @cindex programs, @code{tree1}
752 @cindex assembler
753 @cindex @code{as} command
754 @cindex commands, @code{as}
755 @cindex assembly code
756 @cindex code, assembly
757 @item
758 The compiler itself, is internally named @code{tree1}.
759
760 Note that @code{tree1} does not generate machine code directly---it
761 generates @dfn{assembly code} that is a more readable form
762 of machine code, leaving the conversion to actual machine code
763 to an @dfn{assembler}, usually named @code{as}.
764 @end itemize
765
766 @code{GCC} is often thought of as ``the C compiler'' only,
767 but it does more than that.
768 Based on command-line options and the names given for files
769 on the command line, @code{gcc} determines which actions to perform, including
770 preprocessing, compiling (in a variety of possible languages), assembling,
771 and linking.
772
773 @cindex driver, gcc command as
774 @cindex @code{gcc}, command as driver
775 @cindex executable file
776 @cindex files, executable
777 @cindex cc1 program
778 @cindex programs, cc1
779 @cindex preprocessor
780 @cindex cpp program
781 @cindex programs, cpp
782 For example, the command @samp{gcc foo.c} @dfn{drives} the file
783 @file{foo.c} through the preprocessor @code{cpp}, then
784 the C compiler (internally named
785 @code{cc1}), then the assembler (usually @code{as}), then the linker
786 (@code{ld}), producing an executable program named @file{a.out} (on
787 UNIX systems).
788
789 @cindex treelang program
790 @cindex programs, treelang
791 As another example, the command @samp{gcc foo.tree} would do much the
792 same as @samp{gcc foo.c}, but instead of using the C compiler named
793 @code{cc1}, @code{gcc} would use the treelang compiler (named
794 @code{tree1}). However there is no preprocessor for treelang.
795
796 @cindex @code{tree1}, program
797 @cindex programs, @code{tree1}
798 In a GNU Treelang installation, @code{gcc} recognizes Treelang source
799 files by name just like it does C and C++ source files.  It knows to use
800 the Treelang compiler named @code{tree1}, instead of @code{cc1} or
801 @code{cc1plus}, to compile Treelang files.  If a file's name ends in
802 @code{.tree} then GCC knows that the program is written in treelang.  You
803 can also manually override the language.
804
805 @cindex @code{gcc}, not recognizing Treelang source
806 @cindex unrecognized file format
807 @cindex file format not recognized
808 Non-Treelang-related operation of @code{gcc} is generally
809 unaffected by installing the GNU Treelang version of @code{gcc}.
810 However, without the installed version of @code{gcc} being the
811 GNU Treelang version, @code{gcc} will not be able to compile
812 and link Treelang programs.
813
814 @cindex printing version information
815 @cindex version information, printing
816 The command @samp{gcc -v x.tree} where @samp{x.tree} is a file which
817 must exist but whose contents are ignored, is a quick way to display
818 version information for the various programs used to compile a typical
819 Treelang source file. 
820
821 The @code{tree1} program represents most of what is unique to GNU
822 Treelang; @code{tree1} is a combination of two rather large chunks of
823 code.
824
825 @cindex GCC Back End (GBE)
826 @cindex GBE
827 @cindex @code{GCC}, back end
828 @cindex back end, GCC
829 @cindex code generator
830 One chunk is the so-called @dfn{GNU Back End}, or GBE,
831 which knows how to generate fast code for a wide variety of processors.
832 The same GBE is used by the C, C++, and Treelang compiler programs @code{cc1},
833 @code{cc1plus}, and @code{tree1}, plus others.
834 Often the GBE is referred to as the ``GCC back end'' or
835 even just ``GCC''---in this manual, the term GBE is used
836 whenever the distinction is important.
837
838 @cindex GNU Treelang Front End (TFE)
839 @cindex tree1
840 @cindex @code{treelang}, front end
841 @cindex front end, @code{treelang}
842 The other chunk of @code{tree1} is the majority of what is unique about
843 GNU Treelang---the code that knows how to interpret Treelang programs to
844 determine what they are intending to do, and then communicate that
845 knowledge to the GBE for actual compilation of those programs.  This
846 chunk is called the @dfn{Treelang Front End} (TFE).  The @code{cc1} and
847 @code{cc1plus} programs have their own front ends, for the C and C++
848 languages, respectively.  These fronts ends are responsible for
849 diagnosing incorrect usage of their respective languages by the programs
850 the process, and are responsible for most of the warnings about
851 questionable constructs as well.  (The GBE in principle handles
852 producing some warnings, like those concerning possible references to
853 undefined variables, but these warnings should not occur in treelang
854 programs as the front end is meant to pick them up first).
855
856 Because so much is shared among the compilers for various languages,
857 much of the behavior and many of the user-selectable options for these
858 compilers are similar.
859 For example, diagnostics (error messages and
860 warnings) are similar in appearance; command-line
861 options like @samp{-Wall} have generally similar effects; and the quality
862 of generated code (in terms of speed and size) is roughly similar
863 (since that work is done by the shared GBE).
864
865 @node TREELANG and GCC, Compiler, Compiler Overview, Top
866 @chapter Compile Treelang, C, or Other Programs
867 @cindex compiling programs
868 @cindex programs, compiling
869
870 @cindex @code{gcc}, command
871 @cindex commands, @code{gcc}
872 A GNU Treelang installation includes a modified version of the @code{gcc}
873 command.
874
875 In a non-Treelang installation, @code{gcc} recognizes C, C++,
876 and Objective-C source files.
877
878 In a GNU Treelang installation, @code{gcc} also recognizes Treelang source
879 files and accepts Treelang-specific command-line options, plus some
880 command-line options that are designed to cater to Treelang users
881 but apply to other languages as well.
882
883 @xref{G++ and GCC,,Programming Languages Supported by GCC,GCC,Using
884 the GNU Compiler Collection (GCC)},
885 for information on the way different languages are handled
886 by the GCC compiler (@code{gcc}).
887
888 You can use this, combined with the output of the @samp{gcc -v x.tree}
889 command to get the options applicable to treelang.  Treelang programs
890 must end with the suffix @samp{.tree}.
891
892 @cindex preprocessor
893
894 Treelang programs are not by default run through the C
895 preprocessor by @code{gcc}. There is no reason why they cannot be run through the
896 preprocessor manually, but you would need to prevent the preprocessor
897 from generating #line directives, using the @samp{-P} option, otherwise
898 tree1 will not accept the input.
899
900 @node Compiler, Other Languages, TREELANG and GCC, Top
901 @chapter The GNU Treelang Compiler
902
903 The GNU Treelang compiler, @code{treelang}, supports programs written
904 in the GNU Treelang language.
905
906 @node Other Languages, treelang internals, Compiler, Top
907 @chapter Other Languages
908
909 @menu
910 * Interoperating with C and C++::  
911 @end menu
912
913 @node Interoperating with C and C++,  , Other Languages, Other Languages
914 @section Tools and advice for interoperating with C and C++
915
916 The output of treelang programs looks like C program code to the linker
917 and everybody else, so you should be able to freely mix treelang and C
918 (and C++) code, with one proviso.
919
920 C promotes small integer types to 'int' when used as function parameters and 
921 return values in non-prototyped functions.  Since treelang has no
922 non-prototyped functions, the treelang compiler does not do this.
923
924 @ifset INTERNALS
925 @node treelang internals, Open Questions, Other Languages, Top
926 @chapter treelang internals
927
928 @menu
929 * treelang files::              
930 * treelang compiler interfaces::  
931 * Hints and tips::              
932 @end menu
933
934 @node treelang files, treelang compiler interfaces, treelang internals, treelang internals
935 @section treelang files
936
937 To create a compiler that integrates into GCC, you need create many
938 files.  Some of the files are integrated into the main GCC makefile, to
939 build the various parts of the compiler and to run the test
940 suite.  Others are incorporated into various GCC programs such as
941 @file{gcc.c}.  Finally you must provide the actual programs comprising your
942 compiler. 
943
944 @cindex files
945
946 The files are:
947
948 @enumerate 1
949
950 @item
951 COPYING.  This is the copyright file, assuming you are going to use the
952 GNU General Public License.  You probably need to use the GPL because if
953 you use the GCC back end your program and the back end are one program,
954 and the back end is GPLed.
955
956 This need not be present if the language is incorporated into the main
957 GCC tree, as the main GCC directory has this file. 
958
959 @item
960 COPYING.LIB.  This is the copyright file for those parts of your program
961 that are not to be covered by the GPL, but are instead to be covered by
962 the LGPL (Library or Lesser GPL).  This license may be appropriate for
963 the library routines associated with your compiler. These are the
964 routines that are linked with the @emph{output} of the compiler.  Using
965 the LGPL for these programs allows programs written using your compiler
966 to be closed source. For example LIBC is under the LGPL. 
967
968 This need not be present if the language is incorporated into the main
969 GCC tree, as the main GCC directory has this file. 
970
971 @item
972 ChangeLog.  Record all the changes to your compiler.  Use the same format
973 as used in treelang as it is supported by an emacs editing mode and is
974 part of the FSF coding standard.  Normally each directory has its own
975 changelog.  The FSF standard allows but does not require a meaningful
976 comment on why the changes were made, above and beyond @emph{why} they
977 were made.  In the author's opinion it is useful to provide this
978 information.
979
980 @item
981 treelang.texi.  The manual, written in texinfo. Your manual would have a
982 different file name.  You need not write it in texinfo if you don't want
983 do, but a lot of GNU software does use texinfo. 
984
985 @cindex Make-lang.in
986 @item
987 Make-lang.in.  This file is part of the make file which in incorporated
988 with the GCC make file skeleton (Makefile.in in the GCC directory) to
989 make Makefile, as part of the configuration process.
990
991 Makefile in turn is the main instruction to actually build
992 everything.  The build instructions are held in the main GCC manual and
993 web site so they are not repeated here. 
994
995 There are some comments at the top which will help you understand what
996 you need to do.
997
998 There are make commands to build things, remove generated files with
999 various degrees of thoroughness, count the lines of code (so you know
1000 how much progress you are making), build info and html files from the
1001 texinfo source, run the tests etc.
1002
1003 @item
1004 README.  Just a brief informative text file saying what is in this
1005 directory. 
1006
1007 @cindex config-lang.in
1008 @item
1009 config-lang.in.  This file is read by the configuration progress and must
1010 be present. You specify the name of your language, the name(s) of the
1011 compiler(s) including preprocessors you are going to build, whether any,
1012 usually generated, files should be excluded from diffs (ie when making
1013 diff files to send in patches).  Whether the equate 'stagestuff' is used
1014 is unknown (???).
1015
1016 @cindex lang.opt
1017 @item
1018 lang.opt.  This file is included into @file{gcc.c}, the main GCC driver, and
1019 tells it what options your language supports.  This is also used to
1020 display help.
1021
1022 @cindex lang-specs.h
1023 @item
1024 lang-specs.h.  This file is also included in @file{gcc.c}. It tells
1025 @file{gcc.c} when to call your programs and what options to send them.  The
1026 mini-language 'specs' is documented in the source of @file{gcc.c}.  Do not
1027 attempt to write a specs file from scratch - use an existing one as the base
1028 and enhance it. 
1029
1030 @item
1031 Your texi files.  Texinfo can be used to build documentation in HTML,
1032 info, dvi and postscript formats. It is a tagged language, is documented
1033 in its own manual, and has its own emacs mode.
1034
1035 @item
1036 Your programs.  The relationships between all the programs are explained
1037 in the next section.  You need to write or use the following programs:
1038
1039 @itemize @bullet
1040
1041 @item
1042 lexer.  This breaks the input into words and passes these to the
1043 parser.  This is @file{lex.l} in treelang, which is passed through flex, a lex
1044 variant, to produce C code @file{lex.c}.  Note there is a school of thought
1045 that says real men hand code their own lexers.  However, you may prefer to
1046 write far less code and use flex, as was done with treelang.
1047
1048 @item
1049 parser.  This breaks the program into recognizable constructs such as
1050 expressions, statements etc.  This is @file{parse.y} in treelang, which is
1051 passed through bison, which is a yacc variant, to produce C code
1052 @file{parse.c}.
1053
1054 @item
1055 back end interface.  This interfaces to the code generation back end.  In
1056 treelang, this is @file{tree1.c} which mainly interfaces to @file{toplev.c} and
1057 @file{treetree.c} which mainly interfaces to everything else. Many languages
1058 mix up the back end interface with the parser, as in the C compiler for
1059 example.  It is a matter of taste which way to do it, but with treelang
1060 it is separated out to make the back end interface cleaner and easier to
1061 understand.
1062
1063 @item
1064 header files.  For function prototypes and common data items.  One point
1065 to note here is that bison can generate a header files with all the
1066 numbers is has assigned to the keywords and symbols, and you can include
1067 the same header in your lexer.  This technique is demonstrated in
1068 treelang.
1069
1070 @item
1071 compiler main file.  GCC comes with a file @file{toplev.c} which is a
1072 perfectly serviceable main program for your compiler.  GNU Treelang uses
1073 @file{toplev.c} but other languages have been known to replace it with their
1074 own main program.  Again this is a matter of taste and how much code you
1075 want to write. 
1076
1077 @end itemize
1078
1079 @end enumerate
1080
1081 @node treelang compiler interfaces, Hints and tips, treelang files, treelang internals
1082 @section treelang compiler interfaces
1083
1084 @cindex driver
1085 @cindex toplev.c
1086
1087 @menu
1088 * treelang driver::             
1089 * treelang main compiler::      
1090 @end menu
1091
1092 @node treelang driver, treelang main compiler, treelang compiler interfaces, treelang compiler interfaces
1093 @subsection treelang driver
1094
1095 The GCC compiler consists of a driver, which then executes the various
1096 compiler phases based on the instructions in the specs files. 
1097
1098 Typically a program's language will be identified from its suffix
1099 (e.g., @file{.tree}) for treelang programs.
1100
1101 The driver (@file{gcc.c}) will then drive (exec) in turn a preprocessor,
1102 the main compiler, the assembler and the link editor. Options to GCC allow you
1103 to override all of this. In the case of treelang programs there is no
1104 preprocessor, and mostly these days the C preprocessor is run within the
1105 main C compiler rather than as a separate process, apparently for reasons of speed.
1106
1107 You will be using the standard assembler and linkage editor so these are
1108 ignored from now on. 
1109
1110 You have to write your own preprocessor if you want one.  This is usually
1111 totally language specific.  The main point to be aware of is to ensure
1112 that you find some way to pass file name and line number information
1113 through to the main compiler so that it can tell the back end this
1114 information and so the debugger can find the right source line for each
1115 piece of code.  That is all there is to say about the preprocessor except
1116 that the preprocessor will probably not be the slowest part of the
1117 compiler and will probably not use the most memory so don't waste too
1118 much time tuning it until you know you need to do so.
1119
1120 @node treelang main compiler,  , treelang driver, treelang compiler interfaces
1121 @subsection treelang main compiler
1122
1123 The main compiler for treelang consists of @file{toplev.c} from the main GCC
1124 compiler, the parser, lexer and back end interface routines, and the
1125 back end routines themselves, of which there are many.
1126
1127 @file{toplev.c} does a lot of work for you and you should almost certainly
1128 use it.
1129
1130 Writing this code is the hard part of creating a compiler using GCC.  The
1131 back end interface documentation is incomplete and the interface is
1132 complex. 
1133
1134 There are three main aspects to interfacing to the other GCC code. 
1135
1136 @menu
1137 * Interfacing to toplev.c::     
1138 * Interfacing to the garbage collection::  
1139 * Interfacing to the code generation code. ::  
1140 @end menu
1141
1142 @node Interfacing to toplev.c, Interfacing to the garbage collection, treelang main compiler, treelang main compiler
1143 @subsubsection Interfacing to toplev.c
1144
1145 In treelang this is handled mainly in tree1.c
1146 and partly in treetree.c. Peruse toplev.c for details of what you need
1147 to do.
1148
1149 @node Interfacing to the garbage collection, Interfacing to the code generation code. , Interfacing to toplev.c, treelang main compiler
1150 @subsubsection Interfacing to the garbage collection
1151
1152 Interfacing to the garbage collection. In treelang this is mainly in
1153 tree1.c. 
1154
1155 Memory allocation in the compiler should be done using the ggc_alloc and
1156 kindred routines in ggc*.*. At the end of every 'function' in your language, toplev.c calls
1157 the garbage collection several times. The garbage collection calls mark
1158 routines which go through the memory which is still used, telling the
1159 garbage collection not to free it. Then all the memory not used is
1160 freed.
1161
1162 What this means is that you need a way to hook into this marking
1163 process. This is done by calling ggc_add_root. This provides the address
1164 of a callback routine which will be called duing garbage collection and
1165 which can call ggc_mark to save the storage. If storage is only
1166 used within the parsing of a function, you do not need to provide a way
1167 to mark it. 
1168
1169 Note that you can also call ggc_mark_tree to mark any of the back end
1170 internal 'tree' nodes. This routine will follow the branches of the
1171 trees and mark all the subordinate structures. This is useful for
1172 example when you have created a variable declaration that will be used
1173 across multiple functions, or for a function declaration (from a
1174 prototype) that may be used later on. See the next item for more on the
1175 tree nodes. 
1176
1177 @node Interfacing to the code generation code. ,  , Interfacing to the garbage collection, treelang main compiler
1178 @subsubsection Interfacing to the code generation code. 
1179
1180 In treelang this is done in treetree.c. A typedef called 'tree' which is
1181 defined in tree.h and tree.def in the GCC directory and largely
1182 implemented in tree.c and stmt.c forms the basic interface to the
1183 compiler back end.
1184
1185 In general you call various tree routines to generate code, either
1186 directly or through toplev.c. You build up data structures and
1187 expressions in similar ways. 
1188
1189 You can read some documentation on this which can be found via the GCC
1190 main web page. In particular, the documentation produced by Joachim
1191 Nadler and translated by Tim Josling can be quite useful. the C compiler
1192 also has documentation in the main GCC manual (particularly the current
1193 CVS version) which is useful on a lot of the details.
1194
1195 In time it is hoped to enhance this document to provide a more
1196 comprehensive overview of this topic. The main gap is in explaining how
1197 it all works together. 
1198
1199 @node Hints and tips,  , treelang compiler interfaces, treelang internals
1200 @section Hints and tips
1201
1202 @itemize @bullet
1203
1204 @item
1205 TAGS: Use the make ETAGS commands to create TAGS files which can be used in
1206 emacs to jump to any symbol quickly. 
1207
1208 @item
1209 GREP: grep is also a useful way to find all uses of a symbol.
1210
1211 @item
1212 TREE: The main routines to look at are tree.h and tree.def. You will
1213 probably want a hardcopy of these. 
1214
1215 @item
1216 SAMPLE: look at the sample interfacing code in treetree.c. You can use
1217 gdb to trace through the code and learn about how it all works. 
1218
1219 @item
1220 GDB: the GCC back end works well with gdb. It traps abort() and allows
1221 you to trace back what went wrong. 
1222
1223 @item
1224 Error Checking: The compiler back end does some error and consistency
1225 checking. Often the result of an error is just no code being
1226 generated. You will then need to trace through and find out what is
1227 going wrong. The rtl dump files can help here also.
1228
1229 @item
1230 rtl dump files: The main compiler documents these files which are dumps
1231 of the rtl (intermediate code) which is manipulated doing the code
1232 generation process. This can provide useful clues about what is going
1233 wrong. The rtl 'language' is documented in the main GCC manual.
1234
1235 @end itemize
1236
1237 @end ifset
1238
1239 @node Open Questions, Bugs, treelang internals, Top
1240 @chapter Open Questions
1241
1242 If you know GCC well, please consider looking at the file treetree.c and
1243 resolving any questions marked "???".
1244
1245 @node Bugs, Service, Open Questions, Top
1246 @chapter Reporting Bugs
1247 @cindex bugs
1248 @cindex reporting bugs
1249
1250 You can report bugs to @email{@value{email-bugs}}. Please make
1251 sure bugs are real before reporting them. Follow the guidelines in the
1252 main GCC manual for submitting bug reports.
1253
1254 @menu
1255 * Sending Patches::             
1256 @end menu
1257
1258 @node Sending Patches,  , Bugs, Bugs
1259 @section Sending Patches for GNU Treelang
1260
1261 If you would like to write bug fixes or improvements for the GNU
1262 Treelang compiler, that is very helpful.  Send suggested fixes to
1263 @email{@value{email-patches}}.
1264
1265 @node Service, Projects, Bugs, Top
1266 @chapter How To Get Help with GNU Treelang
1267
1268 If you need help installing, using or changing GNU Treelang, there are two
1269 ways to find it:
1270
1271 @itemize @bullet
1272
1273 @item
1274 Look in the service directory for someone who might help you for a fee.
1275 The service directory is found in the file named @file{SERVICE} in the
1276 GCC distribution.
1277
1278 @item
1279 Send a message to @email{@value{email-general}}.
1280
1281 @end itemize
1282
1283 @end ifset
1284 @ifset INTERNALS
1285
1286 @node Projects, Index, Service, Top
1287 @chapter Projects
1288 @cindex projects
1289
1290 If you want to contribute to @code{treelang} by doing research,
1291 design, specification, documentation, coding, or testing,
1292 the following information should give you some ideas.
1293
1294 Send a message to @email{@value{email-general}} if you plan to add a
1295 feature.
1296
1297 The main requirement for treelang is to add features and to add
1298 documentation. Features are things that the GCC back end can do but
1299 which are not reflected in treelang. Examples include structures,
1300 unions, pointers, arrays.
1301
1302 @end ifset
1303
1304 @node Index,  , Projects, Top
1305 @unnumbered Index
1306
1307 @printindex cp
1308 @summarycontents
1309 @contents
1310 @bye