1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Regex compiler back-end: transforms IR into a CompiledRegex

use crate::bytesearch::{AsciiBitmap, ByteArraySet};
use crate::folds;
use crate::insn::{CompiledRegex, Insn, LoopFields, MAX_BYTE_SEQ_LENGTH, MAX_CHAR_SET_LENGTH};
use crate::ir;
use crate::ir::Node;
use crate::startpredicate;
use crate::types::{BracketContents, CaptureGroupID};
use std::convert::TryInto;

/// \return an anchor instruction for a given IR anchor.
fn make_anchor(anchor_type: ir::AnchorType) -> Insn {
    match anchor_type {
        ir::AnchorType::StartOfLine => Insn::StartOfLine,
        ir::AnchorType::EndOfLine => Insn::EndOfLine,
    }
}

/// Weirdly placed optimization.
/// If the given bracket can be represented as ASCII contents, return the
/// bitmap. Otherwise nothing.
fn bracket_as_ascii(bc: &BracketContents) -> Option<AsciiBitmap> {
    let mut result = AsciiBitmap::default();
    // We just assume that inverted brackets contain non-ASCII characters.
    if bc.invert {
        return None;
    }
    for r in bc.cps.intervals() {
        debug_assert!(r.first <= r.last);
        if r.last >= 128 {
            return None;
        }
        for bit in r.first..=r.last {
            result.set(bit as u8)
        }
    }
    Some(result)
}

/// Type which wraps up the context needed to emit a CompiledRegex.
struct Emitter {
    result: CompiledRegex,

    // Number of loops seen so far.
    next_loop_id: u32,
}

impl Emitter {
    /// Emit a ByteSet instruction.
    /// We awkwardly optimize it like so.
    fn make_byte_set_insn(&self, bytes: &[u8]) -> Insn {
        match bytes.len() {
            0 => Insn::JustFail,
            1 => Insn::ByteSeq1(bytes.try_into().unwrap()),
            2 => Insn::ByteSet2(ByteArraySet(bytes.try_into().unwrap())),
            3 => Insn::ByteSet3(ByteArraySet(bytes.try_into().unwrap())),
            4 => Insn::ByteSet4(ByteArraySet(bytes.try_into().unwrap())),
            _ => panic!("Byte set is too long"),
        }
    }

    /// Emit an instruction.
    /// Return the "instruction" as an index.
    fn emit_insn(&mut self, insn: Insn) {
        self.result.insns.push(insn);
    }

    /// Get an instruction at a given index.
    fn get_insn(&mut self, idx: u32) -> &mut Insn {
        &mut self.result.insns[idx as usize]
    }

    /// \return the offset of the next instruction emitted.
    fn next_offset(&self) -> u32 {
        self.result.insns.len() as u32
    }

    fn emit_insn_offset(&mut self, insn: Insn) -> u32 {
        let ret = self.next_offset();
        self.emit_insn(insn);
        ret
    }

    /// Emit instructions corresponding to a given node.
    /// TODO: make this non-recursive to avoid stack overflow.
    fn emit_node(&mut self, node: &Node) {
        match node {
            Node::Empty => {}
            Node::Goal => self.emit_insn(Insn::Goal),
            Node::Char { c, icase } => {
                let c = *c;
                if !*icase {
                    self.emit_insn(Insn::Char(c))
                } else {
                    std::debug_assert!(folds::fold(c) == c, "Char should be folded");
                    self.emit_insn(Insn::CharICase(c))
                }
            }
            Node::Cat(children) => {
                for nn in children {
                    self.emit_node(nn)
                }
            }
            Node::Alt(left, right) => {
                // Alternation is followed by the primary branch and has a jump to secondary
                // branch. After primary branch, jump to the continuation.
                let alt_insn = self.emit_insn_offset(Insn::Alt { secondary: 0 });
                self.emit_node(left);
                let jump_insn = self.emit_insn_offset(Insn::Jump { target: 0 });
                let right_branch = self.next_offset();
                self.emit_node(right);
                let exit = self.next_offset();

                // Fix up our jump targets.
                match self.get_insn(alt_insn) {
                    Insn::Alt { secondary } => *secondary = right_branch,
                    _ => panic!("Should be an Alt instruction"),
                }
                match self.get_insn(jump_insn) {
                    Insn::Jump { target } => *target = exit,
                    _ => panic!("Should be a Jump instruction"),
                }
            }
            Node::Bracket(contents) => {
                if let Some(ascii_contents) = bracket_as_ascii(contents) {
                    self.emit_insn(Insn::AsciiBracket(ascii_contents))
                } else {
                    self.emit_insn(Insn::Bracket(contents.clone()))
                }
            }
            Node::MatchAny => self.emit_insn(Insn::MatchAny),
            Node::MatchAnyExceptLineTerminator => {
                self.emit_insn(Insn::MatchAnyExceptLineTerminator)
            }
            Node::Anchor(anchor_type) => self.emit_insn(make_anchor(*anchor_type)),
            Node::Loop {
                loopee,
                quant,
                enclosed_groups,
            } => {
                let loop_id = self.next_loop_id;
                self.next_loop_id += 1;
                let loop_insn = self.emit_insn_offset(Insn::EnterLoop(LoopFields {
                    loop_id,
                    min_iters: quant.min,
                    max_iters: quant.max,
                    greedy: quant.greedy,
                    exit: 0,
                }));
                self.result.loops += 1;
                // Emit a sequence of ResetCaptureGroup for any contained groups.
                for gid in enclosed_groups.start..enclosed_groups.end {
                    self.emit_insn(Insn::ResetCaptureGroup(gid))
                }
                self.emit_node(loopee);
                self.emit_insn(Insn::LoopAgain { begin: loop_insn });
                // Fix up our loop exit.
                let exit = self.next_offset();
                match self.get_insn(loop_insn) {
                    Insn::EnterLoop(fields) => fields.exit = exit,
                    _ => panic!("Should be an EnterLoop instruction"),
                }
            }
            Node::Loop1CharBody { loopee, quant } => {
                self.emit_insn(Insn::Loop1CharBody {
                    min_iters: quant.min,
                    max_iters: quant.max,
                    greedy: quant.greedy,
                });
                self.emit_node(loopee);
            }
            Node::CaptureGroup(contents, group) => {
                let group = *group as CaptureGroupID;
                self.result.groups += 1;
                self.emit_insn(Insn::BeginCaptureGroup(group));
                self.emit_node(contents);
                self.emit_insn(Insn::EndCaptureGroup(group));
            }
            Node::LookaroundAssertion {
                negate,
                backwards,
                start_group,
                end_group,
                contents,
            } => {
                let lookaround = if *backwards {
                    self.emit_insn_offset(Insn::LookbehindInsn {
                        negate: *negate,
                        start_group: *start_group,
                        end_group: *end_group,
                        continuation: 0,
                    })
                } else {
                    self.emit_insn_offset(Insn::LookaheadInsn {
                        negate: *negate,
                        start_group: *start_group,
                        end_group: *end_group,
                        continuation: 0,
                    })
                };

                self.emit_node(contents);
                self.emit_insn(Insn::Goal);

                // Fix up the continuation.
                let next_insn = self.next_offset();
                match self.get_insn(lookaround) {
                    Insn::LookbehindInsn {
                        ref mut continuation,
                        ..
                    } => *continuation = next_insn,
                    Insn::LookaheadInsn {
                        ref mut continuation,
                        ..
                    } => *continuation = next_insn,
                    _ => panic!("Should be a Lookaround instruction"),
                }
            }
            Node::WordBoundary { invert } => self.emit_insn(Insn::WordBoundary { invert: *invert }),
            &Node::BackRef(group) => {
                debug_assert!(group >= 1, "Group should not be zero");
                // -1 because \1 matches the first capture group, which has index 0.
                self.emit_insn(Insn::BackRef(group - 1))
            }

            Node::ByteSet(bytes) => self.emit_insn(self.make_byte_set_insn(bytes)),

            Node::CharSet(chars) => {
                debug_assert!(!chars.is_empty() && chars.len() <= MAX_CHAR_SET_LENGTH);
                let mut arr = [chars[0]; MAX_CHAR_SET_LENGTH];
                arr[..chars.len()].copy_from_slice(chars.as_slice());
                self.emit_insn(Insn::CharSet(arr))
            }

            #[allow(clippy::assertions_on_constants)]
            Node::ByteSequence(bytes) => {
                assert!(
                    MAX_BYTE_SEQ_LENGTH == 16,
                    "Need to update our emitting logic"
                );
                for chunk in bytes.as_slice().chunks(MAX_BYTE_SEQ_LENGTH) {
                    let insn = match chunk.len() {
                        1 => Insn::ByteSeq1(chunk.try_into().unwrap()),
                        2 => Insn::ByteSeq2(chunk.try_into().unwrap()),
                        3 => Insn::ByteSeq3(chunk.try_into().unwrap()),
                        4 => Insn::ByteSeq4(chunk.try_into().unwrap()),
                        5 => Insn::ByteSeq5(chunk.try_into().unwrap()),
                        6 => Insn::ByteSeq6(chunk.try_into().unwrap()),
                        7 => Insn::ByteSeq7(chunk.try_into().unwrap()),
                        8 => Insn::ByteSeq8(chunk.try_into().unwrap()),
                        9 => Insn::ByteSeq9(chunk.try_into().unwrap()),
                        10 => Insn::ByteSeq10(chunk.try_into().unwrap()),
                        11 => Insn::ByteSeq11(chunk.try_into().unwrap()),
                        12 => Insn::ByteSeq12(chunk.try_into().unwrap()),
                        13 => Insn::ByteSeq13(chunk.try_into().unwrap()),
                        14 => Insn::ByteSeq14(chunk.try_into().unwrap()),
                        15 => Insn::ByteSeq15(chunk.try_into().unwrap()),
                        16 => Insn::ByteSeq16(chunk.try_into().unwrap()),
                        _ => panic!("Unexpected chunk size"),
                    };
                    self.emit_insn(insn)
                }
            }
        }
    }
}

/// Compile the given IR to a CompiledRegex.
pub fn emit(n: &ir::Regex) -> CompiledRegex {
    let mut emitter = Emitter {
        next_loop_id: 0,
        result: CompiledRegex {
            insns: Vec::new(),
            loops: 0,
            groups: 0,
            flags: n.flags,
            start_pred: startpredicate::predicate_for_re(n),
        },
    };
    emitter.emit_node(&n.node);
    emitter.result
}