/
AstToRamTranslator.h
171 lines (136 loc) · 5.71 KB
/
AstToRamTranslator.h
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
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file AstToRamTranslator.h
*
* Translator from AST into RAM
*
***********************************************************************/
#pragma once
#include "souffle/RamTypes.h"
#include "souffle/utility/ContainerUtil.h"
#include <map>
#include <set>
#include <string>
#include <vector>
namespace souffle {
class SymbolTable;
}
namespace souffle::ast {
class Argument;
class Atom;
class Clause;
class Constant;
class Literal;
class Program;
class QualifiedName;
class Relation;
class SipsMetric;
class TranslationUnit;
} // namespace souffle::ast
namespace souffle::ast::analysis {
class IOTypeAnalysis;
class AuxiliaryArityAnalysis;
class FunctorAnalysis;
class PolymorphicObjectsAnalysis;
class RecursiveClausesAnalysis;
class RelationDetailCacheAnalysis;
class RelationScheduleAnalysis;
class SCCGraphAnalysis;
class TypeEnvironment;
} // namespace souffle::ast::analysis
namespace souffle::ram {
class Condition;
class Expression;
class Relation;
class Sequence;
class Statement;
class TranslationUnit;
class TupleElement;
} // namespace souffle::ram
namespace souffle::ast2ram {
struct Location;
class ValueIndex;
class AstToRamTranslator {
public:
AstToRamTranslator();
~AstToRamTranslator();
const ast::analysis::AuxiliaryArityAnalysis* getAuxArityAnalysis() const {
return auxArityAnalysis;
}
const ast::analysis::FunctorAnalysis* getFunctorAnalysis() const {
return functorAnalysis;
}
const ast::analysis::PolymorphicObjectsAnalysis* getPolymorphicObjectsAnalysis() const {
return polyAnalysis;
}
const ast::SipsMetric* getSipsMetric() const {
return sipsMetric.get();
}
/** AST->RAM translation methods */
Own<ram::TranslationUnit> translateUnit(ast::TranslationUnit& tu);
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);
virtual Own<ram::Sequence> translateProgram(const ast::TranslationUnit& translationUnit);
/** 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);
const ram::Relation* lookupRelation(const std::string& name) const;
protected:
/** AST program */
const ast::Program* program = 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;
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);
// TODO (b-scholz): revisit / refactor so that only one directive is translated
std::vector<std::map<std::string, std::string>> getInputDirectives(const ast::Relation* rel);
// TODO (b-scholz): revisit / refactor so that only one directive is translated
std::vector<std::map<std::string, std::string>> getOutputDirectives(const ast::Relation* rel);
/** Return a symbol table **/
SymbolTable& getSymbolTable();
/** Get ram representation of constant */
RamDomain getConstantRamRepresentation(const ast::Constant& constant);
/** 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);
/** translate RAM code for recursive relations in a strongly-connected component */
Own<ram::Statement> translateRecursiveRelation(const std::set<const ast::Relation*>& scc);
/** add a statement to store a relation */
void makeRamClear(VecOwn<ram::Statement>& curStmts, const ast::Relation* relation);
/** add a statement to drop a relation */
void makeRamStore(VecOwn<ram::Statement>& curStmts, const ast::Relation* relation);
/** add a statement to load a relation */
void makeRamLoad(VecOwn<ram::Statement>& curStmts, const ast::Relation* relation);
/** finalise the types of polymorphic objects */
// TODO (azreika): should be removed once the translator is refactored to avoid cloning
void finaliseAstTypes();
};
} // namespace souffle::ast2ram