tpop.c 5.47 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   Opcode of types.
 * @author  Goetz Lindenmaier, Michael Beck
10
 */
Michael Beck's avatar
Michael Beck committed
11
12
13
#include "xmalloc.h"
#include "tpop_t.h"
#include "type_t.h"
14
#include "ident.h"
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
15

Michael Beck's avatar
Michael Beck committed
16
17
18
19
20
21
22
23
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;   }
24
const tp_op *tpop_code;          const tp_op *get_tpop_code_type  (void) { return tpop_code;        }
Michael Beck's avatar
Michael Beck committed
25
26
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
27

Matthias Braun's avatar
Matthias Braun committed
28
29
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
30
{
31
	tp_op *res = XMALLOC(tp_op);
Michael Beck's avatar
Michael Beck committed
32
33
34
35
	res->code          = code;
	res->name          = name;
	res->flags         = flags;
	res->attr_size     = attr_size;
36

Michael Beck's avatar
Michael Beck committed
37
	if (ops)
38
		res->ops = *ops;
Michael Beck's avatar
Michael Beck committed
39
40
	else
		memset(&res->ops, 0, sizeof(res->ops));
41

Michael Beck's avatar
Michael Beck committed
42
	return res;
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
43
44
}

45
void free_tpop(const tp_op *tpop)
46
{
47
	free((void *)tpop);
48
49
}

50
static const tp_op_ops
Michael Beck's avatar
Michael Beck committed
51
52
53
54
55
56
	/** tpop operations for class types */
	class_ops = {
		free_class_attrs,
		free_class_entities,
		NULL,
		set_class_mode,
57
		set_class_size,
Michael Beck's avatar
Michael Beck committed
58
59
60
61
62
63
64
65
66
67
		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,
68
		set_struct_size,
Michael Beck's avatar
Michael Beck committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
		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,
90
		set_union_size,
Michael Beck's avatar
Michael Beck committed
91
92
93
94
95
96
97
98
99
100
		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,
101
		set_array_size,
Michael Beck's avatar
Michael Beck committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
		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,
134
		set_default_size,
Michael Beck's avatar
Michael Beck committed
135
136
137
138
139
140
141
142
143
144
		NULL,
		NULL,
		NULL
	},
	/** tpop operations for primitive types */
	null_ops = {
		NULL,
		NULL,
		NULL,
		NULL,
145
		set_default_size,
Michael Beck's avatar
Michael Beck committed
146
147
148
		NULL,
		NULL,
		NULL
Matthias Braun's avatar
Matthias Braun committed
149
150
	}
;
151

152
153
void init_tpop(void)
{
154
155
156
157
158
159
160
161
#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);
162
	type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0,                   0,                &null_ops);
163
164
165
	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
166
#undef ID
167
}
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
168

169
170
void finish_tpop(void)
{
Michael Beck's avatar
Michael Beck committed
171
172
173
174
175
176
177
178
	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;
179
	free_tpop(tpop_code       ); tpop_code        = NULL;
Michael Beck's avatar
Michael Beck committed
180
181
	free_tpop(tpop_none       ); tpop_none        = NULL;
	free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
Michael Beck's avatar
Michael Beck committed
182
}
183

184
const char *get_tpop_name(const tp_op *op)
185
{
Michael Beck's avatar
Michael Beck committed
186
	return get_id_str(op->name);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
187
188
}

189
190
tp_opcode (get_tpop_code)(const tp_op *op)
{
Michael Beck's avatar
Michael Beck committed
191
	return _get_tpop_code(op);
Götz Lindenmaier's avatar
new.  
Götz Lindenmaier committed
192
}