Skip to content

Commit

Permalink
Moved out subsection of program translator to new method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdul Zreika committed Nov 12, 2020
1 parent 2c15748 commit ea1f6dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
66 changes: 35 additions & 31 deletions src/ast2ram/AstToRamTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,40 @@ void AstToRamTranslator::makeRamClear(VecOwn<ram::Statement>& curStmts, const as
appendStmt(curStmts, mk<ram::Clear>(getConcreteRelationName(relation)));
}

void AstToRamTranslator::createRamRelation(size_t scc) {
const auto& isRecursive = sccGraph->isRecursive(scc);
const auto& allInterns = sccGraph->getInternalRelations(scc);
for (const auto& rel : allInterns) {
std::string name = rel->getQualifiedName().toString();
auto arity = rel->getArity();
auto auxiliaryArity = auxArityAnalysis->getArity(rel);
auto representation = rel->getRepresentation();
const auto& attributes = rel->getAttributes();

std::vector<std::string> attributeNames;
std::vector<std::string> attributeTypeQualifiers;
for (size_t i = 0; i < rel->getArity(); ++i) {
attributeNames.push_back(attributes[i]->getName());
if (typeEnv != nullptr) {
attributeTypeQualifiers.push_back(
getTypeQualifier(typeEnv->getType(attributes[i]->getTypeName())));
}
}
ramRels[name] = mk<ram::Relation>(
name, arity, auxiliaryArity, attributeNames, attributeTypeQualifiers, representation);

// recursive relations also require @delta and @new variants, with the same signature
if (isRecursive) {
std::string deltaName = "@delta_" + name;
std::string newName = "@new_" + name;
ramRels[deltaName] = mk<ram::Relation>(deltaName, arity, auxiliaryArity, attributeNames,
attributeTypeQualifiers, representation);
ramRels[newName] = mk<ram::Relation>(
newName, arity, auxiliaryArity, attributeNames, attributeTypeQualifiers, representation);
}
}
}

/** translates the given datalog program into an equivalent RAM program */
void AstToRamTranslator::translateProgram(const ast::TranslationUnit& translationUnit) {
// keep track of relevant analyses
Expand Down Expand Up @@ -1061,37 +1095,7 @@ void AstToRamTranslator::translateProgram(const ast::TranslationUnit& translatio

// create all Ram relations in ramRels
for (const auto& scc : sccOrder.order()) {
const auto& isRecursive = sccGraph->isRecursive(scc);
const auto& allInterns = sccGraph->getInternalRelations(scc);
for (const auto& rel : allInterns) {
std::string name = rel->getQualifiedName().toString();
auto arity = rel->getArity();
auto auxiliaryArity = auxArityAnalysis->getArity(rel);
auto representation = rel->getRepresentation();
const auto& attributes = rel->getAttributes();

std::vector<std::string> attributeNames;
std::vector<std::string> attributeTypeQualifiers;
for (size_t i = 0; i < rel->getArity(); ++i) {
attributeNames.push_back(attributes[i]->getName());
if (typeEnv != nullptr) {
attributeTypeQualifiers.push_back(
getTypeQualifier(typeEnv->getType(attributes[i]->getTypeName())));
}
}
ramRels[name] = mk<ram::Relation>(
name, arity, auxiliaryArity, attributeNames, attributeTypeQualifiers, representation);

// recursive relations also require @delta and @new variants, with the same signature
if (isRecursive) {
std::string deltaName = "@delta_" + name;
std::string newName = "@new_" + name;
ramRels[deltaName] = mk<ram::Relation>(deltaName, arity, auxiliaryArity, attributeNames,
attributeTypeQualifiers, representation);
ramRels[newName] = mk<ram::Relation>(newName, arity, auxiliaryArity, attributeNames,
attributeTypeQualifiers, representation);
}
}
createRamRelation(scc);
}

// maintain the index of the SCC within the topological order
Expand Down
3 changes: 3 additions & 0 deletions src/ast2ram/AstToRamTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class AstToRamTranslator {
/** translate RAM code for a given SCC */
Own<ram::Sequence> translateSCC(size_t scc, size_t idx);

/** create RAM relations for a given SCC */
void createRamRelation(size_t scc);

/** translate RAM code for the non-recursive clauses of the given relation */
Own<ram::Statement> translateNonRecursiveRelation(const ast::Relation& rel);

Expand Down

0 comments on commit ea1f6dd

Please sign in to comment.