source: icGREP/icgrep-devel/icgrep/casefold.cpp @ 5063

Last change on this file since 5063 was 5033, checked in by cameron, 3 years ago

Refactor: move grep-specific code out of toolchain

File size: 4.2 KB
RevLine 
[4988]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#include <sstream>
11
[5033]12#include <toolchain.h>
[4988]13#include <llvm/IR/Function.h>
14#include <llvm/IR/Module.h>
15#include <llvm/ExecutionEngine/ExecutionEngine.h>
16#include <llvm/Support/CommandLine.h>
17#include <re/re_cc.h>
18#include <cc/cc_compiler.h>
19#include <pablo/function.h>
20#include <IDISA/idisa_builder.h>
21#include <IDISA/idisa_target.h>
22#include <kernels/casefold_pipeline.h>
23
24#include <utf_encoding.h>
25
26// mmap system
27#include <boost/filesystem.hpp>
28#include <boost/iostreams/device/mapped_file.hpp>
29using namespace boost::iostreams;
30using namespace boost::filesystem;
31
32#include <fcntl.h>
33
34static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
35
36
37
38//
[4993]39//  Generate Pablo code for case folding.
[4988]40//
41
42pablo::PabloFunction * casefold2pablo(const Encoding encoding) {
43    pablo::PabloFunction * function = pablo::PabloFunction::Create("casefold_block", 8, 8);
44    cc::CC_Compiler cc_compiler(*function, encoding);
45    pablo::PabloBuilder pBuilder(cc_compiler.getBuilder().getPabloBlock(), cc_compiler.getBuilder());
46    const std::vector<pablo::Var *> basis_bits = cc_compiler.getBasisBits();
[4990]47   
[4993]48    pablo::PabloAST * alpha = cc_compiler.compileCC(re::makeCC(0x41, 0x5A));  // ASCII A-Z
[4990]49   
[4988]50    function->setResult(0, pBuilder.createAssign("b0", basis_bits[0]));
51    function->setResult(1, pBuilder.createAssign("b1", basis_bits[1]));
52    function->setResult(2, pBuilder.createAssign("b2", pBuilder.createXor(basis_bits[2], alpha)));
53    function->setResult(3, pBuilder.createAssign("b3", basis_bits[3]));
54    function->setResult(4, pBuilder.createAssign("b4", basis_bits[4]));
55    function->setResult(5, pBuilder.createAssign("b5", basis_bits[5]));
56    function->setResult(6, pBuilder.createAssign("b6", basis_bits[6]));
57    function->setResult(7, pBuilder.createAssign("b7", basis_bits[7]));
58    return function;
59}
60
61
62
63typedef void (*casefoldFunctionType)(char * byte_data, size_t filesize);
64
65casefoldFunctionType caseFoldCodeGen(void) {
66                           
67    Module * M = new Module("casefold", getGlobalContext());
68   
[5033]69    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
[4988]70
71    kernel::PipelineBuilder pipelineBuilder(M, idb);
72
73    Encoding encoding(Encoding::Type::UTF_8, 8);
74   
75    pablo::PabloFunction * function = casefold2pablo(encoding);
76   
77
78    pipelineBuilder.CreateKernels(function);
79
80    pipelineBuilder.ExecuteKernels();
81
82    //std::cerr << "ExecuteKernels(); done\n";
83    llvm::Function * main_IR = M->getFunction("Main");
84    ExecutionEngine * mEngine = JIT_to_ExecutionEngine(M);
85   
86    mEngine->finalizeObject();
87    //std::cerr << "finalizeObject(); done\n";
88
89    delete idb;
90
91    return reinterpret_cast<casefoldFunctionType>(mEngine->getPointerToFunction(main_IR));
92}
93
94void doCaseFold(casefoldFunctionType fn_ptr, const std::string & fileName) {
95    std::string mFileName = fileName;
96    size_t mFileSize;
97    char * mFileBuffer;
98   
99    const path file(mFileName);
100    if (exists(file)) {
101        if (is_directory(file)) {
102            return;
103        }
104    } else {
105        std::cerr << "Error: cannot open " << mFileName << " for processing. Skipped.\n";
106        return;
107    }
108   
109    mFileSize = file_size(file);
110    mapped_file mFile;
111    if (mFileSize == 0) {
112        mFileBuffer = nullptr;
113    }
114    else {
115        try {
116            mFile.open(mFileName, mapped_file::priv, mFileSize, 0);
117        } catch (std::ios_base::failure e) {
118            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
119            return;
120        }
121        mFileBuffer = mFile.data();
122    }
123    //std::cerr << "mFileSize =" << mFileSize << "\n";
124    //std::cerr << "fn_ptr =" << std::hex << reinterpret_cast<intptr_t>(fn_ptr) << "\n";
125
126    fn_ptr(mFileBuffer, mFileSize);
127
128    mFile.close();
129   
130}
131
132
133int main(int argc, char *argv[]) {
134    cl::ParseCommandLineOptions(argc, argv);
135
136    casefoldFunctionType fn_ptr = caseFoldCodeGen();
137
138    for (unsigned i = 0; i != inputFiles.size(); ++i) {
139        doCaseFold(fn_ptr, inputFiles[i]);
140    }
141
142    return 0;
143}
144
145                       
Note: See TracBrowser for help on using the repository browser.