From 114e1583393a6a214e41bf85adcf25344fc270cc Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Wed, 17 Feb 2016 17:59:01 -0800 Subject: [PATCH] edits --- grammer/util.jj | 17 ++ .../uci/eecs/codeGenerator/CodeGenerator.java | 20 ++- .../codeGenerator/CodeGeneratorUtils.java | 13 +- .../eecs/specExtraction/SpecExtractor.java | 155 ++++++++++++------ .../specExtraction/VariableDeclaration.java | 21 +-- 5 files changed, 150 insertions(+), 76 deletions(-) diff --git a/grammer/util.jj b/grammer/util.jj index 7e8b0c3..901e2e6 100644 --- a/grammer/util.jj +++ b/grammer/util.jj @@ -49,6 +49,12 @@ public class UtilParser { return parser.FuncDecl(); } + public static VariableDeclaration parseDeclaration(String line) + throws ParseException { + InputStream input = new ByteArrayInputStream(line.getBytes()); + UtilParser parser = new UtilParser(input); + return parser.Declaration(); + } public static String stringArray2String(ArrayList content) { StringBuilder sb = new StringBuilder(); @@ -426,6 +432,17 @@ ArrayList FormalParamList() : } } +VariableDeclaration Declaration() : +{ + String type, param; +} +{ + (type = Type()) (param = .image) + { + return new VariableDeclaration(type, param); + } +} + VariableDeclaration TypeParam() : { String type, param; diff --git a/src/edu/uci/eecs/codeGenerator/CodeGenerator.java b/src/edu/uci/eecs/codeGenerator/CodeGenerator.java index 38a4981..3d6b224 100644 --- a/src/edu/uci/eecs/codeGenerator/CodeGenerator.java +++ b/src/edu/uci/eecs/codeGenerator/CodeGenerator.java @@ -59,10 +59,8 @@ public class CodeGenerator { public CodeGenerator(String dirName) { this.dirName = dirName; - originalDir = Environment.BenchmarksDir + dirName - + "/"; - generatedDir = Environment.GeneratedFilesDir + dirName - + "/"; + originalDir = Environment.BenchmarksDir + dirName + "/"; + generatedDir = Environment.GeneratedFilesDir + dirName + "/"; try { files = this.getSrcFiles(originalDir); } catch (FileNotFoundException e) { @@ -252,7 +250,7 @@ public class CodeGenerator { .GenerateCDSSpecHeaderFile(extractor); Code generatedCPP = CodeGeneratorUtils .GenerateCDSSpecCPPFile(extractor); - + System.out.println("/***** Generated header file *****/"); System.out.println(generatedHeader); System.out.println("/***** Generated cpp file *****/"); @@ -308,8 +306,14 @@ public class CodeGenerator { } static public void main(String[] args) { - String dirName = Environment.MS_QUEUE; - CodeGenerator generator = new CodeGenerator(dirName); - generator.generateCode(); + String[] dirNames = { Environment.REGISTER }; + + for (int i = 0; i < dirNames.length; i++) { + String dirName = dirNames[i]; + System.out.println("/********** Processing " + dirName + + " **********/"); + CodeGenerator generator = new CodeGenerator(dirName); + generator.generateCode(); + } } } diff --git a/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java b/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java index d02fa4f..abcb45c 100644 --- a/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java +++ b/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java @@ -776,10 +776,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 +867,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 + ")"; diff --git a/src/edu/uci/eecs/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specExtraction/SpecExtractor.java index e1d0f26..0e470e0 100644 --- a/src/edu/uci/eecs/specExtraction/SpecExtractor.java +++ b/src/edu/uci/eecs/specExtraction/SpecExtractor.java @@ -50,7 +50,8 @@ public class SpecExtractor { } private void addInterfaceConstruct(InterfaceConstruct construct) { - ArrayList list = interfaceListMap.get(construct.file); + ArrayList list = interfaceListMap + .get(construct.file); if (list == null) { list = new ArrayList(); interfaceListMap.put(construct.file, list); @@ -67,13 +68,14 @@ public class SpecExtractor { list.add(construct); } - private void addEntryConstruct(File file, EntryConstruct construct) throws WrongAnnotationException { + private void addEntryConstruct(File file, EntryConstruct construct) + throws WrongAnnotationException { EntryConstruct old = entryMap.get(file); if (old == null) entryMap.put(file, construct); else { // Error processing - String errMsg = "Multiple @Entry annotations in the same file.\n\t Other @Entry at Line " + old.beginLineNum - + "."; + String errMsg = "Multiple @Entry annotations in the same file.\n\t Other @Entry at Line " + + old.beginLineNum + "."; WrongAnnotationException.err(file, construct.beginLineNum, errMsg); } } @@ -90,7 +92,8 @@ public class SpecExtractor { *

*/ public void printAnnotations() { - System.out.println("/********** Print out of specification extraction **********/"); + System.out + .println("/********** Print out of specification extraction **********/"); System.out.println("// Extracted header files"); for (String header : headerFiles) System.out.println(header); @@ -104,7 +107,8 @@ public class SpecExtractor { System.out.println("// Interface in file: " + file.getName()); for (InterfaceConstruct construct : list) { System.out.println(construct); - System.out.println("EndLineNumFunc: " + construct.getEndLineNumFunction()); + System.out.println("EndLineNumFunc: " + + construct.getEndLineNumFunction()); } } @@ -145,11 +149,16 @@ public class SpecExtractor { ArrayList list = interfaceListMap.get(f); if (list != null) { for (InterfaceConstruct construct : list) { - InterfaceConstruct existingConstruct = interfaceMap.get(construct.getName()); + InterfaceConstruct existingConstruct = interfaceMap + .get(construct.getName()); if (existingConstruct != null) { // Error - errMsg = "Interface labels duplication with: \"" + construct.getName() + "\" in File \"" - + existingConstruct.file.getName() + "\", Line " + existingConstruct.beginLineNum + "."; - WrongAnnotationException.err(construct.file, construct.beginLineNum, errMsg); + errMsg = "Interface labels duplication with: \"" + + construct.getName() + "\" in File \"" + + existingConstruct.file.getName() + + "\", Line " + existingConstruct.beginLineNum + + "."; + WrongAnnotationException.err(construct.file, + construct.beginLineNum, errMsg); } else { interfaceMap.put(construct.getName(), construct); } @@ -161,7 +170,8 @@ public class SpecExtractor { for (File file : OPListMap.keySet()) { ArrayList list = OPListMap.get(file); for (OPConstruct construct : list) { - if (construct.type == OPType.OPCheck || construct.type == OPType.PotentialOP) { + if (construct.type == OPType.OPCheck + || construct.type == OPType.PotentialOP) { String label = construct.label; OPLabelSet.add(label); } @@ -185,7 +195,8 @@ public class SpecExtractor { */ public boolean extractHeaders(String line) { // "^( |\t)*#include( |\t)+("|<)([a-zA-Z_0-9\-\.])+("|>)" - Pattern regexp = Pattern.compile("^( |\\t)*(#include)( |\\t)+(\"|<)([a-zA-Z_0-9\\-\\.]+)(\"|>)"); + Pattern regexp = Pattern + .compile("^( |\\t)*(#include)( |\\t)+(\"|<)([a-zA-Z_0-9\\-\\.]+)(\"|>)"); Matcher matcher = regexp.matcher(line); // process the line. @@ -234,8 +245,9 @@ public class SpecExtractor { * @return Returns the annotation string list of the current construct * @throws WrongAnnotationException */ - private ArrayList extractTillConstructEnd(File file, LineNumberReader lineReader, String curLine, - int beginLineNum) throws WrongAnnotationException { + private ArrayList extractTillConstructEnd(File file, + LineNumberReader lineReader, String curLine, int beginLineNum) + throws WrongAnnotationException { ArrayList annotations = new ArrayList(); annotations.add(curLine); // System.out.println(curLine); @@ -258,8 +270,10 @@ public class SpecExtractor { if (matcher.find()) return annotations; } - WrongAnnotationException.err(file, beginLineNum, - "The interface annotation should have the matching closing symbol closing \"*/\""); + WrongAnnotationException + .err(file, + beginLineNum, + "The interface annotation should have the matching closing symbol closing \"*/\""); } catch (IOException e) { e.printStackTrace(); } @@ -286,16 +300,20 @@ public class SpecExtractor { * annotation * @throws WrongAnnotationException */ - private void extractGlobalConstruct(File file, LineNumberReader lineReader, String curLine, int beginLineNum) - throws WrongAnnotationException { - ArrayList annotations = extractTillConstructEnd(file, lineReader, curLine, beginLineNum); - GlobalConstruct construct = new GlobalConstruct(file, beginLineNum, annotations); + private void extractGlobalConstruct(File file, LineNumberReader lineReader, + String curLine, int beginLineNum) throws WrongAnnotationException { + ArrayList annotations = extractTillConstructEnd(file, + lineReader, curLine, beginLineNum); + GlobalConstruct construct = new GlobalConstruct(file, beginLineNum, + annotations); if (globalConstruct != null) { // Check if we have seen a global state // construct earlier File otherDefinitionFile = globalConstruct.file; int otherDefinitionLine = globalConstruct.beginLineNum; - String errMsg = "Multiple definition of global state.\n" + "\tAnother definition is in File \"" - + otherDefinitionFile.getName() + "\" (Line " + otherDefinitionLine + ")."; + String errMsg = "Multiple definition of global state.\n" + + "\tAnother definition is in File \"" + + otherDefinitionFile.getName() + "\" (Line " + + otherDefinitionLine + ")."; WrongAnnotationException.err(file, beginLineNum, errMsg); } globalConstruct = construct; @@ -307,19 +325,22 @@ public class SpecExtractor { * @param lineReader * Call this function when the lineReader will read the beginning * of the definition right away + * @param startingLine + * The line that we should start processing * @return The line number of the ending line of the interfae definition. If * returning -1, it means the curl symbols in the interface do not * match * @throws WrongAnnotationException */ - private int findEndLineNumFunction(File file, LineNumberReader lineReader) throws WrongAnnotationException { - String line; + private int findEndLineNumFunction(File file, LineNumberReader lineReader, + String startingLine) throws WrongAnnotationException { + String line = startingLine; // FIXME: We assume that in the string of the code, there does not exist // the symbol '{' & '{' try { boolean foundFirstCurl = false; int unmatchedCnt = 0; - while ((line = lineReader.readLine()) != null) { + do { // process the line. // System.out.println(line); @@ -340,7 +361,7 @@ public class SpecExtractor { return endLineNumFunction; } } - } + } while ((line = lineReader.readLine()) != null); } catch (IOException e) { e.printStackTrace(); } @@ -371,11 +392,14 @@ public class SpecExtractor { * @throws IOException * @throws ParseException */ - private void extractInterfaceConstruct(File file, LineNumberReader lineReader, String curLine, int beginLineNum) + private void extractInterfaceConstruct(File file, + LineNumberReader lineReader, String curLine, int beginLineNum) throws WrongAnnotationException, IOException, ParseException { - ArrayList annotations = extractTillConstructEnd(file, lineReader, curLine, beginLineNum); + ArrayList annotations = extractTillConstructEnd(file, + lineReader, curLine, beginLineNum); int endLineNum = lineReader.getLineNumber(); - InterfaceConstruct construct = new InterfaceConstruct(file, beginLineNum, endLineNum, annotations); + InterfaceConstruct construct = new InterfaceConstruct(file, + beginLineNum, endLineNum, annotations); addInterfaceConstruct(construct); // Process the corresponding interface function declaration header @@ -387,23 +411,30 @@ public class SpecExtractor { lineNum = lineReader.getLineNumber(); construct.processFunctionDeclaration(line); } catch (IOException e) { - errMsg = "Spec error in file \"" + file.getName() + "\", Line " + lineNum + errMsg = "Spec error in file \"" + + file.getName() + + "\", Line " + + lineNum + " :\n\tThe function declaration should take only one line and have the correct syntax (follow the annotations immediately)\n"; System.out.println(errMsg); throw e; } catch (ParseException e) { - errMsg = "Spec error in file \"" + file.getName() + "\", Line " + lineNum + errMsg = "Spec error in file \"" + + file.getName() + + "\", Line " + + lineNum + " :\n\tThe function declaration should take only one line and have the correct syntax (follow the annotations immediately)\n"; System.out.println(errMsg); throw e; } // Now we find the end of the interface definition - int endLineNumFunction = findEndLineNumFunction(file, lineReader); + int endLineNumFunction = findEndLineNumFunction(file, lineReader, line); construct.setEndLineNumFunction(endLineNumFunction); if (endLineNumFunction == -1) { - WrongAnnotationException.err(file, beginLineNum, - "The interface definition does NOT have matching curls '}'"); + WrongAnnotationException + .err(file, beginLineNum, + "The interface definition does NOT have matching curls '}'"); } } @@ -425,22 +456,26 @@ public class SpecExtractor { * The type of ordering point construct we are processing * @throws WrongAnnotationException */ - private void extractOPConstruct(File file, int beginLineNum, String curLine, OPType type) - throws WrongAnnotationException { + private void extractOPConstruct(File file, int beginLineNum, + String curLine, OPType type) throws WrongAnnotationException { String condition = null; String label = null; // "(\(\s?(\w+)\s?\))?\s:\s?(.+)\*/\s?$" - Pattern regexp = Pattern.compile("(\\(\\s*(\\w+)\\s*\\))?\\s*:\\s*(.+)\\*/\\s*$"); + Pattern regexp = Pattern + .compile("(\\(\\s*(\\w+)\\s*\\))?\\s*:\\s*(.+)\\*/\\s*$"); Matcher matcher = regexp.matcher(curLine); if (matcher.find()) { label = matcher.group(2); condition = matcher.group(3); } else { - WrongAnnotationException.err(file, beginLineNum, - "Wrong syntax for the ordering point construct. You might need a colon before the condition."); + WrongAnnotationException + .err(file, + beginLineNum, + "Wrong syntax for the ordering point construct. You might need a colon before the condition."); } - OPConstruct op = new OPConstruct(file, beginLineNum, type, label, condition, curLine); + OPConstruct op = new OPConstruct(file, beginLineNum, type, label, + condition, curLine); addOPConstruct(op); } @@ -459,7 +494,8 @@ public class SpecExtractor { * Current line being processed * @throws WrongAnnotationException */ - public void extractEntryConstruct(File file, int beginLineNum, String curLine) throws WrongAnnotationException { + public void extractEntryConstruct(File file, int beginLineNum, + String curLine) throws WrongAnnotationException { addEntryConstruct(file, new EntryConstruct(file, beginLineNum, curLine)); } @@ -479,7 +515,8 @@ public class SpecExtractor { * beginning line of the annotation construct. * @throws WrongAnnotationException */ - private void extractOneLineConstruct(File file, int beginLineNum, String curLine) throws WrongAnnotationException { + private void extractOneLineConstruct(File file, int beginLineNum, + String curLine) throws WrongAnnotationException { // "/\*\*\s*@(Entry|OPDefine|PotentialOP|OPCheck|OPClear|OPClearDefine)" Pattern regexpBegin = Pattern.compile("/\\*\\*\\s*@(\\w+)"); Matcher matcher = regexpBegin.matcher(curLine); @@ -488,9 +525,11 @@ public class SpecExtractor { String name = matcher.group(1); if (name.equals("Entry")) extractEntryConstruct(file, beginLineNum, curLine); - else if (name.equals("OPDefine") || name.equals("PotentialOP") || name.equals("OPCheck") - || name.equals("OPClear") || name.equals("OPClearDefine")) - extractOPConstruct(file, beginLineNum, curLine, OPType.valueOf(name)); + else if (name.equals("OPDefine") || name.equals("PotentialOP") + || name.equals("OPCheck") || name.equals("OPClear") + || name.equals("OPClearDefine")) + extractOPConstruct(file, beginLineNum, curLine, + OPType.valueOf(name)); } } @@ -512,7 +551,8 @@ public class SpecExtractor { * @throws WrongAnnotationException * @throws ParseException */ - public void extractConstruct(File file) throws WrongAnnotationException, ParseException { + public void extractConstruct(File file) throws WrongAnnotationException, + ParseException { BufferedReader br = null; LineNumberReader lineReader = null; try { @@ -520,7 +560,8 @@ public class SpecExtractor { br = new BufferedReader(new FileReader(file)); lineReader = new LineNumberReader(br); // "/\*\*\s*@(DeclareState|Interface)" - Pattern regexpBegin = Pattern.compile("/\\*\\*\\s*@(DeclareState|Interface)"); + Pattern regexpBegin = Pattern + .compile("/\\*\\*\\s*@(DeclareState|Interface)"); Matcher matcher = regexpBegin.matcher(""); String line; @@ -546,12 +587,15 @@ public class SpecExtractor { // Process each annotation accordingly if (constructName.equals(SpecNaming.DeclareState)) { - extractGlobalConstruct(file, lineReader, line, beginLineNum); + extractGlobalConstruct(file, lineReader, line, + beginLineNum); } else if (constructName.equals(SpecNaming.Interface)) { - extractInterfaceConstruct(file, lineReader, line, beginLineNum); + extractInterfaceConstruct(file, lineReader, line, + beginLineNum); } else { WrongAnnotationException.err(file, beginLineNum, - constructName + " is not a supported annotation."); + constructName + + " is not a supported annotation."); } } @@ -582,15 +626,17 @@ public class SpecExtractor { * @throws WrongAnnotationException * @throws ParseException */ - public void extract(File[] files) throws WrongAnnotationException, ParseException { + public void extract(File[] files) throws WrongAnnotationException, + ParseException { for (int i = 0; i < files.length; i++) extract(files[i]); // Check basic specification semantics checkSemantics(); } - - public void extract(ArrayList files) throws WrongAnnotationException, ParseException { + + public void extract(ArrayList files) throws WrongAnnotationException, + ParseException { for (int i = 0; i < files.size(); i++) extract(files.get(i)); @@ -611,7 +657,8 @@ public class SpecExtractor { * @throws WrongAnnotationException * @throws ParseException */ - public void extract(File file) throws WrongAnnotationException, ParseException { + public void extract(File file) throws WrongAnnotationException, + ParseException { extractConstruct(file); } } diff --git a/src/edu/uci/eecs/specExtraction/VariableDeclaration.java b/src/edu/uci/eecs/specExtraction/VariableDeclaration.java index 541a433..259be57 100644 --- a/src/edu/uci/eecs/specExtraction/VariableDeclaration.java +++ b/src/edu/uci/eecs/specExtraction/VariableDeclaration.java @@ -4,6 +4,9 @@ import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; +import edu.uci.eecs.utilParser.ParseException; +import edu.uci.eecs.utilParser.UtilParser; + /** *

* This class represents a variable declaration in C/C++, in which there exist a @@ -24,17 +27,15 @@ public class VariableDeclaration { public VariableDeclaration(File file, int lineNum, String line) throws WrongAnnotationException { - // "([<>\*\w\s]+)\s?(\w+)\s;" - Pattern regexp = Pattern.compile("([<>&\\*\\w\\s]+)\\s?(\\w+)\\s?;"); - Matcher matcher = regexp.matcher(line); - if (matcher.find()) { - type = matcher.group(1); - name = matcher.group(2); - } else { - type = null; - name = null; + VariableDeclaration decl = null; + try { + decl = UtilParser.parseDeclaration(line); + } catch (ParseException e) { WrongAnnotationException.err(file, lineNum, "The declaration: \"" - + line + "\" has wrong syntax."); + + line + "\" has wrong syntax."); + } finally { + type = decl == null ? null : decl.type; + name = decl == null ? null : decl.name; } } -- 2.34.1