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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [Table.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2012-2018  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// CC64 - 'C' derived language compiler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "stdafx.h"
27
 
28
extern char *prefix;
29
SYM *search2(char *na,TABLE *tbl,TypeArray *typearray);
30
uint8_t hashadd(char *nm);
31
 
32
#ifndef min
33
int min (int a, int b) { return a < b ? a : b; }
34
#endif
35
 
36
SYM *TABLE::match[100];
37
int TABLE::matchno;
38
 
39
TABLE::TABLE()
40
{
41
  base = 0;
42
  head = 0;
43
  tail = 0;
44
  owner = 0;
45
}
46
 
47
// Used when deriving classes from base clasases.
48
 
49
void TABLE::CopySymbolTable(TABLE *dst, TABLE *src)
50
{
51
        SYM *sp, *newsym;
52
        dfs.puts("<CopySymbolTable>\n");
53
        if (src) {
54
          dfs.printf("A");
55
                sp = sp->GetPtr(src->GetHead());
56
                while (sp) {
57
          dfs.printf("B");
58
                        newsym = SYM::Copy(sp);
59
          dfs.printf("C");
60
                        dst->insert(newsym);
61
          dfs.printf("D");
62
                        sp = sp->GetNextPtr();
63
                }
64
        }
65
        dfs.puts("</CopySymbolTable>\n");
66
}
67
 
68
//Generic table insert routine, used for all inserts.
69
 
70
void TABLE::insert(SYM *sp)
71
{
72
        int nn;
73
        TypeArray *ta = sp->fi->GetProtoTypes();
74
        TABLE *tab = this;
75
        int s1,s2,s3;
76
        std::string nm;
77
//  std::string sig;
78
 
79
        if (sp == nullptr || this == nullptr ) {
80
          dfs.printf("Null pointer at insert\n");
81
                throw new C64PException(ERR_NULLPOINTER,1);
82
  }
83
 
84
  if (this==&tagtable) {
85
    dfs.printf("Insert into tagtable:%s|\n",(char *)sp->name->c_str());
86
  }
87
  else
88
    dfs.printf("Insert %s into %p", (char *)sp->name->c_str(), (char *)this);
89
    dfs.printf("(%s)\n",owner ? (char *)SYM::GetPtr(owner)->name->c_str(): (char *)"");
90
//  sig = sp->BuildSignature();
91
        if (tab==&gsyms[0]) {
92
          dfs.printf("Insert into global table\n");
93
                s1 = hashadd((char *)sp->name->c_str());
94
                s2 = hashadd((char *)sp->name2->c_str());
95
                s3 = hashadd((char *)sp->name3->c_str());
96
//              tab = &gsyms[(s1&s2)|(s1&s3)|(s2&s3)];
97
                tab = &gsyms[s1];
98
        }
99
 
100
  nm = *sp->name;
101
  // The symbol may not have a type if it's just a label. Find doens't
102
  // look at the return type parameter anyway, so we just set it to bt_long
103
  // if tp isn't set.
104
  nn = tab->Find(nm,sp->tp ? sp->tp->typeno : bt_long,ta,true);
105
        if(nn == 0) {
106
    if( tab->head == 0) {
107
      tab->SetHead(sp->GetIndex());
108
                        tab->SetTail(sp->GetIndex());
109
                }
110
    else {
111
      sp->GetPtr(tab->tail)->next = sp->GetIndex();
112
      tab->SetTail(sp->GetIndex());
113
    }
114
    sp->SetNext(0);
115
    dfs.printf("At insert:\n");
116
    sp->fi->GetProtoTypes()->Print();
117
  }
118
  else
119
    error(ERR_DUPSYM);
120
        if (ta)
121
                delete ta;
122
//  p = tab->GetHead();
123
//  while(p) {
124
//    printf("Xele:%p|%s|\r\n", p, p->name.c_str());
125
//    p = p->GetNext();
126
//  }
127
}
128
 
129
// Parameters:
130
//  na:                 the name to search for
131
//      rettype:        this parameter is ignored
132
 
133
int TABLE::Find(std::string na,__int16 rettype, TypeArray *typearray, bool exact)
134
{
135
        SYM *thead, *first;
136
        TypeArray *ta;
137
        int s1,s2,s3;
138
        std::string name;
139
 
140
  dfs.puts("<Find>");
141
  dfs.puts((char *)na.c_str());
142
  if (this==nullptr) {
143
    matchno = 0;
144
    return 0;
145
  }
146
        if (na.length()==0) {
147
          dfs.printf("name is empty string\n");
148
                throw new C64PException(ERR_NULLPOINTER,1);
149
  }
150
 
151
        matchno = 0;
152
        if (this==&gsyms[0])
153
                thead = SYM::GetPtr(gsyms[hashadd((char *)na.c_str())].GetHead());
154
        else
155
                thead = SYM::GetPtr(head);
156
        first = thead;
157
//      while(thead != NULL) {
158
//        lfs.printf("Ele:%s|\n", (char *)thead->name->c_str());
159
//        thead = thead->GetNext();
160
// }
161
 thead = first;
162
        while( thead != NULL) {
163
//              dfs.printf((char *)"|%s|,|%s|\n",(char *)thead->name->c_str(),(char *)na.c_str());
164
                if (thead->name) {      // ???
165
    name = *thead->name;
166
                s1 = thead->name->compare(na);
167
                s2 = thead->name2->compare(na);
168
                s3 = thead->name3->compare(na);
169
//              dfs.printf("s1:%d ",s1);
170
//              dfs.printf("s2:%d ",s2);
171
//              dfs.printf("s3:%d\n",s3);
172
                if(((s1&s2)|(s1&s3)|(s2&s3))==0) {
173
                  dfs.printf("Match\n");
174
                        match[matchno] = thead;
175
                        matchno++;
176
                        if (matchno > 98)
177
                                break;
178
 
179
                        if (exact) {
180
                                ta = thead->fi->GetProtoTypes();
181
                                if (ta->IsEqual(typearray)) {
182
                                  dfs.printf("Exact match");
183
                                  ta->Print();
184
                                  typearray->Print();
185
                                  delete ta;
186
                                  return 1;
187
                                }
188
                                ta->Print();
189
                                delete ta;
190
                        }
191
                }
192
                }
193
    thead = thead->GetNextPtr();
194
    if (thead==first) {
195
      dfs.printf("Circular list.\n");
196
      throw new C64PException(ERR_CIRCULAR_LIST,1);
197
    }
198
  }
199
  dfs.puts("</Find>\n");
200
  return exact ? 0 : matchno;
201
}
202
 
203
int TABLE::Find(std::string na)
204
{
205
        return Find(na,(__int16)bt_long,nullptr,false);
206
}
207
 
208
 
209
// Findrising searchs the table hierarchy at higher and higher
210
// levels until the symbol is found. It returns a table full of matches
211
// which is ordered according to the level at which the symbol was found.
212
// Subclasses appear in the table before base classes, so that a 
213
// subclass definiton of the symbol shadows a base class definition.
214
//
215
int TABLE::FindRising(std::string na)
216
{
217
        int sp;
218
  SYM *sym;
219
  int bse;
220
  static SYM *mt[110];
221
  int nn, ii;
222
  int ndx;
223
  TypeArray *ta;
224
 
225
  ndx = 0;
226
  dfs.printf("<FindRising>%s \n",(char *)na.c_str());
227
  if (this==nullptr)
228
    return 0;
229
        sp = Find(na);
230
        nn = min(100,ndx+TABLE::matchno);
231
        memcpy(&mt[0],TABLE::match,nn*sizeof(SYM *));
232
        ndx += nn;
233
        bse = base;
234
        while (bse) {
235
          sym = SYM::GetPtr(bse);
236
          dfs.printf("Searching class:%s \n",(char *)sym->name->c_str());
237
                sp = sym->tp->lst.Find(na);
238
        nn = min(100,ndx+TABLE::matchno);
239
        memcpy(&mt[ndx],TABLE::match,nn*sizeof(SYM *));
240
        ndx += nn;
241
                bse = sym->tp->lst.base;
242
        }
243
        dfs.puts("</FindRising>");
244
 
245
  memcpy(TABLE::match,mt,ndx*sizeof(SYM *));
246
  TABLE::matchno = ndx;
247
  for (nn = 0; nn < ndx; nn++) {
248
    sym = TABLE::match[nn];
249
        if (sym) {
250
                if (sym->tp->type == bt_func || sym->tp->type == bt_ifunc) {
251
                        if (sym->name)
252
                                dfs.printf("Sym:%s Types: (", (char *)sym->name->c_str());
253
                        else
254
                                dfs.printf("Sym:%s Types: (", (char *)"<no name>");
255
                        ta = sym->fi->GetProtoTypes();
256
                        if (ta) {
257
                                for (ii = 0; ii < 20; ii++) {
258
                                        dfs.printf("%03d, ", ta->types[ii]);
259
                                }
260
                        }
261
                        dfs.puts(")\n");
262
                }
263
    }
264
  }
265
  return (ndx);
266
}
267
 
268
SYM *TABLE::Find(std::string na, bool opt)
269
{
270
        Find(na,(__int16)bt_long,nullptr,false);
271
        if (matchno==0)
272
                return nullptr;
273
        return match[matchno-1];
274
}
275
 
276
 

powered by: WebSVN 2.1.0

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