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

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

icgrep linking

File size: 7.7 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/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#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
25#include <UCD/precompiled_properties.h>
26#endif
27
28#include "do_grep.h"
29
30static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
31                                       "These options control the regular expression source.");
32
33static cl::OptionCategory bGrepOutputOptions("Output Options",
34                                      "These options control the output.");
35
36static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
37
38static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
39static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
40static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
41
42static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
43static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
44
45static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
46static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
47static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
48
49static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
50static 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));
51
52
53
54ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
55
56    InitializeNativeTarget();
57    InitializeNativeTargetAsmPrinter();
58    InitializeNativeTargetAsmParser();
59
60    std::string errMessage;
61    EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
62    builder.setErrorStr(&errMessage);
63    builder.setMCPU(sys::getHostCPUName());
64    builder.setOptLevel(CodeGenOpt::Level::None);
65    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
66    ExecutionEngine * engine = builder.create();
67    if (engine == nullptr) {
68        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
69    }
70    //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
71    // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
72
73    return engine;
74}
75
76
77extern "C" {
78  void wrapped_print_register(char * regName, BitBlock bit_block) {
79      print_register<BitBlock>(regName, bit_block);
80  }
81}
82
83void icgrep_Linking(Module * m, ExecutionEngine * e) {
84    Module::FunctionListType & fns = m->getFunctionList();
85    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
86        std::string fnName = it->getName().str();
87        if (fnName == "process_block") continue;
88        if (fnName == "wrapped_print_register") {
89            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
90        }
91#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
92        else {
93            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
94            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
95        }
96#endif
97    }
98}
99
100
101int main(int argc, char *argv[]) {
102    StringMap<cl::Option*> Map;
103    cl::getRegisteredOptions(Map);
104    Map["time-passes"]->setHiddenFlag(cl::Hidden);
105    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
106    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
107    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
108    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
109    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
110    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
111    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
112    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
113    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
114    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
115    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
116    Map["regalloc"]->setHiddenFlag(cl::Hidden);
117    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
118    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
119    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
120    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
121    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
122    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
123    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
124    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
125#ifdef USE_LLVM_3_5
126    Map["spiller"]->setHiddenFlag(cl::Hidden);
127    Map["fatal-assembler-warnings"]->setHiddenFlag(cl::Hidden);
128#else
129    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
130    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
131
132#endif
133    cl::ParseCommandLineOptions(argc, argv);
134   
135   
136    int firstInputFile = 1;  // Normal case when first positional arg is a regex.
137
138    Encoding encoding(Encoding::Type::UTF_8, 8);
139
140   
141   
142   
143    //std::vector<std::string> regexVector;
144    if (RegexFilename != "") {
145        std::ifstream regexFile(RegexFilename.c_str());
146        std::string r;
147        if (regexFile.is_open()) {
148            while (std::getline(regexFile, r)) {
149                regexVector.push_back(r);
150            }
151            regexFile.close();
152        }
153    }
154   
155    // if there are no regexes specified through -e or -f, the first positional argument
156    // must be a regex, not an input file.
157   
158    if (regexVector.size() == 0) {
159        regexVector.push_back(inputFiles[0]);
160        firstInputFile = 1;
161    }
162    else {
163        firstInputFile = 0;
164    }
165   
166    re::ModeFlagSet globalFlags = 0;
167    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
168   
169    llvm::Function * icgrep_IR = icgrep::compile(encoding, regexVector, globalFlags);
170   
171    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
172   
173    icgrep_Linking(icgrep_IR->getParent(), engine);
174   
175    // Ensure everything is ready to go.
176    engine->finalizeObject();
177   
178    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
179   
180    if (icgrep_MCptr) {
181        GrepExecutor grepEngine = GrepExecutor(icgrep_MCptr);
182        grepEngine.setCountOnlyOption(CountOnly);
183        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
184        grepEngine.setShowLineNumberOption(ShowLineNumbers);
185            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
186            grepEngine.setShowFileNameOption();
187        }
188        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
189            grepEngine.doGrep(inputFiles[i].c_str());
190        }
191    }
192   
193    //engine->freeMachineCodeForFunction(icgrep_IR); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
194    delete engine;
195
196    return 0;
197}
198
Note: See TracBrowser for help on using the repository browser.