-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProcessModule.cs
256 lines (240 loc) · 10.2 KB
/
ProcessModule.cs
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
using System.Text;
using System.Xml;
namespace OpenKNXproducer
{
public class ProcessModule
{
private static Dictionary<string, ProcessModule> sModule = new Dictionary<string, ProcessModule>();
private string mName;
private DefineContent mDefine;
private int mChannel;
private XmlNode mRootNode;
private ProcessInclude mInclude;
private int mModuleCount = 0;
private int mModuleParamSize = 0;
private ProcessModule mParentModule = null;
private List<ProcessModule> mSubmodules = null;
private Dictionary<XmlNode, int> mModuleInstance = new Dictionary<XmlNode, int>();
public int ModuleCount
{
get { return mModuleCount; }
}
public bool IsSubmodule
{
get
{
return (mParentModule != null);
}
}
protected ProcessModule(DefineContent iDefine, int iChannel, XmlNode iRootNode, ProcessInclude iInclude)
{
mDefine = iDefine;
mChannel = iChannel;
mRootNode = iRootNode;
mInclude = iInclude;
}
// Adds a ModuleDef to the collection
public static ProcessModule ProcessModuleFactory(DefineContent iDefine, int iChannel, XmlNode iRootNode, ProcessInclude iInclude)
{
ProcessModule lModule = new ProcessModule(iDefine, iChannel, iRootNode, iInclude);
string lId = iRootNode.NodeAttr("Id");
lModule.mName = iRootNode.NodeAttr("Name");
sModule.Add(lId, lModule);
return lModule;
}
public static ProcessModule ModuleById(string iId)
{
if (sModule.ContainsKey(iId))
return sModule[iId];
else
return null;
}
// adds a Module-Reference as UsageCounter
public static void ProcessModuleInstance(DefineContent iDefine, int iChannel, XmlNode iRootNode, ProcessInclude iInclude)
{
// check for Repeat
int lCount = 0;
if (iRootNode.ParentNode.Name == "Repeat")
{
lCount = int.Parse(iRootNode.ParentNode.NodeAttr("Count", "0"));
}
ProcessModule lModule = ModuleById(iRootNode.NodeAttr("RefId"));
if (lModule != null)
{
// for module count lCount == 0 is interpreted as 1
lModule.mModuleCount += (lCount == 0) ? 1 : lCount;
// we store all instances for postprocessing of start-values
// lCount == 0 indicates an unrepeated node, lCount > 0 a repeated one
lModule.mModuleInstance.Add(iRootNode, lCount);
}
}
private void GetSubmoduleList()
{
if (mSubmodules == null)
{
mSubmodules = new List<ProcessModule>();
XmlNodeList lSubmodules = mRootNode.SelectNodes("./SubModuleDefs/ModuleDef");
if (lSubmodules != null)
{
foreach (XmlNode lSubmodule in lSubmodules)
{
ProcessModule lModule = ProcessModule.ModuleById(lSubmodule.NodeAttr("Id"));
lModule.mParentModule = this;
mSubmodules.Add(lModule);
}
}
}
}
public int SubmodulesParamSize
{
get
{
int lResult = 0;
GetSubmoduleList();
foreach (ProcessModule lModule in mSubmodules)
{
lResult += lModule.ModuleParamSize * lModule.ModuleCount;
}
return lResult;
}
}
public int FullParamSize
{
get
{
return ModuleParamSize + SubmodulesParamSize;
}
}
public int ModuleParamSize
{
get
{
if (mModuleParamSize == 0)
{
// we check if we have a predefined size
XmlNode lArgParams = mRootNode.SelectSingleNode("./Arguments/Argument[@Name='argParam']");
XmlNode lAllocates = null;
if (lArgParams != null)
{
lAllocates = lArgParams.Attributes.GetNamedItem("Allocates");
if (lAllocates == null)
{
lAllocates = lArgParams.OwnerDocument.CreateAttribute("Allocates");
lAllocates.Value = "%ModuleSize%";
lArgParams.Attributes.SetNamedItem(lAllocates);
}
if (lAllocates.Value == "%ModuleSize%")
{
// we have to calculate module parameter size
XmlNodeList lParams = mRootNode.SelectNodes("./Static/Parameters/Parameter|./Static/Parameters/Union");
// mModuleParamSize = CalcSubmodulesSize();
mModuleParamSize = mInclude.CalcParamSize(lParams);
lAllocates.Value = (mModuleParamSize + SubmodulesParamSize).ToString();
}
else
{
// we have to take the predefined size
mModuleParamSize = int.Parse(lAllocates.Value);
}
}
}
return mModuleParamSize;
}
}
private void UpdateModuleInstnces(int iParamOffset)
{
foreach (var lItem in mModuleInstance)
{
XmlNode lInstance = lItem.Key;
// argParam has to be the first Arg of a module
XmlNode lArg = lInstance.SelectSingleNode("./NumericArg");
int lRepeatIndicator = lItem.Value;
if (lRepeatIndicator > 0)
{
// we have a repeated instance, we need to modify the allocator start value
string lId = lArg.NodeAttr("AllocatorRefId");
// find allocator
XmlNode lAllocator = lInstance.OwnerDocument.SelectSingleNode(string.Format("//Allocator[@Id='{0}']", lId));
XmlNode lStart = lAllocator.Attributes.GetNamedItem("Start");
if (lStart.Value == "%ModuleStart%")
{
lStart.Value = iParamOffset.ToString();
}
iParamOffset += lRepeatIndicator * FullParamSize;
XmlNode lEnd = lAllocator.Attributes.GetNamedItem("maxInclusive");
if (lEnd.Value == "%ModuleEnd%")
{
lEnd.Value = (iParamOffset - 1).ToString();
}
}
else if (lArg != null)
{
// we have a single module call
XmlNode lValue = lArg.Attributes.GetNamedItem("Value");
if (lValue.Value == "%ModuleStart%")
{
lValue.Value = iParamOffset.ToString();
}
iParamOffset += FullParamSize;
}
}
}
private void ExportHeaderParameter(int iParamOffset, int iModuleOffset, StringBuilder cOut)
{
string lCalcIndexName = "";
string lCalcIndexArgs = "";
cOut.AppendLine();
cOut.AppendLine();
cOut.AppendFormat("// Header generation for Module '{0}'", mName);
cOut.AppendLine();
cOut.AppendLine();
cOut.AppendFormat("#define {0}Count {1}", mName, mModuleCount);
cOut.AppendLine();
cOut.AppendFormat("#define {0}ModuleParamSize {1}", mName, ModuleParamSize);
cOut.AppendLine();
cOut.AppendFormat("#define {0}SubmodulesParamSize {1}", mName, SubmodulesParamSize);
cOut.AppendLine();
cOut.AppendFormat("#define {0}ParamSize {1}", mName, FullParamSize);
cOut.AppendLine();
if (IsSubmodule)
{
cOut.AppendFormat("#define {0}CalcIndex(index, m1, m2) ({1}CalcIndex(index, m1) + {1}ModuleParamSize + m2 * {0}ParamSize)", mName, mParentModule.mName);
lCalcIndexName = "{3}" + $"{mName}CalcIndex" + "({3}{0}, m1, m2)";
lCalcIndexArgs = "(m1, m2)";
}
else
{
cOut.AppendFormat("#define {0}ParamOffset {1}", mName, iParamOffset);
cOut.AppendLine();
cOut.AppendFormat("#define {0}CalcIndex(index, m1) (index + {0}ParamOffset + _channelIndex * {0}Count * {0}ParamSize + m1 * {0}ParamSize)", mName);
lCalcIndexName = "{3}" + $"{mName}CalcIndex" + "({3}{0}, m1)";
lCalcIndexArgs = "(m1)";
}
cOut.AppendLine();
cOut.AppendLine();
XmlNodeList lNodes = mRootNode.SelectNodes("./Static/Parameters//Parameter");
mInclude.ExportHeaderParameter(lNodes, mDefine, cOut, "", false, lCalcIndexName, lCalcIndexArgs);
GetSubmoduleList();
UpdateModuleInstnces(iModuleOffset);
iParamOffset += ModuleParamSize;
foreach (ProcessModule lModule in mSubmodules)
{
lModule.ExportHeaderParameter(iParamOffset, ModuleParamSize, cOut);
iParamOffset += lModule.ModuleCount * lModule.ModuleParamSize;
}
}
static public void ExportHeaderParameterAll(ProcessInclude iInclude, StringBuilder cOut)
{
// find correct module with this include
foreach (var lItem in sModule)
{
ProcessModule lModule = lItem.Value;
if (!lModule.IsSubmodule)
{
lModule.ExportHeaderParameter(iInclude.ParameterBlockSize, iInclude.ParameterBlockSize, cOut);
iInclude.ParameterBlockSize += lModule.ModuleCount * lModule.FullParamSize;
}
}
}
}
}