// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq (%rdi), %r14
testq %r14, %r14
jne 0x73d62
movq %rdi, %rbx
movq %rsi, 0x48(%rsp)
pushq $0x28
popq %rdi
callq 0x4f5d0
movq %rax, %r14
movq %rax, %rdi
callq 0x50750
movq %r14, (%rbx)
leaq 0x50(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
leaq 0x18(%rbx), %rax
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
leaq 0x18(%rsp), %r12
movq $0x0, 0x10(%rsp)
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0x73c3f
movq (%rax,%rbp,8), %r14
movq 0x48(%r14), %r13
testq %r13, %r13
je 0x73ba6
movq %r13, %rdi
movq 0x3b38e(%rip), %rsi     # 0xaeee8
movq 0x3b38f(%rip), %rdx     # 0xaeef0
xorl %ecx, %ecx
callq 0x4fa60
testq %rax, %rax
je 0x73bcc
cmpb $0x1, 0x38(%rax)
jne 0x73bfa
testb $0x1, 0x10(%rsp)
jne 0x73dc7
movq %r12, %rdi
callq 0x50750
movq %r15, %rdi
movq %r12, %rsi
callq 0x51440
movq %r12, %rdi
callq 0x4e060
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x73c12
movq 0x50(%r14), %rsi
testq %rsi, %rsi
je 0x73d77
movq (%rsi), %rax
movq %r12, %rdi
movq %r15, %rdx
callq *0x18(%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x51440
jmp 0x73bf0
movq (%r13), %rax
movq %r13, %rdi
callq *0x38(%rax)
movq (%rax), %rcx
movq %r12, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0x18(%rcx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x51440
movq %r12, %rdi
callq 0x4e060
jmp 0x73c12
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x51270
movq %r15, %rdi
movq %rax, %rsi
callq 0x50440
movq 0x58(%rsp), %r13
leaq -0x28(%r13), %rdi
callq 0x51b80
movl %eax, 0x38(%r14)
cmpb $0x0, -0x8(%r13)
jne 0x73c32
cmpb $0x1, -0x7(%r13)
jne 0x73c37
orb $0x4, 0x58(%r14)
incq %rbp
jmp 0x73b2b
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x73e15
movq (%rdi), %rax
leaq 0x50(%rsp), %rsi
callq *0x10(%rax)
movq (%rbx), %rdi
movq 0x10(%rbx), %rsi
addq $0x10, %rsi
callq 0x50980
movq (%rbx), %rax
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
testb %al, %al
jne 0x73e63
movq (%rbx), %rdi
testb $0x1, 0x10(%rsp)
je 0x73c89
movb $0x1, 0x21(%rdi)
callq 0x51b80
movl %eax, 0x80(%rbx)
leaq 0x70(%rsp), %rsi
movq 0x40(%rsp), %rax
movq %rax, (%rsi)
movaps 0x14368(%rip), %xmm0    # 0x88010
movups %xmm0, 0x8(%rsi)
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
testb %al, %al
je 0x73eae
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorl %edx, %edx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rdx
je 0x73d27
movq (%rax,%rdx,8), %rdi
testb $0x2, 0x58(%rdi)
jne 0x73d1e
movl 0x30(%rdi), %r8d
movl 0x34(%rdi), %r9d
testl %r9d, %r9d
js 0x73d22
movl %r9d, %r9d
movq (%rax,%r9,8), %r10
testb $0x4, 0x58(%r10)
jne 0x73d22
addl 0x38(%r10), %r8d
movl 0x34(%r10), %r9d
addl 0x30(%r10), %r8d
testl %r9d, %r9d
jns 0x73cf0
movl %r9d, 0x34(%rdi)
movl %r8d, 0x30(%rdi)
jmp 0x73cf0
movq %rsi, 0x30(%rdi)
incq %rdx
jmp 0x73cd9
xorl %r14d, %r14d
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x73d4d
movq (%rax,%r14,8), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
incq %r14
jmp 0x73d2a
movq %rbx, %rdi
callq 0x50810
movq (%rbx), %r14
leaq 0x50(%rsp), %rdi
callq 0x51570
movq %r14, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %r15
addq $0x8, %r14
leaq 0x147cc(%rip), %rdx     # 0x88559
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x50160
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x3b0d5(%rip), %rsi     # 0xaee88
movq 0x3ae7e(%rip), %rdx     # 0xaec38
movq %r15, %rdi
callq 0x51630
jmp 0x73ef7
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %r14
leaq 0x1476e(%rip), %rsi     # 0x88547
leaq 0x18(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x515e0
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x3b087(%rip), %rsi     # 0xaee88
movq 0x3ae30(%rip), %rdx     # 0xaec38
movq %r14, %rdi
callq 0x51630
jmp 0x73ef7
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %r14
leaq 0x14749(%rip), %rsi     # 0x88570
leaq 0x18(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x515e0
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x3b039(%rip), %rsi     # 0xaee88
movq 0x3ade2(%rip), %rdx     # 0xaec38
movq %r14, %rdi
callq 0x51630
jmp 0x73ef7
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %r14
leaq 0x1470c(%rip), %rsi     # 0x88581
leaq 0x18(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x515e0
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x3afeb(%rip), %rsi     # 0xaee88
movq 0x3ad94(%rip), %rdx     # 0xaec38
movq %r14, %rdi
callq 0x51630
jmp 0x73ef7
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %r14
leaq 0x146d4(%rip), %rsi     # 0x88594
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x515e0
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x3afa0(%rip), %rsi     # 0xaee88
movq 0x3ad49(%rip), %rdx     # 0xaec38
movq %r14, %rdi
callq 0x51630
jmp 0x73f15
jmp 0x73f29
jmp 0x73f15
jmp 0x73f29
jmp 0x73f15
jmp 0x73f29
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x4f670
jmp 0x73f7e
jmp 0x73f71
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4e020
testb %bpl, %bpl
jne 0x73f2c
jmp 0x73f74
movq %rax, %rbx
movq %r14, %rdi
jmp 0x73f4d
jmp 0x73f71
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4e020
testb %bpl, %bpl
jne 0x73f4a
jmp 0x73f74
movq %rax, %rbx
movq %r15, %rdi
callq 0x4ea60
jmp 0x73f74
jmp 0x73f5a
jmp 0x73f71
jmp 0x73f5a
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4e060
jmp 0x73f74
jmp 0x73f71
jmp 0x73f71
jmp 0x73f71
jmp 0x73f71
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x51570
movq %rbx, %rdi
callq 0x518c0