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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [Open8 Tools/] [open8_src/] [open8_link/] [discard.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 "discard.h"
9
#include "files.h"
10
#include "write.h"
11
 
12
 
13
extern struct reference *reference_first, *reference_last;
14
extern struct section *sec_first, *sec_last;
15
extern struct label *labels_first, *labels_last;
16
extern struct stack *stacks_first, *stacks_last;
17
 
18
 
19
int discard_unused_sections(void) {
20
 
21
  struct section *s;
22
  int a = 0, b = -1;
23
 
24
 
25
  /* iterate section discarding until there's no change in the
26
     amount of dropped sections */
27
  while (a != b) {
28
    s = sec_first;
29
    while (s != NULL) {
30
      s->referenced = 0;
31
      s = s->next;
32
    }
33
 
34
    a = b;
35
    discard_iteration();
36
 
37
    b = 0;
38
    s = sec_first;
39
    while (s != NULL) {
40
      if (s->referenced == 0)
41
        s->alive = NO;
42
      else
43
        s->alive = YES;
44
      if (s->alive == NO)
45
        b++;
46
      s = s->next;
47
    }
48
  }
49
 
50
  /* announce all the unreferenced sections that will get dropped */
51
  s = sec_first;
52
  while (s != NULL) {
53
    if (s->alive == NO)
54
      fprintf(stderr, "DISCARD: %s:%s: Section \"%s\" was discarded.\n",
55
              get_file_name(s->file_id), get_source_file_name(s->file_id, s->file_id_source), s->name);
56
    s = s->next;
57
  }
58
 
59
  return SUCCEEDED;
60
}
61
 
62
 
63
int discard_iteration(void) {
64
 
65
  struct reference *r;
66
  struct stackitem *si;
67
  struct section *s, *se;
68
  struct label *l;
69
  struct stack *st;
70
  int g;
71
 
72
 
73
  /* check section names for special characters '!', and
74
     check if the section is of proper type */
75
  s = sec_first;
76
  while (s != NULL) {
77
    if (s->name[0] == '!' || !(s->status == SECTION_STATUS_FREE || s->status == SECTION_STATUS_SEMIFREE)) {
78
      s->referenced++;
79
      s->alive = YES;
80
    }
81
    s = s->next;
82
  }
83
 
84
  /* loop through references */
85
  r = reference_first;
86
  while (r != NULL) {
87
    /* references to local labels don't count */
88
    if (r->name[0] == '_' || is_label_anonymous(r->name) == SUCCEEDED) {
89
      r = r->next;
90
      continue;
91
    }
92
    l = labels_first;
93
    while (l != NULL) {
94
      if (strcmp(l->name, r->name) == 0)
95
        break;
96
      l = l->next;
97
    }
98
    if (l != NULL && l->section_status == ON) {
99
      s = sec_first;
100
      while (s->id != l->section)
101
        s = s->next;
102
      if (s == NULL)
103
        fprintf(stderr, "DISCARD_ITERATION: Internal error!\n");
104
      if (r->section_status == OFF)
105
        s->referenced++;
106
      else if (r->section != s->id) {
107
        se = sec_first;
108
        while (se->id != r->section)
109
          se = se->next;
110
        if (se->alive == YES)
111
          s->referenced++;
112
      }
113
    }
114
    r = r->next;
115
  }
116
 
117
  /* loop through computations */
118
  st = stacks_first;
119
  while (st != NULL) {
120
    si = st->stack;
121
    g = 0;
122
    while (g != st->stacksize) {
123
      if (si->type == STACK_ITEM_TYPE_STRING && is_label_anonymous(si->string) == FAILED) {
124
        l = labels_first;
125
        while (l != NULL) {
126
          if (strcmp(l->name, si->string) == 0 && l->section_status == ON) {
127
            s = sec_first;
128
            while (s->id != l->section)
129
              s = s->next;
130
            if (st->section_status == OFF)
131
              s->referenced++;
132
            else if (st->section != s->id) {
133
              se = sec_first;
134
              while (se->id != st->section)
135
                se = se->next;
136
              if (se->alive == YES)
137
                s->referenced++;
138
            }
139
          }
140
          l = l->next;
141
        }
142
      }
143
      si++;
144
      g++;
145
    }
146
    st = st->next;
147
  }
148
 
149
  return SUCCEEDED;
150
}
151
 
152
 
153
/* drop labels that are inside discarded sections */
154
int discard_drop_labels(void) {
155
 
156
  struct label *l, *t;
157
  struct section *s;
158
 
159
 
160
  l = labels_first;
161
  while (l != NULL) {
162
    if (l->section_status == ON) {
163
      /* find the section */
164
      s = sec_first;
165
      while (s->id != l->section)
166
        s = s->next;
167
      /* is it dead? */
168
      if (s->alive == NO) {
169
        /* nope! remove the label! */
170
        if (l->prev == NULL)
171
          labels_first = l->next;
172
        else
173
          l->prev->next = l->next;
174
        if (l->next != NULL)
175
          l->next->prev = l->prev;
176
        t = l->next;
177
        free(l);
178
        l = t;
179
      }
180
      else
181
        l = l->next;
182
    }
183
    else
184
      l = l->next;
185
  }
186
 
187
  return SUCCEEDED;
188
}

powered by: WebSVN 2.1.0

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