cfac7a35e5e98d84c090edead3b76adf9295a65f
[libfirm] / testprograms / call_str_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 #include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /**
15 ***  This file constructs the ir for the following pseudo-program:
16 ***
17 ***  void f(char *);
18 ***
19 ***  main() {
20 ***    f("Hello world !");
21 ***  }
22 **/
23
24 int main(int argc, char **argv)
25 {
26   ir_graph *irg;          /* this variable contains the irgraph */
27   type_class *owner;      /* the class in which this method is defined */
28   type_method *proc_main; /* type information for the method main */
29   type_method *proc_called; /* type information for called method f */
30   entity *ent;            /* represents this method as entity of owner */
31   ir_node *x, *const_str, *proc_ptr, *call;
32
33   printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
34
35   /* init library */
36   init_firm ();
37
38   /* FIRM was designed for oo languages where all methods belong to a class.
39    * For imperative languages like C we view a program as a large class containing
40    * all functions of the program as methods in this class.  This class is
41    * automatically generated.
42    * We use the same name for the method type as for the method entity.
43    */
44 #define METHODNAME "main"
45 #define NRARGS 0
46 #define NRES 0
47   owner = get_glob_type();
48   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
49                               NRARGS, NRES);
50
51   /* Make type information for called method which also belongs to the
52      global type. */
53 #define F_METHODNAME "f"
54 #define F_NRARGS 1
55 #define F_NRES 0
56   owner = get_glob_type();
57   proc_called = new_type_method(id_from_str(F_METHODNAME, strlen(F_METHODNAME)),
58                               F_NRARGS, F_NRES);
59
60   /* Make the entity for main needed for a correct  ir_graph.  */
61 #define ENTITYNAME "main"
62   ent = new_entity ((type *)owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)),
63                     (type *)proc_main);
64
65   /* Generates the basic graph for the method represented by entity ent, that
66    * is, generates start and end blocks and nodes and a first, initial block.
67    * The constructor needs to know how many local variables the method has.
68    */
69 #define NUM_OF_LOCAL_VARS 0
70   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
71
72   /* the string is entered in the constant table. const_str is a pointer to the string */
73   const_str = new_Const (mode_p, tarval_p_from_str ("Hello world!"));
74
75   /* get the pointer to the procedure from the class type */
76   /* this is how a pointer to be fixed by the linker is represented after
77      lowering a Sel node. */
78 #define FUNCTIONNAME "f"
79   proc_ptr = new_SymConst ((type_or_id_p)id_from_str (FUNCTIONNAME, strlen(FUNCTIONNAME)),
80                            linkage_ptr_info);
81
82   /* call procedure set_a, first built array with parameters */
83   {
84     ir_node *in[1];
85     in[0] = const_str;
86     call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
87   }
88   /* make the possible changes by the called method to memory visible */
89   set_store(new_Proj(call, mode_M, 0));
90
91   /* Make the return node returning the memory. */
92   {
93     ir_node *in[0]; /* this is the array containing the return parameters */
94     x = new_Return (get_store(), 0, in);
95   }
96   /* Now we generated all instructions for this block and all its predecessor blocks
97    * so we can mature it. */
98   mature_block (get_irg_current_block(irg));
99
100   /* This adds the in edge of the end block which originates at the return statement.
101    * The return node passes controlflow to the end block.  */
102   add_in_edge (get_irg_end_block(irg), x);
103   /* Now we can mature the end block as all it's predecessors are known. */
104   mature_block (get_irg_end_block(irg));
105
106   printf("Optimizing ...\n");
107   dead_node_elimination(irg);
108
109   /* verify the graph */
110   irg_vrfy(irg);
111
112   printf("Done building the graph.  Dumping it.\n");
113   dump_ir_block_graph (irg);
114   printf("Use xvcg to view this graph:\n");
115   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
116
117   return (0);
118 }