stupid encoding error on i44pc47
[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, *at, *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  = id_from_str("C",  strlen("C"));
60   ai  = id_from_str("a",  strlen("a"));
61   fi  = id_from_str("f",  strlen("f"));
62   fti  = id_from_str("f_type",  strlen("f_type"));
63   gi  = id_from_str("g",  strlen("g"));
64   gti  = id_from_str("g_type",  strlen("g_type"));
65   inti = id_from_str("int", strlen("int"));
66   dipti = id_from_str("C_dispatch_table_type", strlen("C_dispatch_table_type"));
67   diptei = id_from_str("C_dispatch_table", strlen("C_dispatch_table"));
68   diptpi = id_from_str("C_dispatch_table_p_type", strlen("C_dispatch_table_p_type"));
69   diptpei = id_from_str("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);
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 <<<<<<< const_ent_example.c
98   n = new_Const(mode_P, new_tarval_from_entity(dipte, mode_P));
99 =======
100   n = new_Const(mode_P, tarval_P_from_entity(dipte));
101 >>>>>>> 1.7
102   set_entity_variability(diptpe, constant);
103   set_atomic_ent_value(diptpe, n);
104
105   /* The entity representing the dispatch table is constant, too. */
106   set_entity_variability(dipte, constant);
107   add_compound_ent_value(dipte, get_atomic_ent_value(fe), fe);
108   add_compound_ent_value(dipte, get_atomic_ent_value(ge), ge);
109
110 {
111   /*** Example with an array ***/
112   ident *arrei, *arrti;
113   type *arrt;
114   entity *arre, *arrelte;
115
116   arrei =  id_from_str("arr", strlen("arr"));
117   arrti =  id_from_str("arr_t",  strlen("arr_t"));
118
119   /** The array type **/
120   /* Don't reuse int type so that graph layout is better readable */
121   intt = new_type_primitive(inti, mode_Is);
122   arrt = new_type_array(arrti, 1, intt);
123   set_array_bounds_int(arrt, 0, 0, 4);
124   arrelte = get_array_element_entity(arrt);
125
126   /** The constant array entity **/
127   arre = new_entity(get_glob_type(), arrei, arrt);
128   set_entity_variability(arre, constant);
129   current_ir_graph = get_const_code_irg();
130   n = new_Const(mode_Is, new_tarval_from_long (7, mode_Is));
131   add_compound_ent_value(arre, n, arrelte);
132   n = new_Const(mode_Is, new_tarval_from_long (2, mode_Is));
133   add_compound_ent_value(arre, n, arrelte);
134   n = new_Const(mode_Is, new_tarval_from_long (13, mode_Is));
135   add_compound_ent_value(arre, n, arrelte);
136   n = new_Const(mode_Is, new_tarval_from_long (92, mode_Is));
137   add_compound_ent_value(arre, n, arrelte);
138 }
139   printf("Done building the graph.  Dumping it.\n");
140   dump_all_types();
141
142   printf("use xvcg to view this graph:\n");
143   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
144
145   return (0);
146 }