souffle  2.0.2-371-g6315b36
AstToRamTranslator.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file AstToRamTranslator.h
12  *
13  * Translator from AST into RAM
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ram/Relation.h"
20 #include "souffle/RamTypes.h"
22 #include <cassert>
23 #include <map>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 namespace souffle {
29 class SymbolTable;
30 }
31 
32 namespace souffle::ast {
33 class Argument;
34 class Atom;
35 class Clause;
36 class Constant;
37 class Literal;
38 class Program;
39 class QualifiedName;
40 class Relation;
41 class SipsMetric;
42 class TranslationUnit;
43 } // namespace souffle::ast
44 
45 namespace souffle::ast::analysis {
46 class IOTypeAnalysis;
47 class AuxiliaryArityAnalysis;
48 class FunctorAnalysis;
49 class PolymorphicObjectsAnalysis;
50 class RecursiveClausesAnalysis;
51 class TypeEnvironment;
52 } // namespace souffle::ast::analysis
53 
54 namespace souffle::ram {
55 class Condition;
56 class Expression;
57 class Relation;
58 class Statement;
59 class TranslationUnit;
60 class TupleElement;
61 } // namespace souffle::ram
62 
63 namespace souffle::ast2ram {
64 
65 struct Location;
66 class ValueIndex;
67 
68 /**
69  * Main class for the AST->RAM translator
70  */
72 public:
75 
77  return auxArityAnalysis;
78  }
79 
81  return functorAnalysis;
82  }
83 
85  return polyAnalysis;
86  }
87 
88  const ast::SipsMetric* getSipsMetric() const {
89  return sips.get();
90  }
91 
92  /** translates AST to translation unit */
94 
95  /** translate an AST argument to a RAM value */
97 
98  /** a utility to translate atoms to relations */
99  std::string translateRelation(const ast::Atom* atom);
100 
101  /** translate an AST relation to a RAM relation */
102  std::string translateRelation(const ast::Relation* rel, const std::string relationNamePrefix = "");
103 
104  /** determine the auxiliary for relations */
105  size_t getEvaluationArity(const ast::Atom* atom) const;
106 
107  /** create a RAM element access node */
109 
110  /** translate an AST constraint to a RAM condition */
112 
113  /** translate RAM code for a constant value */
115 
116  const ram::Relation* lookupRelation(const std::string& name) const {
117  auto it = ramRels.find(name);
118  assert(it != ramRels.end() && "relation not found");
119  return (*it).second.get();
120  }
121 
122 private:
123  /** AST program */
124  const ast::Program* program = nullptr;
125 
126  /** Type environment */
128 
129  /** IO Type */
131 
132  /** Functors' analysis */
134 
135  /** Auxiliary Arity Analysis */
137 
138  /** Polymorphic Objects Analysis */
140 
141  /** SIPS metric for reordering */
143 
144  /** RAM program */
146 
147  /** Subroutines */
148  std::map<std::string, Own<ram::Statement>> ramSubs;
149 
150  /** RAM relations */
151  std::map<std::string, Own<ram::Relation>> ramRels;
152 
153  /** translate AST to RAM Program */
154  void translateProgram(const ast::TranslationUnit& translationUnit);
155 
156  /** replace ADTs with special records */
157  static bool removeADTs(const ast::TranslationUnit& translationUnit);
158 
159  /**
160  * assigns names to unnamed variables such that enclosing
161  * constructs may be cloned without losing the variable-identity
162  */
163  void nameUnnamedVariables(ast::Clause* clause);
164 
165  /** converts the given relation identifier into a relation name */
166  static std::string getRelationName(const ast::QualifiedName& id);
167 
168  // TODO (b-scholz): revisit / refactor so that only one directive is translated
169  std::vector<std::map<std::string, std::string>> getInputDirectives(const ast::Relation* rel);
170 
171  // TODO (b-scholz): revisit / refactor so that only one directive is translated
172  std::vector<std::map<std::string, std::string>> getOutputDirectives(const ast::Relation* rel);
173 
174  /** translate a temporary `delta` relation to a RAM relation for semi-naive evaluation */
175  std::string translateDeltaRelation(const ast::Relation* rel);
176 
177  /** translate a temporary `new` relation to a RAM relation for semi-naive evaluation */
178  std::string translateNewRelation(const ast::Relation* rel);
179 
180  /** Return a symbol table **/
182 
183  /** Get ram representation of constant */
185 
186  /**
187  * translate RAM code for the non-recursive clauses of the given relation.
188  *
189  * @return a corresponding statement or null if there are no non-recursive clauses.
190  */
192  const ast::Relation& rel, const ast::analysis::RecursiveClausesAnalysis* recursiveClauses);
193 
194  /** translate RAM code for recursive relations in a strongly-connected component */
195  Own<ram::Statement> translateRecursiveRelation(const std::set<const ast::Relation*>& scc,
196  const ast::analysis::RecursiveClausesAnalysis* recursiveClauses);
197 
198  /** translate RAM code for subroutine to get subproofs */
200 
201  /** translate RAM code for subroutine to get subproofs for non-existence of a tuple */
203 };
204 
205 } // namespace souffle::ast2ram
souffle::ast2ram::AstToRamTranslator::sips
Own< ast::SipsMetric > sips
SIPS metric for reordering.
Definition: AstToRamTranslator.h:142
souffle::ast2ram::AstToRamTranslator::getConstantRamRepresentation
RamDomain getConstantRamRepresentation(const ast::Constant &constant)
Get ram representation of constant.
Definition: AstToRamTranslator.cpp:418
souffle::ast2ram::AstToRamTranslator::ramMain
Own< ram::Statement > ramMain
RAM program.
Definition: AstToRamTranslator.h:145
souffle::ast2ram::AstToRamTranslator::translateNonRecursiveRelation
Own< ram::Statement > translateNonRecursiveRelation(const ast::Relation &rel, const ast::analysis::RecursiveClausesAnalysis *recursiveClauses)
translate RAM code for the non-recursive clauses of the given relation.
Definition: AstToRamTranslator.cpp:452
souffle::ast2ram::AstToRamTranslator::auxArityAnalysis
const ast::analysis::AuxiliaryArityAnalysis * auxArityAnalysis
Auxiliary Arity Analysis.
Definition: AstToRamTranslator.h:136
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::ast::analysis::IOTypeAnalysis
Definition: IOType.h:39
souffle::ast::analysis::FunctorAnalysis
Definition: Functor.h:39
souffle::ast2ram::AstToRamTranslator::ramSubs
std::map< std::string, Own< ram::Statement > > ramSubs
Subroutines.
Definition: AstToRamTranslator.h:148
souffle::ast::analysis::PolymorphicObjectsAnalysis
Definition: PolymorphicObjects.h:39
souffle::ast2ram::AstToRamTranslator::nameUnnamedVariables
void nameUnnamedVariables(ast::Clause *clause)
assigns names to unnamed variables such that enclosing constructs may be cloned without losing the va...
Definition: AstToRamTranslator.cpp:519
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ast2ram::AstToRamTranslator::functorAnalysis
const ast::analysis::FunctorAnalysis * functorAnalysis
Functors' analysis.
Definition: AstToRamTranslator.h:133
souffle::ast2ram::AstToRamTranslator::translateDeltaRelation
std::string translateDeltaRelation(const ast::Relation *rel)
translate a temporary delta relation to a RAM relation for semi-naive evaluation
Definition: AstToRamTranslator.cpp:228
souffle::ast2ram::AstToRamTranslator::getInputDirectives
std::vector< std::map< std::string, std::string > > getInputDirectives(const ast::Relation *rel)
Definition: AstToRamTranslator.cpp:170
souffle::ram::Condition
Abstract class for conditions and boolean values in RAM.
Definition: Condition.h:35
souffle::ast::Relation
Defines a relation with a name, attributes, qualifiers, and internal representation.
Definition: Relation.h:49
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast2ram::Location
Definition: Location.h:29
souffle::ast2ram::AstToRamTranslator::program
const ast::Program * program
AST program.
Definition: AstToRamTranslator.h:124
souffle::ast2ram::AstToRamTranslator::ramRels
std::map< std::string, Own< ram::Relation > > ramRels
RAM relations.
Definition: AstToRamTranslator.h:151
souffle::ast2ram::AstToRamTranslator::translateRelation
std::string translateRelation(const ast::Atom *atom)
a utility to translate atoms to relations
Definition: AstToRamTranslator.cpp:219
souffle::ram::TupleElement
Access element from the current tuple in a tuple environment.
Definition: TupleElement.h:42
souffle::ast::Atom
An atom class.
Definition: Atom.h:51
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ast2ram::AstToRamTranslator::AstToRamTranslator
AstToRamTranslator()
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
souffle::ast2ram::AstToRamTranslator::getEvaluationArity
size_t getEvaluationArity(const ast::Atom *atom) const
determine the auxiliary for relations
Definition: AstToRamTranslator.cpp:154
souffle::ast2ram::AstToRamTranslator::getFunctorAnalysis
const ast::analysis::FunctorAnalysis * getFunctorAnalysis() const
Definition: AstToRamTranslator.h:80
souffle::ram::TranslationUnit
Translating a RAM program.
Definition: TranslationUnit.h:55
souffle::ast::Program
The program class consists of relations, clauses and types.
Definition: Program.h:52
ContainerUtil.h
souffle::ast2ram::AstToRamTranslator::lookupRelation
const ram::Relation * lookupRelation(const std::string &name) const
Definition: AstToRamTranslator.h:116
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
Relation.h
souffle::ast2ram::AstToRamTranslator::translateConstraint
Own< ram::Condition > translateConstraint(const ast::Literal *arg, const ValueIndex &index)
translate an AST constraint to a RAM condition
Definition: AstToRamTranslator.cpp:340
souffle::ast2ram::ValueIndex
Definition: ValueIndex.h:36
souffle::ast2ram::AstToRamTranslator::~AstToRamTranslator
~AstToRamTranslator()
souffle::ast::Constant
Abstract constant class.
Definition: Constant.h:38
souffle::ast2ram::AstToRamTranslator::polyAnalysis
const ast::analysis::PolymorphicObjectsAnalysis * polyAnalysis
Polymorphic Objects Analysis.
Definition: AstToRamTranslator.h:139
souffle::ast::analysis::TypeEnvironment
A type environment is a set of types.
Definition: TypeSystem.h:401
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::SymbolTable
Definition: SymbolTable.h:48
souffle::ast2ram::AstToRamTranslator::makeNegationSubproofSubroutine
Own< ram::Statement > makeNegationSubproofSubroutine(const ast::Clause &clause)
translate RAM code for subroutine to get subproofs for non-existence of a tuple
Definition: AstToRamTranslator.cpp:841
souffle::ast::Literal
Defines an abstract class for literals in a horn clause.
Definition: Literal.h:36
souffle::ast2ram::AstToRamTranslator::getSipsMetric
const ast::SipsMetric * getSipsMetric() const
Definition: AstToRamTranslator.h:88
souffle::ast2ram::AstToRamTranslator::typeEnv
const ast::analysis::TypeEnvironment * typeEnv
Type environment.
Definition: AstToRamTranslator.h:127
souffle::ast2ram::AstToRamTranslator::translateProgram
void translateProgram(const ast::TranslationUnit &translationUnit)
translate AST to RAM Program
Definition: AstToRamTranslator.cpp:1128
souffle::ast2ram::AstToRamTranslator::removeADTs
static bool removeADTs(const ast::TranslationUnit &translationUnit)
replace ADTs with special records
Definition: AstToRamTranslator.cpp:1056
souffle::ast2ram::AstToRamTranslator::getRelationName
static std::string getRelationName(const ast::QualifiedName &id)
converts the given relation identifier into a relation name
Definition: AstToRamTranslator.cpp:549
souffle::ast2ram::AstToRamTranslator::translateNewRelation
std::string translateNewRelation(const ast::Relation *rel)
translate a temporary new relation to a RAM relation for semi-naive evaluation
Definition: AstToRamTranslator.cpp:232
souffle::ast2ram::AstToRamTranslator::makeRamTupleElement
static Own< ram::TupleElement > makeRamTupleElement(const Location &loc)
create a RAM element access node
Definition: AstToRamTranslator.cpp:150
souffle::ast2ram::AstToRamTranslator::makeSubproofSubroutine
Own< ram::Statement > makeSubproofSubroutine(const ast::Clause &clause)
translate RAM code for subroutine to get subproofs
Definition: AstToRamTranslator.cpp:765
souffle::ast2ram::AstToRamTranslator::getPolymorphicObjectsAnalysis
const ast::analysis::PolymorphicObjectsAnalysis * getPolymorphicObjectsAnalysis() const
Definition: AstToRamTranslator.h:84
souffle::ram::Statement
Abstract class for RAM statements.
Definition: Statement.h:37
souffle::ast2ram::AstToRamTranslator::getSymbolTable
SymbolTable & getSymbolTable()
Return a symbol table.
Definition: AstToRamTranslator.cpp:335
souffle::ast2ram::AstToRamTranslator::getAuxArityAnalysis
const ast::analysis::AuxiliaryArityAnalysis * getAuxArityAnalysis() const
Definition: AstToRamTranslator.h:76
souffle::ast2ram::AstToRamTranslator
Main class for the AST->RAM translator.
Definition: AstToRamTranslator.h:71
RamTypes.h
souffle::ast::SipsMetric
Class for SIPS cost-metric functions Each subclass represents a different heuristic used for evaluati...
Definition: SipsMetric.h:39
souffle::ast2ram::AstToRamTranslator::translateValue
Own< ram::Expression > translateValue(const ast::Argument *arg, const ValueIndex &index)
translate an AST argument to a RAM value
Definition: AstToRamTranslator.cpp:236
souffle::ast2ram
Definition: AstToRamTranslator.cpp:132
souffle::ast2ram::AstToRamTranslator::ioType
const ast::analysis::IOTypeAnalysis * ioType
IO Type.
Definition: AstToRamTranslator.h:130
souffle::ast::analysis
Definition: Aggregate.cpp:39
souffle
Definition: AggregateOp.h:25
souffle::ast::analysis::AuxiliaryArityAnalysis
Determine the auxiliary arity for relations.
Definition: AuxArity.h:38
souffle::ast::analysis::RecursiveClausesAnalysis
Analysis pass identifying clauses which are recursive.
Definition: RecursiveClauses.h:44
souffle::ast2ram::AstToRamTranslator::translateUnit
Own< ram::TranslationUnit > translateUnit(ast::TranslationUnit &tu)
translates AST to translation unit
Definition: AstToRamTranslator.cpp:1329
souffle::ast2ram::AstToRamTranslator::translateConstant
Own< ram::Expression > translateConstant(ast::Constant const &c)
translate RAM code for a constant value
Definition: AstToRamTranslator.cpp:437
souffle::ast
Definition: Aggregator.h:35
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ast2ram::AstToRamTranslator::getOutputDirectives
std::vector< std::map< std::string, std::string > > getOutputDirectives(const ast::Relation *rel)
Definition: AstToRamTranslator.cpp:194
souffle::ram::Expression
Abstract class for describing scalar values in RAM.
Definition: Expression.h:33
souffle::ast2ram::AstToRamTranslator::translateRecursiveRelation
Own< ram::Statement > translateRecursiveRelation(const std::set< const ast::Relation * > &scc, const ast::analysis::RecursiveClausesAnalysis *recursiveClauses)
translate RAM code for recursive relations in a strongly-connected component
Definition: AstToRamTranslator.cpp:554