// File: /workspace/github_cpp_makefile_star2_O2/brianquinlan[P]brainfuck-jit/bf_compile_and_go.cpp
// Function: BrainfuckCompileAndGo::generate_loop_code(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*)
bool BrainfuckCompileAndGo::generate_loop_code(string::const_iterator start,
                                               string::const_iterator end,
                                               string* code) {
  // Converts a Brainfuck command sequence like this:
  // [<code>]
  // Into this:
  // loop_start:
  //   cmpb   [rbx],0
  //   je     loop_end
  //   <code>
  //   jmp    loop_start
  // loop_end:
  //

  int loop_start = code->size();
  *code += string(LOOP_CMP, sizeof(LOOP_CMP) - 1);

  int jump_start = code->size();
  *code += string("\xde\xad\xbe\xef\xde\xad");  // Reserve 6 bytes for je.

  if (!generate_sequence_code(start+1, end, code)) {
    return false;
  }

  add_jmp_to_offset(loop_start, code);  // Jump back to the start of the loop.

  string jump_to_end = "\x0f\x84";                              // je ...
  uint32_t relative_end_of_loop = code->size() -
      (jump_start + jump_to_end.size() + 4);
  jump_to_end += string(
      reinterpret_cast<char *>(&relative_end_of_loop), 4);      // ... loop_end

  code->replace(jump_start, jump_to_end.size(), jump_to_end);
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movl 0x8(%rcx), %r14d
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movzwl 0x628c(%rip), %ecx      # 0x91a8
movw %cx, (%rax)
movb 0x6285(%rip), %cl       # 0x91aa
movb %cl, 0x2(%rax)
movq $0x3, -0x8(%rax)
movb $0x0, 0x3(%rax)
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x2300
movq %rsp, %rdi
callq 0x20f0
movq 0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x2382(%rip), %rsi      # 0x52d9
movq %rsp, %rdi
leaq 0x28(%rsp), %rdx
callq 0x20d0
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x2300
movq %rsp, %rdi
callq 0x20f0
incq %r12
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x304e
movl %eax, %ebp
testb %al, %al
je 0x3010
movl %r14d, %esi
movq %rbx, %rdx
callq 0x2e78
leaq 0x233d(%rip), %rsi      # 0x52e0
movq %rsp, %rdi
leaq 0x28(%rsp), %rdx
callq 0x20d0
movl 0x8(%rbx), %eax
movq %rsp, %rdi
movl 0x8(%rdi), %ecx
movq 0x20(%rsp), %r14
addl %r14d, %ecx
negl %ecx
addl %ecx, %eax
addl $-0x4, %eax
leaq 0x38(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movl %eax, (%rcx)
movq $0x4, -0x8(%rcx)
movb $0x0, 0x4(%rcx)
leaq 0x28(%rsp), %rsi
callq 0x2300
movslq %r14d, %r14
leaq 0x28(%rsp), %rdi
callq 0x20f0
movq %rsp, %rcx
movq 0x8(%rcx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20c0
movq %rsp, %rdi
callq 0x20f0
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x303b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20f0
jmp 0x303e
jmp 0x3036
jmp 0x303b
movq %rax, %rbx
jmp 0x3046
movq %rax, %rbx
movq %rsp, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x22b0