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

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

Refactor and encapsulate grep codegen/execution in GrepEngine?

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