fixed bugs
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index 4ee3e3dd09f4523b29d7d41a0b726bf147f9db6f..8e87cd5c4a1b1534c1904f742cd6d125283c75eb 100644 (file)
 package edu.uci.eecs.specCompiler.codeGenerator;
 
 import java.util.ArrayList;
+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.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.ActionSubConstruct.DefineVar;
+import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
+import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
 
 public class CodeVariables {
        // C++ code or library
-       public static final String HEADER_threads = "threads.h";
+       public static final String HEADER_THREADS = "<threads.h>";
+       public static final String HEADER_STDINT = "<stdint.h>";
        public static final String ThreadIDType = "thrd_t";
        public static final String GET_THREAD_ID = "thrd_current";
        public static final String BOOLEAN = "bool";
        public static final String UINT64 = "uint64_t";
 
        // Model checker code
-       public static final String HEADER_CDSAnnotate = "cdsannotate.h";
-       public static final String HEADER_SPECANNOTATION = "specannotation.h";
+       public static final String HEADER_CDSANNOTATE = "<cdsannotate.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 CDSAnnotateType = "SPEC_ANALYSIS";
-       // It's a SPEC_TAG type, it has current() and next() function
-       public static final String INTERFACE_CALL_SEQUENCE = "__interface_call_sequence";
+       public static final String IDType = "id_t";
 
-       public static final String SPEC_ANNOTATION_TYPE = "spec_anno_type";
+       public static final String SPEC_ANNO_TYPE = "spec_anno_type";
+       public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
+       public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
+       public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
+       public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
+       public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
+       public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
+       public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
        public static final String SPEC_ANNOTATION = "spec_annotation";
+       public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
+       public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
 
        public static final String ANNO_HB_INIT = "anno_hb_init";
-       public static final String ANNO_INTERFACE_BOUNDARY = "anno_interface_boundary";
-       public static final String ANNO_ID = "anno_id";
+       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_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";
-       public static final String ANNO_POST_CHECK = "anno_post_check";
 
        // Specification variables
-       public static final String SPEC_SEQUENTIAL_HEADER = "_spec_sequential.h";
-       public static final String SPEC_SEQUENTIAL_HEADER_MACRO = SPEC_SEQUENTIAL_HEADER
-                       .replace('.', '_').toUpperCase();
-       public static final String SPEC_SEQUENTIAL_STRUCT = "Sequential";
-       public static final String SPEC_SEQUENTIAL_INSTANCE = "__sequential";
-
-       public static final String SPEC_CONDITION = "condition";
-       public static final String SPEC_ID = "id";
-       public static final String SPEC_INTERFACE = "interface";
-       public static final String SPEC_INTERFACE_CALL_SEQUENCE = "interface_call_sequence";
-
        public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
+       public static final String DEFAULT_ID = "0";
 
        // Specification library
+       public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
        public static final String SPEC_QUEUE = "spec_queue";
        public static final String SPEC_STACK = "spec_stack";
+       public static final String SPEC_DEQUE = "spec_deque";
        public static final String SPEC_HASHTABLE = "spec_hashtable";
-       public static final String SPEC_PRIVATE_HASHTABLE_HEADER = "spec_private_hashtable.h";
        public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
        public static final String SPEC_TAG = "spec_tag";
+       public static final String SPEC_TAG_CURRENT = "current";
+       public static final String SPEC_TAG_NEXT = "next";
 
        // 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__";
 
-       // Break the code (String) into multiple lines and add it to newCode
-       private static void breakCodeLines(ArrayList<String> newCode, String code) {
-               int begin = 0, end = 0;
-               while (end < code.length()) {
-                       if (code.charAt(end) == '\n') {
-                               String line = code.substring(begin, end);
-                               newCode.add(line);
-                               begin = end + 1;
-                       }
-                       end++;
+       public static void printCode(ArrayList<String> code) {
+               for (int i = 0; i < code.size(); i++) {
+                       System.out.println(code.get(i));
                }
        }
 
-       private static void printCode(ArrayList<String> code) {
-               for (int i = 0; i < code.size(); i++) {
-                       System.out.println(code.get(i));
+       private static String COMMENT(String comment) {
+               return "/* " + comment + " */";
+       }
+
+       private static String INCLUDE(String header) {
+               return "#include " + header;
+       }
+
+       private static String DEFINE(String left, String right) {
+               return "#define " + left + " " + right;
+       }
+
+       private static String UNDEFINE(String macro) {
+               return "#undef " + macro;
+       }
+
+       private static String GET_FIELD_BY_PTR(String ptr, String field) {
+               return ptr + "->" + field;
+       }
+
+       private static String GET_FIELD(String var, String field) {
+               return var + "->" + field;
+       }
+
+       private static String BRACE(String val) {
+               return "(" + val + ")";
+       }
+
+       private static String ASSIGN(String structName, String field, String val) {
+               return structName + "." + field + " = " + val + ";";
+       }
+
+       private static String ASSIGN(String varName, String val) {
+               return varName + " = " + val + ";";
+       }
+
+       private static String ASSIGN_PTR(String structName, String field, String val) {
+               return structName + "." + field + " = &" + val + ";";
+       }
+
+       private static String ASSIGN_TO_PTR(String structName, String field,
+                       String val) {
+               return structName + "->" + field + " = " + val + ";";
+       }
+
+       private static String DECLARE(String type, String name) {
+               return type + " " + name + ";";
+       }
+
+       private static String DECLARE(VariableDeclaration varDecl) {
+               String type = varDecl.type, name = varDecl.name;
+               return type + " " + name + ";";
+       }
+
+       private static String DECLARE_DEFINE(String type, String var, String val) {
+               return type + " " + var + " = " + val + ";";
+       }
+
+       private static String ANNOTATE(String structName) {
+               return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");";
+       }
+
+       private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
+                       FunctionHeader funcDecl) {
+               ArrayList<String> code = new ArrayList<String>();
+               code.add("typedef struct " + interfaceName + "_info {");
+               code.add(DECLARE(funcDecl.returnType, MACRO_RETURN));
+               for (int i = 0; i < funcDecl.args.size(); i++) {
+                       code.add(DECLARE(funcDecl.args.get(i)));
+               }
+               code.add("} " + interfaceName + "_info {");
+               return code;
+       }
+
+       private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
+                       String idCode) {
+               ArrayList<String> code = new ArrayList<String>();
+               code.add("static " + IDType + " " + interfaceName + "_id() {");
+               if (!idCode.equals("")) {
+                       code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
+               } else {
+                       code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
+               }
+               code.add("return " + MACRO_ID + ";");
+               code.add("}");
+               return code;
+       }
+
+       private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
+                       InterfaceConstruct construct, FunctionHeader header) {
+               String interfaceName = construct.name;
+               ArrayList<String> code = new ArrayList<String>();
+               code.add("static bool " + interfaceName + "_check_action(void *info, "
+                               + IDType + " " + MACRO_ID + ") {");
+               code.add(DECLARE("bool", "check_passed"));
+               String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
+               code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
+                               BRACE(infoStructType) + "info"));
+               code.add((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("");
+               // __COND_SAT
+               if (!construct.condition.equals("")) {
+                       code.add(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;");
+               }
+               // Action
+               if (construct.action.size() > 0) {
+                       code.addAll(construct.action);
+               }
+               // Post_check
+               if (!construct.postCheck.equals("")) {
+                       code.add(ASSIGN("check_passed", construct.postCheck));
+                       code.add("if (!check_passed) return false;");
+               }
+               // Post_action
+               if (construct.postAction.size() > 0) {
+                       code.addAll(construct.postAction);
+               }
+
+               code.add("}");
+
+               return code;
+       }
+
+       private 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);
                }
+               return headers;
+       }
+
+       private static void makeFunctionStatic(ArrayList<String> funcDefine) {
+               String headLine = funcDefine.get(0);
+               headLine = "static " + headLine;
+               funcDefine.set(0, headLine);
        }
-       
+
+       private static String makeVariablesStatic(VariableDeclaration varDecl) {
+               String res = "static " + varDecl.type + " " + varDecl.name + ";";
+               return res;
+       }
+
+       private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
+                       Construct construct) {
+               ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
+               String headerLine = content.get(construct.beginLineNum);
+               if (headerLine.startsWith("template")) {
+                       headerLine = content.get(construct.beginLineNum + 1);
+               }
+               headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
+               try {
+                       return SpecParser.parseFuncHeader(headerLine);
+               } catch (ParseException e) {
+                       e.printStackTrace();
+               }
+               return null;
+       }
+
        public static ArrayList<String> generateGlobalVarDeclaration(
                        SemanticsChecker semantics, GlobalConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
+               HashSet<String> allHeaders = getAllHeaders(semantics);
 
-               // Header conflicting avoidance macro & headers
-               newCode.add("/** @file " + SPEC_SEQUENTIAL_HEADER);
-               newCode.add(" *  @brief Automatically generated header file for sequential variables");
-               newCode.add(" */");
-               newCode.add("#ifndef " + SPEC_SEQUENTIAL_HEADER_MACRO);
-               newCode.add("#define " + SPEC_SEQUENTIAL_HEADER_MACRO);
-               newCode.add("#include " + "<" + SPEC_PRIVATE_HASHTABLE_HEADER + ">");
-               newCode.add("#include " + "<" + HEADER_SPECANNOTATION + ">");
-
-               // Generate all sequential variables into a struct
-               newCode.add("/* Beginning of struct " + SPEC_SEQUENTIAL_STRUCT + " */");
-               newCode.add("typedef struct " + SPEC_SEQUENTIAL_STRUCT + " {");
-               newCode.add("/* Condition of interface */");
-               newCode.add(SPEC_PRIVATE_HASHTABLE + " " + SPEC_CONDITION + ";");
-               newCode.add("/* ID of interface */");
-               newCode.add(SPEC_PRIVATE_HASHTABLE + " " + SPEC_ID + ";");
-               newCode.add("/* Current interface call */");
-               newCode.add(SPEC_PRIVATE_HASHTABLE + " " + SPEC_INTERFACE + ";");
-               newCode.add("/* Current interface call sequence */");
-               newCode.add(SPEC_PRIVATE_HASHTABLE + " " + SPEC_INTERFACE_CALL_SEQUENCE
-                               + ";");
+               // 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("");
-               // DefineVar declaration
-               for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
-                       InterfaceConstruct iConstruct = (InterfaceConstruct) semantics.interfaceName2Construct
-                                       .get(interfaceName).construct;
-                       ArrayList<DefineVar> defineVars = iConstruct.action.defineVars;
-                       newCode.add("/* DefineVar in " + interfaceName + " */");
-                       for (int i = 0; i < defineVars.size(); i++) {
-                               DefineVar var = defineVars.get(i);
-                               newCode.add(SPEC_PRIVATE_HASHTABLE + " " + var.getNewVarName()
-                                               + ";");
-                       }
+               // Other necessary headers
+               newCode.add(INCLUDE(HEADER_STDINT));
+               newCode.add(INCLUDE(HEADER_CDSANNOTATE));
+               newCode.add(INCLUDE(HEADER_SPEC_LIB));
+               newCode.add("");
+
+               SequentialDefineSubConstruct code = construct.code;
+               // User-defined structs first
+               newCode.add(COMMENT("All other user-defined structs"));
+               ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
+               for (int i = 0; i < declareStructs.size(); i++) {
+                       ArrayList<String> declareStruct = declareStructs.get(i);
+                       newCode.addAll(declareStruct);
                        newCode.add("");
                }
-               // Generate user-defined variable declaration
-               newCode.add("/* Beginnint of other user-defined variables */");
-               SequentialDefineSubConstruct globalCode = construct.code;
-               breakCodeLines(newCode, globalCode.declareVar);
-               newCode.add("/* End of other user-defined variables */");
-               // End of struct Sequential
-               newCode.add("} " + SPEC_SEQUENTIAL_STRUCT + "; /* End of struct "
-                               + SPEC_SEQUENTIAL_STRUCT + " */");
-
-               // Generate 
-               
-               // Generate code from the DefineVar, __COND_SAT__ and __ID__
-               // The hashtable in the contract can only contains pointers or integers
-               // __COND_SAT__
-               newCode.add(CodeVariables.SPEC_HASHTABLE + CodeVariables.SPEC_CONDITION
-                               + ";");
-               // __ID__
-               newCode.add(CodeVariables.SPEC_HASHTABLE + CodeVariables.SPEC_ID + ";");
-               // DefineVars
-               for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
-                       InterfaceConstruct iConstruct = (InterfaceConstruct) semantics.interfaceName2Construct
-                                       .get(interfaceName).construct;
-                       ArrayList<DefineVar> defineVars = iConstruct.action.defineVars;
-                       for (int i = 0; i < defineVars.size(); i++) {
-                               DefineVar var = defineVars.get(i);
-                               newCode.add(CodeVariables.SPEC_HASHTABLE + var.getNewVarName()
-                                               + ";");
-                       }
+               // User-defined functions
+               newCode.add(COMMENT("All other user-defined functions"));
+               ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
+               for (int i = 0; i < defineFuncs.size(); i++) {
+                       ArrayList<String> defineFunc = defineFuncs.get(i);
+                       makeFunctionStatic(defineFunc);
+                       newCode.addAll(defineFunc);
+                       newCode.add("");
                }
-               // __interface
-               newCode.add(CodeVariables.SPEC_HASHTABLE + CodeVariables.SPEC_INTERFACE
-                               + ";");
-               // __interface_call_sequence
-               newCode.add(CodeVariables.SPEC_HASHTABLE
-                               + CodeVariables.SPEC_INTERFACE_CALL_SEQUENCE + ";");
-               // Interface call sequence varaiable
-               newCode.add(CodeVariables.SPEC_TAG + " "
-                               + CodeVariables.INTERFACE_CALL_SEQUENCE + ";");
-               // End of the struct
-               newCode.add("}");
 
-               // FIXME: Constructor should be modified and put in the right place
-               // Generate constructor (the place to initialize everything!)
-               breakCodeLines(newCode, globalCode.initVar);
-               // __COND_SAT__
-               newCode.add("init_table(&" + CodeVariables.SPEC_CONDITION + ");");
-               // __ID__
-               newCode.add("init_table(&" + CodeVariables.SPEC_ID + ");");
-               // DefineVars
                for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
-                       InterfaceConstruct iConstruct = (InterfaceConstruct) semantics.interfaceName2Construct
-                                       .get(interfaceName).construct;
-                       ArrayList<DefineVar> defineVars = iConstruct.action.defineVars;
-                       for (int i = 0; i < defineVars.size(); i++) {
-                               DefineVar var = defineVars.get(i);
-                               newCode.add("init_table(&" + var.getNewVarName() + ");");
-                       }
-               }
-               // __interface
-               newCode.add("init_table(&" + CodeVariables.SPEC_INTERFACE + ");");
-               // __interface_call_sequence
-               newCode.add("init_table(&" + CodeVariables.SPEC_INTERFACE_CALL_SEQUENCE
-                               + ");");
-
-               // Pass the happens-before relationship check here
-               newCode.addAll(CodeVariables.generateHBInitAnnotation(semantics));
+                       InterfaceConstruct iConstruct = semantics.interfaceName2Construct
+                                       .get(interfaceName);
+                       FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
+                       // Define necessary info structure
+                       newCode.add(COMMENT("Definition of interface info struct: "
+                                       + interfaceName));
+                       newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
+                       newCode.add(COMMENT("End of info struct definition: "
+                                       + interfaceName));
+                       newCode.add("");
 
-               newCode.add("\n");
+                       // Define ID function
+                       newCode.add(COMMENT("ID function of interface: " + interfaceName));
+                       newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
+                       newCode.add(COMMENT("End of ID function: + " + interfaceName));
+                       newCode.add("");
 
-               // Generate the sequential functions
-               breakCodeLines(newCode, globalCode.defineFunc);
+                       // Define check_action function
+                       newCode.add(COMMENT("Check action function of interface: "
+                                       + interfaceName));
+                       newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
+                       newCode.add(COMMENT("End of check action function: + "
+                                       + interfaceName));
+                       newCode.add("");
+               }
+               // Interface function pointer table
+               String interfaceSize = Integer
+                               .toString(semantics.interfaceName2Construct.size());
+               newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
+               newCode.add(DECLARE("void**", "func_ptr_table"));
+               // User-defined variables
+               ArrayList<VariableDeclaration> varDecls = code.declareVar;
+               for (int i = 0; i < varDecls.size(); i++) {
+                       VariableDeclaration varDecl = varDecls.get(i);
+                       // Don't forget to make them static
+                       newCode.add(makeVariablesStatic(varDecl));
+               }
 
-               // The end
-               newCode.add("#endif " + SPEC_SEQUENTIAL_HEADER_MACRO + "\t/* End of "
-                               + SPEC_SEQUENTIAL_HEADER + " */");
+               newCode.add("");
+               newCode.add(COMMENT("Define function for sequential code initialization"));
+               newCode.add("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*) * 2)"));
+               for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
+                       String interfaceNum = Integer.toString(semantics.interface2Num
+                                       .get(interfaceName));
+                       newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
+                                       "(void*) &" + interfaceName + "_id"));
+                       newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
+                                       "(void*) &" + interfaceName + "_check_action"));
+               }
+               newCode.add("");
+               // Init user-defined variables
+               newCode.addAll(construct.code.initVar);
+               // Pass Happens-before relationship
+               generateHBInitAnnotation(semantics);
+               newCode.add("}");
+               newCode.add(COMMENT("End of Global construct generation in class"));
 
                printCode(newCode);
                return newCode;
        }
 
-       public static ArrayList<String> generateHBInitAnnotation(
+       public static ArrayList<String> generateStaticVarDefine(
+                       SemanticsChecker semantics, GlobalConstruct construct) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               String className = semantics.getClassName();
+               if (className == null)
+                       return newCode; // No need to define any static variables
+               String templateList = semantics.getTemplateStr();
+               String varPrefix;
+               if (templateList == null) {
+                       varPrefix = className + "::";
+               } else {
+                       varPrefix = className + templateList + "::";
+               }
+               String templateDecl = semantics.getTemplateFullStr();
+               if (templateList == null) {
+                       newCode.add(DECLARE("void**", varPrefix + "func_ptr_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));
+                       }
+               } else {
+                       newCode.add(templateDecl);
+                       newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
+                       for (int i = 0; i < construct.code.declareVar.size(); i++) {
+                               VariableDeclaration varDecl = construct.code.declareVar.get(i);
+                               newCode.add(templateDecl);
+                               newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
+                       }
+               }
+               return newCode;
+       }
+
+       private static ArrayList<String> generateHBInitAnnotation(
                        SemanticsChecker semantics) {
                ArrayList<String> newCode = new ArrayList<String>();
                int hbConditionInitIdx = 0;
@@ -209,27 +397,227 @@ public class CodeVariables {
                        for (ConditionalInterface right : semantics.getHBConditions().get(
                                        left)) {
                                String structVarName = "hbConditionInit" + hbConditionInitIdx;
+                               String annotationVarName = "hb_init" + hbConditionInitIdx;
                                hbConditionInitIdx++;
-                               int interfaceNumBefore = semantics.interface2Num
-                                               .get(left.interfaceName), hbLabelNumBefore = semantics.hbLabel2Num
-                                               .get(left.hbConditionLabel), interfaceNumAfter = semantics.interface2Num
-                                               .get(right.interfaceName), hbLabelNumAfter = semantics.hbLabel2Num
-                                               .get(right.hbConditionLabel);
+                               String interfaceNumBefore = Integer
+                                               .toString(semantics.interface2Num
+                                                               .get(left.interfaceName)), hbLabelNumBefore = Integer
+                                               .toString(semantics.hbLabel2Num
+                                                               .get(left.hbConditionLabel)), interfaceNumAfter = Integer
+                                               .toString(semantics.interface2Num
+                                                               .get(right.interfaceName)), hbLabelNumAfter = Integer
+                                               .toString(semantics.hbLabel2Num
+                                                               .get(right.hbConditionLabel));
+                               newCode.add(COMMENT(left + " -> " + right));
+
                                newCode.add(ANNO_HB_INIT + " " + structVarName + ";");
+                               newCode.add(ASSIGN(structVarName, "interface_num_before",
+                                               interfaceNumBefore));
+                               newCode.add(ASSIGN(structVarName, "hb_condition_num_before",
+                                               hbLabelNumBefore));
+                               newCode.add(ASSIGN(structVarName, "interface_num_after",
+                                               interfaceNumAfter));
+                               newCode.add(ASSIGN(structVarName, "hb_condition_num_after",
+                                               hbLabelNumAfter));
 
-                               newCode.add(structVarName + "." + "interface_num_before"
-                                               + " = " + interfaceNumBefore + ";");
-                               newCode.add(structVarName + "." + "hb_condition_num_before"
-                                               + " = " + hbLabelNumBefore + ";");
-                               newCode.add(structVarName + "." + "interface_num_after" + " = "
-                                               + interfaceNumAfter + ";");
-                               newCode.add(structVarName + "." + "hb_condition_num_after"
-                                               + " = " + hbLabelNumAfter + ";");
-
-                               newCode.add(CDSAnnotate + "(" + CDSAnnotateType + ", &"
-                                               + structVarName + ");");
+                               newCode.add(DECLARE(SPEC_ANNOTATION, annotationVarName));
+                               newCode.add(ASSIGN(annotationVarName,
+                                               SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
+                               newCode.add(ASSIGN_PTR(annotationVarName,
+                                               SPEC_ANNOTATION_FIELD_ANNO, structVarName));
+                               newCode.add(ANNOTATE(annotationVarName));
                        }
                }
                return newCode;
        }
+
+       public static ArrayList<String> generateEntryPointInitCall() {
+               ArrayList<String> newCode = new ArrayList<String>(1);
+               newCode.add("__sequential_init();");
+               return newCode;
+       }
+
+       public static ArrayList<String> generateInterfaceWrapper(
+                       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_THREADS));
+               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
+                               .get(construct.name));
+               // Rename the interface
+               renameInterface(semantics, construct);
+               InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
+                               .get(interfaceName);
+               if (defineConstruct != null) {
+                       renameInterface(semantics, defineConstruct);
+               }
+
+               // Generate wrapper header
+               newCode.add(header.toString() + " {");
+               // Wrapper function body
+               newCode.add(COMMENT("Interface begins"));
+               // Interface begin
+               String structName = "interface_begin";
+               newCode.add(DECLARE(ANNO_INTERFACE_BEGIN, "interface_begin"));
+               newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
+               String anno = "annotation_interface_begin";
+               newCode.add(DECLARE(SPEC_ANNOTATION, anno));
+               newCode.add(ASSIGN(structName, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
+               newCode.add(ASSIGN_PTR(structName, "annotation", structName));
+               newCode.add(ANNOTATE(anno));
+               // Call original renamed function
+               newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
+                               header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
+               // HB conditions
+               for (String label : construct.hbConditions.keySet()) {
+                       String condition = construct.hbConditions.get(label);
+                       String hbCondNum = Integer.toString(semantics.interface2Num
+                                       .get(label));
+                       newCode.add("if " + BRACE(condition) + " {");
+                       structName = "hb_condition";
+                       newCode.add(DECLARE(ANNO_HB_CONDITION, structName));
+                       newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
+
+                       newCode.add(ASSIGN(structName, "hb_condition_num", hbCondNum));
+                       anno = "annotation_hb_condition";
+                       newCode.add(DECLARE(SPEC_ANNOTATION, anno));
+                       newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
+                       newCode.add(ASSIGN_PTR(anno, "annotation", structName));
+                       newCode.add(ANNOTATE(anno));
+               }
+               // Interface end
+               String infoStructType = interfaceName + "_info", infoName = "info";
+               newCode.add(DECLARE_DEFINE(infoStructType, infoName,
+                               BRACE(infoStructType + "*") + " malloc(sizeof("
+                                               + infoStructType + "))"));
+               newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
+               for (int i = 0; i < header.args.size(); i++) {
+                       String argName = header.args.get(i).name;
+                       newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
+               }
+               structName = "interface_end";
+               anno = "annoation_interface_end";
+               newCode.add(DECLARE(ANNO_INTERFACE_END, structName));
+               newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
+               newCode.add(ASSIGN(structName, "info", infoName));
+               newCode.add(DECLARE(SPEC_ANNOTATION, anno));
+               newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
+               newCode.add(ASSIGN_PTR(anno, "annotation", structName));
+               ANNOTATE(anno);
+               // End of the wrapper function
+               newCode.add("}");
+
+               // printCode(newCode);
+               return newCode;
+       }
+
+       public static void renameInterface(SemanticsChecker semantics,
+                       Construct construct) {
+               FunctionHeader header = getFunctionHeader(semantics, construct);
+               ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
+               int lineNum = construct.beginLineNum;
+               String headerLine = content.get(construct.beginLineNum);
+               if (headerLine.startsWith("template")) {
+                       headerLine = content.get(construct.beginLineNum + 1);
+                       lineNum++;
+               }
+               String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
+                               .toString() + " {";
+               content.set(lineNum, newLine);
+       }
+
+       public static void addAtomicReturn(SemanticsChecker semantics,
+                       Construct construct) {
+               int lineNum = construct.beginLineNum - 1;
+               ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
+               String oldLine = content.get(lineNum);
+               String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
+               content.set(lineNum, newLine);
+       }
+
+       public static ArrayList<String> generatePotentialCPDefine(
+                       SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               // Add atomic return variable if the predicate accesses to it
+               if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
+                       addAtomicReturn(semantics, construct);
+               }
+               // 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("");
+               // Add annotation
+               newCode.add("if (" + construct.condition + ") {");
+               String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
+               newCode.add(DECLARE(ANNO_POTENTIAL_CP_DEFINE, structName));
+               String labelNum = Integer.toString(semantics.commitPointLabel2Num
+                               .get(construct.label));
+               newCode.add(ASSIGN(structName, "label_num", labelNum));
+               newCode.add(DECLARE(SPEC_ANNOTATION, anno));
+               newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
+               newCode.add(ASSIGN_PTR(anno, "annotation", structName));
+               newCode.add(ANNOTATE(anno));
+               newCode.add("}");
+               return newCode;
+       }
+
+       public static ArrayList<String> generateCPDefineCheck(
+                       SemanticsChecker semantics, CPDefineCheckConstruct construct) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               // Add atomic return variable if the predicate accesses to it
+               if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
+                       addAtomicReturn(semantics, construct);
+               }
+               // 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 + ") {");
+               String structName = "cp_define_check", anno = "annotation_cp_define_check";
+               newCode.add(DECLARE(ANNO_CP_DEFINE_CHECK, structName));
+               String labelNum = Integer.toString(semantics.commitPointLabel2Num
+                               .get(construct.label));
+               newCode.add(ASSIGN(structName, "label_num", labelNum));
+               newCode.add(DECLARE(SPEC_ANNOTATION, anno));
+               newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
+               newCode.add(ASSIGN_PTR(anno, "annotation", structName));
+               newCode.add(ANNOTATE(anno));
+               newCode.add("}");
+               return newCode;
+       }
+
+       public static ArrayList<String> generateCPDefine(
+                       SemanticsChecker semantics, CPDefineConstruct construct) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               // Generate redundant header files
+               newCode.add(COMMENT("Automatically generated code for commit point define check: "
+                               + construct.label));
+               newCode.add("");
+               // Add annotation
+               newCode.add("if (" + construct.condition + ") {");
+               String structName = "cp_define", anno = "annotation_cp_define";
+               newCode.add(DECLARE(ANNO_CP_DEFINE, structName));
+               String labelNum = Integer.toString(semantics.commitPointLabel2Num
+                               .get(construct.label));
+               newCode.add(ASSIGN(structName, "label_num", labelNum));
+               newCode.add(DECLARE(SPEC_ANNOTATION, anno));
+               newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
+               newCode.add(ASSIGN_PTR(anno, "annotation", structName));
+               newCode.add(ANNOTATE(anno));
+               newCode.add("}");
+               return newCode;
+       }
 }