Ignore:
Timestamp:
May 6, 2016, 3:28:07 PM (3 years ago)
Author:
cameron
Message:

Refactor: move grep-specific code out of toolchain

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5030 r5033  
    88#include <IDISA/idisa_builder.h>
    99#include <IDISA/idisa_target.h>
     10#include <llvm/Support/CommandLine.h>
    1011#include <re/re_toolchain.h>
     12#include <re/re_cc.h>
     13
    1114#include <pablo/pablo_toolchain.h>
    1215#include <toolchain.h>
     
    5053
    5154#include <kernels/kernel.h>
     55
     56static cl::OptionCategory bGrepOutputOptions("Output Options",
     57                                             "These options control the output.");
     58
     59static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
     60
     61static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
     62static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
     63
     64static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
     65static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
     66
    5267
    5368
     
    111126    Module * M = new Module(moduleName, getGlobalContext());
    112127   
    113     IDISA::IDISA_Builder * idb = GetIDISA_Builder(M);
     128    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
    114129
    115130    kernel::PipelineBuilder pipelineBuilder(M, idb);
     
    126141
    127142    mEngine = JIT_to_ExecutionEngine(M);
    128    
     143    ApplyObjectCache(mEngine);
    129144    icgrep_Linking(M, mEngine);
     145   
    130146    #ifndef NDEBUG
    131147    verifyModule(*M, &dbgs());
     
    143159}
    144160
     161
     162
    145163re::CC *  GrepEngine::grepCodepoints() {
    146164
     
    161179    delete mEngine;
    162180}
     181
     182
     183static int * total_count;
     184static std::stringstream * resultStrs = nullptr;
     185static std::vector<std::string> inputFiles;
     186
     187void initResult(std::vector<std::string> filenames){
     188    const int n = filenames.size();
     189    if (n > 1) {
     190        ShowFileNames = true;
     191    }
     192    inputFiles = filenames;
     193    resultStrs = new std::stringstream[n];
     194    total_count = new int[n];
     195    for (int i=0; i<inputFiles.size(); i++){
     196        total_count[i] = 0;
     197    }
     198   
     199}
     200
     201extern "C" {
     202    void wrapped_report_match(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer, uint64_t filesize, int fileIdx) {
     203       
     204        int idx = fileIdx;
     205       
     206        if (ShowFileNames) {
     207            resultStrs[idx] << inputFiles[idx] << ':';
     208        }
     209        if (ShowLineNumbers) {
     210            resultStrs[idx] << lineNum << ":";
     211        }
     212       
     213        if ((buffer[line_start] == 0xA) && (line_start != line_end)) {
     214            // The line "starts" on the LF of a CRLF.  Really the end of the last line.
     215            line_start++;
     216        }
     217        if (line_end == filesize) {
     218            // The match position is at end-of-file.   We have a final unterminated line.
     219            resultStrs[idx].write(&buffer[line_start], line_end - line_start);
     220            if (NormalizeLineBreaks) {
     221                resultStrs[idx] << '\n';  // terminate it
     222            }
     223            return;
     224        }
     225        unsigned char end_byte = (unsigned char)buffer[line_end];
     226        if (NormalizeLineBreaks) {
     227            if (end_byte == 0x85) {
     228                // Line terminated with NEL, on the second byte.  Back up 1.
     229                line_end--;
     230            } else if (end_byte > 0xD) {
     231                // Line terminated with PS or LS, on the third byte.  Back up 2.
     232                line_end -= 2;
     233            }
     234            resultStrs[idx].write(&buffer[line_start], line_end - line_start);
     235            resultStrs[idx] << '\n';
     236        }
     237        else{   
     238            if (end_byte == 0x0D) {
     239                // Check for line_end on first byte of CRLF;  note that we don't
     240                // want to access past the end of buffer.
     241                if ((line_end + 1 < filesize) && (buffer[line_end + 1] == 0x0A)) {
     242                    // Found CRLF; preserve both bytes.
     243                    line_end++;
     244                }
     245            }
     246            resultStrs[idx].write(&buffer[line_start], line_end - line_start + 1);
     247        }
     248    }
     249}
     250
     251void PrintResult(bool CountOnly, std::vector<int> & total_CountOnly){
     252    if(CountOnly){
     253        if (!ShowFileNames) {
     254            for (int i=0; i<inputFiles.size(); i++){
     255                std::cout << total_CountOnly[i] << std::endl;
     256            }
     257        }
     258        else {
     259            for (int i=0; i<inputFiles.size(); i++){
     260                std::cout << inputFiles[i] << ':' << total_CountOnly[i] << std::endl;
     261            };
     262        }
     263        return;
     264    }
     265   
     266    std::string out;
     267    for (int i=0; i<inputFiles.size(); i++){
     268        std::cout << resultStrs[i].str();
     269    }
     270}
     271
     272re::CC * parsedCodePointSet;
     273
     274extern "C" {
     275    void insert_codepoints(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer) {
     276        re::codepoint_t c = 0;
     277        ssize_t line_pos = line_start;
     278        while (isxdigit(buffer[line_pos])) {
     279            if (isdigit(buffer[line_pos])) {
     280                c = (c << 4) | (buffer[line_pos] - '0');
     281            }
     282            else {
     283                c = (c << 4) | (tolower(buffer[line_pos]) - 'a' + 10);
     284            }
     285            line_pos++;
     286        }
     287        assert(((line_pos - line_start) >= 4) && ((line_pos - line_start) <= 6)); // UCD format 4 to 6 hex digits.       
     288        parsedCodePointSet->insert(c);
     289    }
     290}
     291
     292void setParsedCodePointSet(){
     293    parsedCodePointSet = re::makeCC();
     294}
     295
     296re::CC * getParsedCodePointSet(){
     297    return parsedCodePointSet;
     298}
     299
     300
     301
     302
     303void icgrep_Linking(Module * m, ExecutionEngine * e) {
     304    Module::FunctionListType & fns = m->getFunctionList();
     305    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
     306        std::string fnName = it->getName().str();
     307        if (fnName == "s2p_block") continue;
     308        if (fnName == "process_block") continue;
     309        if (fnName == "process_block_initialize_carries") continue;
     310       
     311        if (fnName == "wrapped_report_match") {
     312            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_match);
     313        }
     314        if (fnName == "insert_codepoints") {
     315            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&insert_codepoints);
     316        }
     317#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
     318        else {
     319            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
     320            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
     321        }
     322#endif
     323    }
     324}
     325
Note: See TracChangeset for help on using the changeset viewer.