source: icGREP/icgrep-devel/icgrep/icgrep.cpp @ 5000

Last change on this file since 5000 was 5000, checked in by nmedfort, 3 years ago

Redesigned buffer system to allow the pipeline to control selection of the current input and output streams; DoBlock? functions containing lookahead now take multiple input stream arguments. Selection and passing occurs automatically. Some work on Symbol Table.

File size: 6.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 <cstdio>
8
9#include <llvm/Support/CommandLine.h>
10#include <re/re_alt.h>
11#include <re/re_parser.h>
12#include <grep_engine.h>
13#include <fstream>
14#include <string>
15
16#include <boost/uuid/sha1.hpp>
17#include <toolchain.h>
18#include <mutex>
19
20static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
21                                       "These options control the regular expression source.");
22
23static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
24
25static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
26
27static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
28static cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""), cl::cat(aRegexSourceOptions));
29static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
30
31static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(1));
32
33static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
34static std::string allREs;
35static re::ModeFlagSet globalFlags = 0;
36
37re::RE * get_icgrep_RE() {
38 
39    //std::vector<std::string> regexVector;
40    if (RegexFilename != "") {
41        std::ifstream regexFile(RegexFilename.c_str());
42        std::string r;
43        if (regexFile.is_open()) {
44            while (std::getline(regexFile, r)) {
45                regexVector.push_back(r);
46            }
47            regexFile.close();
48        }
49    }
50   
51    // if there are no regexes specified through -e or -f, the first positional argument
52    // must be a regex, not an input file.
53   
54    if (regexVector.size() == 0) {
55        regexVector.push_back(inputFiles[0]);
56        firstInputFile = 1;
57    }
58    else {
59        firstInputFile = 0;
60    }
61   
62    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
63
64 
65    std::vector<re::RE *> REs;
66    re::RE * re_ast = nullptr;
67    for (unsigned i = 0; i < regexVector.size(); i++) {
68        re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
69        REs.push_back(re_ast);
70        allREs += regexVector[i] + "\n";
71    }
72    if (REs.size() > 1) {
73        re_ast = re::makeAlt(REs.begin(), REs.end());
74    }
75   
76    return re_ast;
77}
78
79std::string sha1sum(const std::string & str) {
80    char buffer[41];    // 40 hex-digits and the terminating null
81    unsigned int digest[5];     // 160 bits in total
82
83    boost::uuids::detail::sha1 sha1;
84    sha1.process_bytes(str.c_str(), str.size());
85    sha1.get_digest(digest);
86    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
87             digest[0], digest[1], digest[2], digest[3], digest[4]);
88    return std::string(buffer);
89}
90
91std::mutex count_mutex;
92size_t fileCount;
93void *DoGrep(void *args)
94{
95    size_t fileIdx;
96    GrepEngine * grepEngine = (GrepEngine *)args;
97
98    count_mutex.lock();
99    fileCount++;
100    fileIdx = fileCount;
101    count_mutex.unlock();
102
103    while (fileIdx < inputFiles.size()){
104        grepEngine->doGrep(inputFiles[fileIdx]);
105       
106        count_mutex.lock();
107        fileCount++;
108        fileIdx = fileCount;
109        count_mutex.unlock();
110    }
111
112    pthread_exit(NULL);
113}
114
115int main(int argc, char *argv[]) {
116    StringMap<cl::Option*> Map;
117    cl::getRegisteredOptions(Map);
118    Map["time-passes"]->setHiddenFlag(cl::Hidden);
119    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
120    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
121    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
122    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
123    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
124    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
125    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
126    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
127    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
128    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
129    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
130    Map["regalloc"]->setHiddenFlag(cl::Hidden);
131    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
132    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
133    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
134    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
135    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
136    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
137    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
138    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
139    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
140    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
141
142    cl::ParseCommandLineOptions(argc, argv);
143   
144    re::RE * re_ast = get_icgrep_RE();
145    std::string module_name = "grepcode:" + sha1sum(allREs) + ":" + std::to_string(globalFlags);
146   
147    GrepEngine grepEngine;
148    grepEngine.grepCodeGen(module_name, re_ast);
149
150    initResult(inputFiles, firstInputFile, inputFiles.size());
151    if (Threads <= 1) {
152        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
153            grepEngine.doGrep(inputFiles[i]);
154        }       
155    } else if (Threads > 1) {
156        const unsigned numOfThreads = Threads; // <- convert the command line value into an integer to allow stack allocation
157        pthread_t threads[numOfThreads];
158
159        for(unsigned long i = 0; i < numOfThreads; ++i){
160            const int rc = pthread_create(&threads[i], NULL, DoGrep, (void *)&grepEngine);
161            if (rc) {
162                throw std::runtime_error("Failed to create thread: code " + std::to_string(rc));
163            }
164        }
165
166        for(unsigned i = 0; i < numOfThreads; ++i) {
167            void * status = nullptr;
168            const int rc = pthread_join(threads[i], &status);
169            if (rc) {
170                throw std::runtime_error("Failed to join thread: code " + std::to_string(rc));
171            }
172        }
173    }
174    PrintResult();   
175   
176    return 0;
177}
Note: See TracBrowser for help on using the repository browser.