3 * File name: testprograms/const_ent_example.c
4 * Purpose: Shows how to construct type information for constant entities.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 * This file constructs type information for constant entities.
24 * It constructs the information for a class type with a dispatch
25 * table. The class has a field a, and two methods f and g. The
26 * class is represented by a class type with two entities for the
27 * field a and the reference to the dispatch table. This reference
28 * is a constant entity. Ther dispatch table is also represented
29 * by a class type that contains the two methods. There is one entity
30 * of the dispatch table which is constant.
32 * Further the example shows the representation of a constant global
40 * int[4] arre = (7, 2, 13, 92);
43 int main(int argc, char **argv)
45 ident *Ci, *ai, *fi, *fti, *gi, *gti, *inti, *dipti, *diptpi, *diptpei, *diptei;
46 /* suffix i names identifiers */
47 type *Ct, *intt, *ft, *gt, *diptt, *diptpt;
49 entity *ae, *fe, *ge, *dipte, *diptpe; /* e names entities */
52 printf("\nExample program for constant entites.\n");
53 printf("Creating type information...\n");
58 /** make idents for all used identifiers in the program. */
59 Ci = new_id_from_chars("C", strlen("C"));
60 ai = new_id_from_chars("a", strlen("a"));
61 fi = new_id_from_chars("f", strlen("f"));
62 fti = new_id_from_chars("f_type", strlen("f_type"));
63 gi = new_id_from_chars("g", strlen("g"));
64 gti = new_id_from_chars("g_type", strlen("g_type"));
65 inti = new_id_from_chars("int", strlen("int"));
66 dipti = new_id_from_chars("C_dispatch_table_type", strlen("C_dispatch_table_type"));
67 diptei = new_id_from_chars("C_dispatch_table", strlen("C_dispatch_table"));
68 diptpi = new_id_from_chars("C_dispatch_table_p_type", strlen("C_dispatch_table_p_type"));
69 diptpei = new_id_from_chars("C_dispatch_table_p", strlen("C_dispatch_table_p"));
72 /** make the type information needed */
73 /* Language defined types */
74 intt = new_type_primitive(inti, mode_Is);
75 /* Program defined types */
76 Ct = new_type_class(Ci);
77 ft = new_type_method(fti, 0, 0); /* 0 parameters, 0 results */
78 gt = new_type_method(gti, 1, 0); /* 1 parameter, 0 results */
79 /* Compiler defined types: dispatch table and pointer to it */
80 diptt = new_type_class(dipti);
81 diptpt = new_type_pointer(diptpi, diptt, mode_P);
82 /** add structure to type graph **/
83 /* parameters of methods */
84 set_method_param_type(gt, 0, intt);
87 ae = new_entity(Ct, ai, intt);
88 fe = new_entity(diptt, fi, ft);
89 ge = new_entity(diptt, gi, gt);
90 dipte = new_entity(get_glob_type(), diptei, diptt);
91 diptpe = new_entity(Ct, diptpei, diptpt);
93 /** Add constant entity information **/
94 current_ir_graph = get_const_code_irg();
95 /* The pointer to the dispatch table is constant. */
96 /* The constant is the address of the given entity */
99 n = new_SymConst(sym, symconst_addr_ent);
100 set_entity_variability(diptpe, variability_constant);
101 set_atomic_ent_value(diptpe, n);
103 /* The entity representing the dispatch table is constant, too. */
104 set_entity_variability(dipte, variability_constant);
105 add_compound_ent_value(dipte, get_atomic_ent_value(fe), fe);
106 add_compound_ent_value(dipte, get_atomic_ent_value(ge), ge);
109 /*** Example with an array ***/
110 ident *arrei, *arrti;
112 entity *arre, *arrelte;
114 arrei = new_id_from_chars("arr", strlen("arr"));
115 arrti = new_id_from_chars("arr_t", strlen("arr_t"));
117 /** The array type **/
118 /* Don't reuse int type so that graph layout is better readable */
119 intt = new_type_primitive(inti, mode_Is);
120 arrt = new_type_array(arrti, 1, intt);
121 set_array_bounds_int(arrt, 0, 0, 4);
122 arrelte = get_array_element_entity(arrt);
124 /** The constant array entity **/
125 arre = new_entity(get_glob_type(), arrei, arrt);
126 set_entity_variability(arre, variability_constant);
127 current_ir_graph = get_const_code_irg();
128 n = new_Const(mode_Is, new_tarval_from_long (7, mode_Is));
129 add_compound_ent_value(arre, n, arrelte);
130 n = new_Const(mode_Is, new_tarval_from_long (2, mode_Is));
131 add_compound_ent_value(arre, n, arrelte);
132 n = new_Const(mode_Is, new_tarval_from_long (13, mode_Is));
133 add_compound_ent_value(arre, n, arrelte);
134 n = new_Const(mode_Is, new_tarval_from_long (92, mode_Is));
135 add_compound_ent_value(arre, n, arrelte);
137 printf("Done building the graph. Dumping it.\n");
140 printf("use xvcg to view this graph:\n");
141 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");