more fix
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index db7e7e6..39ef342 100644 (file)
@@ -187,26 +187,28 @@ public class CodeVariables {
                // 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(DECLARE_DEFINE("\t" + infoStructType + "*",
+                                       infoStructName, BRACE(infoStructType + "*") + "info"));
                        if (!header.returnType.equals("void")) {
-                               code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
+                               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((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("");
                }
 
                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;
        }
@@ -218,53 +220,67 @@ public class CodeVariables {
                code.add("inline static bool " + interfaceName
                                + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
                                + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
-               code.add(DECLARE("bool", "check_passed"));
+               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 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()) {
@@ -391,43 +407,43 @@ public class CodeVariables {
                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(semantics, 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;
        }
 
@@ -486,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,
@@ -509,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;
@@ -761,10 +777,12 @@ 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));
+               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(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));