// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, %r12d
movl %edx, %ebx
movq %rdi, %r13
movslq %esi, %rax
movq 0x240(%rdi), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %r15
movq (%r15), %rax
movq %r15, %rdi
callq *0x48(%rax)
testb $0x3, %al
movq %r13, 0x10(%rsp)
je 0x2e179
leaq 0x8(%r15), %rdx
leaq 0x34094(%rip), %rsi     # 0x62166
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x11754
leaq 0x340a3(%rip), %rsi     # 0x62189
movq %r13, %rdi
callq 0xf780
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2e115
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2e11c
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x10(%rsp), %r13
movq 0x8(%rax), %rdx
leaq 0x20(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r13, %rdi
xorl %edx, %edx
callq 0x2d276
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2e15e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e179
movq 0x58(%rsp), %rsi
incq %rsi
callq 0xf380
movq (%r15), %rax
movq %r15, 0x80(%rsp)
movq %r15, %rdi
callq *0x48(%rax)
testl %ebx, %ebx
jle 0x2e4f3
shll $0x3, %eax
movl %eax, 0x6c(%rsp)
leal -0x1(%rbx), %eax
movl %eax, 0x68(%rsp)
leal -0x2(%rbx), %esi
xorl %r8d, %r8d
movl %esi, 0x1c(%rsp)
movq %rbx, 0x78(%rsp)
movslq %r8d, %rcx
movq 0x240(%r13), %rax
shlq $0x4, %rcx
movq %rcx, %r15
movq 0x8(%rax,%rcx), %r13
movl 0x20(%r13), %edi
movl 0x24(%r13), %ebx
movl %ebx, %eax
subl %edi, %eax
cmpl $0x21, %eax
movq %r12, 0x40(%rsp)
jb 0x2e2a9
movq %r8, %r14
movl %edi, %r12d
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
leaq 0x33fb6(%rip), %rsi     # 0x621aa
movq %r13, %rdx
callq 0x11754
movq %rbp, %rdi
leaq 0x33fb6(%rip), %rsi     # 0x621bc
callq 0xf780
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2e233
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2e239
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x2d276
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2e27e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2e29a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0xf380
movl 0x1c(%rsp), %esi
movl %r12d, %edi
movq 0x40(%rsp), %r12
movq %r14, %r8
cmpl 0x6c(%rsp), %ebx
jl 0x2e37a
movq %r8, %r14
movl %edi, %r12d
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
leaq 0x33f09(%rip), %rsi     # 0x621d1
movq %r13, %rdx
callq 0x11754
movq %rbp, %rdi
leaq 0x33f0a(%rip), %rsi     # 0x621e4
callq 0xf780
leaq 0x30(%rsp), %r13
movq %r13, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
movq 0x10(%rsp), %rdi
je 0x2e30c
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2e314
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x2d276
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2e34f
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2e36b
movq 0x58(%rsp), %rsi
incq %rsi
callq 0xf380
movl 0x1c(%rsp), %esi
movl %r12d, %edi
movq 0x40(%rsp), %r12
movq %r14, %r8
leal 0x1(%r8), %ecx
cmpl 0x78(%rsp), %ecx
jge 0x2e3a6
movq 0x10(%rsp), %r13
movq 0x240(%r13), %rax
movslq %ecx, %rdx
shlq $0x4, %rdx
movq 0x8(%rax,%rdx), %rcx
cmpl %ebx, 0x20(%rcx)
jbe 0x2e3b0
movl %r8d, %edx
jmp 0x2e3da
movl %r8d, %edx
movq 0x10(%rsp), %r13
jmp 0x2e3da
addq %rdx, %rax
addq $0x18, %rax
movl %r8d, %edx
movl 0x24(%rcx), %ecx
cmpl %ebx, %ecx
cmoval %ecx, %ebx
cmpl %edx, %esi
je 0x2e3d6
movq (%rax), %rcx
incl %edx
addq $0x10, %rax
cmpl %ebx, 0x20(%rcx)
jbe 0x2e3ba
jmp 0x2e3da
movl 0x68(%rsp), %edx
movl %ebx, %ecx
subl %edi, %ecx
leal (%rcx,%r12), %esi
movl %esi, %eax
xorl %r12d, %eax
andl $-0x20, %esi
cmpl $0x20, %eax
cmovbl %r12d, %esi
cmpl %edx, %r8d
movq %rcx, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
jg 0x2e4ca
movl %esi, %eax
subl %edi, %eax
movl %eax, 0x40(%rsp)
leal (%rsi,%rcx), %eax
subl %ebx, %eax
movl %eax, 0x74(%rsp)
incl %edx
movq %r15, %rcx
orq $0x8, %rcx
movl %edx, 0x70(%rsp)
movq %r8, %r15
movq 0x240(%r13), %rax
movq %rcx, %r14
movq (%rax,%rcx), %r12
movl 0x20(%r12), %ebp
addl 0x40(%rsp), %ebp
movl 0x24(%r12), %ebx
addl 0x74(%rsp), %ebx
movl $0x28, %edi
callq 0xf370
movq %rax, %r13
movzbl 0x28(%r12), %esi
movq %rax, %rdi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x3c8fa
movl $0x50, %edi
callq 0xf370
movq %rax, %rbp
movl 0x20(%r12), %r8d
movl 0x24(%r12), %r9d
movzbl 0x29(%r12), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq 0x80(%rsp), %rcx
callq 0x4a9be
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2da5a
movq %r14, %rcx
addq $0x10, %rcx
movq %r15, %r8
incl %r8d
movl 0x70(%rsp), %edx
cmpl %r8d, %edx
jne 0x2e428
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rcx
leal (%rcx,%rax), %r12d
incl %r12d
movq 0x78(%rsp), %rbx
cmpl %ebx, %r8d
movl 0x1c(%rsp), %esi
jl 0x2e1af
movq 0x80(%rsp), %rax
movb $0x1, 0x50(%rax)
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2e533
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
jmp 0x2e533
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x2e57d
jmp 0x2e540
movq %rax, %rbx
jmp 0x2e573
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
jne 0x2e566
jmp 0x2e573
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2e573
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2e5a9
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0x2e5a4
movq %rax, %rbx
movl $0x50, %esi
movq %rbp, %rdi
jmp 0x2e5a4
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
callq 0xf380
movq %rbx, %rdi
callq 0xf730
nop