1 /* Copyright (C) 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Author: Goetz Lindenmaier
15 *** This file constructs type information for constant entities.
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.
25 *** Further the example shows the representation of a constant global
33 *** int[4] arre = (7, 2, 13, 92);
36 int main(int argc, char **argv)
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;
42 entity *ae, *fe, *ge, *dipte, *diptpe; /* e names entities */
45 printf("\nExample program for constant entites.\n");
46 printf("Creating type information...\n");
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"));
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);
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);
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);
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);
100 /*** Example with an array ***/
101 ident *arrei, *arrti;
103 entity *arre, *arrelte;
105 arrei = id_from_str("arr", strlen("arr"));
106 arrti = id_from_str("arr_t", strlen("arr_t"));
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);
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);
128 printf("Done building the graph. Dumping it.\n");
131 printf("use xvcg to view this graph:\n");
132 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");