// File: jstaursky[P]hutch/src/Sleigh/slghsymbol.cc
// Function: Constructor::buildPattern(std::ostream&)
TokenPattern *Constructor::buildPattern(ostream &s)

{
  if (pattern != (TokenPattern *)0) return pattern; // Already built

  pattern = new TokenPattern();
  vector<TokenPattern> oppattern;
  bool recursion = false;
				// Generate pattern for each operand, store in oppattern
  for(int4 i=0;i<operands.size();++i) {
    OperandSymbol *sym = operands[i];
    TripleSymbol *triple = sym->getDefiningSymbol();
    PatternExpression *defexp = sym->getDefiningExpression();
    if (triple != (TripleSymbol *)0) {
      SubtableSymbol *subsym = dynamic_cast<SubtableSymbol *>(triple);
      if (subsym != (SubtableSymbol *)0) {
	if (subsym->isBeingBuilt()) { // Detected recursion
	  if (recursion) {
	    throw SleighError("Illegal recursion");
	  }
				// We should also check that recursion is rightmost extreme
	  recursion = true;
	  oppattern.push_back(TokenPattern());
	}
	else
	  oppattern.push_back(*subsym->buildPattern(s));
      }
      else
	oppattern.push_back(triple->getPatternExpression()->genMinPattern(oppattern));
    }
    else if (defexp != (PatternExpression *)0)
      oppattern.push_back(defexp->genMinPattern(oppattern));
    else {
      throw SleighError(sym->getName()+": operand is undefined");
    }
    TokenPattern &sympat( oppattern.back() );
    sym->minimumlength = sympat.getMinimumLength();
    if (sympat.getLeftEllipsis() || sympat.getRightEllipsis())
      sym->setVariableLength();
  }

  if (pateq == (PatternEquation *)0)
    throw SleighError("Missing equation");

				// Build the entire pattern
  pateq->genPattern(oppattern);
  *pattern = pateq->getTokenPattern();
  if (pattern->alwaysFalse())
    throw SleighError("Impossible pattern");
  if (recursion)
    pattern->setRightEllipsis(true);
  minimumlength = pattern->getMinimumLength(); // Get length of the pattern in bytes

				// Resolve offsets of the operands
  OperandResolve resolve(operands);
  if (!pateq->resolveOperandLeft(resolve))
    throw SleighError("Unable to resolve operand offsets");

  for(int4 i=0;i<operands.size();++i) { // Unravel relative offsets to absolute (if possible)
    int4 base,offset;
    OperandSymbol *sym = operands[i];
    if (sym->isOffsetIrrelevant()) {
      sym->offsetbase = -1;
      sym->reloffset = 0;
      continue;
    }
    base = sym->offsetbase;
    offset = sym->reloffset;
    while(base >= 0) {
      sym = operands[base];
      if (sym->isVariableLength()) break; // Cannot resolve to absolute
      base = sym->offsetbase;
      offset += sym->getMinimumLength();
      offset += sym->reloffset;
      if (base < 0) {
	operands[i]->offsetbase = base;
	operands[i]->reloffset = offset;
      }
    }
  }

  // Make sure context expressions are valid
  for(int4 i=0;i<context.size();++i)
    context[i]->validate();

  orderOperands();		// Order the operands based on offset dependency
  return pattern;
}

// Assembly:
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, 0x2b8(%rsp)
movq %rsi, 0x2b0(%rsp)
movq 0x2b8(%rsp), %rax
movq %rax, 0xe0(%rsp)
cmpq $0x0, (%rax)
je 0x1480a5
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x2c0(%rsp)
jmp 0x148cde
movl $0x28, %edi
callq 0xd8380
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xd8(%rsp)
callq 0xdc820
jmp 0x1480c4
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
movq %rcx, (%rax)
leaq 0x288(%rsp), %rdi
callq 0xd6920
movb $0x0, 0x287(%rsp)
movl $0x0, 0x280(%rsp)
movq 0xe0(%rsp), %rdi
movslq 0x280(%rsp), %rax
movq %rax, 0xd0(%rsp)
addq $0x18, %rdi
callq 0xd98e0
movq %rax, %rcx
movq 0xd0(%rsp), %rax
cmpq %rcx, %rax
jae 0x14869c
movq 0xe0(%rsp), %rdi
addq $0x18, %rdi
movslq 0x280(%rsp), %rsi
callq 0xdc620
movq (%rax), %rax
movq %rax, 0x278(%rsp)
movq 0x278(%rsp), %rdi
callq 0xdf550
movq %rax, 0xc8(%rsp)
jmp 0x148167
movq 0xc8(%rsp), %rax
movq %rax, 0x270(%rsp)
movq 0x278(%rsp), %rdi
callq 0xd7220
movq %rax, 0xc0(%rsp)
jmp 0x14818e
movq 0xc0(%rsp), %rax
movq %rax, 0x268(%rsp)
cmpq $0x0, 0x270(%rsp)
je 0x1484ac
movq 0x270(%rsp), %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, %rax
je 0x148230
movq 0xb8(%rsp), %rdi
movq 0x7fd0e(%rip), %rsi     # 0x1c7ee0
movq 0x7fd0f(%rip), %rdx     # 0x1c7ee8
xorl %eax, %eax
movl %eax, %ecx
callq 0xd99a0
movq %rax, 0xb0(%rsp)
jmp 0x14823c
movq 0xd8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
movl $0x28, %esi
callq 0xd8620
jmp 0x148cee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x148ccf
xorl %eax, %eax
movq %rax, 0xb0(%rsp)
jmp 0x14823c
movq 0xb0(%rsp), %rax
movq %rax, 0x260(%rsp)
cmpq $0x0, 0x260(%rsp)
je 0x14841d
movq 0x260(%rsp), %rdi
callq 0xde870
movb %al, 0xaf(%rsp)
jmp 0x148271
movb 0xaf(%rsp), %al
testb $0x1, %al
jne 0x148281
jmp 0x1483e0
testb $0x1, 0x287(%rsp)
je 0x14837d
movb $0x1, 0x23e(%rsp)
movl $0x20, %edi
callq 0xd2b40
movq %rax, 0x98(%rsp)
leaq 0x23f(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0xe0c80
movq 0xa0(%rsp), %rdx
leaq 0x4114a(%rip), %rsi     # 0x189417
leaq 0x240(%rsp), %rdi
callq 0xe00a0
jmp 0x1482dc
movq 0x98(%rsp), %rdi
leaq 0x240(%rsp), %rsi
callq 0xd06e0
jmp 0x1482f3
movq 0x98(%rsp), %rdi
movb $0x0, 0x23e(%rsp)
movq 0x7fb5e(%rip), %rsi     # 0x1c7e68
movq 0x7f70f(%rip), %rdx     # 0x1c7a20
callq 0xe01f0
jmp 0x148cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x148352
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x240(%rsp), %rdi
callq 0xd4550
leaq 0x23f(%rsp), %rdi
callq 0xd8ec0
testb $0x1, 0x23e(%rsp)
jne 0x14836b
jmp 0x148378
movq 0x98(%rsp), %rdi
callq 0xd5400
jmp 0x148ccf
movb $0x1, 0x287(%rsp)
leaq 0x210(%rsp), %rdi
callq 0xdc820
jmp 0x148394
leaq 0x288(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xd5aa0
jmp 0x1483ab
leaq 0x210(%rsp), %rdi
callq 0xd2580
jmp 0x148418
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x210(%rsp), %rdi
callq 0xd2580
jmp 0x148ccf
movq 0x260(%rsp), %rdi
movq 0x2b0(%rsp), %rsi
callq 0xdf440
movq %rax, 0x90(%rsp)
jmp 0x1483ff
movq 0x90(%rsp), %rsi
leaq 0x288(%rsp), %rdi
callq 0xdbd70
jmp 0x148416
jmp 0x148418
jmp 0x1484a7
movq 0x270(%rsp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
movq %rax, 0x88(%rsp)
jmp 0x148438
movq 0x88(%rsp), %rsi
movq (%rsi), %rax
movq 0x18(%rax), %rax
leaq 0x1e8(%rsp), %rdi
leaq 0x288(%rsp), %rdx
callq *%rax
jmp 0x14845b
leaq 0x288(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0xd5aa0
jmp 0x148472
leaq 0x1e8(%rsp), %rdi
callq 0xd2580
jmp 0x1484a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0xd2580
jmp 0x148ccf
jmp 0x148602
cmpq $0x0, 0x268(%rsp)
je 0x148529
movq 0x268(%rsp), %rsi
movq (%rsi), %rax
movq 0x18(%rax), %rax
leaq 0x1c0(%rsp), %rdi
leaq 0x288(%rsp), %rdx
callq *%rax
jmp 0x1484da
leaq 0x288(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0xd5aa0
jmp 0x1484f1
leaq 0x1c0(%rsp), %rdi
callq 0xd2580
jmp 0x148600
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0xd2580
jmp 0x148ccf
movb $0x1, 0x19f(%rsp)
movl $0x20, %edi
callq 0xd2b40
movq %rax, 0x78(%rsp)
movq 0x278(%rsp), %rdi
callq 0xd1f90
movq %rax, 0x80(%rsp)
jmp 0x148557
movq 0x80(%rsp), %rsi
leaq 0x40ec3(%rip), %rdx     # 0x189429
leaq 0x1a0(%rsp), %rdi
callq 0xdb370
jmp 0x148575
movq 0x78(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xd06e0
jmp 0x148589
movq 0x78(%rsp), %rdi
movb $0x0, 0x19f(%rsp)
movq 0x7f8cb(%rip), %rsi     # 0x1c7e68
movq 0x7f47c(%rip), %rdx     # 0x1c7a20
callq 0xe01f0
jmp 0x148cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x1485e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0xd4550
testb $0x1, 0x19f(%rsp)
jne 0x1485f1
jmp 0x1485fb
movq 0x78(%rsp), %rdi
callq 0xd5400
jmp 0x148ccf
jmp 0x148602
leaq 0x288(%rsp), %rdi
callq 0xdee50
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rdi
callq 0xe1780
movl %eax, 0x74(%rsp)
jmp 0x14862a
movl 0x74(%rsp), %ecx
movq 0x278(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x190(%rsp), %rdi
callq 0xd84c0
movb %al, 0x73(%rsp)
jmp 0x14864c
movb 0x73(%rsp), %al
testb $0x1, %al
jne 0x148673
jmp 0x148656
movq 0x190(%rsp), %rdi
callq 0xddf00
movb %al, 0x72(%rsp)
jmp 0x148669
movb 0x72(%rsp), %al
testb $0x1, %al
jne 0x148673
jmp 0x148684
movq 0x278(%rsp), %rdi
callq 0xd4160
jmp 0x148682
jmp 0x148684
jmp 0x148686
movl 0x280(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x280(%rsp)
jmp 0x1480f7
movq 0xe0(%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x14878b
movb $0x1, 0x16e(%rsp)
movl $0x20, %edi
callq 0xd2b40
movq %rax, 0x60(%rsp)
leaq 0x16f(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0xe0c80
movq 0x68(%rsp), %rdx
leaq 0x40d5c(%rip), %rsi     # 0x189440
leaq 0x170(%rsp), %rdi
callq 0xe00a0
jmp 0x1486f3
movq 0x60(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0xd06e0
jmp 0x148707
movq 0x60(%rsp), %rdi
movb $0x0, 0x16e(%rsp)
movq 0x7f74d(%rip), %rsi     # 0x1c7e68
movq 0x7f2fe(%rip), %rdx     # 0x1c7a20
callq 0xe01f0
jmp 0x148cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x148763
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x170(%rsp), %rdi
callq 0xd4550
leaq 0x16f(%rsp), %rdi
callq 0xd8ec0
testb $0x1, 0x16e(%rsp)
jne 0x14877c
jmp 0x148786
movq 0x60(%rsp), %rdi
callq 0xd5400
jmp 0x148ccf
movq 0xe0(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x288(%rsp), %rsi
callq *%rax
jmp 0x1487aa
movq 0xe0(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0xe17d0
movq %rax, 0x58(%rsp)
jmp 0x1487c2
movq 0x58(%rsp), %rsi
movq 0xe0(%rsp), %rax
movq (%rax), %rdi
callq 0xdd160
jmp 0x1487d9
movq 0xe0(%rsp), %rax
movq (%rax), %rdi
callq 0xd15a0
movb %al, 0x57(%rsp)
jmp 0x1487ef
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x1487fc
jmp 0x1488d8
movb $0x1, 0x146(%rsp)
movl $0x20, %edi
callq 0xd2b40
movq %rax, 0x40(%rsp)
leaq 0x147(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0xe0c80
movq 0x48(%rsp), %rdx
leaq 0x40c20(%rip), %rsi     # 0x189451
leaq 0x148(%rsp), %rdi
callq 0xe00a0
jmp 0x148840
movq 0x40(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xd06e0
jmp 0x148854
movq 0x40(%rsp), %rdi
movb $0x0, 0x146(%rsp)
movq 0x7f600(%rip), %rsi     # 0x1c7e68
movq 0x7f1b1(%rip), %rdx     # 0x1c7a20
callq 0xe01f0
jmp 0x148cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x1488b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x148(%rsp), %rdi
callq 0xd4550
leaq 0x147(%rsp), %rdi
callq 0xd8ec0
testb $0x1, 0x146(%rsp)
jne 0x1488c9
jmp 0x1488d3
movq 0x40(%rsp), %rdi
callq 0xd5400
jmp 0x148ccf
testb $0x1, 0x287(%rsp)
je 0x1488fb
movq 0xe0(%rsp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0xdda10
jmp 0x1488f9
jmp 0x1488fb
movq 0xe0(%rsp), %rax
movq (%rax), %rdi
callq 0xe1780
movl %eax, 0x3c(%rsp)
jmp 0x148911
movq 0xe0(%rsp), %rsi
movl 0x3c(%rsp), %eax
movl %eax, 0x80(%rsi)
addq $0x18, %rsi
leaq 0x128(%rsp), %rdi
callq 0xd6210
jmp 0x148936
movq 0xe0(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
leaq 0x128(%rsp), %rsi
callq *%rax
movb %al, 0x3b(%rsp)
jmp 0x148959
movb 0x3b(%rsp), %al
testb $0x1, %al
jne 0x148a43
jmp 0x148967
movb $0x1, 0x106(%rsp)
movl $0x20, %edi
callq 0xd2b40
movq %rax, 0x28(%rsp)
leaq 0x107(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xe0c80
movq 0x30(%rsp), %rdx
leaq 0x40ac8(%rip), %rsi     # 0x189464
leaq 0x108(%rsp), %rdi
callq 0xe00a0
jmp 0x1489ab
movq 0x28(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xd06e0
jmp 0x1489bf
movq 0x28(%rsp), %rdi
movb $0x0, 0x106(%rsp)
movq 0x7f495(%rip), %rsi     # 0x1c7e68
movq 0x7f046(%rip), %rdx     # 0x1c7a20
callq 0xe01f0
jmp 0x148cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
jmp 0x148a1b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2a8(%rsp)
movl %eax, 0x2a4(%rsp)
leaq 0x108(%rsp), %rdi
callq 0xd4550
leaq 0x107(%rsp), %rdi
callq 0xd8ec0
testb $0x1, 0x106(%rsp)
jne 0x148a34
jmp 0x148a3e
movq 0x28(%rsp), %rdi
callq 0xd5400
jmp 0x148ccf
movl $0x0, 0x100(%rsp)
movq 0xe0(%rsp), %rdi
movslq 0x100(%rsp), %rax
movq %rax, 0x20(%rsp)
addq $0x18, %rdi
callq 0xd98e0
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x148c2c
movq 0xe0(%rsp), %rdi
addq $0x18, %rdi
movslq 0x100(%rsp), %rsi
callq 0xdc620
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xd8370
movb %al, 0x1f(%rsp)
jmp 0x148ab4
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x148abe
jmp 0x148ae1
movq 0xf0(%rsp), %rax
movl $0xffffffff, 0x34(%rax) # imm = 0xFFFFFFFF
movq 0xf0(%rsp), %rax
movl $0x0, 0x30(%rax)
jmp 0x148c16
movq 0xf0(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, 0xfc(%rsp)
movq 0xf0(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, 0xf8(%rsp)
cmpl $0x0, 0xfc(%rsp)
jl 0x148c14
movq 0xe0(%rsp), %rdi
addq $0x18, %rdi
movslq 0xfc(%rsp), %rsi
callq 0xdc620
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xd7090
movb %al, 0x1e(%rsp)
jmp 0x148b4a
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x148b54
jmp 0x148b59
jmp 0x148c14
movq 0xf0(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, 0xfc(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xe0c10
movl %eax, 0x18(%rsp)
jmp 0x148b7e
movl 0x18(%rsp), %eax
addl 0xf8(%rsp), %eax
movl %eax, 0xf8(%rsp)
movq 0xf0(%rsp), %rax
movl 0x30(%rax), %eax
addl 0xf8(%rsp), %eax
movl %eax, 0xf8(%rsp)
cmpl $0x0, 0xfc(%rsp)
jge 0x148c0f
movq 0xe0(%rsp), %rdi
movl 0xfc(%rsp), %eax
movl %eax, 0x10(%rsp)
addq $0x18, %rdi
movslq 0x100(%rsp), %rsi
callq 0xdc620
movl 0x10(%rsp), %ecx
movq 0xe0(%rsp), %rdi
movq (%rax), %rax
movl %ecx, 0x34(%rax)
movl 0xf8(%rsp), %eax
movl %eax, 0x14(%rsp)
addq $0x18, %rdi
movslq 0x100(%rsp), %rsi
callq 0xdc620
movl 0x14(%rsp), %ecx
movq (%rax), %rax
movl %ecx, 0x30(%rax)
jmp 0x148b05
jmp 0x148c16
movl 0x100(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x100(%rsp)
jmp 0x148a4e
movl $0x0, 0xec(%rsp)
movq 0xe0(%rsp), %rdi
movslq 0xec(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x48, %rdi
callq 0xd27b0
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x148c9e
movq 0xe0(%rsp), %rdi
addq $0x48, %rdi
movslq 0xec(%rsp), %rsi
callq 0xdd800
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x148c89
jmp 0x148c8b
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x148c37
movq 0xe0(%rsp), %rdi
callq 0xdca00
jmp 0x148cad
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x2c0(%rsp)
leaq 0x288(%rsp), %rdi
callq 0xdfe30
jmp 0x148cde
leaq 0x288(%rsp), %rdi
callq 0xdfe30
jmp 0x148cee
movq 0x2c0(%rsp), %rax
addq $0x2c8, %rsp            # imm = 0x2C8
retq
movq 0x2a8(%rsp), %rdi
callq 0xe0be0
nopl (%rax,%rax)