nsharlit: remove duplicate const_string
[suif.git] / info / suif1.info-6
blob159decd8d76ae6b64ae809dc93afa3891d447e11
1 This is Info file suif1.info, produced by Makeinfo version 1.68 from
2 the input file suif1.texi.
4    This file documents the SUIF library.
6    Copyright (C) 1994 Stanford University.  All rights reserved.
8    Permission is given to use, copy, and modify this documentation for
9 any non-commercial purpose as long as this copyright notice is not
10 removed.  All other uses, including redistribution in whole or in part,
11 are forbidden without prior written permission.
13 \x1f
14 File: suif1.info,  Node: Immeds,  Next: Symbolic Addresses,  Up: Other
16 Immediate Values
17 ================
19    An immediate value may be an integer constant, string constant,
20 floating-point constant, SUIF type, symbolic address, expression
21 operand, or instruction, or it can be undefined.  It can be used as the
22 source of an `ldc' (load constant) instruction (*note Load Constant
23 Instructions::.) or as an element of an annotation (*note
24 Annotations::.).  Only integers, floating-point values, and symbolic
25 addresses may be used in `ldc' instructions, while any kind of
26 immediate value may appear in an annotation.  Immediate values are
27 implemented by the `immed' class defined in the files `immed.h' and
28 `immed.cc'.
30    The kind of value in an `immed' is identified by the `kind' method.
31 This returns a member of the `immed_kinds' enumerated type.  The
32 members of this enumeration are:
34 `im_int'
35      Integer.  The `is_integer' method tests for this kind of immediate
36      and the `integer' method returns the integer value.
38 `im_string'
39      Character string.  The string is automatically entered in the
40      lexicon (*note Lexicon::.) when the `immed' is created.  The
41      `is_string' method checks if an `immed' is a string, and the
42      `string' method returns a pointer to the string.
44 `im_float'
45      Floating-point value.  The SUIF library stores all floating-point
46      values using the `double' type, so the precision is limited by the
47      precision for the `double' type.  The `is_flt' method checks for
48      this kind of immediate, and the `flt' method returns the value.
50 `im_symbol'
51      Symbolic address.  *Note Symbolic Addresses::.  The `is_symbol'
52      method checks if an `immed' is a symbolic address.  The `addr'
53      method returns the entire `sym_addr' object, but the `symbol' and
54      `offset' methods are also available to retrieve the symbol pointer
55      and integer offset separately.
57 `im_type'
58      SUIF type.  The `is_type' method checks for this kind of immediate,
59      and the `type' method returns a pointer to the SUIF type node.
61 `im_op'
62      Expression operand.  The immed is considered to "own" the entire
63      expression tree when it's in an annotation, so the same expression
64      shouldn't be used in another annotation or as the source for an
65      instruction; instead a copy should be made with the `clone'
66      method.  The `is_op' method checks for this kind of immediate, and
67      the `op' method returns the expression.
69 `im_instr'
70      SUIF instruction.  The immed is considered to "own" the instruction
71      and its expression trees when it's in an annotation, so the same
72      instruction shouldn't be used in another annotation or in a
73      `tree_node' or expression; instead a copy should be made with the
74      `clone' method.  The `is_instr' method checks for this kind of
75      immediate, and the `instr' method returns the instruction.
77 `im_undef'
78      Undefined value.  This is included to signal errors by marking an
79      immediate as undefined.  The `is_error' method also checks for this
80      condition.
82    Since an immediate value is small, there are no methods to change the
83 value of an `immed'.  Instead a variety of constructors are provided to
84 make it easy to create new immediate values.  That is, you can
85 initialize different kinds of immediates by providing arguments of
86 various types to constructors.
88    The `immed' class provides two print methods, `print' and
89 `rawprint'.  The difference between them is that the latter doesn't
90 escape `"' and `\' when printing strings.
92 \x1f
93 File: suif1.info,  Node: Symbolic Addresses,  Next: Lexicon,  Prev: Immeds,  Up: Other
95 Symbolic Addresses
96 ==================
98    Symbolic addresses are currently only used in immediate values
99 (*note Immeds::.).  A symbolic address has two parts, a symbol and an
100 integer offset.  In many cases, the offset is set to zero, but for an
101 aggregate data structure the offset may specify the number of bits from
102 the beginning of the data structure to a particular field.  The
103 `sym_addr' class defined in the `symaddr.h' and `symaddr.cc' files
104 implements these symbolic addresses.
106    The `symbol' method returns a pointer to the symbol in a symbolic
107 address, and the `offset' method returns the integer offset.  As with
108 most other offsets in SUIF, the offset is in *bits*.  Be careful to
109 avoid treating this as a byte or word offset.  As with immediate
110 values, there are no methods to change the contents of a symbolic
111 address.  Because these are small structures, it is just as easy to
112 create new `sym_addr' objects whenever they are needed.  To print a
113 symbolic address, use the `print' method.
115 \x1f
116 File: suif1.info,  Node: Lexicon,  Next: Target Machine Parameters,  Prev: Symbolic Addresses,  Up: Other
118 Lexicon
119 =======
121    Almost all character strings in SUIF are entered in a hash table
122 called the `lexicon'.  This table removes duplicate strings so that
123 string comparisons can be reduced to simple pointer comparisons.  The
124 `lexicon' is an object of the `string_table' class defined in the
125 `stringtable.h' and `stringtable.cc' files.
127    A `string_table' is an open hash table; each bucket is a linked list
128 of string entries represented by objects of the `string_e' class.  The
129 only public method in the `string_table' class is called `enter'.  This
130 method searches the table for a specified string.  If the string is
131 found, `enter' returns a pointer to the existing `string_e' entry.
132 Otherwise, it creates a new entry and adds it to the table.  In either
133 case, the actual character string pointer can be retrieved directly
134 from the `sp' field of the resulting `string_e' entry.
136    None of the strings in the `lexicon' are deallocated until a SUIF
137 program terminates.  Consequently, when adding a new string to the
138 table, it is copied to the heap in case the original string is stored on
139 the stack.  If the string is known to be allocated on the heap and will
140 never be modified or deallocated, this string copy can be avoided by
141 setting the optional `fixed' flag when calling `enter'.
143 \x1f
144 File: suif1.info,  Node: Target Machine Parameters,  Next: Initialization,  Prev: Lexicon,  Up: Other
146 Target Machine Parameters
147 =========================
149    The SUIF library includes a structure to record various parameters of
150 the target machine.  The back-end of the compiler may be either a code
151 generator for a specific processor or the system's C compiler, using the
152 SUIF-to-C translator.  Thus, the target machine parameters include
153 fields that deal both with hardware requirements and with details of the
154 back-end C compiler.  These parameters are stored in the `target'
155 `machine_params' structure.  The front-end is responsible for setting
156 the target parameters, but they are then saved in the SUIF files and
157 passed along to all subsequent passes of the compiler.
159    The first fields in the `machine_params' structure deal with the
160 addressing in the underlying hardware.  The `is_big_endian' field
161 specifies the byte order, and the `addressable_size' field specifies
162 the size in bits of the smallest addressable unit.  Since most machines
163 are byte-addressable, the `addressable_size' is usually set to eight.
165    Next, the sizes and alignments for various C types are specified.
166 The possible C types are listed in the `C_types' enumeration:
168 `C_char'
170 `C_short'
172 `C_int'
174 `C_long'
176 `C_longlong'
178 `C_float'
180 `C_double'
182 `C_longdouble'
184 `C_ptr'
185 The `size' and `align' fields of the `machine_params' structure are
186 arrays containing the size and alignment for each C type.  Both are
187 specified in bits (not bytes).  Some C compilers require additional
188 alignment restrictions for arrays and structures.  The `array_align'
189 and `struct_align' fields contain these alignment requirements in bits.
190 Note that these do not replace the alignment restrictions for the
191 components of an array or structure; the most restrictive alignments
192 must always be maintained.
194    The remaining fields are only applicable to the back-end C compiler.
195 The `char_is_signed' field specifies if the default `char' type is
196 signed.  This is just to allow the SUIF-to-C translator to avoid
197 cluttering up the C code by explicitly putting `signed' or `unsigned'
198 in every declaration of a `char' type.  When two pointers are
199 subtracted in C, the result type is implementation defined.  The
200 `ptr_diff_type' field in `machine_params' specifies the type produced
201 by such pointer subtractions for use in SUIF-to-C.
203 \x1f
204 File: suif1.info,  Node: Initialization,  Next: Command-Line Parser,  Prev: Target Machine Parameters,  Up: Other
206 Initialization and Finalization
207 ===============================
209    Before using any features of the SUIF library, a program must first
210 initialize it.  The `init_suif' function, defined in the `initsuif.h'
211 and `initsuif.cc' files, performs this initialization.  The `main'
212 function of a SUIF program should call `init_suif' first.  The `argc'
213 and `argv' parameters are passed to `init_suif' so that it can check
214 for the following standard options:
216 `-version'
217      Print the version numbers and compilation information for the
218      program and for any libraries with which it is linked.  The
219      `prog_ver_string' and `prog_who_string' strings contain the
220      version number and compilation information for the program.  They
221      are usually set automatically by the standard SUIF makefiles.
222      Similarly, the `libsuif1_ver_string' and `libsuif1_who_string'
223      strings are used for the SUIF library.  Information for other
224      libraries is recorded when they are registered (as described
225      below).
227 `-print-flat'
228      Do not print structured annotations (*note Structured Annotes::.)
229      using the user-defined printing functions.  If all annotations are
230      printed as flat lists of immediate values, the output could be
231      parsed and converted back to a binary SUIF file.
233 `-no-types'
234      Do not print the result types for instructions.  This is just to
235      help make the output more readable in situations where the result
236      types are not of interest.
238 `-no-symtabs'
239      Do not print out symbol tables.  If the symbolic information is not
240      needed, this makes the output shorter and easier to read.
242 These options are potentially applicable to all SUIF programs, so they
243 are included in the library.
245    At the end of a SUIF program, the data structures in the SUIF library
246 may be deallocated.  Although this is not absolutely necessary, it may
247 simplify debugging by making it easier to read the output from Purify
248 and other tools.  Besides deallocating the data structures, it may
249 someday be useful to perform other actions at the end of a SUIF program.
250 The `exit_suif' function is provided to perform this finalization.  If
251 used, it should be called at the very end of the program.
253    Besides the base SUIF library, many SUIF programs are linked with
254 other libraries that also need to be initialized.  Rather than just
255 initializing them directly, they are registered with the SUIF library
256 along with initialization and finalization functions.  The
257 initialization functions are then called automatically by `init_suif'
258 and the finalization functions by `exit_suif'.  This allows the SUIF
259 library to record and print version numbers and other information for
260 all of the libraries linked with a program.  The `register_library'
261 function records the name, version, and compilation information for a
262 library along with pointers to the functions to initialize and finalize
263 that library.  The initialization function must match the `lib_init_f'
264 type, which takes the `argc' and `argv' parameters for the command line
265 and returns `void'.  Similarly, the finalization function must match the
266 `lib_exit_f' type, which takes no arguments and returns `void'.  The
267 parameters for the name, version, and compilation information are
268 required, but the initialization and finalization functions are
269 optional.  The libraries must be registered, in the order in which they
270 should be initialized, before calling `init_suif'.  The finalization
271 functions are called in the reverse order in which the libraries were
272 registered.
274    The situation is much simpler if you use the standard SUIF makefiles.
275 These makefiles automatically generate strings holding the version and
276 compilation information.  The `LIBRARY' macro may be used to register a
277 library using these version strings.  You only need to specify the base
278 name of the library and the names of the initialization and
279 finalization functions (1).  To make things even easier, the SUIF
280 makefiles also generate a new function called `start_suif'.  This
281 function automatically registers all of the libraries linked with the
282 program and then calls `init_suif'.  So to make a long story short, if
283 a SUIF program begins with a call to `start_suif' all of the libraries
284 will be automatically registered and initialized.
286    ---------- Footnotes ----------
288    (1) Unlike when calling `register_library' directly, the function
289 arguments are required when using the `LIBRARY' macro.
291 \x1f
292 File: suif1.info,  Node: Command-Line Parser,  Next: Error Handling,  Prev: Initialization,  Up: Other
294 Command-Line Parser
295 ===================
297    The SUIF library provides a generic interface for a command line
298 parser.  The parser is defined and implemented in the files
299 `cmdparse.h' and `cmdparse.cc'.  The SUIF program can provide a
300 structure that contains a list of command line options, the type of
301 arguments these options can handle, and pointers to data where the
302 argument values will be stored.  The data locations will be initialized
303 to default values specified in the options table before parsing begins.
304 The `parse_cmd_line' function is called with `argc', `argv', the
305 options table, and the number of options in the table.  If the parser
306 finds one of the options on the command line, the option (and its
307 argument, if any) are removed from `argv' and `argc' is appropriately
308 adjusted.  Thus when parsing is finished, `argv' contains only
309 unrecognized options (such as file names).
311    The following types of arguments are supported:
313 `CLO_NOARG'
314      No argument is expected.  The default value is 0.  If the argument
315      is present, the value becomes 1.
317 `CLO_INT'
318      A single integer is expected.
320 `CLO_STRING'
321      A single string is expected.
323 `CLO_MULTI_STRING'
324      A single string is expected for each occurrence of this option, but
325      the option can be repeated with different arguments.
327    For example:
329      static boolean quiet;
330      static int size;
331      static cmd_line_option my_options[] = {
332          { CLO_NOARG, "-quiet", "", &quiet },
333          { CLO_INT, "-size", "8", &size }
334      };
335      
336      parse_cmd_line(argc, argv, my_options,
337                     sizeof(my_options)/sizeof(cmd_line_option));
339 \x1f
340 File: suif1.info,  Node: Error Handling,  Next: Procedure Iterators,  Prev: Command-Line Parser,  Up: Other
342 Error Handling
343 ==============
345    The SUIF library includes a number of functions for error handling.
346 These functions handle three kinds of problems: errors, warnings, and
347 assertions.  Assertions are tests for things that should never occur in
348 a correct program.  Any problem that could potentially occur in a
349 correct program due to bad input or other external conditions should be
350 treated as an error rather than an assertion failure.  Warnings are used
351 to report potentially troublesome conditions that are not serious enough
352 to cause a fatal error.
354    Two macros are available to implement assertions.  The `assert'
355 macro takes a single expression as an argument.  If the expression
356 evaluates to zero, a message is printed showing the current line number,
357 file name, and the expression that failed, and then the `abort'
358 function is called to terminate the program.  The `assert_msg' macro is
359 similar except that it also prints a user-defined message if the
360 assertion fails.  Besides the expression to be tested, `assert_msg'
361 takes an argument that contains the message to be printed.  This second
362 argument must hold the `printf' format string and the arguments for the
363 call to `printf', separated by commas.  To keep the commas from
364 splitting up the second argument, it must be surrounded by parentheses.
365 For example:
367      assert_msg(sym->parent(), ("no parent for %s", sym->name()));
369    Errors may be reported using the `error_line' function.  The
370 `return_code' parameter specifies the method of terminating the
371 program.  If the return code is negative, the `abort' function is
372 called.  If it is greater than zero, the `exit' function is called with
373 the specified return code.  If the return code is zero, the error
374 message will be printed but the program will not terminate.  The
375 `the_node' parameter is optional and may be set to `NULL'.  If used, it
376 points to the AST node closest to the point where the error was
377 detected, and the library tries to use it to find and print the
378 corresponding source line number.  Finally, the `fmt' parameter
379 specifies the `printf' format string used to print the error message
380 and any additional parameters are passed on to `printf'.  The
381 `verror_line' function is identical to `error_line' except that it
382 passes the variable arguments for `printf' using a `varargs'-style
383 list.  This is just like the standard `vprintf' function.
385    The `warning_line' and `vwarning_line' functions are just like the
386 corresponding error functions, except that they print warning messages
387 and do not terminate the program.  Consequently, they do not have
388 `return_code' parameters.
390 \x1f
391 File: suif1.info,  Node: Procedure Iterators,  Prev: Error Handling,  Up: Other
393 Procedure Iterators
394 ===================
396    Most SUIF programs work on one procedure at a time.  These programs
397 require basically the same code to iterate through the file set entries
398 and procedures.  The `suif_proc_iter' routine is provided to handle
399 this common case.
401    Before calling the procedure iterator, you must initialize the SUIF
402 library (*note Initialization::.), which also parses and removes some
403 standard options from the command-line arguments.  Either the SUIF
404 library's command-line parser (*note Command-Line Parser::.) or the
405 `getopt' function (from the standard C library) may be called before
406 the procedure iterator to parse command-line options not removed by the
407 library initialization.  The `suif_proc_iter' function then reads the
408 input and output file names from the command-line.  The first file name
409 on the command line is treated as an input file.  If the `writeback'
410 flag is set, the second file is treated as an output file and any
411 additional file names on the command line are alternately treated as
412 input and output files.  If the `writeback' flag is not set, all the
413 file names are treated as input files.
415    The procedure bodies are then read one at a time and processed by a
416 user-specified function.  This function must be of the type
417 `prociter_f', which takes a single `tree_proc' argument and has a
418 `void' return type.  When a procedure is read, the `exp_trees' and
419 `use_fortran_form' parameters are passed on to the `read_proc' method
420 for the `proc_sym'.  The `exp_trees' argument specifies whether the
421 procedure bodies are converted to expression trees (*note Expression
422 Trees::.), and the `use_fortran_form' argument determines whether
423 Fortran form is used (*note Fortran::.).  The defaults are to create
424 expression trees but not to use Fortran form.  If the `writeback' flag
425 is set, the procedure bodies are written to the output files after they
426 have been processed.
428 \x1f
429 File: suif1.info,  Node: Appendix,  Next: Function Index,  Prev: Other,  Up: Top
431 Appendix
432 ********
434    Please note that the diagrams are only available in the PostScript of
435 HTML versions of this document.  This section won't mean much to you
436 otherwise.
438 * Menu:
440 * Inherit Diagram::
441 * Overview Diagram::
442 * Symbol Table Diagram::
444 \x1f
445 File: suif1.info,  Node: Inherit Diagram,  Next: Overview Diagram,  Up: Appendix
447 Class Inheritance of Major SUIF Structures
448 ==========================================
450    All major SUIF data structures are derived from the `suif_object'
451 base class. *Note SUIF Objects::. The class `file_set_entry' is defined
452 in *Note File Set Entries::, the class `tree_node' and its derived
453 classes are explained in *Note Tree Nodes::, the class `sym_node' and
454 its derived classes are described in *Note Symbols::, the class
455 `type_node' and its derived classes are defined in *Note Types::, the
456 class `instruction' and its derived classes are defined in *Note
457 Instructions:: and the class `var_def' is defined in *Note Variable
458 Symbols::.
460 \x1f
461 File: suif1.info,  Node: Overview Diagram,  Next: Symbol Table Diagram,  Prev: Inherit Diagram,  Up: Appendix
463 Overview of the SUIF Hierarchy
464 ==============================
466    This diagram represents the overall hierarchical structure of the
467 SUIF intermediate representation. At the top level, the global symbol
468 information is kept. *Note Global Symbol Table::. In the next level,
469 information regarding each input and output file is kept.  *Note File
470 Representation::. The third level is the abstract syntax tree
471 representation.  *Note Trees::. The bottom level contains the expression
472 trees.  *Note Instructions::.
474 \x1f
475 File: suif1.info,  Node: Symbol Table Diagram,  Prev: Overview Diagram,  Up: Appendix
477 The Symbol Table Representation
478 ===============================
480    The first diagram below illustrates the symbol table structure.  The
481 second shows the hierarchy of symbol tables. *Note Symbol Table
482 Hierarchy:: for more details.