d02fa4fc23ba7e796274818c1e9dfb81dabc4186
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
1 package edu.uci.eecs.codeGenerator;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8 import java.io.BufferedWriter;
9 import java.io.File;
10 import java.io.FileWriter;
11 import java.io.IOException;
12
13 import edu.uci.eecs.specExtraction.Code;
14 import edu.uci.eecs.specExtraction.CommutativityRule;
15 import edu.uci.eecs.specExtraction.EntryConstruct;
16 import edu.uci.eecs.specExtraction.FunctionHeader;
17 import edu.uci.eecs.specExtraction.GlobalConstruct;
18 import edu.uci.eecs.specExtraction.InterfaceConstruct;
19 import edu.uci.eecs.specExtraction.OPConstruct;
20 import edu.uci.eecs.specExtraction.SpecExtractor;
21 import edu.uci.eecs.specExtraction.SpecNaming;
22 import edu.uci.eecs.specExtraction.VariableDeclaration;
23
24 /**
25  * <p>
26  * Some utility functions for generating specification checking code.
27  * </p>
28  * 
29  * @author Peizhao Ou
30  * 
31  */
32 public class CodeGeneratorUtils {
33
34         public static void PrintCode(ArrayList<String> code) {
35                 for (int i = 0; i < code.size(); i++) {
36                         System.out.println(code.get(i));
37                 }
38         }
39
40         public static String Comment(String comment) {
41                 return "/** " + comment + " */";
42         }
43
44         public static String ShortComment(String comment) {
45                 return "// " + comment;
46         }
47
48         public static String IncludeHeader(String header) {
49                 return "#include " + header;
50         }
51
52         public static String Brace(String val) {
53                 return "(" + val + ")";
54         }
55
56         public static String Quote(String val) {
57                 return "\"" + val + "\"";
58         }
59
60         public static String Assign(String varName, String val) {
61                 return varName + " = " + val + ";";
62         }
63
64         public static String AssignToPtr(String structName, String field, String val) {
65                 return structName + "->" + field + " = " + val + ";";
66         }
67
68         public static String Declare(String type, String name) {
69                 return type + " " + name + ";";
70         }
71
72         public static String Declare(VariableDeclaration varDecl) {
73                 return Declare(varDecl.type, varDecl.name);
74         }
75
76         public static String DeclareDefine(String type, String var, String val) {
77                 return type + " " + var + " = " + val + ";";
78         }
79
80         /**
81          * <p>
82          * Insert a number of tabs at the beginning of the line.
83          * </p>
84          * 
85          * @param line
86          *            Input line
87          * @param tabCnt
88          *            The number of tabs to be inserted
89          * @return A line that starts with the specific inserted tabs
90          */
91         public static String TabbedLine(String line, int tabCnt) {
92                 String res = "";
93                 for (int i = 0; i < tabCnt; i++)
94                         res = "\t" + res;
95                 res = res + line;
96                 return res;
97         }
98
99         /**
100          * <p>
101          * Insert a tab at the beginning of the line.
102          * </p>
103          * 
104          * @param line
105          *            Input line
106          * @return A line that starts with one inserted tab
107          */
108         public static String TabbedLine(String line) {
109                 return "\t" + line;
110         }
111
112         /**
113          * <p>
114          * This function generates the code for the header file that our
115          * specification generates automatically --- cdsspec-generated.h.
116          * </p>
117          * 
118          * @param extractor
119          *            The SpecExtractor that contains the extracted information
120          * @return The generated code
121          */
122         public static Code GenerateCDSSpecHeaderFile(SpecExtractor extractor) {
123                 HashSet<String> headerFiles = extractor.headerFiles;
124                 GlobalConstruct globalConstruct = extractor.getGlobalConstruct();
125                 HashMap<File, ArrayList<InterfaceConstruct>> interfaceListMap = extractor.interfaceListMap;
126                 HashSet<String> OPLabelSet = extractor.OPLabelSet;
127
128                 Code code = new Code();
129
130                 // Add auto-generated comments
131                 code.addLine("/**");
132                 code.addLine(TabbedLine("This is a header file auto-generated by CDSSpec compiler; together, CDSSpec"));
133                 code.addLine(TabbedLine("compiler should have generated the accompanying implementation file that"));
134                 code.addLine(TabbedLine("implements the some functions declared in this file. In order to instrument"));
135                 code.addLine(TabbedLine("your benchmark for CDSSpec checker to check, you should include this header"));
136                 code.addLine(TabbedLine("file in every file you use an CDSSpec annotation. Note that it should be"));
137                 code.addLine(TabbedLine("placed in the end of all other header files. Currently we require a C++"));
138                 code.addLine(TabbedLine("compiler that supports C++11."));
139                 code.addLine("*/");
140                 code.addLine("");
141
142                 code.addLine("#ifndef _"
143                                 + SpecNaming.CDSSpecGeneratedName.toUpperCase().replace('-',
144                                                 '_') + "_H");
145                 code.addLine("#define _"
146                                 + SpecNaming.CDSSpecGeneratedName.toUpperCase().replace('-',
147                                                 '_') + "_H");
148                 code.addLine("");
149
150                 // System included headers
151                 code.addLine(ShortComment("System included headers go here"));
152                 for (String header : SpecNaming.includedHeadersList) {
153                         code.addLine(IncludeHeader(header));
154                 }
155                 code.addLine("");
156
157                 // Users included headers
158                 code.addLine(ShortComment("User included headers go here"));
159                 for (String header : headerFiles) {
160                         code.addLine(IncludeHeader(header));
161                 }
162                 code.addLine("");
163
164                 code.addLine("using namespace std;");
165                 code.addLine("");
166                 code.addLine("");
167
168                 code.addLine(ShortComment("Declaration of some c-strings (CSTR)"));
169                 code.addLine(ShortComment("A special empty string"));
170                 code.addLine(Declare("extern " + SpecNaming.CString,
171                                 SpecNaming.EmptyCString));
172                 code.addLine("");
173
174                 // Interface name strings
175                 code.addLine(ShortComment("Interface name strings"));
176                 for (File file : interfaceListMap.keySet()) {
177                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
178                         for (InterfaceConstruct construct : list) {
179                                 String name = construct.getName();
180                                 code.addLine(Declare("extern " + SpecNaming.CString,
181                                                 SpecNaming.AppendStr(name)));
182                         }
183                 }
184                 code.addLine("");
185
186                 // Commutativity rule strings
187                 code.addLine(ShortComment("Commutativity rule strings"));
188                 for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
189                         code.addLine(Declare("extern " + SpecNaming.CString,
190                                         SpecNaming.AppendStr(SpecNaming.Commutativity + i)));
191                 }
192                 code.addLine("");
193
194                 // Ordering points label strings
195                 code.addLine(ShortComment("Ordering points label strings"));
196                 for (String label : OPLabelSet) {
197                         code.addLine(Declare("extern " + SpecNaming.CString,
198                                         SpecNaming.AppendStr(label)));
199                 }
200                 code.addLine("");
201
202                 // Special function name strings
203                 code.addLine(ShortComment("Special function name strings"));
204                 code.addLine(Declare("extern " + SpecNaming.CString,
205                                 SpecNaming.AppendStr(SpecNaming.InitalState)));
206                 code.addLine(Declare("extern " + SpecNaming.CString,
207                                 SpecNaming.AppendStr(SpecNaming.CopyState)));
208                 code.addLine(Declare("extern " + SpecNaming.CString,
209                                 SpecNaming.AppendStr(SpecNaming.FinalState)));
210                 code.addLine(Declare("extern " + SpecNaming.CString,
211                                 SpecNaming.AppendStr(SpecNaming.PrintState)));
212                 code.addLine("");
213
214                 // Interface name strings
215                 for (File file : interfaceListMap.keySet()) {
216                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
217                         for (InterfaceConstruct construct : list) {
218                                 String name = construct.getName();
219                                 code.addLine(ShortComment(name + " function strings"));
220                                 // Transition
221                                 String tmpFunc = name + "_" + SpecNaming.Transition;
222                                 code.addLine(Declare("extern " + SpecNaming.CString,
223                                                 SpecNaming.AppendStr(tmpFunc)));
224                                 // PreCondition
225                                 tmpFunc = name + "_" + SpecNaming.PreCondition;
226                                 code.addLine(Declare("extern " + SpecNaming.CString,
227                                                 SpecNaming.AppendStr(tmpFunc)));
228                                 // SideEffect
229                                 tmpFunc = name + "_" + SpecNaming.SideEffect;
230                                 code.addLine(Declare("extern " + SpecNaming.CString,
231                                                 SpecNaming.AppendStr(tmpFunc)));
232                                 // PostCondition
233                                 tmpFunc = name + "_" + SpecNaming.PostCondition;
234                                 code.addLine(Declare("extern " + SpecNaming.CString,
235                                                 SpecNaming.AppendStr(tmpFunc)));
236                                 // Print
237                                 tmpFunc = name + "_" + SpecNaming.PrintValue;
238                                 code.addLine(Declare("extern " + SpecNaming.CString,
239                                                 SpecNaming.AppendStr(tmpFunc)));
240                                 code.addLine("");
241                         }
242                 }
243
244                 // Declare customized StateStruct
245                 code.addLine(ShortComment("Declare customized StateStruct"));
246                 code.addLine("typedef struct " + SpecNaming.StateStruct + " {");
247                 for (VariableDeclaration decl : globalConstruct.declState) {
248                         code.addLine(TabbedLine(Declare(decl)));
249                 }
250                 code.addLine("");
251                 code.addLine(TabbedLine("SNAPSHOTALLOC"));
252                 code.addLine("} " + SpecNaming.StateStruct + ";");
253                 code.addLine("");
254
255                 // Declare customized value struct
256                 for (File file : interfaceListMap.keySet()) {
257                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
258                         for (InterfaceConstruct construct : list) {
259                                 // Declare custom value struct for the interface
260                                 String name = construct.getName();
261                                 code.addLine(ShortComment("Declare custom value struct for "
262                                                 + name));
263                                 code.addLine("typedef struct " + name + " {");
264                                 FunctionHeader funcHeader = construct.getFunctionHeader();
265                                 // RET
266                                 if (!funcHeader.returnType.equals("void"))
267                                         code.addLine(TabbedLine(Declare(funcHeader.returnType,
268                                                         SpecNaming.RET)));
269                                 // Arguments
270                                 for (VariableDeclaration decl : funcHeader.args) {
271                                         code.addLine(TabbedLine(Declare(decl)));
272                                 }
273                                 code.addLine("} " + name + ";");
274                                 code.addLine("");
275                         }
276                 }
277
278                 // Declare @Initial
279                 code.addLine(ShortComment("Declare @" + SpecNaming.InitalState));
280                 code.addLine("void _" + SpecNaming.InitalState.toLowerCase() + "("
281                                 + SpecNaming.Method + " " + SpecNaming.Method1 + ");");
282                 code.addLine("");
283                 // Declare @Copy
284                 code.addLine(ShortComment("Declare @" + SpecNaming.CopyState));
285                 code.addLine("void _" + SpecNaming.CopyState.toLowerCase() + "("
286                                 + SpecNaming.Method + " " + "dest, " + SpecNaming.Method
287                                 + " src);");
288                 code.addLine("");
289                 // Declare @Print
290                 code.addLine(ShortComment("Declare @" + SpecNaming.PrintState));
291                 if (!globalConstruct.printState.isEmpty()) {
292                         code.addLine("void _" + SpecNaming.PrintState.toLowerCase() + "("
293                                         + SpecNaming.Method + " " + SpecNaming.Method1 + ");");
294                         code.addLine("");
295                 }
296
297                 // Declare @Commutativity
298                 code.addLine(ShortComment("Declare commutativity checking functions"));
299                 for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
300                         code.addLine("bool _check" + SpecNaming.Commutativity + i + "("
301                                         + SpecNaming.Method + " m1, " + SpecNaming.Method + " m2);");
302                 }
303                 code.addLine("");
304
305                 // Declare customized interface functions
306                 for (File file : interfaceListMap.keySet()) {
307                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
308                         for (InterfaceConstruct construct : list) {
309                                 // Declare interface functions
310                                 String name = construct.getName();
311                                 code.addLine("/**********    " + name
312                                                 + " functions    **********/");
313                                 // Declare @Transition for INTERFACE
314                                 code.addLine(ShortComment("Declare @" + SpecNaming.Transition
315                                                 + " for " + name));
316                                 code.addLine("void _" + name + "_" + SpecNaming.Transition
317                                                 + "(" + SpecNaming.Method + " " + SpecNaming.Method1
318                                                 + ", " + SpecNaming.Method + " " + SpecNaming.Method2
319                                                 + ");");
320                                 code.addLine("");
321                                 // Declare @PreCondition
322                                 if (!construct.preCondition.isEmpty()) {
323                                         code.addLine(ShortComment("Declare @"
324                                                         + SpecNaming.PreCondition + " for " + name));
325                                         code.addLine("bool _" + name + "_"
326                                                         + SpecNaming.PreCondition + "(" + SpecNaming.Method
327                                                         + " " + SpecNaming.Method1 + ");");
328                                         code.addLine("");
329                                 }
330                                 // Declare @SideEffect
331                                 if (!construct.sideEffect.isEmpty()) {
332                                         code.addLine(ShortComment("Declare @"
333                                                         + SpecNaming.SideEffect + " for " + name));
334                                         code.addLine("void _" + name + "_" + SpecNaming.SideEffect
335                                                         + "(" + SpecNaming.Method + " "
336                                                         + SpecNaming.Method1 + ");");
337                                         code.addLine("");
338                                 }
339                                 // Declare @PostCondition
340                                 if (!construct.postCondition.isEmpty()) {
341                                         code.addLine(ShortComment("Declare @"
342                                                         + SpecNaming.PostCondition + " for " + name));
343                                         code.addLine("bool _" + name + "_"
344                                                         + SpecNaming.PostCondition + "("
345                                                         + SpecNaming.Method + " " + SpecNaming.Method1
346                                                         + ");");
347                                         code.addLine("");
348                                 }
349                                 // Declare @Print
350                                 if (!construct.print.isEmpty()) {
351                                         code.addLine(ShortComment("Declare @"
352                                                         + SpecNaming.PrintValue + " for " + name));
353                                         code.addLine("void _" + name + "_" + SpecNaming.PrintValue
354                                                         + "(" + SpecNaming.Method + " "
355                                                         + SpecNaming.Method1 + ");");
356                                         code.addLine("");
357                                 }
358                         }
359                 }
360
361                 // Declare INIT annotation instrumentation function
362                 code.addLine(ShortComment("Declare INIT annotation instrumentation function"));
363                 code.addLine("void _createInitAnnotation();");
364                 code.addLine("");
365                 code.addLine("#endif");
366
367                 return code;
368         }
369
370         /**
371          * <p>
372          * This function generates the code for the CPP file that our specification
373          * generates automatically --- cdsspec-generated.cc.
374          * </p>
375          * 
376          * @param extractor
377          *            The SpecExtractor that contains the extracted information
378          * @return The generated code
379          */
380         public static Code GenerateCDSSpecCPPFile(SpecExtractor extractor) {
381                 GlobalConstruct globalConstruct = extractor.getGlobalConstruct();
382                 HashMap<File, ArrayList<InterfaceConstruct>> interfaceListMap = extractor.interfaceListMap;
383                 HashSet<String> OPLabelSet = extractor.OPLabelSet;
384
385                 Code code = new Code();
386                 String line = null;
387
388                 // Add auto-generated comments
389                 code.addLine("/**");
390                 code.addLine(TabbedLine("This is an implementation file auto-generated by CDSSpec compiler to"));
391                 code.addLine(TabbedLine("instrument your benchmark for CDSSpec checker to check. Currently we require"));
392                 code.addLine(TabbedLine("a C++ compiler that supports C++11."));
393                 code.addLine("*/");
394                 code.addLine("");
395
396                 code.addLine("#include " + SpecNaming.CDSSpecGeneratedHeader);
397                 code.addLine("");
398                 code.addLine("");
399
400                 code.addLine(ShortComment("Definition of some c-strings (CSTR)"));
401                 code.addLine(ShortComment("A special empty string"));
402                 code.addLine(DeclareDefine(SpecNaming.CString, SpecNaming.EmptyCString,
403                                 "\"\""));
404                 code.addLine("");
405
406                 // Interface name strings
407                 code.addLine(ShortComment("Interface name strings"));
408                 for (File file : interfaceListMap.keySet()) {
409                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
410                         for (InterfaceConstruct construct : list) {
411                                 String name = construct.getName();
412                                 code.addLine(DeclareDefine(SpecNaming.CString,
413                                                 SpecNaming.AppendStr(name), Quote(name)));
414                         }
415                 }
416                 code.addLine("");
417
418                 // Commutativity rule strings
419                 code.addLine(ShortComment("Commutativity rule strings"));
420                 for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
421                         CommutativityRule rule = globalConstruct.commutativityRules
422                                         .get(i - 1);
423                         code.addLine(DeclareDefine(SpecNaming.CString,
424                                         SpecNaming.AppendStr(SpecNaming.Commutativity + i),
425                                         Quote(rule.toString())));
426                 }
427                 code.addLine("");
428
429                 // Ordering points label strings
430                 code.addLine(ShortComment("Ordering points label strings"));
431                 for (String label : OPLabelSet) {
432                         code.addLine(DeclareDefine(SpecNaming.CString,
433                                         SpecNaming.AppendStr(label), Quote(label)));
434                 }
435                 code.addLine("");
436
437                 // Special function name strings
438                 code.addLine(ShortComment("Special function name strings"));
439                 code.addLine(DeclareDefine(SpecNaming.CString,
440                                 SpecNaming.AppendStr(SpecNaming.InitalState), Quote("_"
441                                                 + SpecNaming.InitalState.toLowerCase())));
442                 code.addLine(DeclareDefine(SpecNaming.CString,
443                                 SpecNaming.AppendStr(SpecNaming.CopyState), Quote("_"
444                                                 + SpecNaming.CopyState.toLowerCase())));
445                 code.addLine(DeclareDefine(SpecNaming.CString,
446                                 SpecNaming.AppendStr(SpecNaming.FinalState), Quote("_"
447                                                 + SpecNaming.FinalState.toLowerCase())));
448                 code.addLine(DeclareDefine(SpecNaming.CString,
449                                 SpecNaming.AppendStr(SpecNaming.PrintState), Quote("_"
450                                                 + SpecNaming.PrintState.toLowerCase())));
451                 code.addLine("");
452
453                 // Interface name strings
454                 for (File file : interfaceListMap.keySet()) {
455                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
456                         for (InterfaceConstruct construct : list) {
457                                 String name = construct.getName();
458                                 code.addLine(ShortComment(name + " function strings"));
459                                 // Transition
460                                 String tmpFunc = name + "_" + SpecNaming.Transition;
461                                 code.addLine(DeclareDefine(SpecNaming.CString,
462                                                 SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
463                                 // PreCondition
464                                 tmpFunc = name + "_" + SpecNaming.PreCondition;
465                                 if (!construct.preCondition.isEmpty())
466                                         code.addLine(DeclareDefine(SpecNaming.CString,
467                                                         SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
468                                 else
469                                         code.addLine(DeclareDefine(SpecNaming.CString,
470                                                         SpecNaming.AppendStr(tmpFunc),
471                                                         SpecNaming.EmptyCString));
472                                 // SideEffect
473                                 tmpFunc = name + "_" + SpecNaming.SideEffect;
474                                 if (!construct.sideEffect.isEmpty())
475                                         code.addLine(DeclareDefine(SpecNaming.CString,
476                                                         SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
477                                 else
478                                         code.addLine(DeclareDefine(SpecNaming.CString,
479                                                         SpecNaming.AppendStr(tmpFunc),
480                                                         SpecNaming.EmptyCString));
481                                 // PostCondition
482                                 tmpFunc = name + "_" + SpecNaming.PostCondition;
483                                 if (!construct.postCondition.isEmpty())
484                                         code.addLine(DeclareDefine(SpecNaming.CString,
485                                                         SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
486                                 else
487                                         code.addLine(DeclareDefine(SpecNaming.CString,
488                                                         SpecNaming.AppendStr(tmpFunc),
489                                                         SpecNaming.EmptyCString));
490                                 // Print
491                                 tmpFunc = name + "_" + SpecNaming.PrintValue;
492                                 if (!construct.print.isEmpty())
493                                         code.addLine(DeclareDefine(SpecNaming.CString,
494                                                         SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
495                                 else
496                                         code.addLine(DeclareDefine(SpecNaming.CString,
497                                                         SpecNaming.AppendStr(tmpFunc),
498                                                         SpecNaming.EmptyCString));
499                                 code.addLine("");
500                         }
501                 }
502
503                 // Define @Initial
504                 code.addLine(ShortComment("Define @" + SpecNaming.InitalState));
505                 code.addLine("void _" + SpecNaming.InitalState.toLowerCase() + "("
506                                 + SpecNaming.Method + " " + SpecNaming.Method1 + ") {");
507                 code.addLine(TabbedLine(DeclareDefine(SpecNaming.StateStruct, "*"
508                                 + SpecNaming.StateInst, "new " + SpecNaming.StateStruct)));
509                 // Define macros
510                 for (VariableDeclaration decl : globalConstruct.declState) {
511                         code.addLine(TabbedLine("#define " + decl.name + " "
512                                         + SpecNaming.StateInst + "->" + decl.name));
513                 }
514                 code.addLine(TabbedLine(ShortComment("User-defined intial state code")));
515                 // Align the code with one tab
516                 globalConstruct.initState.align(1);
517                 code.addLines(globalConstruct.initState);
518                 // Undefine macros
519                 for (VariableDeclaration decl : globalConstruct.declState) {
520                         code.addLine(TabbedLine("#undef " + decl.name));
521                 }
522                 code.addLine("");
523                 code.addLine(TabbedLine(AssignToPtr(SpecNaming.Method1,
524                                 SpecNaming.StateInst, SpecNaming.StateInst)));
525                 code.addLine("}");
526                 code.addLine("");
527
528                 // Define @Copy
529                 code.addLine(ShortComment("Define @" + SpecNaming.CopyState));
530                 code.addLine("void _" + SpecNaming.CopyState.toLowerCase() + "("
531                                 + SpecNaming.Method + " " + "dest, " + SpecNaming.Method
532                                 + " src) {");
533                 // StateStruct *OLD = (StateStruct*) src->state;
534                 code.addLine(TabbedLine(DeclareDefine(SpecNaming.StateStruct, "*"
535                                 + SpecNaming.OldStateInst, Brace(SpecNaming.StateStruct + "*")
536                                 + " src->" + SpecNaming.StateInst)));
537                 // StateStruct *NEW = new StateStruct;
538                 code.addLine(TabbedLine(DeclareDefine(SpecNaming.StateStruct, "*"
539                                 + SpecNaming.NewStateInst, "new " + SpecNaming.StateStruct)));
540                 if (!globalConstruct.autoGenCopy)
541                         code.addLine(TabbedLine(ShortComment("User-defined state copy statements")));
542                 else
543                         // Auto-generated the copy function
544                         code.addLine(TabbedLine(ShortComment("Auto-generated state copy statements")));
545                 globalConstruct.copyState.align(1);
546                 code.addLines(globalConstruct.copyState);
547                 code.addLine("");
548                 code.addLine(TabbedLine(AssignToPtr("dest", SpecNaming.StateInst,
549                                 SpecNaming.NewStateInst)));
550                 code.addLine("}");
551                 code.addLine("");
552
553                 // Define @Print
554                 if (!globalConstruct.printState.isEmpty()) {
555                         code.addLine(ShortComment("Define @" + SpecNaming.PrintState));
556                         code.addLine("void _" + SpecNaming.PrintState.toLowerCase() + "("
557                                         + SpecNaming.Method + " " + SpecNaming.Method1 + ") {");
558
559                         // Initialize state struct fields
560                         Code fieldsInit = GenerateStateFieldsInitialization(
561                                         SpecNaming.Method1, SpecNaming.StateInst, globalConstruct);
562                         fieldsInit.align(1);
563                         code.addLines(fieldsInit);
564                         code.addLine("");
565                         code.addLine(TabbedLine(ShortComment("Execute the print-out")));
566                         // Align the code with one tab
567                         globalConstruct.printState.align(1);
568                         code.addLines(globalConstruct.printState);
569                         code.addLine("}");
570                         code.addLine("");
571                 }
572
573                 // Define @Commutativity
574                 code.addLine(ShortComment("Define commutativity checking functions"));
575                 for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
576                         CommutativityRule rule = globalConstruct.commutativityRules
577                                         .get(i - 1);
578                         code.addLine("bool _check" + SpecNaming.Commutativity + i + "("
579                                         + SpecNaming.Method + " m1, " + SpecNaming.Method
580                                         + " m2) {");
581                         // if (m1->name == _ENQ_str && m2->name == _DEQ_str) {
582                         code.addLine(TabbedLine("if (m1->name == "
583                                         + SpecNaming.AppendStr(rule.method1) + " && m2->name == "
584                                         + SpecNaming.AppendStr(rule.method2) + ") {"));
585                         // Initialize M1 & M2 in commutativity rule
586                         // e.g. ENQ *M1 = (ENQ*) m1->value;
587                         code.addLine(TabbedLine(
588                                         DeclareDefine(rule.method1, "*M1", "(" + rule.method1
589                                                         + "*) m1->value"), 2));
590                         code.addLine(TabbedLine(
591                                         DeclareDefine(rule.method2, "*M2", "(" + rule.method2
592                                                         + "*) m2->value"), 2));
593                         code.addLine(TabbedLine("return " + rule.condition + ";", 2));
594                         code.addLine(TabbedLine("}"));
595                         code.addLine(TabbedLine("return false;"));
596
597                         code.addLine("}");
598                         code.addLine("");
599                 }
600
601                 // Define customized interface functions
602                 for (File file : interfaceListMap.keySet()) {
603                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
604                         for (InterfaceConstruct construct : list) {
605                                 Code fieldsInit = null;
606
607                                 // Define interface functions
608                                 String name = construct.getName();
609                                 code.addLine("/**********    " + name
610                                                 + " functions    **********/");
611                                 // Define @Transition for INTERFACE
612                                 code.addLine(ShortComment("Define @" + SpecNaming.Transition
613                                                 + " for " + name));
614                                 code.addLine("void _" + name + "_" + SpecNaming.Transition
615                                                 + "(" + SpecNaming.Method + " " + SpecNaming.Method1
616                                                 + ", " + SpecNaming.Method + " " + SpecNaming.Method2
617                                                 + ") {");
618
619                                 // Initialize value struct fields
620                                 fieldsInit = GenerateInterfaceFieldsInitialization(
621                                                 SpecNaming.Method2, "value", construct);
622                                 fieldsInit.align(1);
623                                 code.addLines(fieldsInit);
624
625                                 construct.transition.align(1);
626                                 code.addLine(TabbedLine(ShortComment("Execute Transition")));
627                                 code.addLines(construct.transition);
628
629                                 code.addLine("}");
630                                 code.addLine("");
631
632                                 // Define @PreCondition
633                                 if (!construct.preCondition.isEmpty()) {
634                                         code.addLine(ShortComment("Define @"
635                                                         + SpecNaming.PreCondition + " for " + name));
636                                         code.addLine("bool _" + name + "_"
637                                                         + SpecNaming.PreCondition + "(" + SpecNaming.Method
638                                                         + " " + SpecNaming.Method1 + ") {");
639
640                                         // Initialize value struct fields
641                                         fieldsInit = GenerateInterfaceFieldsInitialization(
642                                                         SpecNaming.Method1, "value", construct);
643                                         fieldsInit.align(1);
644                                         code.addLines(fieldsInit);
645
646                                         construct.preCondition.align(1);
647                                         code.addLine(TabbedLine(ShortComment("Execute PreCondition")));
648                                         code.addLines(construct.preCondition);
649
650                                         code.addLine("}");
651                                         code.addLine("");
652
653                                 }
654                                 // Define @SideEffect
655                                 if (!construct.sideEffect.isEmpty()) {
656                                         code.addLine(ShortComment("Define @"
657                                                         + SpecNaming.SideEffect + " for " + name));
658                                         code.addLine("void _" + name + "_" + SpecNaming.SideEffect
659                                                         + "(" + SpecNaming.Method + " "
660                                                         + SpecNaming.Method1 + ") {");
661
662                                         // Initialize value struct fields
663                                         fieldsInit = GenerateInterfaceFieldsInitialization(
664                                                         SpecNaming.Method1, "value", construct);
665                                         fieldsInit.align(1);
666                                         code.addLines(fieldsInit);
667
668                                         construct.sideEffect.align(1);
669                                         code.addLine(TabbedLine(ShortComment("Execute SideEffect")));
670                                         code.addLines(construct.sideEffect);
671
672                                         code.addLine("}");
673                                         code.addLine("");
674                                 }
675                                 // Define @PostCondition
676                                 if (!construct.postCondition.isEmpty()) {
677                                         code.addLine(ShortComment("Define @"
678                                                         + SpecNaming.PostCondition + " for " + name));
679                                         code.addLine("bool _" + name + "_"
680                                                         + SpecNaming.PostCondition + "("
681                                                         + SpecNaming.Method + " " + SpecNaming.Method1
682                                                         + ") {");
683
684                                         // Initialize value struct fields
685                                         fieldsInit = GenerateInterfaceFieldsInitialization(
686                                                         SpecNaming.Method1, "value", construct);
687                                         fieldsInit.align(1);
688                                         code.addLines(fieldsInit);
689
690                                         construct.postCondition.align(1);
691                                         code.addLine(TabbedLine(ShortComment("Execute PostCondition")));
692                                         code.addLines(construct.postCondition);
693
694                                         code.addLine("}");
695                                         code.addLine("");
696                                 }
697                                 // Define @Print
698                                 if (!construct.print.isEmpty()) {
699                                         code.addLine(ShortComment("Define @"
700                                                         + SpecNaming.PrintValue + " for " + name));
701                                         code.addLine("void _" + name + "_" + SpecNaming.PrintValue
702                                                         + "(" + SpecNaming.Method + " "
703                                                         + SpecNaming.Method1 + ") {");
704                                         // Initialize value struct fields
705                                         fieldsInit = GenerateInterfaceFieldsInitialization(
706                                                         SpecNaming.Method1, "value", construct);
707                                         fieldsInit.align(1);
708                                         code.addLines(fieldsInit);
709
710                                         construct.print.align(1);
711                                         code.addLine(TabbedLine(ShortComment("Execute Print")));
712                                         code.addLines(construct.print);
713
714                                         code.addLine("}");
715                                         code.addLine("");
716                                 }
717                         }
718                 }
719
720                 // Define INIT annotation instrumentation function
721                 code.addLine(ShortComment("Define INIT annotation instrumentation function"));
722                 code.addLine("void _createInitAnnotation() {");
723
724                 // Init commutativity rules
725                 code.addLine(TabbedLine(ShortComment("Init commutativity rules")));
726                 code.addLine(TabbedLine(DeclareDefine("int",
727                                 SpecNaming.CommutativityRuleSizeInst,
728                                 Integer.toString(globalConstruct.commutativityRules.size()))));
729                 String tmp = SpecNaming.NewSize
730                                 + Brace(SpecNaming.CommutativityRule + ", sizeof"
731                                                 + Brace(SpecNaming.CommutativityRule) + " * "
732                                                 + SpecNaming.CommutativityRuleSizeInst);
733                 code.addLine(TabbedLine(DeclareDefine(SpecNaming.CommutativityRule, "*"
734                                 + SpecNaming.CommutativityRuleInst, tmp)));
735                 for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
736                         CommutativityRule rule = globalConstruct.commutativityRules
737                                         .get(i - 1);
738                         code.addLine(TabbedLine(ShortComment("Initialize commutativity rule ")
739                                         + i));
740                         // new( &commuteRules[0] )CommutativityRule(_ENQ_str, _DEQ_str,
741                         // _Commutativity1_str, _checkCommutativity1)
742                         line = "new"
743                                         + Brace(" &" + SpecNaming.CommutativityRuleInst + "["
744                                                         + (i - 1) + "] ") + SpecNaming.CommutativityRule
745                                         + "(" + SpecNaming.AppendStr(rule.method1) + ", "
746                                         + SpecNaming.AppendStr(rule.method2) + ", "
747                                         + SpecNaming.AppendStr(SpecNaming.Commutativity + i) + ", "
748                                         + "_check" + SpecNaming.Commutativity + i + ");";
749                         code.addLine(TabbedLine(line));
750                 }
751
752                 // Initialize AnnoInit
753                 code.addLine(TabbedLine(ShortComment("Initialize AnnoInit")));
754                 // AnnoInit *init = new AnnoInit(
755                 code.addLine(TabbedLine(SpecNaming.AnnoInit + " *"
756                                 + SpecNaming.AnnoInitInst + " = new " + SpecNaming.AnnoInit
757                                 + "("));
758                 // new NamedFunction(_Initial_str, INITIAL, (void*) _initial),
759                 code.addLine(TabbedLine("new " + SpecNaming.NamedFunction + "("
760                                 + SpecNaming.AppendStr(SpecNaming.InitalState) + ", "
761                                 + SpecNaming.InitalState.toUpperCase() + ", " + "(void*) _"
762                                 + SpecNaming.InitalState.toLowerCase() + "),", 2));
763                 // new NamedFunction(_Final_str, FINAL, (void*) NULL_FUNC),
764                 line = "new " + SpecNaming.NamedFunction + "("
765                                 + SpecNaming.AppendStr(SpecNaming.FinalState) + ", "
766                                 + SpecNaming.FinalState.toUpperCase() + ", " + "(void*) ";
767                 if (globalConstruct.finalState.isEmpty()) {
768                         line = line + SpecNaming.NullFunc + "),";
769                 } else {
770                         line = line + "_" + SpecNaming.FinalState.toUpperCase();
771                 }
772                 code.addLine(TabbedLine(line, 2));
773                 // new NamedFunction(_Copy_str, COPY, (void*) _copy),
774                 code.addLine(TabbedLine("new " + SpecNaming.NamedFunction + "("
775                                 + SpecNaming.AppendStr(SpecNaming.CopyState) + ", "
776                                 + SpecNaming.CopyState.toUpperCase() + ", " + "(void*) _"
777                                 + SpecNaming.CopyState.toLowerCase() + "),", 2));
778                 // new NamedFunction(_Print_str, PRINT_STATE, (void*) _print),
779                 code.addLine(TabbedLine("new " + SpecNaming.NamedFunction + "("
780                                 + SpecNaming.AppendStr(SpecNaming.PrintState) + ", "
781                                 + SpecNaming.PrintStateType + ", " + "(void*) _"
782                                 + SpecNaming.PrintState.toLowerCase() + "),", 2));
783                 // commuteRules, CommuteRuleSize);
784                 code.addLine(TabbedLine(SpecNaming.CommutativityRuleInst + ", "
785                                 + SpecNaming.CommutativityRuleSizeInst + ");", 2));
786                 code.addLine("");
787
788                 // Declare StateFunctions map
789                 code.addLine(TabbedLine(ShortComment("Declare StateFunctions map")));
790                 code.addLine(TabbedLine(Declare(SpecNaming.StateFunctions, "*"
791                                 + SpecNaming.StateFunctionsInst)));
792                 code.addLine("");
793
794                 // StateFunction for interface
795                 for (File file : interfaceListMap.keySet()) {
796                         ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
797                         for (InterfaceConstruct construct : list) {
798                                 String name = construct.getName();
799                                 code.addLine(TabbedLine(ShortComment("StateFunction for "
800                                                 + name)));
801                                 // stateFuncs = new StateFunctions(
802                                 code.addLine(TabbedLine(SpecNaming.StateFunctionsInst
803                                                 + " = new " + SpecNaming.StateFunctions + "("));
804                                 // new NamedFunction(_ENQ_Transition_str, TRANSITION, (void*)
805                                 // _ENQ_Transition),
806                                 // Transition
807                                 code.addLine(TabbedLine(
808                                                 "new "
809                                                                 + SpecNaming.NamedFunction
810                                                                 + "("
811                                                                 + SpecNaming.AppendStr(name + "_"
812                                                                                 + SpecNaming.Transition) + ", "
813                                                                 + SpecNaming.TransitionType + ", (void*) _"
814                                                                 + name + "_" + SpecNaming.Transition + "),", 2));
815                                 // PreCondition
816                                 line = "new "
817                                                 + SpecNaming.NamedFunction
818                                                 + "("
819                                                 + SpecNaming.AppendStr(name + "_"
820                                                                 + SpecNaming.PreCondition) + ", "
821                                                 + SpecNaming.PreConditionType + ", (void*) ";
822                                 if (construct.preCondition.isEmpty()) {
823                                         line = line + SpecNaming.NullFunc + "),";
824                                 } else {
825                                         line = line + "_" + name + "_" + SpecNaming.PreCondition
826                                                         + "),";
827                                 }
828                                 code.addLine(TabbedLine(line, 2));
829                                 // SideEffect
830                                 line = "new "
831                                                 + SpecNaming.NamedFunction
832                                                 + "("
833                                                 + SpecNaming.AppendStr(name + "_"
834                                                                 + SpecNaming.SideEffect) + ", "
835                                                 + SpecNaming.SideEffectType + ", (void*) ";
836                                 if (construct.sideEffect.isEmpty()) {
837                                         line = line + SpecNaming.NullFunc + "),";
838                                 } else {
839                                         line = line + "_" + name + "_" + SpecNaming.SideEffect
840                                                         + "),";
841                                 }
842                                 code.addLine(TabbedLine(line, 2));
843                                 // PostCondition
844                                 line = "new "
845                                                 + SpecNaming.NamedFunction
846                                                 + "("
847                                                 + SpecNaming.AppendStr(name + "_"
848                                                                 + SpecNaming.PostCondition) + ", "
849                                                 + SpecNaming.PostConditionType + ", (void*) ";
850                                 if (construct.postCondition.isEmpty()) {
851                                         line = line + SpecNaming.NullFunc + "),";
852                                 } else {
853                                         line = line + "_" + name + "_" + SpecNaming.PostCondition
854                                                         + "),";
855                                 }
856                                 code.addLine(TabbedLine(line, 2));
857                                 // Print (PrintValue
858                                 line = "new "
859                                                 + SpecNaming.NamedFunction
860                                                 + "("
861                                                 + SpecNaming.AppendStr(name + "_"
862                                                                 + SpecNaming.PrintValue) + ", "
863                                                 + SpecNaming.PrintValueType + ", (void*) ";
864                                 if (construct.print.isEmpty()) {
865                                         line = line + SpecNaming.NullFunc + "),";
866                                 } else {
867                                         line = line + "_" + name + "_" + SpecNaming.PrintValue
868                                                         + ")";
869                                 }
870                                 code.addLine(TabbedLine(line, 2));
871                                 code.addLine(TabbedLine(");"));
872
873                                 // init->addInterfaceFunctions(_ENQ_str, stateFuncs);
874                                 code.addLine(TabbedLine(SpecNaming.AnnoInitInst
875                                                 + "->"
876                                                 + SpecNaming.AddInterfaceFunctions
877                                                 + Brace(SpecNaming.AppendStr(name) + ", "
878                                                                 + SpecNaming.StateFunctionsInst) + ";"));
879                                 code.addLine("");
880                         }
881                 }
882
883                 // Create and instrument with the INIT annotation
884                 code.addLine(TabbedLine(ShortComment("Create and instrument with the INIT annotation")));
885                 // cdsannotate(SPEC_ANALYSIS, new SpecAnnotation(INIT, init));
886                 code.addLine(TabbedLine(SpecNaming.CDSAnnotateFunc
887                                 + Brace(SpecNaming.SPEC_ANALYSIS
888                                                 + ", new "
889                                                 + SpecNaming.SpecAnnotation
890                                                 + Brace(SpecNaming.AnnoTypeInit + ", "
891                                                                 + SpecNaming.AnnoInitInst)) + ";"));
892
893                 code.addLine("}");
894                 code.addLine("");
895
896                 return code;
897         }
898
899         /**
900          * <p>
901          * This function generates a list of lines that initialize the fields of the
902          * global state struct. See below.
903          * </p>
904          * 
905          * <p>
906          * <code>
907          * StateStruct *state = (StateStruct*) _M->state;
908          * <br>
909          * IntList * q = state->q;
910          * </code>
911          * </p>
912          * 
913          * <p>
914          * In this example, _M --> methodInst, state --> inst.
915          * </p>
916          * 
917          * @param methodInst
918          *            See description
919          * @param inst
920          *            See description
921          * @param construct
922          *            The global state construct
923          * @return The generated code
924          */
925         public static Code GenerateStateFieldsInitialization(String methodInst,
926                         String inst, GlobalConstruct construct) {
927                 Code res = new Code();
928                 res.addLine(ShortComment("Initialize " + SpecNaming.StateStruct
929                                 + " fields"));
930                 res.addLine(DeclareDefine(SpecNaming.StateStruct, "*" + inst, "("
931                                 + SpecNaming.StateStruct + "*) " + methodInst + "->state"));
932                 for (VariableDeclaration decl : construct.declState) {
933                         res.addLine(DeclareDefine(decl.type, decl.name, inst + "->"
934                                         + decl.name));
935                 }
936                 return res;
937         }
938
939         /**
940          * <p>
941          * This function generates a list of lines that initialize the fields of a
942          * specific interface struct. See below.
943          * </p>
944          * 
945          * <p>
946          * <code>
947          * ENQ *info = (ENQ*) _M->value;
948          * <br>
949          * IntList * q = info->q;
950          * </code>
951          * </p>
952          * 
953          * <p>
954          * In this example, ENQ --> structType, _M --> methodInst, info --> inst
955          * </p>
956          * 
957          * @param methodInst
958          *            See description
959          * @param inst
960          *            See description
961          * @param construct
962          *            The corresponding interface construct
963          * @return The generated code
964          */
965         public static Code GenerateInterfaceFieldsInitialization(String methodInst,
966                         String inst, InterfaceConstruct construct) {
967                 Code res = new Code();
968                 String name = construct.getName();
969                 res.addLine(ShortComment("Initialize fields for " + name));
970                 // The very first assignment "
971                 res.addLine(DeclareDefine(name, "*" + inst, "(" + name + "*) "
972                                 + methodInst + "->value"));
973                 // Don't leave out the RET field
974                 if (!construct.getFunctionHeader().isReturnVoid()) {
975                         res.addLine(DeclareDefine(construct.getFunctionHeader().returnType,
976                                         SpecNaming.RET, "value->" + SpecNaming.RET));
977                 }
978                 // For arguments
979                 for (VariableDeclaration decl : construct.getFunctionHeader().args) {
980                         res.addLine(DeclareDefine(decl.type, decl.name, inst + "->"
981                                         + decl.name));
982                 }
983                 return res;
984         }
985
986         /**
987          * <p>
988          * This function generates the code to be inserted right after the ordering
989          * point construct (instrumentation code)
990          * </p>
991          * 
992          * @param construct
993          *            The corresponding ordering point construct
994          * @return The generated code
995          */
996         public static Code Generate4OPConstruct(OPConstruct construct) {
997                 Code code = new Code();
998                 String curLine = construct.annotation;
999                 String label = construct.label;
1000                 String prefixTabs = curLine.substring(0, curLine.indexOf("/**"));
1001                 code.addLine(prefixTabs + "if (" + construct.condition + ")");
1002                 switch (construct.type) {
1003                 case OPDefine:
1004                         code.addLine(prefixTabs + "\t" + SpecNaming.CreateOPDefineAnnoFunc
1005                                         + "();");
1006                         break;
1007                 case PotentialOP:
1008                         code.addLine(prefixTabs + "\t"
1009                                         + SpecNaming.CreatePotentialOPAnnoFunc + "("
1010                                         + SpecNaming.AppendStr(label) + ");");
1011                         break;
1012                 case OPCheck:
1013                         code.addLine(prefixTabs + "\t" + SpecNaming.CreateOPCheckAnnoFunc
1014                                         + "(" + SpecNaming.AppendStr(label) + ");");
1015                         break;
1016                 case OPClear:
1017                         code.addLine(prefixTabs + "\t" + SpecNaming.CreateOPClearAnnoFunc
1018                                         + "();");
1019                         break;
1020                 case OPClearDefine:
1021                         code.addLine(prefixTabs + "\t"
1022                                         + SpecNaming.CreateOPClearDefineAnnoFunc + "();");
1023                         break;
1024                 default:
1025                         break;
1026                 }
1027                 return code;
1028         }
1029
1030         /**
1031          * <p>
1032          * This function generates the code to be inserted right after the entry
1033          * construct (instrumentation code)
1034          * </p>
1035          * 
1036          * @param construct
1037          *            The corresponding entry construct
1038          * @return
1039          */
1040         public static Code Generate4Entry(EntryConstruct construct) {
1041                 Code res = new Code();
1042                 String curLine = construct.annotation;
1043                 String prefixTabs = curLine.substring(0, curLine.indexOf("/**"));
1044                 // _createInitAnnotation();
1045                 res.addLine(prefixTabs + SpecNaming.CreateInitAnnoFunc + "();");
1046                 return res;
1047         }
1048
1049         /**
1050          * <p>
1051          * This function generates the new interface wrapper code to be inserted
1052          * right after the end of the interface definition
1053          * </p>
1054          * 
1055          * @param construct
1056          *            The corresponding interface construct
1057          * @return The generated code
1058          */
1059         public static Code GenerateInterfaceWrapper(InterfaceConstruct construct) {
1060                 Code code = new Code();
1061
1062                 String name = construct.getName();
1063                 String beginLine = construct.getFunctionHeader().getHeaderLine();
1064                 Pattern regexpSpace = Pattern.compile("^(\\s*)\\S.*$");
1065                 Matcher matcherSpace = regexpSpace.matcher(beginLine);
1066                 String prefixTabs = "";
1067                 if (matcherSpace.find())
1068                         prefixTabs = matcherSpace.group(1);
1069
1070                 // Add one line to separate
1071                 code.addLine("");
1072                 code.addLine(prefixTabs
1073                                 + ShortComment("Generated wrapper interface for " + name));
1074                 if (beginLine.indexOf('{') == -1) { // We need to add the '{' to the end
1075                                                                                         // of the line
1076                         code.addLine(beginLine + " {");
1077                 } else {
1078                         code.addLine(beginLine);
1079                 }
1080                 // Instrument with the INTERFACE_BEGIN annotation
1081                 code.addLine(prefixTabs
1082                                 + "\t"
1083                                 + ShortComment("Instrument with the INTERFACE_BEGIN annotation"));
1084                 // AnnoInterfaceInfo *info = _createInterfaceBeginAnnotation(_DEQ_str);
1085                 code.addLine(prefixTabs
1086                                 + "\t"
1087                                 + DeclareDefine(SpecNaming.AnnoInterfaceInfo, "*"
1088                                                 + SpecNaming.AnnoInterfaceInfoInst,
1089                                                 SpecNaming.CreateInterfaceBeginAnnoFunc
1090                                                                 + Brace(SpecNaming.AppendStr(name))));
1091                 // Call the actual function
1092                 code.addLine(prefixTabs + "\t"
1093                                 + ShortComment("Call the actual function"));
1094                 // bool RET = dequeue_ORIGINAL__(q, retVal, reclaimNode);
1095                 code.addLine(prefixTabs + "\t"
1096                                 + construct.getFunctionHeader().getRenamedCall() + ";");
1097                 code.addLine("");
1098
1099                 // Initialize the value struct
1100                 code.addLine(prefixTabs + "\t"
1101                                 + ShortComment("Initialize the value struct"));
1102                 // The very first assignment "
1103                 code.addLine(prefixTabs + "\t"
1104                                 + DeclareDefine(name, "*value", "new " + name));
1105                 // Don't leave out the RET field
1106                 if (!construct.getFunctionHeader().isReturnVoid())
1107                         code.addLine(prefixTabs + "\t"
1108                                         + AssignToPtr("value", SpecNaming.RET, SpecNaming.RET));
1109                 // For arguments
1110                 for (VariableDeclaration decl : construct.getFunctionHeader().args)
1111                         code.addLine(prefixTabs + "\t"
1112                                         + AssignToPtr("value", decl.name, decl.name));
1113                 code.addLine("");
1114
1115                 // Store the value info into the current MethodCall
1116                 code.addLine(prefixTabs
1117                                 + "\t"
1118                                 + ShortComment("Store the value info into the current MethodCall"));
1119                 code.addLine(prefixTabs
1120                                 + "\t"
1121                                 + AssignToPtr(SpecNaming.AnnoInterfaceInfoInst, "value",
1122                                                 "value"));
1123                 code.addLine("");
1124
1125                 // Return if necessary
1126                 if (!construct.getFunctionHeader().isReturnVoid())
1127                         code.addLine(prefixTabs + "\treturn " + SpecNaming.RET + ";");
1128                 code.addLine(prefixTabs + "}");
1129
1130                 return code;
1131         }
1132
1133         /**
1134          * <p>
1135          * Write a list of lines (as the whole of the file) to a file ---
1136          * newFileName. If that file does not exist, we create that file and then
1137          * write the lines.
1138          * </p>
1139          * 
1140          * @param newFileName
1141          *            The name of the file to be written
1142          * @param content
1143          *            The list of lines that as a whole become the content of the
1144          *            file
1145          */
1146         public static void write2File(String newFileName, ArrayList<String> content) {
1147                 File newFile = new File(newFileName);
1148                 newFile.getParentFile().mkdirs();
1149                 if (!newFile.exists()) {
1150                         try {
1151                                 newFile.createNewFile();
1152                         } catch (IOException e) {
1153                                 e.printStackTrace();
1154                         }
1155                 }
1156                 BufferedWriter bw = null;
1157                 try {
1158                         bw = new BufferedWriter(new FileWriter(newFile));
1159                         for (int i = 0; i < content.size(); i++) {
1160                                 bw.write(content.get(i) + "\n");
1161                         }
1162                         bw.flush();
1163                 } catch (IOException e) {
1164                         e.printStackTrace();
1165                 } finally {
1166                         if (bw != null)
1167                                 try {
1168                                         bw.close();
1169                                 } catch (IOException e) {
1170                                         e.printStackTrace();
1171                                 }
1172                 }
1173         }
1174 }