Convert comments to doxygen
[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 # include <string.h>
11
12 # include "irvrfy.h"
13 # include "irdump.h"
14 # include "firm.h"
15
16 /**
17 *  This file constructs type information for constant entities.
18 *
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.
26 *
27 *  Further the example shows the representation of a constant global
28 *  array.
29 *
30 *  class C {
31 *    int a;
32 *    void f();
33 *    void g(int);
34 *  }
35 *  int[4] arre = (7, 2, 13, 92);
36 **/
37
38 int main(int argc, char **argv)
39 {
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;
43       /*        t names types       */
44   entity *ae, *fe, *ge, *dipte, *diptpe;   /*        e names entities    */
45   ir_node *n;
46
47   printf("\nExample program for constant entites.\n");
48   printf("Creating type information...\n");
49
50   /** init library */
51   init_firm ();
52
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"));
65
66
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);
80
81   /** make entities **/
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);
87
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);
95
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);
100
101 {
102   /*** Example with an array ***/
103   ident *arrei, *arrti;
104   type *arrt;
105   entity *arre, *arrelte;
106
107   arrei =  id_from_str("arr", strlen("arr"));
108   arrti =  id_from_str("arr_t",  strlen("arr_t"));
109
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);
116
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);
129 }
130   printf("Done building the graph.  Dumping it.\n");
131   dump_all_types();
132
133   printf("use xvcg to view this graph:\n");
134   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
135
136   return (0);
137 }