aboutsummaryrefslogtreecommitdiff
path: root/gcc/gimple-wrapper.h
blob: 883781feb7ecfb9e51271423364a8b4659d18a08 (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#ifndef GIMPLE_WRAPPER_H
#define GIMPLE_WRAPPER_H

#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
#define GIMPLE_CHECKING_ON	1
extern void tree_check_failed (const_tree, const char *, int, const char *,
                               ...) ATTRIBUTE_NORETURN;
extern void tree_class_check_failed (const_tree, const enum tree_code_class,
                                     const char *, int, const char *)
				     ATTRIBUTE_NORETURN;
extern void tree_contains_struct_check_failed (const_tree,
					   const enum tree_node_structure_enum,
					   const char *, int, const char *)
					   ATTRIBUTE_NORETURN;
extern void tree_operand_check_failed (int, const_tree,
				       const char *, int, const char *)
				       ATTRIBUTE_NORETURN;
extern void tree_int_cst_elt_check_failed (int, int, const char *,
					   int, const char *) 
					   ATTRIBUTE_NORETURN;


#else
#define GIMPLE_CHECKING_ON	0
#endif


class gimple_null {
  public:
    gimple_null () { }
    bool operator!() const { return true; }
    operator long int() { return 0; }
    operator tree() { return (tree)0; }
};

static gimple_null NULL_GIMPLE;

template<typename T, typename D>
inline bool is_a (D v)
{ return T::is_a (v); }

template<typename T, typename D>
inline T as_a (D v)
{ return T::as_a (v); }

template<typename T, typename D>
inline T dyn_cast (D v)
{ return T::dyn_cast (v); }

namespace G {

template<typename T>
class _ptr 
{
  protected:
    class tree_desc *Tree;
    inline void check_contents () const;
    static inline _ptr create ();
    inline _ptr copy () const;
    T* ptr() const { return static_cast<T *>(Tree); }
    void set_ptr(const void *p) 
		{ Tree = reinterpret_cast<tree_desc *>(const_cast<void *>(p)); }
  public:
    inline _ptr () { Tree = NULL; }
    inline _ptr (const gimple_null& g ATTRIBUTE_UNUSED) { Tree = NULL; }
    inline _ptr (const tree t)  { set_ptr (t); check_contents (); }
    inline _ptr (const_tree t)  { set_ptr (t); check_contents (); }
    inline _ptr (const T *v) { set_ptr (v); }
    inline _ptr (int x ATTRIBUTE_UNUSED) { gcc_assert (!x); set_ptr (NULL); }
    inline _ptr& operator= (const gimple_null& g ATTRIBUTE_UNUSED)
				    { Tree = NULL; return *this;}
    inline _ptr& operator= (const tree t) 
			    { set_ptr (t); check_contents (); return *this;}
    inline _ptr& operator= (const_tree t)
			    { set_ptr (t); check_contents (); return *this;}
    inline _ptr& operator= (const T *v) { set_ptr (v); return *this;}
    inline _ptr& operator= (int x ATTRIBUTE_UNUSED) 
			  { gcc_assert (!x); set_ptr (NULL); return *this; }

    bool operator!() const { return Tree == NULL; }
    inline operator tree () const { return reinterpret_cast<tree>(Tree); }

    inline T * operator->() { return ptr (); }
    inline const T * operator->() const { return ptr (); }
    inline T& operator*() { return *ptr (); }
    inline const T& operator*() const { return *ptr (); }

    template <typename TT>
    friend bool operator== (const _ptr<TT>&, const _ptr<TT>&);
    template <typename TT>
    friend bool operator!= (const _ptr<TT>&, const _ptr<TT>&);
    template <typename TT>
    friend bool operator== (const _ptr<TT>&, const tree&);
    template <typename TT>
    friend bool operator!= (const _ptr<TT>&, const tree&);
    template <typename TT>
    friend bool operator== (const tree&, const _ptr<TT>&);
    template <typename TT>
    friend bool operator!= (const tree&, const _ptr<TT>&);
    template <typename TT>
    friend bool operator== (const _ptr<TT>&, const gimple_null& v);
    template <typename TT>
    friend bool operator!= (const _ptr<TT>&, const gimple_null& v);
    template <typename TT>
    friend bool operator== (const _ptr<TT>& a, const T *b);
    template <typename TT>
    friend bool operator!= (const _ptr<TT>& a, const T *b);
    template <typename TT>
    friend bool operator== (const T *b, const _ptr<TT>& a);
    template <typename TT>
    friend bool operator!= (const T *b, const _ptr<TT>& a);

    template <typename TT>
    friend bool operator== (const _ptr<TT>&, long);
    template <typename TT>
    friend bool operator!= (const _ptr<TT>&, long); 
    template <typename TT> friend TT copy (TT ptr);
    template <typename TT> friend TT create ();

    template<typename, typename> friend class _dptr;
    template<typename> friend class _addr;
};


template<typename pT, typename dT>
class _dptr : public dT
{
  protected:
    pT *ptr() const { return static_cast<pT *>(dT::Tree); }
    void set_ptr(const void *p) 
	    { dT::Tree = reinterpret_cast<tree_desc *>(const_cast<void *>(p)); }
    static inline bool Test (const dT &);
    inline void check_contents () const;
    static inline _dptr create ();
    inline _dptr copy () const;
    static inline bool is_a(const dT &d) { return Test (d); }
    static inline  pT * as_a(dT &d) { return static_cast<pT *>(d.Tree); }
    static inline  const pT * as_a(const dT &d)
				    { return static_cast<const pT *>(d.Tree); }
    static inline pT * dyn_cast(dT &d) 
		      { return (Test (d)) ? static_cast<pT *>(d.Tree) : NULL; }
    static inline const pT * dyn_cast(const dT &d) 
		{ return (Test (d)) ? static_cast<const pT *>(d.Tree) : NULL; }

  public:

    inline _dptr () : dT () { }
    inline _dptr (const gimple_null& g ATTRIBUTE_UNUSED) : dT() { }
    inline _dptr (const tree t) : dT (t) { check_contents(); }
    inline _dptr (const_tree t) : dT (t) { check_contents(); }
    inline _dptr (const dT& d) : dT () { if (d) set_ptr ((dyn_cast (d))); }
    inline _dptr (const pT *n) : dT () {  dT::set_ptr (n); }
    inline _dptr (int x ATTRIBUTE_UNUSED) : dT () { gcc_assert (!x); }

    inline _dptr& operator= (const tree t) 
				{ set_ptr (t); check_contents(); return *this; }
    inline _dptr& operator= (const_tree t) 
				{ set_ptr (t); check_contents(); return *this; }
    inline _dptr& operator= (const dT& d) 
	   { if (d) set_ptr (dyn_cast (d)); else set_ptr (NULL); return *this; }
    inline _dptr& operator= (const pT *p) { set_ptr (p); return *this; }
    inline _dptr& operator= (const gimple_null& g ATTRIBUTE_UNUSED)
						{ set_ptr (NULL); return *this;}
    inline _dptr& operator= (int x ATTRIBUTE_UNUSED) 
			    { gcc_assert (!x); set_ptr (NULL); return *this; }

    inline pT * operator->() { return ptr(); }
    inline const pT * operator->() const { return ptr(); }
    inline pT& operator*() { return *ptr(); }
    inline const pT& operator*() const { return *ptr(); }

    template <typename T1, typename T2>
    friend bool operator== (const _dptr<T1,T2>& a, const T1 *b);
    template <typename T1, typename T2>
    friend bool operator!= (const _dptr<T1,T2>& a, const T1 *b);
    template <typename T1, typename T2>
    friend bool operator== (const T1 *b, const _dptr<T1,T2>& a);
    template <typename T1, typename T2>
    friend bool operator!= (const T1 *b, const _dptr<T1,T2>& a);

    template <typename T, typename D> bool friend ::is_a(D);
    template <typename T, typename D> T friend ::as_a(D);
    template <typename T, typename D> T friend ::dyn_cast(D);
    template <typename TT> friend TT copy (TT ptr);
    template <typename TT> friend TT create ();
    template<typename> friend class _addr;
};

template<typename T>
class _addr 
{
  protected:
    T *ptr;
  public:
    inline _addr () { ptr = NULL; }
    inline _addr (const gimple_null& g ATTRIBUTE_UNUSED) { ptr = NULL;}
    inline _addr (tree *t) { ptr = reinterpret_cast<T *> (t); }
    inline _addr (T *t) { ptr = t; }
    inline T& operator*() { return *ptr; }
    inline operator tree *() const { return reinterpret_cast<tree *>(ptr); }
};


template <typename T>
inline bool 
operator== (const _ptr<T>& t1, const _ptr<T>& t2) 
{ return t1.Tree == t2.Tree; }

template <typename T>
inline bool 
operator!= (const _ptr<T>& t1, const _ptr<T>& t2) 
{ return t1.Tree != t2.Tree; }

template <typename T>
inline bool 
operator== (const _ptr<T>& t1, const tree& t2) 
{ return reinterpret_cast<tree>(t1.Tree) == t2; }

template <typename T>
inline bool 
operator!= (const _ptr<T>& t1, const tree& t2) 
{ return reinterpret_cast<tree>(t1.Tree) != t2; }

template <typename T>
inline bool 
operator== (const tree& t1, const _ptr<T>& t2) 
{ return t1 == reinterpret_cast<tree>(t2.Tree); }

template <typename T>
inline bool 
operator!= (const tree& t1, const _ptr<T>& t2) 
{ return t1 != reinterpret_cast<tree>(t2.Tree); }

template <typename T>
inline bool 
operator== (const _ptr<T>& a, const T *b)
{ return a.ptr() == b; }

template <typename T>
inline bool
operator!= (const _ptr<T>& a, const T *b)
{ return a.ptr() != b; }

template <typename T>
inline bool
operator== (const T *b, const _ptr<T>& a)
{ return a.ptr() == b; }

template <typename T>
inline bool
operator!= (const T *b, const _ptr<T>& a)
{ return a.ptr() != b; }

template <typename T>
inline bool 
operator== (const _ptr<T>& t1, const gimple_null& n ATTRIBUTE_UNUSED)
{ return t1.Tree == NULL; }

template <typename T>
inline bool 
operator!= (const _ptr<T>& t1, const gimple_null& n ATTRIBUTE_UNUSED) 
{ return t1.Tree != NULL; }



template <typename pT, typename dT>
inline bool operator== (const _dptr<pT, dT>& a, const pT *b)
{ return a.ptr() == b; }

template <typename pT, typename dT>
inline bool operator!= (const _dptr<pT, dT>& a, const pT *b)
{ return a.ptr() != b; }

template <typename pT, typename dT>
inline bool operator== (const pT *b, const _dptr<pT, dT>& a)
{ return a.ptr() == b; }

template <typename pT, typename dT>
inline bool operator!= (const pT *b, const _dptr<pT, dT>& a)
{ return a.ptr() != b; }

template <typename T>
inline bool 
operator== (const _ptr<T>& t1, long t2) 
{ return t1.Tree == reinterpret_cast<class tree_desc *>(t2); }

template <typename T>
inline bool 
operator!= (const _ptr<T>& t1, long t2) 
{ return t1.Tree != reinterpret_cast<class tree_desc *>(t2); }


//////////////////////////////////////////////////////////////////
//
// Instances
//
//////////////////////////////////////////////////////////////////

extern void * gimple_copy_node (const void *t);
extern void * gimple_make_node (enum tree_code t);

template<typename T>
inline _ptr<T> 
_ptr<T>::copy() const
{
  return _ptr<T> (reinterpret_cast<tree> (gimple_copy_node (ptr())));
}

template<typename T, typename D>
inline _dptr<T, D> 
_dptr<T, D>::copy() const
{
  return _dptr<T, D> (reinterpret_cast<tree> (gimple_copy_node (ptr())));
}

template<typename T>
inline T
copy(T ptr)
{
  return ptr.copy();
}

template<typename T>
inline T
create ()
{
  return T::create ();
}


typedef _ptr<value_desc>		value;
typedef _ptr<type_desc>			type;
typedef _ptr<block_desc>		block;

template<>
inline void 
_ptr<value_desc>::check_contents() const
{
}

template<>
inline _ptr<value_desc> 
_ptr<value_desc>::create()
{
  gcc_unreachable ();
}

template<>
inline void 
_ptr<type_desc>::check_contents() const
{
#if GIMPLE_CHECKING_ON
  if (Tree)
    Tree->check_node (tcc_type);
#endif
}

template<>
inline _ptr<type_desc> 
_ptr<type_desc>::create()
{
  gcc_unreachable ();
}

template<>
inline void 
_ptr<block_desc>::check_contents() const
{
#if GIMPLE_CHECKING_ON
  if (Tree)
    Tree->check_node (BLOCK);
#endif
}

template<>
inline _ptr<block_desc> 
_ptr<block_desc>::create()
{
  return _ptr<block_desc> (reinterpret_cast<tree> (gimple_make_node (BLOCK)));
}

#if GIMPLE_CHECKING_ON
#define CHKNODE(...) if (Tree) Tree->check_node (__VA_ARGS__)
#else
#define CHKNODE(CC)
#endif

#define DERIVED_PTR(LAB, CC, DT)			\
typedef _dptr<LAB ## _desc, DT>	LAB;			\
template<> inline void 					\
_dptr<LAB ## _desc, DT>::check_contents() const		\
{ CHKNODE (CC) ; }					\
template<> inline bool					\
_dptr<LAB ## _desc, DT>::Test(const DT &d)		\
{ return d.Tree->test_node (CC); }			\
template<> inline _dptr<LAB ## _desc, DT>		\
_dptr<LAB ## _desc, DT>::create ()			\
{ gcc_unreachable (); }



#define TERMINAL_PTR(LAB, CC, DT)			\
typedef _dptr<LAB ## _desc, DT>	LAB;			\
template<> inline void 					\
_dptr<LAB ## _desc, DT>::check_contents() const		\
{ CHKNODE (CC) ; }					\
template<> inline bool					\
_dptr<LAB ## _desc, DT>::Test(const DT &d)		\
{ return d.Tree->test_node (CC); }			\
template<> inline _dptr<LAB ## _desc, DT>		\
_dptr<LAB ## _desc, DT>::create ()			\
{ return _dptr<LAB ## _desc, DT> 			\
   (reinterpret_cast<tree> (gimple_make_node (CC))); }




DERIVED_PTR (decl, tcc_declaration, value)
DERIVED_PTR (constant, tcc_constant, value)
DERIVED_PTR (comparison, tcc_comparison, value)
DERIVED_PTR (unary, tcc_unary, value)
DERIVED_PTR (binary, tcc_binary, value)

TERMINAL_PTR (identifier, IDENTIFIER_NODE, value) 
TERMINAL_PTR (integer_cst, INTEGER_CST, value)
TERMINAL_PTR (real_cst, REAL_CST, value)
TERMINAL_PTR (string_cst, STRING_CST, value)
TERMINAL_PTR (var_decl, VAR_DECL, value)
TERMINAL_PTR (parm_decl, PARM_DECL, value)
TERMINAL_PTR (result_decl, RESULT_DECL, value)
TERMINAL_PTR (label_decl, LABEL_DECL, value)
TERMINAL_PTR (function_decl, FUNCTION_DECL, value)
TERMINAL_PTR (debug_expr_decl, DEBUG_EXPR_DECL, value)

TERMINAL_PTR (ssa_name, SSA_NAME, value)
TERMINAL_PTR (mem_ref, MEM_REF, value)
TERMINAL_PTR (addr_expr, ADDR_EXPR, value)
TERMINAL_PTR (value_list, TREE_LIST, value)
TERMINAL_PTR (type_list, TREE_LIST, value)
TERMINAL_PTR (identifier_list, TREE_LIST, value)
TERMINAL_PTR (case_label_expr, CASE_LABEL_EXPR, value)
TERMINAL_PTR (truth_not_expr, TRUTH_NOT_EXPR, value)
TERMINAL_PTR (with_size_expr, WITH_SIZE_EXPR, value)
TERMINAL_PTR (call_expr, CALL_EXPR, value)
TERMINAL_PTR (nop_expr, NOP_EXPR, value)
TERMINAL_PTR (bit_field_ref, BIT_FIELD_REF, value)
TERMINAL_PTR (target_mem_ref, TARGET_MEM_REF, value)
TERMINAL_PTR (array_ref, ARRAY_REF, value)
TERMINAL_PTR (array_range_ref, ARRAY_RANGE_REF, value)
TERMINAL_PTR (obj_type_ref, OBJ_TYPE_REF, value)
TERMINAL_PTR (constructor, CONSTRUCTOR, value)
TERMINAL_PTR (modify_expr, MODIFY_EXPR, value)
TERMINAL_PTR (const_decl, CONST_DECL, value)

TERMINAL_PTR (boolean_type, BOOLEAN_TYPE, type)
TERMINAL_PTR (integer_type, INTEGER_TYPE, type)
TERMINAL_PTR (real_type, REAL_TYPE, type)
TERMINAL_PTR (fixed_point_type, FIXED_POINT_TYPE, type)
TERMINAL_PTR (function_type, FUNCTION_TYPE, type)
TERMINAL_PTR (method_type, METHOD_TYPE, type)
TERMINAL_PTR (array_type, ARRAY_TYPE, type)
TERMINAL_PTR (complex_type, COMPLEX_TYPE, type)
TERMINAL_PTR (vector_type, VECTOR_TYPE, type)

DERIVED_PTR (decl_with_rtl, TS_DECL_WRTL, value)
DERIVED_PTR (decl_with_viz, TS_DECL_WITH_VIS, value)
DERIVED_PTR (decl_noncommon, TS_DECL_NON_COMMON, value)

#define MULTIARGS(...) __VA_ARGS__
DERIVED_PTR (numerical_type, 
	     MULTIARGS (INTEGER_TYPE, ENUMERAL_TYPE, BOOLEAN_TYPE, REAL_TYPE,
			FIXED_POINT_TYPE),
	     type)

DERIVED_PTR (function_or_method_type,
	     MULTIARGS (FUNCTION_TYPE, METHOD_TYPE),
	     type)

typedef _addr<type>			type_ptr;
typedef _addr<value>			value_ptr;
typedef _addr<integer_cst>		integer_cst_ptr;

} // namespace G


#endif  /* GIMPLE_WRAPPER_H  */