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

Last change on this file since 5023 was 5021, checked in by cameron, 3 years ago

Large file fix: use boost mapped_file_source

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