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