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_class *owner; /* the class in which this method is defined */
28 entity *ent; /* represents this method as entity of owner */
29 ir_node *x, *const_str, *proc_ptr, *call;
31 printf("creating an IR graph: CALL_STR_EXAMPLE...\n");
36 /* FIRM was designed for oo languages where all methods belong to a class.
37 * For imperative languages like C we view a file as a large class containing
38 * all functions as methods in this file.
39 * Therefore we define a class "CALL_STR_EXAMPLE" with a method main as
42 #define CLASSNAME "CALL_STR_EXAMPLE"
43 #define ENTITYNAME "main"
45 owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
46 ent = new_entity ((type *)owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), NULL);
49 /* Generates the basic graph for the method represented by entity ent, that
50 * is, generates start and end blocks and nodes and a first, initial block.
51 * The constructor needs to know how many local variables the method has.
53 #define NUM_OF_LOCAL_VARS 0
55 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
57 /* the string is enterd in the constant table. const_str is a pointer to the string */
58 const_str = new_Const (mode_p, tarval_p_from_str ("Hello world!"));
60 /* get the pointer to the procedure from the class type */
61 /* this is how a pointer to be fixed by the linker is represented after
62 lowering a Sel node. */
63 #define FUNCTIONNAME "f"
64 proc_ptr = new_Const (mode_p, tarval_p_from_str (FUNCTIONNAME));
66 /* call procedure set_a, first built array with parameters */
70 call = new_Call(get_store(), proc_ptr, 1, in, NULL);
72 /* make the possible change of call to memory visible */
73 set_store(new_Proj(call, mode_M, 0));
76 /* The constructor new_ir_graph() generated a region to place nodes in.
77 * This region is accessible via the attribut current_block of irg and
79 * Generate the return node into this region. The Return node is needed to
80 * return at least the store. */
82 ir_node *in[0]; /* this is the array containing the return parameters */
83 x = new_Return (get_store(), 0, in);
85 /* Now generate all instructions for this block and all its predecessor blocks
86 * so we can mature it. */
87 mature_block (irg->current_block);
89 /* This adds the in edge of the end block which originates at the return statement.
90 * The return node passes controlflow to the end block. */
91 add_in_edge (irg->end_block, x);
92 /* Now we can mature the end block as all it's predecessors are known. */
93 mature_block (irg->end_block);
95 /* verify the graph */
98 printf("\nDone building the graph. Dumping it.\n");
99 dump_ir_block_graph (irg);
101 printf("use xvcg to view this graph:\n");
102 printf("/ben/goetz/bin/xvcg GRAPHNAME\n");