source: icGREP/icgrep-devel/icgrep-1.00/icgrep.cpp @ 4529

Last change on this file since 4529 was 4516, checked in by nmedfort, 5 years ago

More memory leak fixes. All known leaks accounted for.

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