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