edits
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
index 005397e..2914d49 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,29 +148,25 @@ 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
-               // FIXME: We don't add user-defined headers, but as a workaround we only add forward class.
                code.addLine(ShortComment("User included headers go here"));
                for (String header : headerFiles) {
                        code.addLine(IncludeHeader(header));
                }
                code.addLine("");
-               
-//             code.addLine(ShortComment("Forward declaration goes here"));
-//             for (String type : extractor.forwardClass) {
-//                     code.addLine("class " + type + ";");
-//             }
-//             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)"));
@@ -248,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);
@@ -369,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;
@@ -401,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("");
 
@@ -1058,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
@@ -1089,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, "*"
@@ -1113,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"
@@ -1125,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