// File: /workspace/github_cpp_makefile_star2_O2/jstaursky[P]hutch/src/Sleigh/pcodecompile.cc
// Function: PcodeCompile::createBitRange(SpecificSymbol*, unsigned int, unsigned int)
ExprTree *PcodeCompile::createBitRange(SpecificSymbol *sym,uint4 bitoffset,uint4 numbits)

{ // Create an expression computing the indicated bitrange of sym
  // The result is truncated to the smallest byte size that can
  // contain the indicated number of bits. The result has the
  // desired bits shifted all the way to the right
  string errmsg;
  if (numbits == 0)
    errmsg = "Size of bitrange is zero";
  VarnodeTpl *vn = sym->getVarnode();
  uint4 finalsize = (numbits+7)/8; // Round up to neareast byte size
  uint4 truncshift = 0;
  bool maskneeded = ((numbits%8)!=0);
  bool truncneeded = true;

  // Special case where we can set the size, without invoking
  // a truncation operator
  if ((errmsg.size()==0)&&(bitoffset==0)&&(!maskneeded)) {
    if ((vn->getSpace().getType()==ConstTpl::handle)&&vn->isZeroSize()) {
      vn->setSize(ConstTpl(ConstTpl::real,finalsize));
      ExprTree *res = new ExprTree(vn);
      //      VarnodeTpl *cruft = buildTemporary();
      //      delete cruft;
      return res;
    }
  }

  if (errmsg.size()==0) {
    VarnodeTpl *truncvn = buildTruncatedVarnode(vn,bitoffset,numbits);
    if (truncvn != (VarnodeTpl *)0) { // If we are able to construct a simple truncated varnode
      ExprTree *res = new ExprTree(truncvn); // Return just the varnode as an expression
      delete vn;
      return res;
    }
  }

  if (vn->getSize().getType()==ConstTpl::real) {
    // If we know the size of the input varnode, we can
    // do some immediate checks, and possibly simplify things
    uint4 insize = vn->getSize().getReal();
    if (insize > 0) {
      truncneeded = (finalsize < insize);
      insize *= 8;		// Convert to number of bits
      if ((bitoffset >= insize)||(bitoffset+numbits > insize))
	errmsg = "Bitrange is bad";
      if (maskneeded && ((bitoffset+numbits)==insize))
	maskneeded = false;
    }
  }

  uintb mask = (uintb)2;
  mask = ((mask<<(numbits-1))-1);
  
  if (truncneeded && ((bitoffset % 8)==0)) {
    truncshift = bitoffset/8;
    bitoffset = 0;
  }

  if ((bitoffset==0)&&(!truncneeded)&&(!maskneeded))
    errmsg = "Superfluous bitrange";

  if (maskneeded && (finalsize > 8))
    errmsg = "Illegal masked bitrange producing varnode larger than 64 bits: " + sym->getName();

  ExprTree *res = new ExprTree(vn);

  if (errmsg.size()>0) {	// Check for error condition
    reportError(errmsg);
    return res;
  }

  if (bitoffset !=0)
    appendOp(CPUI_INT_RIGHT,res,bitoffset,4);
  if (truncneeded)
    appendOp(CPUI_SUBPIECE,res,truncshift,4);
  if (maskneeded)
    appendOp(CPUI_INT_AND,res,mask,finalsize);
  force_size(res->outvn,ConstTpl(ConstTpl::real,finalsize),*res->ops);
  return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebx
movl %edx, %r15d
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
movb $0x0, (%rax)
testl %ecx, %ecx
jne 0x604a9
leaq 0x26af9(%rip), %rsi     # 0x86f98
leaq 0x28(%rsp), %rdi
callq 0x51eb0
movq (%r12), %rax
movq %r12, %rdi
callq *0x60(%rax)
movq %rax, %r14
leal 0x7(%rbx), %eax
movl %eax, 0x24(%rsp)
shrl $0x3, %eax
movl %eax, 0x10(%rsp)
movl %ebx, %ebp
andl $0x7, %ebp
setne 0x14(%rsp)
movq 0x30(%rsp), %rax
testq %rax, %rax
setne %cl
movl %ebp, %edx
orl %r15d, %edx
setne %dl
orb %cl, %dl
je 0x604f0
testq %rax, %rax
jne 0x60587
jmp 0x60547
cmpl $0x1, (%r14)
jne 0x60547
cmpl $0x0, 0x40(%r14)
jne 0x60547
cmpq $0x0, 0x50(%r14)
jne 0x60547
movl 0x10(%rsp), %edx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x4fa20
movq %r14, %rax
addq $0x40, %rax
movups 0x48(%rsp), %xmm0
movups 0x54(%rsp), %xmm1
movups %xmm1, 0xc(%rax)
movups %xmm0, (%rax)
pushq $0x10
popq %rdi
callq 0x4f5d0
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x4de50
jmp 0x60729
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movl %r15d, %edx
movl %ebx, %ecx
callq 0x4f640
movq %rax, %r13
testq %rax, %rax
je 0x60587
pushq $0x10
popq %rdi
callq 0x4f5d0
movq %rax, %r12
movq %rax, %rdi
movq %r13, %rsi
callq 0x4de50
pushq $0x68
popq %rsi
movq %r14, %rdi
callq 0x4f670
jmp 0x60729
movb $0x1, 0xf(%rsp)
cmpl $0x0, 0x40(%r14)
jne 0x605e6
movl 0x50(%r14), %r13d
testl %r13d, %r13d
je 0x605e6
cmpl %r13d, 0x10(%rsp)
setb 0xf(%rsp)
shll $0x3, %r13d
cmpl %r15d, %r13d
seta %al
leal (%rbx,%r15), %ecx
movl %ecx, 0x14(%rsp)
cmpl %r13d, %ecx
setbe %cl
testb %cl, %al
jne 0x605d3
leaq 0x26a53(%rip), %rsi     # 0x8701c
leaq 0x28(%rsp), %rdi
callq 0x51eb0
testl %ebp, %ebp
setne %al
cmpl %r13d, 0x14(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x14(%rsp)
decb %bl
pushq $0x2
popq %r13
movl %ebx, %ecx
shlq %cl, %r13
testb $0x7, %r15b
sete %al
movl %r15d, %ebx
shrl $0x3, %ebx
xorl %ecx, %ecx
testb %al, 0xf(%rsp)
cmoveq %rcx, %rbx
cmovnel %ecx, %r15d
movb 0x14(%rsp), %bpl
testb %bpl, %bpl
jne 0x60633
cmpb $0x0, 0xf(%rsp)
jne 0x60633
testl %r15d, %r15d
jne 0x60633
leaq 0x26a03(%rip), %rsi     # 0x8702c
leaq 0x28(%rsp), %rdi
callq 0x51eb0
cmpl $0x48, 0x24(%rsp)
setb %al
movl %ebp, %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x60676
addq $0x8, %r12
leaq 0x269f2(%rip), %rsi     # 0x87041
leaq 0x48(%rsp), %rdi
movq %r12, %rdx
callq 0x4f1c0
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %r12
movq %r12, %rsi
callq 0x4fef0
movq %r12, %rdi
callq 0x4e020
pushq $0x10
popq %rdi
callq 0x4f5d0
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x4de50
cmpq $0x0, 0x30(%rsp)
je 0x606a9
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
leaq 0x28(%rsp), %rsi
callq *0x20(%rax)
jmp 0x60729
testl %r15d, %r15d
je 0x606c5
movl %r15d, %ecx
pushq $0x1e
popq %rsi
pushq $0x4
popq %r8
movq 0x18(%rsp), %rdi
movq %r12, %rdx
callq 0x4efc0
cmpb $0x0, 0xf(%rsp)
je 0x606e3
pushq $0x3f
popq %rsi
pushq $0x4
popq %r8
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x4efc0
testb %bpl, %bpl
je 0x60703
decq %r13
pushq $0x1b
popq %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq %r13, %rcx
movl 0x10(%rsp), %r8d
callq 0x4efc0
movq 0x8(%r12), %rbx
movl 0x10(%rsp), %edx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x4fa20
movq (%r12), %rdx
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x4d520
leaq 0x28(%rsp), %rdi
callq 0x4e020
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6076d
jmp 0x60759
jmp 0x6076d
jmp 0x6076d
jmp 0x6076d
jmp 0x6076d
jmp 0x60759
jmp 0x6076d
jmp 0x6076d
jmp 0x6076d
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0x4f670
jmp 0x60770
jmp 0x6076d
jmp 0x6076d
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4e020
movq %rbx, %rdi
callq 0x518c0