tpop.c 6.13 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 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"
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
31

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

tp_op *
45
46
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
Michael Beck's avatar
Michael Beck committed
63
free_tpop(tp_op *tpop) {
Michael Beck's avatar
Michael Beck committed
64
	free(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

Michael Beck's avatar
Michael Beck committed
168
169
#define C     TP_OP_FLAG_COMPOUND
#define ID(s) new_id_from_chars(s, sizeof(s) - 1)
Michael Beck's avatar
Michael Beck committed
170
171
172
173
174
175
176
177
178

void init_tpop(void) {
	type_class       = new_tpop(tpo_class      , ID("class"),       C, sizeof (cls_attr), &class_ops);
	type_struct      = new_tpop(tpo_struct     , ID("struct"),      C, 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"),       C, sizeof (uni_attr), &union_ops);
	type_array       = new_tpop(tpo_array      , ID("array"),       C, 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);
179
	type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0, sizeof (pri_attr), &null_ops);
Michael Beck's avatar
Michael Beck committed
180
181
182
	type_id          = new_tpop(tpo_id         , ID("type_id"),     0, /* sizeof (id_attr)  */ 0, &null_ops);
	tpop_none        = new_tpop(tpo_none       , ID("None"),        0, /* sizeof (non_attr) */ 0, &pseudo_ops);
	tpop_unknown     = new_tpop(tpo_unknown    , ID("Unknown"),     0, /* sizeof (ukn_attr) */ 0, &pseudo_ops);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
183
}
Michael Beck's avatar
Michael Beck committed
184
185
#undef ID
#undef C
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
186

Michael Beck's avatar
Michael Beck committed
187
/* Finalize the tpop module.
188
189
 * Frees all type opcodes.  */
void finish_tpop(void) {
Michael Beck's avatar
Michael Beck committed
190
191
192
193
194
195
196
197
198
199
200
201
	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;
	free_tpop(type_id         ); type_id          = NULL;
	free_tpop(tpop_none       ); tpop_none        = NULL;
	free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
	}
202

Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
203
/* Returns the string for the tp_opcode. */
Michael Beck's avatar
Michael Beck committed
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
tp_opcode (get_tpop_code)(const tp_op *op) {
Michael Beck's avatar
Michael Beck committed
209
	return _get_tpop_code(op);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
210
211
}

212
ident *(get_tpop_ident)(const tp_op *op) {
Michael Beck's avatar
Michael Beck committed
213
	return _get_tpop_ident(op);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
214
215
216
}

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