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"
38 # define obstack_chunk_alloc xmalloc
39 # define obstack_chunk_free free
41 # define NALLOC(size) obstack_alloc (name_obst, size)
43 /* Local Data Types: */
45 /* Local Variables: */
46 struct obstack *qset_obst = NULL;
47 struct obstack *name_obst = NULL;
49 static desc_t *all_descs = NULL;
51 static int name_id = 0;
56 static int last_col_idx = 0;
58 /* Local Prototypes: */
59 static int pto_name_alloc_color (desc_t*);
60 static void pto_name_set_color (desc_t*, int);
61 # endif /* defined PTO_COLOR */
63 /* ===================================================
65 =================================================== */
67 /* set a nice color to an object descriptor */
68 static void pto_name_set_obj_color (obj_desc_t *obj_desc, int col_idx)
72 for (i = 0; i < obj_desc->n_fields; i ++) {
73 qset_t *ptos = obj_desc->values [i];
75 desc_t *tgt = (desc_t*) qset_start (ptos);
78 pto_name_set_color (tgt, col_idx);
80 tgt = (desc_t*) qset_next (ptos);
85 /* set a nice color to an array descriptor */
86 static void pto_name_set_arr_color (arr_desc_t *arr_desc, int col_idx)
88 qset_t *ptos = arr_desc->value;
90 desc_t *tgt = (desc_t*) qset_start (ptos);
93 pto_name_set_color (tgt, col_idx);
95 tgt = (desc_t*) qset_next (ptos);
99 /* set a nice color to a descriptor */
100 static void pto_name_set_color (desc_t *desc, int col_idx)
102 /* assert (0 == desc->col_idx); */
104 desc->col_idx = col_idx;
106 if (FALSE == desc->visit) {
108 if (object == desc->kind) {
109 pto_name_set_obj_color ((obj_desc_t*) desc, col_idx);
110 } else if (array == desc->kind) {
111 pto_name_set_arr_color ((arr_desc_t*) desc, col_idx);
119 /* allocate nice colors for an object descriptor */
120 static int pto_name_alloc_obj_color (obj_desc_t *obj_desc)
125 for (i = 0; (0 == col_idx) && (i < obj_desc->n_fields); i ++) {
126 qset_t *ptos = obj_desc->values [i];
127 desc_t *tgt = (desc_t*) qset_start (ptos);
129 while ((0 == col_idx) && (NULL != tgt)) {
130 col_idx = pto_name_alloc_color (tgt);
132 tgt = (desc_t*) qset_next (ptos);
139 /* allocate nice colors for an array descriptor */
140 static int pto_name_alloc_arr_color (arr_desc_t *arr_desc)
144 qset_t *ptos = arr_desc->value;
146 desc_t *tgt = (desc_t*) qset_start (ptos);
148 while ((NULL != tgt) && (0 == col_idx)) {
149 col_idx = pto_name_alloc_color (tgt);
150 tgt = (desc_t*) qset_next (ptos);
156 /* allocate nice colors for the given descriptor */
157 static int pto_name_alloc_color (desc_t *desc)
161 if (0 != desc->col_idx) {
162 return (desc->col_idx);
165 if (FALSE == desc->visit) {
167 if (object == desc->kind) {
168 col_idx = pto_name_alloc_obj_color ((obj_desc_t*) desc);
169 } else if (array == desc->kind) {
170 col_idx = pto_name_alloc_arr_color ((arr_desc_t*) desc);
176 col_idx = ++ last_col_idx;
179 pto_name_set_color (desc, col_idx);
184 /* allocate nice colors */
185 static void pto_name_alloc_colors (void)
187 desc_t *desc = all_descs;
189 while (NULL != desc) {
190 pto_name_alloc_color (desc);
195 # endif /* defined PTO_COLOR */
197 /* See whether the given entity is a field. */
198 static int is_field (entity *ent)
200 type *tp = get_entity_type (ent);
202 if (is_primitive_type (tp) || is_pointer_type (tp)) {
203 /* actually, we don't get by by restricting ourselves to pointer types */
210 /* Helper to collect_fields(type*): collect all fields of the given
211 clazz and its super classes into the given obstack. */
212 static void _collect_fields (type *clazz, struct obstack *obst)
214 int n_members = get_class_n_members (clazz);
215 int n_supers = get_class_n_supertypes (clazz);
218 for (i = 0; i < n_members; i ++) {
219 entity *ent = get_class_member (clazz, i);
221 if (is_field (ent)) {
222 if (allocation_static != get_entity_allocation (ent)) {
223 obstack_ptr_grow (obst, ent);
228 for (i = 0; i < n_supers; i ++) {
229 type *s_clazz = get_class_supertype (clazz, i);
231 _collect_fields (s_clazz, obst);
235 /* Collect the fields of the given class and its super classes into an array.
236 The last entry of the array is written NULL. */
237 static entity **collect_fields (type *clazz)
244 if (NULL != get_type_link (clazz)) {
245 DBGPRINT (3, (stdout, "collect_fields(): reusing field list for \"%s\"\n",
246 get_type_name (clazz)));
248 return ((entity **) get_type_link (clazz));
250 DBGPRINT (2, (stdout, "collect_fields(): new field list for \"%s\"\n",
251 get_type_name (clazz)));
254 obstack_init (&obst);
256 _collect_fields (clazz, &obst);
258 /* append terminating NULL */
259 obstack_ptr_grow (&obst, NULL);
261 n_fields = obstack_object_size (&obst) / sizeof (void*);
263 fields = NALLOC(n_fields * sizeof(*fields));
264 tmp = obstack_finish(&obst);
266 memcpy (fields, tmp, n_fields * sizeof(*fields));
268 obstack_free (&obst, NULL);
270 set_type_link (clazz, fields);
275 /* Write the intro text for a name dump into the given stream */
276 static void pto_name_dump_start (FILE *stream)
279 pto_name_alloc_colors ();
280 # endif /* defined PTO_COLOR */
282 fprintf (stream, "digraph \"Names\" {\n");
283 fprintf (stream, "\tgraph [rankdir=\"LR\", ordering=\"out\", size=\"11,7\", rotate=\"90\"];\n");
284 fprintf (stream, "\tnode [shape=\"record\", style=\"filled\"];\n");
285 fprintf (stream, "\tedge [color=\"black\"];\n");
286 fprintf (stream, "\n");
289 /* Write the extro text for a name dump into the given stream */
290 static void pto_name_dump_finish (FILE *stream)
292 fprintf (stream, "}\n");
295 /* Write a node for the given descriptor into the given stream */
296 static void pto_name_dump_desc (desc_t *desc, FILE *stream)
299 const char *tp_name = get_type_name (tp);
302 fprintf (stream, "\t/* %s \"%s\" */\n",
303 object == desc->kind ? "Object" : "Array",
306 fprintf (stream, "\tdesc_%i [label=\"<HEAD>type \\[%i\\]",
309 if (-1 != desc->ctx) {
310 fprintf (stream, ", ctx = %i", desc->ctx);
312 fprintf (stream, " (global)");
315 fprintf (stream, "\\lname=\\\"%s\\\"",
321 ir_graph *graph = get_irn_irg (nd);
322 entity *method = get_irg_entity (graph);
323 const char *ent_name = get_entity_name (method);
324 const char *own_name = get_type_name (get_entity_owner (method));
326 fprintf (stream, "\\lnode=%s\\[%li\\]",
327 get_op_name (get_irn_op (nd)),
328 get_irn_node_nr (nd));
329 fprintf (stream, "\\lgraph=\\\"%s.%s\\\"",
334 if (desc->kind == object) {
335 obj_desc_t *obj_desc = (obj_desc_t*) desc;
338 for (i = 0; i < obj_desc->n_fields; i ++) {
339 entity *field = obj_desc->fields [i];
341 if (is_pointer_type (get_entity_type (field))) {
342 const char *ent_name = get_entity_name (field);
344 fprintf (stream, "|<%i>%s", i, ent_name);
347 } else if (array == desc->kind) {
348 fprintf (stream, "|<arr>[]");
350 assert (0 && "invalid descriptor");
353 /* end label string */
354 fprintf (stream, "\"");
358 const char *fontcolor;
359 int col_idx = desc->col_idx;
360 float hue = (float) col_idx / (float) last_col_idx;
361 float sat = 1.000f; /* 0.300 .. 1.000 */
362 float val = 0.800f; /* 0.300 .. 1.000 */
364 # define MAX_COLORS 12
365 if (last_col_idx > MAX_COLORS) {
366 /* too many colors ... vary value too */
367 float div = (float) MAX_COLORS / (float) last_col_idx;
369 sat = (div * ((col_idx / MAX_COLORS)));
370 val = (div * ((col_idx / MAX_COLORS)));
372 col_idx = col_idx % MAX_COLORS;
373 hue = (float) col_idx / (float) MAX_COLORS;
375 // re-adjust sat and val
377 const float sat_min = 0.200f; /* 0.200 .. 0.400 */
378 const float val_min = 0.300f; /* 0.300 .. 0.400 */
380 sat = sat_min + ((1.0f - sat_min) * sat);
381 val = val_min + ((1.0f - val_min) * val);
386 fprintf (stream, ", color=\"%01.3f, %01.3f, %01.3f\"", hue, sat, val);
388 if ((hue > 0.3) && (sat < 0.5)) {
390 } else if (sat < 0.4) {
396 fprintf (stream, ", fontcolor=\"%s\"", fontcolor);
398 # else /* if defined PTO_COLOR */
399 fprintf (stream, ", color=\"lightgrey\"");
400 # endif /* defined PTO_COLOR */
403 fprintf (stream, "];\n");
404 fprintf (stream, "\n");
407 if (desc->kind == object) {
408 obj_desc_t *obj_desc = (obj_desc_t*) desc;
411 for (i = 0; i < obj_desc->n_fields; i ++) {
412 desc_t *tgt = (desc_t*) qset_start (obj_desc->values [i]);
414 while (NULL != tgt) {
415 fprintf (stream, "\tdesc_%i:%i -> desc_%i:HEAD;\n",
416 desc->id, i, tgt->id);
418 tgt = (desc_t*) qset_next (obj_desc->values [i]);
421 } else if (array == desc->kind) {
422 arr_desc_t *arr_desc = (arr_desc_t*) desc;
424 desc_t *tgt = (desc_t*) qset_start (arr_desc->value);
426 while (NULL != tgt) {
427 fprintf (stream, "\tdesc_%i:arr -> desc_%i:HEAD;\n",
430 tgt = (desc_t*) qset_next (arr_desc->value);
434 fprintf (stream, "\n");
438 /* ===================================================
439 Exported Implementation:
440 =================================================== */
441 /* Find the given descriptor's entry for the given entity */
442 qset_t *get_entry (desc_t *desc, entity *ent)
445 if (desc->kind == object) {
446 obj_desc_t *obj_desc = (obj_desc_t*) desc;
448 const int n_fields = obj_desc->n_fields;
450 for (i = 0; i < n_fields; i ++) {
451 if (ent == obj_desc->fields [i]) {
452 return (obj_desc->values [i]);
456 assert (0 && "entry not found");
457 } else if (desc->kind == array) {
458 arr_desc_t *arr_desc = (arr_desc_t*) desc;
460 return (arr_desc->value);
462 assert (0 && "invalid descriptor");
468 /* get a new descriptor for the given type at the given node */
469 desc_t *new_name (type *tp, ir_node *node, int ctx)
473 assert ((is_class_type (tp) || is_array_type (tp)) && "unsuitable type");
475 DBGPRINT (2, (stdout, "new_name(): new name for type \"%s\"\n",
476 get_type_name (tp)));
479 if (is_class_type (tp)) {
480 obj_desc_t *obj_desc = NALLOC(sizeof(*obj_desc));
484 obj_desc->kind = object;
485 obj_desc->fields = collect_fields (tp);
487 for (n_fields = 0; (NULL != obj_desc->fields [n_fields]); n_fields ++) {
488 /* nothing, just count ... */
491 obj_desc->n_fields = n_fields;
492 obj_desc->values = (qset_t**) NALLOC (n_fields * sizeof (qset_t));
494 for (i = 0; i < n_fields; i ++) {
495 obj_desc->values [i] = qset_new (N_INITIAL_OJBS, qset_obst);
498 desc = (desc_t*) obj_desc;
499 } else if (is_array_type (tp)) {
500 arr_desc_t *arr_desc = (arr_desc_t*) NALLOC (sizeof (arr_desc_t));
502 arr_desc->kind = array;
503 arr_desc->value = qset_new (N_INITIAL_OJBS, qset_obst);
505 desc = (desc_t*) arr_desc;
508 desc->id = name_id ++;
515 desc->prev = all_descs;
521 # define N_GLOB_INITIAL_FIELDS 20
522 static obj_desc_t *obj_glob = NULL;
523 static int n_glob_fields = N_GLOB_INITIAL_FIELDS;
525 /* get a new descriptor for the given (presumably static) entity */
526 desc_t *new_ent_name (entity *ent)
530 type *tp = get_entity_type (ent);
532 assert (is_pointer_type (tp));
533 tp = get_pointer_points_to_type (tp);
534 assert (is_class_type (tp));
536 DBGPRINT (2, (stdout, "new_ent_name(): new name for entity \"%s\"\n",
537 get_entity_name (ent)));
538 DBGEXE (2, (fflush (stdout)));
540 assert (((allocation_static == get_entity_allocation (ent)) ||
541 (allocation_automatic == get_entity_allocation (ent))) &&
542 "not a static/automatic field");
544 if (NULL == obj_glob) {
545 obj_glob = (obj_desc_t*) NALLOC (sizeof (obj_desc_t));
547 obj_glob->id = name_id ++;
549 obj_glob->col_idx = 0;
550 obj_glob->visit = FALSE;
551 obj_glob->kind = object;
552 obj_glob->tp = get_glob_type ();
553 obj_glob->node = NULL;
555 obj_glob->n_fields = 0;
556 obj_glob->fields = (entity**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (entity*));
557 obj_glob->values = (qset_t**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (qset_t*));
559 obj_glob->prev = all_descs;
560 all_descs = (desc_t*) obj_glob;
563 for (i = 0; missing && (i < obj_glob->n_fields); i ++) {
564 if (ent == obj_glob->fields [i]) {
570 if (obj_glob->n_fields == n_glob_fields) {
571 entity **fields = obj_glob->fields;
572 qset_t **values = obj_glob->values;
575 obj_glob->fields = (entity**) NALLOC (n_glob_fields * sizeof (entity*));
576 obj_glob->values = (qset_t**) NALLOC (n_glob_fields * sizeof (qset_t*));
578 memcpy (obj_glob->fields, fields, obj_glob->n_fields * sizeof (entity*));
579 memcpy (obj_glob->values, values, obj_glob->n_fields * sizeof (qset_t*));
585 obj_glob->fields [obj_glob->n_fields ] = ent;
586 obj_glob->values [obj_glob->n_fields ++] = qset_new (N_INITIAL_OJBS, qset_obst);
589 return ((desc_t*) obj_glob);
591 # undef N_GLOB_INITIAL_FIELDS
593 /* Dump all names to a file of the given name */
594 void pto_dump_names (const char *name)
596 desc_t *desc = all_descs;
597 FILE *stream = fopen (name, "w");
600 if (NULL == stream) {
601 fprintf (stderr, "pto_dump_names(): unable to open %s (%s)\n",
602 name, strerror (errno));
606 pto_name_dump_start (stream);
608 while (NULL != desc) {
609 pto_name_dump_desc (desc, stream);
614 pto_name_dump_finish (stream);
618 /* Initialise the name module */
619 void pto_name_init (void)
621 DBGPRINT (3, (stdout, "(%s:%i) pto_name_init()\n", __FILE__, __LINE__));
622 assert (NULL == name_obst);
623 assert (NULL == qset_obst);
625 name_obst = xmalloc (sizeof(*name_obst));
626 qset_obst = xmalloc (sizeof(*qset_obst));
628 obstack_init (name_obst);
629 obstack_init (qset_obst);
632 /* Cleanup the name module */
633 void pto_name_cleanup (void)
635 DBGPRINT (3, (stdout, "(%s:%i) pto_name_cleanup()\n", __FILE__, __LINE__));
636 obstack_free (name_obst, NULL);
637 obstack_free (qset_obst, NULL);
639 memset (name_obst, 0x00, sizeof (struct obstack));
640 memset (qset_obst, 0x00, sizeof (struct obstack));
652 Revision 1.10 2004/12/22 14:43:14 beck
653 made allocations C-like
655 Revision 1.9 2004/12/21 15:34:09 beck
656 removed C99 constructs
660 Revision 1.8 2004/12/15 13:30:30 liekweg
661 use DBGEXE correctly; print yet nicer names
663 Revision 1.7 2004/12/15 09:18:18 liekweg
666 Revision 1.6 2004/12/06 12:52:09 liekweg
669 Revision 1.4 2004/11/30 15:49:27 liekweg
672 Revision 1.3 2004/11/30 14:47:54 liekweg
673 fix initialisation; do correct iteration
675 Revision 1.2 2004/11/24 14:53:56 liekweg
678 Revision 1.1 2004/11/18 16:37:34 liekweg