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 18

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 18 lampret
#include "arch.h"
28 2 cvs
 
29
#define MAXLINE_LEN     18000
30
 
31
/* Unused mem memory marker. It is used when allocating program and data memory
32
   during parsing */
33
unsigned int freemem;
34
 
35
int nonempty(char *line)
36
{
37
        int i;
38
 
39
        for(i = 0; i < strlen(line); i++)
40
                if (!isspace(line[i]))
41
                        return(1);
42
        return(0);
43
}
44
 
45
int nondigit(char *line)
46
{
47
        int i;
48
 
49
        for(i = 0; i < strlen(line); i++)
50
                if (!isdigit(line[i]))
51
                        return(1);
52
        return(0);
53
}
54
 
55
char *strtoken(char *in, char *out, int which)
56
{
57
        char    *super;
58
        char    *sub;
59
        char    *newline;
60
 
61
        super = strdup(in);
62
        sub = strtok(super, " \t");
63
        while (sub && --which)
64
                sub = strtok(NULL, " \t");
65
        if (sub && !which) {
66
                if ((newline = strchr(sub, '\n')))
67
                        newline[0] = '\0';
68
                strcpy(out, sub);
69
        } else
70
                out[0] = '\0';
71
        free(super);
72
        if ((newline = strchr(out, '\r')))      /* get rid of CR */
73
                newline[0] = '\0';
74
        return(out);
75
}
76
 
77
void adddatastr(char *str)
78
{
79
        if (str)
80
                str++;
81
        else
82
                return;
83
 
84
        for(; *str && *str != '\"'; str++, freemem++)
85
                if (*str == '\\')
86
                        switch (*++str) {
87
                                case 'n': mem[freemem].data = '\n';
88
                                        break;
89
                                case 't': mem[freemem].data = '\t';
90
                                        break;
91
                                case 'r': mem[freemem].data = '\r';
92
                                        break;
93
                                case '0': mem[freemem].data = '\0';
94
                                        break;
95
                                default: break;
96
                        }
97
                else
98
                        mem[freemem].data = *str;
99
}
100
 
101
void adddataword(char *num)
102
{
103 18 lampret
        mem[freemem].data = (char) (atol(num) >> 24);
104
        mem[freemem + 1].data = (char) (atol(num) >> 16);
105
        mem[freemem + 2].data = (char) (atol(num) >> 8);
106
        mem[freemem + 3].data = (char) (atol(num));
107 2 cvs
 
108
        freemem += 4;
109
}
110
 
111
void adddatahalf(char *num)
112
{
113
        mem[freemem].data = (char) (atol(num) >> 8);
114
        mem[freemem + 1].data = (char) (atol(num));
115
 
116
        freemem += 2;
117
}
118
 
119
void adddatabyte(char *num)
120
{
121
        mem[freemem].data = (char) (atol(num));
122
 
123
        freemem++;
124
}
125
 
126
void adddataspace(char *num)
127
{
128
        freemem += atol(num);
129
}
130
 
131
void addlabel(char *label)
132
{
133
        if (strstr(label, LABELEND_CHAR)) {
134
                *strstr(label, LABELEND_CHAR) = '\0';
135
                strcpy(mem[freemem].label, label);
136
        }
137
 
138
        return;
139
}
140
 
141
void addprogram(char *insn, char *operands)
142
{
143 18 lampret
#ifdef  __HALF_WORD_INSN__
144
        int h_insn_is_word_flag=0;
145
        char insn_first2_char[3];
146
 
147 2 cvs
        strcpy(mem[freemem].insn, insn);
148 18 lampret
        insn_first2_char[0]=insn[0];
149
        insn_first2_char[1]=insn[1];
150
        insn_first2_char[2]='\0';
151 2 cvs
 
152 18 lampret
        if(strcmp("h.", insn_first2_char) == 0) {
153
                if(strcmp("h.load32u", insn) == 0 ||
154
                   strcmp("h.load16u", insn) == 0 ||
155
                   strcmp("h.load8u", insn) == 0 ||
156
                   strcmp("h.stor32", insn) == 0 ||
157
                   strcmp("h.stor16", insn) == 0 ||
158
                   strcmp("h.stor8", insn) == 0 ||
159
                   strcmp("h.jal", insn) == 0 ||
160
                   strcmp("h.mtsr", insn) == 0 ||
161
                   strcmp("h.mfsr", insn) == 0 ||
162
                   strcmp("h.movi16ze", insn) == 0 ||
163
                   strcmp("h.immhi16u", insn) == 0 ||
164
                   strcmp("h.addi16s", insn) == 0 ||
165
                   strcmp("h.subi16s", insn) == 0 ||
166
                   strcmp("h.xori16", insn) == 0 ||
167
                   strcmp("h.ori16", insn) == 0 ||
168
                   strcmp("h.andi16", insn) == 0
169
                  )
170
                        h_insn_is_word_flag = 2; /* h.xxx insn AND occupy 4 bytes */
171
                else
172
                        h_insn_is_word_flag = 1; /* h.xxx insn AND occupy 2 bytes */
173
        }
174
        else {
175
                        h_insn_is_word_flag = 0; /* not h.xxx insn */
176
        }
177
#else
178
        strcpy(mem[freemem].insn, insn);
179
#endif
180
 
181 2 cvs
        /* op1 */
182
        if (*operands)
183
                strcpy(mem[freemem].op1, operands);
184
        if (strstr(mem[freemem].op1, OPERAND_DELIM)) {
185
                operands = strstr(mem[freemem].op1, OPERAND_DELIM);
186
                *operands = '\0';
187
                operands++;
188
        } else {
189 18 lampret
#ifdef __HALF_WORD_INSN__
190
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
191
#else
192 2 cvs
                freemem += 4;
193 18 lampret
#endif
194 2 cvs
                return;
195
        }
196
 
197
        /* op2 */
198
        if (*operands)
199
                strcpy(mem[freemem].op2, operands);
200
        if (strstr(mem[freemem].op2, OPERAND_DELIM)) {
201
                operands = strstr(mem[freemem].op2, OPERAND_DELIM);
202
                *operands = '\0';
203
                operands++;
204
        } else {
205 18 lampret
#ifdef __HALF_WORD_INSN__
206
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
207
#else
208 2 cvs
                freemem += 4;
209 18 lampret
#endif
210 2 cvs
                return;
211
        }
212
 
213
        /* op3 */
214
        if (*operands)
215
                strcpy(mem[freemem].op3, operands);
216
        if (strstr(mem[freemem].op3, OPERAND_DELIM)) {
217
                operands = strstr(mem[freemem].op3, OPERAND_DELIM);
218
                *operands = '\0';
219
                operands++;
220
        } else {
221 18 lampret
#ifdef __HALF_WORD_INSN__
222
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
223
#else
224 2 cvs
                freemem += 4;
225 18 lampret
#endif
226 2 cvs
                return;
227
        }
228
 
229
        /* op4 */
230
        if (*operands)
231
                strcpy(mem[freemem].op4, operands);
232
        if (strstr(mem[freemem].op4, OPERAND_DELIM)) {
233
                operands = strstr(mem[freemem].op4, OPERAND_DELIM);
234
                *operands = '\0';
235
                operands++;
236
        }
237
 
238 18 lampret
#ifdef __HALF_WORD_INSN__
239
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
240
#else
241
                freemem += 4;
242
#endif
243 2 cvs
        return;
244
}
245
 
246
/* Non-architecture dependent parsing: stripping comments, filling
247
   abstract memory */
248
 
249
void parseline(char *inputline)
250
{
251
        char item[MAXLINE_LEN];
252
        char item2[MAXLINE_LEN];
253
        int  i = 0;
254
 
255
        /* Strip comments: simply terminate line where
256
           the first comment character appears. */
257
 
258
        debug("PARSING: %s", inputline);
259
        while (inputline[i] != '\0')
260
                if (inputline[i] == COMMENT_CHAR) {
261
                        inputline[i] = '\0';
262
                        break;
263
                } else
264
                        i++;
265
 
266
        /* Get the first item from this line */
267 18 lampret
        strtoken(inputline, item, 1);   /* opcode */
268
        strtoken(inputline, item2, 2);  /* all the remaining one/two/three operands */
269 2 cvs
 
270
        /* Is this item empty? Nothing to process, so return. */
271
        if (strlen(item) == 0)
272
                return;
273
 
274 18 lampret
        /* Is this item a label? If yes, add it to the label table and return immediately. */
275 2 cvs
        if (strstr(item, LABELEND_CHAR)) {
276
                addlabel(item);
277
                return;
278
        }
279
 
280
        /* Is this item a .directive? If yes, check for some supported
281
           and then return (even if unsupported found). */
282
        if (item[0] == DIRECTIVE_CHAR) {
283
                if ((strcmp(item, ".align") == 0) && (freemem % 4)) {
284
                        freemem &= -4;  /* e.g. 0xfffffffc */
285
                        freemem += 4;   /* always align to word */
286
                        return;
287
                } else
288
                if (strcmp(item, ".ascii") == 0) {
289
                        adddatastr(strstr(inputline, "\""));
290
                        return;
291
                } else
292
                if (strcmp(item, ".word") == 0) {
293
                        adddataword(item2);
294
                        return;
295
                } else
296
                if (strcmp(item, ".half") == 0) {
297
                        adddatahalf(item2);
298
                        return;
299
                } else
300
                if (strcmp(item, ".byte") == 0) {
301
                        adddatabyte(item2);
302
                        return;
303
                } else
304
                if (strcmp(item, ".space") == 0) {
305
                        adddataspace(item2);
306
                        return;
307
                } else  /* .directive but not one of the supported */
308
                        return;
309
        }
310
 
311
        /* This item can only be an instruction. Get all operands
312
           and add everything to mem array but as a program. */
313
        addprogram(item, item2);
314
 
315
        /* Also do static, single stats. */
316
        addsstats(item, 0, 1);
317
 
318
        return;
319
 
320
}
321
 
322
/* Load file and hand over every line to parse routine. */
323
 
324
void readfile(char *filename)
325
{
326
        FILE *inputfs;
327
        char  inputbuf[MAXLINE_LEN];
328
        char  *status;
329
 
330
        if ((inputfs = fopen(filename, "r"))) {
331
                while ((status = fgets(inputbuf, sizeof(inputbuf), inputfs))) {
332
                        if (nonempty(inputbuf))
333
                                parseline(inputbuf);
334
                }
335
                fclose(inputfs);
336
        }
337
        else
338
                perror("readfile");
339
 
340
        return;
341
}
342
 
343
 
344
void loadcode(char *filename)
345
{
346
        freemem = 0;
347
        memset(mem, 0, sizeof(mem));
348
        readfile(filename);
349
        return;
350
}

powered by: WebSVN 2.1.0

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