- implemented get_irg_value_param_type()
[libfirm] / testprograms / call_str_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/call_str_example.c
4  * Purpose:     Shows representation of constant string.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <string.h>
14 #include <stdio.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19  *  This file constructs the ir for the following pseudo-program:
20  *
21  *  void f(char *);
22  *
23  *  void CALL_STR_EXAMPLE_main () {
24  *      f("Hello World\n");
25  *  }
26  *
27  *  This program demonstrates how to represent string constants.
28  */
29
30 int main(void)
31 {
32   char *dump_file_suffix = "";
33   ir_graph *irg;         /* this variable contains the irgraph */
34   ir_type     *owner;       /* the class in which this method is defined */
35   ir_type     *proc_main;   /* ir_type information for the method main */
36   ir_type     *proc_called; /* ir_type information for called method f */
37   ir_type     *U8, *U8array, *string_ptr;  /* ir_type for pointers to strings. */
38   ir_entity   *ent;         /* represents this method as ir_entity of owner */
39   ir_entity   *const_str;   /* represents a constant string. */
40   char     *str = "Hello World\n"; /* The constant string. */
41   ir_node  *x, *str_addr, *proc_ptr, *call;
42   symconst_symbol sym;
43   size_t i;
44
45   printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
46
47   /* init library */
48   init_firm (NULL);
49
50   /* An unsinged 8 bit ir_type */
51   U8 = new_type_primitive (new_id_from_chars("char", 4), mode_Bu);
52   /* An array containing unsigned 8 bit elements. */
53   U8array = new_type_array (new_id_from_chars("char_arr", 8), 1, U8);
54   set_array_lower_bound_int(U8array, 0, 0);
55
56   string_ptr = new_type_pointer (new_id_from_chars ("ptr_to_string", 13), U8array, mode_P);
57
58   /* Make a global ir_entity that represents the constant String. */
59   const_str = new_entity(get_glob_type(), new_id_from_str("constStr"), U8array);
60   set_entity_variability(const_str, variability_constant);
61   for (i = 0; i < strlen(str); i++) {
62     tarval *val = new_tarval_from_long(str[i], mode_Bu);
63     ir_node *con =  new_Const(mode_Bu, val);
64     add_compound_ent_value(const_str, con, get_array_element_entity(U8array));
65   }
66
67   /* FIRM was designed for oo languages where all methods belong to a class.
68    * For imperative languages like C we view a program as a large class containing
69    * all functions of the program as methods in this class.  This class is
70    * automatically generated.
71    * We use the same name for the method ir_type as for the method ir_entity.
72    */
73 #define METHODNAME "CALL_STR_EXAMPLE_main"
74 #define NRARGS 0
75 #define NRES 0
76   owner = get_glob_type();
77   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
78                               NRARGS, NRES);
79
80   /* Make ir_type information for called method which also belongs to the
81      global ir_type. */
82 #define F_METHODNAME "f"
83 #define F_NRARGS 1
84 #define F_NRES 0
85   owner = get_glob_type();
86   proc_called = new_type_method(new_id_from_chars(F_METHODNAME, strlen(F_METHODNAME)),
87                               F_NRARGS, F_NRES);
88   set_method_param_type(proc_called, 0, string_ptr);
89
90
91   /* Make the ir_entity for main needed for a correct  ir_graph.  */
92 #define ENTITYNAME "CALL_STR_EXAMPLE_main"
93   ent = new_entity (owner, new_id_from_chars (ENTITYNAME, strlen(ENTITYNAME)),
94                     proc_main);
95
96   /* Generates the basic graph for the method represented by ir_entity ent, that
97    * is, generates start and end blocks and nodes and a first, initial block.
98    * The constructor needs to know how many local variables the method has.
99    */
100 #define NUM_OF_LOCAL_VARS 0
101   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
102
103   /* get the pointer to the string constant */
104   sym.entity_p = const_str;
105   str_addr = new_SymConst(mode_P, sym, symconst_addr_ent);
106
107   /* get the pointer to the procedure from the class ir_type */
108   /* this is how a pointer to be fixed by the linker is represented. */
109   sym.ident_p = new_id_from_str (F_METHODNAME);
110   proc_ptr = new_SymConst (mode_P, sym, symconst_addr_name);
111
112   /* call procedure set_a, first built array with parameters */
113   {
114     ir_node *in[1];
115     in[0] = str_addr;
116     call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
117   }
118   /* make the possible changes by the called method to memory visible */
119   set_store(new_Proj(call, mode_M, pn_Call_M));
120
121   /* Make the return node returning the memory. */
122   x = new_Return (get_store(), 0, NULL);
123   /* Now we generated all instructions for this block and all its predecessor blocks
124    * so we can mature it. */
125   mature_immBlock (get_irg_current_block(irg));
126
127   /* This adds the in edge of the end block which originates at the return statement.
128    * The return node passes controlflow to the end block.  */
129   add_immBlock_pred (get_irg_end_block(irg), x);
130   /* Now we can mature the end block as all it's predecessors are known. */
131   mature_immBlock (get_irg_end_block(irg));
132
133   irg_finalize_cons (irg);
134
135   printf("Optimizing ...\n");
136   dead_node_elimination(irg);
137
138   /* verify the graph */
139   irg_vrfy(irg);
140
141   printf("Done building the graph.  Dumping it.\n");
142   dump_ir_block_graph (irg, dump_file_suffix);
143   dump_all_types(dump_file_suffix);
144   printf("Use ycomp to view this graph:\n");
145   printf("ycomp GRAPHNAME\n\n");
146
147   return (0);
148 }