1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Goetz Lindenmaier
18 * das leere FIRM Programm
22 * This program shows how to build ir for global variables.
23 * It constructs the ir for the following pseudo-program:
33 int main(int argc, char **argv)
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;
43 printf("\nCreating an IR graph: GLOBAL_VAR ...\n");
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);
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().
60 #define METHODNAME "GLOBAL_VAR_main"
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)),
68 main_ent = new_entity (owner,
69 id_from_str (METHODNAME, strlen(METHODNAME)),
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.
76 #define NUM_OF_LOCAL_VARS 0
78 /* Generate the entities for the global variables. */
79 i_ent = new_entity (get_glob_type(),
80 id_from_str ("i", strlen("i")),
83 irg = new_ir_graph (main_ent, NUM_OF_LOCAL_VARS);
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
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);
92 store = new_Store (get_store(), i_ptr,
93 new_Const(mode_Is, new_tarval_from_long (2, mode_Is)));
94 set_store(new_Proj(store, mode_M, 0));
96 x = new_Return (get_store(), 0, NULL);
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));
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));
110 printf("Optimizing ...\n");
111 dead_node_elimination(irg);
113 /* verify the graph */
116 printf("Done building the graph. Dumping it.\n");
117 dump_ir_block_graph (irg);
118 dump_ir_graph_w_types (irg);
119 printf("Use xvcg to view this graph:\n");
120 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");