-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbols.h
654 lines (540 loc) · 18.6 KB
/
symbols.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Copyright 2018 LPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// symbol table
#pragma once
#include <assert.h>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include "common.h"
#include "stringTable.h"
#include "compilationContext.h"
#include "types.h"
#include "ast.h"
#include "objects.h"
///////////////////////////////////////////////////////////////////////////////
//
// class Symbol - a type, variable or any other language element refered by an identifier
//
struct Symbol
{
enum Category { scUse, scLabel, scType, scConst, scVariable,
scParameter, scSubroutine, scField, scIntrinsic };
// definition (common)
//
// NOTE: pId == nullptr means annonymous symbol
//
Identifier* pId;
Category category;
// category specific information
//
union
{
void* pDummy;
Symbol* pUseSymbol;
ts::Type* pType;
obj::Constant* pConstant;
obj::Variable* pVariable;
obj::Parameter* pParameter;
obj::Subroutine* pSubroutine;
obj::Field* pField;
obj::IntrinsicId intrinsicId;
obj::Label* pLabel;
};
public:
Symbol(Identifier* pId, obj::Label* pLabel) :
pId(pId), category(scLabel), pLabel(pLabel) {}
Symbol(Identifier* pId, Symbol* pUseSymbol) :
pId(pId), category(scUse), pUseSymbol(pUseSymbol) {}
Symbol(Identifier* pId, ts::Type* pType) :
pId(pId), category(scType), pType(pType) {}
Symbol(Identifier* pId, obj::Constant* pConstant) :
pId(pId), category(scConst), pConstant(pConstant) {}
Symbol(Identifier* pId, obj::Variable* pVariable) :
pId(pId), category(scVariable), pVariable(pVariable) {}
Symbol(Identifier* pId, obj::Parameter* pParameter) :
pId(pId), category(scParameter), pParameter(pParameter) {}
Symbol(Identifier* pId, obj::Subroutine* pSubroutine) :
pId(pId), category(scSubroutine), pSubroutine(pSubroutine) {}
Symbol(Identifier* pId, obj::Field* pField) :
pId(pId), category(scField), pField(pField) {}
Symbol(Identifier* pId, obj::IntrinsicId intrinsicId) :
pId(pId), category(scIntrinsic), intrinsicId(intrinsicId) {}
};
///////////////////////////////////////////////////////////////////////////////
//
typedef std::map<string, Symbol*, StricmpLess> SymMap;
typedef SymMap::iterator SymIter;
///////////////////////////////////////////////////////////////////////////////
//
struct Scope
{
enum Category { scGlobal, scProgram, scSubroutine, scRecord };
static const int GLOBAL_SCOPE_LEVEL = 0;
static const int PROGRAM_SCOPE_LEVEL = 1;
Scope* pParent;
Scope* pNested;
Scope* pNext;
SymMap symMap;
Category category;
int level;
int line;
union
{
void* pDummy;
obj::Subroutine* pSubroutine;
};
public:
// extension point (backend-specific information for example)
//
VarPtr ext;
public:
Scope(Category category, int line) :
category(category), pDummy(nullptr),
pParent(nullptr), pNested(nullptr), pNext(nullptr),
level(-1), line(line)
{
}
// CONSIDER: get rid of this
//
const char* name() const
{
switch(category)
{
case scGlobal: return "<global>";
case scProgram: return "<program>";
case scRecord: return "<record>";
case scSubroutine: return pSubroutine->pId->name.c_str();
default:
assert(!"unknown scope type");
return "<???>";
}
}
obj::Subroutine* subroutine() const
{
assert(category == scProgram || category == scSubroutine);
return pSubroutine;
}
};
///////////////////////////////////////////////////////////////////////////////
//
// class SymbolTable
//
// the symbol table is implemented as a tree of Scope objects each representing a scope
//
class SymbolTable
{
public:
SymbolTable() :
m_pRoot(nullptr),
m_pCurrentScope(nullptr),
m_pProgramScope(nullptr)
{
}
~SymbolTable()
{
#if 0
print();
#endif
}
public:
std::vector<string>& programArgs() { return m_programArgs; }
Scope* currentScope() const
{
return m_pCurrentScope;
}
Scope* globalScope() const
{
assert(m_pRoot->category == Scope::scGlobal);
assert(m_pRoot->level == Scope::GLOBAL_SCOPE_LEVEL);
return m_pRoot;
}
Scope* programScope() const
{
assert(m_pProgramScope->category == Scope::scProgram);
assert(m_pProgramScope->level == Scope::PROGRAM_SCOPE_LEVEL);
return m_pProgramScope;
}
void beginGlobalScope()
{
_beginScope(new Scope(Scope::scGlobal, PREDEFINED_LOCATION));
}
void endGlobalScope()
{
if(m_pCurrentScope->category != Scope::scGlobal)
{
context()->error(-1, "mismatched scope, expecting the end of the global scope");
}
_endScope();
}
void beginProgramScope(obj::Subroutine* pSubroutine)
{
assert(pSubroutine != nullptr);
assert(m_pProgramScope == nullptr);
Scope* pScope = new Scope(Scope::scProgram, pSubroutine->defLine);
m_pProgramScope = pScope;
pScope->pSubroutine = pSubroutine;
pSubroutine->pScope = pScope;
_beginScope(pScope);
}
void endProgramScope([[maybe_unused]] obj::Subroutine* pSubroutine)
{
assert(pSubroutine != nullptr);
assert(m_pCurrentScope->category == Scope::scProgram);
assert(m_pCurrentScope->pSubroutine == pSubroutine);
_endScope();
}
void beginSubroutineScope(obj::Subroutine* pSubroutine)
{
assert(pSubroutine != nullptr);
Scope* pScope = new Scope(Scope::scSubroutine, pSubroutine->defLine);
pScope->pSubroutine = pSubroutine;
pSubroutine->pScope = pScope;
assert(m_pCurrentScope->category == Scope::scProgram ||
m_pCurrentScope->category == Scope::scSubroutine);
_beginScope(pScope);
}
void endSubroutineScope([[maybe_unused]] obj::Subroutine* pSubroutine)
{
assert(pSubroutine != nullptr);
assert(m_pCurrentScope->category == Scope::scSubroutine);
assert(m_pCurrentScope->pSubroutine == pSubroutine);
_endScope();
}
void beginRecordScope(int line)
{
Scope* pScope = new Scope(Scope::scRecord, line);
_beginScope(pScope);
}
void endRecordScope()
{
assert(m_pCurrentScope->category == Scope::scRecord);
_endScope();
}
void addScopeRecord(ast::Expr* pRecord)
{
assert(m_pCurrentScope->category == Scope::scRecord);
if(pRecord->pType->isDummy())
{
return;
}
else if(!pRecord->pType->isRecord())
{
context()->error(pRecord->pType->line(), "'with' statements require valid record types");
return;
}
auto pRecordType = pRecord->pType->as<ts::RecordType>();
// create a symbol object for each record field
//
for(auto it = pRecordType->fieldMap().begin(); it != pRecordType->fieldMap().end(); ++it)
{
auto pNewId = new Identifier(it->first->name.c_str(), m_pCurrentScope->line);
auto pField = new obj::Field(pNewId, pRecord);
addSymbol(new Symbol(pNewId, pField), true);
}
}
// resolve pointer definitions immediately after the
// type declarations
//
void resolveTypes()
{
assert(nullptr != m_pCurrentScope);
Scope* pScope = m_pCurrentScope;
for(SymIter it = pScope->symMap.begin(); it != pScope->symMap.end(); ++it)
{
Symbol* pSymbol = it->second;
if(pSymbol->category == Symbol::scType)
pSymbol->pType->resolve();
}
}
// look up a symbol implicitly marking the lookup as a use
//
Symbol* findSymbol(Identifier* pId, Scope* pStartScope = nullptr)
{
assert(m_pCurrentScope != nullptr);
if(nullptr == pStartScope)
pStartScope = m_pCurrentScope;
for(Scope* pScope = pStartScope; nullptr != pScope; pScope = pScope->pParent)
{
SymIter it = pScope->symMap.find(pId->name);
if(it != pScope->symMap.end()
&& it->second->category != Symbol::scUse)
{
// mark uses of inherited symbols
// (except for "record" scopes)
//
if(pScope != pStartScope &&
pStartScope->category != Scope::scRecord)
{
auto tmpIt = pStartScope->symMap.find(pId->name);
if(pStartScope->symMap.end() == tmpIt)
{
assert(pStartScope == m_pCurrentScope);
// create a new "scUse" symbol
//
pStartScope->symMap[pId->name] =
new Symbol(pId, it->second);
}
else
{
assert(tmpIt->second->category == Symbol::scUse);
}
}
return it->second;
}
}
return nullptr;
}
// look up a local function/procedure declaration
// (does NOT count as activation, just a helper to sort out the forward declarations)
//
obj::Subroutine* findLocalSubroutine(Identifier* pId)
{
SymIter it = m_pCurrentScope->symMap.find(pId->name);
if(it != m_pCurrentScope->symMap.end()
&& it->second->category == Symbol::scSubroutine)
{
return it->second->pSubroutine;
}
return nullptr;
}
// search for an existing type. If the type is not
// defined emit an error and return a dummy type so we
// can continue compilation
//
ts::Type* findType(Identifier* pId)
{
Symbol* pSymbol = findSymbol(pId);
if(nullptr == pSymbol || pSymbol->category != Symbol::scType)
{
context()->error(pId->line, "unknown type '%s'", pId->name.c_str());
// create a dummy placeholder type
//
// CONSIDER: we could insert this dummy type in the
// symbol table to avoid cascading errors
//
return new ts::DummyType(pId->line);
}
return pSymbol->pType;
}
obj::Label* findLabel(Identifier* pId)
{
Symbol* pSymbol = findSymbol(pId);
if(nullptr == pSymbol)
{
context()->error(pId->line, "undefined label '%s'", pId->name.c_str());
return new obj::Label(NO_LOCATION, m_pCurrentScope);
}
assert(pSymbol->category == Symbol::scLabel);
return pSymbol->pLabel;
}
obj::Constant* findConstant(Identifier* pId)
{
Symbol* pSymbol = findSymbol(pId);
if(nullptr == pSymbol || pSymbol->category != Symbol::scConst)
{
context()->error(pId->line, "unknown symbolic constant '%s'", pId->name.c_str());
return new obj::Constant(0, pId->line);
}
return pSymbol->pConstant;
}
void addSymbol(Symbol* pSymbol, bool replace = false)
{
assert(m_pCurrentScope != nullptr);
assert(pSymbol != nullptr);
assert(pSymbol->category != Symbol::scVariable ||
pSymbol->pVariable->pScope == m_pCurrentScope);
assert(pSymbol->category != Symbol::scParameter ||
pSymbol->pParameter->pScope == m_pCurrentScope);
Scope* pScope = m_pCurrentScope;
SymIter it = pScope->symMap.find(pSymbol->pId->name);
if(it == pScope->symMap.end())
{
m_pCurrentScope->symMap[pSymbol->pId->name] = pSymbol;
// if we have an "internal" identifier we're done
//
if(pSymbol->pId->isInternal())
return;
// look for identifiers hidden by this definition
//
for(pScope = pScope->pParent; nullptr != pScope; pScope = pScope->pParent)
{
it = pScope->symMap.find(pSymbol->pId->name);
if(it != pScope->symMap.end()
&& it->second->category != Symbol::scUse
&& it->second->category != Symbol::scLabel)
{
if(it->second->pId->line == PREDEFINED_LOCATION)
{
context()->warning(pSymbol->pId->line,
"redefining predefined identifier '%s'",
it->second->pId->name.c_str());
}
else
{
context()->warning(pSymbol->pId->line,
"'%s' hiding existing declaration on line %d",
pSymbol->pId->name.c_str(), it->second->pId->line);
}
}
}
}
else
{
if(replace)
{
context()->warning(pSymbol->pId->line,
"replacing identifier '%s' defined on line %d",
it->second->pId->name.c_str(), it->second->pId->line);
m_pCurrentScope->symMap[pSymbol->pId->name] = pSymbol;
}
else if(it->second->category == Symbol::scUse)
{
context()->error(pSymbol->pId->line,
"can't redefine '%s' because of the local use on line %d",
pSymbol->pId->name.c_str(), it->second->pId->line);
}
else
{
context()->error(pSymbol->pId->line,
"'%s' already declared on line %d",
pSymbol->pId->name.c_str(), it->second->pId->line);
}
}
}
// returns the index of the corresponding program argument
// (or INVALID_ARG_IDX if there's no match)
//
int bindProgramArg(Identifier* pId)
{
for(size_t i = 0; i < m_programArgs.size(); ++i)
{
if(pId->match(m_programArgs[i].c_str()))
return static_cast<int>(i);
}
return INVALID_ARG_IDX;
}
void print()
{
printf("\nsymbol table:\n");
printf("-------------\n");
if(m_pRoot != nullptr)
{
Scope* pScope = m_pRoot;
int level = 0;
bool explored = false;
while(pScope != nullptr)
{
if (!explored)
{
printf("%*s[%s]\n", level * 4, "", pScope->name());
for(SymIter it = pScope->symMap.begin(); it != pScope->symMap.end(); ++it)
{
printf("%*s %s : %d\n", level * 4, "",
it->first.c_str(),
it->second->pId->line);
}
}
if(pScope->pNested != nullptr && !explored)
{
pScope = pScope->pNested;
++level;
}
else if(pScope->pNext != nullptr)
{
pScope = pScope->pNext;
explored = false;
}
else
{
pScope = pScope->pParent;
explored = true;
--level;
}
}
}
else
printf("<empty>\n");
printf("\n");
}
private:
void _beginScope(Scope* pScope)
{
if(m_pCurrentScope == nullptr)
{
assert(pScope->pParent == nullptr);
assert(nullptr == m_pRoot);
assert(pScope->category == Scope::scGlobal);
pScope->level = Scope::GLOBAL_SCOPE_LEVEL;
m_pRoot = pScope;
}
else
{
assert(nullptr != m_pRoot);
assert(pScope->category != Scope::scGlobal);
assert(pScope->category != Scope::scProgram
|| m_pCurrentScope->category == Scope::scGlobal);
assert(pScope->category != Scope::scRecord
|| m_pCurrentScope->category != Scope::scGlobal);
assert(pScope->category == Scope::scRecord
|| m_pCurrentScope->category != Scope::scRecord);
pScope->pParent = m_pCurrentScope;
pScope->level = m_pCurrentScope->level;
if(pScope->category == Scope::scProgram ||
pScope->category == Scope::scSubroutine)
{
++pScope->level;
}
// add the new scope to the list of parent's nested scopes
//
Scope* pTmp = m_pCurrentScope->pNested;
if(nullptr != pTmp)
{
while(nullptr != pTmp->pNext)
pTmp = pTmp->pNext;
pTmp->pNext = pScope;
}
else
m_pCurrentScope->pNested = pScope;
}
m_pCurrentScope = pScope;
}
void _endScope()
{
assert(m_pRoot != nullptr);
assert(m_pCurrentScope->pParent != nullptr || m_pCurrentScope == m_pRoot);
m_pCurrentScope = m_pCurrentScope->pParent;
}
private:
Scope* m_pRoot;
Scope* m_pProgramScope;
Scope* m_pCurrentScope;
// the list of program arguments
//
std::vector<string> m_programArgs;
};
///////////////////////////////////////////////////////////////////////////////
//
void declareProgramArgs(IdList* pNameList, int line);
void declareVars(IdList* pNameList, ts::Type* pType);
void declareType(Identifier* pId, ts::Type* pType);
void declareConst(Identifier* pId, obj::Constant* pConst);
void declareLabel(obj::Constant* pLabelConst, int line);
obj::Subroutine* declareSubroutine(Identifier* pId, ts::ParamSetList* pParamList,
ts::Type* pReturnType, bool isFunction);
obj::Subroutine* declareProgram(Identifier* pId);
void declareParams(obj::Subroutine* pSubroutine);
ast::Expr* lookupObject(Identifier* pId, bool autoFuncCall = false, Scope* pScope = nullptr);
obj::Constant* lookupConstant(Identifier* pId, bool negate);