OSDN Git Service

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