pto_name.c 10.9 KB
Newer Older
Florian Liekweg's avatar
Florian Liekweg committed
1
2
3
/* -*- c -*- */

/*
Florian Liekweg's avatar
Florian Liekweg committed
4
5
6
7
8
9
10
11
12
  Project:     libFIRM
  File name:   ir/ana/pto_name.c
  Purpose:     Names for abstract objects
  Author:      Florian
  Modified by:
  Created:     Sat Nov 13 19:35:27 CET 2004
  CVS-ID:      $Id$
  Copyright:   (c) 1999-2004 Universitt Karlsruhe
  Licence:     This file is protected by the GPL -  GNU GENERAL PUBLIC LICENSE.
Florian Liekweg's avatar
Florian Liekweg committed
13
14
*/

Michael Beck's avatar
Michael Beck committed
15
16
17
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Florian Liekweg's avatar
Florian Liekweg committed
18
19

/*
Florian Liekweg's avatar
Florian Liekweg committed
20
  pto_name: Names for abstract objects
Florian Liekweg's avatar
Florian Liekweg committed
21
22
*/

Florian Liekweg's avatar
Florian Liekweg committed
23
# include "pto.h"
Florian Liekweg's avatar
Florian Liekweg committed
24
# include "pto_name.h"
Florian Liekweg's avatar
Florian Liekweg committed
25
26
27
28
# include "pto_util.h"

# include <string.h>            /* for memcpy */
# include <obstack.h>
29
# include <errno.h>
Florian Liekweg's avatar
Florian Liekweg committed
30
31

# include "irnode.h"
Florian Liekweg's avatar
Florian Liekweg committed
32
# include "irprog.h"
Florian Liekweg's avatar
Florian Liekweg committed
33
34
35
36
37
# include "xmalloc.h"

# include "pto_debug.h"

/* Local Defines: */
Florian Liekweg's avatar
Florian Liekweg committed
38
39
40
41
# define obstack_chunk_alloc xmalloc
# define obstack_chunk_free free

# define NALLOC(size)   obstack_alloc (name_obst, size)
Florian Liekweg's avatar
Florian Liekweg committed
42
43
44
45

/* Local Data Types: */

/* Local Variables: */
Florian Liekweg's avatar
Florian Liekweg committed
46
47
struct obstack *qset_obst = NULL;
struct obstack *name_obst = NULL;
Florian Liekweg's avatar
Florian Liekweg committed
48

49
50
51
52
static desc_t *all_descs = NULL;

static int name_id = 0;

Florian Liekweg's avatar
Florian Liekweg committed
53
54
55
56
57
/* Local Prototypes: */

/* ===================================================
   Local Implementation:
   =================================================== */
Florian Liekweg's avatar
Florian Liekweg committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* See whether the given entity is a field. */
static int is_field (entity *ent)
{
  type *tp = get_entity_type (ent);

  if (is_primitive_type (tp) || is_pointer_type (tp)) {
    /* actually, we could get by by restricting ourselves to pointer types */
    return (TRUE);
  } else {
    return (FALSE);
  }
}

/* Helper to collect_fields(type*): collect all fields of the given
  clazz and its super classes into the given obstack. */
static void _collect_fields (type *clazz, struct obstack *obst)
{
  int n_members = get_class_n_members (clazz);
  int n_supers  = get_class_n_supertypes (clazz);
  int i;

  for (i = 0; i < n_members; i ++) {
    entity *ent = get_class_member (clazz, i);

    if (is_field (ent)) {
      if (allocation_static != get_entity_allocation (ent)) {
        obstack_ptr_grow (obst, ent);
      }
    }
  }

  for (i = 0; i < n_supers; i ++) {
    type *s_clazz = get_class_supertype (clazz, i);

    _collect_fields (s_clazz, obst);
  }
}

/* Collect the fields of the given class and its super classes into an array.
  The last entry of the array is written NULL. */
static entity **collect_fields (type *clazz)
{
  if (NULL != get_type_link (clazz)) {
    DBGPRINT (3, (stdout, "%s: reusing field list for \"%s\"\n",
                  __FUNCTION__, get_type_name (clazz)));

    return ((entity **) get_type_link (clazz));
  } else {
    DBGPRINT (2, (stdout, "%s: new field list for \"%s\"\n",
                  __FUNCTION__, get_type_name (clazz)));
  }

  struct obstack obst;

  obstack_init (&obst);

  _collect_fields (clazz, &obst);

  /* append terminating NULL */
  int *the_null = NULL;
  obstack_ptr_grow (&obst, the_null);

  int n_fields = obstack_object_size (&obst) / sizeof (void*);

  entity ** fields = (entity**) NALLOC (n_fields * sizeof (entity*));
  void *tmp = obstack_finish (&obst);

  memcpy (fields, tmp, n_fields * sizeof (entity*));

  obstack_free (&obst, NULL);

  set_type_link (clazz, (void*) fields);

  return (fields);
}

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Write the intro text for a name dump into the given stream */
static void pto_name_dump_start (FILE *stream)
{
  fprintf (stream, "digraph \"Names\" {\n");
  fprintf (stream, "\tgraph [rankdir=\"LR\", ordering=\"out\"];\n");
  fprintf (stream, "\tnode [shape=\"record\", style=\"filled\"];\n");
  fprintf (stream, "\tedge [color=\"black\"];\n");
  fprintf (stream, "\n");
}

/* Write the extro text for a name dump into the given stream */
static void pto_name_dump_finish (FILE *stream)
{
  fprintf (stream, "}\n");
}

/* Write a node for the given descriptor into the given stream */
static void pto_name_dump_desc (desc_t *desc, FILE *stream)
{
  type *tp = desc->tp;
  const char *tp_name = get_type_name (tp);

  fprintf (stream, "\t/* %s \"%s\" */\n",
           object == desc->kind ? "Object" : "Array",
           tp_name);

  fprintf (stream, "\tdesc_%i [label=\"<HEAD>type=\\\"%s\\\"",
           desc->id,
           tp_name);

  ir_node *nd = desc->node;

  if (NULL != nd) {
    fprintf (stream, "|<NODE>node=%s\\[%li\\]",
             get_op_name (get_irn_op (nd)),
             get_irn_node_nr (nd));
  }

  if (desc->kind == object) {
    obj_desc_t *obj_desc = (obj_desc_t*) desc;

    int i;
    for (i = 0; i < obj_desc->n_fields; i ++) {
      const char *ent_name = get_entity_name (obj_desc->fields [i]);

      fprintf (stream, "|<%i>%s", i, ent_name);
    }
  } else if (array == desc->kind) {
    arr_desc_t *arr_desc = (arr_desc_t*) desc;

    fprintf (stream, "|<arr>[]");
  } else {
    assert (0 && "invalid descriptor");
  }

  fprintf (stream, "\"];\n");
  fprintf (stream, "\n");

  /* now the edges */
  if (desc->kind == object) {
    obj_desc_t *obj_desc = (obj_desc_t*) desc;

    int i;
    for (i = 0; i < obj_desc->n_fields; i ++) {
      desc_t *tgt = (desc_t*) qset_start (obj_desc->values [i]);

      while (NULL != tgt) {
        fprintf (stream, "\tdesc_%i:%i -> desc_%i:HEAD;\n",
                 desc->id, i, tgt->id);

        tgt = (desc_t*) qset_next (obj_desc->values [i]);
      }
    }
  } else if (array == desc->kind) {
    arr_desc_t *arr_desc = (arr_desc_t*) desc;

    desc_t *tgt = (desc_t*) qset_start (arr_desc->value);

    while (NULL != tgt) {
      fprintf (stream, "\tdesc_%i:arr -> desc_%i:HEAD;\n",
               desc->id, tgt->id);

      tgt = (desc_t*) qset_next (arr_desc->value);
    }
  }

  fprintf (stream, "\n");
}
Florian Liekweg's avatar
Florian Liekweg committed
222
223
224
225
226


/* ===================================================
   Exported Implementation:
   =================================================== */
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Find the given descriptor's entry for the given entity */
qset_t *get_entry (desc_t *desc, entity *ent)
{

  if (desc->kind == object) {
    obj_desc_t *obj_desc = (obj_desc_t*) desc;
    int i;
    const int n_fields = obj_desc->n_fields;

    for (i = 0; i < n_fields; i ++) {
      if (ent == obj_desc->fields [i]) {
        return (obj_desc->values [i]);
      }
    }

    assert (0 && "entry not found");
  } else if (desc->kind = array) {
    arr_desc_t *arr_desc = (arr_desc_t*) desc;

    return (arr_desc->value);
  } else {
    assert (0 && "invalid descriptor");
  }
}

Florian Liekweg's avatar
Florian Liekweg committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

/* get a new descriptor for the given type at the given node */
desc_t *new_name (type *tp, ir_node *node)
{
  desc_t *desc = NULL;

  assert ((is_class_type (tp) || is_array_type (tp)) && "unsuitable type");

  DBGPRINT (2, (stdout, "%s: new name for type \"%s\"\n", __FUNCTION__,
                get_type_name (tp)));
  fflush (stdout);

  if (is_class_type (tp)) {
    obj_desc_t *obj_desc = (obj_desc_t*) NALLOC (sizeof (obj_desc_t));
    int i;
    int n_fields;

    obj_desc->kind = object;
    obj_desc->fields = collect_fields (tp);

    for (n_fields = 0; (NULL != obj_desc->fields [n_fields]); n_fields ++) {
      /* nothing, just count ... */
    }

    obj_desc->n_fields = n_fields;
    obj_desc->values = (qset_t**) NALLOC (n_fields * sizeof (qset_t));
Florian Liekweg's avatar
Florian Liekweg committed
278

Florian Liekweg's avatar
Florian Liekweg committed
279
280
281
    for (i = 0; i < n_fields; i ++) {
      obj_desc->values [i] = qset_new (N_INITIAL_OJBS, qset_obst);
    }
Florian Liekweg's avatar
Florian Liekweg committed
282

Florian Liekweg's avatar
Florian Liekweg committed
283
284
285
    desc = (desc_t*) obj_desc;
  } else if (is_array_type (tp)) {
    arr_desc_t *arr_desc = (arr_desc_t*) NALLOC (sizeof (arr_desc_t));
Florian Liekweg's avatar
Florian Liekweg committed
286

Florian Liekweg's avatar
Florian Liekweg committed
287
288
289
290
291
292
    arr_desc->kind = array;
    arr_desc->value = qset_new (N_INITIAL_OJBS, qset_obst);

    desc = (desc_t*) arr_desc;
  }

293
  desc->id   = name_id ++;
Florian Liekweg's avatar
Florian Liekweg committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  desc->tp   = tp;
  desc->node = node;

  desc->prev = all_descs;
  all_descs = desc;

  return (desc);
}

# define N_GLOB_INITIAL_FIELDS  20
static obj_desc_t *obj_glob = NULL;
static int n_glob_fields = N_GLOB_INITIAL_FIELDS;

/* get a new descriptor for the given (presumably static) entity */
desc_t *new_ent_name (entity *ent)
{
  int i;
  int missing = TRUE;
  type *tp = get_entity_type (ent);

  assert (is_pointer_type (tp));
  tp = get_pointer_points_to_type (tp);
  assert (is_class_type (tp));

  assert (((allocation_static == get_entity_allocation (ent)) ||
           (allocation_automatic == get_entity_allocation (ent))) &&
          "not a static/automatic field");

  if (NULL == obj_glob) {
    obj_glob = (obj_desc_t*) NALLOC (sizeof (obj_desc_t));

325
    obj_glob->id   = name_id ++;
Florian Liekweg's avatar
Florian Liekweg committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    obj_glob->kind = object;
    obj_glob->tp   = get_glob_type ();
    obj_glob->node = NULL;

    obj_glob->n_fields = 0;
    obj_glob->fields = (entity**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (entity*));
    obj_glob->values = (qset_t**) NALLOC (N_GLOB_INITIAL_FIELDS * sizeof (qset_t*));

    obj_glob->prev = all_descs;
    all_descs = (desc_t*) obj_glob;
  }

  for (i = 0; missing && (i < obj_glob->n_fields); i ++) {
    if (ent == obj_glob->fields [i]) {
      missing = FALSE;
    }
  }

  if (missing) {
    if (obj_glob->n_fields == n_glob_fields) {
      entity **fields = obj_glob->fields;
      qset_t **values = obj_glob->values;

      n_glob_fields *= 2;
      obj_glob->fields = (entity**) NALLOC (n_glob_fields * sizeof (entity*));
      obj_glob->values = (qset_t**) NALLOC (n_glob_fields * sizeof (qset_t*));

      memcpy (obj_glob->fields, fields, obj_glob->n_fields * sizeof (entity*));
      memcpy (obj_glob->values, values, obj_glob->n_fields * sizeof (qset_t*));

      /* free (fields); */
      /* free (values); */
    }

    obj_glob->fields [obj_glob->n_fields   ] = ent;
    obj_glob->values [obj_glob->n_fields ++] = qset_new (N_INITIAL_OJBS, qset_obst);
  }

  return ((desc_t*) obj_glob);
}
# undef N_GLOB_INITIAL_FIELDS

368
/* Dump all names to a file of the given name */
Florian Liekweg's avatar
Florian Liekweg committed
369
void pto_dump_names (const char *name)
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
{
  desc_t *desc = all_descs;
  FILE *stream = fopen (name, "w");

  errno = 0;
  if  (NULL == stream) {
    fprintf (stderr, "%s: unable to open %s (%s)\n",
             __FUNCTION__, name, strerror (errno));
    return;
  }

  pto_name_dump_start (stream);

  while (NULL != desc) {
    pto_name_dump_desc (desc, stream);

    desc = desc->prev;
  }

  pto_name_dump_finish (stream);
  fclose (stream);
}

Florian Liekweg's avatar
Florian Liekweg committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* Initialise the name module */
void pto_name_init ()
{
  DBGPRINT (3, (stdout, "(%s:%i) %s\n", __FILE__, __LINE__, __FUNCTION__));
  assert (NULL == name_obst);
  assert (NULL == qset_obst);

  name_obst = xmalloc (sizeof (struct obstack));
  qset_obst = xmalloc (sizeof (struct obstack));

  obstack_init (name_obst);
  obstack_init (qset_obst);
}

/* Cleanup the name module */
void pto_name_cleanup ()
{
  DBGPRINT (3, (stdout, "(%s:%i) %s\n", __FILE__, __LINE__, __FUNCTION__));
  obstack_free (name_obst, NULL);
  obstack_free (qset_obst, NULL);

  memset (name_obst, 0x00, sizeof (struct obstack));
  memset (qset_obst, 0x00, sizeof (struct obstack));

  free (name_obst);
  free (qset_obst);

  name_obst = NULL;
  qset_obst = NULL;
}
Florian Liekweg's avatar
Florian Liekweg committed
423
424
425
426


/*
  $Log$
Michael Beck's avatar
Michael Beck committed
427
428
429
  Revision 1.5  2004/12/02 16:17:51  beck
  fixed config.h include

Florian Liekweg's avatar
Florian Liekweg committed
430
431
432
  Revision 1.4  2004/11/30 15:49:27  liekweg
  include 'dump'

433
434
435
  Revision 1.3  2004/11/30 14:47:54  liekweg
  fix initialisation; do correct iteration

Florian Liekweg's avatar
Florian Liekweg committed
436
437
438
  Revision 1.2  2004/11/24 14:53:56  liekweg
  Bugfixes

Florian Liekweg's avatar
Florian Liekweg committed
439
440
441
442
443
  Revision 1.1  2004/11/18 16:37:34  liekweg
  rewritten


*/