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.
16 #include <libfirm/firm.h>
19 * das leere FIRM Programm
23 * This program shows how to build ir for global variables.
24 * It constructs the ir for the following pseudo-program:
36 char *dump_file_suffix = "";
37 ir_graph *irg; /* this variable contains the irgraph */
38 ir_type *owner; /* the class in which this method is defined */
39 ir_type *proc_main; /* ir_type information for the method main */
40 ir_type *prim_t_int; /* describes int ir_type defined by the language */
41 ir_entity *main_ent; /* represents this method as ir_entity of owner */
42 ir_entity *i_ent; /* the ir_entity representing the global variable i */
43 union symconst_symbol symbol;
44 ir_node *x, *i_ptr, *store;
46 printf("\nCreating an IR graph: GLOBAL_VAR ...\n");
51 /* make basic ir_type information for primitive ir_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 ir_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 ir_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 symbol.entity_p = i_ent;
94 i_ptr = new_SymConst(mode_P, symbol, symconst_addr_ent);
96 store = new_Store (get_store(), i_ptr,
97 new_Const(mode_Is, new_tarval_from_long (2, mode_Is)));
98 set_store(new_Proj(store, mode_M, pn_Store_M));
100 x = new_Return (get_store(), 0, NULL);
102 /* Now generate all instructions for this block and all its predecessor blocks
103 * so we can mature it. */
104 mature_immBlock (get_irg_current_block(irg));
106 /* This adds the in edge of the end block which originates at the return statement.
107 * The return node passes controlflow to the end block. */
108 add_immBlock_pred (get_irg_end_block(irg), x);
109 /* Now we can mature the end block as all it's predecessors are known. */
110 mature_immBlock (get_irg_end_block(irg));
112 irg_finalize_cons (irg);
114 printf("Optimizing ...\n");
115 dead_node_elimination(irg);
117 /* verify the graph */
120 printf("Done building the graph. Dumping it.\n");
121 dump_ir_block_graph (irg, dump_file_suffix);
122 dump_ir_graph_w_types (irg, dump_file_suffix);
123 printf("Use ycomp to view this graph:\n");
124 printf("ycomp GRAPHNAME\n\n");