3 * File name: testprograms/const_ent_example.c
4 * Purpose: Shows how to construct ir_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.
19 #include <libfirm/firm.h>
22 * This file constructs ir_type information for constant entities.
24 * It constructs the information for a class ir_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 ir_type with two entities for the
27 * field a and the reference to the dispatch table. This reference
28 * is a constant ir_entity. Ther dispatch table is also represented
29 * by a class ir_type that contains the two methods. There is one ir_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);
45 ident *Ci, *ai, *fi, *fti, *gi, *gti, *inti, *dipti, *diptpi, *diptpei, *diptei;
46 /* suffix i names identifiers */
47 ir_type *Ct, *intt, *ft, *gt, *diptt, *diptpt;
49 ir_entity *ae, *fe, *ge, *dipte, *diptpe; /* e names entities */
53 printf("\nExample program for constant entites.\n");
54 printf("Creating ir_type information...\n");
59 /** make idents for all used identifiers in the program. */
60 Ci = new_id_from_chars("C", strlen("C"));
61 ai = new_id_from_chars("a", strlen("a"));
62 fi = new_id_from_chars("f", strlen("f"));
63 fti = new_id_from_chars("f_type", strlen("f_type"));
64 gi = new_id_from_chars("g", strlen("g"));
65 gti = new_id_from_chars("g_type", strlen("g_type"));
66 inti = new_id_from_chars("int", strlen("int"));
67 dipti = new_id_from_chars("C_dispatch_table_type", strlen("C_dispatch_table_type"));
68 diptei = new_id_from_chars("C_dispatch_table", strlen("C_dispatch_table"));
69 diptpi = new_id_from_chars("C_dispatch_table_p_type", strlen("C_dispatch_table_p_type"));
70 diptpei = new_id_from_chars("C_dispatch_table_p", strlen("C_dispatch_table_p"));
73 /** make the ir_type information needed */
74 /* Language defined types */
75 intt = new_type_primitive(inti, mode_Is);
76 /* Program defined types */
77 Ct = new_type_class(Ci);
78 ft = new_type_method(fti, 0, 0); /* 0 parameters, 0 results */
79 gt = new_type_method(gti, 1, 0); /* 1 parameter, 0 results */
80 /* Compiler defined types: dispatch table and pointer to it */
81 diptt = new_type_class(dipti);
82 diptpt = new_type_pointer(diptpi, diptt, mode_P);
83 /** add structure to ir_type graph **/
84 /* parameters of methods */
85 set_method_param_type(gt, 0, intt);
88 ae = new_entity(Ct, ai, intt);
89 fe = new_entity(diptt, fi, ft);
90 ge = new_entity(diptt, gi, gt);
91 dipte = new_entity(get_glob_type(), diptei, diptt);
92 diptpe = new_entity(Ct, diptpei, diptpt);
94 /** Add constant ir_entity information **/
95 current_ir_graph = get_const_code_irg();
96 /* The pointer to the dispatch table is constant. */
97 /* The constant is the address of the given ir_entity */
99 n = new_SymConst(mode_P, sym, symconst_addr_ent);
100 set_entity_variability(diptpe, variability_constant);
101 set_atomic_ent_value(diptpe, n);
103 /* The ir_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 ir_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 ir_type **/
118 /* Don't reuse int ir_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 ir_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 ycomp to view this graph:\n");
141 printf("ycomp GRAPHNAME\n\n");