*** empty log message ***
[libfirm] / ir / tr / type.c
1
2 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
3 ** All rights reserved.
4 **
5 ** Authors: Martin Trapp, Christian Schaefer &
6 **          Goetz Lindenmaier
7 **
8 ** type.c: datastructures to hold type information.
9 */
10
11 # include "type.h"
12 # include "irprog.h"  /* So that constructors can add the type to global
13                          data structure. */
14 # include "array.h"
15 # include "ident_t.h"
16
17 unsigned long type_visited = 0;
18
19 void
20 init (void)
21 {
22 }
23
24 /*******************************************************************/
25 /** TYPE_CLASS                                                    **/
26 /*******************************************************************/
27
28 type_class *
29 new_type_class (ident *name)//, int members)
30 {
31   type_class *res;
32
33   res = (type_class *) xmalloc (sizeof (type_class));
34   add_irp_type((type *) res);   /* Remember the new type global. */
35   res->kind = k_type_class;
36   res->name = name;
37
38   res->members = NEW_ARR_F (entity *, 1);
39   res->subtypes = NEW_ARR_F (type_class *, 1);
40   res->supertypes = NEW_ARR_F (type_class *, 1);
41
42   res->visit = 0;
43
44   return res;
45 }
46
47 /* manipulate fields of type_class */
48 const char *
49 get_class_name  (type_class *clss) {
50   assert(clss);
51   return id_to_str(clss->name);
52 }
53
54 /* field: ident */
55 ident *
56 get_class_ident (type_class *clss) {
57   assert(clss);
58   return clss->name;
59 }
60
61 /*
62 void   set_class_name  (type_class *clss, char *name);
63 void   set_class_ident (type_class *clss, ident* ident);
64 */
65
66 /* field: member */
67 void
68 add_class_member (type_class *clss, entity *member)
69 {
70   ARR_APP1 (entity *, clss->members, member);
71 }
72
73 entity *
74 get_class_member (type_class *clss, int pos)
75 {
76   assert (clss);
77   return clss->members[pos+1];
78 }
79
80 void
81 set_class_member (type_class *clss, entity *member, int pos)
82 {
83   clss->members[pos+1] = member;
84 }
85
86 int
87 get_class_n_member (type_class *clss)
88 {
89   int res;
90
91   assert(clss);
92   res = (ARR_LEN (clss->members))-1;
93   return res;
94 }
95
96 /* field: subtype */
97 void
98 add_class_subtype (type_class *clss,  type_class *subtype)
99 {
100   ARR_APP1 (type_class *, clss->subtypes, subtype);
101 }
102
103 type_class *
104 get_class_subtype (type_class *clss, int pos)
105 {
106   assert (clss);
107   return clss->subtypes[pos+1];
108 }
109
110 void
111 set_class_subtype (type_class *clss, type_class *subtype, int pos)
112 {
113   clss->subtypes[pos+1] = subtype;
114 }
115
116 int
117 get_class_n_subtype (type_class *clss)
118 {
119   assert(clss);
120   return (ARR_LEN (clss->subtypes))-1;
121 }
122
123 /* field: supertype */
124 void
125 add_class_supertype (type_class *clss, type_class *supertype)
126 {
127   ARR_APP1 (type_class *, clss->supertypes, supertype);
128 }
129
130 type_class *
131 get_class_supertype (type_class *clss, int pos)
132 {
133   assert (clss);
134   return clss->supertypes[pos+1];
135 }
136
137 void
138 set_class_supertype (type_class *clss, type_class *supertype, int pos)
139 {
140   clss->supertypes[pos+1] = supertype;
141 }
142
143 int
144 get_class_n_supertype (type_class *clss)
145 {
146   assert(clss);
147   return (ARR_LEN (clss->supertypes))-1;
148 }
149
150 int
151 get_class_size (type_class *clss) {
152   assert(clss);
153   return clss->size;
154 }
155
156 void
157 set_class_size (type_class *clss, int size) {
158   assert(clss);
159   clss->size = size;
160 }
161
162
163 /*******************************************************************/
164 /** TYPE_STRCT                                                   **/
165 /*******************************************************************/
166
167 type_strct *
168 new_type_strct (ident *name)//, int members)
169 {
170   type_strct *res;
171
172   res = (type_strct *) xmalloc (sizeof (type_strct));
173   add_irp_type((type *) res);   /* Remember the new type global. */
174   res->kind = k_type_strct;
175   res->name = name;
176
177   res->members = NEW_ARR_F (entity *, 1);
178   res->visit = 0;
179
180   return res;
181 }
182
183 /* manipulate fields of type_strct */
184
185 const char *
186 get_strct_name  (type_strct *strct) {
187   assert(strct);
188   return ID_TO_STR(strct->name);
189 }
190
191
192 ident *
193 get_strct_ident (type_strct *strct) {
194   assert(strct);
195   return strct->name;
196 }
197
198 int
199 get_strct_n_member (type_strct *strct)
200 {
201   int res;
202
203   assert(strct);
204   res = (ARR_LEN (strct->members))-1;
205   return res;
206 }
207
208 void
209 add_strct_member (type_strct *strct, entity *member)
210 {
211   ARR_APP1 (type_strct *, strct->members, member);
212 }
213
214 entity *
215 get_strct_member (type_strct *strct, int pos)
216 {
217   assert (strct);
218   return strct->members[pos+1];
219 }
220
221 void
222 set_strct_member (type_strct *strct, int pos, entity *member)
223 {
224   strct->members[pos+1] = member;
225 }
226
227 /*
228 void   set_strct_name  (type_strct *strct, char *name);
229 void   set_strct_ident (type_strct *strct, ident* ident);
230 */
231
232
233 /*******************************************************************/
234 /** TYPE_METHOD                                                   **/
235 /*******************************************************************/
236
237 /* create a new type_method */
238 type_method *
239 new_type_method (ident *name, int arity, int n_res)
240 {
241   type_method *res;
242
243   res = (type_method *) xmalloc (sizeof (type_method));
244   add_irp_type((type *) res);   /* Remember the new type global. */
245   res->kind = k_type_method;
246
247   res->name = name;   // do I need the name, or is the name in entity sufficient?
248   res->arity = arity;
249   res->param_type = (type **) xmalloc (sizeof (type *) * arity);
250   res->n_res  = n_res;
251   res->res_type = (type **) xmalloc (sizeof (type *) * n_res);
252
253   res->visit = 0;
254
255   return res;
256 }
257
258 /* manipulate fields of type_method */
259 const char *
260 get_method_name  (type_method *method) {
261   assert(method);
262   return ID_TO_STR(method->name);
263 }
264
265 ident *
266 get_method_ident (type_method *method) {
267   assert(method);
268   return method->name;
269 }
270
271 /*
272 void   set_method_name  (type_method *method, char *name);
273 void   set_method_ident (type_method *method, ident* ident);
274 */
275
276
277 inline int
278 get_method_n_params (type_method *method) {
279   return method->arity;
280 }
281
282 inline int
283 get_method_arity (type_method *method) {
284   return method->arity;
285 }
286
287 /*
288 inline void
289 set_method_arity (type_method *method, int arity) {
290   method->arity = arity;
291   / change array size, somehow copy.  *
292 }
293 */
294
295 inline type *
296 get_method_param_type(type_method *method, int pos) {
297   return method->param_type[pos];
298 }
299
300 inline void
301 set_method_param_type(type_method *method, int pos, type* type) {
302   method->param_type[pos] = type;
303 }
304
305
306 inline int
307 get_method_n_res (type_method *method) {
308   return method->n_res;
309 }
310
311 /*
312 inline void
313 set_method_n_res (type_method *method, int n_res) {
314   method->n_res = n_res;
315 }
316 */
317
318 inline type *
319 get_method_res_type(type_method *method, int pos) {
320   return method->res_type[pos];
321 }
322
323 inline void
324 set_method_res_type(type_method *method, int pos, type* type) {
325   method->res_type[pos] = type;
326 }
327
328
329 /*******************************************************************/
330 /** TYPE_UNION                                                    **/
331 /*******************************************************************/
332
333 /* create a new type_union -- set unioned types by hand. */
334 type_union *
335 new_type_union (ident *name, int n_types)
336 {
337   type_union *res;
338
339   res = (type_union *) xmalloc (sizeof (type_union));
340   add_irp_type((type *) res);   /* Remember the new type global. */
341   res->kind = k_type_union;
342   res->name = name;   // do I need a name?
343   res->n_types = n_types;
344   /*
345   res->unioned_type = (int *) xmalloc (sizeof (int) * n_types);
346   */
347
348   res->visit = 0;
349
350   return res;
351 }
352
353 /* manipulate fields of type_union */
354 /*
355 char *
356 get_union_name  (type_union *uni) {
357   assert(uni);
358   return ID_TO_STR(uni->name);
359 }
360 */
361
362 ident *
363 get_union_ident (type_union *uni) {
364   assert(uni);
365   return uni->name;
366 }
367
368 /*
369 void   set_union_name  (type_union *union, char *name);
370 void   set_union_ident (type_union *union, ident* ident);
371 */
372 /*
373 int    get_union_n_types (type_union *union);
374 void   set_union_n_types (type_union *union, int n);
375 type  *get_union_unioned_type (type_union *union, int pos);
376 void   set_union_unioned_type (type_union *union, int pos, type *type);
377 */
378
379 /*******************************************************************/
380 /** TYPE_ARRAY                                                    **/
381 /*******************************************************************/
382
383 /* create a new type_array */
384 inline type_array *
385 new_type_array (ident *name, int n_dimensions)
386 {
387   type_array *res;
388
389   res = (type_array *) xmalloc (sizeof (type_array));
390   add_irp_type((type *) res);   /* Remember the new type global. */
391   res->kind = k_type_array;
392   res->name = name;
393   res->n_dimensions = n_dimensions;
394   res->lower_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
395   res->upper_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
396
397   res->visit = 0;
398
399   return res;
400 }
401
402 /* manipulate fields of type_array */
403 /*
404 char *
405 get_array_name  (type_array *array) {
406   assert(array);
407   return ID_TO_STR(array->name);
408 }
409 */
410
411 ident *
412 get_array_ident (type_array *array) {
413   assert(array);
414   return array->name;
415 }
416
417 /*
418 void   set_array_name  (type_array *array, char *name);
419 void   set_array_ident (type_array *array, ident* ident);
420 */
421
422 inline void
423 set_array_dimensions (type_array* array, int n) {
424   array->n_dimensions = n;
425 }
426
427 inline int
428 get_array_dimensions (type_array* array) {
429   return array->n_dimensions;
430 }
431
432 inline void
433 set_array_bounds (type_array* array, int dimension, int lower_bound,
434                   int upper_bound) {
435   array->lower_bound[dimension-1] = lower_bound;
436   array->upper_bound[dimension-1] = upper_bound;
437 }
438
439 inline void
440 set_array_lower_bound (type_array* array, int dimension, int lower_bound) {
441   array->lower_bound[dimension-1] = lower_bound;
442 }
443
444 inline void
445 set_array_upper_bound (type_array* array, int dimension, int upper_bound) {
446   array->upper_bound[dimension-1] = upper_bound;
447 }
448
449 inline int
450 get_array_lower_bound (type_array* array, int dimension) {
451   return array->lower_bound[dimension-1];
452 }
453
454 inline int
455 get_array_upper_bound (type_array* array, int dimension) {
456   return array->upper_bound[dimension-1];
457 }
458
459 inline void set_array_element_type (type_array *array, type *type) {
460   array->element_type = type;
461 }
462
463 inline type *
464 get_array_element_type (type_array *array) {
465   return array->element_type;
466 }
467
468
469 /*******************************************************************/
470 /** TYPE_ENUMERATION                                              **/
471 /*******************************************************************/
472
473 /* create a new type enumeration -- set the enumerators independently */
474 type_enumeration *
475 new_type_enumeration (ident *name /* , int n_enums */)
476 {
477   type_enumeration *res;
478
479   res = (type_enumeration *) xmalloc (sizeof (type_enumeration));
480   add_irp_type((type *) res);   /* Remember the new type global. */
481   res->kind = k_type_enumeration;
482   res->name = name;
483   /*
484   res->n_enums = n_enums;
485   res->enum = (int *) xmalloc (sizeof (int) * n_enums);
486   */
487
488   res->visit = 0;
489
490   return res;
491 }
492
493 /* manipulate fields of type_enumeration */
494 /*
495 char *
496 get_enumeration_name  (type_enumeration *enumeration) {
497   assert(enumeration);
498   return ID_TO_STR(enumeration->name);
499 }
500 */
501
502 ident *
503 get_enumeration_ident (type_enumeration *enumeration) {
504   assert(enumeration);
505   return enumeration->name;
506 }
507
508 /*
509 void   set_enumeration_name  (type_enumeration *enumeration, char *name);
510 void   set_enumeration_ident (type_enumeration *enumeration, ident* ident);
511 */
512 /*
513 void     set_enumeration_n_enums (type_enumeration *enumeration, int n);
514 int     *get_enumeration_n_enums (type_enumeration *enumeration);
515 void     set_enumeration_enum    (type_enumeration *enumeration, int pos,
516                                  ir_node const);
517 ir_node *get_enumeration_enum    (type_enumeration *enumeration, int pos);
518 */
519
520
521 /*******************************************************************/
522 /** TYPE_POINTER                                                  **/
523 /*******************************************************************/
524
525 /* create a new type pointer */
526 type_pointer *
527 new_type_pointer (ident *name, type *points_to)
528 {
529   type_pointer *res;
530
531   res = (type_pointer *) xmalloc (sizeof (type_pointer));
532   add_irp_type((type *) res);   /* Remember the new type global. */
533   res->kind = k_type_pointer;
534   res->name = name;
535   res->points_to = points_to;
536
537   res->visit = 0;
538
539   return res;
540 }
541
542 /* manipulate fields of type_pointer */
543 /*
544 char *
545 get_pointer_name  (type_pointer *pointer) {
546   assert(pointer);
547   return ID_TO_STR(pointer->name);
548 }
549 */
550
551 ident *
552 get_pointer_ident (type_pointer *pointer) {
553   assert(pointer);
554   return pointer->name;
555 }
556
557 /*
558 void   set_pointer_name  (type_pointer *pointer, char *name);
559 void   set_pointer_ident (type_pointer *pointer, ident* ident);
560 */
561
562 inline void
563 set_pointer_points_to_type (type_pointer *pointer, type* type) {
564   pointer->points_to = type;
565 }
566
567 inline type *
568 get_pointer_points_to_type (type_pointer *pointer) {
569   return pointer->points_to;
570 }
571
572
573 /*******************************************************************/
574 /** TYPE_PRIMITIVE                                                **/
575 /*******************************************************************/
576
577 /* create a new type_primitive */
578 inline type_primitive *
579 new_type_primitive (ident *name, ir_mode *mode)
580 {
581   type_primitive *res;
582
583   res = (type_primitive *) xmalloc (sizeof (type_primitive));
584   add_irp_type((type *) res);   /* Remember the new type global. */
585   res->kind = k_type_primitive;
586   res->name = name;
587   res->mode = mode;
588
589   res->visit = 0;
590
591   return res;
592 }
593
594 /* manipulate fields of type_primitive */
595
596 const char  *
597 get_primitive_name  (type_primitive *primitive) {
598   assert(primitive);
599   return ID_TO_STR(primitive->name);
600 }
601
602
603 ident *
604 get_primitive_ident (type_primitive *primitive) {
605   assert(primitive);
606   return primitive->name;
607 }
608 /*
609 void   set_primitive_name  (type_primitive *primitive, char *name);
610 void   set_primitive_ident (type_primitive *primitive, ident* ident);
611 */
612
613 inline ir_mode *
614 get_primitive_mode (type_primitive *primitive) {
615   return primitive->mode;
616 }
617
618 inline void
619 set_primitive_mode (type_primitive *primitive, ir_mode *mode) {
620   primitive->mode = mode;
621 }
622
623
624
625
626 /*******************************************************************/
627 /**  To manage all different types the same                       **/
628 /*******************************************************************/
629
630
631 int
632 is_type(void *thing) {
633   firm_kind kind;
634
635   kind = get_kind(thing);
636   if (   (kind == k_type_class)
637       || (kind == k_type_strct)
638       || (kind == k_type_method)
639       || (kind == k_type_union)
640       || (kind == k_type_array)
641       || (kind == k_type_enumeration)
642       || (kind == k_type_pointer)
643       || (kind == k_type_primitive))
644     return 1;
645   else
646     return 0;
647 }
648
649 int
650 is_type_class(void *thing) {
651   if (get_kind(thing) == k_type_class) return 1;
652   else return 0;
653 }
654
655 int
656 is_type_strct(void *thing) {
657   if (get_kind(thing) == k_type_strct) return 1;
658   else return 0;
659 }
660
661 int
662 is_type_method(void *thing) {
663   if (get_kind(thing) == k_type_method) return 1;
664   else return 0;
665 }
666
667 int
668 is_type_union(void *thing) {
669   if (get_kind(thing) == k_type_union) return 1;
670   else return 0;
671 }
672
673 int
674 is_type_array(void *thing) {
675   if (get_kind(thing) == k_type_array) return 1;
676   else return 0;
677 }
678
679 int
680 is_type_pointer(void *thing) {
681   if (get_kind(thing) == k_type_pointer) return 1;
682   else return 0;
683 }
684
685 int
686 is_type_enumeration(void *thing) {
687   if (get_kind(thing) == k_type_enumeration) return 1;
688   else return 0;
689 }
690
691 int
692 is_type_primitive(void *thing) {
693   if (get_kind(thing) == k_type_primitive) return 1;
694   else return 0;
695 }