used new prefixed enum value
[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 *  void CALL_STR_EXAMPLE_main () {
22       f("Hello World\n");
23 *  }
24 *
25 *  This program shall demonstrate how to represent string constants.
26 **/
27
28 int main(int argc, char **argv)
29 {
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;
39   int i;
40
41   printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
42
43   /* init library */
44   init_firm (NULL);
45
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);
51
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, variability_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));
59   }
60
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.
66    */
67 #define METHODNAME "CALL_STR_EXAMPLE_main"
68 #define NRARGS 0
69 #define NRES 0
70   owner = get_glob_type();
71   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
72                               NRARGS, NRES);
73
74   /* Make type information for called method which also belongs to the
75      global type. */
76 #define F_METHODNAME "f"
77 #define F_NRARGS 1
78 #define F_NRES 0
79   owner = get_glob_type();
80   proc_called = new_type_method(id_from_str(F_METHODNAME, strlen(F_METHODNAME)),
81                               F_NRARGS, F_NRES);
82   set_method_param_type(proc_called, 0, string_ptr);
83
84
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)),
88                     proc_main);
89
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.
93    */
94 #define NUM_OF_LOCAL_VARS 0
95   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
96
97   /* get the pointer to the string constant */
98   str_addr = new_Const(mode_P, new_tarval_from_entity(const_str, mode_P));
99
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)),
103                            linkage_ptr_info);
104
105   /* call procedure set_a, first built array with parameters */
106   {
107     ir_node *in[1];
108     in[0] = str_addr;
109     call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
110   }
111   /* make the possible changes by the called method to memory visible */
112   set_store(new_Proj(call, mode_M, 0));
113
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));
119
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));
125
126   finalize_cons (irg);
127
128   printf("Optimizing ...\n");
129   dead_node_elimination(irg);
130
131   /* verify the graph */
132   irg_vrfy(irg);
133
134   printf("Done building the graph.  Dumping it.\n");
135   dump_ir_block_graph (irg);
136   dump_all_types();
137   printf("Use xvcg to view this graph:\n");
138   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
139
140   return (0);
141 }