seqnumbers.c 2.98 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Project:     libFIRM
 * File name:   ir/debug/seqnumbers.c
 * Purpose:     Implements simple sequence numbers for Firm debug info.
 * Author:      Michael Beck
 * Modified by:
 * Created:     2005
 * CVS-ID:      $Id$
 * Copyright:   (c) 2001-2005 Universitt Karlsruhe
 */

/**
 * @file seqnumbers.c
 *
 * Sequence numbers for Firm.
 *
 * A sequence number is an unique number representing a filename
 * and a line number. The number 0 represents empty information.
 * This module is an optional "snap-in" for the Firm debug info.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "set.h"
#include "hashptr.h"
#include "ident.h"
#include "seqnumbers.h"

/**
 * A entry in the sequence number table.
 */
struct sn_entry {
  ident    *filename;  /**< the filename */
  unsigned lineno;     /**< the line number */
};

static set *seqnos = NULL;

/** hash a seqno entry */
#define HASH(key) (HASH_PTR((key).filename) ^ (key).lineno)

/**
 * Compare two seqno entries.
 */
static int seqno_cmp(const void *elt, const void *key, size_t size)
{
  seqno_t e1 = (seqno_t)elt;
  seqno_t e2 = (seqno_t)key;

  return (e1->filename != e2->filename) | (e1->lineno - e2->lineno);
}
73

74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Create a new sequence number from a filename and a line number.
 */
seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
{
  struct sn_entry key;

  key.filename = new_id_from_str(filename);
  key.lineno   = lineno;

  return set_insert(seqnos, &key, sizeof(key), HASH(key));
}

87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Create a new sequence number from a filename ident and a line number.
 */
seqno_t firm_seqno_enter_id(ident *filename, unsigned lineno)
{
  struct sn_entry key;

  key.filename = filename;
  key.lineno   = lineno;

  return set_insert(seqnos, &key, sizeof(key), HASH(key));
}

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
134
/**
 * Retrieve filename and line number form a sequence number
 */
const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
{
  if (seqnos && seqno) {
    *lineno = seqno->lineno;
    return get_id_str(seqno->filename);
  }
  *lineno = 0;
  return NULL;
}

/*
 * Creates the seqno pool.
 */
void firm_seqno_init(void)
{
  if (seqnos)
    firm_seqno_term();

  seqnos = new_set(seqno_cmp, 8);
}

/*
 * Terminates the seqno pool.
 * Sequence numbers cannot be resolved anymore.
 */
void firm_seqno_term(void)
{
  if (seqnos) {
    del_set(seqnos);
    seqnos = NULL;
  }
}