// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movq %rdi, %r13
movslq %esi, %rax
movq 0x240(%rdi), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x48(%rax)
testb $0x3, %al
movq %r13, 0x10(%rsp)
je 0x2defe
leaq 0x8(%rbx), %rdx
leaq 0x3435f(%rip), %rsi     # 0x621b6
leaq 0x40(%rsp), %r13
movq %r13, %rdi
callq 0x1170e
leaq 0x3436e(%rip), %rsi     # 0x621d9
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 0x2de9a
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2dea1
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 0x2d000
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2dee3
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2defe
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xf380
movq (%rbx), %rax
movq %rbx, 0x80(%rsp)
movq %rbx, %rdi
callq *0x48(%rax)
testl %r15d, %r15d
jle 0x2e287
shll $0x3, %eax
movl %eax, 0x70(%rsp)
leal -0x1(%r15), %eax
movl %eax, 0x68(%rsp)
leal -0x2(%r15), %esi
xorl %edi, %edi
movl %esi, 0x1c(%rsp)
movq %r15, 0x88(%rsp)
movslq %edi, %r14
movq 0x240(%r13), %rax
shlq $0x4, %r14
movq 0x8(%rax,%r14), %r13
movl 0x20(%r13), %r8d
movl 0x24(%r13), %ebp
movl %ebp, %eax
subl %r8d, %eax
movq %rax, 0x60(%rsp)
cmpl $0x21, %eax
movq %rdi, 0x78(%rsp)
movl %r8d, 0xc(%rsp)
jb 0x2e040
movq %r12, %rbx
movq %r15, %r12
leaq 0x40(%rsp), %r15
movq %r15, %rdi
leaq 0x34274(%rip), %rsi     # 0x621fa
movq %r13, %rdx
callq 0x1170e
movq %r15, %rdi
leaq 0x34274(%rip), %rsi     # 0x6220c
callq 0xf780
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2dfc5
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2dfcb
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq %r12, %r15
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 0x2d000
movq %rbx, %r12
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2e016
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2e032
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xf380
movl 0x1c(%rsp), %esi
movq 0x78(%rsp), %rdi
movl 0xc(%rsp), %r8d
cmpl 0x70(%rsp), %ebp
jl 0x2e116
movq %r12, %rbx
movq %r15, %r12
leaq 0x40(%rsp), %r15
movq %r15, %rdi
leaq 0x341c2(%rip), %rsi     # 0x62221
movq %r13, %rdx
callq 0x1170e
movq %r15, %rdi
leaq 0x341c3(%rip), %rsi     # 0x62234
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 0x2e0a3
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2e0ab
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq %r12, %r15
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 0x2d000
movq %rbx, %r12
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2e0ec
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2e108
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xf380
movl 0x1c(%rsp), %esi
movq 0x78(%rsp), %rdi
movl 0xc(%rsp), %r8d
leal 0x1(%rdi), %ecx
cmpl %r15d, %ecx
jge 0x2e13f
movq 0x10(%rsp), %r13
movq 0x240(%r13), %rax
movslq %ecx, %rdx
shlq $0x4, %rdx
movq 0x8(%rax,%rdx), %rcx
cmpl %ebp, 0x20(%rcx)
jbe 0x2e14d
movl %edi, %edx
jmp 0x2e176
movl %edi, %edx
movq 0x10(%rsp), %r13
movq 0x60(%rsp), %rcx
jmp 0x2e17b
addq %rdx, %rax
addq $0x18, %rax
movl %edi, %edx
movl 0x24(%rcx), %ecx
cmpl %ebp, %ecx
cmoval %ecx, %ebp
cmpl %edx, %esi
je 0x2e172
movq (%rax), %rcx
incl %edx
addq $0x10, %rax
cmpl %ebp, 0x20(%rcx)
jbe 0x2e156
jmp 0x2e176
movl 0x68(%rsp), %edx
movl %ebp, %ecx
subl %r8d, %ecx
leal (%rcx,%r12), %r9d
movl %r9d, %eax
xorl %r12d, %eax
andl $-0x20, %r9d
cmpl $0x20, %eax
cmovbl %r12d, %r9d
cmpl %edx, %edi
jg 0x2e277
movq %rcx, 0x60(%rsp)
leal (%r9,%rcx), %eax
subl %ebp, %eax
movl %eax, 0x74(%rsp)
movq %r9, 0x90(%rsp)
movl %r9d, %eax
subl %r8d, %eax
movl %eax, 0xc(%rsp)
incl %edx
orq $0x8, %r14
movl %edx, 0x6c(%rsp)
movl %edx, %r15d
subl %edi, %r15d
movq 0x240(%r13), %rax
movq (%rax,%r14), %r12
movl 0x20(%r12), %ebp
addl 0xc(%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 0x3c61c
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 0x4a5ca
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2d7f2
addq $0x10, %r14
decl %r15d
jne 0x2e1c9
movl 0x6c(%rsp), %eax
movl %eax, %edi
movq 0x88(%rsp), %r15
movl 0x1c(%rsp), %esi
movq 0x60(%rsp), %rcx
movq 0x90(%rsp), %r9
leal (%r9,%rcx), %r12d
incl %r12d
cmpl %r15d, %edi
jl 0x2df39
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 0x2e2c7
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
jmp 0x2e2c7
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x2e311
jmp 0x2e2d4
movq %rax, %rbx
jmp 0x2e307
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
jne 0x2e2fa
jmp 0x2e307
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2e307
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2e33d
movq 0x50(%rsp), %rsi
incq %rsi
jmp 0x2e338
movq %rax, %rbx
movl $0x50, %esi
movq %rbp, %rdi
jmp 0x2e338
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
callq 0xf380
movq %rbx, %rdi
callq 0xf730
nop