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