OpenCores
URL https://opencores.org/ocsvn/igor/igor/trunk

Subversion Repositories igor

[/] [igor/] [trunk/] [simulator/] [debug.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
#define _GNU_SOURCE
2
#include <sys/types.h>
3
 
4
#include <assert.h>
5
#include <stdio.h>
6
#include <err.h>
7
#include <string.h>
8
#include <stdlib.h>
9
#include <unistd.h>
10
 
11
#include "debug.h"
12
#include "hash.h"
13
#include "linkedlist.h"
14
#include "alloc.h"
15
 
16
hash
17
uint_hash(unsigned int *_key)
18
{
19
        hash key = *_key;
20
 
21
        key = ~key + (key << 15); // key = (key << 15) - key - 1;
22
        key = key ^ (key >> 12);
23
        key = key + (key << 2);
24
        key = key ^ (key >> 4);
25
        key = key * 2057; // key = (key + (key << 3)) + (key << 11);
26
        key = key ^ (key >> 16);
27
 
28
        return key;
29
}
30
 
31
hash
32
uint_cmp(unsigned int *k1, unsigned int *k2)
33
{
34
        return (*k1 == *k2);
35
}
36
 
37
 
38
HASH_DEFINE(hash_debuginfo, uint_cmp, uint_hash, unsigned int, void);
39
HASH_DECLARE(hash_debuginfo, uint_cmp, uint_hash, unsigned int, void);
40
 
41
 
42
static void
43
add_constant(char *group, char *name, unsigned int number)
44
{
45
        llist_t list;
46
        struct di_const *c;
47
 
48
        list = hash_debuginfo_lookup(debuginfo->constants, &number);
49
        if (list == NULL) {
50
                unsigned int *key = smalloc(sizeof(*key));
51
 
52
                *key = number;
53
                list = llist_make();
54
 
55
                hash_debuginfo_insert(debuginfo->constants, key, list);
56
        }
57
        c = smalloc(sizeof(*c));
58
        c->group = sstrdup(group);
59
        c->name = sstrdup(name);
60
        llist_add_tail(list, c);
61
}
62
 
63
int
64
debug_init(const char *path)
65
{
66
        char constfile[2048];
67
        char *line, group[1024], name[1024];
68
        unsigned int number;
69
        char *p, *p2;
70
        size_t n;
71
        FILE *fp;
72
 
73
        debuginfo = malloc(sizeof(*debuginfo));
74
        if (debuginfo == NULL)
75
                return 0;
76
        debuginfo->constants = hash_debuginfo_create(128, 0.50);
77
 
78
        snprintf(constfile, sizeof(constfile), "%s.const", path);
79
 
80
        fp = fopen(constfile, "r");
81
        if (fp == NULL) {
82
                debuginfo = NULL;
83
                return 0;
84
        }
85
 
86
        // Load constant information from file
87
        line = NULL;
88
        while (getline(&line, &n, fp) > 0) {
89
                line[strlen(line)-1] = '\0';
90
                p = strchr(line, ' ');
91
                *p = '\0';
92
                strncpy(group, line, sizeof(group));
93
                p++;
94
                p2 = strchr(p, ' ');
95
                *p2 = '\0';
96
                strncpy(name, p, sizeof(name));
97
                p2++;
98
                number = strtol(p2, NULL, 16);
99
                free(line);
100
 
101
                add_constant(group, name, number);
102
                line = NULL;
103
        }
104
 
105
        fclose(fp);
106
 
107
        return 1;
108
}
109
 
110
void
111
debug_show(unsigned int x)
112
{
113
        debug_show_filter(x, NULL);
114
}
115
 
116
void
117
debug_show_filter(unsigned int x, const char *filter)
118
{
119
        llist_t l;
120
        llist_node_t node;
121
 
122
        if (debuginfo == NULL) {
123
                // No debuginfo loaded
124
                return;
125
        }
126
 
127
        l = hash_debuginfo_lookup(debuginfo->constants, &x);
128
        if (l == NULL) {
129
                printf("no debuginfo for: %x\n", x);
130
        } else {
131
                struct di_const *c;
132
 
133
                LLIST_FOREACH(l, node) {
134
                        c = llist_getobj(node);
135
                        if (filter == NULL || strcmp(filter, c->group) == 0)
136
                                printf("> %s %s %x\n", c->group, c->name, x);
137
                }
138
        }
139
}
140
 
141
struct di_const *
142
debug_get1(unsigned int x)
143
{
144
        return debug_get1_filter(x, NULL);
145
}
146
 
147
struct di_const *
148
debug_get1_filter(unsigned int x, const char *filter)
149
{
150
        llist_t l;
151
        llist_node_t node;
152
 
153
        if (debuginfo == NULL) {
154
                // No debuginfo loaded
155
                return NULL;
156
        }
157
 
158
        l = hash_debuginfo_lookup(debuginfo->constants, &x);
159
        if (l == NULL) {
160
                return NULL;
161
        } else {
162
                struct di_const *c;
163
 
164
                LLIST_FOREACH(l, node) {
165
                        c = llist_getobj(node);
166
                        if (filter == NULL || strcmp(filter, c->group) == 0)
167
                                return c;
168
                }
169
        }
170
 
171
        return NULL;
172
}
173
 
174
unsigned int
175
debug_get_key(const char *name, const char *group)
176
{
177
        hash i;
178
        struct hash_bucket *b;
179
        struct di_const *c;
180
        llist_node_t node;
181
        llist_t l;
182
 
183
        for (i = 0; i < debuginfo->constants->h_size; i++) {
184
                b = &debuginfo->constants->h_bucket[i];
185
                if (b->key == NULL)
186
                        continue;
187
                l = b->value;
188
 
189
                LLIST_FOREACH(l, node) {
190
                        c = llist_getobj(node);
191
                        if (strcasecmp(c->name, name) == 0 &&
192
                            strcasecmp(c->group, group) == 0)
193
                                return *((unsigned int*)b->key);
194
                }
195
        }
196
 
197
        return 0xdeadbeef;
198
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.