1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
15 *** This file constructs the ir for the following pseudo-program:
20 *** f("Hello world !");
24 int main(int argc, char **argv)
26 ir_graph *irg; /* this variable contains the irgraph */
27 type *owner; /* the class in which this method is defined */
28 type *proc_main; /* type information for the method main */
29 type *proc_called; /* type information for called method f */
30 type *string_ptr; /* type for pointers to strings. */
31 entity *ent; /* represents this method as entity of owner */
32 ir_node *x, *const_str, *proc_ptr, *call;
34 printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
39 string_ptr = new_type_pointer (
40 id_from_str ("ptr_to_string", 13),
41 new_type_array (id_from_str ("char_arr", 8), 1,
42 new_type_primitive (id_from_str("char", 4), mode_c)));
43 /* FIRM was designed for oo languages where all methods belong to a class.
44 * For imperative languages like C we view a program as a large class containing
45 * all functions of the program as methods in this class. This class is
46 * automatically generated.
47 * We use the same name for the method type as for the method entity.
49 #define METHODNAME "main"
52 owner = get_glob_type();
53 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
56 /* Make type information for called method which also belongs to the
58 #define F_METHODNAME "f"
61 owner = get_glob_type();
62 proc_called = new_type_method(id_from_str(F_METHODNAME, strlen(F_METHODNAME)),
64 set_method_param_type(proc_called, 0, string_ptr);
66 /* Make the entity for main needed for a correct ir_graph. */
67 #define ENTITYNAME "main"
68 ent = new_entity (owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)),
71 /* Generates the basic graph for the method represented by entity ent, that
72 * is, generates start and end blocks and nodes and a first, initial block.
73 * The constructor needs to know how many local variables the method has.
75 #define NUM_OF_LOCAL_VARS 0
76 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
78 /* the string is entered in the constant table. const_str is a pointer to the string */
79 const_str = new_Const (mode_p, tarval_p_from_str ("Hello world!"));
81 /* get the pointer to the procedure from the class type */
82 /* this is how a pointer to be fixed by the linker is represented after
83 lowering a Sel node. */
84 #define FUNCTIONNAME "f"
85 proc_ptr = new_SymConst ((type_or_id_p)id_from_str (FUNCTIONNAME, strlen(FUNCTIONNAME)),
88 /* call procedure set_a, first built array with parameters */
92 call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
94 /* make the possible changes by the called method to memory visible */
95 set_store(new_Proj(call, mode_M, 0));
97 /* Make the return node returning the memory. */
99 ir_node *in[0]; /* this is the array containing the return parameters */
100 x = new_Return (get_store(), 0, in);
102 /* Now we generated all instructions for this block and all its predecessor blocks
103 * so we can mature it. */
104 mature_block (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_in_edge (get_irg_end_block(irg), x);
109 /* Now we can mature the end block as all it's predecessors are known. */
110 mature_block (get_irg_end_block(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);
122 printf("Use xvcg to view this graph:\n");
123 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");