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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [Open8 Tools/] [open8_src/] [open8_link/] [files.c] - Blame information for rev 232

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

Line No. Rev Author Line
1 178 jshamlet
 
2
#include <ctype.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
 
7
#include "defines.h"
8
#include "parse.h"
9
#include "files.h"
10
#include "analyze.h"
11
 
12
 
13
 
14
extern struct object_file *obj_first, *obj_last, *obj_tmp;
15
extern struct label *labels_first, *labels_last;
16
extern unsigned char *file_header, *file_footer;
17
extern int file_header_size, file_footer_size;
18
char file_name_error[] = "???";
19
 
20
 
21
 
22
int load_files(char *argv[], int argc) {
23
 
24
  struct label *l;
25
  int st = STATE_NONE, i, x, line, bank, slot, base, bank_defined, slot_defined, base_defined, n;
26
  char tmp[1024], ou[1024];
27
  FILE *fop;
28
 
29
 
30
  fop = fopen(argv[argc - 2], "rb");
31
  if (fop == NULL) {
32
    fprintf(stderr, "LOAD_FILES: Could not open file \"%s\".\n", argv[argc - 2]);
33
    return FAILED;
34
  }
35
 
36
  line = 0;
37
  while (fgets(tmp, 255, fop) != NULL) {
38
    line++;
39
    x = 0;
40
 
41
    if (tmp[0] == ';' || tmp[0] == '*' || tmp[0] == '#' || tmp[0] == 0x0D || tmp[0] == 0x0A)
42
      continue;
43
 
44
    /* remove garbage from the end */
45
    for (i = 0; !(tmp[i] == 0x0D || tmp[i] == 0x0A); i++);
46
    tmp[i] = 0;
47
 
48
    /* empty line check */
49
    if (get_next_token(tmp, ou, &x) == FAILED)
50
      continue;
51
 
52
    /* first checks */
53
    if (ou[0] == '[') {
54
      if (strcmp("[objects]", ou) == 0) {
55
        st = STATE_OBJECT;
56
        continue;
57
      }
58
      else if (strcmp("[libraries]", ou) == 0) {
59
        st = STATE_LIBRARY;
60
        continue;
61
      }
62
      else if (strcmp("[header]", ou) == 0) {
63
        st = STATE_HEADER;
64
        continue;
65
      }
66
      else if (strcmp("[footer]", ou) == 0) {
67
        st = STATE_FOOTER;
68
        continue;
69
      }
70
      else if (strcmp("[definitions]", ou) == 0) {
71
        st = STATE_DEFINITION;
72
        continue;
73
      }
74
      else {
75
        fprintf(stderr, "%s:%d LOAD_FILES: Unknown group \"%s\".\n", argv[argc - 2], line, ou);
76
        fclose(fop);
77
        return FAILED;
78
      }
79
    }
80
 
81
    if (st == STATE_NONE) {
82
      fprintf(stderr, "%s:%d: LOAD_FILES: Before file \"%s\" can be loaded you must define a group for it.\n", argv[argc - 2], line, ou);
83
      fclose(fop);
84
      return FAILED;
85
    }
86
 
87
    bank_defined = OFF;
88
    slot_defined = OFF;
89
    base_defined = OFF;
90
    bank = 0;
91
    slot = 0;
92
    base = 0;
93
 
94
    /* definition loading? */
95
    if (st == STATE_DEFINITION) {
96
      l = malloc(sizeof(struct label));
97
      if (l == NULL) {
98
        fprintf(stderr, "LOAD_FILES: Out of memory.\n");
99
        return FAILED;
100
      }
101
      strcpy(l->name, ou);
102
      l->status = LABEL_STATUS_DEFINE;
103
      l->bank = 0;
104
      l->slot = 0;
105
      l->base = 0;
106
 
107
      if (get_next_number(&tmp[x], &n, &x) == FAILED) {
108
        fprintf(stderr, "%s:%d: LOAD_FILES: Error in DEFINITION value.\n", argv[argc - 2], line);
109
        fclose(fop);
110
        return FAILED;
111
      }
112
 
113
      l->address = n;
114
      add_label(l);
115
      continue;
116
    }
117
    /* header loading? */
118
    else if (st == STATE_HEADER) {
119
      if (file_header != NULL) {
120
        fprintf(stderr, "%s:%d: LOAD_FILES: There can be only one header file.\n", argv[argc - 2], line);
121
        fclose(fop);
122
        return FAILED;
123
      }
124
 
125
      if (load_file_data(ou, &file_header, &file_header_size) == FAILED) {
126
        fclose(fop);
127
        return FAILED;
128
      }
129
      if (get_next_token(&tmp[x], ou, &x) == FAILED)
130
        continue;
131
 
132
      fprintf(stderr, "%s:%d: LOAD_FILES: Syntax error.\n", argv[argc - 2], line);
133
      fclose(fop);
134
      return FAILED;
135
    }
136
    /* footer loading? */
137
    else if (st == STATE_FOOTER) {
138
      if (file_footer != NULL) {
139
        fprintf(stderr, "%s:%d: LOAD_FILES: There can be only one footer file.\n", argv[argc - 2], line);
140
        fclose(fop);
141
        return FAILED;
142
      }
143
 
144
      if (load_file_data(ou, &file_footer, &file_footer_size) == FAILED) {
145
        fclose(fop);
146
        return FAILED;
147
      }
148
      if (get_next_token(&tmp[x], ou, &x) == FAILED)
149
        continue;
150
 
151
      fprintf(stderr, "%s:%d: LOAD_FILES: Syntax error.\n", argv[argc - 2], line);
152
      fclose(fop);
153
      return FAILED;
154
    }
155
    /* library loading? */
156
    else if (st == STATE_LIBRARY) {
157
      i = SUCCEEDED;
158
      while (i == SUCCEEDED) {
159
        if (strcmp(ou, "bank") == 0 || strcmp(ou, "BANK") == 0) {
160
          if (bank_defined == ON) {
161
            fprintf(stderr, "%s:%d: LOAD_FILES: BANK defined for the second time for a library file.\n", argv[argc - 2], line);
162
            fclose(fop);
163
            return FAILED;
164
          }
165
          bank_defined = ON;
166
 
167
          if (get_next_number(&tmp[x], &bank, &x) == FAILED) {
168
            fprintf(stderr, "%s:%d: LOAD_FILES: Error in BANK number.\n", argv[argc - 2], line);
169
            fclose(fop);
170
            return FAILED;
171
          }
172
        }
173
        else if (strcmp(ou, "slot") == 0 || strcmp(ou, "SLOT") == 0) {
174
          if (slot_defined == ON) {
175
            fprintf(stderr, "%s:%d: LOAD_FILES: SLOT defined for the second time for a library file.\n", argv[argc - 2], line);
176
            fclose(fop);
177
            return FAILED;
178
          }
179
          slot_defined = ON;
180
 
181
          if (get_next_number(&tmp[x], &slot, &x) == FAILED) {
182
            fprintf(stderr, "%s:%d: LOAD_FILES: Error in SLOT number.\n", argv[argc - 2], line);
183
            fclose(fop);
184
            return FAILED;
185
          }
186
        }
187
        else if (strcmp(ou, "base") == 0 || strcmp(ou, "BASE") == 0) {
188
          if (base_defined == ON) {
189
            fprintf(stderr, "%s:%d: LOAD_FILES: BASE defined for the second time for a library file.\n", argv[argc - 2], line);
190
            fclose(fop);
191
            return FAILED;
192
          }
193
          base_defined = ON;
194
 
195
          if (get_next_number(&tmp[x], &base, &x) == FAILED) {
196
            fprintf(stderr, "%s:%d: LOAD_FILES: Error in BASE number.\n", argv[argc - 2], line);
197
            fclose(fop);
198
            return FAILED;
199
          }
200
        }
201
        else
202
          break;
203
 
204
        i = get_next_token(&tmp[x], ou, &x);
205
      }
206
 
207
      if (i == FAILED) {
208
        fprintf(stderr, "%s:%d: LOAD_FILES: No library to load.\n", argv[argc - 2], line);
209
        fclose(fop);
210
        return FAILED;
211
      }
212
      if (slot_defined == OFF) {
213
        fprintf(stderr, "%s:%d: LOAD_FILES: Library file requires a SLOT.\n", argv[argc - 2], line);
214
        fclose(fop);
215
        return FAILED;
216
      }
217
      if (bank_defined == OFF) {
218
        fprintf(stderr, "%s:%d: LOAD_FILES: Library file requires a BANK.\n", argv[argc - 2], line);
219
        fclose(fop);
220
        return FAILED;
221
      }
222
 
223
      if (load_file(ou, STATE_LIBRARY, bank, slot, base, base_defined) == FAILED) {
224
        fclose(fop);
225
        return FAILED;
226
      }
227
 
228
      if (get_next_token(&tmp[x], ou, &x) == SUCCEEDED) {
229
        fprintf(stderr, "%s:%d: LOAD_FILES: Syntax error.\n", argv[argc - 2], line);
230
        fclose(fop);
231
        return FAILED;
232
      }
233
 
234
      continue;
235
    }
236
    /* object file loading */
237
    else if (load_file(ou, STATE_OBJECT, 0, 0, 0, OFF) == FAILED) {
238
      fclose(fop);
239
      return FAILED;
240
    }
241
    if (get_next_token(&tmp[x], ou, &x) == FAILED)
242
      continue;
243
 
244
    fprintf(stderr, "%s:%d: LOAD_FILES: Syntax error.\n", argv[argc - 2], line);
245
    fclose(fop);
246
    return FAILED;
247
  }
248
 
249
  fclose(fop);
250
 
251
  return SUCCEEDED;
252
}
253
 
254
 
255
int load_file(char *fn, int state, int bank, int slot, int base, int base_defined) {
256
 
257
  struct object_file *o;
258
  unsigned char *data;
259
  static int id = 0;
260
  char *n;
261
  int size;
262
 
263
 
264
  o = malloc(sizeof(struct object_file));
265
  n = malloc(strlen(fn)+1);
266
  if (o == NULL || n == NULL) {
267
    if (o != NULL)
268
      free(o);
269
    if (n != NULL)
270
      free(n);
271
    fprintf(stderr, "LOAD_FILE: Out of memory.\n");
272
    return FAILED;
273
  }
274
 
275
  if (load_file_data(fn, &data, &size) == FAILED) {
276
    free(n);
277
    free(o);
278
    return FAILED;
279
  }
280
 
281
  /* only valid for library files */
282
  o->bank = bank;
283
  o->slot = slot;
284
  o->base = base;
285
  o->base_defined = base_defined;
286
 
287
  /* init the rest of the variables */
288
  o->source_file_names = NULL;
289
  o->memorymap = NULL;
290
  o->exported_defines = NULL;
291
  o->data_blocks = NULL;
292
  o->source_file_names_list = NULL;
293
 
294
  if (obj_first == NULL) {
295
    obj_first = o;
296
    obj_last = o;
297
  }
298
  else {
299
    obj_last->next = o;
300
    obj_last = o;
301
  }
302
 
303
  o->next = NULL;
304
  o->size = size;
305
  o->data = data;
306
 
307
  strcpy(n, fn);
308
  o->name = n;
309
  o->id = id++;
310
 
311
  return SUCCEEDED;
312
}
313
 
314
 
315
int load_file_data(char *fn, unsigned char **da, int *size) {
316
 
317
  FILE *fop;
318
 
319
 
320
  fop = fopen(fn, "rb");
321
  if (fop == NULL) {
322
    fprintf(stderr, "LOAD_FILE_DATA: Could not open file \"%s\".\n", fn);
323
    return FAILED;
324
  }
325
 
326
  fseek(fop, 0, SEEK_END);
327
  *size = ftell(fop);
328
  fseek(fop, 0, SEEK_SET);
329
 
330
  *da = malloc(*size);
331
  if (*da == NULL)
332
    return FAILED;
333
 
334
  fread(*da, 1, *size, fop);
335
  fclose(fop);
336
 
337
  return SUCCEEDED;
338
}
339
 
340
 
341
char *get_file_name(int id) {
342
 
343
  static char e[] = "GET_FILE_NAME: Internal data corruption.";
344
  struct object_file *o;
345
 
346
 
347
  o = obj_first;
348
  while (o != NULL) {
349
    if (o->id == id)
350
      return o->name;
351
    o = o->next;
352
  }
353
 
354
  return e;
355
}
356
 
357
 
358
char *get_source_file_name(int file_id, int source_id) {
359
 
360
  struct source_file_name *s;
361
  struct object_file *o;
362
 
363
 
364
  o = obj_first;
365
  while (o != NULL) {
366
    if (o->id == file_id)
367
      break;
368
    o = o->next;
369
  }
370
 
371
  if (o == NULL)
372
    return file_name_error;
373
 
374
  s = o->source_file_names_list;
375
  while (s != NULL) {
376
    if (s->id == source_id)
377
      break;
378
    s = s->next;
379
  }
380
 
381
  if (s == NULL)
382
    return file_name_error;
383
 
384
  return s->name;
385
}

powered by: WebSVN 2.1.0

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