// File: jstaursky[P]hutch/src/Sleigh/slgh_compile.cc
// Function: SleighCompile::expandMacros(ConstructTpl*, std::vector<ConstructTpl*, std::allocator<ConstructTpl*>> const&)
bool SleighCompile::expandMacros(ConstructTpl *ctpl,const vector<ConstructTpl *> &macrotable)

{
  vector<OpTpl *> newvec;
  vector<OpTpl *>::const_iterator iter;
  OpTpl *op;
  
  for(iter=ctpl->getOpvec().begin();iter!=ctpl->getOpvec().end();++iter) {
    op = *iter;
    if (op->getOpcode() == MACROBUILD) {
      MacroBuilder builder(this,newvec,ctpl->numLabels());
      int4 index = op->getIn(0)->getOffset().getReal();
      if (index >= macrotable.size())
	return false;
      builder.setMacroOp(op);
      ConstructTpl *macro_tpl = macrotable[index];
      builder.build(macro_tpl,-1);
      ctpl->setNumLabels( ctpl->numLabels() + macro_tpl->numLabels() );
      delete op;		// Throw away the place holder op
      if (builder.hasError())
	return false;
    }
    else
      newvec.push_back(op);
  }
  ctpl->setOpvec(newvec);
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3f710
leaq -0x40(%rbp), %rdi
callq 0x3fd60
movq -0x18(%rbp), %rdi
callq 0x3fd80
movq %rax, -0xc8(%rbp)
jmp 0x66c96
movq -0xc8(%rbp), %rdi
callq 0x3fda0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3fd80
movq %rax, -0xd8(%rbp)
jmp 0x66cc0
movq -0xd8(%rbp), %rdi
callq 0x3fe10
movq %rax, -0x68(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3fdd0
testb $0x1, %al
jne 0x66ce6
jmp 0x66f29
leaq -0x40(%rbp), %rdi
callq 0x3fe40
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3fd40
movl %eax, -0xdc(%rbp)
jmp 0x66d07
movl -0xdc(%rbp), %eax
cmpl $0x40, %eax
jne 0x66f08
movq -0x18(%rbp), %rdi
callq 0x4e9b0
movl %eax, -0xe0(%rbp)
jmp 0x66d27
movl -0xe0(%rbp), %ecx
movq -0xd0(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x6c750
jmp 0x66d46
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x3fce0
movq %rax, -0xe8(%rbp)
jmp 0x66d5a
movq -0xe8(%rbp), %rdi
callq 0x3fca0
movq %rax, -0xf0(%rbp)
jmp 0x66d6f
movq -0xf0(%rbp), %rdi
callq 0x3fc20
movq %rax, -0xf8(%rbp)
jmp 0x66d84
movq -0xf8(%rbp), %rax
movl %eax, -0xb4(%rbp)
movslq -0xb4(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x20(%rbp), %rdi
callq 0x6c460
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jb 0x66df8
movb $0x0, -0x1(%rbp)
movl $0x1, -0xb8(%rbp)
jmp 0x66eee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x66f5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x6c810
jmp 0x66f5d
movq -0x48(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x604e0
jmp 0x66e0a
movq -0x20(%rbp), %rdi
movslq -0xb4(%rbp), %rsi
callq 0x6c7b0
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4dd60
jmp 0x66e3e
movq -0x18(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x4e9b0
movl %eax, -0x104(%rbp)
jmp 0x66e56
movq -0xc0(%rbp), %rdi
callq 0x4e9b0
movl %eax, -0x114(%rbp)
jmp 0x66e6a
movq -0x110(%rbp), %rdi
movl -0x114(%rbp), %eax
movl -0x104(%rbp), %esi
addl %eax, %esi
callq 0x6c7d0
jmp 0x66e86
movq -0x48(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
je 0x66eb4
movq -0x120(%rbp), %rdi
callq 0x4c740
movq -0x120(%rbp), %rdi
movl $0x28, %esi
callq 0x11450
leaq -0xb0(%rbp), %rdi
callq 0x6c7f0
movb %al, -0x121(%rbp)
jmp 0x66ec8
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0x66ed4
jmp 0x66ee4
movb $0x0, -0x1(%rbp)
movl $0x1, -0xb8(%rbp)
jmp 0x66eee
movl $0x0, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x6c810
movl -0xb8(%rbp), %eax
testl %eax, %eax
jne 0x66f46
jmp 0x66f06
jmp 0x66f19
leaq -0x38(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3f730
jmp 0x66f17
jmp 0x66f19
jmp 0x66f1b
leaq -0x40(%rbp), %rdi
callq 0x3fe60
jmp 0x66cae
movq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x6c860
jmp 0x66f38
movb $0x1, -0x1(%rbp)
movl $0x1, -0xb8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3f800
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x3f800
movq -0x58(%rbp), %rdi
callq 0x118b0
nop