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