1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Goetz Lindenmaier
16 * das leere FIRM Programm
20 *** This program shows how to build ir for global variables.
21 *** It constructs the ir for the following pseudo-program:
31 int main(int argc, char **argv)
33 ir_graph *irg; /* this variable contains the irgraph */
34 type *owner; /* the class in which this method is defined */
35 type *proc_main; /* type information for the method main */
36 type *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;
41 printf("\nCreating an IR graph: GLOBAL_VAR ...\n");
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);
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().
58 #define METHODNAME "main"
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)),
66 main_ent = new_entity (owner,
67 id_from_str (METHODNAME, strlen(METHODNAME)),
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.
74 #define NUM_OF_LOCAL_VARS 0
76 /* Generate the entities for the global variables. */
77 i_ent = new_entity (get_glob_type(),
78 id_from_str ("i", strlen("i")),
81 irg = new_ir_graph (main_ent, NUM_OF_LOCAL_VARS);
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
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);
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));
95 ir_node *in[0]; /* this is the array containing the return parameters */
96 x = new_Return (get_store(), 0, in);
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");