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