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.ConditionalInterface;
+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.ParserUtils;
import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
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_CDSTRACE = "cdstrace.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";
+ public static final String IDType = "id_t";
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 ANNO_POST_CHECK = "anno_post_check";
// Specification variables
- public static final String HEADER_SPEC_SEQUENTIAL = "_spec_sequential.h";
- public static final String SPEC_SEQUENTIAL_HEADER_MACRO = HEADER_SPEC_SEQUENTIAL
- .replace('.', '_').toUpperCase();
-
public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
- public static final String VAR_CALL_SEQUENCE_NUM = "call_sequence_num";
+ 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 HEADER_SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable.h";
public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
- public static final String HEADER_SPEC_TAG = "spec_tag.h";
public static final String SPEC_TAG = "spec_tag";
public static final String SPEC_TAG_CURRENT = "current";
public static final String SPEC_TAG_NEXT = "next";
}
private static String INCLUDE(String header) {
- return "#include <" + header + ">";
+ return "#include " + header;
}
private static String DEFINE(String left, String right) {
return structName + "." + field + " = &" + val + ";";
}
- private static String DECLARE(String structType, String structName) {
- return structType + " " + structName + ";";
+ 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 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 != null) {
+ 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 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 void makeVariablesStatic(ArrayList<String> varDecls) {
+ for (int i = 0; i < varDecls.size(); i++) {
+ String varDecl = varDecls.get(i);
+ varDecl = "static " + varDecl;
+ varDecls.set(i, varDecl);
+ }
+ }
+
+ private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
+ InterfaceConstruct construct) {
+ ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
+ String headerLine = content.get(construct.beginLineNum);
+ 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 " + HEADER_SPEC_SEQUENTIAL);
- 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);
+ // 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("");
- newCode.add(INCLUDE(HEADER_SPEC_PRIVATE_HASHTABLE));
- newCode.add(INCLUDE(HEADER_SPECANNOTATION));
- newCode.add(INCLUDE(HEADER_SPEC_TAG));
+ // 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 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("");
+ }
+
+ for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
+ 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("");
+
+ // Define ID function
+ newCode.add(COMMENT("ID functions of interface: " + interfaceName));
+ newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
+ newCode.add("");
+ newCode.add(COMMENT("End of ID function: + " + interfaceName));
+ newCode.add("");
+
+ // Define check_action function
+
+
+ }
+
+ // User-defined variables
+ ArrayList<VariableDeclaration> varDecls = code.declareVar;
+ for (int i = 0; i < varDecls.size(); i++) {
+ VariableDeclaration varDecl = varDecls.get(i);
+ newCode.add(DECLARE(varDecl.type, varDecl.name));
+ }
// printCode(newCode);
return newCode;
ArrayList<String> newCode = new ArrayList<String>();
// Generate necessary header file (might be redundant but never mind)
-
// Generate wrapper header
-
// Wrapper function body
-
+
// Interface begin
-
+
// Call original renamed function
-
+
// HB conditions
-
-
+
// Interface end
-
// End of the wrapper function
-
-// printCode(newCode);
+ // printCode(newCode);
return newCode;
}
newCode.add(COMMENT("Include redundant headers"));
newCode.add(INCLUDE(HEADER_THREADS));
newCode.add(INCLUDE(HEADER_CDSTRACE));
- newCode.add(INCLUDE(HEADER_SPEC_PRIVATE_HASHTABLE));
- newCode.add(INCLUDE(HEADER_SPEC_SEQUENTIAL));
newCode.add("");
// Some necessary function calls
-
return newCode;
}