-
Notifications
You must be signed in to change notification settings - Fork 1
/
metaGen.cpp
153 lines (149 loc) · 7.01 KB
/
metaGen.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
/*
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"
void metaGenerateModule(ModuleIR *IR, FILE *OStr)
{
std::map<std::string, int> exclusiveSeen;
std::list<std::string> metaList;
// write out metadata comments at end of the file
std::string name = IR->name;
int ind = name.find("(");
if (ind > 0)
name = name.substr(0, ind);
metaList.push_front("//METASTART; " + name);
for (auto item: IR->interfaces)
if (item.isPtr)
metaList.push_back("//METAEXTERNAL; " + item.fldName + "; " + lookupInterface(item.type)->name + ";");
for (auto item: IR->fields) {
std::string vecCount = item.vecCount;
int dimIndex = 0;
std::string pvec;
if (lookupIR(item.type))
do {
std::string fldName = item.fldName;
if (vecCount != "")
fldName += autostr(dimIndex++);
if (item.isPtr)
metaList.push_back("//METAEXTERNAL; " + fldName + "; " + lookupIR(item.type)->name + ";");
else if (!lookupIR(item.type)->isStruct && !lookupIR(item.type)->isInterface)
metaList.push_back("//METAINTERNAL; " + fldName + "; " + lookupIR(item.type)->name + ";");
pvec = autostr(atoi(vecCount.c_str()) - 1);
if (vecCount == "" || pvec == "0" || !isdigit(vecCount[0])) pvec = "";
if(vecCount != GENERIC_INT_TEMPLATE_FLAG_STRING) vecCount = pvec;
} while(vecCount != GENERIC_INT_TEMPLATE_FLAG_STRING && pvec != "");
}
for (auto MI : IR->methods) {
std::string methodName = MI->name;
std::string gtemp = "; " + tree2str(MI->guard) + ";";
if (isRdyName(methodName))
metaList.push_back("//METAGUARD; "
+ baseMethodName(methodName) + gtemp);
else if (endswith(methodName, "__READY"))
metaList.push_back("//METAGUARDV; "
+ methodName.substr(0, methodName.length()-7) + gtemp);
else {
// For each method/rule of the current class,
// gather up metadata generated by processFunction
MetaRef *bm = MI->meta;
std::string temp;
for (auto titem: bm[MetaInvoke])
for (auto item: titem.second)
temp += item + ":" + titem.first + ";";
if (temp != "")
metaList.push_back("//METAINVOKE; " + methodName + "; " + temp);
std::map<std::string,std::string> metaBefore;
std::map<std::string,std::string> metaConflict;
for (auto innerMI : IR->methods) {
std::string innermethodName = innerMI->name;
MetaRef *innerbm = innerMI->meta;
std::string tempConflict;
if (innermethodName == methodName)
continue;
// scan all other rule/methods of this class
for (auto inneritem: innerMI->storeList) {
for (auto item: bm[MetaRead])
// if the current method reads a state element that
// is written by another method, add it to the 'before' list
if (item.first == inneritem->dest->value) {
//printf("[%s:%d] innermethodName %s before conflict '%s' innerunc %s methodName %s\n", __FUNCTION__, __LINE__, innermethodName.c_str(), item.first.c_str(), innermethodName.c_str(), methodName.c_str());
metaBefore[innermethodName] = "; :";
break;
}
for (auto item: MI->storeList)
// if the current method writes a state element that
// is written by another method, add it to the 'conflict' list
if (tree2str(item->dest) == tree2str(inneritem->dest)) {
metaConflict[innermethodName] = "; ";
break;
}
}
for (auto inneritem: innerbm[MetaInvoke]) {
for (auto item: bm[MetaInvoke])
if (item.first == inneritem.first) {
//printf("[%s:%d] conflict methodName %s innermethodName %s item %s\n", __FUNCTION__, __LINE__, methodName.c_str(), innermethodName.c_str(), item.first.c_str());
metaConflict[innermethodName] = "; ";
break;
}
}
}
std::string metaStr;
for (auto item: metaConflict)
if (item.second != "" && !exclusiveSeen[item.first])
metaStr += item.second + item.first;
exclusiveSeen[methodName] = 1;
if (metaStr != "")
metaList.push_back("//METAEXCLUSIVE; " + methodName + metaStr);
metaStr = "";
for (auto item: metaBefore)
if (item.second != "")
metaStr += item.second + item.first;
if (metaStr != "")
metaList.push_back("//METABEFORE; " + methodName + metaStr);
}
}
std::string ruleNames;
for (auto MI : IR->methods) {
std::string methodName = MI->name;
if (MI->isRule && MI->action)
ruleNames += "; " + baseMethodName(methodName);
}
if (ruleNames != "")
metaList.push_back("//METARULES" + ruleNames);
for (auto item: IR->interfaceConnect) {
std::string tname = tree2str(item.target);
std::string sname = tree2str(item.source);
std::string iname = item.type;
if (startswith(iname, "ARRAY_")) {
iname = iname.substr(6);
int ind = iname.find(ARRAY_ELEMENT_MARKER);
if (ind > 0)
iname = iname.substr(ind+1);
}
//printf("[%s:%d] METACONNECT %s %s\n", __FUNCTION__, __LINE__, tname.c_str(), sname.c_str());
for (auto MI: lookupInterface(iname)->methods) {
std::string methodName = MI->name;
metaList.push_back("//METACONNECT; " + tname + PERIOD + MI->name
+ "; " + sname + PERIOD + MI->name);
}
}
for (auto item : IR->priority)
metaList.push_back("//METAPRIORITY; " + item.first + "; " + item.second);
for (auto item : metaList)
fprintf(OStr, "%s\n", item.c_str());
}