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

Last change on this file since 4963 was 4963, checked in by hongpum, 3 years ago

Append globalFlags to the hashsum used by caching

File size: 6.1 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 <string>
8#include <iostream>
9#include <fstream>
10#include <cstdio>
11
12
13#include <llvm/Support/SourceMgr.h>
14#include <llvm/Support/CommandLine.h>
15#include <llvm/Support/Debug.h>
16#include <llvm/Support/TargetSelect.h>
17#include <llvm/Support/Host.h>
18
19#include <re/re_re.h>
20#include <re/parsefailure.h>
21#include <re/re_parser.h>
22#include <re/re_any.h>
23#include <re/re_alt.h>
24
25#include <grep_engine.h>
26
27#include <boost/uuid/sha1.hpp>
28
29static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
30                                       "These options control the regular expression source.");
31
32// static cl::OptionCategory bGrepOutputOptions("Output Options",
33//                                       "These options control the output.");
34
35static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
36
37// static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
38// static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
39// static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
40
41// static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
42// static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
43
44static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
45// static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
46// static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
47
48static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
49static 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));
50static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
51
52
53
54static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
55static std::string allREs;
56static re::ModeFlagSet globalFlags = 0;
57
58re::RE * get_icgrep_RE() {
59 
60    //std::vector<std::string> regexVector;
61    if (RegexFilename != "") {
62        std::ifstream regexFile(RegexFilename.c_str());
63        std::string r;
64        if (regexFile.is_open()) {
65            while (std::getline(regexFile, r)) {
66                regexVector.push_back(r);
67            }
68            regexFile.close();
69        }
70    }
71   
72    // if there are no regexes specified through -e or -f, the first positional argument
73    // must be a regex, not an input file.
74   
75    if (regexVector.size() == 0) {
76        regexVector.push_back(inputFiles[0]);
77        firstInputFile = 1;
78    }
79    else {
80        firstInputFile = 0;
81    }
82   
83    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
84
85 
86    std::vector<re::RE *> REs;
87    re::RE * re_ast = nullptr;
88    for (unsigned i = 0; i < regexVector.size(); i++) {
89        re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
90        REs.push_back(re_ast);
91        allREs += regexVector[i] + "\n";
92    }
93    if (REs.size() > 1) {
94        re_ast = re::makeAlt(REs.begin(), REs.end());
95    }
96   
97    return re_ast;
98}
99
100std::string sha1sum(const std::string & str) {
101    char buffer[41];    // 40 hex-digits and the terminating null
102    unsigned int digest[5];     // 160 bits in total
103
104    boost::uuids::detail::sha1 sha1;
105    sha1.process_bytes(str.c_str(), str.size());
106    sha1.get_digest(digest);
107    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
108             digest[0], digest[1], digest[2], digest[3], digest[4]);
109    return std::string(buffer);
110}
111
112
113int main(int argc, char *argv[]) {
114    StringMap<cl::Option*> Map;
115    cl::getRegisteredOptions(Map);
116    Map["time-passes"]->setHiddenFlag(cl::Hidden);
117    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
118    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
119    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
120    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
121    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
122    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
123    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
124    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
125    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
126    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
127    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
128    Map["regalloc"]->setHiddenFlag(cl::Hidden);
129    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
130    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
131    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
132    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
133    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
134    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
135    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
136    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
137    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
138    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
139
140    cl::ParseCommandLineOptions(argc, argv);
141   
142    re::RE * re_ast = get_icgrep_RE();
143    std::string module_name = "grepcode:" + sha1sum(allREs) + ":" + std::to_string(globalFlags);
144   
145    GrepEngine grepEngine;
146    grepEngine.grepCodeGen(module_name, re_ast);
147   
148    for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
149        if (grepEngine.openMMap(inputFiles[i])) {
150            grepEngine.doGrep();
151            grepEngine.closeMMap();
152        }
153    }
154   
155    return 0;
156}
Note: See TracBrowser for help on using the repository browser.