+ 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(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
+ newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_before",
+ interfaceNumBefore));
+ newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_before",
+ hbLabelNumBefore));
+ newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
+ interfaceNumAfter));
+ newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_after",
+ hbLabelNumAfter));
+
+ newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, annotationVarName));
+ newCode.add(ASSIGN_TO_PTR(annotationVarName,
+ SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
+ newCode.add(ASSIGN_TO_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_STDLIB));
+ 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
+ // If it's not in a class, we should declare the wrapper function
+ if (semantics.getClassName() == null) {
+ FunctionHeader renamedHeader = header
+ .getRenamedHeader(SPEC_INTERFACE_WRAPPER);
+ newCode.add(COMMENT("Declaration of the wrapper"));
+ newCode.add(renamedHeader + ";");
+ newCode.add("");
+ }
+ newCode.add(header.toString() + " {");
+ // Wrapper function body
+ newCode.add(COMMENT("Interface begins"));
+ // Interface begin
+ String structName = "interface_begin";
+ 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));
+ // Call original renamed function
+ if (header.returnType.equals("void")) {
+ newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
+ } else {
+ 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.hbLabel2Num
+ .get(label));
+ newCode.add("if " + BRACE(condition) + " {");
+ structName = "hb_condition";
+ newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
+ newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+
+ newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
+ anno = "annotation_hb_condition";
+ 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("}");
+ newCode.add("");
+ }
+ // Also add the true condition if any
+ if (semantics.containsConditionalInterface(new ConditionalInterface(interfaceName, ""))) {
+ structName = "hb_condition";
+ newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
+ newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+ newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
+ anno = "annotation_hb_condition";
+ 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("");
+ }
+ // Interface end
+ String infoStructType = null, infoName = null;
+ if (!header.returnType.equals("void") || header.args.size() > 0) {
+ infoStructType = interfaceName + "_info";
+ infoName = "info";
+ newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
+ BRACE(infoStructType + "*") + " malloc(sizeof("
+ + infoStructType + "))"));
+ if (!header.returnType.equals("void")) {
+ newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));