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

Subversion Repositories c16

[/] [c16/] [trunk/] [compiler/] [Name.cc] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jsauermann
// Name.cc
2
 
3
#include <stdio.h>
4
#include "Name.hh"
5
#include "Node.hh"
6
 
7
Name * Name::externs  = 0;
8
Name * Name::statics  = 0;
9
Name * Name::enums    = 0;
10
Name * Name::locals   = 0;
11
Name * Name::autos    = 0;
12
 
13
TypedefName * TypedefName::typedefs = 0;
14
StructName  * StructName::structs = 0;
15
 
16
const bool DEBUG = false;
17
 
18
//-----------------------------------------------------------------------------
19
TypeName * TypedefName::Find(const char * na)
20
{
21
   for (TypedefName * n = typedefs; n; n = n->tail)
22
       {
23
          if (!strcmp(na, n->name))   return n->decl;
24
       }
25
   return 0;
26
}
27
//-----------------------------------------------------------------------------
28
bool TypedefName::IsDefined(const char * na)
29
{
30
   for (TypedefName * n = typedefs; n; n = n->tail)
31
       {
32
          if (!strcmp(na, n->name))   return true;
33
       }
34
   return false;
35
}
36
//-----------------------------------------------------------------------------
37
void TypedefName::Add(const char * na, TypeName * decl)
38
{
39
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to typedefs\n", na);
40
   typedefs = new TypedefName(na, typedefs, decl);
41
}
42
//=============================================================================
43
StructDeclarationList * StructName::Find(const char * na)
44
{
45
   for (StructName * n = structs; n; n = n->tail)
46
       {
47
          if (!strcmp(na, n->name))   return n->sdlist;
48
       }
49
   return 0;
50
}
51
//-----------------------------------------------------------------------------
52
bool StructName::IsDefined(const char * na)
53
{
54
   for (StructName * n = structs; n; n = n->tail)
55
       {
56
          if (!strcmp(na, n->name))   return true;
57
       }
58
   return false;
59
}
60
//-----------------------------------------------------------------------------
61
void StructName::Add(const char * na, StructDeclarationList * sdl)
62
{
63
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to structs\n", na);
64
   structs = new StructName(na, structs, sdl);
65
}
66
//=============================================================================
67 7 jsauermann
void Name::PushContext()
68
{
69
   // use "}" as a marker for contexts
70
   autos = new Name("}", autos, 0, 0);
71
}
72
//-----------------------------------------------------------------------------
73
void Name::PopContext()
74
{
75
   while (autos)
76
       {
77
          Name * tl = autos->tail;
78
          const bool marker = !strcmp("}", autos->name);
79
          delete autos;
80
          autos = tl;
81
          if (marker)   return;
82
       }
83
 
84
   assert(0 && "No context marker");
85
}
86
//-----------------------------------------------------------------------------
87 2 jsauermann
void Name::RemoveAuto()
88
{
89
   while (autos)
90
       {
91
          Name * tl = autos->tail;
92
          delete autos;
93
          autos = tl;
94
       }
95
}
96
//-----------------------------------------------------------------------------
97
void Name::AutoToLocal()
98
{
99
   while (autos)
100
      {
101
        Name * n = autos;
102
        autos = autos->tail;
103
        AutoToLocal();
104
        n->tail = locals;
105
        locals = n;
106
      }
107
}
108
//-----------------------------------------------------------------------------
109
TypeName * Name::FindType(const char * na)
110
{
111
Name * np = FindDeclared(na);
112
 
113
   if (np == 0)   return 0;
114
 
115
   return np->decl;
116
}
117
//-----------------------------------------------------------------------------
118
int Name::FindPos(const char * na)
119
{
120
Name * np = FindDeclared(na);
121
 
122
   if (np == 0)   return 1;   // +1 indicates error !
123
 
124
   return np->stack_position;
125
}
126
//-----------------------------------------------------------------------------
127
bool Name::FindEnum(const char * na, int & value)
128
{
129
   for (Name * n = enums; n; n = n->tail)
130
       {
131
          if (strcmp(na, n->name))   continue;
132
          value = n->stack_position;
133
          return true;
134
       }
135
 
136
   return false;
137
}
138
//-----------------------------------------------------------------------------
139
Name * Name::FindDeclared(const char * na)
140
{
141
   for (Name * n = autos; n; n = n->tail)
142
       {
143
          if (!strcmp(na, n->name))   return n;
144
       }
145
 
146
   for (Name * n = locals; n; n = n->tail)
147
       {
148
          if (!strcmp(na, n->name))   return n;
149
       }
150
 
151
   for (Name * n = statics; n; n = n->tail)
152
       {
153
          if (!strcmp(na, n->name))   return n;
154
       }
155
 
156
   for (Name * n = externs; n; n = n->tail)
157
       {
158
          if (!strcmp(na, n->name))   return n;
159
       }
160
 
161
   return 0;
162
}
163
//-----------------------------------------------------------------------------
164
void Name::Print(FILE * out)
165
{
166
   fprintf(out, "'%s' %d\n", name, stack_position);
167
}
168
//-----------------------------------------------------------------------------
169
void Name::PrintAll(FILE * out)
170
{
171
   fprintf(out, "Auto:\n");
172
   for (Name * n = autos; n; n = n->tail)     n->Print(out);
173
 
174
   fprintf(out, "Local:\n");
175
   for (Name * n = locals; n; n = n->tail)    n->Print(out);
176
 
177
   fprintf(out, "Static:\n");
178
   for (Name * n = statics; n; n = n->tail)   n->Print(out);
179
 
180
   fprintf(out, "Extern:\n");
181
   for (Name * n = externs; n; n = n->tail)   n->Print(out);
182
}
183
//-----------------------------------------------------------------------------
184
void Name::AddExtern(const char * na, TypeName * decl)
185
{
186
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to externs\n", na);
187
   externs = new Name(na, externs, decl, 0);
188
}
189
//-----------------------------------------------------------------------------
190
void Name::AddStatic(const char * na, TypeName * decl)
191
{
192
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to statics\n", na);
193
   statics = new Name(na, statics, decl, 0);
194
}
195
//-----------------------------------------------------------------------------
196
void Name::AddLocal(const char * na, TypeName * decl)
197
{
198
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to locals\n", na);
199
   locals = new Name(na, locals, decl, 0);
200
}
201
//-----------------------------------------------------------------------------
202
void Name::AddEnum(const char * na, int spos)
203
{
204
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to enums\n", na);
205
   enums = new Name(na, enums, 0, spos);
206
}
207
//-----------------------------------------------------------------------------
208
void Name::AddAuto(const char * na, TypeName * decl, int spos)
209
{
210
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to autos\n", na);
211
   autos = new Name(na, autos, decl, spos);
212
}
213
//-----------------------------------------------------------------------------
214
void Name::SetAutoPos(const char * na, int spos)
215
{
216
   for (Name * n = autos; n; n = n->tail)
217
       {
218
          if (!strcmp(na, n->name))
219
             {
220
 
221
               if (DEBUG)   fprintf(stderr,
222
                                    "Setting Stack Position of \"%s\" to %d\n",
223
                                    na, spos);
224
               assert(n->stack_position == 0);
225
               assert(spos != 0);
226
               assert(spos != 1);
227
               n->stack_position = spos;
228
               return;
229
             }
230
       }
231
   assert(0);
232
}
233
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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