From cf2eb3dde8d9d49f28b9f91a726c865abe948109 Mon Sep 17 00:00:00 2001 From: Tuowen Zhao Date: Mon, 19 Sep 2016 22:32:41 +0000 Subject: parseRel as lib --- CMakeLists.txt | 11 ++-- include/chill_run_util.hh | 29 ---------- lib/parserel/CMakeLists.txt | 18 ++++++ lib/parserel/include/parseRel.hh | 29 ++++++++++ lib/parserel/src/parseRel.cc | 120 +++++++++++++++++++++++++++++++++++++++ lib/parserel/src/parseRel.ll | 24 ++++++++ lib/parserel/src/parseRel.yy | 85 +++++++++++++++++++++++++++ src/chill_run_util.cc | 120 --------------------------------------- src/chillmodule.cc | 2 +- src/parse_expr.ll | 24 -------- src/parse_expr.yy | 85 --------------------------- 11 files changed, 282 insertions(+), 265 deletions(-) delete mode 100644 include/chill_run_util.hh create mode 100644 lib/parserel/CMakeLists.txt create mode 100644 lib/parserel/include/parseRel.hh create mode 100644 lib/parserel/src/parseRel.cc create mode 100644 lib/parserel/src/parseRel.ll create mode 100644 lib/parserel/src/parseRel.yy delete mode 100644 src/chill_run_util.cc delete mode 100644 src/parse_expr.ll delete mode 100644 src/parse_expr.yy diff --git a/CMakeLists.txt b/CMakeLists.txt index 229a99d..4c5773e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ ADD_FLEX_BISON_DEPENDENCY(ExprScanner ExprParser) string(TIMESTAMP build_date "\\\"%m/%d/%Y\\\"") set(CORE_LIBS - m rose rt util omega codegen rosecg dl + m rose rt util omega codegen rosecg dl parseRel boost_date_time boost_filesystem boost_program_options boost_regex boost_system boost_wave boost_iostreams) @@ -44,10 +44,7 @@ set(IR_CHILL_SRC set(PYTHON_SRC src/chill_run.cc - src/chill_run_util.cc src/chillmodule.cc - ${FLEX_ExprScanner_OUTPUTS} - ${BISON_ExprParser_OUTPUTS} ) set(COMMON_FLAGS "-DCHILL_BUILD_DATE=\"${build_date}\" -DCHILL_BUILD_VERSION=\"\\\"${CHILL_VERSION}\\\"\"") @@ -58,8 +55,9 @@ if (DEFINED OMEGAHOME) link_directories(${OMEGAHOME}/lib) set(OMEGA_INC ${OMEGAHOME}/include) else() - set(OMEGA_INC + set(OMEGA_INC ${CMAKE_CURRENT_SOURCE_DIR}/lib/omega/include + ${CMAKE_CURRENT_SOURCE_DIR}/lib/parserel/include ${CMAKE_CURRENT_SOURCE_DIR}/lib/codegen/include ) endif() @@ -78,7 +76,7 @@ include_directories( add_executable(chill ${CORE_SRC} ${PYTHON_SRC} ${IR_CHILL_SRC}) target_link_libraries(chill ${CORE_LIBS} ${PYTHON_LIBRARY}) -add_dependencies(chill omega codegen rosecg) +add_dependencies(chill omega codegen rosecg parseRel) install(TARGETS chill RUNTIME DESTINATION bin) @@ -86,5 +84,6 @@ install(TARGETS chill add_subdirectory(lib/omega) add_subdirectory(lib/codegen) add_subdirectory(lib/rosecg) +add_subdirectory(lib/parserel) add_subdirectory(doc) diff --git a/include/chill_run_util.hh b/include/chill_run_util.hh deleted file mode 100644 index 8df5871..0000000 --- a/include/chill_run_util.hh +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef CHILL_RUN_UTIL_HH -#define CHILL_RUN_UTIL_HH - -#include -#include -#include - -typedef std::map simap_t; -typedef std::vector > simap_vec_t; - -// in chill_run_util.cc -simap_vec_t* make_prog(simap_vec_t* cond); -simap_vec_t* make_cond_gt(simap_t* lhs, simap_t* rhs); -simap_vec_t* make_cond_lt(simap_t* lhs, simap_t* rhs); -simap_vec_t* make_cond_ge(simap_t* lhs, simap_t* rhs); -simap_vec_t* make_cond_le(simap_t* lhs, simap_t* rhs); -simap_vec_t* make_cond_eq(simap_t* lhs, simap_t* rhs); -simap_t* make_cond_item_add(simap_t* lhs, simap_t* rhs); -simap_t* make_cond_item_sub(simap_t* lhs, simap_t* rhs); -simap_t* make_cond_item_mul(simap_t* lhs, simap_t* rhs); -simap_t* make_cond_item_neg(simap_t* expr); -simap_t* make_cond_item_number(int n); -simap_t* make_cond_item_variable(const char* var); -simap_t* make_cond_item_level(int n); - -// in parse_expr.yy -simap_vec_t* parse_relation_vector(const char* expr); - -#endif diff --git a/lib/parserel/CMakeLists.txt b/lib/parserel/CMakeLists.txt new file mode 100644 index 0000000..adc1d88 --- /dev/null +++ b/lib/parserel/CMakeLists.txt @@ -0,0 +1,18 @@ +find_package(BISON) +find_package(FLEX) + +FLEX_TARGET(ExprScanner src/parseRel.ll ${CMAKE_CURRENT_BINARY_DIR}/parseRel.yy.cc COMPILE_FLAGS + "--header-file=${CMAKE_CURRENT_BINARY_DIR}/parseRel.ll.hh") # Hack to avoid generating header in root +BISON_TARGET(ExprParser src/parseRel.yy ${CMAKE_CURRENT_BINARY_DIR}/parseRel.tab.cc COMPILE_FLAGS "-t -d") +ADD_FLEX_BISON_DEPENDENCY(ExprScanner ExprParser) + +include_directories( + include + ${CMAKE_CURRENT_BINARY_DIR} + ) + +add_library(parseRel + src/parseRel.cc + ${FLEX_ExprScanner_OUTPUTS} + ${BISON_ExprParser_OUTPUTS} + ) diff --git a/lib/parserel/include/parseRel.hh b/lib/parserel/include/parseRel.hh new file mode 100644 index 0000000..8df5871 --- /dev/null +++ b/lib/parserel/include/parseRel.hh @@ -0,0 +1,29 @@ +#ifndef CHILL_RUN_UTIL_HH +#define CHILL_RUN_UTIL_HH + +#include +#include +#include + +typedef std::map simap_t; +typedef std::vector > simap_vec_t; + +// in chill_run_util.cc +simap_vec_t* make_prog(simap_vec_t* cond); +simap_vec_t* make_cond_gt(simap_t* lhs, simap_t* rhs); +simap_vec_t* make_cond_lt(simap_t* lhs, simap_t* rhs); +simap_vec_t* make_cond_ge(simap_t* lhs, simap_t* rhs); +simap_vec_t* make_cond_le(simap_t* lhs, simap_t* rhs); +simap_vec_t* make_cond_eq(simap_t* lhs, simap_t* rhs); +simap_t* make_cond_item_add(simap_t* lhs, simap_t* rhs); +simap_t* make_cond_item_sub(simap_t* lhs, simap_t* rhs); +simap_t* make_cond_item_mul(simap_t* lhs, simap_t* rhs); +simap_t* make_cond_item_neg(simap_t* expr); +simap_t* make_cond_item_number(int n); +simap_t* make_cond_item_variable(const char* var); +simap_t* make_cond_item_level(int n); + +// in parse_expr.yy +simap_vec_t* parse_relation_vector(const char* expr); + +#endif diff --git a/lib/parserel/src/parseRel.cc b/lib/parserel/src/parseRel.cc new file mode 100644 index 0000000..43d7494 --- /dev/null +++ b/lib/parserel/src/parseRel.cc @@ -0,0 +1,120 @@ +#include +#include +#include "parseRel.hh" + +static std::string to_string(int ival) { + char buffer[4]; + sprintf(buffer, "%d", ival); + return std::string(buffer); +} + +simap_vec_t* make_prog(simap_vec_t* cond) { + return cond; +} + +simap_vec_t* make_cond_gt(simap_t* lhs, simap_t* rhs) { + simap_vec_t* nvec = new simap_vec_t(); + for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) + (*lhs)[it->first] -= it->second; + (*lhs)[to_string(0)] -= 1; + nvec->push_back(*lhs); + delete rhs; + delete lhs; + return nvec; +} + +simap_vec_t* make_cond_lt(simap_t* lhs, simap_t* rhs) { + return make_cond_gt(rhs, lhs); +} + +simap_vec_t* make_cond_ge(simap_t* lhs, simap_t* rhs) { + simap_vec_t* nvec = new simap_vec_t(); + for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) + (*lhs)[it->first] -= it->second; + nvec->push_back(*lhs); + delete rhs; + delete lhs; + return nvec; +} + +simap_vec_t* make_cond_le(simap_t* lhs, simap_t* rhs) { + return make_cond_ge(rhs, lhs); +} + +simap_vec_t* make_cond_eq(simap_t* lhs, simap_t* rhs) { + simap_vec_t* nvec = new simap_vec_t(); + for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) + (*rhs)[it->first] -= it->second; + nvec->push_back(*rhs); + for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) + it->second = -it->second; + nvec->push_back(*rhs); + delete rhs; + delete lhs; + return nvec; +} + +simap_t* make_cond_item_add(simap_t* lhs, simap_t* rhs) { + for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) + (*rhs)[it->first] += it->second; + delete lhs; + return rhs; +} + +simap_t* make_cond_item_sub(simap_t* lhs, simap_t* rhs) { + for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) + (*rhs)[it->first] -= it->second; + delete lhs; + return rhs; +} + +simap_t* make_cond_item_mul(simap_t* lhs, simap_t* rhs) { + (*lhs)[to_string(0)] += 0; + (*rhs)[to_string(0)] += 0; + if(rhs->size() == 1) { + int t = (*rhs)[to_string(0)]; + for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) + it->second *= t; + delete rhs; + return lhs; + } + else if(rhs->size() == 1) { + int t = (*lhs)[to_string(0)]; + for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) + it->second *= t; + delete lhs; + return rhs; + } + else { + fprintf(stderr, "require Presburger formula"); + delete lhs; + delete rhs; + // exit(2); <-- this may be a boost feature + } +} + +simap_t* make_cond_item_neg(simap_t* expr) { + for (simap_t::iterator it = expr->begin(); it != expr->end(); it++) { + it->second = -(it->second); + } + return expr; +} + +simap_t* make_cond_item_number(int n) { + simap_t* nmap = new simap_t(); + (*nmap)[to_string(0)] = n; + return nmap; +} + +simap_t* make_cond_item_variable(const char* var) { + simap_t* nmap = new simap_t(); + (*nmap)[std::string(var)] = 1; + return nmap; +} + +simap_t* make_cond_item_level(int n) { + simap_t* nmap = new simap_t(); + (*nmap)[to_string(n)] = 1; + return nmap; +} + diff --git a/lib/parserel/src/parseRel.ll b/lib/parserel/src/parseRel.ll new file mode 100644 index 0000000..f0cac81 --- /dev/null +++ b/lib/parserel/src/parseRel.ll @@ -0,0 +1,24 @@ +%{ +// some C++ code +#include "parseRel.hh" +#include "parseRel.tab.hh" +%} + +%option noyywrap + +%% +[ \t]+ /*ignore*/ +\n /*ignore*/ +L[0-9]+ { yylval.val = atoi(&yytext[1]); return LEVEL; } +[0-9]+ { yylval.val = atoi(yytext); return NUMBER; } +\<\= return LE; +\>\= return GE; +\=(\=)? return EQ; +[a-zA-Z_][a-zA-Z_0-9]* { + yylval.str_val = new char[yyleng+1]; + strcpy(yylval.str_val, yytext); + return VARIABLE; + } +. return (int)yytext[0]; +%% + diff --git a/lib/parserel/src/parseRel.yy b/lib/parserel/src/parseRel.yy new file mode 100644 index 0000000..98c329f --- /dev/null +++ b/lib/parserel/src/parseRel.yy @@ -0,0 +1,85 @@ +%{ +#include "parseRel.hh" +#include "parseRel.ll.hh" + +extern int yydebug; + +void yyerror(const char*); +int yyparse(simap_vec_t** rel); + +static simap_vec_t* return_rel; // used as the return value for yyparse + +%} + +%union { + int val; + char* str_val; + simap_t* cond_item; + simap_vec_t* cond; +} + +%token NUMBER +%token LEVEL +%token VARIABLE + +%left LE GE EQ '<' '>' +%left '-' '+' '*' '/' + +/*the final output from this language should be an Omega Relation object*/ +%type cond prog +%type expr add_expr mul_expr neg_expr + +%% +prog : cond { return_rel = make_prog($1); } +; + +cond : expr '>' expr { $$ = make_cond_gt($1, $3); } + | expr '<' expr { $$ = make_cond_lt($1, $3); } + | expr GE expr { $$ = make_cond_ge($1, $3); } + | expr LE expr { $$ = make_cond_le($1, $3); } + | expr EQ expr { $$ = make_cond_eq($1, $3); } +; + +expr : add_expr { $$ = $1; } +; + +add_expr : add_expr '+' mul_expr { $$ = make_cond_item_add($1,$3); } + | add_expr '-' mul_expr { $$ = make_cond_item_sub($1,$3); } + | mul_expr { $$ = $1; } +; + +mul_expr : mul_expr '*' neg_expr { $$ = make_cond_item_mul($1,$3); } + | neg_expr { $$ = $1; } +; + +neg_expr : '-' neg_expr { $$ = make_cond_item_neg($2); } + | '(' expr ')' { $$ = $2; } + | NUMBER { $$ = make_cond_item_number($1); } + | LEVEL { $$ = make_cond_item_level($1); } + | VARIABLE { $$ = make_cond_item_variable($1); } +; +%% + +void yyerror(const char* msg) { + fprintf(stderr, "Parse error: %s", msg); +} + +simap_vec_t* parse_relation_vector(const char* expr) { + yydebug=0; + YY_BUFFER_STATE state; + + //if(yylex_init()) { + // TODO: error out or something + //} + + state = yy_scan_string(expr); + + if(yyparse()) { + // TODO: error out or something + } + + yy_delete_buffer(state); + yylex_destroy(); + return return_rel; +} + diff --git a/src/chill_run_util.cc b/src/chill_run_util.cc deleted file mode 100644 index 29568e7..0000000 --- a/src/chill_run_util.cc +++ /dev/null @@ -1,120 +0,0 @@ -#include -#include -#include "chill_run_util.hh" - -static std::string to_string(int ival) { - char buffer[4]; - sprintf(buffer, "%d", ival); - return std::string(buffer); -} - -simap_vec_t* make_prog(simap_vec_t* cond) { - return cond; -} - -simap_vec_t* make_cond_gt(simap_t* lhs, simap_t* rhs) { - simap_vec_t* nvec = new simap_vec_t(); - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - (*lhs)[it->first] -= it->second; - (*lhs)[to_string(0)] -= 1; - nvec->push_back(*lhs); - delete rhs; - delete lhs; - return nvec; -} - -simap_vec_t* make_cond_lt(simap_t* lhs, simap_t* rhs) { - return make_cond_gt(rhs, lhs); -} - -simap_vec_t* make_cond_ge(simap_t* lhs, simap_t* rhs) { - simap_vec_t* nvec = new simap_vec_t(); - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - (*lhs)[it->first] -= it->second; - nvec->push_back(*lhs); - delete rhs; - delete lhs; - return nvec; -} - -simap_vec_t* make_cond_le(simap_t* lhs, simap_t* rhs) { - return make_cond_ge(rhs, lhs); -} - -simap_vec_t* make_cond_eq(simap_t* lhs, simap_t* rhs) { - simap_vec_t* nvec = new simap_vec_t(); - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - (*rhs)[it->first] -= it->second; - nvec->push_back(*rhs); - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - it->second = -it->second; - nvec->push_back(*rhs); - delete rhs; - delete lhs; - return nvec; -} - -simap_t* make_cond_item_add(simap_t* lhs, simap_t* rhs) { - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - (*rhs)[it->first] += it->second; - delete lhs; - return rhs; -} - -simap_t* make_cond_item_sub(simap_t* lhs, simap_t* rhs) { - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - (*rhs)[it->first] -= it->second; - delete lhs; - return rhs; -} - -simap_t* make_cond_item_mul(simap_t* lhs, simap_t* rhs) { - (*lhs)[to_string(0)] += 0; - (*rhs)[to_string(0)] += 0; - if(rhs->size() == 1) { - int t = (*rhs)[to_string(0)]; - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - it->second *= t; - delete rhs; - return lhs; - } - else if(rhs->size() == 1) { - int t = (*lhs)[to_string(0)]; - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - it->second *= t; - delete lhs; - return rhs; - } - else { - fprintf(stderr, "require Presburger formula"); - delete lhs; - delete rhs; - // exit(2); <-- this may be a boost feature - } -} - -simap_t* make_cond_item_neg(simap_t* expr) { - for (simap_t::iterator it = expr->begin(); it != expr->end(); it++) { - it->second = -(it->second); - } - return expr; -} - -simap_t* make_cond_item_number(int n) { - simap_t* nmap = new simap_t(); - (*nmap)[to_string(0)] = n; - return nmap; -} - -simap_t* make_cond_item_variable(const char* var) { - simap_t* nmap = new simap_t(); - (*nmap)[std::string(var)] = 1; - return nmap; -} - -simap_t* make_cond_item_level(int n) { - simap_t* nmap = new simap_t(); - (*nmap)[to_string(n)] = 1; - return nmap; -} - diff --git a/src/chillmodule.cc b/src/chillmodule.cc index 0e41f88..72f32d4 100644 --- a/src/chillmodule.cc +++ b/src/chillmodule.cc @@ -1,6 +1,6 @@ #include "chilldebug.h" -#include "chill_run_util.hh" +#include #include #include diff --git a/src/parse_expr.ll b/src/parse_expr.ll deleted file mode 100644 index a9b389f..0000000 --- a/src/parse_expr.ll +++ /dev/null @@ -1,24 +0,0 @@ -%{ -// some C++ code -#include "chill_run_util.hh" -#include "parse_expr.tab.hh" -%} - -%option noyywrap - -%% -[ \t]+ /*ignore*/ -\n /*ignore*/ -L[0-9]+ { yylval.val = atoi(&yytext[1]); return LEVEL; } -[0-9]+ { yylval.val = atoi(yytext); return NUMBER; } -\<\= return LE; -\>\= return GE; -\=(\=)? return EQ; -[a-zA-Z_][a-zA-Z_0-9]* { - yylval.str_val = new char[yyleng+1]; - strcpy(yylval.str_val, yytext); - return VARIABLE; - } -. return (int)yytext[0]; -%% - diff --git a/src/parse_expr.yy b/src/parse_expr.yy deleted file mode 100644 index c2943c2..0000000 --- a/src/parse_expr.yy +++ /dev/null @@ -1,85 +0,0 @@ -%{ -#include "chill_run_util.hh" -#include "parse_expr.ll.hh" - -extern int yydebug; - -void yyerror(const char*); -int yyparse(simap_vec_t** rel); - -static simap_vec_t* return_rel; // used as the return value for yyparse - -%} - -%union { - int val; - char* str_val; - simap_t* cond_item; - simap_vec_t* cond; -} - -%token NUMBER -%token LEVEL -%token VARIABLE - -%left LE GE EQ '<' '>' -%left '-' '+' '*' '/' - -/*the final output from this language should be an Omega Relation object*/ -%type cond prog -%type expr add_expr mul_expr neg_expr - -%% -prog : cond { return_rel = make_prog($1); } -; - -cond : expr '>' expr { $$ = make_cond_gt($1, $3); } - | expr '<' expr { $$ = make_cond_lt($1, $3); } - | expr GE expr { $$ = make_cond_ge($1, $3); } - | expr LE expr { $$ = make_cond_le($1, $3); } - | expr EQ expr { $$ = make_cond_eq($1, $3); } -; - -expr : add_expr { $$ = $1; } -; - -add_expr : add_expr '+' mul_expr { $$ = make_cond_item_add($1,$3); } - | add_expr '-' mul_expr { $$ = make_cond_item_sub($1,$3); } - | mul_expr { $$ = $1; } -; - -mul_expr : mul_expr '*' neg_expr { $$ = make_cond_item_mul($1,$3); } - | neg_expr { $$ = $1; } -; - -neg_expr : '-' neg_expr { $$ = make_cond_item_neg($2); } - | '(' expr ')' { $$ = $2; } - | NUMBER { $$ = make_cond_item_number($1); } - | LEVEL { $$ = make_cond_item_level($1); } - | VARIABLE { $$ = make_cond_item_variable($1); } -; -%% - -void yyerror(const char* msg) { - fprintf(stderr, "Parse error: %s", msg); -} - -simap_vec_t* parse_relation_vector(const char* expr) { - yydebug=0; - YY_BUFFER_STATE state; - - //if(yylex_init()) { - // TODO: error out or something - //} - - state = yy_scan_string(expr); - - if(yyparse()) { - // TODO: error out or something - } - - yy_delete_buffer(state); - yylex_destroy(); - return return_rel; -} - -- cgit v1.2.3-70-g09d2