From 1c89b92ed0363b0c283fc49cdd4f53451eb47d3d Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Fri, 25 Oct 2013 16:17:02 -0700 Subject: [PATCH] lots of changes --- grammer/spec.txt | 193 ++++++++++- grammer/spec_compiler.jj | 199 +++++------ notes/generated_code_examples.txt | 194 ++++------- .../codeGenerator/CodeGenerator.java | 45 +-- .../codeGenerator/CodeVariables.java | 325 +----------------- .../codeGenerator/SemanticsChecker.java | 8 - .../specExtraction/ActionSubConstruct.java | 57 --- .../specExtraction/Construct.java | 8 - .../specExtraction/FunctionHeader.java | 13 +- .../specExtraction/InterfaceConstruct.java | 10 +- .../specExtraction/ParserUtils.java | 10 +- .../specExtraction/QualifiedName.java | 24 ++ .../SequentialDefineSubConstruct.java | 26 +- .../specExtraction/SourceFileInfo.java | 26 ++ .../specExtraction/SpecExtractor.java | 27 +- .../specExtraction/VariableDeclaration.java | 15 + test.c | 10 + test.cc | 53 --- test.h | 2 - 19 files changed, 498 insertions(+), 747 deletions(-) delete mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/QualifiedName.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/SourceFileInfo.java create mode 100644 src/edu/uci/eecs/specCompiler/specExtraction/VariableDeclaration.java create mode 100644 test.c delete mode 100644 test.cc diff --git a/grammer/spec.txt b/grammer/spec.txt index 5fba648..634a051 100644 --- a/grammer/spec.txt +++ b/grammer/spec.txt @@ -1,2 +1,191 @@ -Class* //A::B::id(const char * ch_ptr, int a) -//template < TypeK k, TypeV v> +#include +#include +#include "librace.h" +#include "model-assert.h" + +#include "my_queue.h" + +#define relaxed memory_order_relaxed +#define release memory_order_release +#define acquire memory_order_acquire + +#define MAX_FREELIST 4 /* Each thread can own up to MAX_FREELIST free nodes */ +#define INITIAL_FREE 2 /* Each thread starts with INITIAL_FREE free nodes */ + +#define POISON_IDX 0x666 + +static unsigned int (*free_lists)[MAX_FREELIST]; + +/* Search this thread's free list for a "new" node */ +static unsigned int new_node() +{ + int i; + int t = get_thread_num(); + for (i = 0; i < MAX_FREELIST; i++) { + unsigned int node = load_32(&free_lists[t][i]); + if (node) { + store_32(&free_lists[t][i], 0); + return node; + } + } + /* free_list is empty? */ + MODEL_ASSERT(0); + return 0; +} + +/* Place this node index back on this thread's free list */ +static void reclaim(unsigned int node) +{ + int i; + int t = get_thread_num(); + + /* Don't reclaim NULL node */ + MODEL_ASSERT(node); + + for (i = 0; i < MAX_FREELIST; i++) { + /* Should never race with our own thread here */ + unsigned int idx = load_32(&free_lists[t][i]); + + /* Found empty spot in free list */ + if (idx == 0) { + store_32(&free_lists[t][i], node); + return; + } + } + /* free list is full? */ + MODEL_ASSERT(0); +} + +void init_queue(queue_t *q, int num_threads) +{ + /** + @Begin + @Entry_point + @End + */ + + int i, j; + + /* Initialize each thread's free list with INITIAL_FREE pointers */ + /* The actual nodes are initialized with poison indexes */ + free_lists = malloc(num_threads * sizeof(*free_lists)); + for (i = 0; i < num_threads; i++) { + for (j = 0; j < INITIAL_FREE; j++) { + free_lists[i][j] = 2 + i * MAX_FREELIST + j; + atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0)); + } + } + + /* initialize queue */ + atomic_init(&q->head, MAKE_POINTER(1, 0)); + atomic_init(&q->tail, MAKE_POINTER(1, 0)); + atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0)); +} + +/** + @Begin + @Interface_define: Enqueue + @End +*/ +void enqueue(queue_t *q, unsigned int val) +{ + int success = 0; + unsigned int node; + pointer tail; + pointer next; + pointer tmp; + + node = new_node(); + store_32(&q->nodes[node].value, val); + tmp = atomic_load_explicit(&q->nodes[node].next, relaxed); + set_ptr(&tmp, 0); // NULL + atomic_store_explicit(&q->nodes[node].next, tmp, relaxed); + + while (!success) { + tail = atomic_load_explicit(&q->tail, acquire); + next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire); + if (tail == atomic_load_explicit(&q->tail, relaxed)) { + + /* Check for uninitialized 'next' */ + MODEL_ASSERT(get_ptr(next) != POISON_IDX); + + if (get_ptr(next) == 0) { // == NULL + pointer value = MAKE_POINTER(node, get_count(next) + 1); + success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next, + &next, value, release, release); + } + if (!success) { + unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire)); + pointer value = MAKE_POINTER(ptr, + get_count(tail) + 1); + int commit_success = 0; + commit_success = atomic_compare_exchange_strong_explicit(&q->tail, + &tail, value, release, release); + /** + @Begin + @Commit_point_define_check: __ATOMIC_RET__ == true + @Label: Enqueue_Success_Point + @End + */ + thrd_yield(); + } + } + } + atomic_compare_exchange_strong_explicit(&q->tail, + &tail, + MAKE_POINTER(node, get_count(tail) + 1), + release, release); +} + + +/** + @Begin + @Interface_define: Dequeue + @End +*/ +unsigned int dequeue(queue_t *q) +{ + unsigned int value; + int success = 0; + pointer head; + pointer tail; + pointer next; + + while (!success) { + head = atomic_load_explicit(&q->head, acquire); + tail = atomic_load_explicit(&q->tail, relaxed); + next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire); + if (atomic_load_explicit(&q->head, relaxed) == head) { + if (get_ptr(head) == get_ptr(tail)) { + + /* Check for uninitialized 'next' */ + MODEL_ASSERT(get_ptr(next) != POISON_IDX); + + if (get_ptr(next) == 0) { // NULL + return 0; // NULL + } + atomic_compare_exchange_strong_explicit(&q->tail, + &tail, + MAKE_POINTER(get_ptr(next), get_count(tail) + 1), + release, release); + thrd_yield(); + } else { + value = load_32(&q->nodes[get_ptr(next)].value); + success = atomic_compare_exchange_strong_explicit(&q->head, + &head, + MAKE_POINTER(get_ptr(next), get_count(head) + 1), + release, release); + /** + @Begin + @Commit_point_define_check: __ATOMIC_RET__ == true + @Label: Dequeue_Success_Point + @End + */ + if (!success) + thrd_yield(); + } + } + } + reclaim(get_ptr(head)); + return value; +} diff --git a/grammer/spec_compiler.jj b/grammer/spec_compiler.jj index 440007a..8e2477d 100644 --- a/grammer/spec_compiler.jj +++ b/grammer/spec_compiler.jj @@ -39,9 +39,6 @@ @Check: (Optional) ... @Action: (Optional) - # Type here must be a pointer - @DefineVar: Type var1 = SomeExpression (Optional) - @Code (Optional) ... @Post_action: (Optional) @Post_check: (Optional) @@ -104,14 +101,15 @@ import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct; import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface; -import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct; -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; +import edu.uci.eecs.specCompiler.specExtraction.QualifiedName; +import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo; +import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration; public class SpecParser { private static ArrayList _content; @@ -122,36 +120,43 @@ import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader; public static void main(String[] argvs) throws ParseException, TokenMgrError { try { - String line = "int* A::B::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 content = new ArrayList(); ArrayList constructs = new ArrayList(); - parser.Parse(f, content, constructs); + ArrayList headers = new ArrayList(); + parser.Parse(f, content, constructs, headers); for (int i = 0; i < content.size(); i++) { System.out.println(content.get(i)); } - */ - parser.Test(); + + for (int i = 0; i < constructs.size(); i++) { + System.out.println(constructs.get(i)); + } + + //parser.Test(); System.out.println("Parsing finished!"); } catch (FileNotFoundException e) { e.printStackTrace(); } } - public static void ParseFile(File f, ArrayList content, ArrayList constructs) + public static SourceFileInfo ParseFile(File f) throws ParseException, TokenMgrError { try { InputStream input = new FileInputStream(f); SpecParser parser = new SpecParser(input); - parser.Parse(f, content, constructs); + ArrayList content = new ArrayList(), + headers = new ArrayList(); + ArrayList constructs = new ArrayList(); + parser.Parse(f, content, constructs, headers); + return new SourceFileInfo(f, content, headers, constructs); } catch (FileNotFoundException e) { e.printStackTrace(); } + return null; } public static ArrayList getTemplateArg(String line) @@ -267,8 +272,6 @@ SKIP : { | -| - | | @@ -325,7 +328,6 @@ SKIP : { "> | - | /* C/C++ only token*/ @@ -449,6 +451,7 @@ String Type() : { String type; String str; + QualifiedName name; } { { type = ""; } @@ -457,18 +460,11 @@ String Type() : )? (((str = .image | str = .image) { type = type + " " + str; })? ( - str = QualifiedName() { + name = ParseQualifiedName() { if (!type.equals("")) - type = type + " " + str; + type = type + " " + name.fullName; else - type = str; - }) - ( - str = ParameterizedName() { - if (!type.equals("")) - type = type + " " + str; - else - type = str; + type = name.fullName; }) ) ((str = "const".image { @@ -496,10 +492,21 @@ String Type() : } void Test() : -{} { - Type() - //FuncDecl() + String str; + FunctionHeader func; +} +{ + /* + str = Type() + { + System.out.println(str); + } + */ + func = FuncDecl() + { + System.out.println(func); + } } String ParameterizedName() : @@ -520,31 +527,34 @@ String ParameterizedName() : FunctionHeader FuncDecl() : { - String ret, qualifiedName, bareName; - ArrayList args; + String ret; + QualifiedName funcName; + ArrayList args; } { ret = Type() - qualifiedName = QualifiedName() - bareName = .image + funcName = ParseQualifiedName() args = FormalParamList() { - FunctionHeader res = new FunctionHeader(ret, qualifiedName, bareName, args); + FunctionHeader res = new FunctionHeader(ret, funcName, args); //System.out.println(res); return res; } } -String QualifiedName() : +QualifiedName ParseQualifiedName() : { String qualifiedName, str; } { { qualifiedName = ""; } - (LOOKAHEAD(2) (str = ParameterizedName() { qualifiedName = qualifiedName + - str + "::"; } ))* + (str = ParameterizedName() { qualifiedName = qualifiedName + str; } ) + ( (str = ParameterizedName() { qualifiedName = qualifiedName + + "::" + str; } ))* { - return qualifiedName; + QualifiedName res = new QualifiedName(qualifiedName); + //System.out.println(res); + return res; } } @@ -571,41 +581,43 @@ ArrayList TemplateParamList() : } } -ArrayList FormalParamList() : +ArrayList FormalParamList() : { - ArrayList typeParams; + ArrayList typeParams; + VariableDeclaration varDecl; } { { - typeParams = new ArrayList(); + typeParams = new ArrayList(); } "(" - (TypeParam(typeParams) ( TypeParam(typeParams))*)? + ((varDecl = TypeParam() {typeParams.add(varDecl);}) + (( varDecl = TypeParam() {typeParams.add(varDecl);}))*)? ")" { return typeParams; } } -void TypeParam(ArrayList typeParams) : +VariableDeclaration TypeParam() : { String type, param; } { (type = Type()) (param = .image) { - typeParams.add(type); - typeParams.add(param); + return new VariableDeclaration(type, param); } } -ArrayList C_CPP_CODE() : +ArrayList C_CPP_CODE(ArrayList headers) : { String text; Token t; boolean newLine = false; boolean inTemplate = false; ArrayList content; + String header; } { { @@ -618,7 +630,12 @@ ArrayList C_CPP_CODE() : ( t = | t = | t = | (t =