// Mainly rename and wrap the interface
private void interface2Code(InterfaceConstruct construct) {
- ArrayList<String> newCode = CodeVariables.generateInterfaceWrapper(
- _semantics, construct);
- int lineNum = construct.beginLineNum;
- // Add it to the codeAdditions
- CodeAddition addition = new CodeAddition(lineNum, newCode);
- if (!codeAdditions.containsKey(construct.file)) {
- codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+ // First rename the interface
+ CodeVariables.renameInterface(_semantics, construct);
+
+ // If there's no define construct for it, we generate the wrapper just
+ // in place without declaration
+ InterfaceDefineConstruct defineConstruct = _semantics.interfaceName2DefineConstruct
+ .get(construct.name);
+ ArrayList<String> newCode;
+ int lineNum;
+ CodeAddition addition;
+ // Then generate the wrapper if necessary
+ if (defineConstruct != null) { // Need to have a wrapper declaration
+ newCode = CodeVariables.generateInterfaceWrapperDeclaration(_semantics, construct);
+ lineNum = construct.beginLineNum;
+ // Add the wrapper declaration
+ addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+ }
+ codeAdditions.get(construct.file).add(addition);
+
+ // Add the wrapper definition
+ newCode = CodeVariables.generateInterfaceWrapperDefinition(_semantics, construct);
+ lineNum = defineConstruct.beginLineNum;
+ // Add the wrapper declaration
+ addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(defineConstruct.file)) {
+ codeAdditions.put(defineConstruct.file, new ArrayList<CodeAddition>());
+ }
+ codeAdditions.get(defineConstruct.file).add(addition);
+ } else { // No declaration needed
+ // Last generate the definition
+ newCode = CodeVariables.generateInterfaceWrapperDefinition(_semantics, construct);
+ lineNum = construct.beginLineNum;
+ // Add the wrapper declaration
+ addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+ }
+ codeAdditions.get(construct.file).add(addition);
}
- codeAdditions.get(construct.file).add(addition);
+
}
private void potentialCPDefine2Code(PotentialCPDefineConstruct construct) {
int curSrcLine = 0;
for (int i = 0; i < additions.size(); i++) {
CodeAddition addition = additions.get(i);
- if (curSrcLine < addition.lineNum) {
+ if (curSrcLine < addition.lineNum) {
// Be careful, subList is the interval [begin, end)
- newContent.addAll(content.subList(curSrcLine, addition.lineNum));
+ newContent
+ .addAll(content.subList(curSrcLine, addition.lineNum));
curSrcLine = addition.lineNum;
}
newContent.addAll(addition.newCode);
public static void main(String[] argvs) {
String homeDir = Environment.HOME_DIRECTORY;
File[] srcFiles = {
-// new File(Environment.MODEL_CHECKER_TEST_DIR + "/backup_linuxrwlocks.c") };
-// new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c") };
+ // new File(Environment.MODEL_CHECKER_TEST_DIR +
+ // "/backup_linuxrwlocks.c") };
+ // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c") };
new File(homeDir
+ "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"), };
-// new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
-// new File(homeDir + "/benchmark/ms-queue/my_queue.c") };
-// new File(homeDir + "/benchmark/test/test.c") };
+ // new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
+ // new File(homeDir + "/benchmark/ms-queue/my_queue.c") };
+ // new File(homeDir + "/benchmark/test/test.c") };
CodeGenerator gen = new CodeGenerator(srcFiles);
gen.generateCode();
}
public static final String HEADER_COMMON = "<common.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 CDSAnnotate = "cdsannotate";
public static final String CDSAnnotate = "_Z11cdsannotatemPv";
public static final String CDSAnnotateType = "SPEC_ANALYSIS";
public static final String IDType = "call_id_t";
String val) {
return structName + "->" + field + " = " + val + ";";
}
-
- private static String ASSIGN_PTR_TO_PTR(String structName, String field, String val) {
+
+ private static String ASSIGN_PTR_TO_PTR(String structName, String field,
+ String val) {
return structName + "->" + field + " = &" + val + ";";
}
private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
- return "struct " + type + " *" + name + " = (struct " + type + "*) malloc(sizeof(struct " + type + "));";
+ return "struct " + type + " *" + name + " = (struct " + type
+ + "*) malloc(sizeof(struct " + type + "));";
}
-
+
private static String DECLARE(String type, String name) {
return type + " " + name + ";";
}
private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
Construct construct) {
ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
- String headerLine = content.get(construct.beginLineNum);
+ String headerLine = content.get(construct.beginLineNum), templateLine = null;
if (headerLine.startsWith("template")) {
+ templateLine = headerLine;
headerLine = content.get(construct.beginLineNum + 1);
}
headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
try {
- return SpecParser.parseFuncHeader(headerLine);
+ FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
+ if (templateLine != null) {
+ ArrayList<VariableDeclaration> templateArgs = SpecParser
+ .getTemplateArg(templateLine);
+ header.setTemplateList(templateArgs);
+ }
+ return header;
} catch (ParseException e) {
e.printStackTrace();
}
newCode.add("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*) * " + semantics.interface2Num.size() + " * 2)"));
+ newCode.add(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));
.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(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(ASSIGN_TO_PTR(structVarName,
+ "hb_condition_num_after", hbLabelNumAfter));
- newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, annotationVarName));
+ 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,
return newCode;
}
- public static ArrayList<String> generateInterfaceWrapper(
+ // Only generate the declaration of the wrapper, don't do any renaming
+ public static ArrayList<String> generateInterfaceWrapperDeclaration(
+ SemanticsChecker semantics, InterfaceConstruct construct) {
+ ArrayList<String> newCode = new ArrayList<String>();
+ FunctionHeader header = getFunctionHeader(semantics, construct);
+ newCode.add(COMMENT("Declaration of the wrapper"));
+ String templateStr = header.getTemplateFullStr();
+ newCode.add(templateStr);
+ newCode.add(header.getFuncStr() + ";");
+ newCode.add("");
+
+ return newCode;
+ }
+
+ // Only generate the definition of the wrapper, don't do any renaming
+ public static ArrayList<String> generateInterfaceWrapperDefinition(
SemanticsChecker semantics, InterfaceConstruct construct) {
ArrayList<String> newCode = new ArrayList<String>();
String interfaceName = construct.name;
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() + " {");
+
+ newCode.add(header.getTemplateFullStr());
+ newCode.add(header.getFuncStr() + " {");
// 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(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("");
}
// Also add the true condition if any
- if (semantics.containsConditionalInterface(new ConditionalInterface(interfaceName, ""))) {
+ 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));
return newCode;
}
- // Rename the interface depending on if it's declaration or definition
+ // Rename the interface name for declaration or definition
public static void renameInterface(SemanticsChecker semantics,
Construct construct) {
FunctionHeader header = getFunctionHeader(semantics, construct);
lineNum++;
}
String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
- .toString() + " {";
+ .toString();
+
+ if (construct instanceof InterfaceConstruct) {
+ InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
+ InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
+ .get(iConstruct.name);
+ if (defineConstruct != null) { // There is a defineConstruct
+ newLine = newLine + " ;";
+ renameInterface(semantics, defineConstruct);
+ } else { // This is a declare & define construct
+ newLine = newLine + " {";
+ }
+ } else {
+ newLine = newLine + " {";
+ }
content.set(lineNum, newLine);
}
// Add annotation
newCode.add("if (" + construct.condition + ") {");
String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
- newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE, structName));
+ newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
+ structName));
String labelNum = Integer.toString(semantics.commitPointLabel2Num
.get(construct.label));
newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
- newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
+ newCode.add(ASSIGN_TO_PTR(anno, "type",
+ SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
newCode.add(ANNOTATE(anno));
newCode.add("}");
import java.util.ArrayList;
+import com.sun.xml.internal.ws.wsdl.parser.ParserUtil;
+
public class FunctionHeader {
- public final ArrayList<VariableDeclaration> templateList;
+ private ArrayList<VariableDeclaration> templateList;
public final String returnType;
public final QualifiedName qualifiedName;
public final ArrayList<VariableDeclaration> args;
- public FunctionHeader(ArrayList<VariableDeclaration> templateList, String returnType, QualifiedName qualifiedName,
+ public FunctionHeader(String returnType, QualifiedName qualifiedName,
ArrayList<VariableDeclaration> args) {
- this.templateList = templateList;
+ this.templateList = null;
this.returnType = returnType;
this.qualifiedName = qualifiedName;
this.args = args;
}
+
+ public void setTemplateList(ArrayList<VariableDeclaration> templateList) {
+ this.templateList = templateList;
+ }
+
+ public ArrayList<VariableDeclaration> getTemplateList() {
+ return this.templateList;
+ }
+
+ public String getTemplateFullStr() {
+ String templateStr = "";
+ if (templateList.size() == 0)
+ return templateStr;
+ VariableDeclaration decl;
+ decl = templateList.get(0);
+ templateStr = "<" + decl.type + " " + decl.name;
+ for (int i = 1; i < templateList.size(); i++) {
+ decl = templateList.get(i);
+ templateStr = templateStr + ", " + decl.type + " " + decl.name;
+ }
+ templateStr = templateStr + ">";
+ return templateStr;
+ }
+
+ public String getTemplateArgStr() {
+ String templateStr = null;
+ if (templateList.size() == 0)
+ return templateStr;
+ templateStr = "<" + templateList.get(0).name;
+ for (int i = 1; i < templateList.size(); i++) {
+ templateStr = templateStr + ", " + templateList.get(i);
+ }
+ templateStr = templateStr + ">";
+ return templateStr;
+ }
+
+ public String getFuncStr() {
+ String res = returnType + " " + qualifiedName.fullName + "(";
+ if (args.size() >= 1) {
+ res = res + args.get(0);
+ }
+ for (int i = 1; i < args.size(); i++) {
+ res = res + ", " + args.get(i);
+ }
+ res = res + ")";
+ return res;
+ }
public String toString() {
String res = returnType + " " + qualifiedName.fullName + "(";
public FunctionHeader getRenamedHeader(String prefix) {
String newFullName = qualifiedName.qualifiedName + prefix + "_"
+ qualifiedName.bareName;
- FunctionHeader newHeader = new FunctionHeader(templateList, returnType,
+ FunctionHeader newHeader = new FunctionHeader(returnType,
new QualifiedName(newFullName), args);
return newHeader;
}