fixed warnings
[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  * @file
22  * @brief      Implements simple sequence numbers for Firm debug info.
23  * @author     Michael Beck
24  * @date       2005
25  * @version    $Id$
26  * @summary
27  *  Sequence numbers for Firm.
28  *
29  *  A sequence number is an unique number representing a filename
30  *  and a line number. The number 0 represents empty information.
31  *  This module is an optional "snap-in" for the Firm debug info.
32  */
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include "set.h"
38 #include "hashptr.h"
39 #include "ident.h"
40 #include "seqnumbers.h"
41
42 /**
43  * A entry in the sequence number table.
44  */
45 struct sn_entry {
46   ident    *filename;  /**< the filename */
47   unsigned lineno;     /**< the line number */
48 };
49
50 static set *seqnos = NULL;
51
52 /** hash a seqno entry */
53 #define HASH(key) (HASH_PTR((key).filename) ^ (key).lineno)
54
55 /**
56  * Compare two seqno entries.
57  */
58 static int seqno_cmp(const void *elt, const void *key, size_t size)
59 {
60   seqno_t e1 = (seqno_t)elt;
61   seqno_t e2 = (seqno_t)key;
62   (void) size;
63
64   return (e1->filename != e2->filename) | (e1->lineno - e2->lineno);
65 }
66
67 /*
68  * Create a new sequence number from a filename and a line number.
69  */
70 seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
71 {
72   struct sn_entry key;
73
74   key.filename = new_id_from_str(filename);
75   key.lineno   = lineno;
76
77   return set_insert(seqnos, &key, sizeof(key), HASH(key));
78 }
79
80 /*
81  * Create a new sequence number from a filename ident and a line number.
82  */
83 seqno_t firm_seqno_enter_id(ident *filename, unsigned lineno)
84 {
85   struct sn_entry key;
86
87   key.filename = filename;
88   key.lineno   = lineno;
89
90   return set_insert(seqnos, &key, sizeof(key), HASH(key));
91 }
92
93 /**
94  * Retrieve filename and line number form a sequence number
95  */
96 const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
97 {
98   if (seqnos && seqno) {
99     *lineno = seqno->lineno;
100     return get_id_str(seqno->filename);
101   }
102   *lineno = 0;
103   return NULL;
104 }
105
106 /*
107  * Creates the seqno pool.
108  */
109 void firm_seqno_init(void)
110 {
111   if (seqnos)
112     firm_seqno_term();
113
114   seqnos = new_set(seqno_cmp, 8);
115 }
116
117 /*
118  * Terminates the seqno pool.
119  * Sequence numbers cannot be resolved anymore.
120  */
121 void firm_seqno_term(void)
122 {
123   if (seqnos) {
124     del_set(seqnos);
125     seqnos = NULL;
126   }
127 }