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