rsleigh 0.4.1

SLEIGH (.slaspec) parser and Rust decoder/P-code emitter codegen — Ghidra-compatible disassembly in pure Rust
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
use std::collections::HashMap;

use crate::semantic::disassembly::Assertation;
use crate::semantic::inner::{GlobalScope, Sleigh, SolverStatus};
use crate::semantic::pattern::{Block as FinalBlock, ConstraintValue, Ellipsis, PatternLen};
use crate::semantic::{TableId, TokenFieldId, TokenId};
use crate::{syntax, PatternError, Span};

use super::pattern_len::ConstructorPatternLen;
use super::{
    DisassemblyBuilder, FindValues, Pattern, ProducedTable, ProducedTokenField, Verification,
};

#[derive(Clone, Debug)]
pub struct Block {
    pub base: BlockBase,
    pub phase: BlockPhase,
}

#[derive(Clone, Debug)]
pub enum BlockPhase {
    Phase1(BlockPhase1),
    Phase2(BlockPhase2),
}
#[derive(Clone, Debug)]
pub struct BlockBase {
    /// Op And, Or
    pub op: syntax::block::pattern::Op,

    //TODO remove this? Blocks could be a concat of pattern and with_block, so
    //there is no single location
    /// Location of the block in the source code
    pub location: Span,

    /// token that this block produces, and the number of times it produces
    pub tokens: HashMap<TokenId, usize>,
    /// fields produced, implicitly or explicity, localy or in sub_pattern
    pub token_fields: HashMap<TokenFieldId, ProducedTokenField>,
    /// produced tables
    pub tables: HashMap<TableId, ProducedTable>,

    /// verification in the order they are defined in code
    pub verifications: Vec<Verification>,

    /// disassembly assertations before the block is match
    pub pre: Vec<Assertation>,
    /// disassembly after the block is match
    pub pos: Vec<Assertation>,
}
#[derive(Clone, Debug, Default)]
pub struct BlockPhase1 {
    ///len that will be gradually be calculated
    pub len: Option<ConstructorPatternLen>,
}
#[derive(Clone, Debug)]
pub struct BlockPhase2 {
    /// len that was calculated
    pub len: PatternLen,

    /// number of possible variants for blocks prior to this one
    pub variants_prior: usize,
    /// number of variants this block produce
    pub variants_number: usize,
    //// base, not including sub_pattern with more then one variant
    //pub base: Vec<BitConstraint>,
    /// used on the creation, true if in use, so only modify on false
    pub variants_lock: bool,
}

impl Block {
    pub fn new(
        sleigh: &Sleigh,
        input: syntax::block::pattern::Block,
        this_table: TableId,
    ) -> Result<Self, Box<PatternError>> {
        let base = BlockBase::new(sleigh, input, this_table)?;
        // no verification the block is len 0
        let phase = BlockPhase::Phase1(BlockPhase1::default());
        Ok(Self { base, phase })
    }
    pub fn len(&self) -> Option<ConstructorPatternLen> {
        match &self.phase {
            BlockPhase::Phase1(ph1) => ph1.len,
            BlockPhase::Phase2(ph2) => Some(ConstructorPatternLen::Basic(ph2.len)),
        }
    }
    pub fn solve<T: SolverStatus>(
        &mut self,
        sleigh: &Sleigh,
        solved: &mut T,
    ) -> Result<bool, Box<PatternError>> {
        match &mut self.phase {
            BlockPhase::Phase1(ph1) => ph1.solve(sleigh, solved, &mut self.base),
            BlockPhase::Phase2(_) => Ok(true),
        }
    }
    pub fn build_phase2(&mut self, variants_prior: usize) -> &mut BlockPhase2 {
        //convert all verifications into phase2
        let len: PatternLen = match &self.phase {
            BlockPhase::Phase1(ph1) => ph1.len.unwrap().basic().unwrap(),
            _ => unreachable!(),
        };
        let new_phase = BlockPhase::Phase2(BlockPhase2::new(&mut self.base, len, variants_prior));
        self.phase = new_phase;
        let BlockPhase::Phase2(ph2) = &mut self.phase else {
            unreachable!()
        };
        ph2
    }
    pub fn phase2(&self) -> Option<&BlockPhase2> {
        match &self.phase {
            BlockPhase::Phase2(ph2) => Some(ph2),
            BlockPhase::Phase1(_ph1) => None,
        }
    }
    pub fn convert(self) -> FinalBlock {
        let Block {
            base,
            phase: BlockPhase::Phase2(ph2),
        } = self
        else {
            unreachable!()
        };
        let token_fields = base.token_fields.into_values().collect();
        let tables = base.tables.into_values().collect();
        let verifications = base
            .verifications
            .into_iter()
            .map(|k| k.convert())
            .collect();
        match base.op {
            syntax::block::pattern::Op::And => FinalBlock::And {
                len: ph2.len,
                token_fields,
                tables,
                verifications,
                pre: base.pre.into(),
                pos: base.pos.into(),
                variants_prior: ph2.variants_prior,
                variants_number: ph2.variants_number,
            },
            syntax::block::pattern::Op::Or => FinalBlock::Or {
                len: ph2.len,
                token_fields,
                tables,
                branches: verifications,
                pos: base.pre.into_iter().chain(base.pos).collect(),
                variants_prior: ph2.variants_prior,
                variants_number: ph2.variants_number,
            },
        }
    }
}

impl BlockBase {
    pub fn new(
        sleigh: &Sleigh,
        input: syntax::block::pattern::Block,
        this_table: TableId,
    ) -> Result<Self, Box<PatternError>> {
        let syntax::block::pattern::Block {
            src,
            first,
            elements,
        } = input;

        // Determine the dominant operation at this level.
        // If operations are mixed (& and |), default to AND and treat
        // OR-connected elements as subpatterns. This handles VFP/NEON
        // constructor patterns like (ARM_pattern | Thumb_pattern) & operands.
        let op = match &elements[..] {
            [] => syntax::block::pattern::Op::And,
            [(first_op, _), rest @ ..] => {
                if rest.iter().all(|(op, _)| first_op == op) {
                    *first_op
                } else {
                    // Mixed operations: default to AND (most common case is
                    // OR variants combined with AND operands)
                    syntax::block::pattern::Op::And
                }
            }
        };

        let all_elements = [first]
            .into_iter()
            .chain(elements.into_iter().map(|(_op, element)| element));

        match op {
            syntax::block::pattern::Op::Or => Self::new_or(sleigh, src, all_elements, this_table),
            syntax::block::pattern::Op::And => Self::new_and(sleigh, src, all_elements, this_table),
        }
    }
    fn new_or(
        sleigh: &Sleigh,
        location: Span,
        elements: impl Iterator<Item = syntax::block::pattern::Element>,
        this_table: TableId,
    ) -> Result<Self, Box<PatternError>> {
        //convert the verifications and generate the tokens/token_fields that
        //all branches produces.
        let mut tokens: Option<HashMap<TokenId, usize>> = None;
        let mut token_fields: Option<HashMap<_, _>> = None;
        let branches = elements
            //convert into Verifications
            .map(|element| {
                let syntax::block::pattern::Element { field, ellipsis } = element;
                if ellipsis.is_some() {
                    //TODO error here
                    todo!("Ellipsis on OR pattern?");
                }
                match field {
                    syntax::block::pattern::Field::Field {
                        field,
                        src,
                        constraint: None,
                    } => {
                        let field = sleigh
                            .get_global(&field)
                            .ok_or_else(|| Box::new(PatternError::MissingRef(src.clone())))?;
                        match field {
                            GlobalScope::Table(table) => {
                                //this branch only produces a table, so no
                                //token/token_fields, so clean/empty it
                                match &mut tokens {
                                    Some(tokens) => tokens.clear(),
                                    None => tokens = Some(HashMap::new()),
                                }
                                match &mut token_fields {
                                    Some(token_fields) => token_fields.clear(),
                                    None => token_fields = Some(HashMap::new()),
                                }
                                Ok(Verification::new_table(this_table, table, src, None))
                            }
                            _ => Err(Box::new(PatternError::UnrestrictedOr(src))),
                        }
                    }
                    syntax::block::pattern::Field::Field {
                        field,
                        src,
                        constraint: Some(constraint),
                    } => {
                        let verification = Verification::from_constraint(
                            sleigh, field, src, constraint, this_table,
                        )?;
                        match &verification {
                            Verification::ContextCheck { .. } | Verification::TableBuild { .. } => {
                                //this branch only produces_table/check_context,
                                //so no token/token_fields, so clean/empty it
                                match &mut tokens {
                                    Some(tokens) => tokens.clear(),
                                    None => tokens = Some(HashMap::new()),
                                }
                                match &mut token_fields {
                                    Some(token_fields) => token_fields.clear(),
                                    None => token_fields = Some(HashMap::new()),
                                }
                            }
                            Verification::TokenFieldCheck { field, .. } => {
                                let field_ele = sleigh.token_field(*field);
                                let token = field_ele.token;
                                //this branch checks a token_field, remove all
                                //tokens except for this one
                                match &mut tokens {
                                    Some(tokens) => {
                                        tokens.retain(|this_token, _| *this_token == token)
                                    }
                                    None => tokens = Some(HashMap::from([(token, 1)])),
                                }
                                //to token_fields are produced, clean/empty it
                                match &mut token_fields {
                                    Some(token_fields) => token_fields.clear(),
                                    None => token_fields = Some(HashMap::new()),
                                }
                            }
                            Verification::SubPattern { .. } => {
                                unreachable!()
                            }
                        }
                        Ok(verification)
                    }
                    syntax::block::pattern::Field::SubPattern(sub) => {
                        let pattern = Pattern::new(sleigh, sub, this_table)?;
                        //remote all the tokens that this sub_pattern don't
                        //produces or produces more then once
                        match &mut tokens {
                            Some(tokens) => tokens.retain(|this_token, num| {
                                let found = pattern.tokens.get(this_token);
                                if let Some(found_num) = found {
                                    *num = (*num).max(*found_num);
                                }
                                found.is_some()
                            }),
                            None => tokens = Some(pattern.tokens.clone()),
                        }
                        //remove all token this sub_pattern, don't produces
                        match &mut token_fields {
                            Some(fields) => fields.retain(|this_field, _| {
                                pattern.token_fields.contains_key(this_field)
                            }),
                            None => {
                                token_fields = Some(
                                    pattern
                                        .token_fields
                                        .iter()
                                        .map(|(k, v)| {
                                            let mut v = v.clone();
                                            v.local = false;
                                            (*k, v)
                                        })
                                        .collect(),
                                )
                            }
                        }
                        Ok(Verification::SubPattern {
                            location: location.clone(),
                            pattern,
                        })
                    }
                }
            })
            .collect::<Result<Vec<_>, _>>()?;

        //all tables produced should be included on the final tables list,
        //but mark always=true only tables produced in all branches
        let mut tables_iter = branches.iter();
        let mut tables: HashMap<TableId, ProducedTable> =
            match tables_iter.next().expect("LOGIC_ERROR: empty OR pattern") {
                //no tables in branch/no_branch
                Verification::ContextCheck { .. } | Verification::TokenFieldCheck { .. } => {
                    HashMap::new()
                }
                //branch produces only one table, add this table
                Verification::TableBuild {
                    produced_table,
                    verification: _,
                } => HashMap::from([(produced_table.table, produced_table.clone())]),
                Verification::SubPattern {
                    location: _,
                    pattern,
                } => pattern.tables.clone(),
            };
        tables_iter.for_each(|verification| {
            match verification {
                //no table in this branch, mark all existing tables as
                //not always producing.
                Verification::ContextCheck { .. } | Verification::TokenFieldCheck { .. } => {
                    tables.values_mut().for_each(|table| table.always = false)
                }

                //branch produces only one table, add this table
                Verification::TableBuild {
                    produced_table,
                    verification: _,
                } => {
                    let table = produced_table.table;
                    //if this table don't exists, add it and mark it
                    //not_always produce
                    tables.entry(table).or_insert_with(|| {
                        let mut produced_table = produced_table.clone();
                        produced_table.always = false;
                        produced_table
                    });
                    //mark all other tables as not always produce
                    tables
                        .iter_mut()
                        .filter(|(k, _v)| **k != table)
                        .for_each(|(_k, v)| v.always = false);
                }
                //this branch can produce 0..n tables
                Verification::SubPattern {
                    location: _,
                    pattern,
                } => {
                    pattern.tables.iter().for_each(|(ptr, produced_table)| {
                        //if this table don't exists, add it and mark it
                        //not_always produce
                        tables.entry(*ptr).or_insert_with(|| {
                            let mut produced_table = produced_table.clone();
                            produced_table.always = false;
                            produced_table
                        });
                    });
                    //mark all other tables as not always produce
                    tables
                        .iter_mut()
                        .filter(|(k, _v)| !pattern.tables.contains_key(*k))
                        .for_each(|(_k, v)| v.always = false);
                }
            }
        });

        //tokens that are present in all branches of the or can produce
        //fields,
        let mut tokens_iter = branches.iter();
        let mut tokens: HashMap<TokenId, usize> = match tokens_iter
            .next()
            .expect("LOGIC_ERROR: empty OR pattern")
        {
            //no fields exported
            Verification::ContextCheck { .. } | Verification::TableBuild { .. } => HashMap::new(),
            //TODO: sub_pattern export tokens?
            Verification::SubPattern { .. } => HashMap::new(),
            //one fields exported
            Verification::TokenFieldCheck {
                field,
                op: _,
                value: _,
            } => {
                let token_field = sleigh.token_field(*field);
                let token = token_field.token;
                HashMap::from([(token, 1)])
            }
        };
        for verification in tokens_iter {
            match verification {
                //no fields, just clean it
                Verification::ContextCheck { .. } | Verification::TableBuild { .. } => {
                    tokens.clear();
                }
                //TODO: sub_pattern export tokens?
                Verification::SubPattern { .. } => tokens.clear(),
                //one field, remove all the others
                Verification::TokenFieldCheck {
                    field,
                    op: _,
                    value: _,
                } => {
                    let token_field = sleigh.token_field(*field);
                    tokens.retain(|ptr, _token| *ptr == token_field.token)
                }
            }
            if tokens.is_empty() {
                //don't need to search any further, because is already empty
                break;
            }
        }
        //fields explicitly produced by all branches, are produced by the or
        //pattern
        let mut fields_iter = branches.iter();
        let mut fields: HashMap<TokenFieldId, ProducedTokenField> =
            match fields_iter.next().expect("LOGIC_ERROR: empty OR pattern") {
                //no fields exported
                Verification::ContextCheck { .. }
                | Verification::TokenFieldCheck { .. }
                | Verification::TableBuild { .. } => HashMap::new(),
                //TODO: sub_pattern export tokens?
                Verification::SubPattern { .. } => HashMap::new(),
                //one fields exported
            };
        for verification in fields_iter {
            match verification {
                //no fields, just clean it
                Verification::ContextCheck { .. }
                | Verification::TokenFieldCheck { .. }
                | Verification::TableBuild { .. } => {
                    fields.clear();
                }
                Verification::SubPattern {
                    location: _,
                    pattern,
                } => fields.retain(|token_field, _prod| {
                    pattern
                        .token_fields
                        .keys()
                        .any(|sub_field| sub_field == token_field)
                }),
            }
            if fields.is_empty() {
                //don't need to search any further, because is already empty
                break;
            }
        }
        fields.values_mut().for_each(|prod| prod.local = false);

        Ok(Self {
            op: syntax::block::pattern::Op::Or,
            location,
            tokens,
            token_fields: token_fields.unwrap_or_default(),
            tables,
            verifications: branches,
            pre: vec![],
            pos: vec![],
        })
    }
    fn new_and(
        sleigh: &Sleigh,
        location: Span,
        elements: impl Iterator<Item = syntax::block::pattern::Element>,
        this_table: TableId,
    ) -> Result<Self, Box<PatternError>> {
        //convert into Verifications, also capturing the explicit fields
        let mut token_fields = HashMap::new();
        use std::collections::hash_map::Entry::*;
        //closure for identation sake
        let mut add_explicit_token_field =
            |token_field: ProducedTokenField| match token_fields.entry(token_field.field) {
                Occupied(entry) => {
                    let field_old: &ProducedTokenField = entry.get();
                    let field_old = sleigh.token_field(field_old.field);
                    let token_field = sleigh.token_field(token_field.field);
                    Err(Box::new(PatternError::MultipleProduction(
                        field_old.location.clone(),
                        token_field.location.clone(),
                    )))
                }
                Vacant(entry) => {
                    entry.insert(token_field);
                    Ok(())
                }
            };
        let mut verifications = vec![];
        for element in elements {
            let syntax::block::pattern::Element { field, ellipsis } = element;
            if matches!(ellipsis, Some(Ellipsis::Left)) {
                todo!("Ellispsis on the Left")
            }
            match field {
                syntax::block::pattern::Field::Field {
                    field,
                    constraint: Some(constraint),
                    src,
                } => {
                    let syntax::block::pattern::Constraint { op: cmp_op, value } = constraint;
                    let value = DisassemblyBuilder::parse_expr(sleigh, value.expr)?;
                    let value = ConstraintValue::new(value);
                    let field = sleigh
                        .get_global(&field)
                        .ok_or_else(|| Box::new(PatternError::MissingRef(src.clone())))?;
                    match field {
                        GlobalScope::TokenField(x) => {
                            verifications.push(Verification::new_token_field(x, src, cmp_op, value))
                        }
                        //TODO create InstStart? Does start_start exists?
                        GlobalScope::Context(x) => {
                            verifications.push(Verification::new_context(x, src, cmp_op, value))
                        }
                        GlobalScope::Table(x) => verifications.push(Verification::new_table(
                            this_table,
                            x,
                            src,
                            Some((cmp_op, value)),
                        )),
                        _ => return Err(Box::new(PatternError::InvalidRef(src))),
                    };
                }

                syntax::block::pattern::Field::Field {
                    field,
                    constraint: None,
                    src,
                } => {
                    let field = sleigh
                        .get_global(&field)
                        .ok_or_else(|| Box::new(PatternError::MissingRef(src.clone())))?;
                    match field {
                        //could be explicitly defined, usually for display,
                        //but is not required
                        GlobalScope::Epsilon(_)
                        | GlobalScope::Varnode(_)
                        | GlobalScope::Context(_)
                        | GlobalScope::Bitrange(_) => (),
                        //token_field exported
                        GlobalScope::TokenField(token_field) => {
                            add_explicit_token_field(ProducedTokenField {
                                source: Some(src),
                                local: true,
                                field: token_field,
                            })?
                        }
                        GlobalScope::Table(table) => verifications
                            .push(Verification::new_table(this_table, table, src, None)),
                        _ => return Err(Box::new(PatternError::InvalidRef(src))),
                    }
                }

                syntax::block::pattern::Field::SubPattern(sub) => {
                    let pattern = Pattern::new(sleigh, sub.clone(), this_table)?;
                    //add/verify all token fields
                    pattern
                        .token_fields
                        .values()
                        .map(|prod| {
                            let mut prod = prod.clone();
                            prod.local = false;
                            prod
                        })
                        .try_for_each(&mut add_explicit_token_field)?;
                    verifications.push(Verification::SubPattern {
                        //TODO improve this src
                        location: sub.src.clone(),
                        pattern,
                    })
                }
            }
        }

        //tokens (that can produce implicit fields) are only taken from local,
        //sub_patterns can't produce implicit token_fields
        let fields_from_verifications =
            verifications
                .iter()
                .filter_map(|verification| match verification {
                    Verification::TokenFieldCheck { field, .. } => Some(sleigh.token_field(*field)),
                    Verification::ContextCheck { .. }
                    | Verification::TableBuild { .. }
                    | Verification::SubPattern { .. } => None,
                });
        let fields_from_explicit_fields = token_fields
            .values()
            .map(|token_fields| sleigh.token_field(token_fields.field));
        let token_from_pattern = fields_from_explicit_fields
            .chain(fields_from_verifications)
            .map(|field| field.token);
        let mut tokens = HashMap::new();
        for token in token_from_pattern {
            *tokens.entry(token).or_insert(0) += 1;
        }

        let tables = verifications
            .iter()
            .filter_map(Verification::tables)
            .flatten()
            .map(|produced_table| (produced_table.table, produced_table.clone()))
            .collect();

        Ok(Self {
            op: syntax::block::pattern::Op::And,
            location,
            token_fields,
            tokens,
            verifications,
            tables,
            pre: vec![],
            pos: vec![],
        })
    }
    //token fields required by value comparisons that this block can't produce
    //itself
    pub fn unresolved_token_fields(&self, sleigh: &Sleigh) -> HashMap<TokenFieldId, Span> {
        let mut this_unresolved = FindValues::search(self.verifications.iter());
        //remove all token_fields that can be produced locally
        this_unresolved.retain(|token_field, _location| {
            let token_field = sleigh.token_field(*token_field);
            !self.tokens.contains_key(&token_field.token)
        });
        //for each sub_pattern, find unresolved token_fields
        this_unresolved.extend(
            self.verifications
                .iter()
                .flat_map(Verification::sub_pattern)
                .flat_map(|pattern| pattern.unresolved_token_fields(sleigh)),
        );
        ////remove all token_fields that is produced locally
        //this_unresolved.retain(|_key, token_field| {
        //    !self.token_fields.contains_key(&token_field.element_ptr())
        //});
        this_unresolved
    }
    pub fn is_token_field_produced(&self, search_field: TokenFieldId) -> bool {
        self.token_fields.contains_key(&search_field)
    }
    pub fn is_table_produced(&self, token_field: TableId) -> bool {
        self.tables.contains_key(&token_field)
    }

    // return true if this block can produce this token field
    pub fn produce_token_field(&mut self, sleigh: &Sleigh, token_field_id: TokenFieldId) -> bool {
        // field is already produced
        if self.is_token_field_produced(token_field_id) {
            return true;
        }
        // check if we can produce it implicitly
        let token_field = sleigh.token_field(token_field_id);
        let token_num_used = self.tokens.get(&token_field.token).copied().unwrap_or(0);
        // we can only implicitly produce a field if the token happen only
        // once in this pattern
        if token_num_used > 1 {
            return false;
        }
        //implifitly produce this token field
        self.token_fields.insert(
            token_field_id,
            ProducedTokenField {
                source: None,
                field: token_field_id,
                local: true,
            },
        );
        true
    }
    /// len of the pattern block, except for tables
    pub fn root_len(&self, sleigh: &Sleigh) -> usize {
        match self.op {
            syntax::block::pattern::Op::And => {
                //get the biggest token, from all token_field verifications and
                //explicit/implicit token_fields
                let biggest_token_bytes = self
                    .tokens
                    .keys()
                    .map(|token| sleigh.token(*token).len_bytes.get())
                    .max()
                    .unwrap_or(0);
                let biggest_token = usize::try_from(biggest_token_bytes).unwrap() * 8;
                //calc the len of the biggest sub_pattern
                let biggest_sub_pattern = self
                    .verifications
                    .iter()
                    .filter_map(Verification::sub_pattern)
                    .map(|pattern| pattern.root_len(sleigh))
                    .max()
                    .unwrap_or(0);
                //return the biggest one
                biggest_token.max(biggest_sub_pattern)
            }
            syntax::block::pattern::Op::Or => {
                //get the biggest of all branches
                self.verifications
                    .iter()
                    .map(|v| v.root_len(sleigh))
                    .max()
                    .unwrap_or(0)
            }
        }
    }
}

impl BlockPhase1 {
    pub fn solve_or<T: SolverStatus>(
        &mut self,
        sleigh: &Sleigh,
        solved: &mut T,
        base: &mut BlockBase,
    ) -> Result<(), Box<PatternError>> {
        //each branch of the or is represented by each verification
        enum OrLenPossible {
            Recursive, //recursive in `OR` is not allowed
            Unknown,   //at least one branch len is not known
        }
        let mut branch_len_iter = base
            .verifications
            .iter()
            .map(|v| match v.pattern_len(sleigh) {
                Some(ConstructorPatternLen::Basic(len)) => Ok(len),
                Some(
                    ConstructorPatternLen::NonGrowingRecursive(_)
                    | ConstructorPatternLen::GrowingRecursive { .. },
                ) => Err(OrLenPossible::Recursive),
                None => Err(OrLenPossible::Unknown),
            });
        //unwrap never happen because empty pattern default to `Op::And`
        let first = branch_len_iter.next().unwrap();
        let new_len = first
            .and_then(|first| branch_len_iter.try_fold(first, |acc, x| Ok(acc.intersection(x?))));
        match new_len {
            //all the lens are valid
            Ok(new_len) => {
                solved.i_did_a_thing();
                self.len = Some(ConstructorPatternLen::Basic(new_len));
            }
            //at least one len is not known yet, request another
            //run to solve it
            Err(OrLenPossible::Unknown) => {
                solved.iam_not_finished(&base.location, file!(), line!());
            }
            //at least one len is recursive
            Err(OrLenPossible::Recursive) => {
                return Err(Box::new(PatternError::InvalidOrLen(base.location.clone())))
            }
        }
        Ok(())
    }
    pub fn solve_and<T: SolverStatus>(
        &mut self,
        sleigh: &Sleigh,
        solved: &mut T,
        base: &mut BlockBase,
    ) -> Result<(), Box<PatternError>> {
        //the pattern len is the biggest of all tokens, all sub_patterns
        //and all tables in the pattern
        let tokens_len = base
            .tokens
            .keys()
            .map(|token| sleigh.token(*token).len_bytes)
            .max()
            .map(|token_len| ConstructorPatternLen::Basic(PatternLen::Defined(token_len.get())));
        let mut verifications_len = base.verifications.iter().map(|v| v.pattern_len(sleigh));
        let first = tokens_len.unwrap_or(PatternLen::Defined(0).into());
        let final_len = verifications_len.try_fold(first, |acc, x| acc.greater(x?));

        if let Some(final_len) = final_len {
            //basic means fully solved
            if !final_len.is_basic() {
                //need to solve the recursive len of the pattern
                solved.iam_not_finished(&base.location, file!(), line!());
            }
            if self.len != Some(final_len) {
                solved.i_did_a_thing();
                self.len = Some(final_len);
            }
        } else {
            //if not fully finished yet, request another run
            solved.iam_not_finished(&base.location, file!(), line!());
        }
        Ok(())
    }
    pub fn solve<T: SolverStatus>(
        &mut self,
        sleigh: &Sleigh,
        solved: &mut T,
        base: &mut BlockBase,
    ) -> Result<bool, Box<PatternError>> {
        //if len is already solved and no unresolved_token_field, there is
        //nothing todo
        if matches!(&self.len, Some(ConstructorPatternLen::Basic(_))) {
            return Ok(true);
        }
        //call solve in all sub_patterns
        base.verifications
            .iter_mut()
            .filter_map(Verification::sub_pattern_mut)
            .try_for_each(|sub| sub.calculate_len(sleigh, solved).map(|_| ()))?;
        //TODO check all table value verifications export a const value
        match base.op {
            syntax::block::pattern::Op::And => self.solve_and(sleigh, solved, base)?,
            syntax::block::pattern::Op::Or => self.solve_or(sleigh, solved, base)?,
        }
        let finished = matches!(&self.len, Some(ConstructorPatternLen::Basic(_)));
        if !finished {
            solved.iam_not_finished(&base.location, file!(), line!());
        }
        Ok(finished)
    }
}

impl BlockPhase2 {
    pub fn new(base: &mut BlockBase, len: PatternLen, variants_prior: usize) -> Self {
        match base.op {
            syntax::block::pattern::Op::And => Self::new_and(base, len, variants_prior),
            syntax::block::pattern::Op::Or => Self::new_or(base, len, variants_prior),
        }
    }
    fn new_and(base: &mut BlockBase, len: PatternLen, variants_prior: usize) -> Self {
        let variants_number = base
            .verifications
            .iter_mut()
            .filter_map(Verification::sub_pattern_mut)
            .fold(1, |variants_counter, pattern| {
                pattern.calculate_bits(variants_counter);
                let variants_number = pattern.variants_num();
                variants_counter * variants_number
            });
        Self {
            len,
            variants_prior,
            variants_number,
            variants_lock: false,
        }
    }
    fn new_or(base: &mut BlockBase, len: PatternLen, variants_prior: usize) -> Self {
        let variants_number = base
            .verifications
            .iter_mut()
            .map(|verification| {
                if let Some(pattern) = verification.sub_pattern_mut() {
                    pattern.calculate_bits(variants_prior)
                }
                verification.variants_number()
            })
            .sum();
        Self {
            len,
            variants_prior,
            variants_number,
            variants_lock: false,
        }
    }
}