source: icGREP/icgrep-devel/icgrep/u8u16.cpp @ 5033

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

Refactor: move grep-specific code out of toolchain

File size: 13.0 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#include <sstream>
11
12#include <llvm/IR/Function.h>
13#include <llvm/IR/Module.h>
14#include <llvm/ExecutionEngine/ExecutionEngine.h>
15#include <llvm/ExecutionEngine/MCJIT.h>
16#include <llvm/IRReader/IRReader.h>
17#include <llvm/IR/Verifier.h>
18#include <llvm/Support/Debug.h>
19
20#include <llvm/Support/CommandLine.h>
21
22#include <toolchain.h>
23#include <re/re_cc.h>
24#include <cc/cc_compiler.h>
25#include <pablo/function.h>
26#include <IDISA/idisa_builder.h>
27#include <IDISA/idisa_target.h>
28#include <kernels/u8u16_pipeline.h>
29
30#include <utf_encoding.h>
31
32// mmap system
33#include <boost/filesystem.hpp>
34#include <boost/iostreams/device/mapped_file.hpp>
35using namespace boost::iostreams;
36using namespace boost::filesystem;
37
38#include <fcntl.h>
39
40static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
41
42
43
44//
45//
46//
47namespace pablo {
48
49PabloFunction * u8u16_pablo(const Encoding encoding) {
50    //  input: 8 basis bit streams
51    //  output: 16 u8-indexed streams, + delmask stream + error stream
52    PabloFunction * function = PabloFunction::Create("u8u16", 8, 18);
53    cc::CC_Compiler ccc(*function, encoding);
54   
55    PabloBuilder pBuilder(ccc.getBuilder().getPabloBlock(), ccc.getBuilder());
56    const std::vector<Var *> u8_bits = ccc.getBasisBits();
57    // Outputs
58    Assign * u16_hi[8];
59    Assign * u16_lo[8];
60    Assign * delmask;
61    Assign * error_mask;
62   
63    // The logic for processing non-ASCII bytes is to be embedded within an if-hierarchy.
64    PabloAST * nonASCII = ccc.compileCC(re::makeCC(0x80, 0xFF));
65   
66    // Builder for the if statement handling all non-ASCII logic
67    PabloBuilder nAb = PabloBuilder::Create(pBuilder);
68    // Bits 3 through 7 of a 2-byte prefix are data bits, needed to
69    // produce the UTF-16 code unit data ...,
70    PabloAST * bit3a1 = nAb.createAdvance(u8_bits[3], 1);
71    PabloAST * bit4a1 = nAb.createAdvance(u8_bits[4], 1);
72    PabloAST * bit5a1 = nAb.createAdvance(u8_bits[5], 1);
73    PabloAST * bit6a1 = nAb.createAdvance(u8_bits[6], 1);
74    PabloAST * bit7a1 = nAb.createAdvance(u8_bits[7], 1);
75   
76    // Entry condition for 3 or 4 byte sequences: we have a prefix byte in the range 0xE0-0xFF.
77    PabloAST * pfx34 = ccc.compileCC(re::makeCC(0xE0, 0xFF), nAb);
78    // Builder for the if statement handling all logic for 3- and 4-byte sequences.
79    PabloBuilder p34b = PabloBuilder::Create(nAb);
80    // Bits 4 through 7 of a 3-byte prefix are data bits.  They must be moved
81    // to the final position of the 3-byte sequence.
82    PabloAST * bit2a1 = p34b.createAdvance(u8_bits[2], 1);
83    PabloAST * bit4a2 = p34b.createAdvance(bit4a1, 1);
84    PabloAST * bit5a2 = p34b.createAdvance(bit5a1, 1);
85    PabloAST * bit6a2 = p34b.createAdvance(bit6a1, 1);
86    PabloAST * bit7a2 = p34b.createAdvance(bit7a1, 1);
87    //
88    // Logic for 4-byte UTF-8 sequences
89    //
90    // Entry condition  or 4 byte sequences: we have a prefix byte in the range 0xF0-0xFF.
91    PabloAST * pfx4 = ccc.compileCC(re::makeCC(0xF0, 0xFF), p34b);
92    // Builder for the if statement handling all logic for 4-byte sequences only.
93    PabloBuilder p4b = PabloBuilder::Create(p34b);
94    // Illegal 4-byte sequences
95    PabloAST * F0 = ccc.compileCC(re::makeCC(0xF0), p4b);
96    PabloAST * F4 = ccc.compileCC(re::makeCC(0xF4), p4b);
97    PabloAST * F0_err = p4b.createAnd(p4b.createAdvance(F0, 1), ccc.compileCC(re::makeCC(0x80, 0x8F), p4b));
98    PabloAST * F4_err = p4b.createAnd(p4b.createAdvance(F4, 1), ccc.compileCC(re::makeCC(0x90, 0xBF), p4b));
99    PabloAST * F5_FF = ccc.compileCC(re::makeCC(0xF5, 0xFF), p4b);
100    Assign * FX_err = p4b.createAssign("FX_err", p4b.createOr(F5_FF, p4b.createOr(F0_err, F4_err)));
101    //
102    // 4-byte prefixes have a scope that extends over the next 3 bytes.
103    Assign * u8scope42 = p4b.createAssign("u8scope42", p4b.createAdvance(pfx4, 1));
104    Assign * u8scope43 = p4b.createAssign("u8scope43", p4b.createAdvance(u8scope42, 1));
105    Assign * u8scope44 = p4b.createAssign("u8scope44", p4b.createAdvance(u8scope43, 1));
106    //
107   
108    //  From the 4-byte sequence 11110abc 10defghi 10jklmno 10pqrstu,
109    //  we must calculate the value abcde - 1 to produce the bit values
110    //  for u16_hi6, hi7, lo0, lo1 at the scope43 position.
111    Assign * s43_lo1 = p4b.createAssign("scope43_lo1", p4b.createAnd(u8scope43, p4b.createNot(bit3a1)));           // e - 1
112    Assign * s43_lo0 = p4b.createAssign("scope43_lo0", p4b.createAnd(u8scope43, p4b.createXor(bit2a1, s43_lo1)));  // d - borrow
113    PabloAST * brw1 = p4b.createAnd(s43_lo1, p4b.createNot(bit2a1));
114    Assign * s43_hi7 = p4b.createAssign("scope43_hi7", p4b.createAnd(u8scope43, p4b.createXor(bit7a2, brw1)));     // c - borrow
115    PabloAST * brw2 = p4b.createAnd(brw1, p4b.createNot(bit7a2));
116    Assign * s43_hi6 = p4b.createAssign("scope43_hi6", p4b.createAnd(u8scope43, p4b.createXor(bit6a2, brw2)));     // b - borrow
117    //
118    Assign * s43_lo2 = p4b.createAssign("scope43_lo2", p4b.createAnd(u8scope43, bit4a1));
119    Assign * s43_lo3 = p4b.createAssign("scope43_lo3", p4b.createAnd(u8scope43, bit5a1));
120    Assign * s43_lo4 = p4b.createAssign("scope43_lo4", p4b.createAnd(u8scope43, bit6a1));
121    Assign * s43_lo5 = p4b.createAssign("scope43_lo5", p4b.createAnd(u8scope43, bit7a1));
122    Assign * s43_lo6 = p4b.createAssign("scope43_lo6", p4b.createAnd(u8scope43, u8_bits[2]));
123    Assign * s43_lo7 = p4b.createAssign("scope43_lo7", p4b.createAnd(u8scope43, u8_bits[3]));
124    //
125    //
126    p34b.createIf(pfx4,
127                  {FX_err, u8scope42, u8scope43, u8scope44, s43_hi6, s43_hi7,
128                   s43_lo0, s43_lo1, s43_lo2, s43_lo3, s43_lo4, s43_lo5, s43_lo6, s43_lo7},
129                   p4b);
130    //
131    // Combined logic for 3 and 4 byte sequences
132    //
133    PabloAST * pfx3 = ccc.compileCC(re::makeCC(0xE0, 0xEF), p34b);
134    Assign * u8scope32 = p34b.createAssign("u8scope32", p34b.createAdvance(pfx3, 1));
135    Assign * u8scope33 = p34b.createAssign("u8scope33", p34b.createAdvance(u8scope32, 1));
136
137    // Illegal 3-byte sequences
138    PabloAST * E0 = ccc.compileCC(re::makeCC(0xE0), p34b);
139    PabloAST * ED = ccc.compileCC(re::makeCC(0xED), p34b);
140    PabloAST * E0_err = p34b.createAnd(p34b.createAdvance(E0, 1), ccc.compileCC(re::makeCC(0x80, 0x9F), p34b));
141    PabloAST * ED_err = p34b.createAnd(p34b.createAdvance(ED, 1), ccc.compileCC(re::makeCC(0xA0, 0xBF), p34b));
142    Assign * EX_FX_err = p34b.createAssign("EX_FX_err", p34b.createOr(p34b.createOr(E0_err, ED_err), FX_err));
143    // Two surrogate UTF-16 units are computed at the 3rd and 4th positions of 4-byte sequences.
144    PabloAST * surrogate = p34b.createOr(u8scope43, u8scope44);
145   
146    Assign * p34del = p34b.createAssign("p34del", p34b.createOr(u8scope32, u8scope42));
147
148
149    // The high 5 bits of the UTF-16 code unit are only nonzero for 3 and 4-byte
150    // UTF-8 sequences.
151    u16_hi[0] = p34b.createAssign("u16_hi0", p34b.createOr(p34b.createAnd(u8scope33, bit4a2), surrogate));
152    u16_hi[1] = p34b.createAssign("u16_hi1", p34b.createOr(p34b.createAnd(u8scope33, bit5a2), surrogate));
153    u16_hi[2] = p34b.createAssign("u16_hi2", p34b.createAnd(u8scope33, bit6a2));
154    u16_hi[3] = p34b.createAssign("u16_hi3", p34b.createOr(p34b.createAnd(u8scope33, bit7a2), surrogate));
155    u16_hi[4] = p34b.createAssign("u16_hi4", p34b.createOr(p34b.createAnd(u8scope33, bit2a1), surrogate));
156   
157    //
158    nAb.createIf(pfx34, 
159                 {u8scope33, EX_FX_err, p34del, 
160                  u16_hi[0], u16_hi[1], u16_hi[2], u16_hi[3], u16_hi[4], u8scope44, s43_hi6, s43_hi7,
161                  s43_lo0, s43_lo1, s43_lo2, s43_lo3, s43_lo4, s43_lo5, s43_lo6, s43_lo7},
162                 p34b);
163    //
164    // Combined logic for 2, 3 and 4 byte sequences
165    //
166    PabloAST * pfx2 = ccc.compileCC(re::makeCC(0xC0, 0xDF), nAb);
167    PabloAST * u8scope22 = nAb.createAdvance(pfx2, 1);
168    Assign * u8lastscope = nAb.createAssign("u8lastscope", nAb.createOr(u8scope22, nAb.createOr(u8scope33, u8scope44)));
169    PabloAST * u8anyscope = nAb.createOr(u8lastscope, p34del);
170
171    PabloAST * C0_C1_err = ccc.compileCC(re::makeCC(0xC0, 0xC1), nAb);
172    PabloAST * scope_suffix_mismatch = nAb.createXor(u8anyscope, ccc.compileCC(re::makeCC(0x80, 0xBF), nAb));
173    error_mask = nAb.createAssign("errormask", nAb.createOr(scope_suffix_mismatch, nAb.createOr(C0_C1_err, EX_FX_err)));
174    delmask = nAb.createAssign("delmask", nAb.createOr(p34del, ccc.compileCC(re::makeCC(0xC0, 0xFF), nAb)));
175   
176    // The low 3 bits of the high byte of the UTF-16 code unit as well as the high bit of the
177    // low byte are only nonzero for 2, 3 and 4 byte sequences.
178    u16_hi[5] = nAb.createAssign("u16_hi5", nAb.createOr(nAb.createAnd(u8lastscope, bit3a1), u8scope44));
179    u16_hi[6] = nAb.createAssign("u16_hi6", nAb.createOr(nAb.createAnd(u8lastscope, bit4a1), s43_hi6));
180    u16_hi[7] = nAb.createAssign("u16_hi7", nAb.createOr(nAb.createAnd(u8lastscope, bit5a1), s43_hi7));
181    u16_lo[0] = nAb.createAssign("u16_lo0", nAb.createOr(nAb.createAnd(u8lastscope, bit6a1), s43_lo0));
182    Assign * p234_lo1 = nAb.createAssign("p234_lo1", nAb.createOr(nAb.createAnd(u8lastscope, bit7a1), s43_lo1));
183
184    pBuilder.createIf(nonASCII, 
185                      {error_mask, delmask, u8lastscope,
186                       u16_hi[0], u16_hi[1], u16_hi[2], u16_hi[3], u16_hi[4], u16_hi[5], u16_hi[6], u16_hi[7],
187                       u16_lo[0], p234_lo1, s43_lo2, s43_lo3, s43_lo4, s43_lo5, s43_lo6, s43_lo7},
188                      nAb);
189    //
190    //
191    PabloAST * ASCII = ccc.compileCC(re::makeCC(0x0, 0x7F));
192    PabloAST * last_byte = pBuilder.createOr(ASCII, u8lastscope);
193    u16_lo[1] = pBuilder.createAssign("u16_lo1", pBuilder.createOr(pBuilder.createAnd(ASCII, u8_bits[1]), p234_lo1));
194    u16_lo[2] = pBuilder.createAssign("u16_lo2", pBuilder.createOr(pBuilder.createAnd(last_byte, u8_bits[2]), s43_lo2));
195    u16_lo[3] = pBuilder.createAssign("u16_lo3", pBuilder.createOr(pBuilder.createAnd(last_byte, u8_bits[3]), s43_lo3));
196    u16_lo[4] = pBuilder.createAssign("u16_lo4", pBuilder.createOr(pBuilder.createAnd(last_byte, u8_bits[4]), s43_lo4));
197    u16_lo[5] = pBuilder.createAssign("u16_lo5", pBuilder.createOr(pBuilder.createAnd(last_byte, u8_bits[5]), s43_lo5));
198    u16_lo[6] = pBuilder.createAssign("u16_lo6", pBuilder.createOr(pBuilder.createAnd(last_byte, u8_bits[6]), s43_lo6));
199    u16_lo[7] = pBuilder.createAssign("u16_lo7", pBuilder.createOr(pBuilder.createAnd(last_byte, u8_bits[7]), s43_lo7));
200   
201    for (unsigned i = 0; i < 8; i++) {
202        function->setResult(i, pBuilder.createAssign("u16_hi" + std::to_string(i), u16_hi[i]));
203        function->setResult(i+8, pBuilder.createAssign("u16_lo" + std::to_string(i), u16_lo[i]));
204    }
205    function->setResult(16, pBuilder.createAssign("delbits", delmask));
206    function->setResult(17, pBuilder.createAssign("errors", error_mask));
207
208    return function;
209}
210}
211
212
213typedef void (*u8u16FunctionType)(char * byte_data, size_t filesize);
214
215static ExecutionEngine * u8u16Engine = nullptr;
216
217u8u16FunctionType u8u16CodeGen(void) {
218                           
219    Module * M = new Module("u8u16", getGlobalContext());
220   
221    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
222
223    kernel::PipelineBuilder pipelineBuilder(M, idb);
224
225    Encoding encoding(Encoding::Type::UTF_8, 8);
226   
227    pablo::PabloFunction * function = pablo::u8u16_pablo(encoding);
228   
229
230    pipelineBuilder.CreateKernels(function);
231
232    llvm::Function * main_IR = pipelineBuilder.ExecuteKernels();
233   
234    verifyModule(*M, &dbgs());
235    //std::cerr << "ExecuteKernels(); done\n";
236    u8u16Engine = JIT_to_ExecutionEngine(M);
237   
238    u8u16Engine->finalizeObject();
239    //std::cerr << "finalizeObject(); done\n";
240
241    delete idb;
242    return reinterpret_cast<u8u16FunctionType>(u8u16Engine->getPointerToFunction(main_IR));
243}
244
245void u8u16(u8u16FunctionType fn_ptr, const std::string & fileName) {
246    std::string mFileName = fileName;
247    size_t mFileSize;
248    char * mFileBuffer;
249   
250    const path file(mFileName);
251    if (exists(file)) {
252        if (is_directory(file)) {
253            return;
254        }
255    } else {
256        std::cerr << "Error: cannot open " << mFileName << " for processing. Skipped.\n";
257        return;
258    }
259   
260    mFileSize = file_size(file);
261    mapped_file_source mFile;
262    if (mFileSize == 0) {
263        mFileBuffer = nullptr;
264    }
265    else {
266        try {
267            mFile.open(mFileName);
268        } catch (std::exception &e) {
269            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
270            return;
271        }
272        mFileBuffer = const_cast<char *>(mFile.data());
273    }
274    //std::cerr << "mFileSize =" << mFileSize << "\n";
275    //std::cerr << "fn_ptr =" << std::hex << reinterpret_cast<intptr_t>(fn_ptr) << "\n";
276
277    fn_ptr(mFileBuffer, mFileSize);
278
279    mFile.close();
280   
281}
282
283
284int main(int argc, char *argv[]) {
285    cl::ParseCommandLineOptions(argc, argv);
286
287    u8u16FunctionType fn_ptr = u8u16CodeGen();
288
289    for (unsigned i = 0; i != inputFiles.size(); ++i) {
290        u8u16(fn_ptr, inputFiles[i]);
291    }
292
293    delete u8u16Engine;
294
295    return 0;
296}
297
298                       
Note: See TracBrowser for help on using the repository browser.