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