From: Peizhao Ou <peizhaoo@uci.edu> Date: Fri, 25 Oct 2013 01:07:26 +0000 (-0700) Subject: small change X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5fe4d211752fa05df28c1e2b2e17b69492d3a40b;p=cdsspec-compiler.git small change --- diff --git a/grammer/spec.txt b/grammer/spec.txt index 8b8beb3..5fba648 100644 --- a/grammer/spec.txt +++ b/grammer/spec.txt @@ -1,14 +1,2 @@ -int main() { - struct pair<int> *p; - p -> x = 2 - + 3 - 3; - /** - @Begin - @Potential_commit_point_define: - __ATOMIC_RET__ == true - @Label: - Enqueue_Success_Point - @End - */ - return 0; -} +Class<int>* //A::B<sfd, _sdf>::id(const char * ch_ptr, int a) +//template < TypeK k, TypeV v> diff --git a/grammer/spec_compiler.jj b/grammer/spec_compiler.jj index 57956dc..440007a 100644 --- a/grammer/spec_compiler.jj +++ b/grammer/spec_compiler.jj @@ -109,21 +109,34 @@ import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar; import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct; import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct; +import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct; +import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader; public class SpecParser { private static ArrayList<String> _content; private static File _file; private static ArrayList<Construct> _constructs; + public static void main(String[] argvs) throws ParseException, TokenMgrError { try { + String line = "int* A::B<sfd, _sdf>::id(const char * ch_ptr, int a)"; + System.out.println(parseFuncHeader(line)); + File f = new File("./grammer/spec.txt"); FileInputStream fis = new FileInputStream(f); SpecParser parser = new SpecParser(fis); + /** ArrayList<String> content = new ArrayList<String>(); ArrayList<Construct> constructs = new ArrayList<Construct>(); parser.Parse(f, content, constructs); + for (int i = 0; i < content.size(); i++) { + System.out.println(content.get(i)); + } + */ + parser.Test(); System.out.println("Parsing finished!"); } catch (FileNotFoundException e) { e.printStackTrace(); @@ -141,6 +154,21 @@ import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct; } } + public static ArrayList<String> getTemplateArg(String line) + throws ParseException { + InputStream input = new ByteArrayInputStream(line.getBytes()); + SpecParser parser = new SpecParser(input); + return parser.TemplateParamList(); + } + + public static FunctionHeader parseFuncHeader(String line) + throws ParseException { + InputStream input = new ByteArrayInputStream(line.getBytes()); + SpecParser parser = new SpecParser(input); + return parser.FuncDecl(); + } + + public static String stringArray2String(ArrayList<String> content) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < content.size(); i++) { @@ -223,6 +251,10 @@ SKIP : { <COMMIT_POINT_SET: "@Commit_point_set:"> | <ENTRY_POINT: "@Entry_point"> +| + <CLASS_BEGIN: "@Class_begin"> +| + <CLASS_END: "@Class_end"> | <INTERFACE_DEFINE: "@Interface_define:"> | @@ -264,7 +296,9 @@ SKIP : { | <STRUCT: "struct"> | - <TYPENAME: "typename"> + <CLASS: "class"> +| + <TEMPLATE: "template"> | <#DIGIT: ["0"-"9"]> | @@ -421,13 +455,22 @@ String Type() : ("const" { type = "const"; } )? - (("struct" { type = type + " struct"; })? - (str = <IDENTIFIER>.image { + (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })? + ( + str = QualifiedName() { + if (!type.equals("")) + type = type + " " + str; + else + type = str; + }) + ( + str = ParameterizedName() { if (!type.equals("")) type = type + " " + str; else type = str; - })) + }) + ) ((str = "const".image { if (!type.equals("")) type = type + " " + str; @@ -452,6 +495,82 @@ String Type() : } } +void Test() : +{} +{ + Type() + //FuncDecl() +} + +String ParameterizedName() : +{ + String res = ""; + String str; +} +{ + (str = <IDENTIFIER>.image {res = str;}) + ("<" str = <IDENTIFIER>.image { res = res + "<" + str; } + ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">" + { res = res + ">"; } + )? + { + return res; + } +} + +FunctionHeader FuncDecl() : +{ + String ret, qualifiedName, bareName; + ArrayList<String> args; +} +{ + ret = Type() + qualifiedName = QualifiedName() + bareName = <IDENTIFIER>.image + args = FormalParamList() + { + FunctionHeader res = new FunctionHeader(ret, qualifiedName, bareName, args); + //System.out.println(res); + return res; + } +} + +String QualifiedName() : +{ + String qualifiedName, str; +} +{ + { qualifiedName = ""; } + (LOOKAHEAD(2) (str = ParameterizedName() { qualifiedName = qualifiedName + + str + "::"; } <DOUBLECOLON> ))* + { + return qualifiedName; + } +} + +ArrayList<String> TemplateParamList() : +{ + ArrayList<String> params; + String str; +} +{ + { + params = new ArrayList<String>(); + } + <TEMPLATE> + "<" + (str = <IDENTIFIER>.image + str = <IDENTIFIER>.image {params.add(str);}) + + ("," str = <IDENTIFIER>.image + str = <IDENTIFIER>.image {params.add(str);})* + ">" + { + //System.out.println(params); + return params; + } +} + ArrayList<String> FormalParamList() : { ArrayList<String> typeParams; @@ -460,9 +579,10 @@ ArrayList<String> FormalParamList() : { typeParams = new ArrayList<String>(); } + "(" (TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)? + ")" { - System.out.println(typeParams); return typeParams; } } @@ -484,6 +604,7 @@ ArrayList<String> C_CPP_CODE() : String text; Token t; boolean newLine = false; + boolean inTemplate = false; ArrayList<String> content; } { @@ -495,7 +616,8 @@ ArrayList<String> C_CPP_CODE() : ( LOOKAHEAD(2) ( - t = <CONST> | t = <STRUCT> | t = <TYPENAME> | + t = <CONST> | t = <STRUCT> | t = <CLASS> | + (t = <TEMPLATE> { inTemplate = true; })| t = <IDENTIFIER> | t = <POUND> | (t = <OPEN_BRACE> { newLine = true; } ) | (t = <CLOSE_BRACE> { newLine = true; } ) | @@ -504,7 +626,9 @@ ArrayList<String> C_CPP_CODE() : | t = <HB_SYMBOL> | t = <COMMA> | t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> | t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> | - t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> | + t = <LESS_THAN> | + (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) | + t = <GREATER_EQUALS> | t = <LESS_EQUALS> | t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> | t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> | (t = <SEMI_COLON> { newLine = true; } ) @@ -557,6 +681,8 @@ Construct ParseSpec() : LOOKAHEAD(2) res = Commit_point_define() | LOOKAHEAD(2) res = Commit_point_define_check() | LOOKAHEAD(2) res = Entry_point() | + LOOKAHEAD(2) res = Class_begin() | + LOOKAHEAD(2) res = Class_end() | LOOKAHEAD(2) res = Interface_define() ) { @@ -843,6 +969,30 @@ EntryPointConstruct Entry_point() : } } +ClassBeginConstruct Class_begin() : +{} +{ + + <BEGIN> + <CLASS_BEGIN> + <END> + { + return new ClassBeginConstruct(_file, _content.size()); + } +} + +ClassEndConstruct Class_end() : +{} +{ + + <BEGIN> + <CLASS_END> + <END> + { + return new ClassEndConstruct(_file, _content.size()); + } +} + InterfaceDefineConstruct Interface_define() : { String name; diff --git a/notes/generated_code_examples.txt b/notes/generated_code_examples.txt index 506c5af..1b5f486 100644 --- a/notes/generated_code_examples.txt +++ b/notes/generated_code_examples.txt @@ -1,15 +1,6 @@ ****** Example1: ****** Global Variable Declaration -/* Include the header files first -** This declaration will be written into a header file -*/ -/* @file _spec_sequential_generated.h */ -/* @brief automatically generated file */ -#ifndef __SPEC_SEQUENTIAL_GENERATED_H -#define __SPEC_SEQUENTIAL_GENERATED_H -#include <specannotation.h> -#include <spec_tag.h> /* Include all the header files that contains the interface declaration */ #include <iostream> @@ -17,6 +8,10 @@ Global Variable Declaration #include <memory> #include <assert.h> +/* Other necessary header files */ +#include <specannotation.h> +#include <spec_tag.h> + /* All other user-defined functions */ ALL_USER_DEFINED_FUNCTIONS @@ -33,15 +28,34 @@ typedef struct Get_info { } Get_info; /* End of info struct definition */ +/* ID functions of interface */ +static id_t Put_id() { + id_t id = PUT_ID; + return id; +} -/* All function of action and check of interfaces */ -bool Put_check_action(void *info) { +static id_t Get_id() { + id_t id = GET_ID; + return id; +} +/* End of ID functions */ + +/* Initialization of interface<->function_ptr table */ +#define INTERFACE_SIZE 2 +void* func_ptr_table[INTERFACE_SIZE * 2] = { + CLASS + +/* Check_action function of interfaces */ +bool Put_check_action(void *info, id_t __ID__) { bool check_passed; Put_info *theInfo = (Put_info) info; shared_ptr<TypeV> __RET__ = theInfo->__RET__; TypeK & key = theInfo->key; TypeV & value = theInfo->value; + // __COND_SAT__ + bool __COND_SAT__ = PUT_CONDITION; + // Check check_passed = PUT_CHECK_EXPRESSION; if (!check_passed) @@ -62,10 +76,11 @@ bool Put_check_action(void *info) { PUT_POST_ACTION } -id_t Put_id() { - id_t id = PUT_ID; - return id; + +bool Get_check_action(void *info, id_t __ID__) { + //... } +/* End of check_action function definitions */ /* Beginning of other user-defined variables */ @@ -93,8 +108,7 @@ void __sequential_init() { cdsannotate(SPEC_ANALYSIS, &hb_init0); } -#endif - +#endif /* End of ****** Example2: ****** diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java index 399c036..40f0642 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java @@ -15,6 +15,7 @@ import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface; import edu.uci.eecs.specCompiler.specExtraction.Construct; 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.PotentialCPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct; @@ -43,16 +44,16 @@ public class CodeGenerator { public CodeGenerator(File[] srcFiles) { this.srcFiles = srcFiles; - this.contents = new HashMap<File, ArrayList<String>>(); + _extractor = new SpecExtractor(); + _extractor.extract(srcFiles); + + this.contents = _extractor.contents; + this.globalContent = null; - readSrcFiles(); this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>(); - _extractor = new SpecExtractor(); - - _extractor.extract(srcFiles); - _semantics = new SemanticsChecker(_extractor.getConstructs()); + _semantics = new SemanticsChecker(_extractor.constructs); try { _semantics.check(); System.out.println(_semantics); @@ -71,19 +72,6 @@ public class CodeGenerator { return content; } - private void readSrcFiles() { - for (int i = 0; i < srcFiles.length; i++) { - File f = srcFiles[i]; - if (!contents.containsKey(f)) { - try { - contents.put(f, readSrcFile(f)); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - /** * <p> * Generate all the global code, including the "@DefineVar" in each diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java index 3945136..3638194 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface; 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.PotentialCPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct; diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ClassBeginConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/ClassBeginConstruct.java new file mode 100644 index 0000000..4565015 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/ClassBeginConstruct.java @@ -0,0 +1,13 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.io.File; + +public class ClassBeginConstruct extends Construct { + public ClassBeginConstruct(File file, int beginLineNum) { + super(file, beginLineNum); + } + + public String toString() { + return "@Class_begin"; + } +} diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ClassEndConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/ClassEndConstruct.java new file mode 100644 index 0000000..f163d4d --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/ClassEndConstruct.java @@ -0,0 +1,13 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.io.File; + +public class ClassEndConstruct extends Construct { + public ClassEndConstruct(File file, int beginLineNum) { + super(file, beginLineNum); + } + + public String toString() { + return "@Class_end"; + } +} \ No newline at end of file diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/FunctionHeader.java b/src/edu/uci/eecs/specCompiler/specExtraction/FunctionHeader.java new file mode 100644 index 0000000..e6d1fa2 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/FunctionHeader.java @@ -0,0 +1,23 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.util.ArrayList; + +public class FunctionHeader { + public final String returnType; + public final String qualifiedName; + public final String bareFuncName; + public final ArrayList<String> args; + + public FunctionHeader(String returnType, String qualifiedName, + String bareFuncName, ArrayList<String> args) { + this.returnType = returnType; + this.qualifiedName = qualifiedName; + this.bareFuncName = bareFuncName; + this.args = args; + } + + public String toString() { + return "Ret: " + returnType + "\n" + qualifiedName + "\t" + + bareFuncName + "\n" + args; + } +} diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/IDExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/IDExtractor.java new file mode 100644 index 0000000..81bbdfa --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/IDExtractor.java @@ -0,0 +1,120 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +public class IDExtractor { + private String code; + private int index = 0; + + private int idBeginIdx, idEndIdx; + + public IDExtractor(String code) { + this.code = code; + this.index = 0; + } + + public IDExtractor(String code, int beginIdx) { + this.code = code; + this.index = beginIdx; + } + + public void reset(int beginIdx) { + this.index = beginIdx; + } + + public void reset() { + this.index = 0; + } + + public int getIDBeginIdx() { + return this.idBeginIdx; + } + + public int getIDEndIdx() { + return this.idEndIdx; + } + + public int lineNumOfID() { + int cnt = 0; + for (int i = 0; i < idBeginIdx; i++) { + if (code.charAt(i) == '\n') + cnt++; + } + return cnt; + } + + public int lineBeginIdxOfID() { + int i; + for (i = idBeginIdx - 1; i >= 0; i--) { + if (code.charAt(i) == '\n') + break; + } + return i + 1; + } + + public int lineEndIdxOfID() { + int i = 0; + for (i = idEndIdx + 1; i < code.length(); i++) { + if (code.charAt(i) == '\n') { + break; + } + } + return i - 1; + } + + public String getPrevID() { + int beginIdx = index; + int endIdx = index; + char ch; + while (true) { + ch = code.charAt(endIdx); + if ((ch == '_' || (ch >= 'a' && ch <= 'z') || ch >= 'A' + && ch <= 'Z')) { + break; + } + endIdx--; + } + beginIdx = endIdx; + while (true) { + ch = code.charAt(beginIdx); + if (!((ch == '_' || (ch >= 'a' && ch <= 'z') || ch >= 'A' + && ch <= 'Z'))) { + break; + } + beginIdx--; + } + index = beginIdx; + if (beginIdx > endIdx) + return null; + idBeginIdx = beginIdx + 1; + idEndIdx = endIdx; + return code.substring(beginIdx + 1, endIdx + 1); + } + + public String getNextID() { + int beginIdx = index; + int endIdx = index; + char ch; + while (true) { + ch = code.charAt(beginIdx); + if ((ch == '_' || (ch >= 'a' && ch <= 'z') || ch >= 'A' + && ch <= 'Z')) { + break; + } + beginIdx++; + } + endIdx = beginIdx; + while (true) { + ch = code.charAt(endIdx); + if (!((ch == '_' || (ch >= 'a' && ch <= 'z') || ch >= 'A' + && ch <= 'Z'))) { + break; + } + endIdx++; + } + index = endIdx; + if (beginIdx > endIdx) + return null; + idBeginIdx = beginIdx; + idEndIdx = endIdx - 1; + return code.substring(beginIdx, endIdx); + } +} diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java b/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java new file mode 100644 index 0000000..ca78b43 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java @@ -0,0 +1,26 @@ +package edu.uci.eecs.specCompiler.specExtraction; + +import java.util.ArrayList; + +import edu.uci.eecs.specCompiler.codeGenerator.InterfaceWrongFormatException; + +public class ParserUtils { + public static String trimSpace(String line) { + int i, j; + char ch; + for (i = 0; i < line.length(); i++) { + ch = line.charAt(i); + if (ch != ' ' && ch != '\t') + break; + } + for (j = line.length() - 1; j >= 0; j--) { + ch = line.charAt(j); + if (ch != ' ' && ch != '\t') + break; + } + if (i > j) + return ""; + else + return line.substring(i, j + 1); + } +} diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java index 1286a14..2de7be9 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java @@ -7,6 +7,7 @@ import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.util.ArrayList; +import java.util.HashMap; import edu.uci.eecs.specCompiler.grammerParser.ParseException; import edu.uci.eecs.specCompiler.grammerParser.SpecParser; @@ -24,18 +25,16 @@ import edu.uci.eecs.specCompiler.grammerParser.TokenMgrError; * */ public class SpecExtractor { - private ArrayList<Construct> _constructs; - private int _beginLineNum, _endLineNum; - private String _beginLine; + public final ArrayList<Construct> constructs; + + public final HashMap<File, ArrayList<String>> contents; + public SpecExtractor() { - _constructs = new ArrayList<Construct>(); + constructs = new ArrayList<Construct>(); + contents = new HashMap<File, ArrayList<String>>(); } - ArrayList<Construct> getConstructs() { - return this._constructs; - } - /** * <p> * Given a list of files, it scans each file and add found SpecConstrcut to @@ -51,26 +50,20 @@ public class SpecExtractor { } public void extract(File file) { - StringBuilder specText = new StringBuilder(); - - } - - public static String trimSpace(String line) { - int i, j; - char ch; - for (i = 0; i < line.length(); i++) { - ch = line.charAt(i); - if (ch != ' ' && ch != '\t') - break; - } - for (j = line.length() - 1; j >= 0; j--) { - ch = line.charAt(j); - if (ch != ' ' && ch != '\t') - break; + if (contents.containsKey(file)) + return; + ArrayList<String> content = new ArrayList<String>(); + ArrayList<Construct> localConstructs = new ArrayList<Construct>(); + try { + SpecParser.ParseFile(file, content, localConstructs); + contents.put(file, content); + constructs.addAll(localConstructs); + } catch (ParseException e) { + e.printStackTrace(); + } catch (TokenMgrError e) { + e.printStackTrace(); } - if (i > j) - return ""; - else - return line.substring(i, j + 1); } + + } diff --git a/test.cc b/test.cc index b9b6ed3..b6707b9 100644 --- a/test.cc +++ b/test.cc @@ -1,28 +1,53 @@ #include <stdio.h> +#include <stdlib.h> +//#include "test.h" -typedef void (*action_t)(void*); +typedef void (*action_t)(); +void bar() { + printf("In bar\n"); +} + +template <typename T, action_t val, int b> class Class { public: + static T arr; struct A { int &a; }; + + static void action() { + printf("%d\n", arr); + } + + action_t getFuncPtr() { + foo(); + action(); + return &Class::action; + } - void action(void *info) { - printf("abc\n"); + static void foo() { + + } + + static void init() { + action_t inst1 = &foo; + int a = (1, 2); } Class() { - action_t inst = (action_t) &Class::action; - //(*inst)(NULL); + //ar = T((int)val); + (*val)(); + //foo(); } }; +#include "test.h" + int main() { - Class a; - Class &b = a; - Class *c = &b; - printf("%d\n", &b); - printf("%d\n", &c); + Class<int> c; + action_t f_ptr = c.getFuncPtr(); + //cc.getFuncPtr(); return 1; } + diff --git a/test.h b/test.h index 4517194..d06d734 100644 --- a/test.h +++ b/test.h @@ -1,15 +1,7 @@ #ifndef _TEST_H #define _TEST_H -struct Test { - int x; -/* - Test() { - x = 2; - } - */ -}; - -int globalVar = 0; +template <typename T, action_t a, int b> +T Class<T, a, b>::arr; #endif