- moved peephole_IncSP_IncSP() to bepeephole.c, as this is a generic function and...
[libfirm] / testprograms / const_ent_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/const_ent_example.c
4  * Purpose:     Shows how to construct type information for constant entities.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 # include <stdio.h>
15 # include <string.h>
16
17 # include "irvrfy.h"
18 # include "irdump.h"
19 # include "firm.h"
20
21 /**
22  *  This file constructs type information for constant entities.
23  *
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.
31  *
32  *  Further the example shows the representation of a constant global
33  *  array.
34  *
35  *  class C {
36  *    int a;
37  *    void f();
38  *    void g(int);
39  *  }
40  *  int[4] arre = (7, 2, 13, 92);
41  **/
42
43 int main(int argc, char **argv)
44 {
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;
48       /*        t names types       */
49   entity *ae, *fe, *ge, *dipte, *diptpe;   /*        e names entities    */
50   ir_node *n;
51
52   printf("\nExample program for constant entites.\n");
53   printf("Creating type information...\n");
54
55   /** init library */
56   init_firm (NULL);
57
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"));
70
71
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);
85
86   /** make entities **/
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);
92
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 */
97   symconst_symbol sym;
98   sym.entity_p = dipte;
99   n = new_SymConst(sym, symconst_addr_ent);
100   set_entity_variability(diptpe, variability_constant);
101   set_atomic_ent_value(diptpe, n);
102
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);
107
108 {
109   /*** Example with an array ***/
110   ident *arrei, *arrti;
111   type *arrt;
112   entity *arre, *arrelte;
113
114   arrei =  new_id_from_chars("arr", strlen("arr"));
115   arrti =  new_id_from_chars("arr_t",  strlen("arr_t"));
116
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);
123
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);
136 }
137   printf("Done building the graph.  Dumping it.\n");
138   dump_all_types(0);
139
140   printf("use xvcg to view this graph:\n");
141   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
142
143   return (0);
144 }