block walk in irouts.
[libfirm] / testprograms / const_ent_example.c
1 /* Copyright (C) 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Author: Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 #include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /**
15 ***  This file constructs type information for constant entities.
16 ***
17 ***  It constructs the information for a class type with a dispatch
18 ***  table.  The class has a field a, and two methods f and g.  The
19 ***  class is represented by a class type with two entities for the
20 ***  field a and the reference to the dispatch table.  This reference
21 ***  is a constant entity.  Ther dispatch table is also represented
22 ***  by a class type that contains the two methods.   There is one entity
23 ***  of the dispatch table which is constant.
24 ***
25 ***  Further the example shows the representation of a constant global
26 ***  array.
27 ***
28 ***  class C {
29 ***    int a;
30 ***    void f();
31 ***    void g(int);
32 ***  }
33 ***  int[4] arre = (7, 2, 13, 92);
34 **/
35
36 int main(int argc, char **argv)
37 {
38   ident *Ci, *ai, *fi, *fti, *gi, *gti, *inti, *dipti, *diptpi, *diptpei, *diptei;
39       /* suffix i names identifiers */
40   type  *Ct, *intt, *at, *ft, *gt, *diptt, *diptpt;
41       /*        t names types       */
42   entity *ae, *fe, *ge, *dipte, *diptpe;   /*        e names entities    */
43   ir_node *n;
44
45   printf("\nExample program for constant entites.\n");
46   printf("Creating type information...\n");
47
48   /** init library */
49   init_firm ();
50
51   /** make idents for all used identifiers in the program. */
52   Ci  = id_from_str("C",  strlen("C"));
53   ai  = id_from_str("a",  strlen("a"));
54   fi  = id_from_str("f",  strlen("f"));
55   fti  = id_from_str("f_type",  strlen("f_type"));
56   gi  = id_from_str("g",  strlen("g"));
57   gti  = id_from_str("g_type",  strlen("g_type"));
58   inti = id_from_str("int", strlen("int"));
59   dipti = id_from_str("C_dispatch_table_type", strlen("C_dispatch_table_type"));
60   diptei = id_from_str("C_dispatch_table", strlen("C_dispatch_table"));
61   diptpi = id_from_str("C_dispatch_table_p_type", strlen("C_dispatch_table_p_type"));
62   diptpei = id_from_str("C_dispatch_table_p", strlen("C_dispatch_table_p"));
63
64
65   /** make the type information needed */
66   /* Language defined types */
67   intt = new_type_primitive(inti, mode_i);
68   /* Program defined types */
69   Ct = new_type_class(Ci);
70   ft = new_type_method(fti, 0, 0);  /* 0 parameters, 0 results */
71   gt = new_type_method(gti, 1, 0);  /* 1 parameter, 0 results */
72   /* Compiler defined types: dispatch table and pointer to it  */
73   diptt = new_type_class(dipti);
74   diptpt = new_type_pointer(diptpi, diptt);
75   /** add structure to type graph **/
76   /* parameters of methods */
77   set_method_param_type(gt, 0, intt);
78
79   /** make entities **/
80   ae     = new_entity(Ct, ai, intt);
81   fe     = new_entity(diptt, fi, ft);
82   ge     = new_entity(diptt, gi, gt);
83   dipte  = new_entity(get_glob_type(), diptei, diptt);
84   diptpe = new_entity(Ct, diptpei, diptpt);
85
86   /** Add constant entity information **/
87   current_ir_graph = get_const_code_irg();
88   /* The pointer to the dispatch table is constant. */
89   /* The constant is the address of the given entity */
90   n = new_Const(mode_p, tarval_p_from_entity(dipte));
91   set_entity_variability(diptpe, constant);
92   set_atomic_ent_value(diptpe, n);
93
94   /* The entity representing the dispatch table is constant, too. */
95   set_entity_variability(dipte, constant);
96   add_compound_ent_value(dipte, get_atomic_ent_value(fe), fe);
97   add_compound_ent_value(dipte, get_atomic_ent_value(ge), ge);
98
99 {
100   /*** Example with an array ***/
101   ident *arrei, *arrti;
102   type *arrt;
103   entity *arre, *arrelte;
104
105   arrei =  id_from_str("arr", strlen("arr"));
106   arrti =  id_from_str("arr_t",  strlen("arr_t"));
107
108   /** The array type **/
109   /* Don't reuse int type so that graph layout is better readable */
110   intt = new_type_primitive(inti, mode_i);
111   arrt = new_type_array(arrti, 1, intt);
112   set_array_bounds_int(arrt, 0, 0, 4);
113   arrelte = get_array_element_entity(arrt);
114
115   /** The constant array entity **/
116   arre = new_entity(get_glob_type(), arrei, arrt);
117   set_entity_variability(arre, constant);
118   current_ir_graph = get_const_code_irg();
119   n = new_Const(mode_i, tarval_from_long (mode_i, 7));
120   add_compound_ent_value(arre, n, arrelte);
121   n = new_Const(mode_i, tarval_from_long (mode_i, 2));
122   add_compound_ent_value(arre, n, arrelte);
123   n = new_Const(mode_i, tarval_from_long (mode_i, 13));
124   add_compound_ent_value(arre, n, arrelte);
125   n = new_Const(mode_i, tarval_from_long (mode_i, 92));
126   add_compound_ent_value(arre, n, arrelte);
127 }
128   printf("Done building the graph.  Dumping it.\n");
129   dump_all_types();
130
131   printf("use xvcg to view this graph:\n");
132   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
133
134   return (0);
135 }