-
Notifications
You must be signed in to change notification settings - Fork 1
/
software.cpp
198 lines (191 loc) · 9.41 KB
/
software.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
/*
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 <stdlib.h> // atol
#include <string.h>
#include <assert.h>
#include "AtomiccIR.h"
#include "common.h"
typedef struct {
FieldElement field;
ModuleIR *IR; // containing Module
std::string inter; // interface
} SoftwareItem;
static const char *jsonPrefix =
"{\n"
" \"bsvdefines\": [ ],\n"
" \"globaldecls\": [\n"
" { \"dtype\": \"TypeDef\",\n"
" \"tdtype\": { \"name\": \"Bit\", \"params\": [ { \"name\": \"32\" } ] },\n"
" \"tname\": \"SpecialTypeForSendingFd\"\n"
" },\n"
" { \"dtype\": \"TypeDef\",\n"
" \"tdtype\": {\n"
" \"elements\": [ %s ],\n"
" \"name\": \"IfcNames\",\n"
" \"type\": \"Enum\"\n"
" },\n"
" \"tname\": \"IfcNames\"\n"
" }\n"
" ],\n"
" \"interfaces\": [";
static std::map<std::string, SoftwareItem> softwareNameList;
static std::string jsonSep;
static void jsonGenerate(FILE *OStrJ, std::string aname, SoftwareItem &swInfo)
{
fprintf(OStrJ, "%s\n { \"cdecls\": [", jsonSep.c_str());
std::string msep;
ModuleIR *interface = lookupInterface(swInfo.inter);
for (auto MI : interface->methods) {
std::string methodName = MI->name;
if (isRdyName(methodName))
continue;
methodName = baseMethodName(methodName);
std::string psep, retType;
fprintf(OStrJ, "%s\n { \"dname\": \"%s\", \"dparams\": [",
msep.c_str(), methodName.c_str());
for (auto pitem: MI->params) {
fprintf(OStrJ, "%s\n { \"pname\": \"%s\", "
"\"ptype\": { \"name\": \"Bit\", \"params\": [ { "
"\"name\": \"%s\" } ] } }",
psep.c_str(), pitem.name.c_str(), convertType(pitem.type).c_str());
psep = ",";
}
if (MI->type != "") {
char buffer[1000];
sprintf(buffer, ", \"rtype\": { \"name\": \"Bit\", \"params\": [ { "
"\"name\": \"%s\" } ] }", convertType(MI->type).c_str());
retType = buffer;
}
fprintf(OStrJ, "\n ]%s }", retType.c_str());
msep = ",";
}
fprintf(OStrJ, "\n ], \"direction\": \"%d\", \"cname\": \"%s\" }",
swInfo.field.isPtr, aname.c_str());
jsonSep = ",";
}
int generateSoftware(std::list<ModuleIR *> &irSeq, const char *exename, std::string outName)
{
FILE *OStrJ = nullptr;
std::string pipeName = "PipeIn(width=" + convertType("NOCDataH") + ")";
for (auto IR: irSeq) {
ModuleIR *implements = lookupInterface(IR->interfaceName);
if (!implements) {
printf("[%s:%d] interface defintion error: %s\n", __FUNCTION__, __LINE__, IR->interfaceName.c_str());
dumpModule("SOFT", IR);
exit(-1);
}
for (auto interfaceName: implements->softwareName) {
for (auto iitem: implements->interfaces) {
if (iitem.fldName == interfaceName) {
softwareNameList[CBEMangle(iitem.type)] = SoftwareItem{iitem, IR, iitem.type};
}
}
}
}
if (softwareNameList.size() > 0) {
int counter = 5;
std::string enumList, sep;
ModuleIR *IRifc = allocIR("l_top___IFC", true);
ModuleIR *IR = allocIR("l_top");
IR->interfaceName = IRifc->name;
irSeq.push_back(IR);
std::list<std::string> pipeUser;
OStrJ = fopen((outName + ".json").c_str(), "w");
std::map<std::string, bool> dutPresent;
for (auto item: softwareNameList) {
assert(lookupInterface(item.second.IR->interfaceName));
std::string name = "IfcNames_" + item.first + (item.second.field.isPtr ? "H2S" : "S2H");
enumList += sep + "[ \"" + name + "\", \"" + autostr(counter++) + "\" ]";
sep = ", ";
}
fprintf(OStrJ, jsonPrefix, enumList.c_str());
for (auto item: softwareNameList) {
jsonGenerate(OStrJ, item.first, item.second);
std::string dutType = item.second.IR->name;
std::string localName = "DUT__" + dutType;
bool outcall = item.second.field.isPtr;
std::string userTypeName = item.second.inter;
std::string userInterface = item.second.field.fldName;
std::string fieldName = (outcall ? "M2P" : "P2M") + ("__" + userInterface);
ModuleIR *elementinterface = allocIR(fieldName + "___IFC", true);
ModuleIR *element = allocIR((outcall ? "___M2P" : "___P2M") + userTypeName);
element->interfaceName = elementinterface->name;
printf("[%s:%d] outcall %d usertname %s fieldname %s type %s\n", __FUNCTION__, __LINE__, outcall, userTypeName.c_str(), fieldName.c_str(), element->name.c_str());
elementinterface->interfaces.push_back(FieldElement{"method", "", userTypeName, "CLK:nRST", !outcall, false, false, false, ""/*not param*/, false, false, false});
elementinterface->interfaces.push_back(FieldElement{"pipe", "", pipeName, "CLK:nRST", outcall, false, false, false, ""/*not param*/, false, false, false});
IR->fields.push_back(FieldElement{fieldName, "", element->name, "CLK:nRST", false, false, false, false, ""/*not param*/, false, false, false});
IR->interfaceConnect.push_back(InterfaceConnectType{
allocExpr(localName + DOLLAR + userInterface),
allocExpr(fieldName + DOLLAR + "method"), userTypeName, true});
if (!dutPresent[dutType]) {
dutPresent[dutType] = true;
IR->fields.push_back(FieldElement{localName, "", dutType, "CLK:nRST", false, false, false, false, ""/*not param*/, false, false, false});
IRifc->interfaces.push_back(FieldElement{"request", "", pipeName, "CLK:nRST", false/*in*/, false, false, false, ""/*not param*/, false, false, false});
}
if (outcall)
pipeUser.push_back(fieldName + DOLLAR + "pipe");
else {
if (auto userIR = lookupInterface(userTypeName)) {
for (auto MI: userIR->methods) {
std::string methodName = MI->name;
if (!isRdyName(methodName) && !isEnaName(methodName)) {
// see if the request deserialization can generate an indication
// actionValue methods get a callback for value
elementinterface->interfaces.push_back(FieldElement{"returnInd", "", pipeName, "CLK:nRST", true, false, false, false, ""/*not param*/, false, false, false});
pipeUser.push_back(fieldName + DOLLAR + "returnInd");
break;
}
}
}
IR->interfaceConnect.push_back(InterfaceConnectType{
allocExpr(fieldName + DOLLAR + "pipe"),
allocExpr("request"), pipeName, true});
}
}
fprintf(OStrJ, "\n ]\n}\n");
fclose(OStrJ);
if (int size = pipeUser.size()) {
IRifc->interfaces.push_back(FieldElement{"indication", "", pipeName, "CLK:nRST", true/*out*/, false, false, false, ""/*not param*/, false, false, false});
if (size == 1)
IR->interfaceConnect.push_back(InterfaceConnectType{allocExpr("indication"),
allocExpr(pipeUser.front()), pipeName, true});
else {
IR->fields.push_back(FieldElement{"funnel", "", "FunnelBufferedBase(funnelWidth="
+ autostr(pipeUser.size()) + ",dataWidth=" + convertType("NOCDataH") + ")", "CLK:nRST", false, false, false, false, ""/*not param*/, false, false, false});
int ind = 0;
for (auto inter: pipeUser) {
IR->interfaceConnect.push_back(InterfaceConnectType{allocExpr(inter),
str2tree("funnel$in[" + autostr(ind) + "]"), pipeName, true});
ind++;
}
IR->interfaceConnect.push_back(InterfaceConnectType{allocExpr("indication"),
allocExpr("funnel$out"), pipeName, true});
}
}
//dumpModule("TOP", IR);
std::string commandLine(exename);
int ind = commandLine.rfind("/");
if (ind == -1)
commandLine = "";
else
commandLine = commandLine.substr(0,ind+1);
commandLine += "scripts/atomiccCppgen.py " + outName + ".json";
int ret = system(commandLine.c_str());
printf("[%s:%d] RETURN from '%s' %d\n", __FUNCTION__, __LINE__, commandLine.c_str(), ret);
if (ret)
return -1; // force error return to be propagated
}
return 0;
}