more fix
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index b79d38b962202a170664a65a696e99bd579325b9..39ef342a6d15f87f49e50b2e945e5367557548e5 100644 (file)
@@ -1,24 +1,22 @@
 package edu.uci.eecs.specCompiler.codeGenerator;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.io.File;
 
-import edu.uci.eecs.specCompiler.grammerParser.ParseException;
-import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
+import edu.uci.eecs.specCompiler.grammerParser.utilParser.UtilParser;
+import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
 import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
 
 public class CodeVariables {
@@ -27,8 +25,8 @@ public class CodeVariables {
        public static final String HEADER_THREADS = "<threads.h>";
        public static final String HEADER_STDINT = "<stdint.h>";
        public static final String HEADER_MODELMEMORY = "<model_memory.h>";
-       public static final String ThreadIDType = "thrd_t";
-       public static final String GET_THREAD_ID = "thrd_current";
+       public static final String HEADER_MODELTYPES = "<modeltypes.h>";
+       public static final String ThreadIDType = "thread_id_t";
        public static final String BOOLEAN = "bool";
        public static final String UINT64 = "uint64_t";
 
@@ -37,8 +35,8 @@ public class CodeVariables {
        public static final String HEADER_COMMON = "<common.h>";
        public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
        public static final String HEADER_CDSTRACE = "<cdstrace.h>";
+       public static final String CDSAnnotate = "cdsannotate";
        // public static final String CDSAnnotate = "cdsannotate";
-       public static final String CDSAnnotate = "_Z11cdsannotatemPv";
        public static final String CDSAnnotateType = "SPEC_ANALYSIS";
        public static final String IDType = "call_id_t";
 
@@ -59,7 +57,7 @@ public class CodeVariables {
        public static final String ANNO_HB_INIT = "anno_hb_init";
        public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
        public static final String ANNO_INTERFACE_END = "anno_interface_end";
-       public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potentail_cp_define";
+       public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
        public static final String ANNO_CP_DEFINE = "anno_cp_define";
        public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
        public static final String ANNO_HB_CONDITION = "anno_hb_condition";
@@ -78,14 +76,13 @@ public class CodeVariables {
        public static final String SPEC_TAG = "spec_tag";
        public static final String SPEC_TAG_CURRENT = "current";
        public static final String SPEC_TAG_NEXT = "next";
-       
-       public static final String MODEL_MALLOC = "model_malloc";
 
        // Macro
        public static final String MACRO_ID = "__ID__";
        public static final String MACRO_COND = "__COND_SAT__";
        public static final String MACRO_RETURN = "__RET__";
        public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
+       public static final String MACRO_THREAD_ID = "__TID__";
 
        public static void printCode(ArrayList<String> code) {
                for (int i = 0; i < code.size(); i++) {
@@ -161,7 +158,7 @@ public class CodeVariables {
                return type + " " + var + " = " + val + ";";
        }
 
-       private static String ANNOTATE(String structName) {
+       private static String ANNOTATE(SemanticsChecker semantics, String structName) {
                return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
        }
 
@@ -179,16 +176,39 @@ public class CodeVariables {
                return code;
        }
 
-       private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
-                       String idCode) {
+       private static ArrayList<String> DEFINE_ID_FUNC(
+                       InterfaceConstruct construct, FunctionHeader header) {
+               String interfaceName = construct.name;
                ArrayList<String> code = new ArrayList<String>();
-               code.add("inline static " + IDType + " " + interfaceName + "_id() {");
+               String idCode = construct.idCode;
+               code.add("inline static " + IDType + " " + interfaceName + "_id("
+                               + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
+
+               // Read info struct
+               if (!header.returnType.equals("void") || header.args.size() != 0) {
+                       String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
+                       code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
+                                       infoStructName, BRACE(infoStructType + "*") + "info"));
+                       if (!header.returnType.equals("void")) {
+                               code.add((DECLARE_DEFINE("\t" + header.returnType,
+                                               MACRO_RETURN,
+                                               GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
+                       }
+                       for (int i = 0; i < header.args.size(); i++) {
+                               String type = header.args.get(i).type, var = header.args.get(i).name;
+                               code.add("\t"
+                                               + (DECLARE_DEFINE(type, var,
+                                                               GET_FIELD_BY_PTR(infoStructName, var))));
+                       }
+                       code.add("");
+               }
+
                if (!idCode.equals("")) {
-                       code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
+                       code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
                } else {
-                       code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
+                       code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
                }
-               code.add("return " + MACRO_ID + ";");
+               code.add("\treturn " + MACRO_ID + ";");
                code.add("}");
                return code;
        }
@@ -198,61 +218,84 @@ public class CodeVariables {
                String interfaceName = construct.name;
                ArrayList<String> code = new ArrayList<String>();
                code.add("inline static bool " + interfaceName
-                               + "_check_action(void *info, " + IDType + " " + MACRO_ID
-                               + ") {");
-               code.add(DECLARE("bool", "check_passed"));
+                               + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
+                               + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
+               code.add("\t" + DECLARE("bool", "check_passed"));
                // Read info struct
                if (!header.returnType.equals("void") || header.args.size() != 0) {
                        String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
-                       code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
-                                       BRACE(infoStructType + "*") + "info"));
+                       code.add("\t"
+                                       + DECLARE_DEFINE(infoStructType + "*", infoStructName,
+                                                       BRACE(infoStructType + "*") + "info"));
                        if (!header.returnType.equals("void")) {
-                               code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
-                                               GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
+                               code.add("\t"
+                                               + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
+                                                               GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
                        }
                        for (int i = 0; i < header.args.size(); i++) {
                                String type = header.args.get(i).type, var = header.args.get(i).name;
-                               code.add((DECLARE_DEFINE(type, var,
-                                               GET_FIELD_BY_PTR(infoStructName, var))));
+                               code.add("\t"
+                                               + (DECLARE_DEFINE(type, var,
+                                                               GET_FIELD_BY_PTR(infoStructName, var))));
                        }
                        code.add("");
                }
                // __COND_SAT
                if (!construct.condition.equals("")) {
-                       code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
+                       code.add("\t"
+                                       + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
                }
                // Check
                if (!construct.check.equals("")) {
-                       code.add(ASSIGN("check_passed", construct.check));
-                       code.add("if (!check_passed) return false;");
+                       code.add("\t" + ASSIGN("check_passed", construct.check));
+                       code.add("\tif (!check_passed)");
+                       code.add("\t\treturn false;");
+
                }
                // Action
                if (construct.action.size() > 0) {
-                       code.addAll(construct.action);
+                       addAllCodeWithIndent(code, construct.action, "\t");
                }
                // Post_check
                if (!construct.postCheck.equals("")) {
-                       code.add(ASSIGN("check_passed", construct.postCheck));
-                       code.add("if (!check_passed) return false;");
+                       code.add("\t" + ASSIGN("check_passed", construct.postCheck));
+                       code.add("\tif (!check_passed)");
+                       code.add("\t\treturn false;");
                }
                // Post_action
                if (construct.postAction.size() > 0) {
-                       code.addAll(construct.postAction);
+                       addAllCodeWithIndent(code, construct.postAction, "\t");
                }
                // Return true finally
-               code.add("return true;");
+               code.add("\treturn true;");
 
                code.add("}");
 
                return code;
        }
 
-       private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
+       private static void addAllCodeWithIndent(ArrayList<String> allCode,
+                       ArrayList<String> target, String indent) {
+               for (int i = 0; i < target.size(); i++) {
+                       allCode.add(indent + target.get(i));
+               }
+       }
+
+       public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
                HashSet<String> headers = new HashSet<String>();
                for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
                        File f = semantics.interfaceName2Construct.get(interfaceName).file;
                        headers.addAll(semantics.srcFilesInfo.get(f).headers);
                }
+               headers.add(HEADER_STDLIB);
+               headers.add(HEADER_STDINT);
+               headers.add(HEADER_MODELMEMORY);
+               headers.add(HEADER_MODELTYPES);
+               headers.add(HEADER_SPEC_LIB);
+               headers.add(HEADER_STDINT);
+               headers.add(HEADER_CDSANNOTATE);
+               // headers.add(HEADER_COMMON);
+               headers.add(HEADER_SPECANNOTATION);
                return headers;
        }
 
@@ -270,16 +313,16 @@ public class CodeVariables {
        private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
                        Construct construct) {
                ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
-               String headerLine = content.get(construct.beginLineNum), templateLine = null;
+               String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
                if (headerLine.startsWith("template")) {
                        templateLine = headerLine;
-                       headerLine = content.get(construct.beginLineNum + 1);
+                       headerLine = content.get(construct.beginLineNum + 2);
                }
                headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
                try {
-                       FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
+                       FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
                        if (templateLine != null) {
-                               ArrayList<VariableDeclaration> templateArgs = SpecParser
+                               ArrayList<VariableDeclaration> templateArgs = UtilParser
                                                .getTemplateArg(templateLine);
                                header.setTemplateList(templateArgs);
                        }
@@ -295,22 +338,6 @@ public class CodeVariables {
                ArrayList<String> newCode = new ArrayList<String>();
                HashSet<String> allHeaders = getAllHeaders(semantics);
 
-               // All headers needed by the interface decalration
-               newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
-               for (String header : allHeaders) {
-                       newCode.add(INCLUDE(header));
-               }
-               newCode.add("");
-               // Other necessary headers
-               newCode.add(INCLUDE(HEADER_STDLIB));
-               newCode.add(INCLUDE(HEADER_MODELMEMORY));
-               newCode.add(INCLUDE(HEADER_STDINT));
-               newCode.add(INCLUDE(HEADER_CDSANNOTATE));
-               newCode.add(INCLUDE(HEADER_COMMON));
-               newCode.add(INCLUDE(HEADER_SPEC_LIB));
-               newCode.add(INCLUDE(HEADER_SPECANNOTATION));
-               newCode.add("");
-
                SequentialDefineSubConstruct code = construct.code;
                // User-defined structs first
                newCode.add(COMMENT("All other user-defined structs"));
@@ -354,7 +381,7 @@ public class CodeVariables {
 
                        // Define ID function
                        newCode.add(COMMENT("ID function of interface: " + interfaceName));
-                       newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
+                       newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
                        newCode.add(COMMENT("End of ID function: " + interfaceName));
                        newCode.add("");
 
@@ -370,51 +397,53 @@ public class CodeVariables {
                String interfaceSize = Integer
                                .toString(semantics.interfaceName2Construct.size());
                newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
-               newCode.add(DECLARE("void**", "func_ptr_table"));
+               // Make it static
+               newCode.add("static " + DECLARE("void**", "func_ptr_table"));
                // Happens-before initialization rules
-               newCode.add(DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
+               // Should make it static
+               newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
 
                newCode.add("");
                newCode.add(COMMENT("Define function for sequential code initialization"));
                newCode.add("inline static void __sequential_init() {");
                // Init func_ptr_table
-               newCode.add(COMMENT("Init func_ptr_table"));
-               newCode.add(ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
+               newCode.add("\t" + COMMENT("Init func_ptr_table"));
+               newCode.add("\t" + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
                                + semantics.interface2Num.size() + " * 2)"));
                for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
                        String interfaceNum = Integer.toString(semantics.interface2Num
                                        .get(interfaceName));
-                       newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
+                       newCode.add("\t" + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
                                        "(void*) &" + interfaceName + "_id"));
-                       newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
+                       newCode.add("\t" + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
                                        "(void*) &" + interfaceName + "_check_action"));
                }
                // Init Happens-before rules table
                newCode.addAll(generateHBInitAnnotation(semantics));
 
                // Pass init info, including function table info & HB rules
-               newCode.add(COMMENT("Pass init info, including function table info & HB rules"));
+               newCode.add("\t" + COMMENT("Pass init info, including function table info & HB rules"));
                String structName = "anno_init", anno = "init";
-               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
-               newCode.add(ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
-               newCode.add(ASSIGN_TO_PTR(structName, "func_table_size",
+               newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
+               newCode.add("\t" + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
+               newCode.add("\t" + ASSIGN_TO_PTR(structName, "func_table_size",
                                "INTERFACE_SIZE"));
-               newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
-               newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table_size",
+               newCode.add("\t" + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
+               newCode.add("\t" + ASSIGN_TO_PTR(structName, "hb_init_table_size",
                                "HB_INIT_TABLE_SIZE"));
-               newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
-               newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(anno));
+               newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
+               newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
+               newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
+               newCode.add("\t" + ANNOTATE(semantics, anno));
 
                newCode.add("");
                // Init user-defined variables
-               newCode.addAll(construct.code.initVar);
+               addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
 
                newCode.add("}");
                newCode.add(COMMENT("End of Global construct generation in class"));
 
-               printCode(newCode);
+               //printCode(newCode);
                return newCode;
        }
 
@@ -434,7 +463,7 @@ public class CodeVariables {
                String templateDecl = semantics.getTemplateFullStr();
                if (templateList == null) {
                        newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
-                       newCode.add(DECLARE("void**", varPrefix + "hb_init_table"));
+                       newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
                        for (int i = 0; i < construct.code.declareVar.size(); i++) {
                                VariableDeclaration varDecl = construct.code.declareVar.get(i);
                                newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
@@ -443,7 +472,7 @@ public class CodeVariables {
                        newCode.add(templateDecl);
                        newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
                        newCode.add(templateDecl);
-                       newCode.add(DECLARE("void**", varPrefix + "hb_init_table"));
+                       newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
                        for (int i = 0; i < construct.code.declareVar.size(); i++) {
                                VariableDeclaration varDecl = construct.code.declareVar.get(i);
                                newCode.add(templateDecl);
@@ -473,17 +502,17 @@ public class CodeVariables {
                                                                .get(right.interfaceName)), hbLabelNumAfter = Integer
                                                .toString(semantics.hbLabel2Num
                                                                .get(right.hbConditionLabel));
-                               newCode.add(COMMENT(left + " -> " + right));
+                               newCode.add("\t" + COMMENT(left + " -> " + right));
 
-                               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
+                               newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
                                                structVarName));
-                               newCode.add(ASSIGN_TO_PTR(structVarName,
+                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName,
                                                "interface_num_before", interfaceNumBefore));
-                               newCode.add(ASSIGN_TO_PTR(structVarName,
+                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName,
                                                "hb_condition_num_before", hbLabelNumBefore));
-                               newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
+                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName, "interface_num_after",
                                                interfaceNumAfter));
-                               newCode.add(ASSIGN_TO_PTR(structVarName,
+                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName,
                                                "hb_condition_num_after", hbLabelNumAfter));
 
                                // newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
@@ -496,15 +525,15 @@ public class CodeVariables {
                        }
                }
                // Init hb_init_table
-               newCode.add(COMMENT("Init hb_init_table"));
-               newCode.add(ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
+               newCode.add("\t" + COMMENT("Init hb_init_table"));
+               newCode.add("\t" + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
                                + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
                                + hbConditionInitIdx + ")"));
                // Define HB_INIT_TABLE_SIZE
-               newCode.add(DEFINE("HB_INIT_TABLE_SIZE",
+               newCode.add("\t" + DEFINE("HB_INIT_TABLE_SIZE",
                                Integer.toString(hbConditionInitIdx)));
                for (int i = 0; i < hbConditionInitIdx; i++) {
-                       newCode.add(ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
+                       newCode.add("\t" + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
                                        + i));
                }
                return newCode;
@@ -517,17 +546,26 @@ public class CodeVariables {
        }
 
        // Only generate the declaration of the wrapper, don't do any renaming
+       // public static ArrayList<String> generateInterfaceWrapperDeclaration(
+       // SemanticsChecker semantics, InterfaceConstruct construct) {
+       // ArrayList<String> newCode = new ArrayList<String>();
+       // FunctionHeader header = getFunctionHeader(semantics, construct);
+       // newCode.add(COMMENT("Declaration of the wrapper"));
+       // String templateStr = header.getTemplateFullStr();
+       // newCode.add(templateStr);
+       // newCode.add(header.getFuncStr() + ";");
+       // newCode.add("");
+       //
+       // return newCode;
+       // }
+
        public static ArrayList<String> generateInterfaceWrapperDeclaration(
                        SemanticsChecker semantics, InterfaceConstruct construct) {
-               ArrayList<String> newCode = new ArrayList<String>();
                FunctionHeader header = getFunctionHeader(semantics, construct);
-               newCode.add(COMMENT("Declaration of the wrapper"));
-               String templateStr = header.getTemplateFullStr();
-               newCode.add(templateStr);
-               newCode.add(header.getFuncStr() + ";");
-               newCode.add("");
-
-               return newCode;
+               ArrayList<String> declaration = new ArrayList<String>();
+               declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
+                               .getDeclaration() + ";");
+               return declaration;
        }
 
        // Only generate the definition of the wrapper, don't do any renaming
@@ -535,12 +573,6 @@ public class CodeVariables {
                        SemanticsChecker semantics, InterfaceConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
                String interfaceName = construct.name;
-               // Generate necessary header file (might be redundant but never mind)
-               newCode.add(INCLUDE(HEADER_STDLIB));
-               newCode.add(INCLUDE(HEADER_MODELMEMORY));
-               newCode.add(INCLUDE(HEADER_CDSANNOTATE));
-               newCode.add(INCLUDE(HEADER_SPECANNOTATION));
-               newCode.add(INCLUDE(HEADER_SPEC_LIB));
 
                FunctionHeader header = getFunctionHeader(semantics, construct);
                String interfaceNum = Integer.toString(semantics.interface2Num
@@ -555,11 +587,12 @@ public class CodeVariables {
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
                                "interface_begin"));
                newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+
                String anno = "annotation_interface_begin";
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
                newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(anno));
+               newCode.add(ANNOTATE(semantics, anno));
                // Call original renamed function
                if (header.returnType.equals("void")) {
                        newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
@@ -582,7 +615,7 @@ public class CodeVariables {
                        newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                        newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
                        newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-                       newCode.add(ANNOTATE(anno));
+                       newCode.add(ANNOTATE(semantics, anno));
                        newCode.add("}");
                        newCode.add("");
                }
@@ -597,7 +630,7 @@ public class CodeVariables {
                        newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                        newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
                        newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-                       newCode.add(ANNOTATE(anno));
+                       newCode.add(ANNOTATE(semantics, anno));
                        newCode.add("");
                }
                // Interface end
@@ -626,7 +659,7 @@ public class CodeVariables {
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
                newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(anno));
+               newCode.add(ANNOTATE(semantics, anno));
                // Return __RET__ if it's not void
                if (!header.returnType.equals("void")) {
                        newCode.add("return " + MACRO_RETURN + ";");
@@ -687,10 +720,6 @@ public class CodeVariables {
                // Generate redundant header files
                newCode.add(COMMENT("Automatically generated code for potential commit point: "
                                + construct.label));
-               newCode.add(COMMENT("Include redundant headers"));
-               newCode.add(INCLUDE(HEADER_STDINT));
-               newCode.add(INCLUDE(HEADER_CDSANNOTATE));
-               newCode.add(INCLUDE(HEADER_SPECANNOTATION));
                newCode.add("");
                // Add annotation
                newCode.add("if (" + construct.condition + ") {");
@@ -704,7 +733,7 @@ public class CodeVariables {
                newCode.add(ASSIGN_TO_PTR(anno, "type",
                                SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
                newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(anno));
+               newCode.add(ANNOTATE(semantics, anno));
                newCode.add("}");
                return newCode;
        }
@@ -719,9 +748,6 @@ public class CodeVariables {
                // Generate redundant header files
                newCode.add(COMMENT("Automatically generated code for commit point define check: "
                                + construct.label));
-               newCode.add(COMMENT("Include redundant headers"));
-               newCode.add(INCLUDE(HEADER_STDINT));
-               newCode.add(INCLUDE(HEADER_CDSANNOTATE));
                newCode.add("");
                // Add annotation
                newCode.add("if (" + construct.condition + ") {");
@@ -733,7 +759,7 @@ public class CodeVariables {
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
                newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(anno));
+               newCode.add(ANNOTATE(semantics, anno));
                newCode.add("}");
                return newCode;
        }
@@ -751,11 +777,16 @@ public class CodeVariables {
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
+               String potentialLabelNum = Integer
+                               .toString(semantics.commitPointLabel2Num
+                                               .get(construct.potentialCPLabel));
                newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
+               newCode.add(ASSIGN_TO_PTR(structName, "potential_cp_label_num",
+                               potentialLabelNum));
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
                newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(anno));
+               newCode.add(ANNOTATE(semantics, anno));
                newCode.add("}");
                return newCode;
        }