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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [Open8 Tools/] [open8_src/] [open8_link/] [listfile.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 "listfile.h"
9
#include "files.h"
10
 
11
 
12
/* output */
13
extern unsigned char *rom;
14
 
15
 
16
/* read an integer from t */
17
#define READ_T t[3] + (t[2] << 8) + (t[1] << 16) + (t[0] << 24); t += 4;
18
 
19
 
20
static int _listfile_sort(const void *a, const void *b) {
21
 
22
  struct listfileitem *la, *lb;
23
  int i;
24
 
25
 
26
  la = *((struct listfileitem **)a);
27
  lb = *((struct listfileitem **)b);
28
  i = strcmp(la->sourcefilename, lb->sourcefilename);
29
  if (i == 0) {
30
    /* both lines are from the same source */
31
    if (la->linenumber < lb->linenumber)
32
      return -1;
33
    return 1;
34
  }
35
 
36
  /* sort by source file name */
37
  return i;
38
}
39
 
40
 
41
static int _listfile_write_hex(FILE *f, int i) {
42
 
43
  if (i >= 10)
44
    fprintf(f, "%c", i + 'A' - 10);
45
  else
46
    fprintf(f, "%c", i + '0');
47
 
48
  return SUCCEEDED;
49
}
50
 
51
 
52
int listfile_write_listfiles(struct section *e) {
53
 
54
  struct listfileitem **l = NULL, *d = NULL;
55
  struct section *s;
56
  int n, i, j, k, m, o, p, sid = -1, add, w;
57
  char c, tmp[512], *b, *na;
58
  FILE *f;
59
 
60
 
61
  if (e == NULL)
62
    return FAILED;
63
 
64
  /* collect the list file items from the data */
65
  n = 0;
66
  i = 0;
67
  s = e;
68
  while (s != NULL) {
69
    if (s->listfile_items <= 0 || s->status == SECTION_STATUS_RAM) {
70
      s = s->next;
71
      continue;
72
    }
73
 
74
    n += s->listfile_items;
75
    d = realloc(d, sizeof(struct listfileitem) * n);
76
    if (d == NULL) {
77
      fprintf(stderr, "LISTFILE_WRITE_LISTFILES: Out of memory error.\n");
78
      return FAILED;
79
    }
80
 
81
    /* parse the list file information */
82
    add = 0;
83
    for (j = 0; j < s->listfile_items; j++) {
84
      c = s->listfile_cmds[j];
85
      if (c == 'k') {
86
        /* new line */
87
        if (s->listfile_ints[j*2 + 1] > 0) {
88
          d[i].sourcefilename = get_source_file_name(s->file_id, sid);
89
          d[i].linenumber = s->listfile_ints[j*2 + 0];
90
          d[i].length = s->listfile_ints[j*2 + 1];
91
          d[i].address = s->output_address + add;
92
          add += s->listfile_ints[j*2 + 1];
93
          i++;
94
        }
95
      }
96
      else if (c == 'f') {
97
        /* another file */
98
        sid = s->listfile_ints[j*2 + 0];
99
      }
100
    }
101
 
102
    s = s->next;
103
  }
104
 
105
  /* create pointers for sorting */
106
  l = malloc(sizeof(struct listfileitem *) * i);
107
  if (l == NULL) {
108
    fprintf(stderr, "LISTFILE_WRITE_LISTFILES: Out of memory error.\n");
109
    return FAILED;
110
  }
111
 
112
  for (j = 0; j < i; j++)
113
    l[j] = &d[j];
114
 
115
  /* sort by source file name and line number */
116
  qsort(l, i, sizeof(struct listfileitem *), _listfile_sort);
117
 
118
  /* write the listfiles */
119
  j = 0;
120
  while (j < i) {
121
    na = l[j]->sourcefilename;
122
    f = fopen(na, "rb");
123
    fseek(f, 0, SEEK_END);
124
    n = ftell(f);
125
    fseek(f, 0, SEEK_SET);
126
 
127
    b = malloc(n);
128
    if (b == NULL) {
129
      fprintf(stderr, "LISTFILE_WRITE_LISTFILES: Out of memory error.\n");
130
      fclose(f);
131
      return FAILED;
132
    }
133
 
134
    fread(b, 1, n, f);
135
    fclose(f);
136
 
137
    strcpy(tmp, na);
138
    for (k = strlen(tmp)-1; k >= 0; k--) {
139
      if (tmp[k] == '.')
140
        break;
141
    }
142
    strcpy(&tmp[k], ".lst");
143
 
144
    f = fopen(tmp, "wb");
145
    if (f == NULL) {
146
      fprintf(stderr, "LISTFILE_WRITE_LISTFILES: Could not open file \"%s\" for writing.\n", tmp);
147
      return FAILED;
148
    }
149
 
150
    /* write the lines */
151
    k = 0;
152
    m = 0;
153
    while (j < i && l[j]->sourcefilename == na) {
154
      /* goto line x */
155
      while (k < l[j]->linenumber-1) {
156
        for (o = 0; o < 40; o++)
157
          fprintf(f, " ");
158
        while (1) {
159
          if (b[m] == 0xA) {
160
            m++;
161
            if (b[m] == 0xD)
162
              m++;
163
            k++;
164
            fprintf(f, "\n");
165
            break;
166
          }
167
          else
168
            fprintf(f, "%c", b[m++]);
169
        }
170
      }
171
 
172
      /* write the bytes */
173
      w = 0;
174
      for (p = 0, o = 0; o < l[j]->length; o++) {
175
        fprintf(f, "$");
176
        _listfile_write_hex(f, rom[l[j]->address + o] >> 4);
177
        _listfile_write_hex(f, rom[l[j]->address + o] & 15);
178
        fprintf(f, " ");
179
        p += 4;
180
        if ((o % 10) == 9 && o != 0 && o < l[j]->length-1) {
181
          if (w == 0) {
182
            /* write padding */
183
            w = 1;
184
            while (p < 40) {
185
              fprintf(f, " ");
186
              p++;
187
            }
188
 
189
            /* write the rest of the line */
190
            while (m < n) {
191
              if (b[m] == 0xA) {
192
                m++;
193
                if (b[m] == 0xD)
194
                  m++;
195
                k++;
196
                fprintf(f, "\n");
197
                break;
198
              }
199
              else
200
                fprintf(f, "%c", b[m++]);
201
            }
202
          }
203
          else
204
            fprintf(f, "\n");
205
 
206
          p = 0;
207
        }
208
      }
209
 
210
      /* has the line been written already? */
211
      if (w == 0) {
212
        /* write padding */
213
        while (p < 40) {
214
          fprintf(f, " ");
215
          p++;
216
        }
217
 
218
        /* write the rest of the line */
219
        while (m < n) {
220
          if (b[m] == 0xA) {
221
            m++;
222
            if (b[m] == 0xD)
223
              m++;
224
            k++;
225
            fprintf(f, "\n");
226
            break;
227
          }
228
          else
229
            fprintf(f, "%c", b[m++]);
230
        }
231
      }
232
      else
233
        fprintf(f, "\n");
234
 
235
      j++;
236
    }
237
 
238
    /* write the rest of the file */
239
    while (m < n) {
240
      for (o = 0; o < 40; o++)
241
        fprintf(f, " ");
242
      while (m < n) {
243
        if (b[m] == 0xA) {
244
          m++;
245
          if (b[m] == 0xD)
246
            m++;
247
          k++;
248
          fprintf(f, "\n");
249
          break;
250
        }
251
        else
252
          fprintf(f, "%c", b[m++]);
253
      }
254
    }
255
 
256
    fclose(f);
257
 
258
    if (b != NULL)
259
      free(b);
260
  }
261
 
262
  if (l != NULL)
263
    free(l);
264
  if (d != NULL)
265
    free(d);
266
 
267
  return SUCCEEDED;
268
}
269
 
270
 
271
int listfile_block_read(unsigned char **d, struct section *s) {
272
 
273
  unsigned char *t;
274
  int i;
275
 
276
 
277
  if (d == NULL || s == NULL)
278
    return FAILED;
279
 
280
  if (**d == 0) {
281
    /* no listfile information! */
282
    s->listfile_items = 0;
283
    s->listfile_cmds = NULL;
284
    s->listfile_ints = NULL;
285
    (*d)++;
286
    return SUCCEEDED;
287
  }
288
 
289
  /* we have listfile information */
290
  t = *d;
291
  t++;
292
  s->listfile_items = READ_T;
293
  s->listfile_cmds = malloc(s->listfile_items);
294
  s->listfile_ints = malloc(sizeof(int) * s->listfile_items*2);
295
 
296
  if (s->listfile_cmds == NULL || s->listfile_ints == NULL) {
297
    s->listfile_items = 0;
298
    fprintf(stderr, "LISTFILE_BLOCK_READ: Out of memory error.\n");
299
    return FAILED;
300
  }
301
 
302
  /* read the items */
303
  for (i = 0; i < s->listfile_items; i++) {
304
    s->listfile_cmds[i] = *(t++);
305
    if (s->listfile_cmds[i] == 'k') {
306
      /* new line */
307
      s->listfile_ints[i*2 + 0] = READ_T;
308
      s->listfile_ints[i*2 + 1] = READ_T;
309
    }
310
    else if (s->listfile_cmds[i] == 'f') {
311
      /* file name */
312
      s->listfile_ints[i*2 + 0] = READ_T;
313
    }
314
    else {
315
      s->listfile_items = 0;
316
      fprintf(stderr, "LISTFILE_BLOCK_READ: Unknown command %d.\n", s->listfile_cmds[i]);
317
      return FAILED;
318
    }
319
  }
320
 
321
  *d = t;
322
 
323
  return SUCCEEDED;
324
}

powered by: WebSVN 2.1.0

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