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)
159 if (0 != desc->col_idx) {
160 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)
239 if (NULL != get_type_link (clazz)) {
240 DBGPRINT (3, (stdout, "%s: reusing field list for \"%s\"\n",
241 __FUNCTION__, get_type_name (clazz)));
243 return ((entity **) get_type_link (clazz));
245 DBGPRINT (2, (stdout, "%s: new field list for \"%s\"\n",
246 __FUNCTION__, get_type_name (clazz)));
251 obstack_init (&obst);
253 _collect_fields (clazz, &obst);
255 /* append terminating NULL */
256 int *the_null = NULL;
257 obstack_ptr_grow (&obst, the_null);
259 int n_fields = obstack_object_size (&obst) / sizeof (void*);
261 entity ** fields = (entity**) NALLOC (n_fields * sizeof (entity*));
262 void *tmp = obstack_finish (&obst);
264 memcpy (fields, tmp, n_fields * sizeof (entity*));
266 obstack_free (&obst, NULL);
268 set_type_link (clazz, (void*) fields);
273 /* Write the intro text for a name dump into the given stream */
274 static void pto_name_dump_start (FILE *stream)
277 pto_name_alloc_colors ();
278 # endif /* defined PTO_COLOR */
280 fprintf (stream, "digraph \"Names\" {\n");
281 fprintf (stream, "\tgraph [rankdir=\"LR\", ordering=\"out\", size=\"11,7\", rotate=\"90\"];\n");
282 fprintf (stream, "\tnode [shape=\"record\", style=\"filled\"];\n");
283 fprintf (stream, "\tedge [color=\"black\"];\n");
284 fprintf (stream, "\n");
287 /* Write the extro text for a name dump into the given stream */
288 static void pto_name_dump_finish (FILE *stream)
290 fprintf (stream, "}\n");
293 /* Write a node for the given descriptor into the given stream */
294 static void pto_name_dump_desc (desc_t *desc, FILE *stream)
297 const char *tp_name = get_type_name (tp);
299 fprintf (stream, "\t/* %s \"%s\" */\n",
300 object == desc->kind ? "Object" : "Array",
303 fprintf (stream, "\tdesc_%i [label=\"<HEAD>type \\[%i\\]\\lname=\\\"%s\\\"",
308 ir_node *nd = desc->node;
311 ir_graph *graph = get_irn_irg (nd);
312 entity *method = get_irg_entity (graph);
313 const char *ent_name = get_entity_name (method);
314 const char *own_name = get_type_name (get_entity_owner (method));
316 fprintf (stream, "\\lnode=%s\\[%li\\]",
317 get_op_name (get_irn_op (nd)),
318 get_irn_node_nr (nd));
319 fprintf (stream, "\\lgraph=\\\"%s.%s\\\"",
324 if (desc->kind == object) {
325 obj_desc_t *obj_desc = (obj_desc_t*) desc;
328 for (i = 0; i < obj_desc->n_fields; i ++) {
329 entity *field = obj_desc->fields [i];
331 if (is_pointer_type (get_entity_type (field))) {
332 const char *ent_name = get_entity_name (field);
334 fprintf (stream, "|<%i>%s", i, ent_name);
337 } else if (array == desc->kind) {
338 arr_desc_t *arr_desc = (arr_desc_t*) desc;
340 fprintf (stream, "|<arr>[]");
342 assert (0 && "invalid descriptor");
345 /* end label string */
346 fprintf (stream, "\"");
349 int col_idx = desc->col_idx;
350 float hue = (float) col_idx / (float) last_col_idx;
351 float sat = 1.000; /* 0.300 .. 1.000 */
352 float val = 1.000; /* 0.300 .. 1.000 */
354 # define MAX_COLORS 12
355 if (last_col_idx > MAX_COLORS) {
356 /* too many colors ... vary value too */
357 float div = (float) MAX_COLORS / (float) last_col_idx;
359 sat = (div * ((col_idx / MAX_COLORS)));
360 val = (div * ((col_idx / MAX_COLORS)));
362 col_idx = col_idx % MAX_COLORS;
363 hue = (float) col_idx / (float) MAX_COLORS;
367 // re-adjust sat and val
369 const float sat_min = 0.200; /* 0.200 .. 0.400 */
370 const float val_min = 0.300; /* 0.300 .. 0.400 */
372 sat = sat_min + ((1.0 - sat_min) * sat);
373 val = val_min + ((1.0 - val_min) * val);
376 fprintf (stream, ", color=\"%01.3f, %01.3f, %01.3f\"", hue, sat, val);
380 if ((hue > 0.3) && (sat < 0.5)) {
382 } else if (sat < 0.4) {
388 fprintf (stream, ", fontcolor=\"%s\"", fontcolor);
389 # endif /* defined PTO_COLOR */
392 fprintf (stream, "];\n");
393 fprintf (stream, "\n");
396 if (desc->kind == object) {
397 obj_desc_t *obj_desc = (obj_desc_t*) desc;
400 for (i = 0; i < obj_desc->n_fields; i ++) {
401 desc_t *tgt = (desc_t*) qset_start (obj_desc->values [i]);
403 while (NULL != tgt) {
404 fprintf (stream, "\tdesc_%i:%i -> desc_%i:HEAD;\n",
405 desc->id, i, tgt->id);
407 tgt = (desc_t*) qset_next (obj_desc->values [i]);
410 } else if (array == desc->kind) {
411 arr_desc_t *arr_desc = (arr_desc_t*) desc;
413 desc_t *tgt = (desc_t*) qset_start (arr_desc->value);
415 while (NULL != tgt) {
416 fprintf (stream, "\tdesc_%i:arr -> desc_%i:HEAD;\n",
419 tgt = (desc_t*) qset_next (arr_desc->value);
423 fprintf (stream, "\n");
427 /* ===================================================
428 Exported Implementation:
429 =================================================== */
430 /* Find the given descriptor's entry for the given entity */
431 qset_t *get_entry (desc_t *desc, entity *ent)
434 if (desc->kind == object) {
435 obj_desc_t *obj_desc = (obj_desc_t*) desc;
437 const int n_fields = obj_desc->n_fields;
439 for (i = 0; i < n_fields; i ++) {
440 if (ent == obj_desc->fields [i]) {
441 return (obj_desc->values [i]);
445 assert (0 && "entry not found");
446 } else if (desc->kind = array) {
447 arr_desc_t *arr_desc = (arr_desc_t*) desc;
449 return (arr_desc->value);
451 assert (0 && "invalid descriptor");
456 /* get a new descriptor for the given type at the given node */
457 desc_t *new_name (type *tp, ir_node *node)
461 assert ((is_class_type (tp) || is_array_type (tp)) && "unsuitable type");
463 DBGPRINT (2, (stdout, "%s: new name for type \"%s\"\n", __FUNCTION__,
464 get_type_name (tp)));
467 if (is_class_type (tp)) {
468 obj_desc_t *obj_desc = (obj_desc_t*) NALLOC (sizeof (obj_desc_t));
472 obj_desc->kind = object;
473 obj_desc->fields = collect_fields (tp);
475 for (n_fields = 0; (NULL != obj_desc->fields [n_fields]); n_fields ++) {
476 /* nothing, just count ... */
479 obj_desc->n_fields = n_fields;
480 obj_desc->values = (qset_t**) NALLOC (n_fields * sizeof (qset_t));
482 for (i = 0; i < n_fields; i ++) {
483 obj_desc->values [i] = qset_new (N_INITIAL_OJBS, qset_obst);
486 desc = (desc_t*) obj_desc;
487 } else if (is_array_type (tp)) {
488 arr_desc_t *arr_desc = (arr_desc_t*) NALLOC (sizeof (arr_desc_t));
490 arr_desc->kind = array;
491 arr_desc->value = qset_new (N_INITIAL_OJBS, qset_obst);
493 desc = (desc_t*) arr_desc;
496 desc->id = name_id ++;
502 desc->prev = all_descs;
508 # define N_GLOB_INITIAL_FIELDS 20
509 static obj_desc_t *obj_glob = NULL;
510 static int n_glob_fields = N_GLOB_INITIAL_FIELDS;
512 /* get a new descriptor for the given (presumably static) entity */
513 desc_t *new_ent_name (entity *ent)
517 type *tp = get_entity_type (ent);
519 assert (is_pointer_type (tp));
520 tp = get_pointer_points_to_type (tp);
521 assert (is_class_type (tp));
523 assert (((allocation_static == get_entity_allocation (ent)) ||
524 (allocation_automatic == get_entity_allocation (ent))) &&
525 "not a static/automatic field");
527 if (NULL == obj_glob) {
528 obj_glob = (obj_desc_t*) NALLOC (sizeof (obj_desc_t));
530 obj_glob->id = name_id ++;
531 obj_glob->col_idx = 0;
532 obj_glob->visit = FALSE;
533 obj_glob->kind = object;
534 obj_glob->tp = get_glob_type ();
535 obj_glob->node = NULL;
537 obj_glob->n_fields = 0;
538 obj_glob->fields = (entity**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (entity*));
539 obj_glob->values = (qset_t**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (qset_t*));
541 obj_glob->prev = all_descs;
542 all_descs = (desc_t*) obj_glob;
545 for (i = 0; missing && (i < obj_glob->n_fields); i ++) {
546 if (ent == obj_glob->fields [i]) {
552 if (obj_glob->n_fields == n_glob_fields) {
553 entity **fields = obj_glob->fields;
554 qset_t **values = obj_glob->values;
557 obj_glob->fields = (entity**) NALLOC (n_glob_fields * sizeof (entity*));
558 obj_glob->values = (qset_t**) NALLOC (n_glob_fields * sizeof (qset_t*));
560 memcpy (obj_glob->fields, fields, obj_glob->n_fields * sizeof (entity*));
561 memcpy (obj_glob->values, values, obj_glob->n_fields * sizeof (qset_t*));
567 obj_glob->fields [obj_glob->n_fields ] = ent;
568 obj_glob->values [obj_glob->n_fields ++] = qset_new (N_INITIAL_OJBS, qset_obst);
571 return ((desc_t*) obj_glob);
573 # undef N_GLOB_INITIAL_FIELDS
575 /* Dump all names to a file of the given name */
576 void pto_dump_names (const char *name)
578 desc_t *desc = all_descs;
579 FILE *stream = fopen (name, "w");
582 if (NULL == stream) {
583 fprintf (stderr, "%s: unable to open %s (%s)\n",
584 __FUNCTION__, name, strerror (errno));
588 pto_name_dump_start (stream);
590 while (NULL != desc) {
591 pto_name_dump_desc (desc, stream);
596 pto_name_dump_finish (stream);
600 /* Initialise the name module */
601 void pto_name_init ()
603 DBGPRINT (3, (stdout, "(%s:%i) %s\n", __FILE__, __LINE__, __FUNCTION__));
604 assert (NULL == name_obst);
605 assert (NULL == qset_obst);
607 name_obst = xmalloc (sizeof (struct obstack));
608 qset_obst = xmalloc (sizeof (struct obstack));
610 obstack_init (name_obst);
611 obstack_init (qset_obst);
614 /* Cleanup the name module */
615 void pto_name_cleanup ()
617 DBGPRINT (3, (stdout, "(%s:%i) %s\n", __FILE__, __LINE__, __FUNCTION__));
618 obstack_free (name_obst, NULL);
619 obstack_free (qset_obst, NULL);
621 memset (name_obst, 0x00, sizeof (struct obstack));
622 memset (qset_obst, 0x00, sizeof (struct obstack));
634 Revision 1.6 2004/12/06 12:52:09 liekweg
637 Revision 1.4 2004/11/30 15:49:27 liekweg
640 Revision 1.3 2004/11/30 14:47:54 liekweg
641 fix initialisation; do correct iteration
643 Revision 1.2 2004/11/24 14:53:56 liekweg
646 Revision 1.1 2004/11/18 16:37:34 liekweg