// File: jstaursky[P]hutch/src/Sleigh/slgh_compile.cc
// Function: SleighCompile::calcContextVarLayout(int, int, int)
int4 SleighCompile::calcContextVarLayout(int4 start,int4 sz,int4 numbits)

{
  VarnodeSymbol *sym = contexttable[start].sym;
  FieldQuality *qual;
  int4 i,j;
  int4 maxbits;
  
  if ((sym->getSize()) % 4 != 0)
    reportError("Invalid size of context register: "+sym->getName()+" : must be a multiple of 4 bytes",false);
  maxbits = sym->getSize() * 8 -1;
  i = 0;
  while(i<sz) {

    qual = contexttable[i].qual;
    int4 min = qual->low;
    int4 max = qual->high;
    if ((max - min) > (8*sizeof(uintm)))
      reportError("Size of bitfield " + qual->name + " larger than 32-bits",false);
    if (max > maxbits)
      reportError("Scope of bitfield " + qual->name + " extends beyond the size of context register",false);
    j = i+1;
    // Find union of fields overlapping with first field
    while(j<sz) {
      qual = contexttable[j].qual;
      if (qual->low <= max) {	// We have overlap of context variables
	if (qual->high > max)
	  max = qual->high;
	// reportWarning("Local context variables overlap in "+sym->getName(),false);
      }
      else
	break;
      j = j+1;
    }

    int4 alloc = max-min+1;
    int4 startword = numbits / (8*sizeof(uintm));
    int4 endword = (numbits+alloc-1) / (8*sizeof(uintm));
    if (startword != endword)
      numbits = endword * (8*sizeof(uintm)); // Bump up to next word

    uint4 low = numbits;
    numbits += alloc;

    for(;i<j;++i) {
      qual = contexttable[i].qual;
      uint4 l = qual->low - min + low;
      uint4 h = numbits-1-(max-qual->high);
      ContextField *field = new ContextField(qual->signext,l,h);
      addSymbol(new ContextSymbol(qual->name,field,sym,qual->low,qual->high,qual->flow));
    }
    
  }
  sym->markAsContext();
  return numbits;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x138(%rbp)
addq $0x240, %rdi            # imm = 0x240
movslq -0xc(%rbp), %rsi
callq 0x6b520
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x61c20
movq -0x20(%rbp), %rdi
callq 0x401a0
movq %rax, %rdx
leaq 0x777e6(%rip), %rsi     # 0xd937e
leaq -0x78(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x147f0
movq -0x140(%rbp), %rsi
leaq 0x777eb(%rip), %rdx     # 0xd93a1
leaq -0x58(%rbp), %rdi
callq 0x6b540
jmp 0x61bc1
movq -0x138(%rbp), %rdi
leaq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x603b0
jmp 0x61bd5
leaq -0x58(%rbp), %rdi
callq 0x11300
leaq -0x78(%rbp), %rdi
callq 0x11300
jmp 0x61c20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x61c12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x11300
leaq -0x78(%rbp), %rdi
callq 0x11300
jmp 0x6203a
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
shll $0x3, %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x62025
movq -0x138(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
movslq -0x2c(%rbp), %rsi
callq 0x6b520
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
subl -0x88(%rbp), %eax
cltq
cmpq $0x20, %rax
jbe 0x61d3f
movq -0x28(%rbp), %rdx
leaq 0x77720(%rip), %rsi     # 0xd93c2
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x147f0
movq -0x148(%rbp), %rsi
leaq 0x77711(%rip), %rdx     # 0xd93d4
leaq -0xb0(%rbp), %rdi
callq 0x6b540
jmp 0x61cd1
movq -0x138(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
xorl %edx, %edx
callq 0x603b0
jmp 0x61ce8
leaq -0xb0(%rbp), %rdi
callq 0x11300
leaq -0xd0(%rbp), %rdi
callq 0x11300
jmp 0x61d3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x61d2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x11300
leaq -0xd0(%rbp), %rdi
callq 0x11300
jmp 0x6203a
movl -0x8c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x61df6
movq -0x28(%rbp), %rdx
leaq 0x77690(%rip), %rsi     # 0xd93e9
leaq -0x110(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x147f0
movq -0x150(%rbp), %rsi
leaq 0x77682(%rip), %rdx     # 0xd93fc
leaq -0xf0(%rbp), %rdi
callq 0x6b540
jmp 0x61d88
movq -0x138(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
xorl %edx, %edx
callq 0x603b0
jmp 0x61d9f
leaq -0xf0(%rbp), %rdi
callq 0x11300
leaq -0x110(%rbp), %rdi
callq 0x11300
jmp 0x61df6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x61de5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x11300
leaq -0x110(%rbp), %rdi
callq 0x11300
jmp 0x6203a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x61e60
movq -0x138(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
movslq -0x30(%rbp), %rsi
callq 0x6b520
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
cmpl -0x8c(%rbp), %eax
ja 0x61e53
movq -0x28(%rbp), %rax
movl 0x24(%rax), %eax
cmpl -0x8c(%rbp), %eax
jbe 0x61e51
movq -0x28(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x8c(%rbp)
jmp 0x61e55
jmp 0x61e60
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x61dff
movl -0x8c(%rbp), %eax
subl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
movslq -0x14(%rbp), %rax
shrq $0x5, %rax
movl %eax, -0x118(%rbp)
movl -0x14(%rbp), %eax
addl -0x114(%rbp), %eax
subl $0x1, %eax
cltq
shrq $0x5, %rax
movl %eax, -0x11c(%rbp)
movl -0x118(%rbp), %eax
cmpl -0x11c(%rbp), %eax
je 0x61eb7
movslq -0x11c(%rbp), %rax
shlq $0x5, %rax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x120(%rbp)
movl -0x114(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x62020
movq -0x138(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
movslq -0x2c(%rbp), %rsi
callq 0x6b520
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
movl -0x88(%rbp), %ecx
subl %ecx, %eax
movl -0x120(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x124(%rbp)
movl -0x14(%rbp), %esi
movl -0x8c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl 0x24(%rax), %edx
movl %esi, %eax
subl %ecx, %edx
movl %edx, %ecx
leal -0x1(%rax,%rcx), %eax
movl %eax, -0x128(%rbp)
movl $0x28, %edi
callq 0x11440
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x158(%rbp)
movq -0x28(%rbp), %rax
movl -0x124(%rbp), %edx
movl -0x128(%rbp), %ecx
movzbl 0x28(%rax), %esi
andl $0x1, %esi
callq 0x8a280
jmp 0x61f69
movq -0x158(%rbp), %rax
movq %rax, -0x130(%rbp)
movl $0x50, %edi
callq 0x11440
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x160(%rbp)
movq -0x28(%rbp), %rsi
movq -0x130(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl 0x20(%rsi), %r8d
movl 0x24(%rsi), %r9d
movzbl 0x29(%rsi), %r10d
andl $0x1, %r10d
movq %rsp, %rax
movl %r10d, (%rax)
callq 0xa6ef0
jmp 0x61fbb
movq -0x160(%rbp), %rsi
movq -0x138(%rbp), %rdi
callq 0x60f90
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x61ecc
movq -0x158(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x28, %esi
callq 0x11450
jmp 0x6203a
movq -0x160(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x50, %esi
callq 0x11450
jmp 0x6203a
jmp 0x61c3a
movq -0x20(%rbp), %rdi
callq 0x6b590
movl -0x14(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x118b0
nopw %cs:(%rax,%rax)