1 /* Copyright (C) 2000 by Universitaet Karlsruhe
4 * Author: Goetz Lindenmaier
17 * This file constructs type information for constant entities.
19 * It constructs the information for a class type with a dispatch
20 * table. The class has a field a, and two methods f and g. The
21 * class is represented by a class type with two entities for the
22 * field a and the reference to the dispatch table. This reference
23 * is a constant entity. Ther dispatch table is also represented
24 * by a class type that contains the two methods. There is one entity
25 * of the dispatch table which is constant.
27 * Further the example shows the representation of a constant global
35 * int[4] arre = (7, 2, 13, 92);
38 int main(int argc, char **argv)
40 ident *Ci, *ai, *fi, *fti, *gi, *gti, *inti, *dipti, *diptpi, *diptpei, *diptei;
41 /* suffix i names identifiers */
42 type *Ct, *intt, *at, *ft, *gt, *diptt, *diptpt;
44 entity *ae, *fe, *ge, *dipte, *diptpe; /* e names entities */
47 printf("\nExample program for constant entites.\n");
48 printf("Creating type information...\n");
53 /** make idents for all used identifiers in the program. */
54 Ci = id_from_str("C", strlen("C"));
55 ai = id_from_str("a", strlen("a"));
56 fi = id_from_str("f", strlen("f"));
57 fti = id_from_str("f_type", strlen("f_type"));
58 gi = id_from_str("g", strlen("g"));
59 gti = id_from_str("g_type", strlen("g_type"));
60 inti = id_from_str("int", strlen("int"));
61 dipti = id_from_str("C_dispatch_table_type", strlen("C_dispatch_table_type"));
62 diptei = id_from_str("C_dispatch_table", strlen("C_dispatch_table"));
63 diptpi = id_from_str("C_dispatch_table_p_type", strlen("C_dispatch_table_p_type"));
64 diptpei = id_from_str("C_dispatch_table_p", strlen("C_dispatch_table_p"));
67 /** make the type information needed */
68 /* Language defined types */
69 intt = new_type_primitive(inti, mode_Is);
70 /* Program defined types */
71 Ct = new_type_class(Ci);
72 ft = new_type_method(fti, 0, 0); /* 0 parameters, 0 results */
73 gt = new_type_method(gti, 1, 0); /* 1 parameter, 0 results */
74 /* Compiler defined types: dispatch table and pointer to it */
75 diptt = new_type_class(dipti);
76 diptpt = new_type_pointer(diptpi, diptt);
77 /** add structure to type graph **/
78 /* parameters of methods */
79 set_method_param_type(gt, 0, intt);
82 ae = new_entity(Ct, ai, intt);
83 fe = new_entity(diptt, fi, ft);
84 ge = new_entity(diptt, gi, gt);
85 dipte = new_entity(get_glob_type(), diptei, diptt);
86 diptpe = new_entity(Ct, diptpei, diptpt);
88 /** Add constant entity information **/
89 current_ir_graph = get_const_code_irg();
90 /* The pointer to the dispatch table is constant. */
91 /* The constant is the address of the given entity */
92 n = new_Const(mode_P, tarval_P_from_entity(dipte));
93 set_entity_variability(diptpe, constant);
94 set_atomic_ent_value(diptpe, n);
96 /* The entity representing the dispatch table is constant, too. */
97 set_entity_variability(dipte, constant);
98 add_compound_ent_value(dipte, get_atomic_ent_value(fe), fe);
99 add_compound_ent_value(dipte, get_atomic_ent_value(ge), ge);
102 /*** Example with an array ***/
103 ident *arrei, *arrti;
105 entity *arre, *arrelte;
107 arrei = id_from_str("arr", strlen("arr"));
108 arrti = id_from_str("arr_t", strlen("arr_t"));
110 /** The array type **/
111 /* Don't reuse int type so that graph layout is better readable */
112 intt = new_type_primitive(inti, mode_Is);
113 arrt = new_type_array(arrti, 1, intt);
114 set_array_bounds_int(arrt, 0, 0, 4);
115 arrelte = get_array_element_entity(arrt);
117 /** The constant array entity **/
118 arre = new_entity(get_glob_type(), arrei, arrt);
119 set_entity_variability(arre, constant);
120 current_ir_graph = get_const_code_irg();
121 n = new_Const(mode_Is, tarval_from_long (mode_Is, 7));
122 add_compound_ent_value(arre, n, arrelte);
123 n = new_Const(mode_Is, tarval_from_long (mode_Is, 2));
124 add_compound_ent_value(arre, n, arrelte);
125 n = new_Const(mode_Is, tarval_from_long (mode_Is, 13));
126 add_compound_ent_value(arre, n, arrelte);
127 n = new_Const(mode_Is, tarval_from_long (mode_Is, 92));
128 add_compound_ent_value(arre, n, arrelte);
130 printf("Done building the graph. Dumping it.\n");
133 printf("use xvcg to view this graph:\n");
134 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");