-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathSymbol_IR.cpp
697 lines (610 loc) · 18.3 KB
/
Symbol_IR.cpp
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*******************************************************************************
* Copyright IBM Corp. and others 2015
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "ddr/ir/Symbol_IR.hpp"
#include <assert.h>
#include <algorithm>
#include <map>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include "ddr/std/unordered_map.hpp"
#include <vector>
#include "ddr/ir/ClassUDT.hpp"
#include "ddr/ir/EnumUDT.hpp"
#include "ddr/ir/TextFile.hpp"
#include "ddr/ir/TypedefUDT.hpp"
#include "ddr/ir/UnionUDT.hpp"
#undef DEBUG_PRINT_TYPES
#if defined(DEBUG_PRINT_TYPES)
#include "ddr/ir/TypePrinter.hpp"
#endif
using std::map;
using std::stack;
Symbol_IR::OverrideInfo::OverrideInfo()
: opaqueTypeNames()
, fieldOverrides()
{
static const char * const scalarTypes[] = {
"char",
"I8", "I_8", "U8", "U_8",
"I16", "I_16", "U16", "U_16",
"I32", "I_32", "U32", "U_32",
"I64", "I_64", "U64", "U_64",
"I128", "I_128", "U128", "U_128",
"IDATA", "intptr_t", "UDATA", "uintptr_t",
NULL
};
for (const char * const *cursor = scalarTypes; NULL != *cursor; ++cursor) {
opaqueTypeNames.insert(*cursor);
}
}
Symbol_IR::~Symbol_IR()
{
for (std::vector<Type *>::const_iterator it = _types.begin(); it != _types.end(); ++it) {
delete *it;
}
_types.clear();
}
DDR_RC
Symbol_IR::applyOverridesList(const char *overridesListFile)
{
DDR_RC rc = DDR_RC_OK;
vector<string> filenames;
OverrideInfo overrideInfo;
TextFile listFile(_portLibrary);
/* Read list of type override files from specified file. */
if (!listFile.openRead(overridesListFile)) {
ERRMSG("Failure attempting to open %s\nExiting...\n", overridesListFile);
rc = DDR_RC_ERROR;
} else {
string line;
/* Read each line as a file name. */
while (listFile.readLine(line)) {
size_t commentPosition = line.find("#");
if (string::npos != commentPosition) {
line.erase(commentPosition, line.length());
}
line.erase(line.find_last_not_of(" \t") + 1, line.length());
if (!line.empty()) {
filenames.push_back(line);
}
}
listFile.close();
}
for (vector<string>::const_iterator it = filenames.begin(); filenames.end() != it; ++it) {
rc = readOverridesFile(it->c_str(), &overrideInfo);
if (DDR_RC_OK != rc) {
break;
}
}
if (DDR_RC_OK == rc) {
/* mark opaque types */
for (vector<Type *>::const_iterator it = _types.begin(); it != _types.end(); ++it) {
Type *type = *it;
if (overrideInfo.opaqueTypeNames.find(type->_name) != overrideInfo.opaqueTypeNames.end()) {
type->_opaque = true;
}
}
/* Create a map of type name to vector of types to check all types
* by name for type overrides.
*/
map<string, vector<Type *> > typeNames;
for (vector<Type *>::const_iterator it = _types.begin(); it != _types.end(); ++it) {
Type *type = *it;
typeNames[type->_name].push_back(type);
}
/* Apply the type overrides. */
for (vector<FieldOverride>::const_iterator it = overrideInfo.fieldOverrides.begin(); it != overrideInfo.fieldOverrides.end(); ++it) {
const FieldOverride &type = *it;
/* Check if the structure to override exists. */
map<string, vector<Type *> >::const_iterator typeEntry = typeNames.find(type.structName);
if (typeNames.end() != typeEntry) {
Type *replacementType = NULL;
if (type.isTypeOverride) {
/* If the type for the override exists in the IR, use it. Otherwise, create it. */
map<string, vector<Type *> >::const_iterator overEntry = typeNames.find(type.overrideName);
if (typeNames.end() != overEntry) {
replacementType = overEntry->second.front();
} else {
replacementType = new Type(0);
replacementType->_name = type.overrideName;
typeNames[replacementType->_name].push_back(replacementType);
}
}
/* Iterate over the types with a matching name for the override. */
const vector<Type *> &typesWithName = typeEntry->second;
for (vector<Type *>::const_iterator it2 = typesWithName.begin(); it2 != typesWithName.end(); ++it2) {
(*it2)->renameFieldsAndMacros(type, replacementType);
}
}
}
}
return rc;
}
static bool
startsWith(const string &str, const string &prefix)
{
return 0 == str.compare(0, prefix.length(), prefix);
}
DDR_RC
Symbol_IR::readOverridesFile(const char *overridesFile, OverrideInfo *overrideInfo)
{
DDR_RC rc = DDR_RC_OK;
TextFile file(_portLibrary);
/* Read list of type overrides from specified file. */
if (!file.openRead(overridesFile)) {
ERRMSG("Failure attempting to open %s\nExiting...\n", overridesFile);
rc = DDR_RC_ERROR;
} else {
const string blobprefix = "ddrblob.";
const string opaquetype = "opaquetype=";
const string fieldoverride = "fieldoverride.";
const string typeoverride = "typeoverride.";
string line;
/*
* Read each line, expecting one of the following formats:
* opaquetype={TypeName}
* fieldoverride.{StructureName}.{fieldName}={newName}
* typeoverride.{StructureName}.{fieldName}={newType}"
*/
while (file.readLine(line)) {
/* Remove comment portion of the line, if present. */
size_t commentPosition = line.find("#");
if (string::npos != commentPosition) {
line.erase(commentPosition, line.length());
}
line.erase(line.find_last_not_of(" \t") + 1, line.length());
/* Ignore the prefix "ddrblob.", if present. */
if (startsWith(line, blobprefix)) {
line.erase(0, blobprefix.length());
}
/* Check for opaque types. */
if (startsWith(line, opaquetype)) {
line.erase(0, opaquetype.length());
if (!line.empty()) {
overrideInfo->opaqueTypeNames.insert(line);
}
continue;
}
/* Check if the override is a typeoverride or a fieldoverride. */
bool isTypeOverride = false;
if (startsWith(line, fieldoverride)) {
line.erase(0, fieldoverride.length());
} else if (startsWith(line, typeoverride)) {
isTypeOverride = true;
line.erase(0, typeoverride.length());
} else {
continue;
}
/* Correctly formatted lines must have exactly 1 dot and 1 equals in that order. */
size_t dotPosition = line.find('.');
if ((string::npos == dotPosition) || (string::npos != line.find('.', dotPosition + 1))) {
/* not exactly one '.' */
continue;
}
size_t equalsPosition = line.find('=', dotPosition);
if ((string::npos == equalsPosition) || (string::npos != line.find('=', equalsPosition + 1))) {
/* not exactly one '=' after the '.' */
continue;
}
/* Get the structure name, field name, and override name. Add to list to process. */
FieldOverride override = {
line.substr(0, dotPosition),
line.substr(dotPosition + 1, equalsPosition - dotPosition - 1),
line.substr(equalsPosition + 1),
isTypeOverride
};
overrideInfo->fieldOverrides.push_back(override);
}
file.close();
}
return rc;
}
void
Symbol_IR::removeDuplicates()
{
for (vector<Type *>::iterator it = _types.begin(); it != _types.end();) {
if ((*it)->insertUnique(this)) {
++it;
} else {
it = _types.erase(it);
}
}
}
class MergeVisitor : public TypeVisitor
{
private:
Symbol_IR * const _ir;
Type * const _other;
vector<Type *> *const _merged;
public:
MergeVisitor(Symbol_IR *ir, Type *other, vector<Type *> *merged)
: _ir(ir), _other(other), _merged(merged)
{
}
virtual DDR_RC visitType(Type *type) const;
virtual DDR_RC visitClass(ClassUDT *type) const;
virtual DDR_RC visitEnum(EnumUDT *type) const;
virtual DDR_RC visitNamespace(NamespaceUDT *type) const;
virtual DDR_RC visitTypedef(TypedefUDT *type) const;
virtual DDR_RC visitUnion(UnionUDT *type) const;
private:
DDR_RC visitComposite(ClassType *type) const;
};
DDR_RC
MergeVisitor::visitType(Type *type) const
{
/* No merging necessary for base types. */
return DDR_RC_OK;
}
DDR_RC
MergeVisitor::visitNamespace(NamespaceUDT *type) const
{
/* Merge by adding the fields/subtypes of '_other' into 'type'. */
NamespaceUDT *other = (NamespaceUDT *)_other;
/* This type may be derived from only a forward declaration; if so, update the size now. */
if (0 == type->_sizeOf) {
type->_sizeOf = other->_sizeOf;
}
_ir->mergeEnums(&type->_enumMembers, &other->_enumMembers);
_ir->mergeTypes(type->getSubUDTS(), other->getSubUDTS(), type, _merged);
return DDR_RC_OK;
}
DDR_RC
MergeVisitor::visitEnum(EnumUDT *type) const
{
/* Merge by adding the fields/subtypes of '_other' into 'type'. */
EnumUDT *other = (EnumUDT *)_other;
/* This type may be derived from only a forward declaration; if so, update the size now. */
if (0 == type->_sizeOf) {
type->_sizeOf = other->_sizeOf;
}
_ir->mergeEnums(&type->_enumMembers, &other->_enumMembers);
return DDR_RC_OK;
}
DDR_RC
MergeVisitor::visitTypedef(TypedefUDT *type) const
{
/* No merging necessary for typedefs. */
return DDR_RC_OK;
}
DDR_RC
MergeVisitor::visitComposite(ClassType *type) const
{
/* Merge by adding the fields/subtypes of '_other' into 'type'. */
visitNamespace(type);
ClassType *other = (ClassType *)_other;
_ir->mergeFields(&type->_fieldMembers, &other->_fieldMembers, type, _merged);
return DDR_RC_OK;
}
DDR_RC
MergeVisitor::visitClass(ClassUDT *type) const
{
if (NULL == type->_superClass) {
ClassUDT *otherSuper = ((ClassUDT *)_other)->_superClass;
if (NULL != otherSuper) {
Type *thisSuper = _ir->findTypeInMap(otherSuper);
if (NULL != thisSuper) {
otherSuper = (ClassUDT *)thisSuper;
} else {
_merged->push_back(otherSuper);
}
type->_superClass = otherSuper;
}
}
return visitComposite(type);
}
DDR_RC
MergeVisitor::visitUnion(UnionUDT *type) const
{
return visitComposite(type);
}
template<typename T> void
Symbol_IR::mergeTypes(vector<T *> *source, vector<T *> *other,
NamespaceUDT *outerNamespace, vector<Type *> *merged)
{
if ((NULL != source) && (NULL != other)) {
for (typename vector<T *>::iterator it = other->begin(); it != other->end();) {
Type *otherType = (Type *)*it;
Type *sourceType = findTypeInMap(otherType);
/* Types in the other list not in the source list are added. */
/* Types in both lists are merged. */
if (NULL != sourceType) {
sourceType->acceptVisitor(MergeVisitor(this, otherType, merged));
++it;
} else {
it = other->erase(it);
source->push_back((T *)otherType);
if (NULL != outerNamespace) {
((UDT *)otherType)->_outerNamespace = outerNamespace;
}
addTypeToMap(otherType);
merged->push_back(otherType);
}
}
}
}
void
Symbol_IR::mergeFields(vector<Field *> *source, vector<Field *> *other, Type *type, vector<Type *> *merged)
{
/* Create a map of all fields in the source list. */
set<string> fieldNames;
for (vector<Field *>::const_iterator it = source->begin(); it != source->end(); ++it) {
fieldNames.insert((*it)->_name);
}
bool fieldsMerged = false;
for (vector<Field *>::iterator it = other->begin(); it != other->end();) {
/* Fields in the other list not in the source list are added. */
Field * field = *it;
const string &fieldName = field->_name;
if (fieldNames.end() == fieldNames.find(fieldName)) {
it = other->erase(it);
source->push_back(field);
fieldNames.insert(fieldName);
fieldsMerged = true;
} else {
++it;
}
}
if (fieldsMerged) {
/* We must revisit 'type' to fix the type of the newly added fields. */
merged->push_back(type);
}
}
void
Symbol_IR::mergeEnums(vector<EnumMember *> *source, vector<EnumMember *> *other)
{
/* Create a map of all fields in the source list. */
set<string> enumNames;
for (vector<EnumMember *>::const_iterator it = source->begin(); it != source->end(); ++it) {
enumNames.insert((*it)->_name);
}
for (vector<EnumMember *>::iterator it = other->begin(); it != other->end();) {
EnumMember *member = *it;
const string & memberName = (*it)->_name;
/* Fields in the other list not in the source list are added. */
if (enumNames.end() == enumNames.find(memberName)) {
it = other->erase(it);
source->push_back(member);
enumNames.insert(memberName);
} else {
++it;
}
}
}
class TypeReplaceVisitor : public TypeVisitor
{
private:
Symbol_IR * const _ir;
public:
explicit TypeReplaceVisitor(Symbol_IR *ir)
: _ir(ir)
{
}
virtual DDR_RC visitType(Type *type) const;
virtual DDR_RC visitClass(ClassUDT *type) const;
virtual DDR_RC visitEnum(EnumUDT *type) const;
virtual DDR_RC visitNamespace(NamespaceUDT *type) const;
virtual DDR_RC visitTypedef(TypedefUDT *type) const;
virtual DDR_RC visitUnion(UnionUDT *type) const;
private:
DDR_RC visitComposite(ClassType *type) const;
};
class TypeCheck
{
private:
const Type * const _other;
public:
explicit TypeCheck(const Type *other)
: _other(other)
{
}
bool operator()(Type *compare) const {
return *compare == *_other;
}
};
DDR_RC
TypeReplaceVisitor::visitType(Type *type) const
{
return DDR_RC_OK;
}
DDR_RC
TypeReplaceVisitor::visitNamespace(NamespaceUDT *type) const
{
DDR_RC rc = DDR_RC_OK;
for (vector<UDT *>::const_iterator it = type->_subUDTs.begin(); it != type->_subUDTs.end(); ++it) {
rc = (*it)->acceptVisitor(*this);
if (DDR_RC_OK != rc) {
break;
}
}
return rc;
}
DDR_RC
TypeReplaceVisitor::visitEnum(EnumUDT *type) const
{
return DDR_RC_OK;
}
DDR_RC
TypeReplaceVisitor::visitTypedef(TypedefUDT *type) const
{
return _ir->replaceTypeUsingMap(&type->_aliasedType, type);
}
DDR_RC
TypeReplaceVisitor::visitComposite(ClassType *type) const
{
DDR_RC rc = visitNamespace(type);
if (DDR_RC_OK == rc) {
for (vector<Field *>::const_iterator it = type->_fieldMembers.begin(); it != type->_fieldMembers.end(); ++it) {
rc = _ir->replaceTypeUsingMap(&(*it)->_fieldType, type);
if (DDR_RC_OK != rc) {
break;
}
}
}
return rc;
}
DDR_RC
TypeReplaceVisitor::visitClass(ClassUDT *type) const
{
DDR_RC rc = visitComposite(type);
if (DDR_RC_OK == rc) {
rc = _ir->replaceTypeUsingMap((Type **)&type->_superClass, type);
}
return rc;
}
DDR_RC
TypeReplaceVisitor::visitUnion(UnionUDT *type) const
{
return visitComposite(type);
}
DDR_RC
Symbol_IR::replaceTypeUsingMap(Type **type, Type *outer)
{
DDR_RC rc = DDR_RC_OK;
if (NULL != type) {
Type *oldType = *type;
if ((NULL != oldType) && (_typeSet.end() == _typeSet.find(oldType))) {
Type *replacementType = findTypeInMap(oldType);
if (NULL == replacementType) {
ERRMSG("Error replacing type '%s' in '%s'",
oldType->getFullName().c_str(),
outer->getFullName().c_str());
rc = DDR_RC_ERROR;
} else if (oldType == replacementType) {
/* don't create a cycle */
ERRMSG("Cycle found replacing type '%s' in '%s'",
oldType->getFullName().c_str(),
outer->getFullName().c_str());
rc = DDR_RC_ERROR;
*type = NULL;
} else {
*type = replacementType;
}
}
}
return rc;
}
Type *
Symbol_IR::findTypeInMap(Type *typeToFind)
{
Type *returnType = NULL;
if ((NULL != typeToFind) && !typeToFind->isAnonymousType()) {
const string nameKey = typeToFind->getTypeNameKey();
unordered_map<string, set<Type *> >::const_iterator map_it = _typeMap.find(nameKey);
if (_typeMap.end() != map_it) {
const set<Type *> &matches = map_it->second;
if (1 == matches.size()) {
returnType = *matches.begin();
} else if (1 < matches.size()) {
set<Type *>::const_iterator it = find_if(matches.begin(), matches.end(), TypeCheck(typeToFind));
if (matches.end() != it) {
returnType = *it;
}
}
}
}
return returnType;
}
void
Symbol_IR::addTypeToMap(Type *type)
{
Type * const startType = type;
stack<vector<UDT *>::const_iterator> itStack;
vector<UDT *> *subs = NULL;
subs = type->getSubUDTS();
if ((NULL != subs) && !subs->empty()) {
itStack.push(subs->begin());
}
while (NULL != type) {
if (!type->isAnonymousType()) {
_typeMap[type->getTypeNameKey()].insert(type);
}
_typeSet.insert(type);
subs = type->getSubUDTS();
if (NULL == subs || subs->empty()) {
if (startType == type) {
type = NULL;
} else {
type = type->getNamespace();
}
} else if (subs->end() == itStack.top()) {
if (startType == type) {
type = NULL;
} else {
type = type->getNamespace();
}
itStack.pop();
} else {
type = *itStack.top();
itStack.top()++;
subs = type->getSubUDTS();
if ((NULL != subs) && !subs->empty()) {
itStack.push(subs->begin());
}
}
}
}
#if defined(DEBUG_PRINT_TYPES)
static Symbol_IR *previousIR = NULL;
#endif /* DEBUG_PRINT_TYPES */
DDR_RC
Symbol_IR::mergeIR(Symbol_IR *other)
{
#if defined(DEBUG_PRINT_TYPES)
TypePrinter printer(_portLibrary, TypePrinter::LITERALS);
if (this != previousIR) {
printf("\n");
printf("== Merge before ==\n");
printer.printUDTs(_types);
previousIR = this;
}
printf("\n");
printf("== Merge source ==\n");
printer.printUDTs(other->_types);
#endif /* DEBUG_PRINT_TYPES */
DDR_RC rc = DDR_RC_OK;
/* Create map of this source IR of type full name to type. Use it to replace all
* pointers in types acquired from other IRs. The problem of encountering
* types to replace that are not yet in this IR is solved by doing all of the type
* replacement after all types are merged.
*/
if (_typeMap.empty() && _typeSet.empty()) {
for (vector<Type *>::const_iterator it = _types.begin(); it != _types.end(); ++it) {
addTypeToMap(*it);
}
}
vector<Type *> mergedTypes;
mergeTypes(&_types, &other->_types, NULL, &mergedTypes);
const TypeReplaceVisitor typeReplacer(this);
for (vector<Type *>::const_iterator it = mergedTypes.begin(); it != mergedTypes.end(); ++it) {
rc = (*it)->acceptVisitor(typeReplacer);
if (DDR_RC_OK != rc) {
break;
}
}
#if defined(DEBUG_PRINT_TYPES)
printf("\n");
printf("== Merge result ==\n");
printer.printUDTs(_types);
#endif /* DEBUG_PRINT_TYPES */
return rc;
}