changed definition of subdir only (added prefix ir/)
[libfirm] / ir / tr / type.h
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Martin Trapp, Christian Schaefer &
5 **          Goetz Lindenmaier
6 **
7
8 @@@@@@@  Improve documentation: distinguish fields that are
9 set by the frontend and contain knowledge specified by the source
10 program from fields containing information derived by analysis/optimization
11 or lowering phases.
12
13
14 **  type.h: datastructures to hold type information.
15 **
16 **  This module supplies datastructures to represent all types
17 **  known in the compiled program.  This includes types specified
18 **  in the program as well as types defined by the language.  In the
19 **  view of the intermediate representation there is no difference
20 **  between these types.
21 **  Types are different from the modes defined in irmode:  Types are
22 **  on the level of the programming language, modes at the level of
23 **  the target processor.
24 **
25 **
26 **  General datastructure
27 **  =====================
28 **
29 **  Firm distinguishes several different type constructs.  These are
30 **  implemented as structs.  A union of the individual structs constructs
31 **  the firm node "type".
32 **
33 **  All type constructs have the following fields:
34 **
35 **  kind         A firm_kind tag containing k_type_class.  This is useful
36 **               for dynamically checking the sort of a type.  Automatically
37 **               generated.
38 **
39 **  name         An identifier specifying the type name.  Set by the frontend.
40 **
41 **  visit        A counter for walks of the type information.
42 **
43 **
44 **  General functionality
45 **  =====================
46 **
47 **  is_type(t)   Returns true if t is a type node, else false.
48 **
49 **
50 **  type_class
51 **  ==========
52 **
53 **  Type_class represents class types.  A list of fields and
54 **  methods is associated with a class.  Further a class can
55 **  inherit from and bequest to other classes.
56 **
57 **  fields:
58 **  -------
59 **
60 **  **member     All entities belonging to this class.  This are methodes
61 **               which have type_method or fields that can have any of the
62 **               following types: k_type_class, k_type_strct, k_type_union,
63 **               k_type_array, k_type_enumeration, k_type_pointer, k_type_primitive.
64 **
65 **  **subtypes   A list of direct subclasses.
66 **
67 **  **supertypes A list of direct superclasses.
68 **
69 **
70 **  type_strct
71 **  ==========
72 **
73 **  Type_strct represents aggregate types that consist of a list
74 **  of fields.
75 **
76 **  fields:
77 **  -------
78 **
79 **  **member     All entities belonging to this class.  This are the fields
80 **               that can have any of the following types:  k_type_class,
81 **               k_type_strct, k_type_union, k_type_array, k_type_enumeration,
82 **               k_type_pointer, k_type_primitive.
83 **
84 **  type_method
85 **  ===========
86 **
87 **  Type_method represents method, function and procedure types.
88 **
89 **  fields:
90 **  -------
91 **
92 **  arity        Number of parameters to the procedure. @@@ better n_params
93 **               A procedure in FIRM has only call by value parameters.
94 **
95 **  **param_type A list with the types of parameters.  This list is ordered.
96 **               The nth type in this list corresponds to the nth element
97 **               in the parameter tuple that is a result of the start node.
98 **               (See ircons.h for more information.)
99 **
100 **  n_res        The number of results of the method.  In general, procedures
101 **               have zero results, functions one.
102 **
103 **  **res_type   A list with the types of parameters.  This list is ordered.
104 **               The nth type in this list corresponds to the nth input to
105 **               Return nodes.  (See ircons.h for more information.)
106 **
107 **
108 **  type_union
109 **  ==========
110 **
111 **  Type_union represents union types.
112 **
113 **  fields:
114 **  -------
115 **
116 **  **unioned_type   A list of unioned types.
117 **
118 **
119 **  type_array
120 **  ==========
121 **
122 **  Type_array represents rectangular multi dimensional arrays.
123 **
124 **  fields:
125 **  -------
126 **
127 **  n_dimensions     Number of array dimensions.
128 **
129 **  *lower_bound     Lower bounds of dimensions.  Mostly all 0.
130 **
131 **  *upper_bound     Upper bounds or dimensions.
132 **
133 **  *element_type    The type of the array elements.
134 **
135 **
136 **  type_enumeration
137 **  ================
138 **
139 **  Enumeration types.  These need not necessarily be represented explicitly
140 **  by Firm types, as the frontend can lower them to integer constants as
141 **  well.  For debugging purposes or similar tasks this information is useful.
142 **
143 **  fields:
144 **  -------
145 **
146 **  **enum           The target values representing the constants used to
147 **                   represent individual enumerations.
148 **
149 **  **enum_name      Idents containing the source program name of the enumeration
150 **                   constants
151 **
152 **  type_pointer
153 **  ============
154 **
155 **  Pointer types.
156 **
157 **  fields:
158 **  -------
159 **
160 **  *mode            The mode used to implement a pointer.  @@@ So far this field
161 **                   is constant and set to mode_P.  Maybe we will move this
162 **                   to a global constant (irprog), or are there processors
163 **                   that require a set of different pointer modes?
164 **
165 **  *points_to       The type of the entity this pointer points to.
166 **
167 **  type_primitive
168 **  ==============
169 **
170 **  Primitive types are types that represent indivisible data values that
171 **  map directly to modes.
172 **
173 **  fields:
174 **  -------
175 **
176 **  mode             The mode to be used for this type.
177 **
178 */
179
180 # ifndef _TYPE_H_
181 # define _TYPE_H_
182
183 # include "common.h"
184 # include "ident.h"
185 # include "irmode.h"
186
187 #ifndef _ENTITY_TYPEDEF_
188 #define _ENTITY_TYPEDEF_
189 /* to resolve recursion between entity.h and type.h */
190 typedef struct entity entity;
191 #endif
192
193 /* for recursive type definiton */
194 /*#ifndef _TYPE_TYPEDEF_ */
195 /*#define _TYPE_TYPEDEF_ */
196 /* to resolve recursion between entity.h and irgraph.h */
197 typedef union type type;
198 /*#endif */
199
200
201 /* visited flag to traverse the type information */
202 extern unsigned long type_visited;
203
204 /*******************************************************************/
205 /** TYPE_CLASS                                                    **/
206 /*******************************************************************/
207
208 typedef struct type_class type_class;
209
210 struct type_class {
211   firm_kind kind;
212   ident *name;             /* needs list with it's entities
213                               does it really??
214                               Entities can be added during their creation. */
215   struct entity **members;        /* to represent inheritance */
216   type_class **subtypes;   /* direct subtypes */
217   type_class **supertypes; /* direct supertypes */
218   unsigned long visit;     /* visited counter for walks of
219                               the type information */
220   int size;                /* Size of an entity of this type.  This is determined
221                               when fixing the layout of this class.  Size must be
222                               given in bytes. */
223
224 };
225
226
227 /* create a new type_class */
228 type_class *new_type_class (ident *name);
229
230 /* manipulate fields of type_class */
231
232 const char  *get_class_name  (type_class *clss);
233 ident       *get_class_ident (type_class *clss);
234
235 /* Not necessary now!
236 void   set_class_name  (type_class *clss, char *name);
237 void   set_class_ident (type_class *clss, ident* ident);
238 */
239
240 void    add_class_member (type_class *clss, entity *member);
241 int     get_class_n_member (type_class *clss);
242 entity *get_class_member (type_class *clss, int pos);
243 void    set_class_member (type_class *clss, entity *member, int pos);
244
245 void        add_class_subtype (type_class *clss,type_class *subtype);
246 int         get_class_n_subtype (type_class *clss);
247 type_class *get_class_subtype (type_class *clss, int pos);
248 void        set_class_subtype (type_class *clss, type_class *subtype, int pos);
249
250 void        add_class_supertype (type_class *clss, type_class *supertype);
251 int         get_class_n_supertype (type_class *clss);
252 type_class *get_class_supertype (type_class *clss, int pos);
253 void        set_class_supertype (type_class *clss, type_class *supertype, int pos);
254
255 int         get_class_size (type_class *clss);
256 void        set_class_size (type_class *clss, int size);
257
258
259 /*******************************************************************/
260 /** TYPE_STRCT                                                   **/
261 /*******************************************************************/
262
263 typedef struct {
264   firm_kind kind;
265   ident *name;
266   entity **members;
267   unsigned long visit;     /* visited counter for walks of the type information */
268 } type_strct;
269
270
271 /* create a new type_strct */
272 type_strct *new_type_strct (ident *name);
273
274 /* manipulate fields of type_strct */
275 const char  *get_strct_name  (type_strct *strct);
276 ident       *get_strct_ident (type_strct *strct);
277
278 void         add_strct_member (type_strct *strct, entity *member);
279 int          get_strct_n_member (type_strct *strct);
280 entity      *get_strct_member (type_strct *strct, int pos);
281 void         set_strct_member (type_strct *strct, int pos, entity *member);
282
283 /*
284 void   set_strct_name  (type_strct *strct, char *name);
285 void   set_strct_ident (type_strct *strct, ident* ident);
286 */
287
288
289 /*******************************************************************/
290 /** TYPE_METHOD                                                   **/
291 /*******************************************************************/
292
293 typedef struct {
294   firm_kind kind;
295   ident *name;         /* Name of the method type.  Usually method
296                           types are not explicitly named (but the entity). */
297   int arity;           /* number of parameters, better n_params */
298   type **param_type;   /* code generation needs this information.
299                           Should it be generated by the frontend,
300                           or does this impose unnecessary work for
301                           optimizations that change the parameters of
302                           methods? */
303   int n_res;           /* number of results */
304   type **res_type;     /* array with result types */
305   unsigned long visit; /* visited counter for walks of the type information */
306 } type_method;
307
308 /* Create a new type_method.
309    Arity is the number of parameters. */
310 type_method *new_type_method (ident *name, int arity, int n_res);
311
312 /* manipulate fields of type_method */
313 const char  *get_method_name  (type_method *method);
314 ident       *get_method_ident (type_method *method);
315 /*
316 void   set_method_name  (type_method *method, char *name);
317 void   set_method_ident (type_method *method, ident* ident); */
318
319 inline int   get_method_n_params (type_method *method);
320 inline int   get_method_arity (type_method *method);
321 /*inline void  set_method_arity (type_method *method, int arity);*/
322 inline type *get_method_param_type(type_method *method, int pos);
323 inline void  set_method_param_type(type_method *method, int pos, type* type);
324
325 inline int   get_method_n_res (type_method *method);
326 /*inline void  set_method_n_res (type_method *method, int n_res);*/
327 inline type *get_method_res_type(type_method *method, int pos);
328 inline void  set_method_res_type(type_method *method, int pos, type* type);
329
330
331 /*******************************************************************/
332 /** TYPE_UNION                                                    **/
333 /*******************************************************************/
334
335 typedef struct {
336   firm_kind kind;
337   ident *name;             /* do I need a name? */
338   int n_types;
339   /* type **unioned_type;    ... or something like that? */
340   unsigned long visit;     /* visited counter for walks of the type information */
341 } type_union;
342
343 /* create a new type_union -- set unioned types by hand. */
344 type_union *new_type_union (ident *name, int n_types);
345
346 /* manipulate fields of type_union */
347 const char  *get_union_name  (type_union *uni);
348 ident       *get_union_ident (type_union *uni);
349 /*
350 void   set_union_name  (type_union *union, char *name);
351 void   set_union_ident (type_union *union, ident* ident);
352 */
353 /*
354 int    get_union_n_types (type_union *union);
355 void   set_union_n_types (type_union *union, int n);
356 type  *get_union_unioned_type (type_union *union, int pos);
357 void   set_union_unioned_type (type_union *union, int pos, type *type);
358 */
359
360 /*******************************************************************/
361 /** TYPE_ARRAY                                                    **/
362 /*******************************************************************/
363
364 /* multidimensional, polyhedric arrays */
365 typedef struct {
366   firm_kind kind;
367   ident *name;
368   int n_dimensions;
369   int *lower_bound;
370   int *upper_bound;
371   type *element_type;
372   unsigned long visit;     /* visited counter for walks of the type information */
373 } type_array;
374
375 /* create a new type array -- set dimension sizes independently */
376 type_array *new_type_array (ident *name, int n_dimensions);
377
378 /* manipulate fields of type_array */
379 const char  *get_array_name  (type_array *array);
380 ident       *get_array_ident (type_array *array);
381 /*
382 void   set_array_name  (type_array *array, char *name);
383 void   set_array_ident (type_array *array, ident* ident);
384 */
385 void  set_array_n_dimensions  (type_array *array, int n);
386 int   get_array_n_dimensions  (type_array *array);
387
388 void  set_array_bounds      (type_array *array, int dimension, int lower_bound,
389                                                               int upper_bound);
390 void  set_array_lower_bound (type_array *array, int dimension, int lower_bound);
391 void  set_array_upper_bound (type_array *array, int dimension, int upper_bound);
392 int   get_array_lower_bound (type_array *array, int dimension);
393 int   get_array_upper_bound (type_array *array, int dimension);
394
395 void  set_array_element_type (type_array *array, type *type);
396 type *get_array_element_type (type_array *array);
397
398 /*******************************************************************/
399 /** TYPE_ENUMERATION                                              **/
400 /*******************************************************************/
401 /** Enums are needed to keep debugging information.  They can as well
402     be lowered to integers. **/
403
404 typedef struct {
405   firm_kind kind;
406   ident *name;
407   /*
408   tarval **enum     * Contains all constant nodes that represent a member
409                       of the enum -- enumerators. */
410   /*
411   ident **enum_name * Contains the names of the enum fields as specified by
412                       the source program */
413   /* is ir_node the propper array member? */
414   unsigned long visit;     /* visited counter for walks of the type information */
415 } type_enumeration;
416
417 /* create a new type enumeration -- set the enumerators independently */
418 type_enumeration *new_type_enumeration (ident *name /* , int n_enums */);
419
420 /* manipulate fields of type_enumeration */
421 const char  *get_enumeration_name  (type_enumeration *enumeration);
422 ident       *get_enumeration_ident (type_enumeration *enumeration);
423 /*
424 void   set_enumeration_name  (type_enumeration *enumeration, char *name);
425 void   set_enumeration_ident (type_enumeration *enumeration, ident* ident);
426 */
427 /*
428 void     set_enumeration_n_enums (type_enumeration *enumeration, int n);
429 int     *get_enumeration_n_enums (type_enumeration *enumeration);
430 void     set_enumeration_enum    (type_enumeration *enumeration, int pos,
431                                  ir_node const);
432 ir_node *get_enumeration_enum    (type_enumeration *enumeration, int pos);
433 */
434
435 /*******************************************************************/
436 /** TYPE_POINTER                                                  **/
437 /*******************************************************************/
438
439 typedef struct {
440   firm_kind kind;
441   ident *name;
442   /* ir_mode *mode;      * The mode to be used for this type.
443                             Not here as there might be several pointer types?
444                             A method get_pointer_mode should read a unique,
445                             global variable. */
446   type *points_to;
447   unsigned long visit;     /* visited counter for walks of the type information */
448 } type_pointer;
449
450 /* create a new type pointer */
451 type_pointer *new_type_pointer (ident *name, type *points_to);
452
453 /* manipulate fields of type_pointer */
454 const char  *get_pointer_name  (type_pointer *pointer);
455 ident *get_pointer_ident (type_pointer *pointer);
456 /*
457 void   set_pointer_name  (type_pointer *pointer, char *name);
458 void   set_pointer_ident (type_pointer *pointer, ident* ident);
459 */
460 void  set_pointer_points_to_type (type_pointer *pointer, type *type);
461 type *get_pointer_points_to_type (type_pointer *pointer);
462
463 /*******************************************************************/
464 /** TYPE_PRIMITIVE                                                **/
465 /*******************************************************************/
466
467 /* primitive, language-defined types */
468 /* What is the type of an entity if it is atomic?  Are alle basic data
469    types classses in Sather? Else this is needed. */
470 typedef struct {
471   firm_kind kind;
472   ident *name;
473   ir_mode *mode;           /* The mode to be used for this type */
474   unsigned long visit;     /* visited counter for walks of the type information */
475 } type_primitive;
476
477 /* create a new type primitive */
478 type_primitive *new_type_primitive (ident *name, ir_mode *mode);
479
480 /* manipulate fields of type_primitive */
481 const char  *get_primitive_name  (type_primitive *primitive);
482 ident *get_primitive_ident (type_primitive *primitive);
483 /*
484 void   set_primitive_name  (type_primitive *primitive, char *name);
485 void   set_primitive_ident (type_primitive *primitive, ident* ident);
486 */
487 ir_mode *get_primitive_mode (type_primitive *primitive);
488 void     set_primitive_mode (type_primitive *primitive, ir_mode *mode);
489
490
491
492
493 /*******************************************************************/
494 /**  To manage all different types the same                       **/
495 /*******************************************************************/
496
497 union type {
498   firm_kind kind;
499   type_class clss;
500   type_strct strct;
501   type_method method;
502   type_array array;
503   type_union uni;  /* union is keyword */
504   type_enumeration enumeration;
505   type_pointer pointer;
506   type_primitive primitive;
507 };
508
509
510 int is_type            (void *thing);
511 int is_type_class      (void *thing);
512 int is_type_strct      (void *thing);
513 int is_type_method     (void *thing);
514 int is_type_union      (void *thing);
515 int is_type_array      (void *thing);
516 int is_type_pointer    (void *thing);
517 int is_type_enumeration(void *thing);
518 int is_type_primitive  (void *thing);
519
520
521
522
523 # endif /* _TYPE_H_ */