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