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

Subversion Repositories c16

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

Go to most recent revision | 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
void Name::RemoveAuto()
68
{
69
   while (autos)
70
       {
71
          Name * tl = autos->tail;
72
          delete autos;
73
          autos = tl;
74
       }
75
}
76
//-----------------------------------------------------------------------------
77
void Name::AutoToLocal()
78
{
79
   while (autos)
80
      {
81
        Name * n = autos;
82
        autos = autos->tail;
83
        AutoToLocal();
84
        n->tail = locals;
85
        locals = n;
86
      }
87
}
88
//-----------------------------------------------------------------------------
89
TypeName * Name::FindType(const char * na)
90
{
91
Name * np = FindDeclared(na);
92
 
93
   if (np == 0)   return 0;
94
 
95
   return np->decl;
96
}
97
//-----------------------------------------------------------------------------
98
int Name::FindPos(const char * na)
99
{
100
Name * np = FindDeclared(na);
101
 
102
   if (np == 0)   return 1;   // +1 indicates error !
103
 
104
   return np->stack_position;
105
}
106
//-----------------------------------------------------------------------------
107
bool Name::FindEnum(const char * na, int & value)
108
{
109
   for (Name * n = enums; n; n = n->tail)
110
       {
111
          if (strcmp(na, n->name))   continue;
112
          value = n->stack_position;
113
          return true;
114
       }
115
 
116
   return false;
117
}
118
//-----------------------------------------------------------------------------
119
Name * Name::FindDeclared(const char * na)
120
{
121
   for (Name * n = autos; n; n = n->tail)
122
       {
123
          if (!strcmp(na, n->name))   return n;
124
       }
125
 
126
   for (Name * n = locals; n; n = n->tail)
127
       {
128
          if (!strcmp(na, n->name))   return n;
129
       }
130
 
131
   for (Name * n = statics; n; n = n->tail)
132
       {
133
          if (!strcmp(na, n->name))   return n;
134
       }
135
 
136
   for (Name * n = externs; n; n = n->tail)
137
       {
138
          if (!strcmp(na, n->name))   return n;
139
       }
140
 
141
   return 0;
142
}
143
//-----------------------------------------------------------------------------
144
void Name::Print(FILE * out)
145
{
146
   fprintf(out, "'%s' %d\n", name, stack_position);
147
}
148
//-----------------------------------------------------------------------------
149
void Name::PrintAll(FILE * out)
150
{
151
   fprintf(out, "Auto:\n");
152
   for (Name * n = autos; n; n = n->tail)     n->Print(out);
153
 
154
   fprintf(out, "Local:\n");
155
   for (Name * n = locals; n; n = n->tail)    n->Print(out);
156
 
157
   fprintf(out, "Static:\n");
158
   for (Name * n = statics; n; n = n->tail)   n->Print(out);
159
 
160
   fprintf(out, "Extern:\n");
161
   for (Name * n = externs; n; n = n->tail)   n->Print(out);
162
}
163
//-----------------------------------------------------------------------------
164
void Name::AddExtern(const char * na, TypeName * decl)
165
{
166
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to externs\n", na);
167
   externs = new Name(na, externs, decl, 0);
168
}
169
//-----------------------------------------------------------------------------
170
void Name::AddStatic(const char * na, TypeName * decl)
171
{
172
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to statics\n", na);
173
   statics = new Name(na, statics, decl, 0);
174
}
175
//-----------------------------------------------------------------------------
176
void Name::AddLocal(const char * na, TypeName * decl)
177
{
178
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to locals\n", na);
179
   locals = new Name(na, locals, decl, 0);
180
}
181
//-----------------------------------------------------------------------------
182
void Name::AddEnum(const char * na, int spos)
183
{
184
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to enums\n", na);
185
   enums = new Name(na, enums, 0, spos);
186
}
187
//-----------------------------------------------------------------------------
188
void Name::AddAuto(const char * na, TypeName * decl, int spos)
189
{
190
   if (DEBUG)   fprintf(stderr, "Adding \"%s\" to autos\n", na);
191
   autos = new Name(na, autos, decl, spos);
192
}
193
//-----------------------------------------------------------------------------
194
void Name::SetAutoPos(const char * na, int spos)
195
{
196
   for (Name * n = autos; n; n = n->tail)
197
       {
198
          if (!strcmp(na, n->name))
199
             {
200
 
201
               if (DEBUG)   fprintf(stderr,
202
                                    "Setting Stack Position of \"%s\" to %d\n",
203
                                    na, spos);
204
               assert(n->stack_position == 0);
205
               assert(spos != 0);
206
               assert(spos != 1);
207
               n->stack_position = spos;
208
               return;
209
             }
210
       }
211
   assert(0);
212
}
213
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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