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

Last change on this file since 4981 was 4974, checked in by nmedfort, 4 years ago

Added the kernel instance class; removed original mmap file access in favour of the boost mmap system. corrected PrintRegister? routine.

File size: 4.3 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 <toolchain.h>
9#include <utf_encoding.h>
10#include <pablo/pablo_compiler.h>
11#include <kernels/pipeline.h>
12#include <llvm/IR/Function.h>
13#include <llvm/IR/Type.h>
14#include <llvm/IR/Module.h>
15#include <llvm/ExecutionEngine/MCJIT.h>
16#include <llvm/IRReader/IRReader.h>
17#include <llvm/Support/Debug.h>
18#include <llvm/IR/Verifier.h>
19#include <UCD/UnicodeNameData.h>
20
21#include <fstream>
22#include <sstream>
23#include <iostream>
24#include <string>
25#include <stdint.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <errno.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <stdexcept>
34#include <cctype>
35
36
37#include <llvm/Support/raw_os_ostream.h>
38
39// mmap system
40#include <boost/filesystem.hpp>
41#include <boost/iostreams/device/mapped_file.hpp>
42using namespace boost::iostreams;
43using namespace boost::filesystem;
44
45#include <fcntl.h>
46
47#include <kernels/kernel.h>
48
49
50
51bool GrepEngine::finalLineIsUnterminated(char * mFileBuffer, size_t mFileSize) const {
52    if (mFileSize == 0) return false;
53    unsigned char end_byte = static_cast<unsigned char>(mFileBuffer[mFileSize-1]);
54    // LF through CR are line break characters
55    if ((end_byte >= 0xA) && (end_byte <= 0xD)) return false;
56    // Other line breaks require at least two bytes.
57    if (mFileSize == 1) return true;
58    // NEL
59    unsigned char penult_byte = static_cast<unsigned char>(mFileBuffer[mFileSize-2]);
60    if ((end_byte == 0x85) && (penult_byte == 0xC2)) return false;
61    if (mFileSize == 2) return true;
62    // LS and PS
63    if ((end_byte < 0xA8) || (end_byte > 0xA9)) return true;
64    return (static_cast<unsigned char>(mFileBuffer[mFileSize-3]) != 0xE2) || (penult_byte != 0x80);
65}
66
67void GrepEngine::doGrep(const std::string & fileName) {
68    std::string mFileName = fileName;
69    size_t mFileSize;
70    char * mFileBuffer;
71
72    const path file(mFileName);
73    if (exists(file)) {
74        if (is_directory(file)) {
75            return;
76        }
77    } else {
78        std::cerr << "Error: cannot open " << mFileName << " for processing. Skipped.\n";
79        return;
80    }
81
82    mFileSize = file_size(file);
83    mapped_file mFile;
84    if (mFileSize == 0) {
85        mFileBuffer = nullptr;
86    }
87    else {
88        try {
89            mFile.open(mFileName, mapped_file::priv, mFileSize, 0);
90        } catch (std::ios_base::failure e) {
91            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
92            return;
93        }
94        mFileBuffer = mFile.data();
95    }
96   
97    uint64_t finalLineUnterminated = 0;
98    if(finalLineIsUnterminated(mFileBuffer, mFileSize))
99        finalLineUnterminated = 1;
100   
101    mMainFcn(mFileBuffer, mFileSize, mFileName.c_str(), finalLineUnterminated);
102
103
104    mFile.close();
105
106}
107
108
109void GrepEngine::grepCodeGen(std::string moduleName, re::RE * re_ast, bool isNameExpression) {
110                           
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    pipelineBuilder.CreateKernels(function, isNameExpression);
123
124    pipelineBuilder.ExecuteKernels();
125
126    llvm::Function * main_IR = M->getFunction("Main");
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    mMainFcn = reinterpret_cast<GrepFunctionType>(mEngine->getPointerToFunction(main_IR));
137}
138
139re::CC *  GrepEngine::grepCodepoints() {
140
141    setParsedCodePointSet();
142    char * mFileBuffer = getUnicodeNameDataPtr();
143    size_t mFileSize = getUnicodeNameDataSize();
144    std::string mFileName = "Uname.txt";
145
146    uint64_t finalLineUnterminated = 0;
147    if(finalLineIsUnterminated(mFileBuffer, mFileSize))
148        finalLineUnterminated = 1;   
149    mMainFcn(mFileBuffer, mFileSize, mFileName.c_str(), finalLineUnterminated);
150
151    return getParsedCodePointSet();
152}
153
154GrepEngine::~GrepEngine() {
155    delete mEngine;
156}
Note: See TracBrowser for help on using the repository browser.