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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_42/] [or1ksim/] [cpu/] [common/] [parse.c] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 cvs
/* parce.c -- Architecture independent load and parsing of assembly
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdio.h>
21
#include <ctype.h>
22
#include <string.h>
23
#include <stdlib.h>
24
 
25
#include "parse.h"
26
#include "abstract.h"
27
 
28
#define MAXLINE_LEN     18000
29
 
30
/* Unused mem memory marker. It is used when allocating program and data memory
31
   during parsing */
32
unsigned int freemem;
33
 
34
int nonempty(char *line)
35
{
36
        int i;
37
 
38
        for(i = 0; i < strlen(line); i++)
39
                if (!isspace(line[i]))
40
                        return(1);
41
        return(0);
42
}
43
 
44
int nondigit(char *line)
45
{
46
        int i;
47
 
48
        for(i = 0; i < strlen(line); i++)
49
                if (!isdigit(line[i]))
50
                        return(1);
51
        return(0);
52
}
53
 
54
char *strtoken(char *in, char *out, int which)
55
{
56
        char    *super;
57
        char    *sub;
58
        char    *newline;
59
 
60
        super = strdup(in);
61
        sub = strtok(super, " \t");
62
        while (sub && --which)
63
                sub = strtok(NULL, " \t");
64
        if (sub && !which) {
65
                if ((newline = strchr(sub, '\n')))
66
                        newline[0] = '\0';
67
                strcpy(out, sub);
68
        } else
69
                out[0] = '\0';
70
        free(super);
71
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
72
                newline[0] = '\0';
73
        return(out);
74
}
75
 
76
void adddatastr(char *str)
77
{
78
        if (str)
79
                str++;
80
        else
81
                return;
82
 
83
        for(; *str && *str != '\"'; str++, freemem++)
84
                if (*str == '\\')
85
                        switch (*++str) {
86
                                case 'n': mem[freemem].data = '\n';
87
                                        break;
88
                                case 't': mem[freemem].data = '\t';
89
                                        break;
90
                                case 'r': mem[freemem].data = '\r';
91
                                        break;
92
                                case '0': mem[freemem].data = '\0';
93
                                        break;
94
                                default: break;
95
                        }
96
                else
97
                        mem[freemem].data = *str;
98
}
99
 
100
void adddataword(char *num)
101
{
102
        mem[freemem].data = (char) (atol(num) >> 24);
103
        mem[freemem + 1].data = (char) (atol(num) >> 16);
104
        mem[freemem + 2].data = (char) (atol(num) >> 8);
105
        mem[freemem + 3].data = (char) (atol(num));
106
 
107
        freemem += 4;
108
}
109
 
110
void adddatahalf(char *num)
111
{
112
        mem[freemem].data = (char) (atol(num) >> 8);
113
        mem[freemem + 1].data = (char) (atol(num));
114
 
115
        freemem += 2;
116
}
117
 
118
void adddatabyte(char *num)
119
{
120
        mem[freemem].data = (char) (atol(num));
121
 
122
        freemem++;
123
}
124
 
125
void adddataspace(char *num)
126
{
127
        freemem += atol(num);
128
}
129
 
130
void addlabel(char *label)
131
{
132
        if (strstr(label, LABELEND_CHAR)) {
133
                *strstr(label, LABELEND_CHAR) = '\0';
134
                strcpy(mem[freemem].label, label);
135
        }
136
 
137
        return;
138
}
139
 
140
void addprogram(char *insn, char *operands)
141
{
142
 
143
        strcpy(mem[freemem].insn, insn);
144
 
145
        /* op1 */
146
        if (*operands)
147
                strcpy(mem[freemem].op1, operands);
148
        if (strstr(mem[freemem].op1, OPERAND_DELIM)) {
149
                operands = strstr(mem[freemem].op1, OPERAND_DELIM);
150
                *operands = '\0';
151
                operands++;
152
        } else {
153
                freemem += 4;
154
                return;
155
        }
156
 
157
        /* op2 */
158
        if (*operands)
159
                strcpy(mem[freemem].op2, operands);
160
        if (strstr(mem[freemem].op2, OPERAND_DELIM)) {
161
                operands = strstr(mem[freemem].op2, OPERAND_DELIM);
162
                *operands = '\0';
163
                operands++;
164
        } else {
165
                freemem += 4;
166
                return;
167
        }
168
 
169
        /* op3 */
170
        if (*operands)
171
                strcpy(mem[freemem].op3, operands);
172
        if (strstr(mem[freemem].op3, OPERAND_DELIM)) {
173
                operands = strstr(mem[freemem].op3, OPERAND_DELIM);
174
                *operands = '\0';
175
                operands++;
176
        } else {
177
                freemem += 4;
178
                return;
179
        }
180
 
181
        /* op4 */
182
        if (*operands)
183
                strcpy(mem[freemem].op4, operands);
184
        if (strstr(mem[freemem].op4, OPERAND_DELIM)) {
185
                operands = strstr(mem[freemem].op4, OPERAND_DELIM);
186
                *operands = '\0';
187
                operands++;
188
        }
189
 
190
        freemem += 4;
191
        return;
192
}
193
 
194
/* Non-architecture dependent parsing: stripping comments, filling
195
   abstract memory */
196
 
197
void parseline(char *inputline)
198
{
199
        char item[MAXLINE_LEN];
200
        char item2[MAXLINE_LEN];
201
        int  i = 0;
202
 
203
        /* Strip comments: simply terminate line where
204
           the first comment character appears. */
205
 
206
        debug("PARSING: %s", inputline);
207
        while (inputline[i] != '\0')
208
                if (inputline[i] == COMMENT_CHAR) {
209
                        inputline[i] = '\0';
210
                        break;
211
                } else
212
                        i++;
213
 
214
        /* Get the first item from this line */
215
        strtoken(inputline, item, 1);
216
        strtoken(inputline, item2, 2);
217
 
218
        /* Is this item empty? Nothing to process, so return. */
219
        if (strlen(item) == 0)
220
                return;
221
 
222
        /* Is this item a label? If yes, add it to the label
223
           table and return immediately. */
224
        if (strstr(item, LABELEND_CHAR)) {
225
                addlabel(item);
226
                return;
227
        }
228
 
229
        /* Is this item a .directive? If yes, check for some supported
230
           and then return (even if unsupported found). */
231
        if (item[0] == DIRECTIVE_CHAR) {
232
                if ((strcmp(item, ".align") == 0) && (freemem % 4)) {
233
                        freemem &= -4;  /* e.g. 0xfffffffc */
234
                        freemem += 4;   /* always align to word */
235
                        return;
236
                } else
237
                if (strcmp(item, ".ascii") == 0) {
238
                        adddatastr(strstr(inputline, "\""));
239
                        return;
240
                } else
241
                if (strcmp(item, ".word") == 0) {
242
                        adddataword(item2);
243
                        return;
244
                } else
245
                if (strcmp(item, ".half") == 0) {
246
                        adddatahalf(item2);
247
                        return;
248
                } else
249
                if (strcmp(item, ".byte") == 0) {
250
                        adddatabyte(item2);
251
                        return;
252
                } else
253
                if (strcmp(item, ".space") == 0) {
254
                        adddataspace(item2);
255
                        return;
256
                } else  /* .directive but not one of the supported */
257
                        return;
258
        }
259
 
260
        /* This item can only be an instruction. Get all operands
261
           and add everything to mem array but as a program. */
262
        addprogram(item, item2);
263
 
264
        /* Also do static, single stats. */
265
        addsstats(item, 0, 1);
266
 
267
        return;
268
 
269
}
270
 
271
/* Load file and hand over every line to parse routine. */
272
 
273
void readfile(char *filename)
274
{
275
        FILE *inputfs;
276
        char  inputbuf[MAXLINE_LEN];
277
        char  *status;
278
 
279
        if ((inputfs = fopen(filename, "r"))) {
280
                while ((status = fgets(inputbuf, sizeof(inputbuf), inputfs))) {
281
                        if (nonempty(inputbuf))
282
                                parseline(inputbuf);
283
                }
284
                fclose(inputfs);
285
        }
286
        else
287
                perror("readfile");
288
 
289
        return;
290
}
291
 
292
 
293
void loadcode(char *filename)
294
{
295
        freemem = 0;
296
        memset(mem, 0, sizeof(mem));
297
        readfile(filename);
298
        return;
299
}

powered by: WebSVN 2.1.0

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