-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkami.cpp
268 lines (257 loc) · 10.8 KB
/
kami.cpp
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
/*
Copyright (C) 2018, The Connectal Project
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
//#include <string.h>
//#include <assert.h>
#include "AtomiccIR.h"
#include "common.h"
static std::string kamiType(std::string type)
{
std::string ret = type;
int i = ret.find("INTEGER_");
if (i == 0) {
ret = "(Bit " + ret.substr(i + 8) + ")";
} else {
int i = ret.find("(");
if (i > 0)
ret = ret.substr(0, i) + " " + ret.substr(i + 1, ret.length() - i - 2);
}
return ret;
}
static int kamiWidth(std::string type)
{
std::string ktype = kamiType(type);
int i = ktype.find(" ");
if (i > 0)
return atoi(ktype.substr(i+1).c_str());
return 32;
}
static ACCExpr *kamiChanges(ACCExpr *expr, int width)
{
std::string value = expr->value;
if (isIdChar(value[0]))
value = "#" + value + "_v";
else if (isdigit(value[0]))
value = "$$ (* intwidth *) (natToWord " + autostr(width) + " " + value + ")";
auto ext = allocExpr(value);
for (auto item: expr->operands)
ext->operands.push_back(kamiChanges(item, width));
return ext;
}
static std::string kamiValue(ACCExpr *expr, int width = 32)
{
return tree2str(kamiChanges(expr, width));
}
static std::string kamiCall(ACCExpr *expr, MethodInfo *MI)
{
//dumpExpr("KCALL", expr);
std::string ret;
if (expr->value == PERIOD) {
ret = getRHS(expr, 0)->value + "'";
expr = getRHS(expr, 1);
}
ret += expr->value + " (";
expr = expr->operands.front();
if (expr->value == "(") {
std::string sep;
auto param = MI->params.begin();
for (auto item: expr->operands) {
printf("[%s:%d] param type %s = %s\n", __FUNCTION__, __LINE__, param->type.c_str(), kamiType(param->type).c_str());
dumpExpr("PARAM", item);
ret += sep + "(" + kamiValue(item) + ") : " + kamiType(param->type);
sep = ", ";
param++;
}
}
return ret + ")";
}
static void generateInterfaces(FILE *OStrV)
{
for (auto item: interfaceIndex) {
std::list<std::string> hints;
std::string name = item.first;
ModuleIR *IR = item.second;
//printf("[%s:%d] interface %s = %s\n", __FUNCTION__, __LINE__, name.c_str(), IR->name.c_str());
fprintf(OStrV, "(* * interface %s *)\nRecord %s := {\n %s'mod: Mod;\n",
IR->name.c_str(), name.c_str(), name.c_str());
for (auto MI: IR->methods) {
std::string methodName = MI->name;
char buf[200];
if (isRdyName(methodName))
continue;
snprintf(buf, sizeof(buf), "Hint Unfold %s'%s : ModuleDefs.", name.c_str(), methodName.c_str());
hints.push_back(buf);
fprintf(OStrV, " %s'%s : string;\n", name.c_str(), methodName.c_str());
}
fprintf(OStrV, "}.\n\n");
for (auto iitem: hints)
fprintf(OStrV, "%s\n", iitem.c_str());
}
}
static void generateModule(FILE *OStrV, ModuleIR *IR)
{
std::string name = IR->name;
int i = name.find("(");
if (i > 0)
name = name.substr(0, i);
//printf("[%s:%d] module '%s'\n", __FUNCTION__, __LINE__, IR->name.c_str());
fprintf(OStrV, "\nModule module'mk%s.\n", name.c_str());
fprintf(OStrV, " Section Section'mk%s.\n", name.c_str());
fprintf(OStrV, " Variable instancePrefix: string.\n"
" (* method bindings *)\n");
std::list<std::string> letList;
std::map<std::string, std::string> fieldType;
for (auto iitem: IR->fields) {
fieldType[iitem.fldName] = iitem.type;
std::string tname = iitem.type;
int i = tname.find("(");
if (i > 0)
tname = tname.substr(0, i);
//printf("[%s:%d] tname %s\n", __FUNCTION__, __LINE__, tname.c_str());
auto IIR = lookupIR(tname);
if (IIR) {
fprintf(OStrV, " Let (* action binding *) %s := mk%s (instancePrefix--\"%s\").\n",
iitem.fldName.c_str(), tname.c_str(), iitem.fldName.c_str());
for (auto interfaceItem: IIR->interfaces) {
auto interfaceIR = lookupInterface(interfaceItem.type);
for (auto MI: interfaceIR->methods) {
std::string methodName = MI->name;
if (isRdyName(methodName))
continue;
char buf[200];
snprintf(buf, sizeof(buf), " Let %s'%s : string := (%s'%s %s).", iitem.fldName.c_str(),
methodName.c_str(), tname.c_str(), methodName.c_str(), iitem.fldName.c_str());
letList.push_back(buf);
}
}
}
else
fprintf(OStrV, " Let %s : string := instancePrefix--\"%s\".\n",
iitem.fldName.c_str(), iitem.fldName.c_str());
}
fprintf(OStrV, " (* instance methods *)\n");
for (auto iitem: letList)
fprintf(OStrV, "%s\n", iitem.c_str());
std::string interfaceName = "Empty";
for (auto iitem: IR->interfaces) {
//auto II = lookupInterface(iitem.type);
//printf("[%s:%d] interfacename %s lookup %s\n", __FUNCTION__, __LINE__, iitem.type.c_str(), II ? II->name.c_str() : "none");
interfaceName = iitem.fldName;
}
fprintf(OStrV, " Local Open Scope kami_expr.\n\n");
fprintf(OStrV, " Definition mk%sModule: Mod :=\n", name.c_str());
fprintf(OStrV, " (BKMODULE {\n");
std::string sep = " ";
for (auto iitem: IR->fields) {
std::string tname = iitem.type;
int i = tname.find("(");
if (i > 0)
tname = tname.substr(0, i);
//printf("[%s:%d] tname %s\n", __FUNCTION__, __LINE__, tname.c_str());
auto IIR = lookupIR(tname);
if (IIR)
fprintf(OStrV, " %s (BKMod (%s'mod %s :: nil))\n",
sep.c_str(), tname.c_str(), iitem.fldName.c_str());
else
fprintf(OStrV, " %s Register (instancePrefix--\"%s\") : %s <- Default\n",
sep.c_str(), iitem.fldName.c_str(), kamiType(iitem.type).c_str());
sep = "with";
}
for (auto MI: IR->methods) {
std::string methodName = MI->name;
if (isRdyName(methodName))
continue;
MethodInfo *MIRdy = lookupMethod(IR, getRdyName(methodName, MI->async));
fprintf(OStrV, " %s %s instancePrefix--\"%s\" :=\n (\n", sep.c_str(),
MI->isRule ? "Rule" : "Method", methodName.c_str());
for (auto pitem: MI->params) {
fprintf(OStrV, " Read %s_v: %s <- (instancePrefix--\"%s\") ;\n",
pitem.name.c_str(), kamiType(pitem.type).c_str(), pitem.name.c_str());
}
if (MIRdy)
fprintf(OStrV, " Assert(%s);\n", kamiValue(cleanupBool(MIRdy->guard), 1).c_str());
int unusedNumber = 1;
for (auto citem: MI->callList) {
MethodInfo *callMI = nullptr;
if (citem->value->value == PERIOD) {
for (auto fitem: IR->fields)
if (fitem.fldName == getRHS(citem->value, 0)->value) {
ModuleIR *CIR = lookupIR(fitem.type);
callMI = lookupMethod(CIR, getRHS(citem->value, 1)->value);
}
}
fprintf(OStrV, " BKCall unused%d : Void %s <- %s ;\n",
unusedNumber++, citem->isAction ? "(* actionBinding *)" : "",
kamiCall(citem->value, callMI).c_str());
}
for (auto sitem: MI->storeList) {
std::string dest = tree2str(sitem->dest);
std::string dtype = fieldType[dest];
if (dtype == "")
dtype = "Bit(32)";
fprintf(OStrV, " Write (instancePrefix--\"%s\") : %s <- %s ;\n",
dest.c_str(), kamiType(dtype).c_str(), kamiValue(sitem->value, kamiWidth(dtype)).c_str());
}
if (MI->isRule)
fprintf(OStrV, " Retv ) (* rule %s *)\n", methodName.c_str());
else
fprintf(OStrV, " Retv )\n");
sep = "with";
}
fprintf(OStrV, "\n }). (* mk%s *)\n\n", name.c_str());
fprintf(OStrV, " Hint Unfold mk%sModule : ModuleDefs.\n", name.c_str());
std::string mtype = IR->name;
if (mtype == "Empty")
mtype = "Main"; // HACK
std::string interfaceNames;
for (auto interfaceItem: IR->interfaces) {
auto interfaceIR = lookupInterface(interfaceItem.type);
for (auto MI: interfaceIR->methods) {
std::string methodName = MI->name;
if (isRdyName(methodName))
continue;
char buf[200];
snprintf(buf, sizeof(buf), " (instancePrefix--\"%s\")", methodName.c_str());
interfaceNames += buf;
}
}
fprintf(OStrV, "(* Module mk%s type Module#(%s) return type %s *)\n", name.c_str(), mtype.c_str(), mtype.c_str());
fprintf(OStrV, " Definition mk%s := Build_%s mk%sModule%s.\n", name.c_str(), name.c_str(), name.c_str(), interfaceNames.c_str());
fprintf(OStrV, " Hint Unfold mk%s : ModuleDefs.\n", name.c_str());
fprintf(OStrV, " Hint Unfold mk%sModule : ModuleDefs.\n\n", name.c_str());
fprintf(OStrV, " End Section'mk%s.\n", name.c_str());
fprintf(OStrV, "End module'mk%s.\n\n", name.c_str());
fprintf(OStrV, "Definition mk%s := module'mk%s.mk%s.\n", name.c_str(), name.c_str(), name.c_str());
fprintf(OStrV, "Hint Unfold mk%s : ModuleDefs.\n", name.c_str());
fprintf(OStrV, "Hint Unfold module'mk%s.mk%s : ModuleDefs.\n", name.c_str(), name.c_str());
fprintf(OStrV, "Hint Unfold module'mk%s.mk%sModule : ModuleDefs.\n", name.c_str(), name.c_str());
}
void generateKami(std::list<ModuleIR *> &irSeq, std::string myName, std::string OutputDir)
{
FILE *OStrV = fopen((OutputDir + ".kami").c_str(), "w");
if (!OStrV) {
printf("kamigen: unable to open '%s'\n", (OutputDir + ".kami").c_str());
exit(-1);
}
fprintf(OStrV, "Require Import Bool String List Arith.\n"
"Require Import Omega.\n"
"Require Import Kami.All.\n"
"Require Import Bsvtokami.\n\n"
"Require Import FunctionalExtensionality.\n\n"
"Set Implicit Arguments.\n\n\n");
generateInterfaces(OStrV);
for (auto IR : irSeq)
generateModule(OStrV, IR);
fclose(OStrV);
}