remove #ifdef HAVE_CONFIG_Hs
[libfirm] / ir / debug / seqnumbers.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
34
35 #include "set.h"
36 #include "hashptr.h"
37 #include "ident.h"
38 #include "seqnumbers.h"
39
40 /**
41  * A entry in the sequence number table.
42  */
43 struct sn_entry {
44   ident    *filename;  /**< the filename */
45   unsigned lineno;     /**< the line number */
46 };
47
48 static set *seqnos = NULL;
49
50 /** hash a seqno entry */
51 #define HASH(key) (HASH_PTR((key).filename) ^ (key).lineno)
52
53 /**
54  * Compare two seqno entries.
55  */
56 static int seqno_cmp(const void *elt, const void *key, size_t size)
57 {
58   seqno_t e1 = (seqno_t)elt;
59   seqno_t e2 = (seqno_t)key;
60   (void) size;
61
62   return (e1->filename != e2->filename) | (e1->lineno - e2->lineno);
63 }
64
65 /*
66  * Create a new sequence number from a filename and a line number.
67  */
68 seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
69 {
70   struct sn_entry key;
71
72   key.filename = new_id_from_str(filename);
73   key.lineno   = lineno;
74
75   return set_insert(seqnos, &key, sizeof(key), HASH(key));
76 }
77
78 /*
79  * Create a new sequence number from a filename ident and a line number.
80  */
81 seqno_t firm_seqno_enter_id(ident *filename, unsigned lineno)
82 {
83   struct sn_entry key;
84
85   key.filename = filename;
86   key.lineno   = lineno;
87
88   return set_insert(seqnos, &key, sizeof(key), HASH(key));
89 }
90
91 /**
92  * Retrieve filename and line number form a sequence number
93  */
94 const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
95 {
96   if (seqnos && seqno) {
97     *lineno = seqno->lineno;
98     return get_id_str(seqno->filename);
99   }
100   *lineno = 0;
101   return NULL;
102 }
103
104 /*
105  * Creates the seqno pool.
106  */
107 void firm_seqno_init(void)
108 {
109   if (seqnos)
110     firm_seqno_term();
111
112   seqnos = new_set(seqno_cmp, 8);
113 }
114
115 /*
116  * Terminates the seqno pool.
117  * Sequence numbers cannot be resolved anymore.
118  */
119 void firm_seqno_term(void)
120 {
121   if (seqnos) {
122     del_set(seqnos);
123     seqnos = NULL;
124   }
125 }