added new licence header
[libfirm] / ir / debug / seqnumbers.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /*
21  * Project:     libFIRM
22  * File name:   ir/debug/seqnumbers.c
23  * Purpose:     Implements simple sequence numbers for Firm debug info.
24  * Author:      Michael Beck
25  * Modified by:
26  * Created:     2005
27  * CVS-ID:      $Id$
28  * Copyright:   (c) 2001-2005 Universität Karlsruhe
29  */
30
31 /**
32  * @file seqnumbers.c
33  *
34  * Sequence numbers for Firm.
35  *
36  * A sequence number is an unique number representing a filename
37  * and a line number. The number 0 represents empty information.
38  * This module is an optional "snap-in" for the Firm debug info.
39  */
40
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #include "set.h"
46 #include "hashptr.h"
47 #include "ident.h"
48 #include "seqnumbers.h"
49
50 /**
51  * A entry in the sequence number table.
52  */
53 struct sn_entry {
54   ident    *filename;  /**< the filename */
55   unsigned lineno;     /**< the line number */
56 };
57
58 static set *seqnos = NULL;
59
60 /** hash a seqno entry */
61 #define HASH(key) (HASH_PTR((key).filename) ^ (key).lineno)
62
63 /**
64  * Compare two seqno entries.
65  */
66 static int seqno_cmp(const void *elt, const void *key, size_t size)
67 {
68   seqno_t e1 = (seqno_t)elt;
69   seqno_t e2 = (seqno_t)key;
70
71   return (e1->filename != e2->filename) | (e1->lineno - e2->lineno);
72 }
73
74 /*
75  * Create a new sequence number from a filename and a line number.
76  */
77 seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
78 {
79   struct sn_entry key;
80
81   key.filename = new_id_from_str(filename);
82   key.lineno   = lineno;
83
84   return set_insert(seqnos, &key, sizeof(key), HASH(key));
85 }
86
87 /*
88  * Create a new sequence number from a filename ident and a line number.
89  */
90 seqno_t firm_seqno_enter_id(ident *filename, unsigned lineno)
91 {
92   struct sn_entry key;
93
94   key.filename = filename;
95   key.lineno   = lineno;
96
97   return set_insert(seqnos, &key, sizeof(key), HASH(key));
98 }
99
100 /**
101  * Retrieve filename and line number form a sequence number
102  */
103 const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
104 {
105   if (seqnos && seqno) {
106     *lineno = seqno->lineno;
107     return get_id_str(seqno->filename);
108   }
109   *lineno = 0;
110   return NULL;
111 }
112
113 /*
114  * Creates the seqno pool.
115  */
116 void firm_seqno_init(void)
117 {
118   if (seqnos)
119     firm_seqno_term();
120
121   seqnos = new_set(seqno_cmp, 8);
122 }
123
124 /*
125  * Terminates the seqno pool.
126  * Sequence numbers cannot be resolved anymore.
127  */
128 void firm_seqno_term(void)
129 {
130   if (seqnos) {
131     del_set(seqnos);
132     seqnos = NULL;
133   }
134 }