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

Last change on this file since 4944 was 4944, checked in by lindanl, 3 years ago

64 bit version.

File size: 6.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 "toolchain.h"
12#include "utf_encoding.h"
13#include "pablo/pablo_compiler.h"
14#include <llvm/IR/Function.h>
15#include <llvm/IR/Type.h>
16#include <llvm/IR/Module.h>
17#include <llvm/ExecutionEngine/ExecutionEngine.h>
18#include <llvm/ExecutionEngine/MCJIT.h>
19#include <llvm/IRReader/IRReader.h>
20#include <llvm/Support/SourceMgr.h>
21#include <llvm/Support/CommandLine.h>
22#include <llvm/Support/Debug.h>
23#include <llvm/Support/TargetSelect.h>
24#include <llvm/Support/Host.h>
25#include <llvm/IR/Verifier.h>
26
27#include <re/re_re.h>
28#include <re/parsefailure.h>
29#include <re/re_parser.h>
30#include <re/re_any.h>
31#include <re/re_alt.h>
32#include <pablo/function.h>
33
34#include "do_grep.h"
35#include <kernels/pipeline.h>
36
37static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
38                                       "These options control the regular expression source.");
39
40// static cl::OptionCategory bGrepOutputOptions("Output Options",
41//                                       "These options control the output.");
42
43static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
44
45// static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
46// static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
47// static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
48
49// static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
50// static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
51
52static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
53// static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
54// static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
55
56static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
57static 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));
58static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
59
60
61
62static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
63
64re::RE * get_icgrep_RE() {
65 
66    //std::vector<std::string> regexVector;
67    if (RegexFilename != "") {
68        std::ifstream regexFile(RegexFilename.c_str());
69        std::string r;
70        if (regexFile.is_open()) {
71            while (std::getline(regexFile, r)) {
72                regexVector.push_back(r);
73            }
74            regexFile.close();
75        }
76    }
77   
78    // if there are no regexes specified through -e or -f, the first positional argument
79    // must be a regex, not an input file.
80   
81    if (regexVector.size() == 0) {
82        regexVector.push_back(inputFiles[0]);
83        firstInputFile = 1;
84    }
85    else {
86        firstInputFile = 0;
87    }
88   
89    re::ModeFlagSet globalFlags = 0;
90    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
91
92 
93    std::vector<re::RE *> REs;
94    re::RE * re_ast = nullptr;
95    for (unsigned i = 0; i < regexVector.size(); i++) {
96        re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
97        REs.push_back(re_ast);
98    }
99    if (REs.size() > 1) {
100        re_ast = re::makeAlt(REs.begin(), REs.end());
101    }
102   
103    return re_ast;
104}
105
106
107int main(int argc, char *argv[]) {
108    StringMap<cl::Option*> Map;
109    cl::getRegisteredOptions(Map);
110    Map["time-passes"]->setHiddenFlag(cl::Hidden);
111    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
112    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
113    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
114    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
115    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
116    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
117    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
118    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
119    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
120    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
121    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
122    Map["regalloc"]->setHiddenFlag(cl::Hidden);
123    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
124    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
125    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
126    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
127    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
128    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
129    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
130    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
131    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
132    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
133
134    cl::ParseCommandLineOptions(argc, argv);
135   
136    Module * M = new Module("grepcode", getGlobalContext());
137   
138    IDISA::IDISA_Builder * idb = GetNativeIDISA_Builder(M, VectorType::get(IntegerType::get(getGlobalContext(), 64), BLOCK_SIZE/64));
139
140    PipelineBuilder pipelineBuilder(M, idb);
141
142    re::RE * re_ast = get_icgrep_RE();
143    Encoding encoding(Encoding::Type::UTF_8, 8);
144    re_ast = regular_expression_passes(encoding, re_ast);   
145    pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
146
147    pipelineBuilder.CreateKernels(function, false);
148
149    pipelineBuilder.ExecuteKernels();
150
151    llvm::Function * main_IR = M->getFunction("Main");
152    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(M);
153   
154    icgrep_Linking(M, engine);
155    verifyModule(*M, &dbgs());
156    engine->finalizeObject();
157
158    void * main_MCptr = engine->getPointerToFunction(main_IR);
159
160    if(main_MCptr){
161        GrepExecutor grepEngine(main_MCptr);
162        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
163            grepEngine.doGrep(inputFiles[i]);
164        }
165    }
166   
167    delete engine;
168    delete idb; 
169
170    return 0;
171}
Note: See TracBrowser for help on using the repository browser.