1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Christian Schaefer, Goetz Lindenmaier
17 * This file constructs the ir for the following pseudo-program:
21 * void CALL_STR_EXAMPLE_main () {
25 * This program shall demonstrate how to represent string constants.
28 int main(int argc, char **argv)
30 ir_graph *irg; /* this variable contains the irgraph */
31 type *owner; /* the class in which this method is defined */
32 type *proc_main; /* type information for the method main */
33 type *proc_called; /* type information for called method f */
34 type *U8, *U8array, *string_ptr; /* type for pointers to strings. */
35 entity *ent; /* represents this method as entity of owner */
36 entity *const_str; /* represents a constant string. */
37 char *str = "Hello World\n"; /* The constant string. */
38 ir_node *x, *str_addr, *proc_ptr, *call;
41 printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
46 /* An unsinged 8 bit type */
47 U8 = new_type_primitive (id_from_str("char", 4), mode_Bu);
48 /* An array containing unsigned 8 bit elements. */
49 U8array = new_type_array (id_from_str ("char_arr", 8), 1, U8);
50 string_ptr = new_type_pointer (id_from_str ("ptr_to_string", 13), U8array);
52 /* Make a global entity that represents the constant String. */
53 const_str = new_entity(get_glob_type(), new_id_from_str("constStr"), U8array);
54 set_entity_variability(const_str, constant);
55 for (i = 0; i < strlen(str); i++) {
56 tarval *val = new_tarval_from_long(str[i], mode_Bu);
57 ir_node *con = new_Const(mode_Bu, val);
58 add_compound_ent_value(const_str, con, get_array_element_entity(U8array));
61 /* FIRM was designed for oo languages where all methods belong to a class.
62 * For imperative languages like C we view a program as a large class containing
63 * all functions of the program as methods in this class. This class is
64 * automatically generated.
65 * We use the same name for the method type as for the method entity.
67 #define METHODNAME "CALL_STR_EXAMPLE_main"
70 owner = get_glob_type();
71 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
74 /* Make type information for called method which also belongs to the
76 #define F_METHODNAME "f"
79 owner = get_glob_type();
80 proc_called = new_type_method(id_from_str(F_METHODNAME, strlen(F_METHODNAME)),
82 set_method_param_type(proc_called, 0, string_ptr);
85 /* Make the entity for main needed for a correct ir_graph. */
86 #define ENTITYNAME "CALL_STR_EXAMPLE_main"
87 ent = new_entity (owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)),
90 /* Generates the basic graph for the method represented by entity ent, that
91 * is, generates start and end blocks and nodes and a first, initial block.
92 * The constructor needs to know how many local variables the method has.
94 #define NUM_OF_LOCAL_VARS 0
95 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
97 /* get the pointer to the string constant */
98 str_addr = new_Const(mode_P, new_tarval_from_entity(const_str, mode_P));
100 /* get the pointer to the procedure from the class type */
101 /* this is how a pointer to be fixed by the linker is represented. */
102 proc_ptr = new_SymConst ((type_or_id_p)id_from_str (F_METHODNAME, strlen(F_METHODNAME)),
105 /* call procedure set_a, first built array with parameters */
109 call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
111 /* make the possible changes by the called method to memory visible */
112 set_store(new_Proj(call, mode_M, 0));
114 /* Make the return node returning the memory. */
115 x = new_Return (get_store(), 0, NULL);
116 /* Now we generated all instructions for this block and all its predecessor blocks
117 * so we can mature it. */
118 mature_block (get_irg_current_block(irg));
120 /* This adds the in edge of the end block which originates at the return statement.
121 * The return node passes controlflow to the end block. */
122 add_in_edge (get_irg_end_block(irg), x);
123 /* Now we can mature the end block as all it's predecessors are known. */
124 mature_block (get_irg_end_block(irg));
128 printf("Optimizing ...\n");
129 dead_node_elimination(irg);
131 /* verify the graph */
134 printf("Done building the graph. Dumping it.\n");
135 dump_ir_block_graph (irg);
137 printf("Use xvcg to view this graph:\n");
138 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");