3 * File name: testprograms/global_var_example.c
4 * Purpose: Illustrates representation of global variable.
5 * Author: Christian Schaefer, Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 * das leere FIRM Programm
25 * This program shows how to build ir for global variables.
26 * It constructs the ir for the following pseudo-program:
36 int main(int argc, char **argv)
38 ir_graph *irg; /* this variable contains the irgraph */
39 type *owner; /* the class in which this method is defined */
40 type *proc_main; /* type information for the method main */
41 type *prim_t_int; /* describes int type defined by the language */
42 entity *main_ent; /* represents this method as entity of owner */
43 entity *i_ent; /* the entity representing the global variable i */
44 ir_node *x, *i_ptr, *store;
46 printf("\nCreating an IR graph: GLOBAL_VAR ...\n");
51 /* make basic type information for primitive type int.
52 In Sather primitive types are represented by a class.
53 This is the modeling appropriate for other languages.
54 Mode_i says that all integers shall be implemented as a
55 32 bit integer value. */
56 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
58 /* FIRM was designed for oo languages where all methods belong to a class.
59 * For imperative languages like C we view a file or compilation unit as
60 * a large class containing all functions as methods in this file.
61 * This class is automatically generated and can be obtained by get_glob_type().
63 #define METHODNAME "GLOBAL_VAR_main"
67 /* Main is an entity of this global class. */
68 owner = get_glob_type();
69 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
71 main_ent = new_entity (owner,
72 new_id_from_chars (METHODNAME, strlen(METHODNAME)),
75 /* Generates the basic graph for the method represented by entity main_ent, that
76 * is, generates start and end blocks and nodes and a first, initial block.
77 * The constructor needs to know how many local variables the method has.
79 #define NUM_OF_LOCAL_VARS 0
81 /* Generate the entities for the global variables. */
82 i_ent = new_entity (get_glob_type(),
83 new_id_from_chars ("i", strlen("i")),
86 irg = new_ir_graph (main_ent, NUM_OF_LOCAL_VARS);
88 /* The constructor new_ir_graph() generated a region to place nodes in.
89 * This region is accessible via the attribut current_block of irg and
91 * Generate the assignment to i and the return node into this region.
92 * The Return node is needed to return at least the store. */
93 i_ptr = new_simpleSel(get_store(), get_irg_globals(irg), i_ent);
95 store = new_Store (get_store(), i_ptr,
96 new_Const(mode_Is, new_tarval_from_long (2, mode_Is)));
97 set_store(new_Proj(store, mode_M, pn_Store_M));
99 x = new_Return (get_store(), 0, NULL);
101 /* Now generate all instructions for this block and all its predecessor blocks
102 * so we can mature it. */
103 mature_immBlock (get_irg_current_block(irg));
105 /* This adds the in edge of the end block which originates at the return statement.
106 * The return node passes controlflow to the end block. */
107 add_immBlock_pred (get_irg_end_block(irg), x);
108 /* Now we can mature the end block as all it's predecessors are known. */
109 mature_immBlock (get_irg_end_block(irg));
111 irg_finalize_cons (irg);
113 printf("Optimizing ...\n");
114 dead_node_elimination(irg);
116 /* verify the graph */
119 printf("Done building the graph. Dumping it.\n");
120 char *dump_file_suffix = "";
121 dump_ir_block_graph (irg, dump_file_suffix);
122 dump_ir_graph_w_types (irg, dump_file_suffix);
123 printf("Use xvcg to view this graph:\n");
124 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");