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

Last change on this file since 5435 was 5435, checked in by nmedfort, 2 years ago

Continued refactoring work.

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
15using namespace cc;
16using namespace kernel;
17using namespace pablo;
18using namespace re;
19using namespace llvm;
20
21#define UNICODE_LINE_BREAK (!AlgorithmOptionIsSet(DisableUnicodeLineBreak))
22
23LineBreakKernelBuilder::LineBreakKernelBuilder(const std::unique_ptr<IDISA::IDISA_Builder> & b, unsigned basisBitsCount)
24: PabloKernel(b, "lb", {Binding{b->getStreamSetTy(basisBitsCount), "basis"}}, {Binding{b->getStreamSetTy(1), "linebreak", Add1()}}) {
25
26}
27
28void LineBreakKernelBuilder::prepareKernel() {
29
30    CC_Compiler ccc(this, getInput(0));
31    auto & pb = ccc.getBuilder();
32
33    PabloAST * LineBreak = nullptr;
34    PabloAST * LF = ccc.compileCC("LF", makeCC(0x0A), pb);
35    PabloAST * CR = ccc.compileCC(makeCC(0x0D));
36    PabloAST * LF_VT_FF_CR = ccc.compileCC(makeCC(0x0A, 0x0D));
37
38    Zeroes * const zero = pb.createZeroes();
39    Var * crlf = pb.createVar("crlf", zero);
40    PabloBuilder crb = PabloBuilder::Create(pb);
41#ifndef USE_LOOKAHEAD_CRLF
42    PabloAST * cr1 = crb.createAdvance(CR, 1, "cr1");
43    crb.createAssign(crlf, crb.createAnd(cr1, LF));
44#else
45    PabloAST * lookaheadLF = crb.createLookahead(LF, 1, "lookaheadLF");
46    crb.createAssign(crlf, crb.createAnd(CR, lookaheadLF));
47#endif
48    pb.createIf(CR, crb);
49
50    Var * NEL_LS_PS = pb.createVar("NEL_LS_PS", zero);
51
52    PabloAST * u8pfx = ccc.compileCC(makeCC(0xC0, 0xFF));
53    PabloBuilder it = PabloBuilder::Create(pb);
54    pb.createIf(u8pfx, it);
55    PabloAST * u8pfx2 = ccc.compileCC(makeCC(0xC2, 0xDF), it);
56    PabloAST * u8pfx3 = ccc.compileCC(makeCC(0xE0, 0xEF), it);
57
58    //
59    // Two-byte sequences
60    Var * NEL = it.createVar("NEL", zero);
61    PabloBuilder it2 = PabloBuilder::Create(it);
62    it2.createAssign(NEL, it2.createAnd(it2.createAdvance(ccc.compileCC(makeCC(0xC2), it2), 1), ccc.compileCC(makeCC(0x85), it2)));
63    it.createIf(u8pfx2, it2);
64
65    //
66    // Three-byte sequences
67    Var * LS_PS = it.createVar("LS_PS", zero);
68    PabloBuilder it3 = PabloBuilder::Create(it);
69    it.createIf(u8pfx3, it3);
70    PabloAST * E2_80 = it3.createAnd(it3.createAdvance(ccc.compileCC(makeCC(0xE2), it3), 1), ccc.compileCC(makeCC(0x80), it3));
71    it3.createAssign(LS_PS, it3.createAnd(it3.createAdvance(E2_80, 1), ccc.compileCC(makeCC(0xA8,0xA9), it3)));
72    it.createAssign(NEL_LS_PS, it.createOr(NEL, LS_PS));
73
74    PabloAST * LB_chars = pb.createOr(LF_VT_FF_CR, NEL_LS_PS);
75    PabloAST * UnicodeLineBreak = pb.createAnd(LB_chars, pb.createNot(crlf));  // count the CR, but not CRLF
76
77    PabloAST * lb = UNICODE_LINE_BREAK ? UnicodeLineBreak : LF;
78    PabloAST * unterminatedLineAtEOF = pb.createAtEOF(pb.createAdvance(pb.createNot(LB_chars), 1));
79    LineBreak = pb.createOr(lb, unterminatedLineAtEOF);
80    PabloAST * const r = pb.createExtract(getOutput(0), pb.getInteger(0));
81    pb.createAssign(r, LineBreak);
82#ifdef USE_LOOKAHEAD_CRLF
83    setLookAhead(1);
84#endif
85
86    PabloKernel::prepareKernel();
87}
Note: See TracBrowser for help on using the repository browser.