remove #ifdef HAVE_CONFIG_Hs
[libfirm] / ir / tr / tpop.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
27
28 #include "xmalloc.h"
29 #include "tpop_t.h"
30 #include "type_t.h"
31
32 tp_op *type_class;         tp_op *get_tpop_class      (void) { return type_class;       }
33 tp_op *type_struct;        tp_op *get_tpop_struct     (void) { return type_struct;      }
34 tp_op *type_method;        tp_op *get_tpop_method     (void) { return type_method;      }
35 tp_op *type_union;         tp_op *get_tpop_union      (void) { return type_union;       }
36 tp_op *type_array;         tp_op *get_tpop_array      (void) { return type_array;       }
37 tp_op *type_enumeration;   tp_op *get_tpop_enumeration(void) { return type_enumeration; }
38 tp_op *type_pointer;       tp_op *get_tpop_pointer    (void) { return type_pointer;     }
39 tp_op *type_primitive;     tp_op *get_tpop_primitive  (void) { return type_primitive;   }
40 tp_op *type_id;            tp_op *get_tpop_id         (void) { return type_id;          }
41 tp_op *tpop_none;          tp_op *get_tpop_none       (void) { return tpop_none;        }
42 tp_op *tpop_unknown;       tp_op *get_tpop_unknown    (void) { return tpop_unknown;     }
43
44 tp_op *
45 new_tpop(tp_opcode code, ident *name, unsigned flags, size_t attr_size,
46          const tp_op_ops *ops)
47 {
48         tp_op *res = XMALLOC(tp_op);
49         res->code          = code;
50         res->name          = name;
51         res->flags         = flags;
52         res->attr_size     = attr_size;
53
54         if (ops)
55                 memcpy(&res->ops, ops, sizeof(res->ops));
56         else
57                 memset(&res->ops, 0, sizeof(res->ops));
58
59         return res;
60 }
61
62 void
63 free_tpop(tp_op *tpop) {
64         free(tpop);
65 }
66
67 static const tp_op_ops
68         /** tpop operations for class types */
69         class_ops = {
70                 free_class_attrs,
71                 free_class_entities,
72                 NULL,
73                 set_class_mode,
74                 set_class_size,
75                 get_class_n_members,
76                 get_class_member,
77                 get_class_member_index
78         },
79         /** tpop operations for struct types */
80         struct_ops = {
81                 free_struct_attrs,
82                 free_struct_entities,
83                 NULL,
84                 set_struct_mode,
85                 set_struct_size,
86                 get_struct_n_members,
87                 get_struct_member,
88                 get_struct_member_index
89         },
90         /** tpop operations for method types */
91         method_ops = {
92                 free_method_attrs,
93                 free_method_entities,
94                 NULL,
95                 NULL,
96                 NULL,
97                 NULL,
98                 NULL,
99                 NULL
100                 },
101         /** tpop operations for union types */
102         union_ops = {
103                 free_union_attrs,
104                 free_union_entities,
105                 NULL,
106                 NULL,
107                 set_union_size,
108                 get_union_n_members,
109                 get_union_member,
110                 get_union_member_index
111         },
112         /** tpop operations for array types */
113         array_ops = {
114                 free_array_attrs,
115                 free_array_entities,
116                 free_array_automatic_entities,
117                 NULL,
118                 set_array_size,
119                 NULL,
120                 NULL,
121                 NULL
122         },
123         /** tpop operations for enumeration types */
124         enum_ops = {
125                 free_enumeration_attrs,
126                 free_enumeration_entities,
127                 NULL,
128                 set_enumeration_mode,
129                 NULL,
130                 NULL,
131                 NULL,
132                 NULL
133         },
134         /** tpop operations for pointer types */
135         pointer_ops = {
136                 free_pointer_attrs,
137                 free_pointer_entities,
138                 NULL,
139                 set_pointer_mode,
140                 NULL,
141                 NULL,
142                 NULL,
143                 NULL
144         },
145         /** tpop operations for pseudo types */
146         pseudo_ops = {
147                 NULL,
148                 NULL,
149                 NULL,
150                 NULL,
151                 set_default_size,
152                 NULL,
153                 NULL,
154                 NULL
155         },
156         /** tpop operations for primitive types */
157         null_ops = {
158                 NULL,
159                 NULL,
160                 NULL,
161                 NULL,
162                 NULL,
163                 NULL,
164                 NULL,
165                 NULL
166         };
167
168 #define C     TP_OP_FLAG_COMPOUND
169 #define ID(s) new_id_from_chars(s, sizeof(s) - 1)
170
171 void init_tpop(void) {
172         type_class       = new_tpop(tpo_class      , ID("class"),       C, sizeof (cls_attr), &class_ops);
173         type_struct      = new_tpop(tpo_struct     , ID("struct"),      C, sizeof (stc_attr), &struct_ops);
174         type_method      = new_tpop(tpo_method     , ID("method"),      0, sizeof (mtd_attr), &method_ops);
175         type_union       = new_tpop(tpo_union      , ID("union"),       C, sizeof (uni_attr), &union_ops);
176         type_array       = new_tpop(tpo_array      , ID("array"),       C, sizeof (arr_attr), &array_ops);
177         type_enumeration = new_tpop(tpo_enumeration, ID("enumeration"), 0, sizeof (enm_attr), &enum_ops);
178         type_pointer     = new_tpop(tpo_pointer    , ID("pointer"),     0, sizeof (ptr_attr), &pointer_ops);
179         type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0, sizeof (pri_attr), &null_ops);
180         type_id          = new_tpop(tpo_id         , ID("type_id"),     0, /* sizeof (id_attr)  */ 0, &null_ops);
181         tpop_none        = new_tpop(tpo_none       , ID("None"),        0, /* sizeof (non_attr) */ 0, &pseudo_ops);
182         tpop_unknown     = new_tpop(tpo_unknown    , ID("Unknown"),     0, /* sizeof (ukn_attr) */ 0, &pseudo_ops);
183 }
184 #undef ID
185 #undef C
186
187 /* Finalize the tpop module.
188  * Frees all type opcodes.  */
189 void finish_tpop(void) {
190         free_tpop(type_class      ); type_class       = NULL;
191         free_tpop(type_struct     ); type_struct      = NULL;
192         free_tpop(type_method     ); type_method      = NULL;
193         free_tpop(type_union      ); type_union       = NULL;
194         free_tpop(type_array      ); type_array       = NULL;
195         free_tpop(type_enumeration); type_enumeration = NULL;
196         free_tpop(type_pointer    ); type_pointer     = NULL;
197         free_tpop(type_primitive  ); type_primitive   = NULL;
198         free_tpop(type_id         ); type_id          = NULL;
199         free_tpop(tpop_none       ); tpop_none        = NULL;
200         free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
201         }
202
203 /* Returns the string for the tp_opcode. */
204 const char  *get_tpop_name(const tp_op *op) {
205         return get_id_str(op->name);
206 }
207
208 tp_opcode (get_tpop_code)(const tp_op *op) {
209         return _get_tpop_code(op);
210 }
211
212 ident *(get_tpop_ident)(const tp_op *op) {
213         return _get_tpop_ident(op);
214 }
215
216 /* returns the attribute size of the operator. */
217 int (get_tpop_attr_size)(const tp_op *op) {
218         return _get_tpop_attr_size(op);
219 }