Skip to content

Commit ae78dab

Browse files
authored
Refactor subsort table code (#951)
The backend had duplicated logic in a couple of places to build the subsort[^1] relation for a definition; this PR refactors that code to live with `KOREDefinition` so that it can be more easily reused. I have also taken the opportunity to pull some code out of the `AST.cpp` monolith into a separate translation unit to make it easier to read. No actual functionality is introduced here; it's just a refactoring to move identical code inside a common method. [^1]: Analogously for overloads as well
1 parent 0231f18 commit ae78dab

File tree

7 files changed

+236
-216
lines changed

7 files changed

+236
-216
lines changed

include/kllvm/ast/AST.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,26 @@ class KOREDefinition {
934934
void addAttribute(sptr<KORECompositePattern> Attribute);
935935
void print(std::ostream &Out, unsigned indent = 0) const;
936936

937+
/*
938+
* Build this definition's subsort relation from axioms that have the
939+
* `subsort` attribute.
940+
*
941+
* The returned map is as follows:
942+
*
943+
* S |-> {T . S is a subsort of T}
944+
*/
945+
SubsortMap getSubsorts() const;
946+
947+
/*
948+
* Build this definition's overload relation from axioms that have the
949+
* `overload` attribute.
950+
*
951+
* The returned map is as follows:
952+
*
953+
* P |-> {Q . P is a more specific overload of Q}
954+
*/
955+
SymbolMap getOverloads() const;
956+
937957
const std::vector<sptr<KOREModule>> &getModules() const { return modules; }
938958
const KORECompositeSortDeclarationMapType &getSortDeclarations() const {
939959
return sortDeclarations;
@@ -969,27 +989,6 @@ void readMultimap(
969989

970990
sptr<KOREPattern> stripRawTerm(sptr<KOREPattern> const &term);
971991

972-
template <typename Elem, typename Hash, typename Equal>
973-
std::unordered_map<Elem *, std::unordered_set<Elem *, Hash, Equal>, Hash, Equal>
974-
transitiveClosure(std::unordered_map<
975-
Elem *, std::unordered_set<Elem *, Hash, Equal>, Hash, Equal>
976-
relations) {
977-
bool dirty;
978-
do {
979-
dirty = false;
980-
for (auto &entry : relations) {
981-
SortSet newSucc;
982-
for (auto &elem : entry.second) {
983-
auto &relation = relations[elem];
984-
for (auto elem2 : relation) {
985-
dirty |= relations[entry.first].insert(elem2).second;
986-
}
987-
}
988-
}
989-
} while (dirty);
990-
return relations;
991-
}
992-
993992
namespace detail {
994993

995994
template <typename T>

lib/ast/AST.cpp

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,149 +1362,6 @@ void KOREModule::addDeclaration(sptr<KOREDeclaration> Declaration) {
13621362
declarations.push_back(std::move(Declaration));
13631363
}
13641364

1365-
void KOREDefinition::addModule(sptr<KOREModule> Module) {
1366-
for (const auto &decl : Module->getDeclarations()) {
1367-
if (auto *sortDecl
1368-
= dynamic_cast<KORECompositeSortDeclaration *>(decl.get())) {
1369-
sortDeclarations.insert({sortDecl->getName(), sortDecl});
1370-
auto sort = KORECompositeSort::Create(sortDecl->getName());
1371-
} else if (
1372-
auto *symbolDecl = dynamic_cast<KORESymbolDeclaration *>(decl.get())) {
1373-
symbolDeclarations.insert(
1374-
{symbolDecl->getSymbol()->getName(), symbolDecl});
1375-
} else if (
1376-
auto *aliasDecl = dynamic_cast<KOREAliasDeclaration *>(decl.get())) {
1377-
aliasDeclarations.insert({aliasDecl->getSymbol()->getName(), aliasDecl});
1378-
} else if (auto *axiom = dynamic_cast<KOREAxiomDeclaration *>(decl.get())) {
1379-
axioms.push_back(axiom);
1380-
}
1381-
}
1382-
modules.push_back(std::move(Module));
1383-
}
1384-
1385-
void KOREDefinition::addAttribute(sptr<KORECompositePattern> Attribute) {
1386-
std::string name = Attribute->getConstructor()->getName();
1387-
attributes.insert({name, std::move(Attribute)});
1388-
}
1389-
1390-
void KOREDefinition::insertReservedSymbols() {
1391-
auto mod = KOREModule::Create("K-RAW-TERM");
1392-
auto decl = KORESymbolDeclaration::Create("rawTerm", true);
1393-
auto sort = KORECompositeSort::Create("SortKItem");
1394-
1395-
decl->getSymbol()->addSort(sort);
1396-
decl->getSymbol()->addArgument(sort);
1397-
mod->addDeclaration(std::move(decl));
1398-
1399-
addModule(std::move(mod));
1400-
}
1401-
1402-
// NOLINTNEXTLINE(*-cognitive-complexity)
1403-
void KOREDefinition::preprocess() {
1404-
insertReservedSymbols();
1405-
1406-
for (auto *axiom : axioms) {
1407-
axiom->pattern = axiom->pattern->expandAliases(this);
1408-
}
1409-
auto symbols = std::map<std::string, std::vector<KORESymbol *>>{};
1410-
unsigned nextOrdinal = 0;
1411-
for (const auto &decl : symbolDeclarations) {
1412-
if (decl.second->getAttributes().count("freshGenerator")) {
1413-
auto sort = decl.second->getSymbol()->getSort();
1414-
if (sort->isConcrete()) {
1415-
freshFunctions[dynamic_cast<KORECompositeSort *>(sort.get())->getName()]
1416-
= decl.second->getSymbol();
1417-
}
1418-
}
1419-
}
1420-
for (auto iter = axioms.begin(); iter != axioms.end();) {
1421-
auto *axiom = *iter;
1422-
axiom->ordinal = nextOrdinal;
1423-
ordinals[nextOrdinal++] = axiom;
1424-
axiom->pattern->markSymbols(symbols);
1425-
if (!axiom->isRequired()) {
1426-
iter = axioms.erase(iter);
1427-
} else {
1428-
++iter;
1429-
}
1430-
}
1431-
for (auto &module : modules) {
1432-
const auto &declarations = module->getDeclarations();
1433-
for (const auto &declaration : declarations) {
1434-
auto *decl = dynamic_cast<KORESymbolDeclaration *>(declaration.get());
1435-
if (decl == nullptr) {
1436-
continue;
1437-
}
1438-
if (decl->isHooked() && decl->getObjectSortVariables().empty()) {
1439-
KORESymbol *symbol = decl->getSymbol();
1440-
symbols.emplace(symbol->getName(), std::vector<KORESymbol *>{symbol});
1441-
}
1442-
}
1443-
}
1444-
for (const auto &entry : symbols) {
1445-
for (auto *symbol : entry.second) {
1446-
auto *decl = symbolDeclarations.at(symbol->getName());
1447-
symbol->instantiateSymbol(decl);
1448-
}
1449-
}
1450-
uint32_t nextSymbol = 0;
1451-
uint16_t nextLayout = 1;
1452-
auto instantiations = std::unordered_map<KORESymbol, uint32_t, HashSymbol>{};
1453-
auto layouts = std::unordered_map<std::string, uint16_t>{};
1454-
auto variables
1455-
= std::unordered_map<std::string, std::pair<uint32_t, uint32_t>>{};
1456-
for (const auto &entry : symbols) {
1457-
uint32_t firstTag = nextSymbol;
1458-
for (auto *symbol : entry.second) {
1459-
if (symbol->isConcrete()) {
1460-
if (!instantiations.count(*symbol)) {
1461-
instantiations.emplace(*symbol, nextSymbol++);
1462-
}
1463-
std::string layoutStr = symbol->layoutString(this);
1464-
if (!layouts.count(layoutStr)) {
1465-
layouts.emplace(layoutStr, nextLayout++);
1466-
}
1467-
symbol->firstTag = symbol->lastTag = instantiations.at(*symbol);
1468-
symbol->layout = layouts.at(layoutStr);
1469-
objectSymbols[symbol->firstTag] = symbol;
1470-
allObjectSymbols[ast_to_string(*symbol)] = symbol;
1471-
}
1472-
}
1473-
uint32_t lastTag = nextSymbol - 1;
1474-
if (!entry.second.empty()) {
1475-
variables.emplace(
1476-
entry.first, std::pair<uint32_t, uint32_t>{firstTag, lastTag});
1477-
}
1478-
}
1479-
for (const auto &entry : symbols) {
1480-
auto range = variables.at(entry.first);
1481-
for (auto *symbol : entry.second) {
1482-
for (const auto &sort : symbol->getArguments()) {
1483-
if (sort->isConcrete()) {
1484-
hookedSorts[dynamic_cast<KORECompositeSort *>(sort.get())
1485-
->getCategory(this)]
1486-
= std::dynamic_pointer_cast<KORECompositeSort>(sort);
1487-
}
1488-
}
1489-
if (symbol->getSort()->isConcrete()) {
1490-
hookedSorts[dynamic_cast<KORECompositeSort *>(symbol->getSort().get())
1491-
->getCategory(this)]
1492-
= std::dynamic_pointer_cast<KORECompositeSort>(symbol->getSort());
1493-
}
1494-
if (!symbol->isConcrete()) {
1495-
if (symbol->isPolymorphic()) {
1496-
symbol->firstTag = range.first;
1497-
symbol->lastTag = range.second;
1498-
auto *decl = symbolDeclarations.at(symbol->getName());
1499-
if (decl->getAttributes().count("sortInjection")) {
1500-
injSymbol = symbol;
1501-
}
1502-
}
1503-
}
1504-
}
1505-
}
1506-
}
1507-
15081365
// Pretty printer
15091366
void KORESortVariable::print(std::ostream &Out, unsigned indent) const {
15101367
std::string Indent(indent, ' ');

lib/ast/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_library(AST
22
AST.cpp
33
pattern_matching.cpp
4+
definition.cpp
45
)
56

67
target_link_libraries(AST

0 commit comments

Comments
 (0)