changed ir_vrfy and vrfy_graph to irn_vrfy and irg_vrfy!
[libfirm] / testprograms / array-stack_example.c
1  /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 # include "irdump.h"
10 # include "firm.h"
11
12 /**  This example describes representation of stack allocated variables of
13 ***  imperative programs.
14 ***  It constructs the IR for the following program:
15 ***
16 ***
17 ***  main(): int
18 ***    int a[10];
19 ***
20 ***    return (a[3]);
21 ***  end;
22 ***
23 ***  The array is placed on the stack, i.e., a pointer to the array
24 ***  is obtained by selecting the entity "a" from the stack.  The variables
25 ***  on the stack are considered to be entities of the method, as locals
26 ***  of a method are only visible within the method.  (An alternative to
27 ***  make the method owner of the stack variables is to give the ownership
28 ***  to the class representing the C-file.  This would extend the visibility
29 ***  of the locals, though.)
30 **/
31
32
33 #define OPTIMIZE_NODE 0
34
35 int
36 main(void)
37 {
38   /* describes the general structure of a C-file */
39   type_class     *owner;        /* the class standing for everything in this file */
40   type_method    *proc_main;    /* Typeinformation for method main. */
41   entity         *proc_main_e;  /* The entity describing that method main is an
42                                    entity of the fake class representing the file. */
43
44   /* describes types defined by the language */
45   type_primitive *prim_t_int;
46
47   /* describes the array and its fields. */
48   entity         *array_ent;    /* the entity representing the array as member
49                                    of the stack/method */
50   type_array     *array_type;   /* the type information for the array */
51   entity         *field_ent;    /* the entity representing a field of the array */
52
53   /* Needed while finding the element size.  */
54   type_primitive *elt_type;
55   ir_mode        *elt_type_mode;
56   int            size;
57   ir_node        *arr_size;
58
59   /* holds the graph and nodes. */
60   ir_graph       *main_irg;
61   ir_node        *array, *array_ptr, *c3, *elt, *val, *x;
62
63
64   init_firm ();
65
66   printf("creating an IR graph: ARRAY-STACK_EXAMPLE...\n");
67
68   /* make basic type information for primitive type int.
69      In Sather primitive types are represented by a class.
70      This is the modeling appropriate for other languages.
71      Mode_i says that all language-integers shall be implemented
72      as a 32 bit processor-integer value.  */
73   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
74
75   /* build typeinformation of procedure main */
76   owner = new_type_class (id_from_str ("ARRAY-STACK_EXAMPLE", 19));
77   proc_main = new_type_method(id_from_str("main", 4), 0, 1);
78   set_method_res_type(proc_main, 0, (type *)prim_t_int);
79   proc_main_e = new_entity ((type*)owner, id_from_str ("main", 4), (type *)proc_main);
80
81   main_irg = new_ir_graph (proc_main_e, 4);
82
83   /* make type information for the array and set the bounds */
84 # define N_DIMS 1
85 # define L_BOUND 0
86 # define U_BOUND 9
87   array_type = new_type_array(id_from_str("a", 1), N_DIMS);
88   set_array_bounds(array_type, 1, L_BOUND, U_BOUND);
89   set_array_element_type(array_type, (type*)prim_t_int);
90   /* The array is an entity of the method, placed on the mehtod's own memory,
91      the stack frame. */
92   array_ent = new_entity((type *)proc_main, id_from_str("a", 1), (type *)array_type);
93   /* As the array is accessed by Sel nodes, we need information about
94      the entity the node select.  Entities of an array are it's elements
95      which are, in this case, integers. */
96   /* change entity owner types.   */
97   field_ent = new_entity((type*)array_type, id_from_str("array_field", 11), (type*)prim_t_int);
98
99   /* Now the "real" program: */
100   /* Select the array from the stack frame.  */
101   array_ptr = new_simpleSel(get_store(), main_irg->frame, array_ent);
102   /* Load element 3 of the array. For this first generate the pointer
103      to this the element by a select node.  (Alternative: increase
104      array pointer by (three * elt_size), but this complicates some
105      optimizations.) The type information accessible via the entity
106      allows to generate the pointer increment later. */
107   c3 = new_Const (mode_I, tarval_from_long (mode_I, 3));
108   {
109      ir_node *in[1];
110      in[0] = c3;
111      elt = new_Sel(get_store(), array_ptr, 1, in, field_ent);
112   }
113   val = new_Load(get_store(), elt);
114   set_store(new_Proj(val, mode_M, 0));
115   val = new_Proj(val, mode_i, 1);
116
117   /* return the result of procedure main */
118   {
119      ir_node *in[1];
120      in[0] = val;
121
122      x = new_Return (get_store (), 1, in);
123   }
124   mature_block (main_irg->current_block);
125
126   /* complete the end_block */
127   add_in_edge (main_irg->end_block, x);
128   mature_block (main_irg->end_block);
129
130   /* verify the graph */
131   irg_vrfy(main_irg);
132
133   printf("\nDone building the graph.\n");
134   printf("Dumping the graph and a type graph.\n");
135   dump_ir_block_graph (main_irg);
136   dump_type_graph(main_irg);
137
138   printf("\nuse xvcg to view these graphs:\n");
139   printf("/ben/goetz/bin/xvcg GRAPHNAME\n");
140
141   return (1);
142 }