-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsdt_engine.cc
More file actions
224 lines (196 loc) · 6.28 KB
/
sdt_engine.cc
File metadata and controls
224 lines (196 loc) · 6.28 KB
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
/*************************************************************************
*
* Copyright (c) 2024 Rajit Manohar
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
**************************************************************************
*/
#include "synth.h"
#include "sdt/basicsdt.h"
#include "sdt/externoptsdt.h"
#include "opt/chp-opt.h"
#include "engines.h"
/**
* SDT Engine
*/
static BasicSDT *_pending = NULL;
static void kill_mapper_on_exit (void)
{
if (_pending) {
delete _pending;
}
_pending = NULL;
}
class SDTSynth : public ActSynthesize {
public:
SDTSynth (const char *prefix,
char *infile,
char *outfile,
char *exprfile)
: ActSynthesize (prefix, infile, outfile, exprfile) {
if (!exprfile) {
fatal_error ("SDT synthesis: requires an expression file!");
}
}
void emitTopImports(ActPass *ap) {
ActDynamicPass *dp = dynamic_cast <ActDynamicPass *> (ap);
Assert (dp, "Hmm");
int bundled_data = dp->getIntParam ("bundled_dpath");
/* print imports */
if (bundled_data) {
pp_printf_raw (_pp, "import \"syn/bdopt/_all_.act\";\n");
}
else {
pp_printf_raw (_pp, "import \"syn/qdi/_all_.act\";\n");
}
pp_printf_raw (_pp, "import \"%s\";\n", _ename);
// open the operating namespace
pp_printf_raw (_pp, "open syn;\n");
pp_forced (_pp, 0);
pp_forced (_pp, 0);
fprintf (_expr, "namespace syn {\n\nexport namespace expr {\n\n");
fclose (_expr);
_expr = NULL;
}
void typeInt (char *buf, int sz, int bitwidth) {
snprintf (buf, sz, "syn::sdtvar<%d>", bitwidth);
}
void typeBool (char *buf, int sz) {
snprintf (buf, sz, "syn::sdtboolvar");
}
void typeIntChan (char *buf, int sz, int bitwidth) {
snprintf (buf, sz, "syn::sdtchan<%d>", bitwidth);
}
void typeBoolChan (char *buf, int sz) {
snprintf (buf, sz, "syn::sdtboolchan");
}
bool chpopt_option;
void runPreSynth (ActPass *ap, Process *p) {
ActDynamicPass *dp = dynamic_cast <ActDynamicPass *> (ap);
Assert (dp, "What?");
chpopt_option = dp->getIntParam ("chp_optimize");
}
bool skipOverride (ValueIdx *vx) {
if (!chpopt_option) {
return false;
}
if (TypeFactory::isIntType (vx->t) ||
TypeFactory::isBoolType (vx->t) ||
TypeFactory::isStructure (vx->t)) {
return true;
}
return false;
}
void processStruct (Data *d) {
int w = TypeFactory::totBitWidth (d);
Assert (w>=0, "What");
fprintf(_pp->fp, "\n// Total Bitwidth : %d\n", w);
char name[10240];
char mname[10240];
std::string ns = "";
if (d->getns() && d->getns() != ActNamespace::Global()) {
ns = d->getns()->Name(true);
}
d->snprintActName(name, 10240);
ns = ns + name;
ActNamespace::Act()->msnprintf(mname, 10240, ns.c_str());
const char *scn = config_get_string("synth.struct_chan_name");;
fprintf(_pp->fp, "defchan chan_%s <: chan(%s) (sdtchan<%d> %s) {}\n\n",
mname, ns.c_str(), w, scn);
}
void typeStructChan (char *buf, int sz, InstType *t) {
InstType *td = TypeFactory::getChanDataType(t);
Assert (td, "What");
char name[10240];
td->sPrint(name, 10240);
char mname[10240];
std::string ns = "";
if (td->getNamespace() && td->getNamespace() != ActNamespace::Global()) {
ns = td->getNamespace()->Name(true);
}
ns = ns + name;
ActNamespace::Act()->msnprintf(mname, 10240, ns.c_str());
snprintf (buf, sz, "chan_%s", mname);
}
void runSynth (ActPass *ap, Process *p) {
pp_printf (_pp, "/* synthesis output */");
pp_forced (_pp, 0);
int chpopt, externopt, bundled;
BasicSDT *sdt;
ActDynamicPass *dp;
dp = dynamic_cast <ActDynamicPass *> (ap);
Assert (dp, "What?");
chpopt = dp->getIntParam ("chp_optimize");
externopt = dp->getIntParam ("externopt");
bundled = dp->getIntParam ("bundled_dpath");
// convert this to chpgraph format
// run optimizations
// convert back to ACT data structures
if (p->getlang() && p->getlang()->getchp()) {
if (chpopt) {
auto g = ChpOptimize::chp_graph_from_act (p->getlang()->getchp()->c,
p->CurScope ());
ChpOptimize::optimize_chp_O2 (g.graph, p->getName(), false);
uninlineBitfieldExprsHack (g.graph);
std::vector<ActId *> newnames;
act_chp_lang_t *l = chp_graph_to_act (g, newnames, p->CurScope());
p->getlang()->getchp()->c = l;
for (auto id : newnames) {
InstType *it = p->CurScope()->Lookup (id->getName());
if (TypeFactory::isBoolType (it)) {
pp_printf (_pp, "syn::sdtboolvar %s;", id->getName());
pp_forced (_pp, 0);
}
else {
pp_printf (_pp, "syn::sdtvar<%d> %s;",
TypeFactory::bitWidth (it), id->getName());
pp_forced (_pp, 0);
}
}
}
else {
//ChpOptimize::optimize_chp_O0 (g.graph, p->getName(), false);
//ChpOptimize::eliminateDeadCode (g.graph);
}
}
pp_flush (_pp);
fprintf (_pp->fp, "/* start sdt */\n");
fflush (_pp->fp);
if (externopt) {
sdt = new ExternOptSDT (bundled, chpopt, _pp->fp, _ename,
(char *) dp->getPtrParam ("externopt_toolname"));
_pending = sdt;
}
else {
if (bundled) {
fatal_error ("Bundled-data not supported is Basic mode");
}
sdt = new BasicSDT (bundled, chpopt, _pp->fp, _ename);
_pending = sdt;
}
atexit (kill_mapper_on_exit);
sdt->run_sdt (p, 0);
kill_mapper_on_exit ();
fprintf (_pp->fp, "/* end sdt */\n");
pp_forced (_pp, 0);
}
};
ActSynthesize *gen_sdt_engine (const char *prefix, char *infile,
char *outfile, char *exprfile)
{
return new SDTSynth (prefix, infile, outfile, exprfile);
}