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

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

Temporary check-in for dynamic unicode class compilation.

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