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

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

Link in print_register as needed

File size: 7.1 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    builder.setOptLevel(CodeGenOpt::Level::None);
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    return engine;
70}
71
72
73extern "C" {
74  void wrapped_print_register(char * regName, BitBlock bit_block) {
75      print_register<BitBlock>(regName, bit_block);
76  }
77}
78
79void icgrep_Linking(Module * m, ExecutionEngine * e) {
80    Function * printRegFn = m->getFunction("wrapped_print_register");
81    if (printRegFn) {
82        e->addGlobalMapping(cast<GlobalValue>(printRegFn), (void *)&wrapped_print_register); 
83    }
84}
85
86
87int main(int argc, char *argv[]) {
88    StringMap<cl::Option*> Map;
89    cl::getRegisteredOptions(Map);
90    Map["time-passes"]->setHiddenFlag(cl::Hidden);
91    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
92    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
93    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
94    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
95    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
96    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
97    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
98    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
99    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
100    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
101    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
102    Map["regalloc"]->setHiddenFlag(cl::Hidden);
103    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
104    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
105    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
106    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
107    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
108    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
109    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
110    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
111#ifdef USE_LLVM_3_5
112    Map["spiller"]->setHiddenFlag(cl::Hidden);
113    Map["fatal-assembler-warnings"]->setHiddenFlag(cl::Hidden);
114#else
115    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
116    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
117
118#endif
119    cl::ParseCommandLineOptions(argc, argv);
120   
121   
122    int firstInputFile = 1;  // Normal case when first positional arg is a regex.
123
124    Encoding encoding(Encoding::Type::UTF_8, 8);
125
126   
127   
128   
129    //std::vector<std::string> regexVector;
130    if (RegexFilename != "") {
131        std::ifstream regexFile(RegexFilename.c_str());
132        std::string r;
133        if (regexFile.is_open()) {
134            while (std::getline(regexFile, r)) {
135                regexVector.push_back(r);
136            }
137            regexFile.close();
138        }
139    }
140   
141    // if there are no regexes specified through -e or -f, the first positional argument
142    // must be a regex, not an input file.
143   
144    if (regexVector.size() == 0) {
145        regexVector.push_back(inputFiles[0]);
146        firstInputFile = 1;
147    }
148    else {
149        firstInputFile = 0;
150    }
151   
152    re::ModeFlagSet globalFlags = 0;
153    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
154   
155    llvm::Function * icgrep_IR = icgrep::compile(encoding, regexVector, globalFlags);
156   
157    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
158   
159    icgrep_Linking(icgrep_IR->getParent(), engine);
160   
161    // Ensure everything is ready to go.
162    engine->finalizeObject();
163   
164    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
165   
166    if (icgrep_MCptr) {
167        GrepExecutor grepEngine = GrepExecutor(icgrep_MCptr);
168        grepEngine.setCountOnlyOption(CountOnly);
169        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
170        grepEngine.setShowLineNumberOption(ShowLineNumbers);
171            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
172            grepEngine.setShowFileNameOption();
173        }
174        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
175            grepEngine.doGrep(inputFiles[i].c_str());
176        }
177    }
178   
179    //engine->freeMachineCodeForFunction(icgrep_IR); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
180    delete engine;
181
182    return 0;
183}
184
Note: See TracBrowser for help on using the repository browser.