Added support for constant entities. A new example program illustrates
[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("\nCreating type information...\n");
46
47   /** init library */
48   init_firm ();
49
50   /** make idents for all used identifiers in the program. */
51   Ci  = id_from_str("C",  strlen("C"));
52   ai  = id_from_str("a",  strlen("a"));
53   fi  = id_from_str("f",  strlen("f"));
54   fti  = id_from_str("f_type",  strlen("f_type"));
55   gi  = id_from_str("g",  strlen("g"));
56   gti  = id_from_str("g_type",  strlen("g_type"));
57   inti = id_from_str("int", strlen("int"));
58   dipti = id_from_str("C_dispatch_table_type", strlen("C_dispatch_table_type"));
59   diptei = id_from_str("C_dispatch_table", strlen("C_dispatch_table"));
60   diptpi = id_from_str("C_dispatch_table_p_type", strlen("C_dispatch_table_p_type"));
61   diptpei = id_from_str("C_dispatch_table_p", strlen("C_dispatch_table_p"));
62
63
64   /** make the type information needed */
65   /* Language defined types */
66   intt = new_type_primitive(inti, mode_i);
67   /* Program defined types */
68   Ct = new_type_class(Ci);
69   ft = new_type_method(fti, 0, 0);  /* 0 parameters, 0 results */
70   gt = new_type_method(gti, 1, 0);  /* 1 parameter, 0 results */
71   /* Compiler defined types: dispatch table and pointer to it  */
72   diptt = new_type_class(dipti);
73   diptpt = new_type_pointer(diptpi, diptt);
74   /** add structure to type graph **/
75   /* parameters of methods */
76   set_method_param_type(gt, 0, intt);
77
78   /** make entities **/
79   ae     = new_entity(Ct, ai, intt);
80   fe     = new_entity(diptt, fi, ft);
81   ge     = new_entity(diptt, gi, gt);
82   dipte  = new_entity(get_glob_type(), diptei, diptt);
83   diptpe = new_entity(Ct, diptpei, diptpt);
84
85   /** Add constant entity information **/
86   current_ir_graph = get_const_code_irg();
87   /* The pointer to the dispatch table is constant. */
88   /* The constant is the address of the given entity */
89   n = new_Const(mode_p, tarval_p_from_entity(dipte));
90   set_entity_variability(diptpe, constant);
91   set_atomic_ent_value(diptpe, n);
92
93   /* The entity representing the dispatch table is constant, too. */
94   set_entity_variability(dipte, constant);
95   add_compound_ent_value(dipte, get_atomic_ent_value(fe), fe);
96   add_compound_ent_value(dipte, get_atomic_ent_value(ge), ge);
97
98 {
99   /*** Example with an array ***/
100   ident *arrei, *arrti;
101   type *arrt;
102   entity *arre, *arrelte;
103
104   arrei =  id_from_str("arr", strlen("arr"));
105   arrti =  id_from_str("arr_t",  strlen("arr_t"));
106
107   /** The array type **/
108   /* Don't reuse int type so that graph layout is better readable */
109   intt = new_type_primitive(inti, mode_i);
110   arrt = new_type_array(arrti, 1, intt);
111   set_array_bounds_int(arrt, 0, 0, 4);
112   arrelte = get_array_element_entity(arrt);
113
114   /** The constant array entity **/
115   arre = new_entity(get_glob_type(), arrei, arrt);
116   set_entity_variability(arre, constant);
117   current_ir_graph = get_const_code_irg();
118   n = new_Const(mode_i, tarval_from_long (mode_i, 7));
119   add_compound_ent_value(arre, n, arrelte);
120   n = new_Const(mode_i, tarval_from_long (mode_i, 2));
121   add_compound_ent_value(arre, n, arrelte);
122   n = new_Const(mode_i, tarval_from_long (mode_i, 13));
123   add_compound_ent_value(arre, n, arrelte);
124   n = new_Const(mode_i, tarval_from_long (mode_i, 92));
125   add_compound_ent_value(arre, n, arrelte);
126 }
127   printf("Done building the graph.  Dumping it.\n");
128   dump_all_types();
129
130   printf("use xvcg to view this graph:\n");
131   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
132
133   return (0);
134 }