Skip to content

Commit

Permalink
Gave clearer names to some ast2ram constructs.
Browse files Browse the repository at this point in the history
  • Loading branch information
azreika committed Nov 13, 2020
1 parent 3a56357 commit b582c1f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 54 deletions.
16 changes: 8 additions & 8 deletions src/ast2ram/AstToRamTranslator.cpp
Expand Up @@ -714,16 +714,16 @@ void AstToRamTranslator::createRamRelation(size_t scc) {
getTypeQualifier(typeEnv->getType(attributes[i]->getTypeName())));
}
}
ramRels[name] = mk<ram::Relation>(
ramRelations[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,
ramRelations[deltaName] = mk<ram::Relation>(deltaName, arity, auxiliaryArity, attributeNames,
attributeTypeQualifiers, representation);
ramRels[newName] = mk<ram::Relation>(
ramRelations[newName] = mk<ram::Relation>(
newName, arity, auxiliaryArity, attributeNames, attributeTypeQualifiers, representation);
}
}
Expand Down Expand Up @@ -767,15 +767,15 @@ void AstToRamTranslator::translateProgram(const ast::TranslationUnit& translatio
if (Global::config().has("RamSIPS")) {
sipsChosen = Global::config().get("RamSIPS");
}
sips = ast::SipsMetric::create(sipsChosen, translationUnit);
sipsMetric = ast::SipsMetric::create(sipsChosen, translationUnit);

// replace ADTs with record representatives
removeADTs(translationUnit);

// handle the case of an empty SCC graph
if (sccGraph->getNumberOfSCCs() == 0) return;

// create all Ram relations in ramRels
// create all Ram relations in ramRelations
for (const auto& scc : sccOrder.order()) {
createRamRelation(scc);
}
Expand All @@ -784,7 +784,7 @@ void AstToRamTranslator::translateProgram(const ast::TranslationUnit& translatio
size_t indexOfScc = 0;
for (const auto& scc : sccOrder.order()) {
// create subroutine for this stratum
ramSubs["stratum_" + std::to_string(indexOfScc)] = translateSCC(scc, indexOfScc);
ramSubroutines["stratum_" + std::to_string(indexOfScc)] = translateSCC(scc, indexOfScc);
indexOfScc++;
}

Expand Down Expand Up @@ -815,14 +815,14 @@ Own<ram::TranslationUnit> AstToRamTranslator::translateUnit(ast::TranslationUnit
ErrorReport& errReport = tu.getErrorReport();
DebugReport& debugReport = tu.getDebugReport();
VecOwn<ram::Relation> rels;
for (auto& cur : ramRels) {
for (auto& cur : ramRelations) {
rels.push_back(std::move(cur.second));
}

if (ramMain == nullptr) {
ramMain = mk<ram::Sequence>();
}
auto ramProg = mk<ram::Program>(std::move(rels), std::move(ramMain), std::move(ramSubs));
auto ramProg = mk<ram::Program>(std::move(rels), std::move(ramMain), std::move(ramSubroutines));

// add the translated program to the debug report
if (!Global::config().get("debug-report").empty()) {
Expand Down
65 changes: 21 additions & 44 deletions src/ast2ram/AstToRamTranslator.h
Expand Up @@ -86,30 +86,24 @@ class AstToRamTranslator {
}

const ast::SipsMetric* getSipsMetric() const {
return sips.get();
return sipsMetric.get();
}

/** translates AST to translation unit */
/** AST->RAM translation methods */
Own<ram::TranslationUnit> translateUnit(ast::TranslationUnit& tu);

/** translate an AST argument to a RAM value */
Own<ram::Expression> translateValue(const ast::Argument* arg, const ValueIndex& index);
Own<ram::Condition> translateConstraint(const ast::Literal* arg, const ValueIndex& index);
Own<ram::Expression> translateConstant(const ast::Constant& c);

/** determine the auxiliary for relations */
size_t getEvaluationArity(const ast::Atom* atom) const;

/** create a RAM element access node */
static Own<ram::TupleElement> makeRamTupleElement(const Location& loc);

/** translate an AST constraint to a RAM condition */
Own<ram::Condition> translateConstraint(const ast::Literal* arg, const ValueIndex& index);

/** translate RAM code for a constant value */
Own<ram::Expression> translateConstant(ast::Constant const& c);

const ram::Relation* lookupRelation(const std::string& name) const {
auto it = ramRels.find(name);
assert(it != ramRels.end() && "relation not found");
auto it = ramRelations.find(name);
assert(it != ramRelations.end() && "relation not found");
return (*it).second.get();
}

Expand All @@ -120,46 +114,32 @@ class AstToRamTranslator {
/** RAM program */
Own<ram::Statement> ramMain;

/** Subroutines */
std::map<std::string, Own<ram::Statement>> ramSubs;

/** RAM relations */
std::map<std::string, Own<ram::Relation>> ramRels;

const ast::analysis::AuxiliaryArityAnalysis* auxArityAnalysis = nullptr;

/**
* assigns names to unnamed variables such that enclosing
* constructs may be cloned without losing the variable-identity
*/
virtual void addNegation(ast::Clause& clause, const ast::Atom* atom);

void nameUnnamedVariables(ast::Clause* clause);

void appendStmt(VecOwn<ram::Statement>& stmtList, Own<ram::Statement> stmt);

/** translate AST to RAM Program */
virtual void translateProgram(const ast::TranslationUnit& translationUnit);

virtual void clearExpiredRelations(
VecOwn<ram::Statement>& stmts, const std::set<const ast::Relation*>& expiredRelations);

private:
/** Type environment */
const ast::analysis::TypeEnvironment* typeEnv = nullptr;
std::map<std::string, Own<ram::Statement>> ramSubroutines;
std::map<std::string, Own<ram::Relation>> ramRelations;
Own<ast::SipsMetric> sipsMetric;

/** Analyses needed */
const ast::analysis::TypeEnvironment* typeEnv = nullptr;
const ast::analysis::IOTypeAnalysis* ioType = nullptr;
const ast::analysis::FunctorAnalysis* functorAnalysis = nullptr;
const ast::analysis::AuxiliaryArityAnalysis* auxArityAnalysis = nullptr;
const ast::analysis::RelationScheduleAnalysis* relationSchedule = nullptr;
const ast::analysis::SCCGraphAnalysis* sccGraph = nullptr;
const ast::analysis::RecursiveClausesAnalysis* recursiveClauses = nullptr;
const ast::analysis::RelationDetailCacheAnalysis* relDetail = nullptr;
const ast::analysis::PolymorphicObjectsAnalysis* polyAnalysis = nullptr;

/** SIPS metric for reordering */
Own<ast::SipsMetric> sips;
/** Translate AST to RAM Program */
virtual void translateProgram(const ast::TranslationUnit& translationUnit);

void nameUnnamedVariables(ast::Clause* clause);
void appendStmt(VecOwn<ram::Statement>& stmtList, Own<ram::Statement> stmt);
Own<ram::Sequence> translateSCC(size_t scc, size_t idx);
virtual void addNegation(ast::Clause& clause, const ast::Atom* atom);
virtual void clearExpiredRelations(
VecOwn<ram::Statement>& stmts, const std::set<const ast::Relation*>& expiredRelations);

private:
/** replace ADTs with special records */
static bool removeADTs(const ast::TranslationUnit& translationUnit);

Expand All @@ -175,9 +155,6 @@ class AstToRamTranslator {
/** Get ram representation of constant */
RamDomain getConstantRamRepresentation(const ast::Constant& constant);

/** 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);

Expand Down
4 changes: 2 additions & 2 deletions src/ast2ram/ProvenanceTranslator.cpp
Expand Up @@ -67,11 +67,11 @@ void ProvenanceTranslator::addProvenanceClauseSubroutines(const ast::Program* pr

std::string subroutineLabel =
relName.str() + "_" + std::to_string(getClauseNum(program, &clause)) + "_subproof";
ramSubs[subroutineLabel] = makeSubproofSubroutine(clause);
ramSubroutines[subroutineLabel] = makeSubproofSubroutine(clause);

std::string negationSubroutineLabel =
relName.str() + "_" + std::to_string(getClauseNum(program, &clause)) + "_negation_subproof";
ramSubs[negationSubroutineLabel] = makeNegationSubproofSubroutine(clause);
ramSubroutines[negationSubroutineLabel] = makeNegationSubproofSubroutine(clause);
});
}

Expand Down

0 comments on commit b582c1f

Please sign in to comment.