tpop.c 6.33 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Michael Beck's avatar
Michael Beck committed
20
21
22
23
24
/**
 * @file
 * @brief   Opcode of types.
 * @author  Goetz Lindenmaier, Michael Beck
 * @version $Id$
25
 */
Matthias Braun's avatar
Matthias Braun committed
26
#include "config.h"
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
27

Michael Beck's avatar
Michael Beck committed
28
29
30
#include "xmalloc.h"
#include "tpop_t.h"
#include "type_t.h"
31
#include "ident.h"
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
32

Michael Beck's avatar
Michael Beck committed
33
34
35
36
37
38
39
40
const tp_op *type_class;         const tp_op *get_tpop_class      (void) { return type_class;       }
const tp_op *type_struct;        const tp_op *get_tpop_struct     (void) { return type_struct;      }
const tp_op *type_method;        const tp_op *get_tpop_method     (void) { return type_method;      }
const tp_op *type_union;         const tp_op *get_tpop_union      (void) { return type_union;       }
const tp_op *type_array;         const tp_op *get_tpop_array      (void) { return type_array;       }
const tp_op *type_enumeration;   const tp_op *get_tpop_enumeration(void) { return type_enumeration; }
const tp_op *type_pointer;       const tp_op *get_tpop_pointer    (void) { return type_pointer;     }
const tp_op *type_primitive;     const tp_op *get_tpop_primitive  (void) { return type_primitive;   }
41
const tp_op *tpop_code;          const tp_op *get_tpop_code_type  (void) { return tpop_code;        }
Michael Beck's avatar
Michael Beck committed
42
43
const tp_op *tpop_none;          const tp_op *get_tpop_none       (void) { return tpop_none;        }
const tp_op *tpop_unknown;       const tp_op *get_tpop_unknown    (void) { return tpop_unknown;     }
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
44

Michael Beck's avatar
Michael Beck committed
45
46
const tp_op *
new_tpop(tp_opcode code, ident *name, unsigned flags, size_t attr_size, const tp_op_ops *ops)
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
47
{
48
	tp_op *res = XMALLOC(tp_op);
Michael Beck's avatar
Michael Beck committed
49
50
51
52
	res->code          = code;
	res->name          = name;
	res->flags         = flags;
	res->attr_size     = attr_size;
53

Michael Beck's avatar
Michael Beck committed
54
55
56
57
	if (ops)
		memcpy(&res->ops, ops, sizeof(res->ops));
	else
		memset(&res->ops, 0, sizeof(res->ops));
58

Michael Beck's avatar
Michael Beck committed
59
	return res;
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
60
61
}

62
void free_tpop(const tp_op *tpop)
63
{
Michael Beck's avatar
Michael Beck committed
64
	xfree((void *)tpop);
65
66
}

67
static const tp_op_ops
Michael Beck's avatar
Michael Beck committed
68
69
70
71
72
73
	/** tpop operations for class types */
	class_ops = {
		free_class_attrs,
		free_class_entities,
		NULL,
		set_class_mode,
74
		set_class_size,
Michael Beck's avatar
Michael Beck committed
75
76
77
78
79
80
81
82
83
84
		get_class_n_members,
		get_class_member,
		get_class_member_index
	},
	/** tpop operations for struct types */
	struct_ops = {
		free_struct_attrs,
		free_struct_entities,
		NULL,
		set_struct_mode,
85
		set_struct_size,
Michael Beck's avatar
Michael Beck committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
		get_struct_n_members,
		get_struct_member,
		get_struct_member_index
	},
	/** tpop operations for method types */
	method_ops = {
		free_method_attrs,
		free_method_entities,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL
		},
	/** tpop operations for union types */
	union_ops = {
		free_union_attrs,
		free_union_entities,
		NULL,
		NULL,
107
		set_union_size,
Michael Beck's avatar
Michael Beck committed
108
109
110
111
112
113
114
115
116
117
		get_union_n_members,
		get_union_member,
		get_union_member_index
	},
	/** tpop operations for array types */
	array_ops = {
		free_array_attrs,
		free_array_entities,
		free_array_automatic_entities,
		NULL,
118
		set_array_size,
Michael Beck's avatar
Michael Beck committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		NULL,
		NULL,
		NULL
	},
	/** tpop operations for enumeration types */
	enum_ops = {
		free_enumeration_attrs,
		free_enumeration_entities,
		NULL,
		set_enumeration_mode,
		NULL,
		NULL,
		NULL,
		NULL
	},
	/** tpop operations for pointer types */
	pointer_ops = {
		free_pointer_attrs,
		free_pointer_entities,
		NULL,
		set_pointer_mode,
		NULL,
		NULL,
		NULL,
		NULL
	},
	/** tpop operations for pseudo types */
	pseudo_ops = {
		NULL,
		NULL,
		NULL,
		NULL,
151
		set_default_size,
Michael Beck's avatar
Michael Beck committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
		NULL,
		NULL,
		NULL
	},
	/** tpop operations for primitive types */
	null_ops = {
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL
	};
167

168
169
void init_tpop(void)
{
170
171
172
173
174
175
176
177
178
179
180
181
#define ID(s) new_id_from_chars(s, sizeof(s) - 1)
	type_class       = new_tpop(tpo_class      , ID("class"),       TP_OP_FLAG_COMPOUND, sizeof(cls_attr), &class_ops);
	type_struct      = new_tpop(tpo_struct     , ID("struct"),      TP_OP_FLAG_COMPOUND, sizeof(stc_attr), &struct_ops);
	type_method      = new_tpop(tpo_method     , ID("method"),      0,                   sizeof(mtd_attr), &method_ops);
	type_union       = new_tpop(tpo_union      , ID("union"),       TP_OP_FLAG_COMPOUND, sizeof(uni_attr), &union_ops);
	type_array       = new_tpop(tpo_array      , ID("array"),       0,                   sizeof(arr_attr), &array_ops);
	type_enumeration = new_tpop(tpo_enumeration, ID("enumeration"), 0,                   sizeof(enm_attr), &enum_ops);
	type_pointer     = new_tpop(tpo_pointer    , ID("pointer"),     0,                   sizeof(ptr_attr), &pointer_ops);
	type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0,                   sizeof(pri_attr), &null_ops);
	tpop_code        = new_tpop(tpo_code       , ID("code"),        0,                   0,                &null_ops);
	tpop_none        = new_tpop(tpo_none       , ID("None"),        0,                   0,                &pseudo_ops);
	tpop_unknown     = new_tpop(tpo_unknown    , ID("Unknown"),     0,                   0,                &pseudo_ops);
Michael Beck's avatar
Michael Beck committed
182
#undef ID
183
}
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
184

Michael Beck's avatar
Michael Beck committed
185
/* Finalize the tpop module.
186
 * Frees all type opcodes.  */
187
188
void finish_tpop(void)
{
Michael Beck's avatar
Michael Beck committed
189
190
191
192
193
194
195
196
	free_tpop(type_class      ); type_class       = NULL;
	free_tpop(type_struct     ); type_struct      = NULL;
	free_tpop(type_method     ); type_method      = NULL;
	free_tpop(type_union      ); type_union       = NULL;
	free_tpop(type_array      ); type_array       = NULL;
	free_tpop(type_enumeration); type_enumeration = NULL;
	free_tpop(type_pointer    ); type_pointer     = NULL;
	free_tpop(type_primitive  ); type_primitive   = NULL;
197
	free_tpop(tpop_code       ); tpop_code        = NULL;
Michael Beck's avatar
Michael Beck committed
198
199
	free_tpop(tpop_none       ); tpop_none        = NULL;
	free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
Michael Beck's avatar
Michael Beck committed
200
}
201

Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
202
/* Returns the string for the tp_opcode. */
203
204
const char  *get_tpop_name(const tp_op *op)
{
Michael Beck's avatar
Michael Beck committed
205
	return get_id_str(op->name);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
206
207
}

208
209
tp_opcode (get_tpop_code)(const tp_op *op)
{
Michael Beck's avatar
Michael Beck committed
210
	return _get_tpop_code(op);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
211
212
213
}

/* returns the attribute size of the operator. */
214
size_t (get_tpop_attr_size)(const tp_op *op)
215
{
Michael Beck's avatar
Michael Beck committed
216
	return _get_tpop_attr_size(op);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
217
}