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

Last change on this file since 4801 was 4801, checked in by cameron, 4 years ago

Refactor tool chain components

File size: 7.8 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 "toolchain.h"
13#include "utf_encoding.h"
14#include "pablo/pablo_compiler.h"
15#include <llvm/IR/Function.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/TargetSelect.h>
23#include <llvm/Support/Host.h>
24
25#include <re/re_re.h>
26#include <re/parsefailure.h>
27#include <re/re_parser.h>
28#include <re/re_any.h>
29#include <re/re_alt.h>
30#include <pablo/function.h>
31
32#include "do_grep.h"
33
34static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
35                                       "These options control the regular expression source.");
36
37static cl::OptionCategory bGrepOutputOptions("Output Options",
38                                      "These options control the output.");
39
40static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
41
42static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
43static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
44static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
45
46static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
47static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
48
49static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
50static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
51static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
52
53static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
54static 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));
55static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
56
57
58
59static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
60
61re::RE * get_icgrep_RE() {
62 
63    //std::vector<std::string> regexVector;
64    if (RegexFilename != "") {
65        std::ifstream regexFile(RegexFilename.c_str());
66        std::string r;
67        if (regexFile.is_open()) {
68            while (std::getline(regexFile, r)) {
69                regexVector.push_back(r);
70            }
71            regexFile.close();
72        }
73    }
74   
75    // if there are no regexes specified through -e or -f, the first positional argument
76    // must be a regex, not an input file.
77   
78    if (regexVector.size() == 0) {
79        regexVector.push_back(inputFiles[0]);
80        firstInputFile = 1;
81    }
82    else {
83        firstInputFile = 0;
84    }
85   
86    re::ModeFlagSet globalFlags = 0;
87    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
88
89 
90    std::vector<re::RE *> REs;
91    re::RE * re_ast = nullptr;
92    for (unsigned i = 0; i < regexVector.size(); i++) {
93        try
94        {
95            re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
96        }
97        catch (ParseFailure failure)
98        {
99            std::cerr << "Regex parsing failure: " << failure.what() << std::endl;
100            std::cerr << regexVector[i] << std::endl;
101            exit(1);
102        }
103        catch (UCD::UnicodePropertyExpressionError e)
104        {
105            std::cerr << "Unicode error: " << e.what() << std::endl;
106            std::cerr << regexVector[i] << std::endl;
107            exit(1);
108        }
109        REs.push_back(re_ast);
110    }
111    if (REs.size() > 1) {
112        re_ast = re::makeAlt(REs.begin(), REs.end());
113    }
114   
115    return re_ast;
116}
117
118
119int main(int argc, char *argv[]) {
120    StringMap<cl::Option*> Map;
121    cl::getRegisteredOptions(Map);
122    Map["time-passes"]->setHiddenFlag(cl::Hidden);
123    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
124    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
125    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
126    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
127    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
128    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
129    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
130    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
131    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
132    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
133    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
134    Map["regalloc"]->setHiddenFlag(cl::Hidden);
135    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
136    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
137    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
138    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
139    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
140    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
141    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
142    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
143    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
144    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
145
146    cl::ParseCommandLineOptions(argc, argv);
147   
148    Encoding encoding(Encoding::Type::UTF_8, 8);
149
150    llvm::Function * icgrep_IR = nullptr;
151   
152    if (IRFileName == "") {       
153        re::RE * re_ast = get_icgrep_RE();
154        re_ast = regular_expression_passes(encoding, re_ast);
155       
156        pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
157
158        pablo_function_passes(function);
159        pablo::PabloCompiler pablo_compiler;
160        try {
161            icgrep_IR = pablo_compiler.compile(function);
162            releaseSlabAllocatorMemory();
163        }
164        catch (std::runtime_error e) {
165            releaseSlabAllocatorMemory();
166            std::cerr << "Runtime error: " << e.what() << std::endl;
167            exit(1);
168        }
169    }
170    else {
171        firstInputFile = 0;  // No regexp arguments; first positional argument is a file to process.
172        SMDiagnostic ParseErr;
173        Module * M = parseIRFile(IRFileName, ParseErr, getGlobalContext()).release();
174        if (!M) {
175            throw std::runtime_error("Error in Parsing IR File " + IRFileName);
176        }
177        icgrep_IR = M->getFunction("process_block");
178    }
179   
180    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
181   
182    icgrep_Linking(icgrep_IR->getParent(), engine);
183   
184    // Ensure everything is ready to go.
185    engine->finalizeObject();
186   
187    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
188   
189    if (icgrep_MCptr) {
190        GrepExecutor grepEngine(icgrep_MCptr);
191        grepEngine.setCountOnlyOption(CountOnly);
192        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
193        grepEngine.setShowLineNumberOption(ShowLineNumbers);
194            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
195            grepEngine.setShowFileNameOption();
196        }
197        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
198            grepEngine.doGrep(inputFiles[i]);
199        }
200    }
201   
202    //engine->freeMachineCodeForFunction(icgrep_IR); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
203    delete engine;
204
205    return 0;
206}
207
Note: See TracBrowser for help on using the repository browser.