Changeset 4132


Ignore:
Timestamp:
Sep 5, 2014, 8:50:49 PM (5 years ago)
Author:
cameron
Message:

Restructure - collapse cc_codegenobject into cc_compiler

Location:
icGREP/icgrep-devel
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/QA/abc_testgen.py

    r4094 r4132  
    77from random import randint
    88import os
     9import shutil
    910#
    1011#
  • icGREP/icgrep-devel/icgrep/CMakeLists.txt

    r4130 r4132  
    5353
    5454# add the executable
    55 add_executable(icgrep icgrep.cpp llvm_gen.cpp llvm_gen_helper.cpp utf_encoding.cpp cc_codegenobject.cpp  cc_compiler.cpp  cc_compiler_helper.cpp re_simplifier.cpp re_reducer.cpp re_nullable.cpp re_compiler.cpp pbix_compiler.cpp  symbol_generator.cpp utf8_encoder.cpp unicode_categories.h unicode_categories-flat.h unicode_categories-simple.h)
     55add_executable(icgrep icgrep.cpp llvm_gen.cpp llvm_gen_helper.cpp utf_encoding.cpp cc_compiler.cpp  cc_compiler_helper.cpp re_simplifier.cpp re_reducer.cpp re_nullable.cpp re_compiler.cpp pbix_compiler.cpp  symbol_generator.cpp utf8_encoder.cpp unicode_categories.h unicode_categories-flat.h unicode_categories-simple.h)
    5656
    5757target_link_libraries (icgrep PabloADT RegExpADT ${REQ_LLVM_LIBRARIES})
  • icGREP/icgrep-devel/icgrep/cc_compiler.cpp

    r4129 r4132  
    66
    77#include "cc_compiler.h"
    8 
    9 CC_Compiler::CC_Compiler(UTF_Encoding encoding)
     8#include "ps_pablos.h"
     9#include "utf_encoding.h"
     10#include "cc_compiler_helper.h"
     11
     12#include <math.h>
     13#include <utility>
     14#include <iostream>
     15#include <sstream>
     16#include <string>
     17#include <list>
     18#include <map>
     19#include <algorithm>
     20
     21#include <cassert>
     22#include <stdlib.h>
     23
     24CC_Compiler::CC_Compiler(const UTF_Encoding encoding, const std::string basis_pattern, const std::string gensym_pattern)
    1025{
    1126    mEncoding = encoding;
    12 }
    13 
    14 std::list<PabloS*> CC_Compiler::compile(std::string basis_pattern,
    15                                         std::string gensym_pattern,
    16                                         const std::map<std::string, RE*>& re_map)
    17 {
    1827    mEncoding.setBasisPattern(basis_pattern);
    19 
    20     CC_CodeGenObject cgo(gensym_pattern);
    21 
     28    mGenSym_Template = gensym_pattern;
     29    mGenSymCounter = 0;
     30
     31 
    2232    for (int i = 0; i < mEncoding.getBits(); i++)
    2333    {
     
    2636        expr->expr_string  =  b_pattern;
    2737        expr->pablo_expr = make_bitv(i);
    28         cgo.add_predefined(b_pattern, expr);
    29     }
    30 
    31     process_re_map(cgo, re_map);
    32 
    33     return cgo.get_stmtsl();
    34 }
    35 
    36 void CC_Compiler::process_re_map(CC_CodeGenObject &cgo,const std::map<std::string, RE*>& re_map)
     38        add_predefined(b_pattern, expr);
     39    }
     40}
     41
     42
     43void CC_Compiler::add_predefined(std::string key_value, Expression* mapped_value)
     44{
     45    mCommon_Expression_Map.insert(make_pair(key_value, mapped_value));
     46}
     47
     48Expression* CC_Compiler::add_assignment(std::string varname, Expression* expr)
     49{   
     50    //Add the new mapping to the list of pablo statements:
     51    mStmtsl.push_back(new Assign(varname, expr->pablo_expr));
     52
     53    //Add the new mapping to the common expression map:
     54    std::string key_value = expr->expr_string;
     55    Expression* mapped_value = new Expression();
     56    mapped_value->expr_string = varname;
     57    mapped_value->pablo_expr = new Var(varname);
     58
     59    std::pair<std::map<std::string, Expression*>::iterator, bool> ret = mCommon_Expression_Map.insert(make_pair(key_value, mapped_value));
     60
     61    return ret.first->second;
     62}
     63
     64Expression* CC_Compiler::expr_to_variable(Expression* expr)
     65{
     66    if (mCommon_Expression_Map.count(expr->expr_string) > 0)
     67    {
     68        std::map<std::string, Expression*>::iterator itGet = mCommon_Expression_Map.find(expr->expr_string);
     69        return itGet->second;
     70    }
     71    else
     72    {
     73        mGenSymCounter++;
     74        std::string sym = mGenSym_Template + std::to_string(mGenSymCounter);
     75        return add_assignment(sym, expr);
     76    }
     77}
     78
     79std::list<PabloS*> CC_Compiler::get_compiled()
     80{
     81    return mStmtsl;
     82}
     83
     84
     85
     86std::string CC_Compiler::compile1(CC* cc)
     87{
     88  cc2pablos(cc);
     89  return cc->getName();
     90}
     91
     92void CC_Compiler::compile_from_map(const std::map<std::string, RE*>& re_map)
     93{
     94    process_re_map(re_map);
     95}
     96
     97void CC_Compiler::process_re_map(const std::map<std::string, RE*>& re_map)
    3798{
    3899    for (auto it =  re_map.rbegin(); it != re_map.rend(); ++it)
    39100    {
    40         process_re(cgo, it->second);
    41     }
    42 }
    43 
    44 void CC_Compiler::process_re(CC_CodeGenObject &cgo, RE* re)
     101        process_re(it->second);
     102    }
     103}
     104
     105void CC_Compiler::process_re(RE* re)
    45106{
    46107
     
    50111        for (it = re_alt->GetREList()->begin(); it != re_alt->GetREList()->end(); ++it)
    51112        {
    52             process_re(cgo, *it);
     113            process_re(*it);
    53114        }
    54115    }
    55116    else if (CC* re_cc = dynamic_cast<CC*>(re))
    56117    {
    57         cgo = cc2pablos(cgo, re_cc);
     118        cc2pablos(re_cc);
    58119    }
    59120    else if (Rep* re_rep = dynamic_cast<Rep*>(re))
    60121    {
    61         process_re(cgo, re_rep->getRE());
     122        process_re(re_rep->getRE());
    62123    }
    63124    else if (Seq* re_seq = dynamic_cast<Seq*>(re))
     
    66127        for (it = re_seq->GetREList()->begin(); it != re_seq->GetREList()->end(); ++it)
    67128        {
    68             process_re(cgo, *it);
     129            process_re(*it);
    69130        }
    70131    }
     
    313374}
    314375
    315 Expression* CC_Compiler::expr2pabloe(CC_CodeGenObject &cgo, PabloE* expr)
     376Expression* CC_Compiler::expr2pabloe(PabloE* expr)
    316377{
    317378    /*
     
    342403    else if (Not* pe_not = dynamic_cast<Not*>(expr))
    343404    {
    344         Expression* ret = cgo.expr_to_variable(expr2pabloe(cgo, pe_not->getExpr()));
     405        Expression* ret = expr_to_variable(expr2pabloe(pe_not->getExpr()));
    345406        retExpr->expr_string =  "~" + ret->expr_string;
    346407        retExpr->pablo_expr = new Not(ret->pablo_expr);
     
    348409    else if(Or* pe_or = dynamic_cast<Or*>(expr))
    349410    {
    350         Expression* ret1 = cgo.expr_to_variable(expr2pabloe(cgo, pe_or->getExpr1()));
    351         Expression* ret2 = cgo.expr_to_variable(expr2pabloe(cgo, pe_or->getExpr2()));
     411        Expression* ret1 = expr_to_variable(expr2pabloe(pe_or->getExpr1()));
     412        Expression* ret2 = expr_to_variable(expr2pabloe(pe_or->getExpr2()));
    352413        retExpr->expr_string = "(" + ret1->expr_string + "|" + ret2->expr_string + ")";
    353414        retExpr->pablo_expr = new Or(ret1->pablo_expr, ret2->pablo_expr);
     
    355416    else if (Xor* pe_xor = dynamic_cast<Xor*>(expr))
    356417    {
    357         Expression* ret1 = cgo.expr_to_variable(expr2pabloe(cgo, pe_xor->getExpr1()));
    358         Expression* ret2 = cgo.expr_to_variable(expr2pabloe(cgo, pe_xor->getExpr2()));
     418        Expression* ret1 = expr_to_variable(expr2pabloe(pe_xor->getExpr1()));
     419        Expression* ret2 = expr_to_variable(expr2pabloe(pe_xor->getExpr2()));
    359420        retExpr->expr_string = "(" + ret1->expr_string + "^" + ret2->expr_string + ")";
    360421        retExpr->pablo_expr = new Xor(ret1->pablo_expr, ret2->pablo_expr);
     
    364425        if (Not* pe_not = dynamic_cast<Not*>(pe_and->getExpr1()))
    365426        {
    366             Expression* ret1 = cgo.expr_to_variable(expr2pabloe(cgo, pe_not->getExpr()));
    367             Expression* ret2 = cgo.expr_to_variable(expr2pabloe(cgo, pe_and->getExpr2()));
     427            Expression* ret1 = expr_to_variable(expr2pabloe(pe_not->getExpr()));
     428            Expression* ret2 = expr_to_variable(expr2pabloe(pe_and->getExpr2()));
    368429            retExpr->expr_string = "(" + ret2->expr_string + "&~" + ret1->expr_string + ")";
    369430            retExpr->pablo_expr = new And(ret2->pablo_expr, new Not(ret1->pablo_expr));
     
    371432        else if (Not* pe_not = dynamic_cast<Not*>(pe_and->getExpr2()))
    372433        {
    373             Expression* ret1 = cgo.expr_to_variable(expr2pabloe(cgo, pe_and->getExpr1()));
    374             Expression* ret2 = cgo.expr_to_variable(expr2pabloe(cgo, pe_not->getExpr()));
     434            Expression* ret1 = expr_to_variable(expr2pabloe(pe_and->getExpr1()));
     435            Expression* ret2 = expr_to_variable(expr2pabloe(pe_not->getExpr()));
    375436            retExpr->expr_string = "(" + ret1->expr_string  + "&~" + ret2->expr_string + ")";
    376437            retExpr->pablo_expr = new And(ret1->pablo_expr, new Not(ret2->pablo_expr));
     
    378439        else
    379440        {
    380             Expression* ret1 = cgo.expr_to_variable(expr2pabloe(cgo, pe_and->getExpr1()));
    381             Expression* ret2 = cgo.expr_to_variable(expr2pabloe(cgo, pe_and->getExpr2()));
     441            Expression* ret1 = expr_to_variable(expr2pabloe(pe_and->getExpr1()));
     442            Expression* ret2 = expr_to_variable(expr2pabloe(pe_and->getExpr2()));
    382443            retExpr->expr_string = "(" + ret1->expr_string + "&" + ret2->expr_string + ")";
    383444            retExpr->pablo_expr = new And(ret1->pablo_expr, ret2->pablo_expr);
     
    386447    else if (Sel * pe_sel = dynamic_cast<Sel*>(expr))
    387448    {
    388         Expression* ret_sel = cgo.expr_to_variable(expr2pabloe(cgo, pe_sel->getIf_expr()));
    389         Expression* ret_true = cgo.expr_to_variable(expr2pabloe(cgo, pe_sel->getT_expr()));
    390         Expression* ret_false = cgo.expr_to_variable(expr2pabloe(cgo, pe_sel->getF_expr()));
     449        Expression* ret_sel = expr_to_variable(expr2pabloe(pe_sel->getIf_expr()));
     450        Expression* ret_true = expr_to_variable(expr2pabloe(pe_sel->getT_expr()));
     451        Expression* ret_false = expr_to_variable(expr2pabloe(pe_sel->getF_expr()));
    391452        retExpr->expr_string = "((" + ret_sel->expr_string + "&" + ret_true->expr_string + ")|(~("
    392453            + ret_sel->expr_string + ")&" + ret_false->expr_string + ")";
     
    397458}
    398459
    399 CC_CodeGenObject CC_Compiler::cc2pablos(CC_CodeGenObject cgo, CC* cc)
    400 {
    401     cgo.add_assignment(cc->getName(), expr2pabloe(cgo, charset_expr(cc)));
    402 
    403     return cgo;
     460void CC_Compiler::cc2pablos(CC* cc)
     461{
     462    add_assignment(cc->getName(), expr2pabloe(charset_expr(cc)));
    404463}
    405464
  • icGREP/icgrep-devel/icgrep/cc_compiler.h

    r4129 r4132  
    88#define CC_COMPILER_H
    99
    10 #include "ps_pablos.h"
    11 #include "cc_codegenobject.h"
    12 #include "utf_encoding.h"
    13 #include "cc_compiler_helper.h"
    14 
    15 #include <math.h>
    16 #include <utility>
    17 #include <iostream>
    18 #include <sstream>
    1910#include <string>
    2011#include <list>
    2112#include <map>
    22 #include <algorithm>
     13#include "utf_encoding.h"
     14#include "ps_pablos.h"
     15#include "pe_pabloe.h"
     16#include "pe_sel.h"
     17#include "pe_advance.h"
     18#include "pe_all.h"
     19#include "pe_and.h"
     20#include "pe_charclass.h"
     21#include "pe_matchstar.h"
     22#include "pe_not.h"
     23#include "pe_or.h"
     24#include "pe_var.h"
     25#include "pe_xor.h"
     26#include "re_cc.h"
    2327
    24 #include <cassert>
    25 #include <stdlib.h>
     28struct Expression{
     29    std::string expr_string;
     30    PabloE* pablo_expr;
     31};
    2632
    2733
     
    2935{
    3036public:
    31     CC_Compiler(UTF_Encoding encoding);
    32     std::list<PabloS*> compile(std::string basis_pattern,
    33                                std::string gensym_pattern,
    34                                const std::map<std::string, RE*>& re_map);
     37    CC_Compiler(const UTF_Encoding encoding, const std::string basis_pattern, const std::string gensym_pattern);
     38    std::string compile1(CC* cc);   
     39    void compile_from_map(const std::map<std::string, RE*>& re_map);   
     40    std::list<PabloS*> get_compiled();
    3541private:
    36     void process_re_map(CC_CodeGenObject& cgo, const std::map<std::string, RE*>& re_map);
    37     void process_re(CC_CodeGenObject& cgo, RE* re);
     42    void process_re_map(const std::map<std::string, RE*>& re_map);
     43    void process_re(RE* re);
    3844    std::string bit_var(int n);
    3945    PabloE* make_bitv(int n);
     
    4551    PabloE* char_or_range_expr(CharSetItem charset_item);
    4652    PabloE* charset_expr(CC* cc);
    47     Expression* expr2pabloe(CC_CodeGenObject& cgo, PabloE* expr);
    48     CC_CodeGenObject cc2pablos(CC_CodeGenObject cgo, CC* cc);
     53    Expression* expr2pabloe(PabloE* expr);
     54    void cc2pablos(CC* cc);
    4955
    5056    UTF_Encoding mEncoding;
     57
     58
     59    void add_predefined(std::string key_value, Expression *mapped_value);
     60    Expression* add_assignment(std::string value, Expression* expr);
     61    Expression* expr_to_variable(Expression* cgo);
     62
     63    std::string mGenSym_Template;
     64    int mGenSymCounter;
     65    std::list<PabloS*> mStmtsl;
     66    std::map<std::string, Expression*> mCommon_Expression_Map;
    5167};
    5268
  • icGREP/icgrep-devel/icgrep/llvm_gen.h

    r4118 r4132  
    4040#include "ps_while.h"
    4141
    42 #include "cc_codegenobject.h"
     42#include "cc_compiler.h"
    4343
    4444#include "pbix_compiler.h"
  • icGREP/icgrep-devel/icgrep/re_compiler.cpp

    r4129 r4132  
    1616                                     std::string input_string)
    1717{
    18 
     18    CC_Compiler cc_compiler(encoding, basis_pattern, gensym_pattern);
    1919    ParseResult* parse_result = RE_Parser::parse_re(input_string);
    2020
     
    9797    name_map.insert(make_pair("UTF8-Prefix4", cc_name));
    9898
    99     CC_Compiler cc_compiler(encoding);
    100     std::list<PabloS*> cc_stmtsl = cc_compiler.compile(basis_pattern, gensym_pattern, re_map);
    101 
     99    cc_compiler.compile_from_map(re_map);
     100    std::list<PabloS*> cc_stmtsl = cc_compiler.get_compiled();
    102101    //Print to the terminal the AST that was generated by the character class compiler.
    103102    //std::cout << "\n" << "(" << StatementPrinter::Print_CC_PabloStmts(cc_stmtsl) << ")" << "\n" << std::endl;
  • icGREP/icgrep-devel/icgrep/re_compiler.h

    r3914 r4132  
    3434#include "re_parser.h"
    3535#include "cc_compiler.h"
    36 #include "cc_codegenobject.h"
    37 #include "cc_compiler.h"
    3836
    3937#include "pbix_compiler.h"
  • icGREP/icgrep-devel/icgrep/re_parser.h

    r3993 r4132  
    99
    1010#include "re_re.h"
     11#include "re_alt.h"
     12#include "re_cc.h"
     13#include "re_name.h"
     14#include "re_end.h"
     15#include "re_rep.h"
     16#include "re_seq.h"
     17#include "re_start.h"
    1118#include "parseresult.h"
    1219#include "parsesuccess.h"
    1320#include "parsefailure.h"
    14 
    15 #include "cc_compiler.h"
    1621
    1722#include <fstream>
Note: See TracChangeset for help on using the changeset viewer.