sleigh-sys 0.1.0

Rust bindings for Ghidra's Sleigh decompiler
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "slghpattern.hh"

uintm DisjointPattern::getMask(int4 startbit,int4 size,bool context) const

{
  PatternBlock *block = getBlock(context);
  if (block != (PatternBlock *)0)
    return block->getMask(startbit,size);
  return 0;
}

uintm DisjointPattern::getValue(int4 startbit,int4 size,bool context) const

{
  PatternBlock *block = getBlock(context);
  if (block != (PatternBlock *)0)
    return block->getValue(startbit,size);
  return 0;
}

int4 DisjointPattern::getLength(bool context) const

{
  PatternBlock *block = getBlock(context);
  if (block != (PatternBlock *)0)
    return block->getLength();
  return 0;
}

bool DisjointPattern::specializes(const DisjointPattern *op2) const

{				// Return true, if everywhere this's mask is non-zero
				// op2's mask is non-zero and op2's value match this's
  PatternBlock *a,*b;

  a = getBlock(false);
  b = op2->getBlock(false);
  if ((b != (PatternBlock *)0)&&(!b->alwaysTrue())) {	// a must match existing block
    if (a == (PatternBlock *)0) return false;
    if (!a->specializes(b)) return false;
  }
  a = getBlock(true);
  b = op2->getBlock(true);
  if ((b != (PatternBlock *)0)&&(!b->alwaysTrue())) {	// a must match existing block
    if (a == (PatternBlock *)0) return false;
    if (!a->specializes(b)) return false;
  }
  return true;
}

bool DisjointPattern::identical(const DisjointPattern *op2) const

{				// Return true if patterns match exactly
  PatternBlock *a,*b;
  
  a = getBlock(false);
  b = op2->getBlock(false);
  if (b != (PatternBlock *)0) {	// a must match existing block
    if (a == (PatternBlock *)0) {
      if (!b->alwaysTrue())
	return false;
    }
    else if (!a->identical(b))
      return false;
  }
  else {
    if ((a != (PatternBlock *)0)&&(!a->alwaysTrue()))
      return false;
  }
  a = getBlock(true);
  b = op2->getBlock(true);
  if (b != (PatternBlock *)0) {	// a must match existing block
    if (a == (PatternBlock *)0) {
      if (!b->alwaysTrue())
	return false;
    }
    else if (!a->identical(b))
      return false;
  }
  else {
    if ((a != (PatternBlock *)0)&&(!a->alwaysTrue()))
      return false;
  }
  return true;
}

static bool resolveIntersectBlock(PatternBlock *bl1,PatternBlock *bl2,PatternBlock *thisblock)

{
  PatternBlock *inter;
  bool allocated = false;
  bool res = true;

  if (bl1 == (PatternBlock *)0)
    inter = bl2;
  else if (bl2 == (PatternBlock *)0)
    inter = bl1;
  else {
    allocated = true;
    inter = bl1->intersect(bl2);
  }
  if (inter == (PatternBlock *)0) {
    if (thisblock != (PatternBlock *)0)
      res = false;
  }
  else if (thisblock == (PatternBlock *)0)
    res = false;
  else
    res = thisblock->identical(inter);
  if (allocated)
    delete inter;
  return res;
}

bool DisjointPattern::resolvesIntersect(const DisjointPattern *op1,const DisjointPattern *op2) const

{ // Is this pattern equal to the intersection of -op1- and -op2-
  if (!resolveIntersectBlock(op1->getBlock(false),op2->getBlock(false),getBlock(false)))
    return false;
  return resolveIntersectBlock(op1->getBlock(true),op2->getBlock(true),getBlock(true));
}

DisjointPattern *DisjointPattern::restoreDisjoint(const Element *el)

{				// DisjointPattern factory
  DisjointPattern *res;
  if (el->getName() == "instruct_pat")
    res = new InstructionPattern();
  else if (el->getName() == "context_pat")
    res = new ContextPattern();
  else
    res = new CombinePattern();
  res->restoreXml(el);
  return res;
}

void PatternBlock::normalize(void)

{
  if (nonzerosize<=0) {		// Check if alwaystrue or alwaysfalse
    offset = 0;			// in which case we don't need mask and value
    maskvec.clear();
    valvec.clear();
    return;
  }
  vector<uintm>::iterator iter1,iter2;
  
  iter1 = maskvec.begin();	// Cut zeros from beginning of mask
  iter2 = valvec.begin();
  while((iter1 != maskvec.end())&&((*iter1)==0)) {
    iter1++;
    iter2++;
    offset += sizeof(uintm);
  }
  maskvec.erase(maskvec.begin(),iter1);
  valvec.erase(valvec.begin(),iter2);

  if (!maskvec.empty()) {
    int4 suboff = 0;		// Cut off unaligned zeros from beginning of mask
    uintm tmp = maskvec[0];
    while(tmp != 0) {
      suboff += 1;
      tmp >>= 8;
    }
    suboff = sizeof(uintm)-suboff;
    if (suboff != 0) {
      offset += suboff;		// Slide up maskvec by suboff bytes
      for(int4 i=0;i<maskvec.size()-1;++i) {
	tmp = maskvec[i] << (suboff*8);
	tmp |= (maskvec[i+1] >> ((sizeof(uintm)-suboff)*8));
	maskvec[i] = tmp;
      }
      maskvec.back() <<= suboff*8;
      for(int4 i=0;i<valvec.size()-1;++i) { // Slide up valvec by suboff bytes
	tmp = valvec[i] << (suboff*8);
	tmp |= (valvec[i+1] >> ((sizeof(uintm)-suboff)*8));
	valvec[i] = tmp;
      }
      valvec.back() <<= suboff*8;
    }
    
    iter1 = maskvec.end();	// Cut zeros from end of mask
    iter2 = valvec.end();
    while(iter1 != maskvec.begin()) {
      --iter1;
      --iter2;
      if ((*iter1) != 0) break; // Find last non-zero
    }
    if (iter1 != maskvec.end()) {
      iter1++;			// Find first zero, in last zero chain
      iter2++;
    }
    maskvec.erase(iter1,maskvec.end());
    valvec.erase(iter2,valvec.end());
  }

  if (maskvec.empty()) {
    offset = 0;
    nonzerosize = 0;		// Always true
    return;
  }
  nonzerosize = maskvec.size() * sizeof(uintm);
  uintm tmp = maskvec.back();	// tmp must be nonzero
  while( (tmp&0xff) == 0) {
    nonzerosize -= 1;
    tmp >>= 8;
  }
}

PatternBlock::PatternBlock(int4 off,uintm msk,uintm val)

{				// Define mask and value pattern, confined to one uintm
  offset = off;
  maskvec.push_back(msk);
  valvec.push_back(val);
  nonzerosize = sizeof(uintm);	// Assume all non-zero bytes before normalization
  normalize();
}

PatternBlock::PatternBlock(bool tf)

{
  offset = 0;
  if (tf)
    nonzerosize = 0;
  else
    nonzerosize = -1;
}

PatternBlock::PatternBlock(const PatternBlock *a,const PatternBlock *b)

{				// Construct PatternBlock by ANDing two others together
  PatternBlock *res = a->intersect(b);
  offset = res->offset;
  nonzerosize = res->nonzerosize;
  maskvec = res->maskvec;
  valvec = res->valvec;
  delete res;
}

PatternBlock::PatternBlock(vector<PatternBlock *> &list)

{				// AND several blocks together to construct new block
  PatternBlock *res,*next;

  if (list.empty()) {		// If not ANDing anything
    offset = 0;			// make constructed block always true
    nonzerosize = 0;
    return;
  }
  res = list[0];
  for(int4 i=1;i<list.size();++i) {
    next = res->intersect(list[i]);
    delete res;
    res = next;
  }
  offset = res->offset;
  nonzerosize = res->nonzerosize;
  maskvec = res->maskvec;
  valvec = res->valvec;
  delete res;
}

PatternBlock *PatternBlock::clone(void) const

{
  PatternBlock *res = new PatternBlock(true);

  res->offset = offset;
  res->nonzerosize = nonzerosize;
  res->maskvec = maskvec;
  res->valvec = valvec;
  return res;
}

PatternBlock *PatternBlock::commonSubPattern(const PatternBlock *b) const

{				// The resulting pattern has a 1-bit in the mask
				// only if the two pieces have a 1-bit and the
				// values agree
  PatternBlock *res = new PatternBlock(true);
  int4 maxlength = (getLength() > b->getLength()) ? getLength() : b->getLength();

  res->offset = 0;
  int4 offset = 0;
  uintm mask1,val1,mask2,val2;
  uintm resmask,resval;
  while(offset < maxlength) {
    mask1 = getMask(offset*8,sizeof(uintm)*8);
    val1 = getValue(offset*8,sizeof(uintm)*8);
    mask2 = b->getMask(offset*8,sizeof(uintm)*8);
    val2 = b->getValue(offset*8,sizeof(uintm)*8);
    resmask = mask1 & mask2 & ~(val1^val2);
    resval = val1 & val2 & resmask;
    res->maskvec.push_back(resmask);
    res->valvec.push_back(resval);
    offset += sizeof(uintm);
  }
  res->nonzerosize = maxlength;
  res->normalize();
  return res;
}

PatternBlock *PatternBlock::intersect(const PatternBlock *b) const

{ // Construct the intersecting pattern
  if (alwaysFalse() || b->alwaysFalse())
    return new PatternBlock(false);
  PatternBlock *res = new PatternBlock(true);
  int4 maxlength = (getLength() > b->getLength()) ? getLength() : b->getLength();

  res->offset = 0;
  int4 offset = 0;
  uintm mask1,val1,mask2,val2,commonmask;
  uintm resmask,resval;
  while(offset < maxlength) {
    mask1 = getMask(offset*8,sizeof(uintm)*8);
    val1 = getValue(offset*8,sizeof(uintm)*8);
    mask2 = b->getMask(offset*8,sizeof(uintm)*8);
    val2 = b->getValue(offset*8,sizeof(uintm)*8);
    commonmask = mask1 & mask2;	// Bits in mask shared by both patterns
    if ((commonmask & val1) != (commonmask & val2)) {
      res->nonzerosize = -1;	// Impossible pattern
      res->normalize();
      return res;
    }
    resmask = mask1 | mask2;
    resval = (mask1 & val1) | (mask2 & val2);
    res->maskvec.push_back(resmask);
    res->valvec.push_back(resval);
    offset += sizeof(uintm);
  }
  res->nonzerosize = maxlength;
  res->normalize();
  return res;
}

bool PatternBlock::specializes(const PatternBlock *op2) const

{				// does every masked bit in -this- match the corresponding
				// masked bit in -op2-
  int4 length = 8*op2->getLength();
  int4 tmplength;
  uintm mask1,mask2,value1,value2;
  int4 sbit = 0;
  while(sbit < length) {
    tmplength = length-sbit;
    if (tmplength > 8*sizeof(uintm))
      tmplength = 8*sizeof(uintm);
    mask1 = getMask(sbit,tmplength);
    value1 = getValue(sbit,tmplength);
    mask2 = op2->getMask(sbit,tmplength);
    value2 = op2->getValue(sbit,tmplength);
    if ((mask1 & mask2) != mask2) return false;
    if ((value1 & mask2) != (value2 & mask2)) return false;
    sbit += tmplength;
  }
  return true;
}

bool PatternBlock::identical(const PatternBlock *op2) const

{				// Do the mask and value match exactly
  int4 tmplength;
  int4 length = 8*op2->getLength();
  tmplength = 8*getLength();
  if (tmplength > length)
    length = tmplength;		// Maximum of two lengths
  uintm mask1,mask2,value1,value2;
  int4 sbit = 0;
  while(sbit < length) {
    tmplength = length-sbit;
    if (tmplength > 8*sizeof(uintm))
      tmplength = 8*sizeof(uintm);
    mask1 = getMask(sbit,tmplength);
    value1 = getValue(sbit,tmplength);
    mask2 = op2->getMask(sbit,tmplength);
    value2 = op2->getValue(sbit,tmplength);
    if (mask1 != mask2) return false;
    if ((mask1&value1) != (mask2&value2)) return false;
    sbit += tmplength;
  }
  return true;
}

uintm PatternBlock::getMask(int4 startbit,int4 size) const

{
  startbit -= 8*offset;
  // Note the division and remainder here is unsigned.  Then it is recast to signed. 
  // If startbit is negative, then wordnum1 is either negative or very big,
  // if (unsigned size is same as sizeof int)
  // In either case, shift should come out between 0 and 8*sizeof(uintm)-1
  int4 wordnum1 = startbit/(8*sizeof(uintm));
  int4 shift = startbit % (8*sizeof(uintm));
  int4 wordnum2 = (startbit+size-1)/(8*sizeof(uintm));
  uintm res;

  if ((wordnum1<0)||(wordnum1>=maskvec.size()))
    res = 0;
  else
    res = maskvec[wordnum1];

  res <<= shift;
  if (wordnum1 != wordnum2) {
    uintm tmp;
    if ((wordnum2<0)||(wordnum2>=maskvec.size()))
      tmp = 0;
    else
      tmp = maskvec[wordnum2];
    res |= (tmp>>(8*sizeof(uintm)-shift));
  }
  res >>= (8*sizeof(uintm) - size);
  
  return res;
}

uintm PatternBlock::getValue(int4 startbit,int4 size) const

{
  startbit -= 8*offset;
  int4 wordnum1 = startbit/(8*sizeof(uintm));
  int4 shift = startbit % (8*sizeof(uintm));
  int4 wordnum2 = (startbit+size-1)/(8*sizeof(uintm));
  uintm res;

  if ((wordnum1<0)||(wordnum1>=valvec.size()))
    res = 0;
  else
    res = valvec[wordnum1];
  res <<= shift;
  if (wordnum1 != wordnum2) {
    uintm tmp;
    if ((wordnum2<0)||(wordnum2>=valvec.size()))
      tmp = 0;
    else
      tmp = valvec[wordnum2];
    res |= (tmp>>(8*sizeof(uintm)-shift));
  }
  res >>= (8*sizeof(uintm) - size);
  
  return res;
}

bool PatternBlock::isInstructionMatch(ParserWalker &walker) const

{
  if (nonzerosize<=0) return (nonzerosize==0);
  int4 off = offset;
  for(int4 i=0;i<maskvec.size();++i) {
    uintm data = walker.getInstructionBytes(off,sizeof(uintm));
    if ((maskvec[i] & data)!=valvec[i]) return false;
    off += sizeof(uintm);
  }
  return true;
}

bool PatternBlock::isContextMatch(ParserWalker &walker) const

{
  if (nonzerosize<=0) return (nonzerosize==0);
  int4 off = offset;
  for(int4 i=0;i<maskvec.size();++i) {
    uintm data = walker.getContextBytes(off,sizeof(uintm));
    if ((maskvec[i] & data)!=valvec[i]) return false;
    off += sizeof(uintm);
  }
  return true;
}

void PatternBlock::saveXml(ostream &s) const

{
  s << "<pat_block ";
  s << "offset=\"" << dec << offset << "\" ";
  s << "nonzero=\"" << nonzerosize << "\">\n";
  for(int4 i=0;i<maskvec.size();++i) {
    s << "  <mask_word ";
    s << "mask=\"0x" << hex << maskvec[i] << "\" ";
    s << "val=\"0x" << valvec[i] << "\"/>\n";
  }
  s << "</pat_block>\n";
}

void PatternBlock::restoreXml(const Element *el)

{
  {
    istringstream s(el->getAttributeValue("offset"));
    s.unsetf(ios::dec | ios::hex | ios::oct);
    s >> offset;
  }
  {
    istringstream s(el->getAttributeValue("nonzero"));
    s.unsetf(ios::dec | ios::hex | ios::oct);
    s >> nonzerosize;
  }
  const List &list(el->getChildren());
  List::const_iterator iter;
  iter = list.begin();
  uintm mask,val;
  while(iter != list.end()) {
    Element *subel = *iter;
    {
      istringstream s(subel->getAttributeValue("mask"));
      s.unsetf(ios::dec | ios::hex | ios::oct);
      s >> mask;
    }
    {
      istringstream s(subel->getAttributeValue("val"));
      s.unsetf(ios::dec | ios::hex | ios::oct);
      s >> val;
    }
    maskvec.push_back(mask);
    valvec.push_back(val);
    ++iter;
  }
  normalize();
}

Pattern *InstructionPattern::doAnd(const Pattern *b,int4 sa) const

{
  if (b->numDisjoint()>0)
    return b->doAnd(this,-sa);

  const CombinePattern *b2 = dynamic_cast<const CombinePattern *>(b);
  if (b2 != (const CombinePattern *)0)
    return b->doAnd(this,-sa);

  const ContextPattern *b3 = dynamic_cast<const ContextPattern *>(b);
  if (b3 != (const ContextPattern *)0) {
    InstructionPattern *newpat = (InstructionPattern *)simplifyClone();
    if (sa < 0)
      newpat->shiftInstruction(-sa);
    return new CombinePattern((ContextPattern *)b3->simplifyClone(),newpat);
  }
  const InstructionPattern *b4 = (const InstructionPattern *)b;

  PatternBlock *respattern;
  if (sa < 0) {
    PatternBlock *a = maskvalue->clone();
    a->shift(-sa);
    respattern = a->intersect(b4->maskvalue);
    delete a;
  }
  else {
    PatternBlock *c = b4->maskvalue->clone();
    c->shift(sa);
    respattern = maskvalue->intersect(c);
    delete c;
  }
  return new InstructionPattern(respattern);
}

Pattern *InstructionPattern::commonSubPattern(const Pattern *b,int4 sa) const

{
  if (b->numDisjoint()>0)
    return b->commonSubPattern(this,-sa);

  const CombinePattern *b2 = dynamic_cast<const CombinePattern *>(b);
  if (b2 != (const CombinePattern *)0)
    return b->commonSubPattern(this,-sa);

  const ContextPattern *b3 = dynamic_cast<const ContextPattern *>(b);
  if (b3 != (const ContextPattern *)0) {
    InstructionPattern *res = new InstructionPattern(true);
    return res;
  }
  const InstructionPattern *b4 = (const InstructionPattern *)b;
  
  PatternBlock *respattern;
  if (sa < 0) {
    PatternBlock *a = maskvalue->clone();
    a->shift(-sa);
    respattern = a->commonSubPattern(b4->maskvalue);
    delete a;
  }
  else {
    PatternBlock *c = b4->maskvalue->clone();
    c->shift(sa);
    respattern = maskvalue->commonSubPattern(c);
    delete c;
  }
  return new InstructionPattern(respattern);
}

Pattern *InstructionPattern::doOr(const Pattern *b,int4 sa) const

{
  if (b->numDisjoint()>0)
    return b->doOr(this,-sa);

  const CombinePattern *b2 = dynamic_cast<const CombinePattern *>(b);
  if (b2 != (const CombinePattern *)0)
    return b->doOr(this,-sa);

  DisjointPattern *res1,*res2;
  res1 = (DisjointPattern *)simplifyClone();
  res2 = (DisjointPattern *)b->simplifyClone();
  if (sa < 0)
    res1->shiftInstruction(-sa);
  else
    res2->shiftInstruction(sa);
  return new OrPattern(res1,res2);
}

void InstructionPattern::saveXml(ostream &s) const

{
  s << "<instruct_pat>\n";
  maskvalue->saveXml(s);
  s << "</instruct_pat>\n";
}

void InstructionPattern::restoreXml(const Element *el)

{
  const List &list(el->getChildren());
  List::const_iterator iter;
  iter = list.begin();
  maskvalue = new PatternBlock(true);
  maskvalue->restoreXml(*iter);
}

Pattern *ContextPattern::doOr(const Pattern *b,int4 sa) const

{
  const ContextPattern *b2 = dynamic_cast<const ContextPattern *>(b);
  if (b2 == (const ContextPattern *)0)
    return b->doOr(this,-sa);

  return new OrPattern((DisjointPattern *)simplifyClone(),(DisjointPattern *)b2->simplifyClone());
}

Pattern *ContextPattern::doAnd(const Pattern *b,int4 sa) const

{
  const ContextPattern *b2 = dynamic_cast<const ContextPattern *>(b);
  if (b2 == (const ContextPattern *)0)
    return b->doAnd(this,-sa);

  PatternBlock *resblock = maskvalue->intersect(b2->maskvalue);
  return new ContextPattern(resblock);
}

Pattern *ContextPattern::commonSubPattern(const Pattern *b,int4 sa) const

{
  const ContextPattern *b2 = dynamic_cast<const ContextPattern *>(b);
  if (b2 == (const ContextPattern *)0)
    return b->commonSubPattern(this,-sa);

  PatternBlock *resblock = maskvalue->commonSubPattern(b2->maskvalue);
  return new ContextPattern(resblock);
}

void ContextPattern::saveXml(ostream &s) const

{
  s << "<context_pat>\n";
  maskvalue->saveXml(s);
  s << "</context_pat>\n";
}

void ContextPattern::restoreXml(const Element *el)

{
  const List &list(el->getChildren());
  List::const_iterator iter;
  iter = list.begin();
  maskvalue = new PatternBlock(true);
  maskvalue->restoreXml(*iter);
}

CombinePattern::~CombinePattern(void)

{
  if (context != (ContextPattern *)0)
    delete context;
  if (instr != (InstructionPattern *)0)
    delete instr;
}

bool CombinePattern::isMatch(ParserWalker &walker) const

{
  if (!instr->isMatch(walker)) return false;
  if (!context->isMatch(walker)) return false;
  return true;
}

bool CombinePattern::alwaysTrue(void) const

{
  return (context->alwaysTrue() && instr->alwaysTrue());
}

bool CombinePattern::alwaysFalse(void) const

{
  return (context->alwaysFalse() || instr->alwaysFalse());
}

Pattern *CombinePattern::doAnd(const Pattern *b,int4 sa) const

{
  CombinePattern *tmp;
  
  if (b->numDisjoint() != 0)
    return b->doAnd(this,-sa);

  const CombinePattern *b2 = dynamic_cast<const CombinePattern *>(b);
  if (b2 != (CombinePattern *)0) {
    ContextPattern *c = (ContextPattern *)context->doAnd(b2->context,0);
    InstructionPattern *i = (InstructionPattern *)instr->doAnd(b2->instr,sa);
    tmp = new CombinePattern(c,i);
  }
  else {
    const InstructionPattern *b3 = dynamic_cast<const InstructionPattern *>(b);
    if (b3 != (const InstructionPattern *)0) {
      InstructionPattern *i = (InstructionPattern *)instr->doAnd(b3,sa);
      tmp = new CombinePattern((ContextPattern *)context->simplifyClone(),i);
    }
    else {			// Must be a ContextPattern
      ContextPattern *c = (ContextPattern *)context->doAnd(b,0);
      InstructionPattern *newpat = (InstructionPattern *) instr->simplifyClone();
      if (sa < 0)
	newpat->shiftInstruction(-sa);
      tmp = new CombinePattern(c,newpat);
    }
  }
  return tmp;
}

Pattern *CombinePattern::commonSubPattern(const Pattern *b,int4 sa) const

{
  Pattern *tmp;

  if (b->numDisjoint() != 0)
    return b->commonSubPattern(this,-sa);

  const CombinePattern *b2 = dynamic_cast<const CombinePattern *>(b);
  if (b2 != (CombinePattern *)0) {
    ContextPattern *c = (ContextPattern *)context->commonSubPattern(b2->context,0);
    InstructionPattern *i = (InstructionPattern *)instr->commonSubPattern(b2->instr,sa);
    tmp = new CombinePattern(c,i);
  }
  else {
    const InstructionPattern *b3 = dynamic_cast<const InstructionPattern *>(b);
    if (b3 != (const InstructionPattern *)0)
      tmp = instr->commonSubPattern(b3,sa);
    else			// Must be a ContextPattern
      tmp = context->commonSubPattern(b,0);
  }
  return tmp;
}

Pattern *CombinePattern::doOr(const Pattern *b,int4 sa) const

{
  if (b->numDisjoint() != 0)
    return b->doOr(this,-sa);

  DisjointPattern *res1 = (DisjointPattern *)simplifyClone();
  DisjointPattern *res2 = (DisjointPattern *)b->simplifyClone();
  if (sa < 0)
    res1->shiftInstruction(-sa);
  else
    res2->shiftInstruction(sa);
  OrPattern *tmp = new OrPattern(res1,res2);
  return tmp;
}

Pattern *CombinePattern::simplifyClone(void) const

{				// We should only have to think at "our" level
  if (context->alwaysTrue())
    return instr->simplifyClone();
  if (instr->alwaysTrue())
    return context->simplifyClone();
  if (context->alwaysFalse()||instr->alwaysFalse())
    return new InstructionPattern(false);
  return new CombinePattern((ContextPattern *)context->simplifyClone(),
			    (InstructionPattern *)instr->simplifyClone());
}

void CombinePattern::saveXml(ostream &s) const

{
  s << "<combine_pat>\n";
  context->saveXml(s);
  instr->saveXml(s);
  s << "</combine_pat>\n";
}

void CombinePattern::restoreXml(const Element *el)

{
  const List &list(el->getChildren());
  List::const_iterator iter;
  iter = list.begin();
  context = new ContextPattern();
  context->restoreXml(*iter);
  ++iter;
  instr = new InstructionPattern();
  instr->restoreXml(*iter);
}

OrPattern::OrPattern(DisjointPattern *a,DisjointPattern *b)

{
  orlist.push_back(a);
  orlist.push_back(b);
}

OrPattern::OrPattern(const vector<DisjointPattern *> &list)

{
  vector<DisjointPattern *>::const_iterator iter;

  for(iter=list.begin();iter!=list.end();++iter)
    orlist.push_back(*iter);
}

OrPattern::~OrPattern(void)

{
  vector<DisjointPattern *>::iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter)
    delete *iter;
}

void OrPattern::shiftInstruction(int4 sa)

{
  vector<DisjointPattern *>::iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter)
    (*iter)->shiftInstruction(sa);
}

bool OrPattern::isMatch(ParserWalker &walker) const

{
  for(int4 i=0;i<orlist.size();++i)
    if (orlist[i]->isMatch(walker))
      return true;
  return false;
}

bool OrPattern::alwaysTrue(void) const

{				// This isn't quite right because different branches
				// may cover the entire gamut
  vector<DisjointPattern *>::const_iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter)
    if ((*iter)->alwaysTrue()) return true;
  return false;
}

bool OrPattern::alwaysFalse(void) const

{
  vector<DisjointPattern *>::const_iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter)
    if (!(*iter)->alwaysFalse()) return false;
  return true;
}

bool OrPattern::alwaysInstructionTrue(void) const

{
  vector<DisjointPattern *>::const_iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter)
    if (!(*iter)->alwaysInstructionTrue()) return false;
  return true;
}

Pattern *OrPattern::doAnd(const Pattern *b,int4 sa) const

{
  const OrPattern *b2 = dynamic_cast<const OrPattern *>(b);
  vector<DisjointPattern *> newlist;
  vector<DisjointPattern *>::const_iterator iter,iter2;
  DisjointPattern *tmp;
  OrPattern *tmpor;

  if (b2 == (const OrPattern *)0) {
    for(iter=orlist.begin();iter!=orlist.end();++iter) {
      tmp = (DisjointPattern *)(*iter)->doAnd(b,sa);
      newlist.push_back(tmp);
    }
  }
  else {
    for(iter=orlist.begin();iter!=orlist.end();++iter)
      for(iter2=b2->orlist.begin();iter2!=b2->orlist.end();++iter2) {
	tmp = (DisjointPattern *)(*iter)->doAnd(*iter2,sa);
	newlist.push_back(tmp);
      }
  }
  tmpor = new OrPattern(newlist);
  return tmpor;
}

Pattern *OrPattern::commonSubPattern(const Pattern *b,int4 sa) const

{
  vector<DisjointPattern *>::const_iterator iter;
  Pattern *res,*next;

  iter = orlist.begin();
  res = (*iter)->commonSubPattern(b,sa);
  iter++;

  if (sa > 0)
    sa = 0;
  while(iter!=orlist.end()) {
    next = (*iter)->commonSubPattern(res,sa);
    delete res;
    res = next;
    ++iter;
  }
  return res;
}

Pattern *OrPattern::doOr(const Pattern *b,int4 sa) const

{
  const OrPattern *b2 = dynamic_cast<const OrPattern *>(b);
  vector<DisjointPattern *> newlist;
  vector<DisjointPattern *>::const_iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter)
    newlist.push_back((DisjointPattern *)(*iter)->simplifyClone());
  if (sa < 0)
    for(iter=orlist.begin();iter!=orlist.end();++iter)
      (*iter)->shiftInstruction(-sa);

  if (b2 == (const OrPattern *)0)
    newlist.push_back((DisjointPattern *)b->simplifyClone());
  else {
    for(iter=b2->orlist.begin();iter!=b2->orlist.end();++iter)
      newlist.push_back((DisjointPattern *)(*iter)->simplifyClone());
  }
  if (sa > 0)
    for(int4 i=0;i<newlist.size();++i)
      newlist[i]->shiftInstruction(sa);

  OrPattern *tmpor = new OrPattern(newlist);
  return tmpor;
}

Pattern *OrPattern::simplifyClone(void) const

{				// Look for alwaysTrue eliminate alwaysFalse
  vector<DisjointPattern *>::const_iterator iter;

  for(iter=orlist.begin();iter!=orlist.end();++iter) // Look for alwaysTrue
    if ((*iter)->alwaysTrue())
      return new InstructionPattern(true);

  vector<DisjointPattern *> newlist;
  for(iter=orlist.begin();iter!=orlist.end();++iter) // Look for alwaysFalse
    if (!(*iter)->alwaysFalse())
      newlist.push_back((DisjointPattern *)(*iter)->simplifyClone());
  
  if (newlist.empty())
    return new InstructionPattern(false);
  else if (newlist.size() == 1)
    return newlist[0];
  return new OrPattern(newlist);
}

void OrPattern::saveXml(ostream &s) const

{
  s << "<or_pat>\n";
  for(int4 i=0;i<orlist.size();++i)
    orlist[i]->saveXml(s);
  s << "</or_pat>\n";
}

void OrPattern::restoreXml(const Element *el)

{
  const List &list(el->getChildren());
  List::const_iterator iter;
  iter = list.begin();
  while(iter != list.end()) {
    DisjointPattern *pat = DisjointPattern::restoreDisjoint(*iter);
    orlist.push_back(pat);
    ++iter;
  }
}