5 File name: ir/ana/pto_name.c
6 Purpose: Names for abstract objects
9 Created: Sat Nov 13 19:35:27 CET 2004
11 Copyright: (c) 1999-2004 Universität Karlsruhe
12 Licence: This file is protected by the GPL - GNU GENERAL PUBLIC LICENSE.
20 pto_name: Names for abstract objects
24 # include "pto_name.h"
25 # include "pto_util.h"
27 # include <string.h> /* for memcpy */
35 # include "pto_debug.h"
39 # define obstack_chunk_alloc xmalloc
40 # define obstack_chunk_free free
42 # define NALLOC(size) obstack_alloc (name_obst, size)
44 /* Local Data Types: */
46 /* Local Variables: */
47 struct obstack *qset_obst = NULL;
48 struct obstack *name_obst = NULL;
50 static desc_t *all_descs = NULL;
52 static int name_id = 0;
57 static int last_col_idx = 0;
59 /* Local Prototypes: */
60 static int pto_name_alloc_color (desc_t*);
61 static void pto_name_set_color (desc_t*, int);
62 # endif /* defined PTO_COLOR */
64 /* ===================================================
66 =================================================== */
68 /* set a nice color to an object descriptor */
69 static void pto_name_set_obj_color (obj_desc_t *obj_desc, int col_idx)
73 for (i = 0; i < obj_desc->n_fields; i ++) {
74 qset_t *ptos = obj_desc->values [i];
76 desc_t *tgt = (desc_t*) qset_start (ptos);
79 pto_name_set_color (tgt, col_idx);
81 tgt = (desc_t*) qset_next (ptos);
86 /* set a nice color to an array descriptor */
87 static void pto_name_set_arr_color (arr_desc_t *arr_desc, int col_idx)
89 qset_t *ptos = arr_desc->value;
91 desc_t *tgt = (desc_t*) qset_start (ptos);
94 pto_name_set_color (tgt, col_idx);
96 tgt = (desc_t*) qset_next (ptos);
100 /* set a nice color to a descriptor */
101 static void pto_name_set_color (desc_t *desc, int col_idx)
103 /* assert (0 == desc->col_idx); */
105 desc->col_idx = col_idx;
107 if (FALSE == desc->visit) {
109 if (object == desc->kind) {
110 pto_name_set_obj_color ((obj_desc_t*) desc, col_idx);
111 } else if (array == desc->kind) {
112 pto_name_set_arr_color ((arr_desc_t*) desc, col_idx);
120 /* allocate nice colors for an object descriptor */
121 static int pto_name_alloc_obj_color (obj_desc_t *obj_desc)
126 for (i = 0; (0 == col_idx) && (i < obj_desc->n_fields); i ++) {
127 qset_t *ptos = obj_desc->values [i];
128 desc_t *tgt = (desc_t*) qset_start (ptos);
130 while ((0 == col_idx) && (NULL != tgt)) {
131 col_idx = pto_name_alloc_color (tgt);
133 tgt = (desc_t*) qset_next (ptos);
140 /* allocate nice colors for an array descriptor */
141 static int pto_name_alloc_arr_color (arr_desc_t *arr_desc)
145 qset_t *ptos = arr_desc->value;
147 desc_t *tgt = (desc_t*) qset_start (ptos);
149 while ((NULL != tgt) && (0 == col_idx)) {
150 col_idx = pto_name_alloc_color (tgt);
151 tgt = (desc_t*) qset_next (ptos);
157 /* allocate nice colors for the given descriptor */
158 static int pto_name_alloc_color (desc_t *desc)
162 if (0 != desc->col_idx) {
163 return (desc->col_idx);
166 if (FALSE == desc->visit) {
168 if (object == desc->kind) {
169 col_idx = pto_name_alloc_obj_color ((obj_desc_t*) desc);
170 } else if (array == desc->kind) {
171 col_idx = pto_name_alloc_arr_color ((arr_desc_t*) desc);
177 col_idx = ++ last_col_idx;
180 pto_name_set_color (desc, col_idx);
185 /* allocate nice colors */
186 static void pto_name_alloc_colors (void)
188 desc_t *desc = all_descs;
190 while (NULL != desc) {
191 pto_name_alloc_color (desc);
196 # endif /* defined PTO_COLOR */
198 /* See whether the given entity is a field. */
199 static int is_field (entity *ent)
201 type *tp = get_entity_type (ent);
203 if (is_Primitive_type (tp) || is_Pointer_type (tp)) {
204 /* actually, we don't get by by restricting ourselves to pointer types */
211 /* Helper to collect_fields(type*): collect all fields of the given
212 clazz and its super classes into the given obstack. */
213 static void _collect_fields (type *clazz, struct obstack *obst)
215 int n_members = get_class_n_members (clazz);
216 int n_supers = get_class_n_supertypes (clazz);
219 for (i = 0; i < n_members; i ++) {
220 entity *ent = get_class_member (clazz, i);
222 if (is_field (ent)) {
223 if (allocation_static != get_entity_allocation (ent)) {
224 obstack_ptr_grow (obst, ent);
229 for (i = 0; i < n_supers; i ++) {
230 type *s_clazz = get_class_supertype (clazz, i);
232 _collect_fields (s_clazz, obst);
236 /* Collect the fields of the given class and its super classes into an array.
237 The last entry of the array is written NULL. */
238 static entity **collect_fields (type *clazz)
245 if (NULL != get_type_link (clazz)) {
246 DBGPRINT (3, (stdout, "%s: reusing field list for \"%s\"\n",
248 get_type_name (clazz)));
250 return ((entity **) get_type_link (clazz));
252 DBGPRINT (2, (stdout, "%s: new field list for \"%s\"\n",
254 get_type_name (clazz)));
257 obstack_init (&obst);
259 _collect_fields (clazz, &obst);
261 /* append terminating NULL */
262 obstack_ptr_grow (&obst, NULL);
264 n_fields = obstack_object_size (&obst) / sizeof (void*);
266 fields = NALLOC (n_fields * sizeof (entity*));
267 tmp = obstack_finish(&obst);
269 memcpy (fields, tmp, n_fields * sizeof (entity*));
271 obstack_free (&obst, NULL);
273 set_type_link (clazz, fields);
278 /* Write the intro text for a name dump into the given stream */
279 static void pto_name_dump_start (FILE *stream)
282 pto_name_alloc_colors ();
283 # endif /* defined PTO_COLOR */
285 fprintf (stream, "digraph \"Names\" {\n");
286 fprintf (stream, "\tgraph [rankdir=\"LR\", ordering=\"out\", size=\"11,7\", rotate=\"90\"];\n");
287 fprintf (stream, "\tnode [shape=\"record\", style=\"filled\"];\n");
288 fprintf (stream, "\tedge [color=\"black\"];\n");
289 fprintf (stream, "\n");
292 /* Write the extro text for a name dump into the given stream */
293 static void pto_name_dump_finish (FILE *stream)
295 fprintf (stream, "}\n");
298 /* Write a node for the given descriptor into the given stream */
299 static void pto_name_dump_desc (desc_t *desc, FILE *stream)
302 const char *tp_name = get_type_name (tp);
305 fprintf (stream, "\t/* %s \"%s\" */\n",
306 object == desc->kind ? "Object" : "Array",
309 fprintf (stream, "\tdesc_%i [label=\"<HEAD>type \\[%i\\]",
312 if (-1 != desc->ctx) {
313 fprintf (stream, ", ctx = %i", desc->ctx);
315 fprintf (stream, " (global)");
318 fprintf (stream, "\\lname=\\\"%s\\\"",
324 ir_graph *graph = get_irn_irg (nd);
325 entity *method = get_irg_entity (graph);
326 const char *ent_name = get_entity_name (method);
327 const char *own_name = get_type_name (get_entity_owner (method));
329 fprintf (stream, "\\lnode=%s\\[%li\\]",
330 get_op_name (get_irn_op (nd)),
331 get_irn_node_nr (nd));
332 fprintf (stream, "\\lgraph=\\\"%s.%s\\\"",
337 if (desc->kind == object) {
338 obj_desc_t *obj_desc = (obj_desc_t*) desc;
341 for (i = 0; i < obj_desc->n_fields; i ++) {
342 entity *field = obj_desc->fields [i];
344 if (is_Pointer_type (get_entity_type (field))) {
345 const char *ent_name = get_entity_name (field);
347 fprintf (stream, "|<%i>%s", i, ent_name);
350 } else if (array == desc->kind) {
351 fprintf (stream, "|<arr>[]");
353 assert (0 && "invalid descriptor");
356 /* end label string */
357 fprintf (stream, "\"");
361 const char *fontcolor;
362 int col_idx = desc->col_idx;
363 float hue = (float) col_idx / (float) last_col_idx;
364 float sat = 1.000f; /* 0.300 .. 1.000 */
365 float val = 0.800f; /* 0.300 .. 1.000 */
367 # define MAX_COLORS 12
368 if (last_col_idx > MAX_COLORS) {
369 /* too many colors ... vary value too */
370 float div = (float) MAX_COLORS / (float) last_col_idx;
372 sat = (div * ((col_idx / MAX_COLORS)));
373 val = (div * ((col_idx / MAX_COLORS)));
375 col_idx = col_idx % MAX_COLORS;
376 hue = (float) col_idx / (float) MAX_COLORS;
378 // re-adjust sat and val
380 const float sat_min = 0.200f; /* 0.200 .. 0.400 */
381 const float val_min = 0.300f; /* 0.300 .. 0.400 */
383 sat = sat_min + ((1.0f - sat_min) * sat);
384 val = val_min + ((1.0f - val_min) * val);
389 fprintf (stream, ", color=\"%01.3f, %01.3f, %01.3f\"", hue, sat, val);
391 if ((hue > 0.3) && (sat < 0.5)) {
393 } else if (sat < 0.4) {
399 fprintf (stream, ", fontcolor=\"%s\"", fontcolor);
401 # else /* if defined PTO_COLOR */
402 fprintf (stream, ", color=\"lightgrey\"");
403 # endif /* defined PTO_COLOR */
406 fprintf (stream, "];\n");
407 fprintf (stream, "\n");
410 if (desc->kind == object) {
411 obj_desc_t *obj_desc = (obj_desc_t*) desc;
414 for (i = 0; i < obj_desc->n_fields; i ++) {
415 desc_t *tgt = (desc_t*) qset_start (obj_desc->values [i]);
417 while (NULL != tgt) {
418 fprintf (stream, "\tdesc_%i:%i -> desc_%i:HEAD;\n",
419 desc->id, i, tgt->id);
421 tgt = (desc_t*) qset_next (obj_desc->values [i]);
424 } else if (array == desc->kind) {
425 arr_desc_t *arr_desc = (arr_desc_t*) desc;
427 desc_t *tgt = (desc_t*) qset_start (arr_desc->value);
429 while (NULL != tgt) {
430 fprintf (stream, "\tdesc_%i:arr -> desc_%i:HEAD;\n",
433 tgt = (desc_t*) qset_next (arr_desc->value);
437 fprintf (stream, "\n");
441 /* ===================================================
442 Exported Implementation:
443 =================================================== */
444 /* Find the given descriptor's entry for the given entity */
445 qset_t *get_entry (desc_t *desc, entity *ent)
448 if (desc->kind == object) {
449 obj_desc_t *obj_desc = (obj_desc_t*) desc;
451 const int n_fields = obj_desc->n_fields;
453 for (i = 0; i < n_fields; i ++) {
454 if (ent == obj_desc->fields [i]) {
455 return (obj_desc->values [i]);
459 assert (0 && "entry not found");
460 } else if (desc->kind == array) {
461 arr_desc_t *arr_desc = (arr_desc_t*) desc;
463 return (arr_desc->value);
465 assert (0 && "invalid descriptor");
471 /* get a new descriptor for the given type at the given node */
472 desc_t *new_name (type *tp, ir_node *node, int ctx)
476 assert ((is_Class_type (tp) || is_Array_type (tp)) && "unsuitable type");
478 DBGPRINT (2, (stdout, "%s: new name for type \"%s\"\n",
480 get_type_name (tp)));
483 if (is_Class_type (tp)) {
484 obj_desc_t *obj_desc = NALLOC (sizeof (obj_desc_t));
488 obj_desc->kind = object;
489 obj_desc->fields = collect_fields (tp);
491 for (n_fields = 0; (NULL != obj_desc->fields [n_fields]); n_fields ++) {
492 /* nothing, just count ... */
495 obj_desc->n_fields = n_fields;
496 obj_desc->values = (qset_t**) NALLOC (n_fields * sizeof (qset_t*));
498 for (i = 0; i < n_fields; i ++) {
499 obj_desc->values [i] = qset_new (N_INITIAL_OJBS, qset_obst);
502 desc = (desc_t*) obj_desc;
503 } else if (is_Array_type (tp)) {
504 arr_desc_t *arr_desc = (arr_desc_t*) NALLOC (sizeof (arr_desc_t));
506 arr_desc->kind = array;
507 arr_desc->value = qset_new (N_INITIAL_OJBS, qset_obst);
509 desc = (desc_t*) arr_desc;
512 desc->id = name_id ++;
519 desc->prev = all_descs;
525 # define N_GLOB_INITIAL_FIELDS 20
526 static obj_desc_t *obj_glob = NULL;
527 static int n_glob_fields = N_GLOB_INITIAL_FIELDS;
529 /* get a new descriptor for the given (presumably static) entity */
530 desc_t *new_ent_name (entity *ent)
534 type *tp = get_entity_type (ent);
536 assert (is_Pointer_type (tp));
537 tp = get_pointer_points_to_type (tp);
538 assert (is_Class_type (tp));
540 DBGPRINT (2, (stdout, "%s: new name for entity \"%s\"\n",
542 get_entity_name (ent)));
543 DBGEXE (2, (fflush (stdout)));
545 assert (((allocation_static == get_entity_allocation (ent)) ||
546 (allocation_automatic == get_entity_allocation (ent))) &&
547 "not a static/automatic field");
549 if (NULL == obj_glob) {
550 obj_glob = (obj_desc_t*) NALLOC (sizeof (obj_desc_t));
552 obj_glob->id = name_id ++;
554 obj_glob->col_idx = 0;
555 obj_glob->visit = FALSE;
556 obj_glob->kind = object;
557 obj_glob->tp = get_glob_type ();
558 obj_glob->node = NULL;
560 obj_glob->n_fields = 0;
561 obj_glob->fields = (entity**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (entity*));
562 obj_glob->values = (qset_t**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (qset_t*));
564 obj_glob->prev = all_descs;
565 all_descs = (desc_t*) obj_glob;
568 for (i = 0; missing && (i < obj_glob->n_fields); i ++) {
569 if (ent == obj_glob->fields [i]) {
575 if (obj_glob->n_fields == n_glob_fields) {
576 entity **fields = obj_glob->fields;
577 qset_t **values = obj_glob->values;
580 obj_glob->fields = (entity**) NALLOC (n_glob_fields * sizeof (entity*));
581 obj_glob->values = (qset_t**) NALLOC (n_glob_fields * sizeof (qset_t*));
583 memcpy (obj_glob->fields, fields, obj_glob->n_fields * sizeof (entity*));
584 memcpy (obj_glob->values, values, obj_glob->n_fields * sizeof (qset_t*));
590 obj_glob->fields [obj_glob->n_fields ] = ent;
591 obj_glob->values [obj_glob->n_fields ++] = qset_new (N_INITIAL_OJBS, qset_obst);
594 return ((desc_t*) obj_glob);
596 # undef N_GLOB_INITIAL_FIELDS
598 /* Dump all names to a file of the given name */
599 void pto_dump_names (const char *name)
601 desc_t *desc = all_descs;
602 FILE *stream = fopen (name, "w");
605 if (NULL == stream) {
606 fprintf (stderr, "%s: unable to open %s (%s)\n",
607 __FUNCTION__, name, strerror (errno));
611 pto_name_dump_start (stream);
613 while (NULL != desc) {
614 pto_name_dump_desc (desc, stream);
619 pto_name_dump_finish (stream);
623 /* Initialise the name module */
624 void pto_name_init (void)
626 DBGPRINT (3, (stdout, "%s\n", __FUNCTION__));
627 assert (NULL == name_obst);
628 assert (NULL == qset_obst);
630 name_obst = xmalloc (sizeof (struct obstack));
631 qset_obst = xmalloc (sizeof (struct obstack));
633 obstack_init (name_obst);
634 obstack_init (qset_obst);
637 /* Cleanup the name module */
638 void pto_name_cleanup (void)
640 DBGPRINT (3, (stdout, "%s\n", __FUNCTION__));
641 obstack_free (name_obst, NULL);
642 obstack_free (qset_obst, NULL);
644 memset (name_obst, 0x00, sizeof (struct obstack));
645 memset (qset_obst, 0x00, sizeof (struct obstack));
657 Revision 1.13 2005/02/11 10:21:28 beck
658 get_entry now always returns a value
660 Revision 1.12 2005/01/14 14:13:56 liekweg
661 fix gnu extension, fix fprintf's, fix allocs
663 Revision 1.11 2005/01/05 14:25:54 beck
664 renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG fronten
666 Revision 1.10 2004/12/22 14:43:14 beck
667 made allocations C-like
669 Revision 1.9 2004/12/21 15:34:09 beck
670 removed C99 constructs
674 Revision 1.8 2004/12/15 13:30:30 liekweg
675 use DBGEXE correctly; print yet nicer names
677 Revision 1.7 2004/12/15 09:18:18 liekweg
680 Revision 1.6 2004/12/06 12:52:09 liekweg
683 Revision 1.4 2004/11/30 15:49:27 liekweg
686 Revision 1.3 2004/11/30 14:47:54 liekweg
687 fix initialisation; do correct iteration
689 Revision 1.2 2004/11/24 14:53:56 liekweg
692 Revision 1.1 2004/11/18 16:37:34 liekweg