source: icGREP/icgrep-devel/icgrep/kernels/linebreak_kernel.cpp @ 5769

Last change on this file since 5769 was 5755, checked in by nmedfort, 18 months ago

Bug fixes and simplified MultiBlockKernel? logic

File size: 3.2 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#include "linebreak_kernel.h"
7#include <re/re_cc.h>
8#include <re/re_toolchain.h>
9#include <pablo/pe_ones.h>          // for Ones
10#include <pablo/pe_var.h>           // for Var
11#include <pablo/pe_zeroes.h>        // for Zeroes
12#include <cc/cc_compiler.h>
13#include <pablo/builder.hpp>
14#include <kernels/kernel_builder.h>
15
16using namespace cc;
17using namespace kernel;
18using namespace pablo;
19using namespace re;
20using namespace llvm;
21
22LineBreakKernelBuilder::LineBreakKernelBuilder(const std::unique_ptr<kernel::KernelBuilder> & b, unsigned basisBitsCount)
23: PabloKernel(b, "lb",
24    {Binding{b->getStreamSetTy(basisBitsCount), "basis", FixedRate(), Principal()}},
25    {Binding{b->getStreamSetTy(1), "linebreak", FixedRate(), Add1()}}) {
26
27}
28
29void LineBreakKernelBuilder::generatePabloMethod() {
30
31    CC_Compiler ccc(this, getInput(0));
32    auto & pb = ccc.getBuilder();
33
34    PabloAST * LineBreak = nullptr;
35    PabloAST * LF = ccc.compileCC("LF", makeCC(0x0A), pb);
36    PabloAST * CR = ccc.compileCC(makeCC(0x0D));
37    PabloAST * LF_VT_FF_CR = ccc.compileCC(makeCC(0x0A, 0x0D));
38
39    Zeroes * const zero = pb.createZeroes();
40    Var * crlf = pb.createVar("crlf", zero);
41    PabloBuilder crb = PabloBuilder::Create(pb);
42#ifndef USE_LOOKAHEAD_CRLF
43    PabloAST * cr1 = crb.createAdvance(CR, 1, "cr1");
44    crb.createAssign(crlf, crb.createAnd(cr1, LF));
45#else
46    PabloAST * lookaheadLF = crb.createLookahead(LF, 1, "lookaheadLF");
47    crb.createAssign(crlf, crb.createAnd(CR, lookaheadLF));
48#endif
49    pb.createIf(CR, crb);
50
51    Var * NEL_LS_PS = pb.createVar("NEL_LS_PS", zero);
52
53    PabloAST * u8pfx = ccc.compileCC(makeCC(0xC0, 0xFF));
54    PabloBuilder it = PabloBuilder::Create(pb);
55    pb.createIf(u8pfx, it);
56    PabloAST * u8pfx2 = ccc.compileCC(makeCC(0xC2, 0xDF), it);
57    PabloAST * u8pfx3 = ccc.compileCC(makeCC(0xE0, 0xEF), it);
58
59    //
60    // Two-byte sequences
61    Var * NEL = it.createVar("NEL", zero);
62    PabloBuilder it2 = PabloBuilder::Create(it);
63    it2.createAssign(NEL, it2.createAnd(it2.createAdvance(ccc.compileCC(makeCC(0xC2), it2), 1), ccc.compileCC(makeCC(0x85), it2)));
64    it.createIf(u8pfx2, it2);
65
66    //
67    // Three-byte sequences
68    Var * LS_PS = it.createVar("LS_PS", zero);
69    PabloBuilder it3 = PabloBuilder::Create(it);
70    it.createIf(u8pfx3, it3);
71    PabloAST * E2_80 = it3.createAnd(it3.createAdvance(ccc.compileCC(makeCC(0xE2), it3), 1), ccc.compileCC(makeCC(0x80), it3));
72    it3.createAssign(LS_PS, it3.createAnd(it3.createAdvance(E2_80, 1), ccc.compileCC(makeCC(0xA8,0xA9), it3)));
73    it.createAssign(NEL_LS_PS, it.createOr(NEL, LS_PS));
74
75    PabloAST * LB_chars = pb.createOr(LF_VT_FF_CR, NEL_LS_PS);
76    PabloAST * lb = nullptr;
77    if (AlgorithmOptionIsSet(DisableUnicodeLineBreak)) {
78        lb = LF;
79    } else {
80        lb = pb.createAnd(LB_chars, pb.createNot(crlf));  // count the CR, but not CRLF
81    }
82
83    PabloAST * unterminatedLineAtEOF = pb.createAtEOF(pb.createAdvance(pb.createNot(LB_chars), 1));
84    LineBreak = pb.createOr(lb, unterminatedLineAtEOF);
85    PabloAST * const r = pb.createExtract(getOutput(0), pb.getInteger(0));
86    pb.createAssign(r, LineBreak);
87#ifdef USE_LOOKAHEAD_CRLF
88    setLookAhead(1);
89#endif
90}
Note: See TracBrowser for help on using the repository browser.