edits
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
index d02fa4fc23ba7e796274818c1e9dfb81dabc4186..2914d49b6d7e1b41ac40beeae61acd11f96896f2 100644 (file)
@@ -12,6 +12,7 @@ import java.io.IOException;
 
 import edu.uci.eecs.specExtraction.Code;
 import edu.uci.eecs.specExtraction.CommutativityRule;
+import edu.uci.eecs.specExtraction.DefineConstruct;
 import edu.uci.eecs.specExtraction.EntryConstruct;
 import edu.uci.eecs.specExtraction.FunctionHeader;
 import edu.uci.eecs.specExtraction.GlobalConstruct;
@@ -147,11 +148,12 @@ public class CodeGeneratorUtils {
                                                '_') + "_H");
                code.addLine("");
 
+               // FIXME: We have included ad-hoc header files here
                // System included headers
                code.addLine(ShortComment("System included headers go here"));
-               for (String header : SpecNaming.includedHeadersList) {
-                       code.addLine(IncludeHeader(header));
-               }
+               code.addLine(IncludeHeader(SpecNaming.SPECANNOTATION_API));
+               code.addLine(IncludeHeader(SpecNaming.STDLIB));
+
                code.addLine("");
 
                // Users included headers
@@ -161,8 +163,10 @@ public class CodeGeneratorUtils {
                }
                code.addLine("");
 
-               code.addLine("using namespace std;");
-               code.addLine("");
+               // Decalre extern "C" --- begin
+               code.addLine("#ifdef __cplusplus");
+               code.addLine("extern \"C\" {");
+               code.addLine("#endif");
                code.addLine("");
 
                code.addLine(ShortComment("Declaration of some c-strings (CSTR)"));
@@ -241,17 +245,6 @@ public class CodeGeneratorUtils {
                        }
                }
 
-               // Declare customized StateStruct
-               code.addLine(ShortComment("Declare customized StateStruct"));
-               code.addLine("typedef struct " + SpecNaming.StateStruct + " {");
-               for (VariableDeclaration decl : globalConstruct.declState) {
-                       code.addLine(TabbedLine(Declare(decl)));
-               }
-               code.addLine("");
-               code.addLine(TabbedLine("SNAPSHOTALLOC"));
-               code.addLine("} " + SpecNaming.StateStruct + ";");
-               code.addLine("");
-
                // Declare customized value struct
                for (File file : interfaceListMap.keySet()) {
                        ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
@@ -362,6 +355,14 @@ public class CodeGeneratorUtils {
                code.addLine(ShortComment("Declare INIT annotation instrumentation function"));
                code.addLine("void _createInitAnnotation();");
                code.addLine("");
+
+               // Decalre extern "C" --- begin
+               code.addLine("#ifdef __cplusplus");
+               code.addLine("};");
+               code.addLine("#endif");
+               code.addLine("");
+
+               // Declare #endif
                code.addLine("#endif");
 
                return code;
@@ -394,6 +395,23 @@ public class CodeGeneratorUtils {
                code.addLine("");
 
                code.addLine("#include " + SpecNaming.CDSSpecGeneratedHeader);
+               code.addLine("#include " + SpecNaming.CDSANNOTATE);
+               code.addLine("#include " + SpecNaming.SPEC_COMMON);
+               code.addLine("#include " + SpecNaming.METHODCALL);
+               code.addLine("#include " + SpecNaming.CDSSPEC);
+               code.addLine("#include " + SpecNaming.SPECANNOTATION);
+               code.addLine("");
+               code.addLine("");
+
+               // Declare customized StateStruct
+               code.addLine(ShortComment("Declare customized StateStruct"));
+               code.addLine("typedef struct " + SpecNaming.StateStruct + " {");
+               for (VariableDeclaration decl : globalConstruct.declState) {
+                       code.addLine(TabbedLine(Declare(decl)));
+               }
+               code.addLine("");
+               code.addLine(TabbedLine("SNAPSHOTALLOC"));
+               code.addLine("} " + SpecNaming.StateStruct + ";");
                code.addLine("");
                code.addLine("");
 
@@ -776,10 +794,15 @@ public class CodeGeneratorUtils {
                                + SpecNaming.CopyState.toUpperCase() + ", " + "(void*) _"
                                + SpecNaming.CopyState.toLowerCase() + "),", 2));
                // new NamedFunction(_Print_str, PRINT_STATE, (void*) _print),
-               code.addLine(TabbedLine("new " + SpecNaming.NamedFunction + "("
+               line = "new " + SpecNaming.NamedFunction + "("
                                + SpecNaming.AppendStr(SpecNaming.PrintState) + ", "
-                               + SpecNaming.PrintStateType + ", " + "(void*) _"
-                               + SpecNaming.PrintState.toLowerCase() + "),", 2));
+                               + SpecNaming.PrintStateType + ", " + "(void*)";
+               if (globalConstruct.printState.isEmpty()) {
+                       line = line + SpecNaming.NullFunc + "),";
+               } else {
+                       line = line + "_" + SpecNaming.PrintState.toLowerCase() + "),";
+               }
+               code.addLine(TabbedLine(line, 2));
                // commuteRules, CommuteRuleSize);
                code.addLine(TabbedLine(SpecNaming.CommutativityRuleInst + ", "
                                + SpecNaming.CommutativityRuleSizeInst + ");", 2));
@@ -862,7 +885,7 @@ public class CodeGeneratorUtils {
                                                                + SpecNaming.PrintValue) + ", "
                                                + SpecNaming.PrintValueType + ", (void*) ";
                                if (construct.print.isEmpty()) {
-                                       line = line + SpecNaming.NullFunc + "),";
+                                       line = line + SpecNaming.NullFunc + ")";
                                } else {
                                        line = line + "_" + name + "_" + SpecNaming.PrintValue
                                                        + ")";
@@ -1046,6 +1069,25 @@ public class CodeGeneratorUtils {
                return res;
        }
 
+       /**
+        * <p>
+        * This function generates the code to be inserted right after the "@Define"
+        * construct (instrumentation code)
+        * </p>
+        * 
+        * @param construct
+        *            The corresponding entry construct
+        * @return
+        */
+       public static Code Generate4Define(DefineConstruct construct) {
+               Code code = new Code();
+               code.addLine("");
+               code.addLine("/**********    User-defined code in annotation (BEGIN)    **********/");
+               code.addLines(construct.code);
+               code.addLine("/**********    User-defined code in annotation (END)    **********/");
+               return code;
+       }
+
        /**
         * <p>
         * This function generates the new interface wrapper code to be inserted
@@ -1077,11 +1119,11 @@ public class CodeGeneratorUtils {
                } else {
                        code.addLine(beginLine);
                }
-               // Instrument with the INTERFACE_BEGIN annotation
+               // Instrument with the INTERFACE_BEGIN annotations
                code.addLine(prefixTabs
                                + "\t"
                                + ShortComment("Instrument with the INTERFACE_BEGIN annotation"));
-               // AnnoInterfaceInfo *info = _createInterfaceBeginAnnotation(_DEQ_str);
+               // CAnnoInterfaceInfo info = _createInterfaceBeginAnnotation(_DEQ_str);
                code.addLine(prefixTabs
                                + "\t"
                                + DeclareDefine(SpecNaming.AnnoInterfaceInfo, "*"
@@ -1101,7 +1143,7 @@ public class CodeGeneratorUtils {
                                + ShortComment("Initialize the value struct"));
                // The very first assignment "
                code.addLine(prefixTabs + "\t"
-                               + DeclareDefine(name, "*value", "new " + name));
+                               + DeclareDefine(name, "*value", SpecNaming.New + Brace(name)));
                // Don't leave out the RET field
                if (!construct.getFunctionHeader().isReturnVoid())
                        code.addLine(prefixTabs + "\t"
@@ -1113,13 +1155,13 @@ public class CodeGeneratorUtils {
                code.addLine("");
 
                // Store the value info into the current MethodCall
+               // _setInterfaceBeginAnnotationValue(info, value);
                code.addLine(prefixTabs
                                + "\t"
                                + ShortComment("Store the value info into the current MethodCall"));
-               code.addLine(prefixTabs
-                               + "\t"
-                               + AssignToPtr(SpecNaming.AnnoInterfaceInfoInst, "value",
-                                               "value"));
+               code.addLine(prefixTabs + "\t"
+                               + SpecNaming.SetInterfaceBeginAnnoValueFunc
+                               + Brace(SpecNaming.AnnoInterfaceInfoInst + ", value") + ";");
                code.addLine("");
 
                // Return if necessary