/
GenerateGetResTrans.java
346 lines (260 loc) · 10.5 KB
/
GenerateGetResTrans.java
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package org.overture.codegen.vdm2c.distribution.transformations;
import java.util.LinkedList;
import java.util.Set;
import org.overture.ast.intf.lex.ILexNameToken;
import org.overture.ast.types.AClassType;
import org.overture.cgc.extast.analysis.DepthFirstAnalysisCAdaptor;
import org.overture.codegen.ir.SExpIR;
import org.overture.codegen.ir.SStmIR;
import org.overture.codegen.ir.STypeIR;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
import org.overture.codegen.ir.declarations.AFieldDeclIR;
import org.overture.codegen.ir.declarations.AFormalParamLocalParamIR;
import org.overture.codegen.ir.declarations.AMethodDeclIR;
import org.overture.codegen.ir.expressions.AApplyExpIR;
import org.overture.codegen.ir.expressions.AEqualsBinaryExpIR;
import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
import org.overture.codegen.ir.expressions.AIntLiteralExpIR;
import org.overture.codegen.ir.expressions.AOrBoolBinaryExpIR;
import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
import org.overture.codegen.ir.statements.ABlockStmIR;
import org.overture.codegen.ir.statements.AIfStmIR;
import org.overture.codegen.ir.statements.AReturnStmIR;
import org.overture.codegen.ir.types.AExternalTypeIR;
import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
import org.overture.codegen.ir.types.AMethodTypeIR;
import org.overture.codegen.ir.types.ANatNumericBasicTypeIR;
import org.overture.codegen.trans.assistants.TransAssistantIR;
import org.overture.codegen.vdm2c.distribution.SystemArchitectureAnalysis;
public class GenerateGetResTrans extends DepthFirstAnalysisCAdaptor
{
public GenerateGetResTrans(TransAssistantIR transformationAssistant)
{
}
@Override
public void caseADefaultClassDeclIR(ADefaultClassDeclIR node)
throws AnalysisException
{
ADefaultClassDeclIR cl = node;
// System.out.println("Dist transformation, method name: " + node.getName());
if(node.getTag()!=null){
String cpu = cl.getTag().toString();
// Create a new method:
AMethodDeclIR m = new AMethodDeclIR();
m.setAsync(false);
m.setAbstract(false);
m.setAccess("public");
m.setImplicit(false);
m.setIsConstructor(false);
m.setStatic(false);
m.setName("getRes");
LinkedList<AFormalParamLocalParamIR> par = new LinkedList<AFormalParamLocalParamIR>();
// First parameter
AFormalParamLocalParamIR par1 = new AFormalParamLocalParamIR();
AIdentifierPatternIR idPat1 = new AIdentifierPatternIR();
idPat1.setName("objID");
par1.setPattern(idPat1);
AExternalTypeIR tyPat = new AExternalTypeIR();
tyPat.setName("int");
par1.setType(tyPat);
par.add(par1);
// Second parameter
AFormalParamLocalParamIR par2 = new AFormalParamLocalParamIR();
AIdentifierPatternIR idPat2 = new AIdentifierPatternIR();
idPat2.setName("funID");
par2.setPattern(idPat2);
par2.setType(tyPat.clone());
par.add(par2);
// Third parameter
AFormalParamLocalParamIR par5 = new AFormalParamLocalParamIR();
AIdentifierPatternIR idPat5 = new AIdentifierPatternIR();
idPat5.setName("supID");
par5.setPattern(idPat5);
par5.setType(tyPat.clone());
par.add(par5);
// Third parameter
AFormalParamLocalParamIR par4 = new AFormalParamLocalParamIR();
AIdentifierPatternIR idPat4 = new AIdentifierPatternIR();
idPat4.setName("nrArgs");
par4.setPattern(idPat4);
par4.setType(tyPat.clone());
par.add(par4);
// Fourth parameter
AFormalParamLocalParamIR par3 = new AFormalParamLocalParamIR();
AIdentifierPatternIR idPat3 = new AIdentifierPatternIR();
idPat3.setName("args []");
par3.setPattern(idPat3);
AExternalTypeIR tyVar = new AExternalTypeIR();
tyVar.setName("TVP");
par3.setType(tyVar);
par.add(par3);
// The method return type
AExternalTypeIR tyRet = new AExternalTypeIR();
tyRet.setName("TVP");
AMethodTypeIR mTy = new AMethodTypeIR();
LinkedList<STypeIR> paramsTy = new LinkedList<STypeIR>();
paramsTy.add(new AIntNumericBasicTypeIR());
paramsTy.add(new AIntNumericBasicTypeIR());
mTy.setParams(paramsTy);
mTy.setResult(tyRet);
m.setMethodType(mTy.clone());
m.setFormalParams(par);
// New method body
ABlockStmIR body = new ABlockStmIR();
body.setScoped(false);
// Create the statements
LinkedList<SStmIR> st = new LinkedList<SStmIR>();
/** One if statement pr. bus for a cpu ***/
for(String bus : SystemArchitectureAnalysis.connectionMapStr.keySet()){
// 1. statement
// AIfStmIR first = new AIfStmIR();
//**** Exp part
//AEqualsBinaryExpIR bin = new AEqualsBinaryExpIR();
// Left side , Always the same
AIdentifierVarExpIR id = new AIdentifierVarExpIR();
id.setIsLambda(false);
id.setIsLocal(true);
id.setName("objID");
id.setType(new AIntNumericBasicTypeIR());
LinkedList<AEqualsBinaryExpIR> binList = new LinkedList<AEqualsBinaryExpIR>();
Set<String> cpuList = SystemArchitectureAnalysis.connectionMapStr.get(bus);
int obj_id = 0;
if(cpuList.contains(cpu)){
for(String c : cpuList){
if(c.equals(cpu)){
// get deployed objects of the current CPU
Set<String> depObj = SystemArchitectureAnalysis.distributionMapStr.get(c);
for(String dobj : depObj){
// One if statement pr. deployed object
int idVal = SystemArchitectureAnalysis.systemDeployedObjectsStr.indexOf(dobj) + 1;
LinkedList<AFieldDeclIR> va = SystemArchitectureAnalysis.systemDeployedObjects;
obj_id = idVal - 1; // stor current object id
AFieldDeclIR o = va.get(obj_id);
LinkedList<ILexNameToken> classSupNames = new LinkedList<ILexNameToken>();
if(o.getType().getSourceNode().getVdmNode() instanceof AClassType){
AClassType classN = (AClassType) o.getType().getSourceNode().getVdmNode();
classSupNames = classN.getClassdef().getSupernames();
}
LinkedList<String> classNames = new LinkedList<String>();
classNames.add(o.getType().toString());
for(ILexNameToken sup : classSupNames){
classNames.add(sup.toString());
}
AIfStmIR firstStm = new AIfStmIR();
AEqualsBinaryExpIR binEqStm = new AEqualsBinaryExpIR();
// Left side: id
AIdentifierVarExpIR idObjStm = new AIdentifierVarExpIR();
idObjStm.setIsLambda(false);
idObjStm.setIsLocal(true);
idObjStm.setName("objID");
idObjStm.setType(new AIntNumericBasicTypeIR());
binEqStm.setLeft(idObjStm);
// Right side: Add id to if for this specific bus
// Add 1 since we start from 1 and not 0
AIntLiteralExpIR val = new AIntLiteralExpIR();
val.setType(new ANatNumericBasicTypeIR());
val.setValue((long) idVal);
binEqStm.setRight(val);
firstStm.setIfExp(binEqStm);
int len = classSupNames.size() + 1;
ABlockStmIR if_block = new ABlockStmIR();
for(int p = 0; p<len; p++){
AIfStmIR first = new AIfStmIR();
AEqualsBinaryExpIR binEq = new AEqualsBinaryExpIR();
// Left side: id
AIdentifierVarExpIR idObj = new AIdentifierVarExpIR();
idObj.setIsLambda(false);
idObj.setIsLocal(true);
idObj.setName("supID");
idObj.setType(new AIntNumericBasicTypeIR());
binEq.setLeft(idObj);
// Right side: Add id to if for this specific bus
// Add 1 since we start from 1 and not 0
AIdentifierVarExpIR valS = new AIdentifierVarExpIR();
valS.setIsLambda(false);
valS.setIsLocal(true);
valS.setName("CLASS_ID_" + classNames.get(p) + "_ID");
valS.setType(new AIntNumericBasicTypeIR());
binEq.setRight(valS);
first.setIfExp(binEq);
//binList.add(binEq);
//**** Then part, e.g. call the specific BUS
// Then part is a block statement pr. inheritaed class
AReturnStmIR ret = new AReturnStmIR();
AApplyExpIR app = new AApplyExpIR();
// Set args
LinkedList<SExpIR> args = new LinkedList<SExpIR>();
AIntLiteralExpIR v = new AIntLiteralExpIR();
v.setValue((long) 6);
// 1. argument: Global variable name
AIdentifierVarExpIR arg1 = new AIdentifierVarExpIR();
arg1.setIsLambda(false);
arg1.setIsLocal(true);
arg1.setName("g_" + SystemArchitectureAnalysis.systemName + "_" + o.getName());
arg1.setType(new AIntNumericBasicTypeIR());
args.add(arg1);
// 2. argument: function id
AIdentifierVarExpIR arg2 = new AIdentifierVarExpIR();
arg2.setIsLambda(false);
arg2.setIsLocal(true);
arg2.setName("funID");
arg2.setType(new AIntNumericBasicTypeIR());
args.add(arg2);
// // 3. argument: number of arguments
// AIdentifierVarExpIR arg3 = new AIdentifierVarExpIR();
// arg3.setIsLambda(false);
// arg3.setIsLocal(true);
// arg3.setName("supID");
// arg3.setType(new AIntNumericBasicTypeIR());
// args.add(arg3);
//
// 4. argument: number of arguments
AIdentifierVarExpIR arg5 = new AIdentifierVarExpIR();
arg5.setIsLambda(false);
arg5.setIsLocal(true);
arg5.setName("nrArgs");
arg5.setType(new AIntNumericBasicTypeIR());
args.add(arg5);
// 5. argument: Array of arguments
AIdentifierVarExpIR arg4 = new AIdentifierVarExpIR();
arg4.setIsLambda(false);
arg4.setIsLocal(true);
arg4.setName("args");
arg4.setType(new AIntNumericBasicTypeIR());
args.add(arg4);
//args.add(id);
app.setArgs(args);
// Type
app.setType(new AIntNumericBasicTypeIR());
// Root
AIdentifierVarExpIR idVar = new AIdentifierVarExpIR();
idVar.setIsLambda(false);
idVar.setIsLocal(false);
idVar.setName("dist" + classNames.get(p));
// set method type
idVar.setType(mTy.clone());
app.setRoot(idVar);
ret.setExp(app);
// Create inner if statement for
first.setThenStm(ret);
if_block.getStatements().add(first.clone());
}
//if_block.getStatements().add(first.clone());
firstStm.setThenStm(if_block);
//first.setIfExp(orBin);
st.add(firstStm);
//st.add(first.clone());
}
}
}
}
body.setStatements(st);
}
//LinkedList<SStmIR> s = ((ABlockStmIR) node.getBody()).getStatements();
m.setBody(body);
cl.getMethods().add(m);
//System.out.println();
}
}
}