added doxygen comments
[libfirm] / testprograms / global_var_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors: Goetz Lindenmaier
5 *
6 * testprogram.
7 *
8 */
9
10 # include <stdio.h>
11 # include <string.h>
12
13 # include "irvrfy.h"
14 # include "irdump.h"
15 # include "firm.h"
16
17 /*
18  * das leere FIRM Programm
19  */
20
21 /**
22 *  This program shows how to build ir for global variables.
23 *  It constructs the ir for the following pseudo-program:
24 *
25 *  int i;
26 *
27 *  main() {
28 *    i = 2;
29 *    return;
30 *  }
31 **/
32
33 int main(int argc, char **argv)
34 {
35   ir_graph *irg;        /* this variable contains the irgraph */
36   type     *owner;      /* the class in which this method is defined */
37   type     *proc_main;  /* type information for the method main */
38   type     *prim_t_int; /* describes int type defined by the language */
39   entity   *main_ent;   /* represents this method as entity of owner */
40   entity   *i_ent;      /* the entity representing the global variable i */
41   ir_node  *x, *i_ptr, *store;
42
43   printf("\nCreating an IR graph: GLOBAL_VAR ...\n");
44
45   /* init library */
46   init_firm ();
47
48   /* make basic type information for primitive type int.
49      In Sather primitive types are represented by a class.
50      This is the modeling appropriate for other languages.
51      Mode_i says that all integers shall be implemented as a
52      32 bit integer value.  */
53   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
54
55   /* FIRM was designed for oo languages where all methods belong to a class.
56    * For imperative languages like C we view a file or compilation unit as
57    * a large class containing all functions as methods in this file.
58    * This class is automatically generated and can be obtained by get_glob_type().
59    */
60 #define METHODNAME "main"
61 #define NRARGS 0
62 #define NRES 0
63
64   /* Main is an entity of this global class. */
65   owner = get_glob_type();
66   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
67                               NRARGS, NRES);
68   main_ent = new_entity (owner,
69                          id_from_str (METHODNAME, strlen(METHODNAME)),
70                          proc_main);
71
72   /* Generates the basic graph for the method represented by entity main_ent, that
73    * is, generates start and end blocks and nodes and a first, initial block.
74    * The constructor needs to know how many local variables the method has.
75    */
76 #define NUM_OF_LOCAL_VARS 0
77
78   /* Generate the entities for the global variables. */
79   i_ent = new_entity (get_glob_type(),
80                       id_from_str ("i", strlen("i")),
81                       prim_t_int);
82
83   irg = new_ir_graph (main_ent, NUM_OF_LOCAL_VARS);
84
85   /* The constructor new_ir_graph() generated a region to place nodes in.
86    * This region is accessible via the attribut current_block of irg and
87    * it is not matured.
88    * Generate the assignment to i and the return node into this region.
89    * The Return node is needed to return at least the store. */
90   i_ptr = new_simpleSel(get_store(), get_irg_globals(irg), i_ent);
91
92   store = new_Store (get_store(), i_ptr,
93                      new_Const(mode_Is, tarval_from_long (mode_Is, 2)));
94   set_store(new_Proj(store, mode_M, 0));
95
96   {
97     ir_node *in[0]; /* this is the array containing the return parameters */
98     x = new_Return (get_store(), 0, in);
99   }
100   /* Now generate all instructions for this block and all its predecessor blocks
101    * so we can mature it. */
102   mature_block (get_irg_current_block(irg));
103
104   /* This adds the in edge of the end block which originates at the return statement.
105    * The return node passes controlflow to the end block.  */
106   add_in_edge (get_irg_end_block(irg), x);
107   /* Now we can mature the end block as all it's predecessors are known. */
108   mature_block (get_irg_end_block(irg));
109
110   finalize_cons (irg);
111
112   printf("Optimizing ...\n");
113   dead_node_elimination(irg);
114
115   /* verify the graph */
116   irg_vrfy(irg);
117
118   printf("Done building the graph.  Dumping it.\n");
119   dump_ir_block_graph (irg);
120   dump_ir_graph_w_types (irg);
121   printf("Use xvcg to view this graph:\n");
122   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
123
124   return (0);
125 }