Updated header
[libfirm] / ir / tr / tpop.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Opcode of types.
23  * @author  Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "xmalloc.h"
31 #include "tpop_t.h"
32 #include "type_t.h"
33
34 tp_op *type_class;         tp_op *get_tpop_class      (void) { return type_class;       }
35 tp_op *type_struct;        tp_op *get_tpop_struct     (void) { return type_struct;      }
36 tp_op *type_method;        tp_op *get_tpop_method     (void) { return type_method;      }
37 tp_op *type_union;         tp_op *get_tpop_union      (void) { return type_union;       }
38 tp_op *type_array;         tp_op *get_tpop_array      (void) { return type_array;       }
39 tp_op *type_enumeration;   tp_op *get_tpop_enumeration(void) { return type_enumeration; }
40 tp_op *type_pointer;       tp_op *get_tpop_pointer    (void) { return type_pointer;     }
41 tp_op *type_primitive;     tp_op *get_tpop_primitive  (void) { return type_primitive;   }
42 tp_op *type_id;            tp_op *get_tpop_id         (void) { return type_id;          }
43 tp_op *tpop_none;          tp_op *get_tpop_none       (void) { return tpop_none;        }
44 tp_op *tpop_unknown;       tp_op *get_tpop_unknown    (void) { return tpop_unknown;     }
45
46 tp_op *
47 new_tpop(tp_opcode code, ident *name, unsigned flags, size_t attr_size,
48          const tp_op_ops *ops)
49 {
50         tp_op *res;
51
52         res = xmalloc(sizeof(*res));
53         res->code          = code;
54         res->name          = name;
55         res->flags         = flags;
56         res->attr_size     = attr_size;
57
58         if (ops)
59                 memcpy(&res->ops, ops, sizeof(res->ops));
60         else
61                 memset(&res->ops, 0, sizeof(res->ops));
62
63         return res;
64 }
65
66 void
67 free_tpop(tp_op *tpop) {
68         free(tpop);
69 }
70
71 static const tp_op_ops
72         /** tpop operations for class types */
73         class_ops = {
74                 free_class_attrs,
75                 free_class_entities,
76                 NULL,
77                 set_class_mode,
78                 set_class_size_bits,
79                 get_class_n_members,
80                 get_class_member,
81                 get_class_member_index
82         },
83         /** tpop operations for struct types */
84         struct_ops = {
85                 free_struct_attrs,
86                 free_struct_entities,
87                 NULL,
88                 set_struct_mode,
89                 set_struct_size_bits,
90                 get_struct_n_members,
91                 get_struct_member,
92                 get_struct_member_index
93         },
94         /** tpop operations for method types */
95         method_ops = {
96                 free_method_attrs,
97                 free_method_entities,
98                 NULL,
99                 NULL,
100                 NULL,
101                 NULL,
102                 NULL,
103                 NULL
104                 },
105         /** tpop operations for union types */
106         union_ops = {
107                 free_union_attrs,
108                 free_union_entities,
109                 NULL,
110                 NULL,
111                 set_union_size_bits,
112                 get_union_n_members,
113                 get_union_member,
114                 get_union_member_index
115         },
116         /** tpop operations for array types */
117         array_ops = {
118                 free_array_attrs,
119                 free_array_entities,
120                 free_array_automatic_entities,
121                 NULL,
122                 set_array_size_bits,
123                 NULL,
124                 NULL,
125                 NULL
126         },
127         /** tpop operations for enumeration types */
128         enum_ops = {
129                 free_enumeration_attrs,
130                 free_enumeration_entities,
131                 NULL,
132                 set_enumeration_mode,
133                 NULL,
134                 NULL,
135                 NULL,
136                 NULL
137         },
138         /** tpop operations for pointer types */
139         pointer_ops = {
140                 free_pointer_attrs,
141                 free_pointer_entities,
142                 NULL,
143                 set_pointer_mode,
144                 NULL,
145                 NULL,
146                 NULL,
147                 NULL
148         },
149         /** tpop operations for pseudo types */
150         pseudo_ops = {
151                 NULL,
152                 NULL,
153                 NULL,
154                 NULL,
155                 set_default_size_bits,
156                 NULL,
157                 NULL,
158                 NULL
159         },
160         /** tpop operations for primitive types */
161         null_ops = {
162                 NULL,
163                 NULL,
164                 NULL,
165                 NULL,
166                 NULL,
167                 NULL,
168                 NULL,
169                 NULL
170         };
171
172 #define C     TP_OP_FLAG_COMPOUND
173 #define ID(s) new_id_from_chars(s, sizeof(s) - 1)
174
175 void init_tpop(void) {
176         type_class       = new_tpop(tpo_class      , ID("class"),       C, sizeof (cls_attr), &class_ops);
177         type_struct      = new_tpop(tpo_struct     , ID("struct"),      C, sizeof (stc_attr), &struct_ops);
178         type_method      = new_tpop(tpo_method     , ID("method"),      0, sizeof (mtd_attr), &method_ops);
179         type_union       = new_tpop(tpo_union      , ID("union"),       C, sizeof (uni_attr), &union_ops);
180         type_array       = new_tpop(tpo_array      , ID("array"),       C, sizeof (arr_attr), &array_ops);
181         type_enumeration = new_tpop(tpo_enumeration, ID("enumeration"), 0, sizeof (enm_attr), &enum_ops);
182         type_pointer     = new_tpop(tpo_pointer    , ID("pointer"),     0, sizeof (ptr_attr), &pointer_ops);
183         type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0, /* sizeof (pri_attr) */ 0, &null_ops);
184         type_id          = new_tpop(tpo_id         , ID("type_id"),     0, /* sizeof (id_attr)  */ 0, &null_ops);
185         tpop_none        = new_tpop(tpo_none       , ID("None"),        0, /* sizeof (non_attr) */ 0, &pseudo_ops);
186         tpop_unknown     = new_tpop(tpo_unknown    , ID("Unknown"),     0, /* sizeof (ukn_attr) */ 0, &pseudo_ops);
187 }
188 #undef ID
189 #undef C
190
191 /* Finalize the tpop module.
192  * Frees all type opcodes.  */
193 void finish_tpop(void) {
194         free_tpop(type_class      ); type_class       = NULL;
195         free_tpop(type_struct     ); type_struct      = NULL;
196         free_tpop(type_method     ); type_method      = NULL;
197         free_tpop(type_union      ); type_union       = NULL;
198         free_tpop(type_array      ); type_array       = NULL;
199         free_tpop(type_enumeration); type_enumeration = NULL;
200         free_tpop(type_pointer    ); type_pointer     = NULL;
201         free_tpop(type_primitive  ); type_primitive   = NULL;
202         free_tpop(type_id         ); type_id          = NULL;
203         free_tpop(tpop_none       ); tpop_none        = NULL;
204         free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
205         }
206
207 /* Returns the string for the tp_opcode. */
208 const char  *get_tpop_name(const tp_op *op) {
209         return get_id_str(op->name);
210 }
211
212 tp_opcode (get_tpop_code)(const tp_op *op) {
213         return _get_tpop_code(op);
214 }
215
216 ident *(get_tpop_ident)(const tp_op *op) {
217         return _get_tpop_ident(op);
218 }
219
220 /* returns the attribute size of the operator. */
221 int (get_tpop_attr_size)(const tp_op *op) {
222         return _get_tpop_attr_size(op);
223 }