/
type_mgr.cc
380 lines (307 loc) · 10.3 KB
/
type_mgr.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* @file type_mgr.cc
* @brief Type system classes (TypeMgr)
*
* This module contains definitions and services that implement an
* optimized storage for expressions. Expressions are stored in a
* Directed Acyclic Graph (DAG) for data sharing.
*
* Copyright (C) 2012 Marco Pensallorto < marco AT pensallorto DOT gmail DOT com >
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
**/
#include <type.hh>
#include <type_mgr.hh>
#include <type_exceptions.hh>
// static initialization
TypeMgr_ptr TypeMgr::f_instance = NULL;
TypeMgr::TypeMgr()
: f_register()
, f_em(ExprMgr::INSTANCE())
{
// register predefined types
register_type( f_em.make_boolean_type(),
new BooleanType(*this));
register_type( f_em.make_int_const_type(),
new IntConstType(*this));
}
const Type_ptr TypeMgr::find_unsigned(unsigned digits)
{
Expr_ptr descr(f_em.make_unsigned_int_type(digits));
Type_ptr res = lookup_type(descr);
if (NULL != res) return res;
// new type, needs to be registered before returning
res = new UnsignedAlgebraicType( *this, digits);
register_type(descr, res);
return res;
}
const Type_ptr TypeMgr::find_default_unsigned()
{
/* TODO: move this somewhere */
return find_unsigned(8);
}
const Type_ptr TypeMgr::find_unsigned_array(unsigned digits, unsigned size)
{
Expr_ptr descr(f_em.make_subscript( f_em.make_unsigned_int_type(digits),
f_em.make_iconst(size)));
Type_ptr res = lookup_type(descr);
if (NULL != res) return res;
// new type, needs to be registered before returning
res = new ArrayType( *this,
find_unsigned(digits), size);
register_type(descr, res);
return res;
}
const Type_ptr TypeMgr::find_signed(unsigned digits)
{
Expr_ptr descr(f_em.make_signed_int_type(digits));
Type_ptr res = lookup_type(descr);
if (NULL != res) return res;
// new type, needs to be registered before returning
res = new SignedAlgebraicType( *this, digits);
register_type(descr, res);
return res;
}
const Type_ptr TypeMgr::find_signed_array(unsigned digits, unsigned size)
{
Expr_ptr descr(f_em.make_subscript( f_em.make_signed_int_type(digits),
f_em.make_iconst(size)));
Type_ptr res = lookup_type(descr);
if (NULL != res) return res;
// new type, needs to be registered before returning
res = new ArrayType( *this,
find_signed(digits), size);
register_type(descr, res);
return res;
}
void TypeMgr::add_enum(Expr_ptr ctx, Expr_ptr name, ExprSet& lits)
{
/*
IMPORTANT: lits ordering has to be canonical for enum types to
work as expected! Otherwise same set of lits with different
ordering could be mistakingly seen as a different type.
*/
Expr_ptr fullname = ExprMgr::INSTANCE().make_dot( ctx, name );
if (NULL != lookup_type(fullname)) {
assert(0); // tODO: better error handling
}
EnumType_ptr tp = new EnumType( *this, lits );
// Define the ENUM
IEnum_ptr enm = new Enum(ctx, name, tp);
f_enums.insert( make_pair<FQExpr,
IEnum_ptr>( FQExpr( ctx, name), enm));
// Literals are all maintained together by the type mgr. This
// greatly simplifies the resolver.
for (ExprSet::iterator eye = lits.begin(); eye != lits.end(); ++ eye) {
f_lits.insert( make_pair<FQExpr,
ILiteral_ptr>(FQExpr( fullname, *eye),
new Literal(enm, *eye)));
}
// new type, needs to be registered before returning
register_type(fullname, tp);
}
const Type_ptr TypeMgr::find_enum(Expr_ptr ctx, Expr_ptr name)
{
Type_ptr res = lookup_type(ExprMgr::INSTANCE().make_dot(ctx, name));
assert( NULL != res ); // TODO error handling
return res;
}
BooleanType_ptr TypeMgr::as_boolean(const Type_ptr tp) const
{
BooleanType_ptr res = dynamic_cast <const BooleanType_ptr> (tp);
assert(res);
return res;
}
EnumType_ptr TypeMgr::as_enum(const Type_ptr tp) const
{
EnumType_ptr res = dynamic_cast<EnumType_ptr> (tp);
assert(res);
return res;
}
AlgebraicType_ptr TypeMgr::as_algebraic(const Type_ptr tp) const
{
AlgebraicType_ptr res = dynamic_cast
<const AlgebraicType_ptr> (tp);
assert(res);
return res;
}
SignedAlgebraicType_ptr TypeMgr::as_signed_algebraic(const Type_ptr tp) const
{
SignedAlgebraicType_ptr res = dynamic_cast
<const SignedAlgebraicType_ptr> (tp);
assert(res);
return res;
}
UnsignedAlgebraicType_ptr TypeMgr::as_unsigned_algebraic(const Type_ptr tp) const
{
UnsignedAlgebraicType_ptr res = dynamic_cast
<const UnsignedAlgebraicType_ptr> (tp);
assert(res);
return res;
}
ArrayType_ptr TypeMgr::as_array(const Type_ptr tp) const
{
ArrayType_ptr res = dynamic_cast<ArrayType_ptr> (tp);
assert(res);
return res;
}
/* unary variant */
Type_ptr TypeMgr::result_type(Expr_ptr expr, Type_ptr lhs)
{ return lhs; /* nop */ }
/* binary variant */
Type_ptr TypeMgr::result_type(Expr_ptr expr, Type_ptr lhs, Type_ptr rhs)
{
ExprMgr& em = f_em;
if (em.is_binary_arithmetical(expr)) {
return arithmetical_result_type(lhs, rhs);
}
else if (em.is_binary_logical(expr)) {
return logical_result_type(lhs, rhs);
}
else if (em.is_binary_relational(expr)) {
return find_boolean();
}
else assert (false); // unexpected
}
/* ternary variant */
Type_ptr TypeMgr::result_type(Expr_ptr expr, Type_ptr cnd,
Type_ptr lhs, Type_ptr rhs)
{
ExprMgr& em = f_em;
assert(is_boolean(cnd));
if (em.is_ite(expr)) {
return ite_result_type(lhs, rhs);
}
else assert (false); // unexpected
}
Type_ptr TypeMgr::arithmetical_result_type(Type_ptr lhs, Type_ptr rhs)
{
// both ops integers -> integer
if (is_int_const(lhs) && (is_int_const(rhs))) {
return find_int_const();
}
// at least one algebraic -> algebraic
if (is_algebraic(lhs) || is_algebraic(rhs)) {
if ( is_boolean(lhs) || is_enum(lhs) || is_array(lhs) ||
is_boolean(rhs) || is_enum(rhs) || is_array(rhs)) {
throw TypeMismatch(lhs->repr(),
rhs->repr());
}
bool is_signed = \
is_signed_algebraic(lhs) ||
is_signed_algebraic(rhs) ;
unsigned width = max( calculate_width(lhs),
calculate_width(rhs));
return (!is_signed)
? find_unsigned(width)
: find_signed(width)
;
}
throw TypeMismatch(lhs->repr(),
rhs->repr());
assert( false ); /* unexpected */
return NULL;
}
Type_ptr TypeMgr::logical_result_type(Type_ptr lhs, Type_ptr rhs)
{
// both ops booleans -> boolean
if (is_boolean(lhs) && (is_boolean(rhs))) {
return find_boolean();
}
// both ops integers -> integer
if (is_int_const(lhs) && (is_int_const(rhs))) {
return find_int_const();
}
// at least one algebraic -> algebraic
if (is_algebraic(lhs) || is_algebraic(rhs)) {
if ( is_boolean(lhs) || is_enum(lhs) || is_array(lhs) ||
is_boolean(rhs) || is_enum(rhs) || is_array(rhs)) {
throw TypeMismatch(lhs->repr(),
rhs->repr());
}
bool is_signed = \
is_signed_algebraic(lhs) ||
is_signed_algebraic(rhs) ;
unsigned width = max( calculate_width(lhs),
calculate_width(rhs));
return (!is_signed)
? find_unsigned(width)
: find_signed(width)
;
}
throw TypeMismatch(lhs->repr(),
rhs->repr());
assert( false ); /* unexpected */
return NULL;
}
Type_ptr TypeMgr::ite_result_type(Type_ptr lhs, Type_ptr rhs)
{
/* monoliths */
if (is_boolean(lhs) && is_boolean(rhs))
return rhs;
if (is_enum(lhs) && is_enum(rhs) && (lhs == rhs))
return rhs;
/* algebraics */
if (is_int_const(lhs) && is_int_const(rhs))
return find_default_unsigned();
// at least one algebraic -> algebraic
if (is_algebraic(lhs) || is_algebraic(rhs)) {
if ( is_boolean(lhs) || is_enum(lhs) || is_array(lhs) ||
is_boolean(rhs) || is_enum(rhs) || is_array(rhs)) {
throw TypeMismatch(lhs->repr(),
rhs->repr());
}
bool is_signed = \
is_signed_algebraic(lhs) ||
is_signed_algebraic(rhs) ;
unsigned width = max( calculate_width(lhs),
calculate_width(rhs));
return (!is_signed)
? find_unsigned(width)
: find_signed(width)
;
}
throw TypeMismatch(lhs->repr(),
rhs->repr());
assert (false); // unexpected
return NULL;
}
/* low level service */
void TypeMgr::register_type(const Expr_ptr expr, Type_ptr vtype)
{
assert ((NULL != expr) && (NULL != vtype) && (! lookup_type(expr)));
DEBUG << "Registering new type: " << expr << endl;
f_register [ expr ] = vtype;
}
unsigned TypeMgr::calculate_width(Type_ptr type) const
{
if (is_boolean(type) ||
is_int_const(type) ||
is_enum(type)) {
return 1; /* monolithic */
}
else if (is_signed_algebraic(type)) {
return as_signed_algebraic(type)->width();
}
else if (is_unsigned_algebraic(type)) {
return as_unsigned_algebraic(type)->width();
}
else if (is_array(type)) {
assert(false); // FIXME yet to be supported
}
assert(false); /* unexpected */
return 0;
}