souffle  2.0.2-371-g6315b36
Synthesiser.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, The Souffle Developers. 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 Synthesiser.h
12  *
13  * Declares synthesiser classes to synthesise C++ code from a RAM program.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ram/Operation.h"
20 #include "ram/Relation.h"
21 #include "ram/Statement.h"
22 #include "ram/TranslationUnit.h"
23 #include "ram/utility/Visitor.h"
24 #include "souffle/RecordTable.h"
26 #include "synthesiser/Relation.h"
27 #include <cstddef>
28 #include <map>
29 #include <memory>
30 #include <ostream>
31 #include <set>
32 #include <string>
33 
34 namespace souffle::synthesiser {
35 
36 /**
37  * A RAM synthesiser: synthesises a C++ program from a RAM program.
38  */
39 class Synthesiser {
40 private:
41  /** Record Table */
42  RecordTable recordTable;
43 
44  /** RAM translation unit */
46 
47  /** RAM identifier to C++ identifier map */
48  std::map<const std::string, const std::string> identifiers;
49 
50  /** Frequency profiling of searches */
51  std::map<std::string, unsigned> idxMap;
52 
53  /** Frequency profiling of non-existence checks */
54  std::map<std::string, size_t> neIdxMap;
55 
56  /** Cache for generated types for relations */
57  std::set<std::string> typeCache;
58 
59  /** Relation map */
60  std::map<std::string, const ram::Relation*> relationMap;
61 
62 protected:
63  /** Get record table */
64  const RecordTable& getRecordTable();
65 
66  /** Convert RAM identifier */
67  const std::string convertRamIdent(const std::string& name);
68 
69  /** Get relation name */
70  const std::string getRelationName(const ram::Relation& rel);
71  const std::string getRelationName(const ram::Relation* rel);
72 
73  /** Get context name */
74  const std::string getOpContextName(const ram::Relation& rel);
75 
76  /** Get relation struct definition */
77  void generateRelationTypeStruct(std::ostream& out, Own<Relation> relationType);
78 
79  /** Get referenced relations */
80  std::set<const ram::Relation*> getReferencedRelations(const ram::Operation& op);
81 
82  /** Generate code */
83  void emitCode(std::ostream& out, const ram::Statement& stmt);
84 
85  /** Lookup frequency counter */
86  unsigned lookupFreqIdx(const std::string& txt);
87 
88  /** Lookup read counter */
89  size_t lookupReadIdx(const std::string& txt);
90 
91  /** Lookup relation by relation name */
92  const ram::Relation* lookup(const std::string& relName) {
93  auto it = relationMap.find(relName);
94  assert(it != relationMap.end() && "relation not found");
95  return it->second;
96  }
97 
98 public:
99  explicit Synthesiser(ram::TranslationUnit& tUnit) : translationUnit(tUnit) {
101  [&](const ram::Relation& relation) { relationMap[relation.getName()] = &relation; });
102  }
103 
104  virtual ~Synthesiser() = default;
105 
106  /** Get translation unit */
108  return translationUnit;
109  }
110 
111  /** Generate code */
112  void generateCode(std::ostream& os, const std::string& id, bool& withSharedLibrary);
113 };
114 } // namespace souffle::synthesiser
souffle::synthesiser::Synthesiser::generateRelationTypeStruct
void generateRelationTypeStruct(std::ostream &out, Own< Relation > relationType)
Get relation struct definition.
Definition: Synthesiser.cpp:200
souffle::synthesiser::Synthesiser::emitCode
void emitCode(std::ostream &out, const ram::Statement &stmt)
Generate code.
Definition: Synthesiser.cpp:230
souffle::synthesiser::Synthesiser::lookup
const ram::Relation * lookup(const std::string &relName)
Lookup relation by relation name.
Definition: Synthesiser.h:104
souffle::synthesiser::Synthesiser::getRelationName
const std::string getRelationName(const ram::Relation &rel)
Get relation name.
Definition: Synthesiser.cpp:186
Relation.h
souffle::synthesiser::Synthesiser::lookupFreqIdx
unsigned lookupFreqIdx(const std::string &txt)
Lookup frequency counter.
Definition: Synthesiser.cpp:129
souffle::synthesiser::Synthesiser::getRecordTable
const RecordTable & getRecordTable()
Get record table.
souffle::synthesiser::Synthesiser::typeCache
std::set< std::string > typeCache
Cache for generated types for relations.
Definition: Synthesiser.h:69
souffle::synthesiser::Synthesiser::~Synthesiser
virtual ~Synthesiser()=default
souffle::RecordTable
Definition: RecordTable.h:114
souffle::synthesiser::Synthesiser::generateCode
void generateCode(std::ostream &os, const std::string &id, bool &withSharedLibrary)
Generate code.
Definition: Synthesiser.cpp:2260
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
relation
Relation & relation
Definition: Reader.h:130
souffle::synthesiser::Synthesiser::Synthesiser
Synthesiser(ram::TranslationUnit &tUnit)
Definition: Synthesiser.h:111
souffle::synthesiser::Synthesiser::lookupReadIdx
size_t lookupReadIdx(const std::string &txt)
Lookup read counter.
Definition: Synthesiser.cpp:140
souffle::synthesiser::Synthesiser::getTranslationUnit
ram::TranslationUnit & getTranslationUnit()
Get translation unit.
Definition: Synthesiser.h:119
souffle::ram::TranslationUnit::getProgram
Program & getProgram() const
Get the RAM Program of the translation unit
Definition: TranslationUnit.h:107
souffle::ram::TranslationUnit
Translating a RAM program.
Definition: TranslationUnit.h:55
Visitor.h
souffle::synthesiser::Synthesiser::neIdxMap
std::map< std::string, size_t > neIdxMap
Frequency profiling of non-existence checks.
Definition: Synthesiser.h:66
ContainerUtil.h
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
Relation.h
souffle::synthesiser
Souffle - A Datalog Compiler Copyright (c) 2013, 2015, Oracle and/or its affiliates.
Definition: Relation.cpp:22
souffle::synthesiser::Synthesiser::getOpContextName
const std::string getOpContextName(const ram::Relation &rel)
Get context name.
Definition: Synthesiser.cpp:195
souffle::ram::Operation
Abstract class for a relational algebra operation.
Definition: Operation.h:34
souffle::synthesiser::Synthesiser::translationUnit
ram::TranslationUnit & translationUnit
RAM translation unit.
Definition: Synthesiser.h:57
RecordTable.h
TranslationUnit.h
souffle::synthesiser::Synthesiser::convertRamIdent
const std::string convertRamIdent(const std::string &name)
Convert RAM identifier.
Definition: Synthesiser.cpp:153
souffle::synthesiser::Synthesiser::relationMap
std::map< std::string, const ram::Relation * > relationMap
Relation map.
Definition: Synthesiser.h:72
souffle::ram::Statement
Abstract class for RAM statements.
Definition: Statement.h:37
Operation.h
souffle::synthesiser::Synthesiser::idxMap
std::map< std::string, unsigned > idxMap
Frequency profiling of searches.
Definition: Synthesiser.h:63
Statement.h
souffle::synthesiser::Synthesiser::identifiers
std::map< const std::string, const std::string > identifiers
RAM identifier to C++ identifier map.
Definition: Synthesiser.h:60
souffle::synthesiser::Synthesiser::recordTable
RecordTable recordTable
Record Table.
Definition: Synthesiser.h:54
souffle::ram::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the RAM fragments rooted by the given node recursively i...
Definition: Visitor.h:357
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::synthesiser::Synthesiser::getReferencedRelations
std::set< const ram::Relation * > getReferencedRelations(const ram::Operation &op)
Get referenced relations.
Definition: Synthesiser.cpp:212