f6851a38b9edbba18338770b73619fd2e419871c
[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 # include "entity.h"
187
188 /* for recursive type definiton */
189 #ifndef _TYPE_TYPEDEF_
190 #define _TYPE_TYPEDEF_
191 /* to resolve recursion between entity.h and irgraph.h */
192 typedef union type type;
193 #endif
194
195 /* visited flag to traverse the type information */
196 extern unsigned long type_visited;
197
198 /*******************************************************************/
199 /** TYPE_CLASS                                                    **/
200 /*******************************************************************/
201
202 typedef struct type_class type_class;
203
204 struct type_class {
205   firm_kind kind;
206   ident *name;             /* needs list with it's entities
207                               does it really??
208                               Entities can be added during their creation. */
209   struct entity **members;        /* to represent inheritance */
210   type_class **subtypes;   /* direct subtypes */
211   type_class **supertypes; /* direct supertypes */
212   unsigned long visit;     /* visited counter for walks of
213                               the type information */
214 };
215
216
217 /* create a new type_class */
218 type_class *new_type_class (ident *name);
219
220 /* manipulate fields of type_class */
221
222 char  *get_class_name  (type_class *class);
223 ident *get_class_ident (type_class *class);
224
225 /* Not necessary now!
226 void   set_class_name  (type_class *class, char *name);
227 void   set_class_ident (type_class *class, ident* ident);
228 */
229
230 void add_class_member (type_class *class, entity *member);
231 entity *get_class_member (type_class *class, int pos);
232 void set_class_member (type_class *class, entity *member, int pos);
233
234 void add_class_subtype (type_class *class,  type_class *subtype);
235 type_class *get_class_subtype (type_class *class, int pos);
236 void set_class_subtype (type_class *class, type_class *subtype, int pos);
237
238 void add_class_supertype (type_class *class, type_class *supertype);
239 type_class *get_class_supertype (type_class *class, int pos);
240 void set_class_supertype (type_class *class, type_class *supertype, int pos);
241
242
243 /*  get_class_entity_arr
244     get_class_n_entities
245     get_class_entity(class, pos)
246     set_class_entity(class, pos, entity)
247     get_class_sub_arr
248     ...
249     get_class_super_arr
250 */
251
252 /*******************************************************************/
253 /** TYPE_STRCT                                                   **/
254 /*******************************************************************/
255
256 typedef struct {
257   firm_kind kind;
258   ident *name;
259   /** needs list with it's entities -- does it really??
260       Entities can be added during their creation.
261   int n_members;
262   entity **member; **/
263   unsigned long visit;     /* visited counter for walks of the type information */
264 } type_strct;
265
266
267 /* create a new type_strct */
268 type_strct *new_type_strct (ident *name);
269
270 /* manipulate fields of type_strct */
271 char  *get_strct_name  (type_strct *strct);
272 ident *get_strct_ident (type_strct *strct);
273 /*
274 void   set_strct_name  (type_strct *strct, char *name);
275 void   set_strct_ident (type_strct *strct, ident* ident);
276 */
277
278
279 /*******************************************************************/
280 /** TYPE_METHOD                                                   **/
281 /*******************************************************************/
282
283 typedef struct {
284   firm_kind kind;
285   ident *name;         /* Name of the method type.  Usually method
286                           types are not explicitly named (but the entity). */
287   int arity;           /* number of parameters, better n_params */
288   type **param_type;   /* code generation needs this information.
289                           Should it be generated by the frontend,
290                           or does this impose unnecessary work for
291                           optimizations that change the parameters of
292                           methods? */
293   int n_res;           /* number of results */
294   type **res_type;     /* array with result types */
295   unsigned long visit; /* visited counter for walks of the type information */
296 } type_method;
297
298 /* create a new type_method */
299 type_method *new_type_method (ident *name, int arity, int n_res);
300
301 /* manipulate fields of type_method */
302 char  *get_method_name  (type_method *method);
303 ident *get_method_ident (type_method *method);
304 /*
305 void   set_method_name  (type_method *method, char *name);
306 void   set_method_ident (type_method *method, ident* ident); */
307
308 inline int   get_method_arity (type_method *method);
309 inline void  set_method_arity (type_method *method, int arity);
310 inline type *get_method_param_type(type_method *method, int pos);
311 inline void  set_method_param_type(type_method *method, int pos, type* type);
312
313 inline int   get_method_n_res (type_method *method);
314 inline void  set_method_n_res (type_method *method, int n_res);
315 inline type *get_method_res_type(type_method *method, int pos);
316 inline void  set_method_res_type(type_method *method, int pos, type* type);
317
318
319 /*******************************************************************/
320 /** TYPE_UNION                                                    **/
321 /*******************************************************************/
322
323 typedef struct {
324   firm_kind kind;
325   ident *name;             /* do I need a name? */
326   int n_types;
327   /* type **unioned_type;    ... or something like that? */
328   unsigned long visit;     /* visited counter for walks of the type information */
329 } type_union;
330
331 /* create a new type_union -- set unioned types by hand. */
332 type_union *new_type_union (ident *name, int n_types);
333
334 /* manipulate fields of type_union */
335 char  *get_union_name  (type_union *uni);
336 ident *get_union_ident (type_union *uni);
337 /*
338 void   set_union_name  (type_union *union, char *name);
339 void   set_union_ident (type_union *union, ident* ident);
340 */
341 /*
342 int    get_union_n_types (type_union *union);
343 void   set_union_n_types (type_union *union, int n);
344 type  *get_union_unioned_type (type_union *union, int pos);
345 void   set_union_unioned_type (type_union *union, int pos, type *type);
346 */
347
348 /*******************************************************************/
349 /** TYPE_ARRAY                                                    **/
350 /*******************************************************************/
351
352 /* multidimensional, polyhedric arrays */
353 typedef struct {
354   firm_kind kind;
355   ident *name;
356   int n_dimensions;
357   int *lower_bound;
358   int *upper_bound;
359   type *element_type;
360   unsigned long visit;     /* visited counter for walks of the type information */
361 } type_array;
362
363 /* create a new type array -- set dimension sizes independently */
364 type_array *new_type_array (ident *name, int n_dimensions);
365
366 /* manipulate fields of type_array */
367 char  *get_array_name  (type_array *array);
368 ident *get_array_ident (type_array *array);
369 /*
370 void   set_array_name  (type_array *array, char *name);
371 void   set_array_ident (type_array *array, ident* ident);
372 */
373 void  set_array_n_dimensions  (type_array *array, int n);
374 int   get_array_n_dimensions  (type_array *array);
375
376 void  set_array_bounds      (type_array *array, int dimension, int lower_bound,
377                                                               int upper_bound);
378 void  set_array_lower_bound (type_array *array, int dimension, int lower_bound);
379 void  set_array_upper_bound (type_array *array, int dimension, int upper_bound);
380 int   get_array_lower_bound (type_array *array, int dimension);
381 int   get_array_upper_bound (type_array *array, int dimension);
382
383 void  set_array_element_type (type_array *array, type *type);
384 type *get_array_element_type (type_array *array);
385
386 /*******************************************************************/
387 /** TYPE_ENUMERATION                                              **/
388 /*******************************************************************/
389 /** Enums are needed to keep debugging information.  They can as well
390     be lowered to integers. **/
391
392 typedef struct {
393   firm_kind kind;
394   ident *name;
395   /*
396   tarval **enum     * Contains all constant nodes that represent a member
397                       of the enum -- enumerators. */
398   /*
399   ident **enum_name * Contains the names of the enum fields as specified by
400                       the source program */
401   /* is ir_node the propper array member? */
402   unsigned long visit;     /* visited counter for walks of the type information */
403 } type_enumeration;
404
405 /* create a new type enumeration -- set the enumerators independently */
406 type_enumeration *new_type_enumeration (ident *name /* , int n_enums */);
407
408 /* manipulate fields of type_enumeration */
409 char  *get_enumeration_name  (type_enumeration *enumeration);
410 ident *get_enumeration_ident (type_enumeration *enumeration);
411 /*
412 void   set_enumeration_name  (type_enumeration *enumeration, char *name);
413 void   set_enumeration_ident (type_enumeration *enumeration, ident* ident);
414 */
415 /*
416 void     set_enumeration_n_enums (type_enumeration *enumeration, int n);
417 int     *get_enumeration_n_enums (type_enumeration *enumeration);
418 void     set_enumeration_enum    (type_enumeration *enumeration, int pos,
419                                  ir_node const);
420 ir_node *get_enumeration_enum    (type_enumeration *enumeration, int pos);
421 */
422
423 /*******************************************************************/
424 /** TYPE_POINTER                                                  **/
425 /*******************************************************************/
426
427 typedef struct {
428   firm_kind kind;
429   ident *name;
430   /* ir_mode *mode;      * The mode to be used for this type.
431                             Not here as there might be several pointer types?
432                             A method get_pointer_mode should read a unique,
433                             global variable. */
434   type *points_to;
435   unsigned long visit;     /* visited counter for walks of the type information */
436 } type_pointer;
437
438 /* create a new type pointer */
439 type_pointer *new_type_pointer (ident *name, type *points_to);
440
441 /* manipulate fields of type_pointer */
442 char  *get_pointer_name  (type_pointer *pointer);
443 ident *get_pointer_ident (type_pointer *pointer);
444 /*
445 void   set_pointer_name  (type_pointer *pointer, char *name);
446 void   set_pointer_ident (type_pointer *pointer, ident* ident);
447 */
448 void  set_pointer_points_to_type (type_pointer *pointer, type *type);
449 type *get_pointer_points_to_type (type_pointer *pointer);
450
451 /*******************************************************************/
452 /** TYPE_PRIMITIVE                                                **/
453 /*******************************************************************/
454
455 /* primitive, language-defined types */
456 /* What is the type of an entity if it is atomic?  Are alle basic data
457    types classses in Sather? Else this is needed. */
458 typedef struct {
459   firm_kind kind;
460   ident *name;
461   ir_mode *mode;           /* The mode to be used for this type */
462   unsigned long visit;     /* visited counter for walks of the type information */
463 } type_primitive;
464
465 /* create a new type primitive */
466 type_primitive *new_type_primitive (ident *name, ir_mode *mode);
467
468 /* manipulate fields of type_primitive */
469 char  *get_primitive_name  (type_primitive *primitive);
470 ident *get_primitive_ident (type_primitive *primitive);
471 /*
472 void   set_primitive_name  (type_primitive *primitive, char *name);
473 void   set_primitive_ident (type_primitive *primitive, ident* ident);
474 */
475 ir_mode *get_primitive_mode (type_primitive *primitive);
476 void     set_primitive_mode (type_primitive *primitive, ir_mode *mode);
477
478
479
480
481 /*******************************************************************/
482 /**  To manage all different types the same                       **/
483 /*******************************************************************/
484
485 union type {
486   firm_kind kind;
487   type_class clss;
488   type_strct strct;
489   type_method method;
490   type_array array;
491   type_union uni;  /* union is keyword */
492   type_enumeration enumeration;
493   type_pointer pointer;
494   type_primitive primitive;
495 };
496
497
498 int is_type(void *thing);
499
500
501
502
503 # endif /* _TYPE_H_ */