7e4ab29c446c74db5683957664c197fe86600aa7
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
1 package edu.uci.eecs.specCompiler.codeGenerator;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.io.File;
7
8 import edu.uci.eecs.specCompiler.grammerParser.utilParser.UtilParser;
9 import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
10 import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.CommutativityRule;
14 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
15 import edu.uci.eecs.specCompiler.specExtraction.Construct;
16 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
17 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
22 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
23
24 /**
25  * <p>
26  * Defines a list of commonly used constant strings.
27  * </p>
28  * 
29  * @author peizhaoo
30  * 
31  */
32 public class CodeVariables {
33         // C++ code or library
34         public static final String HEADER_STDLIB = "<stdlib.h>";
35         public static final String HEADER_THREADS = "<threads.h>";
36         public static final String HEADER_STDINT = "<stdint.h>";
37         public static final String HEADER_MODELMEMORY = "<model_memory.h>";
38         public static final String HEADER_MODELTYPES = "<modeltypes.h>";
39         public static final String ThreadIDType = "thread_id_t";
40         public static final String BOOLEAN = "bool";
41         public static final String UINT64 = "uint64_t";
42
43         // Model checker code
44         public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
45         public static final String HEADER_COMMON = "<common.h>";
46         public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
47         public static final String HEADER_CDSTRACE = "<cdstrace.h>";
48         public static final String CDSAnnotate = "cdsannotate";
49         // public static final String CDSAnnotate = "cdsannotate";
50         public static final String CDSAnnotateType = "SPEC_ANALYSIS";
51         public static final String IDType = "call_id_t";
52
53         public static final String SPEC_ANNO_TYPE = "spec_anno_type";
54         public static final String SPEC_ANNO_TYPE_INIT = "INIT";
55         public static final String SPEC_ANNO_TYPE_HB_RULE = "HB_RULE";
56         public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
57         public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
58         public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
59         public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
60         public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
61         public static final String SPEC_ANNO_TYPE_CP_CLEAR = "CP_CLEAR";
62         public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
63         public static final String SPEC_ANNOTATION = "spec_annotation";
64         public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
65         public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
66
67         public static final String ANNO_INIT = "anno_init";
68         public static final String HB_RULE = "hb_rule";
69         public static final String COMMUTATIVITY_RULE = "commutativity_rule";
70         public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
71         public static final String ANNO_INTERFACE_END = "anno_interface_end";
72         public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
73         public static final String ANNO_CP_DEFINE = "anno_cp_define";
74         public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
75         public static final String ANNO_CP_CLEAR = "anno_cp_clear";
76         public static final String ANNO_HB_CONDITION = "anno_hb_condition";
77
78         // Specification variables
79         public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
80         public static final String DEFAULT_ID = "0";
81
82         // Specification library
83         public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
84         public static final String SPEC_QUEUE = "spec_queue";
85         public static final String SPEC_STACK = "spec_stack";
86         public static final String SPEC_DEQUE = "spec_deque";
87         public static final String SPEC_HASHTABLE = "spec_hashtable";
88         public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
89         public static final String SPEC_TAG = "spec_tag";
90         public static final String SPEC_TAG_CURRENT = "current";
91         public static final String SPEC_TAG_NEXT = "next";
92
93         // Macro
94         public static final String MACRO_ID = "__ID__";
95         public static final String MACRO_COND = "__COND_SAT__";
96         public static final String MACRO_RETURN = "__RET__";
97         public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
98         public static final String MACRO_THREAD_ID = "__TID__";
99
100         public static void printCode(ArrayList<String> code) {
101                 for (int i = 0; i < code.size(); i++) {
102                         System.out.println(code.get(i));
103                 }
104         }
105
106         private static String COMMENT(String comment) {
107                 return "/* " + comment + " */";
108         }
109
110         private static String SHORT_COMMENT(String comment) {
111                 return " // " + comment;
112         }
113
114         private static String INCLUDE(String header) {
115                 return "#include " + header;
116         }
117
118         private static String DEFINE(String left, String right) {
119                 return "#define " + left + " " + right;
120         }
121
122         private static String UNDEFINE(String macro) {
123                 return "#undef " + macro;
124         }
125
126         private static String GET_FIELD_BY_PTR(String ptr, String field) {
127                 return ptr + "->" + field;
128         }
129
130         private static String GET_FIELD(String var, String field) {
131                 return var + "->" + field;
132         }
133
134         private static String BRACE(String val) {
135                 return "(" + val + ")";
136         }
137
138         private static String ASSIGN(String structName, String field, String val) {
139                 return structName + "." + field + " = " + val + ";";
140         }
141
142         private static String ASSIGN(String varName, String val) {
143                 return varName + " = " + val + ";";
144         }
145
146         private static String ASSIGN_PTR(String structName, String field, String val) {
147                 return structName + "." + field + " = &" + val + ";";
148         }
149
150         private static String ASSIGN_TO_PTR(String structName, String field,
151                         String val) {
152                 return structName + "->" + field + " = " + val + ";";
153         }
154
155         private static String ASSIGN_PTR_TO_PTR(String structName, String field,
156                         String val) {
157                 return structName + "->" + field + " = &" + val + ";";
158         }
159
160         private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
161                 return "struct " + type + " *" + name + " = (struct " + type
162                                 + "*) malloc(sizeof(struct " + type + "));";
163         }
164
165         private static String DECLARE(String type, String name) {
166                 return type + " " + name + ";";
167         }
168
169         private static String DECLARE(VariableDeclaration varDecl) {
170                 String type = varDecl.type, name = varDecl.name;
171                 return type + " " + name + ";";
172         }
173
174         private static String DECLARE_DEFINE(String type, String var, String val) {
175                 return type + " " + var + " = " + val + ";";
176         }
177
178         private static String ANNOTATE(SemanticsChecker semantics, String structName) {
179                 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
180         }
181
182         private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
183                         FunctionHeader header) {
184                 ArrayList<String> code = new ArrayList<String>();
185                 code.add("typedef struct " + interfaceName + "_info {");
186                 if (!header.returnType.equals("void")) {
187                         code.add(DECLARE(header.returnType, MACRO_RETURN));
188                 }
189                 for (int i = 0; i < header.args.size(); i++) {
190                         code.add(DECLARE(header.args.get(i)));
191                 }
192                 code.add("} " + interfaceName + "_info;");
193                 return code;
194         }
195
196         private static ArrayList<String> DEFINE_ID_FUNC(
197                         InterfaceConstruct construct, FunctionHeader header) {
198                 String interfaceName = construct.name;
199                 ArrayList<String> code = new ArrayList<String>();
200                 String idCode = construct.idCode;
201                 code.add("inline static " + IDType + " " + interfaceName + "_id("
202                                 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
203
204                 // Read info struct
205                 if (!header.returnType.equals("void") || header.args.size() != 0) {
206                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
207                         code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
208                                         infoStructName, BRACE(infoStructType + "*") + "info"));
209                         if (!header.returnType.equals("void")) {
210                                 code.add((DECLARE_DEFINE("\t" + header.returnType,
211                                                 MACRO_RETURN,
212                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
213                         }
214                         for (int i = 0; i < header.args.size(); i++) {
215                                 String type = header.args.get(i).type, var = header.args.get(i).name;
216                                 code.add("\t"
217                                                 + (DECLARE_DEFINE(type, var,
218                                                                 GET_FIELD_BY_PTR(infoStructName, var))));
219                         }
220                         code.add("");
221                 }
222
223                 if (!idCode.equals("")) {
224                         code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
225                 } else {
226                         code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
227                 }
228                 code.add("\treturn " + MACRO_ID + ";");
229                 code.add("}");
230                 return code;
231         }
232
233         private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
234                         InterfaceConstruct construct, FunctionHeader header) {
235                 String interfaceName = construct.name;
236                 ArrayList<String> code = new ArrayList<String>();
237                 code.add("inline static bool " + interfaceName
238                                 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
239                                 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
240                 code.add("\t" + DECLARE("bool", "check_passed"));
241                 // Read info struct
242                 if (!header.returnType.equals("void") || header.args.size() != 0) {
243                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
244                         code.add("\t"
245                                         + DECLARE_DEFINE(infoStructType + "*", infoStructName,
246                                                         BRACE(infoStructType + "*") + "info"));
247                         if (!header.returnType.equals("void")) {
248                                 code.add("\t"
249                                                 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
250                                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
251                         }
252                         for (int i = 0; i < header.args.size(); i++) {
253                                 String type = header.args.get(i).type, var = header.args.get(i).name;
254                                 code.add("\t"
255                                                 + (DECLARE_DEFINE(type, var,
256                                                                 GET_FIELD_BY_PTR(infoStructName, var))));
257                         }
258                         code.add("");
259                 }
260                 // __COND_SAT
261                 if (!construct.condition.equals("")) {
262                         code.add("\t"
263                                         + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
264                 }
265                 // Check
266                 if (!construct.check.equals("")) {
267                         code.add("\t" + ASSIGN("check_passed", construct.check));
268                         code.add("\tif (!check_passed)");
269                         code.add("\t\treturn false;");
270
271                 }
272                 // Action
273                 if (construct.action.size() > 0) {
274                         addAllCodeWithIndent(code, construct.action, "\t");
275                 }
276                 // Post_check
277                 if (!construct.postCheck.equals("")) {
278                         code.add("\t" + ASSIGN("check_passed", construct.postCheck));
279                         code.add("\tif (!check_passed)");
280                         code.add("\t\treturn false;");
281                 }
282                 // Post_action
283                 if (construct.postAction.size() > 0) {
284                         addAllCodeWithIndent(code, construct.postAction, "\t");
285                 }
286                 // Return true finally
287                 code.add("\treturn true;");
288
289                 code.add("}");
290
291                 return code;
292         }
293
294         private static void addAllCodeWithIndent(ArrayList<String> allCode,
295                         ArrayList<String> target, String indent) {
296                 for (int i = 0; i < target.size(); i++) {
297                         allCode.add(indent + target.get(i));
298                 }
299         }
300
301         public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
302                 HashSet<String> headers = new HashSet<String>();
303                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
304                         File f = semantics.interfaceName2Construct.get(interfaceName).file;
305                         headers.addAll(semantics.srcFilesInfo.get(f).headers);
306                 }
307                 headers.add(HEADER_STDLIB);
308                 headers.add(HEADER_STDINT);
309                 headers.add(HEADER_MODELMEMORY);
310                 headers.add(HEADER_MODELTYPES);
311                 headers.add(HEADER_SPEC_LIB);
312                 headers.add(HEADER_STDINT);
313                 headers.add(HEADER_CDSANNOTATE);
314                 // headers.add(HEADER_COMMON);
315                 headers.add(HEADER_SPECANNOTATION);
316                 return headers;
317         }
318
319         private static void makeFunctionStatic(ArrayList<String> funcDefine) {
320                 String headLine = funcDefine.get(0);
321                 headLine = "inline static " + headLine;
322                 funcDefine.set(0, headLine);
323         }
324
325         private static String makeVariablesStatic(VariableDeclaration varDecl) {
326                 String res = "static " + varDecl.type + " " + varDecl.name + ";";
327                 return res;
328         }
329
330         private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
331                         Construct construct) {
332                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
333                 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
334                 if (headerLine.startsWith("template")) {
335                         templateLine = headerLine;
336                         headerLine = content.get(construct.beginLineNum + 2);
337                 }
338                 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
339                 try {
340                         FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
341                         if (templateLine != null) {
342                                 ArrayList<VariableDeclaration> templateArgs = UtilParser
343                                                 .getTemplateArg(templateLine);
344                                 header.setTemplateList(templateArgs);
345                         }
346                         return header;
347                 } catch (ParseException e) {
348                         e.printStackTrace();
349                 }
350                 return null;
351         }
352
353         public static ArrayList<String> generateGlobalVarDeclaration(
354                         SemanticsChecker semantics, GlobalConstruct construct) {
355                 ArrayList<String> newCode = new ArrayList<String>();
356                 HashSet<String> allHeaders = getAllHeaders(semantics);
357
358                 SequentialDefineSubConstruct code = construct.code;
359                 // User-defined structs first
360                 newCode.add(COMMENT("All other user-defined structs"));
361                 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
362                 for (int i = 0; i < declareStructs.size(); i++) {
363                         ArrayList<String> declareStruct = declareStructs.get(i);
364                         newCode.addAll(declareStruct);
365                         newCode.add("");
366                 }
367                 // User-defined variables
368                 ArrayList<VariableDeclaration> varDecls = code.declareVar;
369                 for (int i = 0; i < varDecls.size(); i++) {
370                         VariableDeclaration varDecl = varDecls.get(i);
371                         // Don't forget to make them static
372                         newCode.add(makeVariablesStatic(varDecl));
373                 }
374                 // User-defined functions
375                 newCode.add(COMMENT("All other user-defined functions"));
376                 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
377                 for (int i = 0; i < defineFuncs.size(); i++) {
378                         ArrayList<String> defineFunc = defineFuncs.get(i);
379                         makeFunctionStatic(defineFunc);
380                         newCode.addAll(defineFunc);
381                         newCode.add("");
382                 }
383
384                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
385                         InterfaceConstruct iConstruct = semantics.interfaceName2Construct
386                                         .get(interfaceName);
387                         FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
388                         // Define necessary info structure
389                         if (!funcHeader.returnType.equals("void")
390                                         || funcHeader.args.size() > 0) {
391                                 newCode.add(COMMENT("Definition of interface info struct: "
392                                                 + interfaceName));
393                                 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
394                                 newCode.add(COMMENT("End of info struct definition: "
395                                                 + interfaceName));
396                                 newCode.add("");
397                         }
398
399                         // Define ID function
400                         newCode.add(COMMENT("ID function of interface: " + interfaceName));
401                         newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
402                         newCode.add(COMMENT("End of ID function: " + interfaceName));
403                         newCode.add("");
404
405                         // Define check_action function
406                         newCode.add(COMMENT("Check action function of interface: "
407                                         + interfaceName));
408                         newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
409                         newCode.add(COMMENT("End of check action function: "
410                                         + interfaceName));
411                         newCode.add("");
412                 }
413                 // Interface function pointer table
414                 String interfaceSize = Integer
415                                 .toString(semantics.interfaceName2Construct.size());
416                 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
417                 // Make it static
418                 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
419                 // Happens-before initialization rules
420                 // Should make it static
421                 newCode.add("static " + DECLARE(HB_RULE + "**", "hb_rule_table"));
422
423                 // Declare the Commutativity Rule table
424                 newCode.add("static "
425                                 + DECLARE(COMMUTATIVITY_RULE + "**", "commutativity_rule_table"));
426                 // Define the Commutativity Rule condition functions
427                 ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
428                 for (int i = 0; i < rules.size(); i++) {
429                         CommutativityRule rule = rules.get(i);
430                         String infoStructType1 = rule.method1 + "_info";
431                         String infoStructType2 = rule.method2 + "_info";
432                         String condition = rule.condition;
433                         String conditionFuncName = "CommutativityCondition"
434                                         + Integer.toString(i);
435
436                         // Replace the "_M1." and "_M2." with the actual info struct
437                         condition = condition.replaceAll("_Method1 \\.", "_info1->");
438                         condition = condition.replaceAll("_Method2 \\.", "_info2->");
439
440                         // Declare the signature of the condition function
441                         newCode.add("inline static bool " + conditionFuncName
442                                         + "(void *info1, void *info2) {");
443
444                         // Cast the "void*" type to the actual info type
445                         newCode.add("\t"
446                                         + DECLARE_DEFINE(infoStructType1, "*_info1", "("
447                                                         + infoStructType1 + "*) info1"));
448                         newCode.add("\t"
449                                         + DECLARE_DEFINE(infoStructType2, "*_info2", "("
450                                                         + infoStructType2 + "*) info2"));
451                         newCode.add("\treturn " + condition + ";");
452
453                         // End of the condition function
454                         newCode.add("}");
455                 }
456
457                 newCode.add("");
458
459                 // Beginning initialization
460                 // Define the __SPEC_INIT__ function to initialize user-defined
461                 // variables
462                 newCode.add(COMMENT("Initialization of sequential varialbes"));
463                 newCode.add("static void __SPEC_INIT__() {");
464                 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
465                 newCode.add("}");
466                 newCode.add("");
467
468                 // Define the __SPEC_CLEAN__ function for clean-up
469                 newCode.add(COMMENT("Cleanup routine of sequential variables"));
470                 newCode.add("static void __SPEC_CLEANUP__() {");
471                 addAllCodeWithIndent(newCode, construct.code.cleanupCode, "\t");
472                 newCode.add("}");
473                 newCode.add("");
474
475                 newCode.add(COMMENT("Define function for sequential code initialization"));
476                 newCode.add("inline static void __sequential_init() {");
477
478                 // Init func_ptr_table
479                 newCode.add("\t" + COMMENT("Init func_ptr_table"));
480                 newCode.add("\t"
481                                 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
482                                                 + semantics.interface2Num.size() + " * 2)"));
483                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
484                         String interfaceNum = Integer.toString(semantics.interface2Num
485                                         .get(interfaceName));
486                         newCode.add("\t"
487                                         + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
488                                                         "(void*) &" + interfaceName + "_id"));
489                         newCode.add("\t"
490                                         + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
491                                                         "(void*) &" + interfaceName + "_check_action"));
492                 }
493
494                 // Init Happens-before rules table
495                 newCode.addAll(generateHBInitAnnotation(semantics));
496
497                 // Init Commutativity rules table
498                 newCode.addAll(generateCommutativityAnnotation(semantics));
499
500                 // Pass init info, including function table info & HB rules
501                 newCode.add("\t"
502                                 + COMMENT("Pass init info, including function table info & HB rules & Commutativity Rules"));
503                 String structName = "anno_init", anno = "init";
504                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
505                 newCode.add("\t"
506                                 + ASSIGN_TO_PTR(structName, "init_func",
507                                                 "(void_func_t*) __SPEC_INIT__"));
508                 newCode.add("\t"
509                                 + ASSIGN_TO_PTR(structName, "cleanup_func",
510                                                 "(void_func_t*) __SPEC_CLEANUP__"));
511                 newCode.add("\t"
512                                 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
513                 newCode.add("\t"
514                                 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
515                 newCode.add("\t"
516                                 + ASSIGN_TO_PTR(structName, "hb_rule_table", "hb_rule_table"));
517                 newCode.add("\t"
518                                 + ASSIGN_TO_PTR(structName, "hb_rule_table_size",
519                                                 "HB_RULE_TABLE_SIZE"));
520                 newCode.add("\t"
521                                 + ASSIGN_TO_PTR(structName, "commutativity_rule_table",
522                                                 "commutativity_rule_table"));
523                 newCode.add("\t"
524                                 + ASSIGN_TO_PTR(structName, "commutativity_rule_table_size",
525                                                 Integer.toString(rules.size())));
526
527                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
528                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
529                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
530                 newCode.add("\t" + ANNOTATE(semantics, anno));
531
532                 newCode.add("");
533                 newCode.add("}");
534                 newCode.add("");
535
536                 newCode.add(COMMENT("End of Global construct generation in class"));
537
538                 // printCode(newCode);
539                 return newCode;
540         }
541
542         public static ArrayList<String> generateStaticVarDefine(
543                         SemanticsChecker semantics, GlobalConstruct construct) {
544                 ArrayList<String> newCode = new ArrayList<String>();
545                 String className = semantics.getClassName();
546                 if (className == null)
547                         return newCode; // No need to define any static variables
548                 String templateList = semantics.getTemplateStr();
549                 String varPrefix;
550                 if (templateList == null) {
551                         varPrefix = className + "::";
552                 } else {
553                         varPrefix = className + templateList + "::";
554                 }
555                 String templateDecl = semantics.getTemplateFullStr();
556                 if (templateList == null) {
557                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
558                         newCode.add(DECLARE("hb_rule**", varPrefix + "hb_rule_table"));
559                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
560                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
561                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
562                         }
563                 } else {
564                         newCode.add(templateDecl);
565                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
566                         newCode.add(templateDecl);
567                         newCode.add(DECLARE("hb_rule**", varPrefix + "hb_rule_table"));
568                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
569                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
570                                 newCode.add(templateDecl);
571                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
572                         }
573                 }
574                 return newCode;
575         }
576
577         private static ArrayList<String> generateHBInitAnnotation(
578                         SemanticsChecker semantics) {
579                 ArrayList<String> newCode = new ArrayList<String>();
580
581                 int hbConditionInitIdx = 0;
582                 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
583                         for (ConditionalInterface right : semantics.getHBConditions().get(
584                                         left)) {
585                                 String structVarName = "hbConditionInit" + hbConditionInitIdx;
586                                 // String annotationVarName = "hb_rule" + hbConditionInitIdx;
587                                 hbConditionInitIdx++;
588                                 String interfaceNumBefore = Integer
589                                                 .toString(semantics.interface2Num
590                                                                 .get(left.interfaceName)), hbLabelNumBefore = Integer
591                                                 .toString(semantics.hbLabel2Num
592                                                                 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
593                                                 .toString(semantics.interface2Num
594                                                                 .get(right.interfaceName)), hbLabelNumAfter = Integer
595                                                 .toString(semantics.hbLabel2Num
596                                                                 .get(right.hbConditionLabel));
597                                 newCode.add("\t" + COMMENT(left + " -> " + right));
598
599                                 newCode.add("\t"
600                                                 + STRUCT_NEW_DECLARE_DEFINE(HB_RULE, structVarName));
601                                 newCode.add("\t"
602                                                 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
603                                                                 interfaceNumBefore)
604                                                 + SHORT_COMMENT(left.interfaceName));
605                                 newCode.add("\t"
606                                                 + ASSIGN_TO_PTR(structVarName,
607                                                                 "hb_condition_num_before", hbLabelNumBefore)
608                                                 + SHORT_COMMENT(left.hbConditionLabel));
609                                 newCode.add("\t"
610                                                 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
611                                                                 interfaceNumAfter)
612                                                 + SHORT_COMMENT(right.interfaceName));
613                                 newCode.add("\t"
614                                                 + ASSIGN_TO_PTR(structVarName,
615                                                                 "hb_condition_num_after", hbLabelNumAfter)
616                                                 + SHORT_COMMENT(right.hbConditionLabel));
617                         }
618                 }
619                 // Init hb_rule_table
620                 newCode.add("\t" + COMMENT("Init hb_rule_table"));
621                 newCode.add("\t"
622                                 + ASSIGN("hb_rule_table", "(" + HB_RULE
623                                                 + "**) malloc(sizeof(" + HB_RULE + "*) * "
624                                                 + hbConditionInitIdx + ")"));
625                 // Define HB_RULE_TABLE_SIZE
626                 newCode.add("\t"
627                                 + DEFINE("HB_RULE_TABLE_SIZE",
628                                                 Integer.toString(hbConditionInitIdx)));
629                 for (int i = 0; i < hbConditionInitIdx; i++) {
630                         newCode.add("\t"
631                                         + ASSIGN("hb_rule_table[" + i + "]", "hbConditionInit" + i));
632                 }
633                 return newCode;
634         }
635
636         private static ArrayList<String> generateCommutativityAnnotation(
637                         SemanticsChecker semantics) {
638                 ArrayList<String> newCode = new ArrayList<String>();
639                 ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
640
641                 // Init commutativity_rule_table
642                 newCode.add("\t" + COMMENT("Init commutativity_rule_table"));
643
644                 // Users have not defined any commutativity rules
645                 if (rules.size() == 0)
646                         return newCode;
647
648                 newCode.add("\t"
649                                 + ASSIGN("commutativity_rule_table", "(" + COMMUTATIVITY_RULE
650                                                 + "**) malloc(sizeof(" + COMMUTATIVITY_RULE + "*) * "
651                                                 + rules.size() + ")"));
652
653                 // Declare a rule pointer
654                 newCode.add("\t" + DECLARE("commutativity_rule*", "rule"));
655
656                 for (int i = 0; i < rules.size(); i++) {
657                         CommutativityRule rule = rules.get(i);
658                         String interfaceNumBefore = Integer
659                                         .toString(semantics.interface2Num.get(rule.method1));
660                         String interfaceNumAfter = Integer.toString(semantics.interface2Num
661                                         .get(rule.method2));
662                         String conditionFuncName = "CommutativityCondition" + i;
663
664                         // Construct a new rule
665                         newCode.add("\t"
666                                         + ASSIGN("rule",
667                                                         "(commutativity_rule*) malloc (sizeof(commutativity_rule))"));
668                         newCode.add("\t"
669                                         + ASSIGN_TO_PTR("rule", "interface_num_before",
670                                                         interfaceNumBefore));
671                         newCode.add("\t"
672                                         + ASSIGN_TO_PTR("rule", "interface_num_after",
673                                                         interfaceNumAfter));
674                         newCode.add("\t"
675                                         + ASSIGN_TO_PTR("rule", "rule",
676                                                         "\"" + rule.condition + "\""));
677                         
678                         newCode.add("\t"
679                                         + ASSIGN_TO_PTR("rule", "condition", conditionFuncName));
680
681                         // Assign the rule to the corresponding commutativity table slot
682                         newCode.add("\t"
683                                         + ASSIGN("commutativity_rule_table[" + i + "]", "rule"));
684                 }
685
686                 return newCode;
687         }
688
689         public static ArrayList<String> generateEntryPointInitCall() {
690                 ArrayList<String> newCode = new ArrayList<String>();
691                 newCode.add("\t" + "__sequential_init();");
692                 return newCode;
693         }
694
695         public static ArrayList<String> generateInterfaceWrapperDeclaration(
696                         SemanticsChecker semantics, InterfaceConstruct construct) {
697                 FunctionHeader header = getFunctionHeader(semantics, construct);
698                 ArrayList<String> declaration = new ArrayList<String>();
699                 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
700                                 .getDeclaration() + ";");
701                 return declaration;
702         }
703
704         // Only generate the definition of the wrapper, don't do any renaming
705         public static ArrayList<String> generateInterfaceWrapperDefinition(
706                         SemanticsChecker semantics, InterfaceConstruct construct) {
707                 ArrayList<String> newCode = new ArrayList<String>();
708                 String interfaceName = construct.name;
709
710                 FunctionHeader header = getFunctionHeader(semantics, construct);
711                 String interfaceNum = Integer.toString(semantics.interface2Num
712                                 .get(construct.name));
713
714                 newCode.add(header.getTemplateFullStr());
715                 newCode.add(header.getFuncStr() + " {");
716                 // Wrapper function body
717                 newCode.add("\t" + COMMENT("Interface begins"));
718                 // Interface begin
719                 String structName = "interface_begin";
720                 newCode.add("\t"
721                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
722                                                 "interface_begin"));
723                 newCode.add("\t"
724                                 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
725                                 + SHORT_COMMENT(construct.name));
726                 newCode.add("\t\t"
727                                 + ASSIGN_TO_PTR(structName, "interface_name", "\""
728                                                 + construct.name + "\""));
729
730                 String anno = "annotation_interface_begin";
731                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
732                 newCode.add("\t"
733                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
734                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
735                 newCode.add("\t" + ANNOTATE(semantics, anno));
736                 // Call original renamed function
737                 if (header.returnType.equals("void")) {
738                         newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
739                                         + ";");
740                 } else {
741                         newCode.add("\t"
742                                         + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
743                                                         header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
744                 }
745                 // HB conditions
746                 for (String label : construct.hbConditions.keySet()) {
747                         String condition = construct.hbConditions.get(label);
748                         String hbCondNum = Integer.toString(semantics.hbLabel2Num
749                                         .get(label));
750                         newCode.add("\t" + "if " + BRACE(condition) + " {");
751                         structName = "hb_condition";
752                         newCode.add("\t\t"
753                                         + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
754                         newCode.add("\t\t"
755                                         + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
756                                         + SHORT_COMMENT(construct.name));
757
758                         newCode.add("\t\t"
759                                         + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
760                         anno = "annotation_hb_condition";
761                         newCode.add("\t\t"
762                                         + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
763                         newCode.add("\t\t"
764                                         + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
765                         newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
766                         newCode.add("\t\t" + ANNOTATE(semantics, anno));
767                         newCode.add("\t" + "}");
768                         newCode.add("");
769                 }
770                 // Also add the true condition if any
771                 if (semantics.containsConditionalInterface(new ConditionalInterface(
772                                 interfaceName, ""))) {
773                         structName = "hb_condition";
774                         newCode.add("\t"
775                                         + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
776                         newCode.add("\t"
777                                         + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
778                                         + SHORT_COMMENT(construct.name));
779                         newCode.add("\t"
780                                         + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
781                         anno = "annotation_hb_condition";
782                         newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
783                         newCode.add("\t"
784                                         + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
785                         newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
786                         newCode.add("\t" + ANNOTATE(semantics, anno));
787                         newCode.add("");
788                 }
789                 // Interface end
790                 String infoStructType = null, infoName = null;
791                 if (!header.returnType.equals("void") || header.args.size() > 0) {
792                         infoStructType = interfaceName + "_info";
793                         infoName = "info";
794                         newCode.add("\t"
795                                         + DECLARE_DEFINE(infoStructType + "*", infoName,
796                                                         BRACE(infoStructType + "*") + " malloc(sizeof("
797                                                                         + infoStructType + "))"));
798                         if (!header.returnType.equals("void")) {
799                                 newCode.add("\t"
800                                                 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
801                         }
802                         for (int i = 0; i < header.args.size(); i++) {
803                                 String argName = header.args.get(i).name;
804                                 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
805                         }
806                 } else {
807                         infoName = "NULL";
808                 }
809                 structName = "interface_end";
810                 anno = "annoation_interface_end";
811                 newCode.add("\t"
812                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
813                 newCode.add("\t"
814                                 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
815                                 + SHORT_COMMENT(construct.name));
816                 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
817                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
818                 newCode.add("\t"
819                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
820                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
821                 newCode.add("\t" + ANNOTATE(semantics, anno));
822                 // Return __RET__ if it's not void
823                 if (!header.returnType.equals("void")) {
824                         newCode.add("\t" + "return " + MACRO_RETURN + ";");
825                 }
826                 // End of the wrapper function
827                 newCode.add("}");
828
829                 // printCode(newCode);
830                 return newCode;
831         }
832
833         // Rename the interface name for declaration or definition
834         public static void renameInterface(SemanticsChecker semantics,
835                         Construct construct) {
836                 FunctionHeader header = getFunctionHeader(semantics, construct);
837                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
838                 int lineNum = construct.beginLineNum;
839                 String headerLine = content.get(construct.beginLineNum);
840                 if (headerLine.startsWith("template")) {
841                         headerLine = content.get(construct.beginLineNum + 1);
842                         lineNum++;
843                 }
844                 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
845                                 .toString();
846                 String oldLine = content.get(lineNum + 1);
847
848                 if (construct instanceof InterfaceConstruct) {
849                         InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
850                         InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
851                                         .get(iConstruct.name);
852                         if (defineConstruct != null) { // There is a defineConstruct
853                                 newLine = newLine + " ;";
854                                 renameInterface(semantics, defineConstruct);
855                         } else { // This is a declare & define construct
856                                 if (oldLine.indexOf('{') != -1)
857                                         newLine = newLine + " {";
858                         }
859                 } else {
860                         if (oldLine.indexOf('{') != -1)
861                                 newLine = newLine + " {";
862                 }
863
864                 content.set(lineNum + 1, newLine);
865         }
866
867         public static void addAtomicReturn(SemanticsChecker semantics,
868                         Construct construct) {
869                 int lineNum = construct.beginLineNum - 1;
870                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
871                 String oldLine = content.get(lineNum);
872                 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
873                 content.set(lineNum, newLine);
874         }
875
876         public static ArrayList<String> generatePotentialCPDefine(
877                         SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
878                 ArrayList<String> newCode = new ArrayList<String>();
879                 // Add atomic return variable if the predicate accesses to it
880                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
881                         addAtomicReturn(semantics, construct);
882                 }
883                 // Generate redundant header files
884                 newCode.add("\t"
885                                 + COMMENT("Automatically generated code for potential commit point: "
886                                                 + construct.label));
887                 newCode.add("");
888                 // Add annotation
889                 newCode.add("\t" + "if (" + construct.condition + ") {");
890                 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
891                 newCode.add("\t\t"
892                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
893                                                 structName));
894                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
895                                 .get(construct.label));
896                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
897                 newCode.add("\t\t"
898                                 + ASSIGN_TO_PTR(structName, "label_name", "\""
899                                                 + construct.label + "\""));
900
901                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
902                 newCode.add("\t\t"
903                                 + ASSIGN_TO_PTR(anno, "type",
904                                                 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
905                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
906                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
907                 newCode.add("\t" + "}");
908                 return newCode;
909         }
910
911         public static String getCPInterfaceNum(SemanticsChecker semantics,
912                         String commitPointLabel) {
913                 HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
914                 InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
915                 String interfaceName = iConstruct.name;
916                 String interfaceNum = Integer.toString(semantics.interface2Num
917                                 .get(interfaceName));
918                 return interfaceNum;
919         }
920
921         /**
922          * <p>
923          * Commit point define check should be unique to each interface, meaning
924          * that they are not shared between different interfaces
925          * </p>
926          * 
927          * @param semantics
928          * @param construct
929          * @return
930          */
931         public static ArrayList<String> generateCPDefineCheck(
932                         SemanticsChecker semantics, CPDefineCheckConstruct construct) {
933                 ArrayList<String> newCode = new ArrayList<String>();
934                 // Add atomic return variable if the predicate accesses to it
935                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
936                         addAtomicReturn(semantics, construct);
937                 }
938                 // Generate redundant header files
939                 newCode.add("\t"
940                                 + COMMENT("Automatically generated code for commit point define check: "
941                                                 + construct.label));
942                 newCode.add("");
943                 // Add annotation
944
945                 newCode.add("\t" + "if (" + construct.condition + ") {");
946                 String structName = "cp_define_check", anno = "annotation_cp_define_check";
947                 newCode.add("\t\t"
948                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
949                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
950                                 .get(construct.label));
951                 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
952                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
953                 newCode.add("\t\t"
954                                 + ASSIGN_TO_PTR(structName, "label_name", "\""
955                                                 + construct.label + "\""));
956                 newCode.add("\t\t"
957                                 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
958
959                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
960                 newCode.add("\t\t"
961                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
962                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
963                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
964                 newCode.add("\t" + "}");
965                 return newCode;
966         }
967
968         /**
969          * <p>
970          * Commit point define check should be unique to each interface, meaning
971          * that they are not shared between different interfaces
972          * </p>
973          * 
974          * @param semantics
975          * @param construct
976          * @return
977          */
978         public static ArrayList<String> generateCPClear(SemanticsChecker semantics,
979                         CPClearConstruct construct) {
980                 ArrayList<String> newCode = new ArrayList<String>();
981                 // Add atomic return variable if the predicate accesses to it
982                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
983                         addAtomicReturn(semantics, construct);
984                 }
985                 // Generate redundant header files
986                 newCode.add("\t"
987                                 + COMMENT("Automatically generated code for commit point clear: "
988                                                 + construct.label));
989                 newCode.add("");
990                 // Add annotation
991
992                 newCode.add("\t" + "if (" + construct.condition + ") {");
993                 String structName = "cp_clear", anno = "annotation_cp_clear";
994                 newCode.add("\t\t"
995                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
996                  
997                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
998                                 .get(construct.label));
999                 String labelName = construct.label;
1000                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_name",
1001                                 "\"" + labelName + "\""));
1002                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num",
1003                                 labelNum));
1004                 
1005                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
1006                 newCode.add("\t\t"
1007                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_CLEAR));
1008                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
1009                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
1010                 newCode.add("\t" + "}");
1011                 return newCode;
1012         }
1013
1014         /**
1015          * <p>
1016          * Commit point define should be unique to each interface, meaning that they
1017          * are not shared between different interfaces
1018          * </p>
1019          * 
1020          * @param semantics
1021          * @param construct
1022          * @return
1023          */
1024         public static ArrayList<String> generateCPDefine(
1025                         SemanticsChecker semantics, CPDefineConstruct construct) {
1026                 ArrayList<String> newCode = new ArrayList<String>();
1027                 // Generate redundant header files
1028                 newCode.add("\t"
1029                                 + COMMENT("Automatically generated code for commit point define: "
1030                                                 + construct.label));
1031                 newCode.add("");
1032                 // Add annotation
1033                 newCode.add("\t" + "if (" + construct.condition + ") {");
1034                 String structName = "cp_define", anno = "annotation_cp_define";
1035                 newCode.add("\t\t"
1036                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
1037                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
1038                                 .get(construct.label));
1039                 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
1040                 String potentialLabelNum = Integer
1041                                 .toString(semantics.commitPointLabel2Num
1042                                                 .get(construct.potentialCPLabel));
1043                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
1044                 newCode.add("\t\t"
1045                                 + ASSIGN_TO_PTR(structName, "label_name", "\""
1046                                                 + construct.label + "\""));
1047                 newCode.add("\t\t"
1048                                 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
1049                                                 potentialLabelNum));
1050                 newCode.add("\t\t"
1051                                 + ASSIGN_TO_PTR(structName, "potential_label_name", "\""
1052                                                 + construct.potentialCPLabel + "\""));
1053                 newCode.add("\t\t"
1054                                 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
1055                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
1056                 newCode.add("\t\t"
1057                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
1058                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
1059                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
1060                 newCode.add("\t" + "}");
1061                 return newCode;
1062         }
1063 }