// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movl %edx, %r14d
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testl %ecx, %ecx
jne 0x53e6d
leaq 0x2c15f(%rip), %rcx     # 0x7ffb8
leaq 0x20(%rsp), %rdi
movl $0x18, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x417c0
movq (%r12), %rax
movq %r12, %rdi
callq *0x60(%rax)
movq %rax, %rbp
leal 0x7(%r13), %esi
movl %esi, %eax
shrl $0x3, %eax
movl %eax, 0x4(%rsp)
movl %r13d, %r15d
andl $0x7, %r15d
setne (%rsp)
movq 0x28(%rsp), %rax
testq %rax, %rax
setne %cl
movl %r15d, %edx
orl %r14d, %edx
setne %dl
orb %cl, %dl
jne 0x53f02
cmpl $0x1, (%rbp)
jne 0x53f02
cmpl $0x0, 0x40(%rbp)
jne 0x53f02
cmpq $0x0, 0x50(%rbp)
jne 0x53f02
movl 0x4(%rsp), %edx
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x40b10
movq %rbp, %rax
addq $0x40, %rax
movups 0x40(%rsp), %xmm0
movups 0x4c(%rsp), %xmm1
movups %xmm1, 0xc(%rax)
movups %xmm0, (%rax)
movl $0x10, %edi
callq 0x40800
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3f8f0
jmp 0x5414c
movl %esi, 0x1c(%rsp)
movq %rbp, 0x10(%rsp)
testq %rax, %rax
je 0x54062
movb $0x1, %bpl
movq 0x10(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x53f7a
movl 0x50(%rax), %ebx
testl %ebx, %ebx
je 0x53f7a
cmpl %ebx, 0x4(%rsp)
setb %bpl
shll $0x3, %ebx
cmpl %r14d, %ebx
seta %al
leal (%r14,%r13), %edx
cmpl %ebx, %edx
setbe %cl
testb %cl, %al
jne 0x53f6a
leaq 0x20(%rsp), %rdi
movl %edx, (%rsp)
movq 0x8(%rdi), %rdx
leaq 0x2c0e2(%rip), %rcx     # 0x8003c
movl $0xf, %r8d
xorl %esi, %esi
callq 0x417c0
movl (%rsp), %edx
testl %r15d, %r15d
setne %al
cmpl %ebx, %edx
setne %cl
andb %al, %cl
movb %cl, (%rsp)
decb %r13b
movl $0x2, %eax
movl %r13d, %ecx
shlq %cl, %rax
movq %rax, 0x60(%rsp)
testb $0x7, %r14b
sete %al
movl %r14d, %r13d
shrl $0x3, %r13d
xorl %ecx, %ecx
testb %bpl, %al
cmoveq %rcx, %r13
cmovnel %ecx, %r14d
cmpb $0x0, (%rsp)
jne 0x53fd5
testb %bpl, %bpl
jne 0x53fd5
testl %r14d, %r14d
jne 0x53fd5
leaq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x2c084(%rip), %rcx     # 0x8004c
movl $0x14, %r8d
xorl %esi, %esi
callq 0x417c0
cmpl $0x48, 0x1c(%rsp)
setb %al
movb (%rsp), %cl
xorb $0x1, %cl
orb %al, %cl
jne 0x5402b
addq $0x8, %r12
leaq 0x2c06f(%rip), %rsi     # 0x80061
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
callq 0x40500
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x40da0
movq (%rbx), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x5402b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x40870
movl $0x10, %edi
callq 0x40800
movq %rax, %rbx
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x3f8f0
cmpq $0x0, 0x28(%rsp)
je 0x540b8
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
callq *0x20(%rax)
jmp 0x5414c
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %r14d, %edx
movl %r13d, %ecx
callq 0x40840
movq %rax, %rbp
testq %rax, %rax
je 0x54143
movl $0x10, %edi
callq 0x40800
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3f8f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x54143
movl $0x68, %esi
callq 0x40870
jmp 0x54143
testl %r14d, %r14d
je 0x540d8
movl %r14d, %ecx
movq 0x8(%rsp), %rdi
movl $0x1e, %esi
movq %rbx, %rdx
movl $0x4, %r8d
callq 0x403f0
testb %bpl, %bpl
je 0x540f8
movq 0x8(%rsp), %rdi
movl $0x3f, %esi
movq %rbx, %rdx
movq %r13, %rcx
movl $0x4, %r8d
callq 0x403f0
cmpb $0x0, (%rsp)
je 0x5411d
movq 0x60(%rsp), %rcx
decq %rcx
movq 0x8(%rsp), %rdi
movl $0x1b, %esi
movq %rbx, %rdx
movl 0x4(%rsp), %r8d
callq 0x403f0
movq 0x8(%rbx), %r14
movl 0x4(%rsp), %edx
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x40b10
movq (%rbx), %rdx
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x3f380
jmp 0x5414c
testq %rbp, %rbp
je 0x53f14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x54168
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x40870
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x541a4
jmp 0x54190
jmp 0x541a4
jmp 0x541a4
jmp 0x54190
jmp 0x541a4
jmp 0x541a4
jmp 0x541a4
jmp 0x541a4
jmp 0x541a4
jmp 0x541a4
movq %rax, %r14
movl $0x10, %esi
movq %rbx, %rdi
callq 0x40870
jmp 0x541a7
jmp 0x541a4
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x541c3
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x40870
movq %r14, %rdi
callq 0x41f00
nop