added sequence numbers for a pair of (filename, lineno)
[libfirm] / ir / debug / seqnumbers.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/debug/seqnumbers.c
4  * Purpose:     Implements simple sequence numbers for Firm debug info.
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2005 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file seqnumbers.c
15  *
16  * Sequence numbers for Firm.
17  *
18  * A sequence number is an unique number representing a filename
19  * and a line number. The number 0 represents empty information.
20  * This module is an optional "snap-in" for the Firm debug info.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "set.h"
28 #include "hashptr.h"
29 #include "ident.h"
30 #include "seqnumbers.h"
31
32 /**
33  * A entry in the sequence number table.
34  */
35 struct sn_entry {
36   ident    *filename;  /**< the filename */
37   unsigned lineno;     /**< the line number */
38 };
39
40 static set *seqnos = NULL;
41
42 /** hash a seqno entry */
43 #define HASH(key) (HASH_PTR((key).filename) ^ (key).lineno)
44
45 /**
46  * Compare two seqno entries.
47  */
48 static int seqno_cmp(const void *elt, const void *key, size_t size)
49 {
50   seqno_t e1 = (seqno_t)elt;
51   seqno_t e2 = (seqno_t)key;
52
53   return (e1->filename != e2->filename) | (e1->lineno - e2->lineno);
54 }
55 /*
56  * Create a new sequence number from a filename and a line number.
57  */
58 seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
59 {
60   struct sn_entry key;
61
62   key.filename = new_id_from_str(filename);
63   key.lineno   = lineno;
64
65   return set_insert(seqnos, &key, sizeof(key), HASH(key));
66 }
67
68 /**
69  * Retrieve filename and line number form a sequence number
70  */
71 const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
72 {
73   if (seqnos && seqno) {
74     *lineno = seqno->lineno;
75     return get_id_str(seqno->filename);
76   }
77   *lineno = 0;
78   return NULL;
79 }
80
81 /*
82  * Creates the seqno pool.
83  */
84 void firm_seqno_init(void)
85 {
86   if (seqnos)
87     firm_seqno_term();
88
89   seqnos = new_set(seqno_cmp, 8);
90 }
91
92 /*
93  * Terminates the seqno pool.
94  * Sequence numbers cannot be resolved anymore.
95  */
96 void firm_seqno_term(void)
97 {
98   if (seqnos) {
99     del_set(seqnos);
100     seqnos = NULL;
101   }
102 }