/
PrologExporter.h
347 lines (273 loc) · 8.46 KB
/
PrologExporter.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef PROLOG_EXPORTER_H
#define PROLOG_EXPORTER_H
#include <iostream>
#include <vector>
#include <sstream>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include "Visitor.h"
#include "ExprVisitor.h"
class PrologExporter;
using namespace std;
class FoldNames;
class FoldNamesExtended;
class UninterpretedFunction;
class PrologExporter : public Visitor
{
public:
PrologExporter(ostream &o, Module* m) :
out(o), module(m), moduleInstantiation(NULL),
parentExporter(NULL)
{ }
PrologExporter(PrologExporter* pe, Module* m, PGModule* mi) :
out(pe->out), parentModules(pe->parentModules),
module(m), moduleInstantiation(mi),
parentExporter(pe)
{
ModulePair mp;
mp.module = pe->module;
mp.moduleInstantiation = pe->moduleInstantiation;
this->parentModules.push_back(mp);
}
void visit(Module*) ;
void visit(PWire*) ;
void visit(PGate*) ;
void visit(PGAssign*) ;
void visit(PGBuiltin*) ;
void visit(PGModule*) ;
void visit(Statement*) ;
void visit(PProcess*) ;
void visit(PEventStatement*) ;
void visit(PCondit*) ;
void visit(PAssign*) ;
void visit(PAssignNB*) ;
void visit(PBlock*) ;
void visit(PCase*) ;
void visit(PCallTask*) ;
void visit(PForStatement*) ;
void visit(PExpr*) ;
void visit(PEIdent*) ;
void visit(PETernary*) ;
void visit(PEConcat*) ;
void visit(PECallFunction*) ;
void visit(PENumber*) ;
void visit(PEEvent*) ;
void visit(PEBinary*) ;
void visit(PEUnary*) ;
void visit(PEString*) ;
void visit(data_type_t*) ;
void visit(PDelays*) ;
typedef struct ModulePair {
Module* module;
PGModule* moduleInstantiation;
} ModulePair;
// private:
unsigned ind = 0;
vector<UninterpretedFunction> ufs;
ostream& out;
// first pair's inst is always NULL
vector<ModulePair> parentModules;
bool isToplevel() const {
return parentModules.empty();
}
Module* module;
PGModule* moduleInstantiation;
unordered_map<string,PExpr*> portMapping;
string export_pform_name_to_prolog(const pform_name_t&that);
string export_name_component_to_prolog(const perm_string&, const std::list<index_component_t>&);
void export_pins_to_prolog(PGate* g);
string exportExpr(PExpr*) ;
void printModuleName(ostream&);
void printModuleHeader(ostream&);
static unsigned long idCounter;
enum AssignType {ContAsgn, BlockingAsgn, NonBlockingAsgn};
private:
void printModInstPortAssignments();
PrologExporter* parentExporter;
typedef struct {PrologExporter* pe; PExpr* e;} AsgnArg;
void doAsgn(AssignType, AsgnArg, AsgnArg);
void doAsgn(AssignType a, PExpr* lhs, PExpr* rhs) {
return doAsgn(a, {this, lhs}, {this, rhs});
}
};
extern const string id_prefix;
class FoldNames : public ExprVisitor
{
public:
FoldNames(PrologExporter* _pe) : pe(_pe) {}
void visit(PEIdent* id) {
auto itr = id->path().begin();
string s = pe->export_name_component_to_prolog((*itr).name, (*itr).index);
++itr;
if (itr != id->path_.end()) {
cerr << endl << "NOT SUPPORTED: " << typeid(*this).name() << " @ ";
cerr << "multiple name components: " << id->path_ << endl;
exit(1);
}
names.insert(s);
}
void visit(PETernary* ter) {
ter->expr_->accept(this);
ter->tru_->accept(this);
ter->fal_->accept(this);
}
void visit(PEConcat* cat) {
for(auto itr = cat->parms_.begin(); itr != cat->parms_.end(); ++itr) {
if (*itr) {
(*itr)->accept(this);
}
}
}
void visit(PECallFunction* cf) {
for(auto itr = cf->parms_.begin(); itr != cf->parms_.end(); ++itr) {
if (*itr) {
(*itr)->accept(this);
}
}
}
void visit(PENumber*) {}
void visit(PEEvent*) {}
void visit(PEString*) {}
void visit(PEBinary* be) {
be->left_->accept(this);
be->right_->accept(this);
}
void visit(PEUnary* ue) {
ue->expr_->accept(this);
}
void print_names(ostream& out) {
if (! names.empty()) {
auto itr = names.begin();
out << (*itr);
++itr;
for (; itr != names.end(); ++itr) {
out << ", " << (*itr);
}
}
}
PrologExporter* pe;
unordered_set<string> names;
};
class FoldNamesExtended : public FoldNames {
public:
FoldNamesExtended(PrologExporter* _pe) : FoldNames(_pe) { }
void visit(PForStatement* o) {
if (o->statement_) {
o->statement_->accept(this);
}
}
void visit(PCondit*o) {
o->expr_->accept(this);
if (o->if_) {
o->if_->accept(this);
}
if (o->else_) {
o->else_->accept(this);
}
}
void visit(PAssign* ba) {
if (ba->lval_) {
ba->lval_->accept(this);
}
if (ba->rval_) {
ba->rval_->accept(this);
}
}
void visit(PAssignNB* nba){
if (nba->lval_) {
nba->lval_->accept(this);
}
if (nba->rval_) {
nba->rval_->accept(this);
}
}
void visit(PBlock* b) {
for (unsigned idx = 0; idx < b->list_.size(); idx += 1) {
if (b->list_[idx]) {
b->list_[idx]->accept(this);
}
}
}
};
class UninterpretedFunction {
public:
UninterpretedFunction(string& i) {
this->output = get_unique_name(i);
this->inputs.push_back(i);
}
UninterpretedFunction(string& i1, string& i2) {
this->output = get_unique_name(i1);
this->inputs.push_back(i1);
this->inputs.push_back(i2);
}
UninterpretedFunction(const string& functionName, unordered_set<string>::const_iterator begin, unordered_set<string>::const_iterator end) {
if (begin == end) {
this->output = get_unique_name("const_expr");
return;
}
this->output = get_unique_name(functionName);
while (begin != end) {
this->inputs.push_back(*begin);
++begin;
}
}
UninterpretedFunction(unordered_set<string>::const_iterator begin, unordered_set<string>::const_iterator end) :
UninterpretedFunction(*begin, begin, end) { }
UninterpretedFunction(PrologExporter* pe, PExpr* expr) {
assert(expr != NULL);
FoldNames fn(pe);
if (PECallFunction *cf = dynamic_cast<PECallFunction *>(expr); cf != NULL) {
this->isVerilogFunction = true;
this->verilogFunction = pe->export_pform_name_to_prolog(cf->path_);
this->output = get_unique_name(this->verilogFunction);
for(auto parmE : cf->parms_) {
parmE->dump(cerr); cerr<<endl;
this->inputs.push_back(pe->exportExpr(parmE));
}
} else {
expr->accept(&fn);
this->output = get_unique_name("");
auto begin = fn.names.begin();
auto end = fn.names.end();
if (begin != end) {
while (begin != end) {
this->inputs.push_back(*begin);
++begin;
}
} else {
this->output = get_unique_name("const_expr");
}
}
}
void export_to_prolog(ostream& out) {
if (isVerilogFunction)
out << "linkF(" << output << ", " << verilogFunction << ", [";
else
out << "link(" << output << ", [";
if (inputs.size() > 0) {
out << inputs[0];
for (unsigned i = 1; i < inputs.size(); ++i) {
out << ", " << inputs[i];
}
}
out << "])";
}
string getOutput() {
return this->output;
}
private:
string output;
vector<string> inputs;
bool isVerilogFunction = false;
string verilogFunction = "";
static unsigned long name_counter;
string get_unique_name(const string& s) {
ostringstream oss;
oss << "uf_" << name_counter++;
if (s.size() > 0)
oss << "_" << s;
return oss.str();
}
};
#endif // PROLOG_EXPORTER_H