move backend into libfirm
[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 /*
57  * Create a new sequence number from a filename and a line number.
58  */
59 seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
60 {
61   struct sn_entry key;
62
63   key.filename = new_id_from_str(filename);
64   key.lineno   = lineno;
65
66   return set_insert(seqnos, &key, sizeof(key), HASH(key));
67 }
68
69 /*
70  * Create a new sequence number from a filename ident and a line number.
71  */
72 seqno_t firm_seqno_enter_id(ident *filename, unsigned lineno)
73 {
74   struct sn_entry key;
75
76   key.filename = filename;
77   key.lineno   = lineno;
78
79   return set_insert(seqnos, &key, sizeof(key), HASH(key));
80 }
81
82 /**
83  * Retrieve filename and line number form a sequence number
84  */
85 const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
86 {
87   if (seqnos && seqno) {
88     *lineno = seqno->lineno;
89     return get_id_str(seqno->filename);
90   }
91   *lineno = 0;
92   return NULL;
93 }
94
95 /*
96  * Creates the seqno pool.
97  */
98 void firm_seqno_init(void)
99 {
100   if (seqnos)
101     firm_seqno_term();
102
103   seqnos = new_set(seqno_cmp, 8);
104 }
105
106 /*
107  * Terminates the seqno pool.
108  * Sequence numbers cannot be resolved anymore.
109  */
110 void firm_seqno_term(void)
111 {
112   if (seqnos) {
113     del_set(seqnos);
114     seqnos = NULL;
115   }
116 }