source: icGREP/icgrep-devel/icgrep/toolchain.cpp @ 4960

Last change on this file since 4960 was 4959, checked in by nmedfort, 4 years ago

Initial modifications to Pablo Compiler and Kernel Builder to support circular buffers for Lookahead.

File size: 24.2 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 <llvm/IR/Function.h>
12#include <llvm/IR/Module.h>
13#include <llvm/ExecutionEngine/ExecutionEngine.h>
14#include <llvm/ExecutionEngine/MCJIT.h>
15#include <llvm/IRReader/IRReader.h>
16#include <llvm/Support/CommandLine.h>
17#include <llvm/CodeGen/CommandFlags.h>
18#include <llvm/Support/SourceMgr.h>
19#include <llvm/Support/TargetSelect.h>
20#include <llvm/Support/Host.h>
21
22#include <IDISA/idisa_avx_builder.h>
23#include <IDISA/idisa_sse_builder.h>
24#include <IDISA/idisa_i64_builder.h>
25#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
26#include <UCD/precompiled_properties.h>
27#endif
28#include <re/re_cc.h>
29#include <re/re_nullable.h>
30#include <re/re_simplifier.h>
31#include <re/re_alt.h>
32#include <re/parsefailure.h>
33#include <re/re_parser.h>
34#include <re/re_compiler.h>
35#include <utf8_encoder.h>
36#include <cc/cc_compiler.h>
37#include <pablo/pablo_compiler.h>
38#include <pablo/optimizers/pablo_simplifier.hpp>
39#include <pablo/optimizers/codemotionpass.h>
40#include <pablo/passes/flattenassociativedfg.h>
41#include <pablo/passes/factorizedfg.h>
42#ifdef ENABLE_MULTIPLEXING
43#include <pablo/optimizers/pablo_automultiplexing.hpp>
44#include <pablo/optimizers/pablo_bddminimization.h>
45#include <pablo/optimizers/distributivepass.h>
46#include <pablo/optimizers/schedulingprepass.h>
47#endif
48#include <pablo/function.h>
49#include <pablo/analysis/pabloverifier.hpp>
50#include <re/printer_re.h>
51#include <pablo/printer_pablos.h>
52// Dynamic processor detection
53#define ISPC_LLVM_VERSION ISPC_LLVM_3_6
54#include "ispc.cpp"
55
56using namespace pablo;
57
58static cl::OptionCategory bGrepOutputOptions("Output Options",
59                                      "These options control the output.");
60
61static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
62static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
63static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
64
65static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
66static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
67
68static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
69static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
70
71
72static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
73                                              "These options control printing of intermediate regular expression structures.");
74static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
75static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
76static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
77static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
78static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
79static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
80
81static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options", "These options control printing of intermediate Pablo code.");
82
83static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
84static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
85static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
86static cl::opt<std::string> PabloOutputFilename("print-pablo-output", cl::init(""), cl::desc("output Pablo filename"), cl::cat(dPabloDumpOptions));
87
88static cl::OptionCategory cMachineCodeOptimization("Machine Code Optimizations", "These options control back-end compilier optimization levels.");
89
90
91static cl::opt<char> OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O0')"),
92                              cl::cat(cMachineCodeOptimization), cl::Prefix, cl::ZeroOrMore, cl::init('0'));
93
94static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
95
96static cl::opt<bool> DisableSimplification("disable-simplification", cl::init(false),
97                                     cl::desc("Disable Pablo Simplification pass (not recommended)"),
98                                     cl::cat(cPabloOptimizationsOptions));
99
100static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
101                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
102                                      cl::cat(cPabloOptimizationsOptions));
103
104#ifdef ENABLE_MULTIPLEXING
105static cl::opt<bool> PrintUnloweredCode("print-unlowered-pablo", cl::init(false), cl::desc("print Pablo output prior to lowering. "), cl::cat(dPabloDumpOptions));
106
107static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(false),
108                                        cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
109                                        cl::cat(cPabloOptimizationsOptions));
110
111static cl::opt<bool> EnableLowering("lowering", cl::init(false),
112                                         cl::desc("coalesce associative functions prior to optimization passes."),
113                                         cl::cat(cPabloOptimizationsOptions));
114
115static cl::opt<bool> EnablePreDistribution("pre-dist", cl::init(false),
116                                         cl::desc("apply distribution law optimization prior to multiplexing."),
117                                         cl::cat(cPabloOptimizationsOptions));
118
119static cl::opt<bool> EnablePostDistribution("post-dist", cl::init(false),
120                                         cl::desc("apply distribution law optimization after multiplexing."),
121                                         cl::cat(cPabloOptimizationsOptions));
122
123static cl::opt<bool> EnablePrePassScheduling("pre-pass-scheduling", cl::init(false),
124                                         cl::desc("apply pre-pass scheduling prior to LLVM IR generation."),
125                                         cl::cat(cPabloOptimizationsOptions));
126#endif
127
128static cl::OptionCategory dCodeGenOptions("Code Generation Options", "These options control code generation.");
129
130static cl::opt<bool> DisableAVX2("disable-AVX2", cl::init(false), cl::desc("disable AVX2 instruction set."), cl::cat(dCodeGenOptions));
131
132static cl::opt<int> BlockSize("BlockSize", cl::init(0), cl::desc("specify a block size (defaults to widest SIMD register width in bits)."), cl::cat(dCodeGenOptions));
133
134
135re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
136    if (PrintAllREs || PrintParsedREs) {
137        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
138    }
139
140    //Optimization passes to simplify the AST.
141    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
142    if (PrintAllREs || PrintStrippedREs) {
143        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
144    }
145    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
146    if (PrintAllREs || PrintStrippedREs) {
147        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
148    }
149
150    re_ast = re::RE_Simplifier::simplify(re_ast);
151    if (PrintAllREs || PrintSimplifiedREs) {
152        //Print to the terminal the AST that was generated by the simplifier.
153        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
154    }
155    return re_ast;
156}
157   
158PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
159    PabloFunction * function = PabloFunction::Create("process_block", 8, 2);
160    cc::CC_Compiler cc_compiler(*function, encoding);
161    re::RE_Compiler re_compiler(*function, cc_compiler);
162    re_compiler.initializeRequiredStreams();
163    re_compiler.compileUnicodeNames(re_ast);
164    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
165
166    if (PrintCompiledREcode) {
167        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
168        llvm::raw_os_ostream cerr(std::cerr);
169        cerr << "Initial Pablo AST:\n";
170        PabloPrinter::print(*function, cerr);
171    }
172    #ifndef NDEBUG
173    PabloVerifier::verify(*function, "creation");
174    #endif
175    return function;
176}
177
178#ifdef PRINT_TIMING_INFORMATION
179#define READ_CYCLE_COUNTER(name) name = read_cycle_counter();
180#else
181#define READ_CYCLE_COUNTER(name)
182#endif
183
184#ifdef PRINT_TIMING_INFORMATION
185unsigned COUNT_STATEMENTS(const PabloFunction * const entry) {
186    std::stack<const Statement *> scope;
187    unsigned statements = 0;
188    // Scan through and collect all the advances, calls, scanthrus and matchstars ...
189    for (const Statement * stmt = entry->getEntryBlock()->front(); ; ) {
190        while ( stmt ) {
191            ++statements;
192            if (LLVM_UNLIKELY(isa<If>(stmt) || isa<While>(stmt))) {
193                // Set the next statement to be the first statement of the inner scope and push the
194                // next statement of the current statement into the scope stack.
195                const PabloBlock * const nested = isa<If>(stmt) ? cast<If>(stmt)->getBody() : cast<While>(stmt)->getBody();
196                scope.push(stmt->getNextNode());
197                stmt = nested->front();
198                assert (stmt);
199                continue;
200            }
201            stmt = stmt->getNextNode();
202        }
203        if (scope.empty()) {
204            break;
205        }
206        stmt = scope.top();
207        scope.pop();
208    }
209    return statements;
210}
211
212unsigned COUNT_ADVANCES(const PabloFunction * const entry) {
213
214    std::stack<const Statement *> scope;
215    unsigned advances = 0;
216
217    // Scan through and collect all the advances, calls, scanthrus and matchstars ...
218    for (const Statement * stmt = entry->getEntryBlock()->front(); ; ) {
219        while ( stmt ) {
220            if (isa<Advance>(stmt)) {
221                ++advances;
222            }
223            else if (LLVM_UNLIKELY(isa<If>(stmt) || isa<While>(stmt))) {
224                // Set the next statement to be the first statement of the inner scope and push the
225                // next statement of the current statement into the scope stack.
226                const PabloBlock * const nested = isa<If>(stmt) ? cast<If>(stmt)->getBody() : cast<While>(stmt)->getBody();
227                scope.push(stmt->getNextNode());
228                stmt = nested->front();
229                assert (stmt);
230                continue;
231            }
232            stmt = stmt->getNextNode();
233        }
234        if (scope.empty()) {
235            break;
236        }
237        stmt = scope.top();
238        scope.pop();
239    }
240    return advances;
241}
242
243using DistributionMap = boost::container::flat_map<unsigned, unsigned>;
244
245DistributionMap SUMMARIZE_VARIADIC_DISTRIBUTION(const PabloFunction * const entry) {
246    std::stack<const Statement *> scope;
247    DistributionMap distribution;
248    // Scan through and collect all the advances, calls, scanthrus and matchstars ...
249    for (const Statement * stmt = entry->getEntryBlock()->front(); ; ) {
250        while ( stmt ) {
251            if (isa<Variadic>(stmt)) {
252                auto f = distribution.find(stmt->getNumOperands());
253                if (f == distribution.end()) {
254                    distribution.emplace(stmt->getNumOperands(), 1);
255                } else {
256                    f->second += 1;
257                }
258            }
259            else if (LLVM_UNLIKELY(isa<If>(stmt) || isa<While>(stmt))) {
260                // Set the next statement to be the first statement of the inner scope and push the
261                // next statement of the current statement into the scope stack.
262                const PabloBlock * const nested = isa<If>(stmt) ? cast<If>(stmt)->getBody() : cast<While>(stmt)->getBody();
263                scope.push(stmt->getNextNode());
264                stmt = nested->front();
265                assert (stmt);
266                continue;
267            }
268            stmt = stmt->getNextNode();
269        }
270        if (scope.empty()) {
271            break;
272        }
273        stmt = scope.top();
274        scope.pop();
275    }
276    return distribution;
277}
278#endif
279
280void pablo_function_passes(PabloFunction * function) {
281    // Scan through the pablo code and perform DCE and CSE
282
283#ifdef PRINT_TIMING_INFORMATION
284    timestamp_t simplification_start = 0, simplification_end = 0;
285    timestamp_t coalescing_start = 0, coalescing_end = 0;
286    timestamp_t sinking_start = 0, sinking_end = 0;
287    timestamp_t pre_distribution_start = 0, pre_distribution_end = 0;
288    timestamp_t multiplexing_start = 0, multiplexing_end = 0;
289    timestamp_t post_distribution_start = 0, post_distribution_end = 0;
290    timestamp_t lowering_start = 0, lowering_end = 0;
291    timestamp_t scheduling_start = 0, scheduling_end = 0;
292    DistributionMap distribution;
293    const timestamp_t optimization_start = read_cycle_counter();
294#endif
295    if (!DisableSimplification) {
296        READ_CYCLE_COUNTER(simplification_start);
297        Simplifier::optimize(*function);
298        READ_CYCLE_COUNTER(simplification_end);
299    }
300#ifdef ENABLE_MULTIPLEXING
301    if (EnableLowering || EnablePreDistribution || EnablePostDistribution) {
302        READ_CYCLE_COUNTER(coalescing_start);
303        CanonicalizeDFG::transform(*function);
304        READ_CYCLE_COUNTER(coalescing_end);
305    }
306    if (EnablePreDistribution) {
307        READ_CYCLE_COUNTER(pre_distribution_start);
308        DistributivePass::optimize(*function);
309        READ_CYCLE_COUNTER(pre_distribution_end);
310    }
311    if (EnableMultiplexing) {
312        READ_CYCLE_COUNTER(multiplexing_start);
313        MultiplexingPass::optimize(*function);
314        READ_CYCLE_COUNTER(multiplexing_end);
315        if (EnableLowering || EnablePreDistribution || EnablePostDistribution) {
316            CanonicalizeDFG::transform(*function);
317        }
318    }
319    if (EnablePostDistribution) {
320        READ_CYCLE_COUNTER(post_distribution_start);
321        DistributivePass::optimize(*function);
322        READ_CYCLE_COUNTER(post_distribution_end);
323    }
324#endif
325    if (PabloSinkingPass) {
326        READ_CYCLE_COUNTER(sinking_start);
327        CodeMotionPass::optimize(*function);
328        READ_CYCLE_COUNTER(sinking_end);
329    }
330#ifdef ENABLE_MULTIPLEXING
331    if (PrintUnloweredCode) {
332        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
333        llvm::raw_os_ostream cerr(std::cerr);
334        cerr << "Unlowered Pablo AST:\n";
335        PabloPrinter::print(*function, cerr);
336    }
337    #ifdef PRINT_TIMING_INFORMATION
338    distribution = SUMMARIZE_VARIADIC_DISTRIBUTION(function);
339    #endif
340    if (EnableLowering || EnablePreDistribution || EnablePostDistribution) {
341        READ_CYCLE_COUNTER(lowering_start);
342        FactorizeDFG::transform(*function);
343        READ_CYCLE_COUNTER(lowering_end);
344    }
345    if (EnablePrePassScheduling) {
346        READ_CYCLE_COUNTER(scheduling_start);
347        SchedulingPrePass::optimize(*function);
348        READ_CYCLE_COUNTER(scheduling_end);
349    }
350#endif
351#ifdef PRINT_TIMING_INFORMATION
352    const timestamp_t optimization_end = read_cycle_counter();
353#endif
354    if (PrintOptimizedREcode) {
355        if (PabloOutputFilename.empty()) {
356            //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
357            llvm::raw_os_ostream cerr(std::cerr);
358            cerr << "Final Pablo AST:\n";
359            PabloPrinter::print(*function, cerr);
360        } else {
361            std::error_code error;
362            llvm::raw_fd_ostream out(PabloOutputFilename, error, sys::fs::OpenFlags::F_None);
363            PabloPrinter::print(*function, out);
364        }
365    }
366#ifdef PRINT_TIMING_INFORMATION
367    std::cerr << "PABLO OPTIMIZATION TIME: " << (optimization_end - optimization_start) << std::endl;
368    std::cerr << "  SIMPLIFICATION TIME: " << (simplification_end - simplification_start) << std::endl;
369    std::cerr << "  COALESCING TIME: " << (coalescing_end - coalescing_start) << std::endl;
370    std::cerr << "  SINKING TIME: " << (sinking_end - sinking_start) << std::endl;
371    std::cerr << "  PRE-DISTRIBUTION TIME: " << (pre_distribution_end - pre_distribution_start) << std::endl;
372    std::cerr << "  MULTIPLEXING TIME: " << (multiplexing_end - multiplexing_start) << std::endl;
373    std::cerr << "  MULTIPLEXING SEED: " << MultiplexingPass::SEED << std::endl;
374    std::cerr << "  MULTIPLEXING NODES USED: " << MultiplexingPass::NODES_USED << std::endl;
375    std::cerr << "  MULTIPLEXING NODES ALLOCATED: " << MultiplexingPass::NODES_ALLOCATED << std::endl;
376    std::cerr << "  LOWERING TIME: " << (lowering_end - lowering_start) << std::endl;
377    std::cerr << "  POST-DISTRIBUTION TIME: " << (post_distribution_end - post_distribution_start) << std::endl;
378    std::cerr << "  SCHEDULING TIME: " << (scheduling_end - scheduling_start) << std::endl;
379    std::cerr << "PABLO STATEMENTS: " << COUNT_STATEMENTS(function) << std::endl;
380    std::cerr << "PABLO ADVANCES: " << COUNT_ADVANCES(function) << std::endl;
381    std::cerr << "PRE-LOWERING VARIADIC DISTRIBUTION: ";
382    bool join = false;
383    for (auto dist : distribution) {
384        if (join) {
385            std::cerr << ';';
386        }
387        std::cerr << dist.first << '|' << dist.second;
388        join = true;
389    }
390    std::cerr << std::endl;
391#endif
392}
393
394
395IDISA::IDISA_Builder * GetIDISA_Builder(Module * mod) {
396    bool hasAVX2 = (strncmp(lGetSystemISA(), "avx2", 4) == 0);
397   
398    unsigned theBlockSize = BlockSize;  // from command line
399   
400    if (theBlockSize == 0) {  // No BlockSize override: use processor SIMD width
401        theBlockSize = hasAVX2 ? 256 : 128;
402    }
403    Type * bitBlockType = VectorType::get(IntegerType::get(getGlobalContext(), 64), theBlockSize/64);
404   
405    int blockSize = bitBlockType->isIntegerTy() ? cast<IntegerType>(bitBlockType)->getIntegerBitWidth() : cast<VectorType>(bitBlockType)->getBitWidth();
406    if (blockSize >= 256) {
407        if (hasAVX2) {
408            return new IDISA::IDISA_AVX2_Builder(mod, bitBlockType);
409        }
410        else{
411            return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
412        }
413    }
414    else if (blockSize == 64)
415        return new IDISA::IDISA_I64_Builder(mod, bitBlockType); 
416    return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
417}
418
419
420
421ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
422
423    InitializeNativeTarget();
424    InitializeNativeTargetAsmPrinter();
425    InitializeNativeTargetAsmParser();
426
427    std::string errMessage;
428    EngineBuilder builder(std::move(std::unique_ptr<Module>(m)));
429    builder.setErrorStr(&errMessage);
430    builder.setMCPU(sys::getHostCPUName());
431    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
432    switch (OptLevel) {
433        case '0': optLevel = CodeGenOpt::None; break;
434        case '1': optLevel = CodeGenOpt::Less; break;
435        case '2': optLevel = CodeGenOpt::Default; break;
436        case '3': optLevel = CodeGenOpt::Aggressive; break;
437        default: errs() << OptLevel << " is an invalid optimization level.\n";
438    }
439    builder.setOptLevel(optLevel);
440
441    if (!DisableAVX2 && (strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
442            std::vector<std::string> attrs;
443            attrs.push_back("avx2");
444            builder.setMAttrs(attrs);
445    }
446
447    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
448    ExecutionEngine * engine = builder.create();
449    if (engine == nullptr) {
450        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
451    }
452
453    return engine;
454}
455
456int total_count = 0;
457
458extern "C" {
459    void wrapped_report_match(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer, int filesize, char * filename) {
460        if(CountOnly){
461            total_count++;
462            return;
463        }
464
465        llvm::raw_os_ostream out(std::cout);
466        if (ShowFileNames) {
467            out << filename << ':';
468        }
469        if (ShowLineNumbers) {
470            out << lineNum << ":";
471        }
472
473        if ((buffer[line_start] == 0xA) && (line_start != line_end)) {
474            // The line "starts" on the LF of a CRLF.  Really the end of the last line.
475            line_start++;
476        }
477        if (line_end == filesize) {
478            // The match position is at end-of-file.   We have a final unterminated line.
479            out.write(&buffer[line_start], line_end - line_start);
480            if (NormalizeLineBreaks) {
481                out << '\n';  // terminate it
482            }
483            return;
484        }
485        unsigned char end_byte = (unsigned char)buffer[line_end]; 
486        if (NormalizeLineBreaks) {
487            if (end_byte == 0x85) {
488                // Line terminated with NEL, on the second byte.  Back up 1.
489                line_end--;
490            } else if (end_byte > 0xD) {
491                // Line terminated with PS or LS, on the third byte.  Back up 2.
492                line_end -= 2;
493            }
494            out.write(&buffer[line_start], line_end - line_start);
495            out << '\n';
496        }
497        else{   
498            if (end_byte == 0x0D) {
499                // Check for line_end on first byte of CRLF;  note that we don't
500                // want to access past the end of buffer.
501                if ((line_end + 1 < filesize) && (buffer[line_end + 1] == 0x0A)) {
502                    // Found CRLF; preserve both bytes.
503                    line_end++;
504                }
505            }
506            out.write(&buffer[line_start], line_end - line_start + 1);
507        }
508    }
509}
510
511
512void PrintTotalCount(){
513    if(CountOnly){
514        std::cout << total_count << std::endl;
515    }
516}
517
518re::CC * parsedCodePointSet;
519
520extern "C" {
521    void insert_codepoints(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer) {
522       re::codepoint_t c = 0;
523        ssize_t line_pos = line_start;
524        while (isxdigit(buffer[line_pos])) {
525            if (isdigit(buffer[line_pos])) {
526                c = (c << 4) | (buffer[line_pos] - '0');
527            }
528            else {
529                c = (c << 4) | (tolower(buffer[line_pos]) - 'a' + 10);
530            }
531            line_pos++;
532        }
533        assert(((line_pos - line_start) >= 4) && ((line_pos - line_start) <= 6)); // UCD format 4 to 6 hex digits.       
534        parsedCodePointSet->insert(c);
535    }
536}
537
538void setParsedCodePointSet(){
539    parsedCodePointSet = re::makeCC();
540}
541
542re::CC * getParsedCodePointSet(){
543    return parsedCodePointSet;
544}
545
546// extern "C" {
547//   void wrapped_print_register(char * regName, BitBlock bit_block) {
548//       print_register<BitBlock>(regName, bit_block);
549//   }
550// }
551
552void icgrep_Linking(Module * m, ExecutionEngine * e) {
553    Module::FunctionListType & fns = m->getFunctionList();
554    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
555        std::string fnName = it->getName().str();
556        if (fnName == "s2p_block") continue;
557        if (fnName == "process_block") continue;
558        if (fnName == "process_block_initialize_carries") continue;
559       
560        // if (fnName == "wrapped_print_register") {
561        //     e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
562        // }
563        if (fnName == "wrapped_report_match") {
564            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_match);
565        }
566        if (fnName == "insert_codepoints") {
567            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&insert_codepoints);
568        }
569#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
570        else {
571            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
572            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
573        }
574#endif
575    }
576}
Note: See TracBrowser for help on using the repository browser.