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

Last change on this file since 5017 was 5017, checked in by lindanl, 20 months ago

Passing file index instead of file name as internal state.

File size: 4.2 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) {
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 file;
85        try {
86            file.open(fileName, mapped_file::priv, fileSize, 0);
87        } catch (std::ios_base::failure e) {
88            throw std::runtime_error("Boost mmap error: " + fileName + ": " + e.what());
89        }
90        char * const fileBuffer = file.data();
91        mGrepFunction(fileBuffer, fileSize, fileIdx, finalLineIsUnterminated(fileBuffer, fileSize));
92        file.close();
93    }
94}
95
96
97void GrepEngine::grepCodeGen(std::string moduleName, re::RE * re_ast, bool isNameExpression) {
98                           
99    Module * M = new Module(moduleName, getGlobalContext());
100   
101    IDISA::IDISA_Builder * idb = GetIDISA_Builder(M);
102
103    kernel::PipelineBuilder pipelineBuilder(M, idb);
104
105    Encoding encoding(Encoding::Type::UTF_8, 8);
106    mIsNameExpression = isNameExpression;
107    re_ast = regular_expression_passes(encoding, re_ast);   
108    pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
109   
110
111    pipelineBuilder.CreateKernels(function, isNameExpression);
112
113    llvm::Function * grepIR = pipelineBuilder.ExecuteKernels();
114
115    mEngine = JIT_to_ExecutionEngine(M);
116   
117    icgrep_Linking(M, mEngine);
118    #ifndef NDEBUG
119    verifyModule(*M, &dbgs());
120    #endif
121    mEngine->finalizeObject();
122    delete idb;
123
124    mGrepFunction = reinterpret_cast<GrepFunctionType>(mEngine->getPointerToFunction(grepIR));
125}
126
127re::CC *  GrepEngine::grepCodepoints() {
128
129    setParsedCodePointSet();
130    char * mFileBuffer = getUnicodeNameDataPtr();
131    size_t mFileSize = getUnicodeNameDataSize();
132    std::string mFileName = "Uname.txt";
133
134    uint64_t finalLineUnterminated = 0;
135    if(finalLineIsUnterminated(mFileBuffer, mFileSize))
136        finalLineUnterminated = 1;   
137    mGrepFunction(mFileBuffer, mFileSize, 0, finalLineUnterminated);
138
139    return getParsedCodePointSet();
140}
141
142GrepEngine::~GrepEngine() {
143    delete mEngine;
144}
Note: See TracBrowser for help on using the repository browser.