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

Subversion Repositories diogenes

[/] [diogenes/] [trunk/] [asm/] [macros.c] - Blame information for rev 66

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 66 fellnhofer
 
2
 
3
#include<stdio.h>
4
#include <search.h>
5
 
6
#define RDLINE() fgets(line, 1024, stdin)
7
 
8
#define STATE_MACRO 1
9
#define STATE_ENDM 2
10
#define STATE_USEM 3
11
 
12
 
13
char line[1024];
14
char *p;
15
 
16
char buffer[10000];
17
 
18
 
19
void get_params(char *p, char names[12][256]) {
20
        int newname = 0;
21
        int i=0, j=0;
22
 
23
        if(!p) return;
24
 
25
        memset(names, 0, 12*256);
26
 
27
        while(*p) {
28
 
29
                if(isalnum(*p)) {
30
                        newname = 1;
31
                        names[i][j++] = *p;
32
                } else {
33
                        if(newname==1) {
34
                                names[i][j] = 0;
35
                                j=0;
36
                                i++;
37
                        }
38
                        newname = 0;
39
                }
40
 
41
                p++;
42
        }
43
}
44
 
45
 
46
char *dollarfy(char *srci, char names[12][256])
47
{
48
        int i=2;
49
        char *dest, *src, *h;
50
        char *pd, *ps;
51
 
52
        src = malloc(strlen(srci) + 12);
53
        dest = malloc(strlen(srci) + 12);
54
        strcpy(src, srci);
55
 
56
        while(names[i][0]) {
57
                pd = dest;
58
                ps = src;
59
 
60
                while(*ps) {
61
                        if(*ps=='\\')
62
                                if(!strncmp(++ps, names[i], strlen(names[i]))) {
63
                                        while(*ps && isalnum(*ps++));
64
                                        ps--;
65
                                        *pd++ = '$';
66
                                        *pd++ = ('0' + i-2);
67
                                        *pd = ' ';
68
                                } else ps--;
69
                        *pd = *ps;
70
                        pd++;
71
                        ps++;
72
                }
73
                *pd = 0;
74
                i++;
75
                h = src;
76
                src = dest;
77
                dest = h;
78
        }
79
 
80
        free(dest);
81
        return src;
82
}
83
 
84
 
85
int hash_macros()
86
{
87
        int state=0;
88
        char *p;
89
        buffer[0] = 0;
90
        ENTRY e, *ep;
91
        char name[128];
92
        char parm[12][256];
93
 
94
        if(!RDLINE()) return 1;
95
 
96
 
97
        while(1) {
98
                if(p = strstr(line, ".macro")) {
99
                        sscanf(line, " .macro %s ", name);
100
                        get_params(p, parm);
101
                        if(!RDLINE()) return 1;
102
                        state = STATE_MACRO;
103
                }
104
                else if(p = strstr(line, ".endm")) state = STATE_ENDM;
105
                else if(p = strstr(line, ".usem")) {
106
                        state = STATE_USEM;
107
                        sscanf(line, " .usem %s ", name);
108
                }
109
 
110
                switch(state) {
111
                        case STATE_MACRO: strcat(buffer, line); break;
112
                        case STATE_ENDM:
113
                                p = dollarfy(buffer, parm);
114
                                e.key = strdup(name);
115
                                e.data = (void *) p;
116
                                hsearch(e, ENTER);
117
                                state = 0; buffer[0] = 0;
118
                                break;
119
                        case STATE_USEM:
120
                                e.key = strdup(name);
121
                                ep = hsearch(e, FIND);
122
                                if(ep) printf((char *) ep->data);
123
                                state = 0; buffer[0] = 0;
124
                                break;
125
                        default: printf(line);
126
                }
127
                if(!RDLINE()) return 1;
128
        }
129
}
130
 
131
 
132
int main(int argc, char **argv)
133
{
134
 
135
  hcreate(100);
136
 
137
  hash_macros();
138
 
139
 
140
}

powered by: WebSVN 2.1.0

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