import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
+import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
import edu.uci.eecs.specCompiler.specExtraction.Construct;
*/
private void globalConstruct2Code(SpecConstruct inst) {
int lineNum = inst.endLineNum + 1;
- GlobalConstruct construct = (GlobalConstruct) inst.construct;
+ GlobalConstruct construct = (GlobalConstruct) inst.construct;
ArrayList<String> newCode = new ArrayList<String>();
-
+
// Generate the inner class definition
- newCode.add("class Sequential {\n");
+ newCode.add("class " + CodeVariables.SPEC_CLASS + " {\n");
newCode.add("public:\n");
-
+
// Generate the code in global construct first
SequentialDefineSubConstruct globalCode = construct.code;
breakCodeLines(newCode, globalCode.declareVar);
- breakCodeLines(newCode, globalCode.defineFunc);
-
+
// Generate code from the DefineVar, __COND_SAT__ and __ID__
-
-
+ // __COND_SAT__
+ newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
+ + "> " + CodeVariables.SPEC_CONDITION + ";");
+ // __ID__
+ newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
+ + "> " + CodeVariables.SPEC_ID + ";");
+
+ // DefineVars
+ for (String interfaceName : _semantics.interfaceName2Construct.keySet()) {
+ InterfaceConstruct iConstruct = (InterfaceConstruct) _semantics.interfaceName2Construct
+ .get(interfaceName).construct;
+ ArrayList<DefineVar> defineVars = iConstruct.action.defineVars;
+ for (int i = 0; i < defineVars.size(); i++) {
+ DefineVar var = defineVars.get(i);
+ newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + var.varType
+ + "> " + var.getNewVarName() + ";");
+ }
+ }
+
+ // Enum of all interface
+ String enumDefinition = "enum " + CodeVariables.SPEC_INTERFACE_ENUM
+ + " {";
+ Iterator<String> iter = _semantics.interfaceName2Construct.keySet()
+ .iterator();
+ String interfaceName;
+ if (iter.hasNext()) {
+ interfaceName = iter.next();
+ enumDefinition = enumDefinition + "_" + interfaceName + "_";
+ }
+ while (iter.hasNext()) {
+ interfaceName = iter.next();
+ enumDefinition = enumDefinition + ", _" + interfaceName + "_";
+ }
+ enumDefinition = enumDefinition + "};";
+ newCode.add(enumDefinition);
+
+ // __interface
+ newCode.add(CodeVariables.SPEC_HASHTABLE + "<enum "
+ + CodeVariables.SPEC_INTERFACE_ENUM + "> "
+ + CodeVariables.SPEC_INTERFACE + ";");
+
+ // Generate constructor (the place to initialize everything!)
+ newCode.add("\n");
+ newCode.add(CodeVariables.SPEC_CLASS + "() {");
+
+ breakCodeLines(newCode, globalCode.initVar);
+ // __COND_SAT__
+ newCode.add(CodeVariables.SPEC_CONDITION + " = "
+ + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
+ + ">();");
+ // __ID__
+ newCode.add(CodeVariables.SPEC_ID + " = "
+ + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
+ + ">();");
+ // __interface
+ newCode.add(CodeVariables.SPEC_INTERFACE + " = "
+ + CodeVariables.SPEC_HASHTABLE + "<enum "
+ + CodeVariables.SPEC_INTERFACE_ENUM + ">();");
+ // FIXME: Pass the happens-before relationship check here
+ newCode.add("}");
+
+ // Generate the sequential functions
+ breakCodeLines(newCode, globalCode.defineFunc);
+
// Generate the end of the inner class definition
newCode.add("};\n");
-// printCode(newCode);
-
+ printCode(newCode);
+
CodeAddition addition = new CodeAddition(lineNum, newCode);
if (!codeAdditions.containsKey(inst.file)) {
codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
}
codeAdditions.get(inst.file).add(addition);
}
-
+
+ // Break the code (String) into multiple lines and add it to newCode
private void breakCodeLines(ArrayList<String> newCode, String code) {
int begin = 0, end = 0;
while (end < code.length()) {
end++;
}
}
-
+
private void printCode(ArrayList<String> code) {
for (int i = 0; i < code.size(); i++) {
System.out.println(code.get(i));
}
}
- private void interface2Code(SpecConstruct inst) {
+ // Mainly rename and wrap the interface
+ private void interface2Code(SpecConstruct inst)
+ throws InterfaceWrongFormatException {
int lineNum = inst.endLineNum + 1;
- GlobalConstruct construct = (GlobalConstruct) inst.construct;
+ InterfaceConstruct construct = (InterfaceConstruct) inst.construct;
ArrayList<String> newCode = new ArrayList<String>();
+
+ // Rename the interface name
+ File file = inst.file;
+ ArrayList<String> content = contents.get(file);
+ String funcDecl = inst.interfaceDeclBody;
+ String funcName = renameInterface(funcDecl, content, lineNum);
+
+ // Generate new wrapper
+ breakCodeLines(newCode, funcDecl);
+ newCode.add("{");
+
+ // Generate
+ // FIXME: Add Happens-before check here
+ newCode.add("}");
CodeAddition addition = new CodeAddition(lineNum, newCode);
if (!codeAdditions.containsKey(inst.file)) {
codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
codeAdditions.get(inst.file).add(addition);
}
+ // Returns the function name that has been renamed and replace the old line
+ private String renameInterface(String funcDecl, ArrayList<String> content,
+ int lineNum) throws InterfaceWrongFormatException {
+ int begin = 0, end = funcDecl.indexOf('(');
+ if (end == -1) {
+ throw new InterfaceWrongFormatException(funcDecl
+ + "\n has wrong format!");
+ }
+ end--;
+ while (end > 0) {
+ char ch = funcDecl.charAt(end);
+ if (ch == '\n' || ch == '\t' || ch == ' ')
+ continue;
+ }
+ begin = end;
+ while (begin > 0) {
+ char ch = funcDecl.charAt(begin);
+ if (ch == '_' || (ch >= 'a' && ch <= 'z')
+ || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
+ continue;
+ }
+ }
+ String funcName = funcDecl.substring(begin + 1, end + 1), newLine;
+ int lineBreakIdx = funcDecl.indexOf('\n');
+ int firstLineBreak = lineBreakIdx == -1 ? funcDecl.length() : lineBreakIdx;
+ newLine = funcDecl.substring(0, begin + 1)
+ + CodeVariables.SPEC_INTERFACE_WRAPPER + funcName
+ + funcDecl.substring(end + 1, firstLineBreak);
+ content.set(lineNum, newLine);
+ return funcName;
+ }
+
private void potentialCP2Code(SpecConstruct inst) {
int lineNum = inst.endLineNum + 1;
- GlobalConstruct construct = (GlobalConstruct) inst.construct;
+ GlobalConstruct construct = (GlobalConstruct) inst.construct;
ArrayList<String> newCode = new ArrayList<String>();
-
-
+
CodeAddition addition = new CodeAddition(lineNum, newCode);
if (!codeAdditions.containsKey(inst.file)) {
codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
private void CPDefine2Code(SpecConstruct inst) {
int lineNum = inst.endLineNum + 1;
- GlobalConstruct construct = (GlobalConstruct) inst.construct;
+ GlobalConstruct construct = (GlobalConstruct) inst.construct;
ArrayList<String> newCode = new ArrayList<String>();
-
-
+
CodeAddition addition = new CodeAddition(lineNum, newCode);
if (!codeAdditions.containsKey(inst.file)) {
codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
private void CPDefineCheck2Code(SpecConstruct inst) {
int lineNum = inst.endLineNum + 1;
- GlobalConstruct construct = (GlobalConstruct) inst.construct;
+ GlobalConstruct construct = (GlobalConstruct) inst.construct;
ArrayList<String> newCode = new ArrayList<String>();
-
-
+
CodeAddition addition = new CodeAddition(lineNum, newCode);
if (!codeAdditions.containsKey(inst.file)) {
codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
if (construct instanceof GlobalConstruct) {
globalConstruct2Code(inst);
} else if (construct instanceof InterfaceConstruct) {
- interface2Code(inst);
+ try {
+ interface2Code(inst);
+ } catch (InterfaceWrongFormatException e) {
+ e.printStackTrace();
+ }
} else if (construct instanceof PotentialCPDefineConstruct) {
potentialCP2Code(inst);
} else if (construct instanceof CPDefineConstruct) {