-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>
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();
}
}
+ 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++) {
<COMMIT_POINT_SET: "@Commit_point_set:">
|
<ENTRY_POINT: "@Entry_point">
+|
+ <CLASS_BEGIN: "@Class_begin">
+|
+ <CLASS_END: "@Class_end">
|
<INTERFACE_DEFINE: "@Interface_define:">
|
|
<STRUCT: "struct">
|
- <TYPENAME: "typename">
+ <CLASS: "class">
+|
+ <TEMPLATE: "template">
|
<#DIGIT: ["0"-"9"]>
|
("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;
}
}
+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;
{
typeParams = new ArrayList<String>();
}
+ "("
(TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
+ ")"
{
- System.out.println(typeParams);
return typeParams;
}
}
String text;
Token t;
boolean newLine = false;
+ boolean inTemplate = false;
ArrayList<String> content;
}
{
(
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; } ) |
| 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; } )
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()
)
{
}
}
+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;
****** 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>
#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
} 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)
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 */
cdsannotate(SPEC_ANALYSIS, &hb_init0);
}
-#endif
-
+#endif /* End of
****** Example2: ******
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;
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);
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
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;
--- /dev/null
+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";
+ }
+}
--- /dev/null
+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
--- /dev/null
+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;
+ }
+}
--- /dev/null
+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);
+ }
+}
--- /dev/null
+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);
+ }
+}
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;
*
*/
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
}
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);
}
+
+
}
#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;
}
+
#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