Add a simple dissasembler to igen
[deliverable/binutils-gdb.git] / sim / igen / table.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995,1997 Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <ctype.h>
27
28 #include "config.h"
29 #include "misc.h"
30 #include "lf.h"
31 #include "table.h"
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40
41 struct _table {
42 size_t size;
43 char *buffer;
44 char *pos;
45 int nr_fields;
46 int nr_model_fields;
47 int line_nr;
48 char *file_name;
49 int current_file_line_offset;
50 char *current_file_name;
51 };
52
53 extern table *
54 table_open(const char *file_name,
55 int nr_fields,
56 int nr_model_fields)
57 {
58 int fd;
59 struct stat stat_buf;
60 table *file;
61
62 /* create a file descriptor */
63 file = ZALLOC(table);
64 ASSERT(file != NULL);
65 file->nr_fields = nr_fields;
66 file->nr_model_fields = nr_model_fields;
67
68 /* save the file name */
69 file->file_name = (char*)zalloc(strlen(file_name) + 1);
70 ASSERT(file->file_name != NULL);
71 strcpy(file->file_name, file_name);
72 file->current_file_name = file->file_name;
73
74 /* open the file */
75 fd = open(file->file_name, O_RDONLY, 0);
76 if (fd < 0) {
77 perror(file->file_name);
78 exit (1);
79 }
80
81 /* determine the size */
82 if (fstat(fd, &stat_buf) < 0) {
83 perror("table_open.fstat");
84 exit(1);
85 }
86 file->size = stat_buf.st_size;
87
88 /* allocate this much memory */
89 file->buffer = (char*)zalloc(file->size+1);
90 ASSERT(file->buffer != NULL);
91 file->pos = file->buffer;
92
93 /* read it in */
94 if (read(fd, file->buffer, file->size) < file->size) {
95 perror(file->file_name);
96 exit(1);
97 }
98 file->buffer[file->size] = '\0';
99
100 /* set the initial line numbering */
101 file->line_nr = 0;
102 file->current_file_line_offset = 0;
103
104 /* done */
105 close(fd);
106 return file;
107 }
108
109
110 extern table_entry *
111 table_entry_read(table *file)
112 {
113 int field;
114 table_entry *entry;
115
116 /* skip comments/blanks */
117 while(1) {
118 /* leading white space */
119 while (*file->pos != '\0'
120 && *file->pos != '\n'
121 && isspace(*file->pos))
122 file->pos++;
123 /* cpp line nr directive - # <line-nr> "<file>" */
124 if (file->pos[0] == '#'
125 && file->pos[1] == ' '
126 && isdigit(file->pos[2])) {
127 file->pos += strlen("# ");
128 /* parse the number */
129 file->current_file_line_offset = atoi(file->pos) - file->line_nr - 2;
130 /* skip to the file name */
131 while (file->pos[0] != '0'
132 && file->pos[0] != '"'
133 && file->pos[0] != '\0')
134 file->pos++;
135 if (file->pos[0] != '"') {
136 error("%s:%d: Missing opening quote",
137 file->file_name,
138 file->line_nr);
139 }
140 /* parse the file name */
141 file->pos++;
142 file->current_file_name = file->pos;
143 while (file->pos[0] != '"'
144 && file->pos[0] != '\0')
145 file->pos++;
146 if (file->pos[0] != '"') {
147 error("%s:%d: Missing closing quote",
148 file->file_name,
149 file->line_nr);
150 }
151 file->pos[0] = '\0';
152 file->pos ++;
153 while (file->pos[0] != '\0'
154 && file->pos[0] != '\n')
155 file->pos[0]++;
156 if (file->pos[0] != '\n')
157 error("%s:%d: Missing newline",
158 file->file_name,
159 file->line_nr);
160 }
161 /* comment - leading // or # - skip */
162 else if ((file->pos[0] == '/' && file->pos[1] == '/')
163 || (file->pos[0] == '#')) {
164 do {
165 file->pos++;
166 } while (*file->pos != '\0' && *file->pos != '\n');
167 }
168 /* end of line? */
169 if (*file->pos == '\n') {
170 file->pos++;
171 file->line_nr++;
172 }
173 else
174 break;
175 }
176 if (*file->pos == '\0')
177 return NULL;
178
179 /* create this new entry */
180 entry = (table_entry*)zalloc(sizeof(table_entry)
181 + (file->nr_fields + 1) * sizeof(char*));
182 ASSERT(entry != NULL);
183 entry->file_name = file->current_file_name;
184 entry->nr_fields = file->nr_fields;
185
186 /* break the line into its colon delimitered fields */
187 for (field = 0; field < file->nr_fields-1; field++) {
188 entry->fields[field] = file->pos;
189 while(*file->pos && *file->pos != ':' && *file->pos != '\n')
190 file->pos++;
191 if (*file->pos == ':') {
192 *file->pos = '\0';
193 file->pos++;
194 }
195 }
196
197 /* any trailing stuff not the last field */
198 ASSERT(field == file->nr_fields-1);
199 entry->fields[field] = file->pos;
200 while (*file->pos && *file->pos != '\n') {
201 file->pos++;
202 }
203 if (*file->pos == '\n') {
204 *file->pos = '\0';
205 file->pos++;
206 }
207 file->line_nr++;
208
209 /* If following lines being with a double quote ("), add them to the
210 list of assembler lines */
211 {
212 table_assembler_entry **current = &entry->assembler;
213 while (*file->pos == '"') {
214 char *tmpchp;
215 const char *format;
216 int strlen_format;
217 const char *condition;
218 int strlen_condition;
219
220 /* skip over the format string */
221 format = file->pos;
222 strlen_format = 0;
223 do {
224 if (file->pos[0] == '\\' && file->pos[1] == '"')
225 file->pos += 2;
226 else
227 file->pos += 1;
228 } while (*file->pos != '\0' && *file->pos != '\n' && *file->pos != '"');
229 if (*file->pos != '"')
230 error ("%s:%d: Missing closing quote in assembler line",
231 file->file_name,
232 file->line_nr);
233 file->pos++;
234 strlen_format = file->pos - format;
235
236 /* skip over the boolean condition */
237 condition = NULL;
238 strlen_condition = 0;
239 if (*file->pos == ':')
240 {
241 file->pos++;
242 while (isspace(*file->pos) && *file->pos != '\0' && *file->pos != '\n')
243 file->pos++;
244 condition = file->pos;
245 while (*file->pos != '\0' && *file->pos != '\n')
246 file->pos++;
247 strlen_condition = file->pos - condition;
248 }
249
250 /* create the new assembler entry */
251 *current = ZALLOC (table_assembler_entry);
252 tmpchp = zalloc (strlen_format + 1);
253 strncpy (tmpchp, format, strlen_format);
254 (*current)->format = tmpchp;
255 (*current)->file_name = file->file_name;
256 (*current)->line_nr = file->line_nr;
257 if (condition != NULL && strlen_condition > 0)
258 {
259 tmpchp = zalloc (strlen_condition + 1);
260 strncpy (tmpchp, condition, strlen_condition);
261 (*current)->condition = tmpchp;
262 }
263 current = &(*current)->next;
264
265 /* end of line? */
266 if (*file->pos != '\n')
267 error ("%s:%d: Missing eoln in assembler line",
268 file->file_name,
269 file->line_nr);
270 file->pos++;
271 file->line_nr++;
272 }
273 }
274
275 /* if following lines begin with a star, add them to the model
276 section. */
277 while ((file->nr_model_fields > 0) && (*file->pos == '*')) {
278 table_model_entry *model = (table_model_entry*)zalloc(sizeof(table_model_entry)
279 + (file->nr_model_fields + 1) * sizeof(char*));
280 if (entry->model_last)
281 entry->model_last->next = model;
282 else
283 entry->model_first = model;
284 entry->model_last = model;
285
286 /* break the line into its colon delimitered fields */
287 file->pos++;
288 for (field = 0; field < file->nr_model_fields-1; field++) {
289 model->fields[field] = file->pos;
290 while(*file->pos && *file->pos != ':' && *file->pos != '\n')
291 file->pos++;
292 if (*file->pos == ':') {
293 *file->pos = '\0';
294 file->pos++;
295 }
296 }
297
298 /* any trailing stuff not the last field */
299 ASSERT(field == file->nr_model_fields-1);
300 model->fields[field] = file->pos;
301 while (*file->pos && *file->pos != '\n') {
302 file->pos++;
303 }
304 if (*file->pos == '\n') {
305 *file->pos = '\0';
306 file->pos++;
307 }
308
309 file->line_nr++;
310 model->line_nr = file->current_file_line_offset + file->line_nr;
311 }
312
313 entry->line_nr = file->current_file_line_offset + file->line_nr;
314
315 /* if following lines are tab indented, put in the annex */
316 if (*file->pos == '\t') {
317 entry->annex = file->pos;
318 do {
319 do {
320 file->pos++;
321 } while (*file->pos != '\0' && *file->pos != '\n');
322 if (*file->pos == '\n') {
323 char *save_pos = ++file->pos;
324 int extra_lines = 0;
325 file->line_nr++;
326 /* Allow tab indented to have blank lines */
327 while (*save_pos == '\n') {
328 save_pos++;
329 extra_lines++;
330 }
331 if (*save_pos == '\t') {
332 file->pos = save_pos;
333 file->line_nr += extra_lines;
334 }
335 }
336 } while (*file->pos != '\0' && *file->pos == '\t');
337 if (file->pos[-1] == '\n')
338 file->pos[-1] = '\0';
339 }
340 else
341 entry->annex = NULL;
342
343 /* return it */
344 return entry;
345
346 }
347
348
349 extern void
350 dump_table_entry(table_entry *entry,
351 int indent)
352 {
353 printf("(table_entry*)%p\n", entry);
354
355 if (entry != NULL) {
356 int field;
357 char sep;
358
359 sep = ' ';
360 dumpf(indent, "(fields");
361 for (field = 0; field < entry->nr_fields; field++) {
362 printf("%c%s", sep, entry->fields[field]);
363 sep = ':';
364 }
365 printf(")\n");
366
367 dumpf(indent, "(line_nr %d)\n", entry->line_nr);
368
369 dumpf(indent, "(file_name %s)\n", entry->file_name);
370
371 dumpf(indent, "(annex\n%s\n", entry->annex);
372 dumpf(indent, " )\n");
373
374 }
375 }
376
377
378 extern void
379 table_entry_print_cpp_line_nr(lf *file,
380 table_entry *entry)
381 {
382 lf_print__external_reference(file, entry->line_nr, entry->file_name);
383 }
384
385
This page took 0.055037 seconds and 5 git commands to generate.