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