// File: jstaursky[P]hutch/src/Sleigh/slghsymbol.cc
// Function: DecisionNode::orderPatterns(DecisionProperties&)
void DecisionNode::orderPatterns(DecisionProperties &props)

{
  // This is a tricky routine.  When this routine is called, the patterns remaining in the
  // the decision node can no longer be distinguished by examining additional bits. The basic
  // idea here is that the patterns should be ordered so that the most specialized should come
  // first in the list. Pattern 1 is a specialization of pattern 2, if the set of instructions
  // matching 1 is contained in the set matching 2.  So in the simplest case, the pattern order
  // should represent a strict nesting.  Unfortunately, there are many potential situations where
  // patterns don't necessarily nest.
  //   1) An "or" of two patterns.  This can be an explicit '|' operator in the Constructor, in
  //      which case this can be detected because the two patterns point to the same constructor
  //      But the "or" can be implied across two constructors that do the same thing.  This should
  //      probably be flagged as an error except in the following case.
  //   2) Two patterns aren't properly nested, but they are "resolved" by a third pattern which
  //      covers the intersection of the first two patterns.  Sometimes its easier to specify
  //      three cases that need to be distinguished in this way.
  //   3) Recursive constructors that use a "guard" context bit.  The guard bit is used to prevent
  //      the recursive constructor from matching repeatedly, but it's too much work to put a
  //      constraint an the bit for every other pattern.
  //   4) Other situations where the ability to distinguish between constructors is hidden in
  //      the subconstructors.
  // This routine can determine if an intersection results from case 1) or case 2)
  int4 i,j,k;
  vector<pair<DisjointPattern *,Constructor *> > newlist;
  vector<pair<DisjointPattern *,Constructor *> > conflictlist;

  // Check for identical patterns
  for(i=0;i<list.size();++i) {
    for(j=0;j<i;++j) {
      DisjointPattern *ipat = list[i].first;
      DisjointPattern *jpat = list[j].first;
      if (ipat->identical(jpat))
	props.identicalPattern(list[i].second,list[j].second);
    }
  }

  newlist = list;
  for(i=0;i<list.size();++i) {
    for(j=0;j<i;++j) {
      DisjointPattern *ipat = newlist[i].first;
      DisjointPattern *jpat = list[j].first;
      if (ipat->specializes(jpat))
	break;
      if (!jpat->specializes(ipat)) { // We have a potential conflict
	Constructor *iconst = newlist[i].second;
	Constructor *jconst = list[j].second;
	if (iconst == jconst) { // This is an OR in the pattern for ONE constructor
	  // So there is no conflict
	}
	else {			// A true conflict that needs to be resolved
	  conflictlist.push_back(pair<DisjointPattern *,Constructor *>(ipat,iconst));
	  conflictlist.push_back(pair<DisjointPattern *,Constructor *>(jpat,jconst));
	}
      }
    }
    for(k=i-1;k>=j;--k)
      list[k+1] = list[k];
    list[j] = newlist[i];
  }
  
  // Check if intersection patterns are present, which resolve conflicts
  for(i=0;i<conflictlist.size();i+=2) {
    DisjointPattern *pat1,*pat2;
    Constructor *const1,*const2;
    pat1 = conflictlist[i].first;
    const1 = conflictlist[i].second;
    pat2 = conflictlist[i+1].first;
    const2 = conflictlist[i+1].second;
    bool resolved = false;
    for(j=0;j<list.size();++j) {
      DisjointPattern *tpat = list[j].first;
      Constructor *tconst = list[j].second;
      if ((tpat == pat1)&&(tconst==const1)) break; // Ran out of possible specializations
      if ((tpat == pat2)&&(tconst==const2)) break;
      if (tpat->resolvesIntersect(pat1,pat2)) {
	resolved = true;
	break;
      }
    }
    if (!resolved)
      props.conflictingPattern(const1,const2);
  }
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x140(%rsp)
movq %rsi, 0x138(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x110(%rsp), %rdi
callq 0xdbf00
leaq 0xf8(%rsp), %rdi
callq 0xdbf00
movl $0x0, 0x134(%rsp)
movq 0x58(%rsp), %rdi
movslq 0x134(%rsp), %rax
movq %rax, 0x50(%rsp)
callq 0xd5590
movq %rax, %rcx
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
jae 0x14baf6
movl $0x0, 0x130(%rsp)
movl 0x130(%rsp), %eax
cmpl 0x134(%rsp), %eax
jge 0x14bade
movq 0x58(%rsp), %rdi
movslq 0x134(%rsp), %rsi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
callq 0xd20c0
movb %al, 0x4f(%rsp)
jmp 0x14ba35
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x14ba42
jmp 0x14bac6
movq 0x58(%rsp), %rdi
movq 0x138(%rsp), %rax
movq %rax, 0x38(%rsp)
movslq 0x134(%rsp), %rsi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x8(%rax), %rdx
callq 0xe2160
jmp 0x14ba91
jmp 0x14bac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0xdfb60
leaq 0x110(%rsp), %rdi
callq 0xdfb60
jmp 0x14bf9d
jmp 0x14bac8
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0x14b9cc
jmp 0x14bae0
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x14b999
movq 0x58(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xd68c0
jmp 0x14bb0a
movl $0x0, 0x134(%rsp)
movq 0x58(%rsp), %rdi
movslq 0x134(%rsp), %rax
movq %rax, 0x30(%rsp)
callq 0xd5590
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0x14bd8c
movl $0x0, 0x130(%rsp)
movl 0x130(%rsp), %eax
cmpl 0x134(%rsp), %eax
jge 0x14bcc7
movslq 0x134(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xd0(%rsp)
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq (%rax), %rax
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0xdb780
movb %al, 0x2f(%rsp)
jmp 0x14bbb4
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x14bbbe
jmp 0x14bbc3
jmp 0x14bcc7
movq 0xc8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0xdb780
movb %al, 0x2e(%rsp)
jmp 0x14bbde
movb 0x2e(%rsp), %al
testb $0x1, %al
jne 0x14bcaf
jmp 0x14bbec
movslq 0x134(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq 0x8(%rax), %rax
movq %rax, 0xc0(%rsp)
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq 0x8(%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
cmpq 0xb8(%rsp), %rax
jne 0x14bc3f
jmp 0x14bcad
leaq 0xa8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0xcf1c0
jmp 0x14bc5e
leaq 0xf8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xd8660
jmp 0x14bc75
leaq 0x98(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xcf1c0
jmp 0x14bc94
leaq 0xf8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xd8660
jmp 0x14bcab
jmp 0x14bcad
jmp 0x14bcaf
jmp 0x14bcb1
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0x14bb48
movl 0x134(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x12c(%rsp)
movl 0x12c(%rsp), %eax
cmpl 0x130(%rsp), %eax
jl 0x14bd39
movq 0x58(%rsp), %rdi
movslq 0x12c(%rsp), %rsi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq %rax, 0x20(%rsp)
movl 0x12c(%rsp), %eax
incl %eax
movslq %eax, %rsi
callq 0xd9cd0
movq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0xe1320
jmp 0x14bd24
jmp 0x14bd26
movl 0x12c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x12c(%rsp)
jmp 0x14bcd8
movslq 0x134(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq %rax, 0x18(%rsp)
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0xe1320
jmp 0x14bd74
jmp 0x14bd76
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x14bb15
movl $0x0, 0x134(%rsp)
movslq 0x134(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0xd5590
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x14bf7b
movslq 0x134(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0xd9cd0
movq (%rax), %rax
movq %rax, 0x90(%rsp)
movslq 0x134(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0xd9cd0
movq 0x8(%rax), %rax
movq %rax, 0x80(%rsp)
movl 0x134(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq 0xf8(%rsp), %rdi
callq 0xd9cd0
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movl 0x134(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq 0xf8(%rsp), %rdi
callq 0xd9cd0
movq 0x8(%rax), %rax
movq %rax, 0x78(%rsp)
movb $0x0, 0x77(%rsp)
movl $0x0, 0x130(%rsp)
movq 0x58(%rsp), %rdi
movslq 0x130(%rsp), %rax
movq %rax, 0x8(%rsp)
callq 0xd5590
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x14bf3e
movq 0x58(%rsp), %rdi
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq 0x58(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x68(%rsp)
movslq 0x130(%rsp), %rsi
callq 0xd9cd0
movq 0x8(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
cmpq 0x90(%rsp), %rax
jne 0x14bed8
movq 0x60(%rsp), %rax
cmpq 0x80(%rsp), %rax
jne 0x14bed8
jmp 0x14bf3e
movq 0x68(%rsp), %rax
cmpq 0x88(%rsp), %rax
jne 0x14bef5
movq 0x60(%rsp), %rax
cmpq 0x78(%rsp), %rax
jne 0x14bef5
jmp 0x14bf3e
movq 0x68(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdx
callq 0xd1e00
movb %al, 0x7(%rsp)
jmp 0x14bf15
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x14bf1f
jmp 0x14bf26
movb $0x1, 0x77(%rsp)
jmp 0x14bf3e
jmp 0x14bf28
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0x14be5b
testb $0x1, 0x77(%rsp)
jne 0x14bf63
movq 0x138(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %rdx
callq 0xdfd90
jmp 0x14bf61
jmp 0x14bf63
jmp 0x14bf65
movl 0x134(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x134(%rsp)
jmp 0x14bd97
leaq 0xf8(%rsp), %rdi
callq 0xdfb60
leaq 0x110(%rsp), %rdi
callq 0xdfb60
addq $0x148, %rsp            # imm = 0x148
retq
movq 0xe0(%rsp), %rdi
callq 0xe0be0
nopw (%rax,%rax)