source: icGREP/icgrep-devel/icgrep/grep_engine.cpp @ 5025

Last change on this file since 5025 was 5025, checked in by xuedongx, 3 years ago

If '-c', ignore the scanmatch kernel.

File size: 4.7 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <grep_engine.h>
8#include <IDISA/idisa_builder.h>
9#include <IDISA/idisa_target.h>
10#include <re/re_toolchain.h>
11#include <pablo/pablo_toolchain.h>
12#include <toolchain.h>
13#include <utf_encoding.h>
14#include <pablo/pablo_compiler.h>
15#include <kernels/pipeline.h>
16#include <llvm/IR/Function.h>
17#include <llvm/IR/Type.h>
18#include <llvm/IR/Module.h>
19#include <llvm/ExecutionEngine/MCJIT.h>
20#include <llvm/IRReader/IRReader.h>
21#include <llvm/Support/Debug.h>
22#include <llvm/IR/Verifier.h>
23#include <UCD/UnicodeNameData.h>
24
25#include <fstream>
26#include <sstream>
27#include <iostream>
28#include <string>
29#include <stdint.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <errno.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <stdexcept>
38#include <cctype>
39
40
41#include <llvm/Support/raw_os_ostream.h>
42
43// mmap system
44#include <boost/filesystem.hpp>
45#include <boost/iostreams/device/mapped_file.hpp>
46using namespace boost::iostreams;
47using namespace boost::filesystem;
48
49#include <fcntl.h>
50
51#include <kernels/kernel.h>
52
53
54
55bool GrepEngine::finalLineIsUnterminated(const char * const fileBuffer, const size_t fileSize) {
56    if (fileSize == 0) return false;
57    unsigned char end_byte = static_cast<unsigned char>(fileBuffer[fileSize-1]);
58    // LF through CR are line break characters
59    if ((end_byte >= 0xA) && (end_byte <= 0xD)) return false;
60    // Other line breaks require at least two bytes.
61    if (fileSize == 1) return true;
62    // NEL
63    unsigned char penult_byte = static_cast<unsigned char>(fileBuffer[fileSize-2]);
64    if ((end_byte == 0x85) && (penult_byte == 0xC2)) return false;
65    if (fileSize == 2) return true;
66    // LS and PS
67    if ((end_byte < 0xA8) || (end_byte > 0xA9)) return true;
68    return (static_cast<unsigned char>(fileBuffer[fileSize-3]) != 0xE2) || (penult_byte != 0x80);
69}
70
71void GrepEngine::doGrep(const std::string & fileName, const int fileIdx, bool CountOnly, std::vector<int> & total_CountOnly) {
72    const path file(fileName);
73    if (exists(file)) {
74        if (is_directory(file)) {
75            return;
76        }
77    } else {
78        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
79        return;
80    }
81
82    const size_t fileSize = file_size(file);
83    if (fileSize > 0) {
84        mapped_file_source file;
85        try {
86            file.open(fileName);
87        } catch (std::exception &e) {
88            throw std::runtime_error("Boost mmap error: " + fileName + ": " + e.what());
89        }
90        char * fileBuffer = const_cast<char *>(file.data());
91        if(CountOnly){
92            total_CountOnly[fileIdx] = mGrepFunction_CountOnly(fileBuffer, fileSize, fileIdx, finalLineIsUnterminated(fileBuffer, fileSize));
93        }
94        else{
95            mGrepFunction(fileBuffer, fileSize, fileIdx, finalLineIsUnterminated(fileBuffer, fileSize));
96        }
97        file.close();
98    }
99    else {
100        if(CountOnly) {
101            mGrepFunction_CountOnly(nullptr, 0, fileIdx, false);
102        }
103        else{
104            mGrepFunction(nullptr, 0, fileIdx, false);
105        }
106    }
107}
108
109
110void GrepEngine::grepCodeGen(std::string moduleName, re::RE * re_ast, bool CountOnly, bool isNameExpression) {
111    Module * M = new Module(moduleName, getGlobalContext());
112   
113    IDISA::IDISA_Builder * idb = GetIDISA_Builder(M);
114
115    kernel::PipelineBuilder pipelineBuilder(M, idb);
116
117    Encoding encoding(Encoding::Type::UTF_8, 8);
118    mIsNameExpression = isNameExpression;
119    re_ast = regular_expression_passes(encoding, re_ast);   
120    pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
121   
122
123    pipelineBuilder.CreateKernels(function, isNameExpression);
124
125    llvm::Function * grepIR = pipelineBuilder.ExecuteKernels(CountOnly);
126
127    mEngine = JIT_to_ExecutionEngine(M);
128   
129    icgrep_Linking(M, mEngine);
130    #ifndef NDEBUG
131    verifyModule(*M, &dbgs());
132    #endif
133    mEngine->finalizeObject();
134    delete idb;
135
136    if(CountOnly){
137        mGrepFunction_CountOnly = reinterpret_cast<GrepFunctionType_CountOnly>(mEngine->getPointerToFunction(grepIR));
138    }
139    else{
140        mGrepFunction = reinterpret_cast<GrepFunctionType>(mEngine->getPointerToFunction(grepIR));
141    }
142
143}
144
145re::CC *  GrepEngine::grepCodepoints() {
146
147    setParsedCodePointSet();
148    char * mFileBuffer = getUnicodeNameDataPtr();
149    size_t mFileSize = getUnicodeNameDataSize();
150    std::string mFileName = "Uname.txt";
151
152    uint64_t finalLineUnterminated = 0;
153    if(finalLineIsUnterminated(mFileBuffer, mFileSize))
154        finalLineUnterminated = 1;   
155    mGrepFunction(mFileBuffer, mFileSize, 0, finalLineUnterminated);
156
157    return getParsedCodePointSet();
158}
159
160GrepEngine::~GrepEngine() {
161    delete mEngine;
162}
Note: See TracBrowser for help on using the repository browser.