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