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

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

Factor out execution engine code from Pablo compiler into new JIT_to_ExecutionEngine routine.

File size: 6.6 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 <llvm/IR/Function.h>
16#include <llvm/ExecutionEngine/ExecutionEngine.h>
17#include <llvm/ExecutionEngine/MCJIT.h>
18#include <llvm/IRReader/IRReader.h>
19#include <llvm/Support/SourceMgr.h>
20#include <llvm/Support/CommandLine.h>
21#include <llvm/Support/TargetSelect.h>
22#include <llvm/Support/Host.h>
23
24#include "do_grep.h"
25
26static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
27                                       "These options control the regular expression source.");
28
29static 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
34static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
35static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
36static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
37
38static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
39static 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));
42static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
43static 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));
47
48
49
50ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
51
52    InitializeNativeTarget();
53    InitializeNativeTargetAsmPrinter();
54    InitializeNativeTargetAsmParser();
55
56    std::string errMessage;
57    EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
58    builder.setErrorStr(&errMessage);
59    builder.setMCPU(sys::getHostCPUName());
60
61    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
62    ExecutionEngine * engine = builder.create();
63    if (engine == nullptr) {
64        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
65    }
66    //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
67    // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
68
69    engine->finalizeObject();
70    return engine;
71}
72
73
74int main(int argc, char *argv[]) {
75    StringMap<cl::Option*> Map;
76    cl::getRegisteredOptions(Map);
77    Map["time-passes"]->setHiddenFlag(cl::Hidden);
78    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
79    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
80    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
81    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
82    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
83    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
84    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
85    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
86    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
87    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
88    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
89    Map["regalloc"]->setHiddenFlag(cl::Hidden);
90    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
91    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
92    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
93    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
94    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
95    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
96    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
97    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
98#ifdef USE_LLVM_3_5
99    Map["spiller"]->setHiddenFlag(cl::Hidden);
100    Map["fatal-assembler-warnings"]->setHiddenFlag(cl::Hidden);
101#else
102    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
103    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
104
105#endif
106    cl::ParseCommandLineOptions(argc, argv);
107   
108   
109    int firstInputFile = 1;  // Normal case when first positional arg is a regex.
110
111    Encoding encoding(Encoding::Type::UTF_8, 8);
112
113   
114    //std::vector<std::string> regexVector;
115    if (RegexFilename != "") {
116        std::ifstream regexFile(RegexFilename.c_str());
117        std::string r;
118        if (regexFile.is_open()) {
119            while (std::getline(regexFile, r)) {
120                regexVector.push_back(r);
121            }
122            regexFile.close();
123        }
124    }
125   
126    // if there are no regexes specified through -e or -f, the first positional argument
127    // must be a regex, not an input file.
128   
129    if (regexVector.size() == 0) {
130        regexVector.push_back(inputFiles[0]);
131        firstInputFile = 1;
132    }
133    else {
134        firstInputFile = 0;
135    }
136   
137    re::ModeFlagSet globalFlags = 0;
138    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
139   
140   
141    llvm::Function * llvm_codegen = icgrep::compile(encoding, regexVector, globalFlags);
142   
143    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(llvm_codegen);
144   
145    void * functionPointer = engine->getPointerToFunction(llvm_codegen);
146   
147    if (functionPointer) {
148        GrepExecutor grepEngine = GrepExecutor(functionPointer);
149        grepEngine.setCountOnlyOption(CountOnly);
150        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
151        grepEngine.setShowLineNumberOption(ShowLineNumbers);
152            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
153            grepEngine.setShowFileNameOption();
154        }
155        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
156            grepEngine.doGrep(inputFiles[i].c_str());
157        }
158    }
159   
160    //engine->freeMachineCodeForFunction(llvm_codegen); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
161    delete engine;
162
163    return 0;
164}
165
Note: See TracBrowser for help on using the repository browser.