// File: brianquinlan[P]brainfuck-jit/bf_compile_and_go.cpp
// Function: BrainfuckCompileAndGo::generate_sequence_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_sequence_code(string::const_iterator start,
                                                   string::const_iterator end,
                                                   string* code) {
  int8_t offset = 0;
  // Maps offset relative to the datapointer into the amount to change it.
  map<int8_t, uint8_t> offset_to_change;

  for (string::const_iterator it = start; it != end; ++it) {
    switch (*it) {
      case '<':
        --offset;
        if (offset == 127 || offset == -128) {
          emit_offset_table(&offset_to_change, &offset, code);
        }
        break;
      case '>':
        ++offset;
        if (offset == 127 || offset == -128) {
          emit_offset_table(&offset_to_change, &offset, code);
        }
        break;
      case '-':
        offset_to_change[offset] -= 1;
        break;
      case '+':
        offset_to_change[offset] += 1;
        break;
      case ',':
        emit_offset_table(&offset_to_change, &offset, code);
        generate_read_code(code);
        break;
      case '.':
        emit_offset_table(&offset_to_change, &offset, code);
        generate_write_code(code);
        break;
      case '[':
        emit_offset_table(&offset_to_change, &offset, code);
        string::const_iterator loop_end;
        if (!find_loop_end(it, end, &loop_end)) {
          fprintf(
              stderr,
              "Unable to find loop end in block starting with: %s\n",
              string(it, end).c_str());
          return false;
        }
        if (!generate_loop_code(it, loop_end, code)) {
          return false;
        }
        it = loop_end;
        break;
    }
  }
  // The offset table must be emitted because this function is called
  // recursively to handle loops.
  emit_offset_table(&offset_to_change, &offset, code);
  return true;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq %rdi, 0x108(%rsp)
movq %rcx, 0x100(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x48(%rsp)
movb $0x0, 0xff(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x6da0
movq 0x118(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x6db0
testb $0x1, %al
jne 0x5db7
jmp 0x6147
leaq 0xc0(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
addl $-0x2b, %eax
movl %eax, %ecx
movq %rcx, 0x40(%rsp)
subl $0x30, %eax
ja 0x6133
movq 0x40(%rsp), %rax
leaq 0x847a(%rip), %rcx      # 0xe260
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb 0xff(%rsp), %al
addb $-0x1, %al
movb %al, 0xff(%rsp)
movsbl 0xff(%rsp), %eax
cmpl $0x7f, %eax
je 0x5e19
movsbl 0xff(%rsp), %eax
cmpl $-0x80, %eax
jne 0x5e58
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rcx
leaq 0xc8(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0x63d0
jmp 0x5e3d
jmp 0x5e58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x6199
jmp 0x6133
movb 0xff(%rsp), %al
addb $0x1, %al
movb %al, 0xff(%rsp)
movsbl 0xff(%rsp), %eax
cmpl $0x7f, %eax
je 0x5e87
movsbl 0xff(%rsp), %eax
cmpl $-0x80, %eax
jne 0x5ead
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rcx
leaq 0xc8(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0x63d0
jmp 0x5eab
jmp 0x5ead
jmp 0x6133
leaq 0xc8(%rsp), %rdi
leaq 0xff(%rsp), %rsi
callq 0x6e00
movq %rax, 0x38(%rsp)
jmp 0x5ece
movq 0x38(%rsp), %rax
movzbl (%rax), %ecx
subl $0x1, %ecx
movb %cl, (%rax)
jmp 0x6133
leaq 0xc8(%rsp), %rdi
leaq 0xff(%rsp), %rsi
callq 0x6e00
movq %rax, 0x30(%rsp)
jmp 0x5efc
movq 0x30(%rsp), %rax
movzbl (%rax), %ecx
addl $0x1, %ecx
movb %cl, (%rax)
jmp 0x6133
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rcx
leaq 0xc8(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0x63d0
jmp 0x5f32
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rsi
callq 0x61c0
jmp 0x5f46
jmp 0x6133
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rcx
leaq 0xc8(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0x63d0
jmp 0x5f6f
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rsi
callq 0x6310
jmp 0x5f83
jmp 0x6133
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rcx
leaq 0xc8(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0x63d0
jmp 0x5fac
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6ef0
movq 0x20(%rsp), %rdx
movq 0xc0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x66d0
movb %al, 0x2f(%rsp)
jmp 0x5ffe
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x60cb
jmp 0x600c
movq 0xdfcd(%rip), %rax      # 0x13fe0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x67(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x33d0
movq 0x18(%rsp), %rcx
movq 0x70(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0x78(%rsp), %rdi
callq 0x6f10
jmp 0x605f
leaq 0x78(%rsp), %rdi
callq 0x3100
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x832f(%rip), %rsi      # 0xe3a7
movb $0x0, %al
callq 0x3360
leaq 0x78(%rsp), %rdi
callq 0x3160
leaq 0x67(%rsp), %rdi
callq 0x3280
movb $0x0, 0x127(%rsp)
movl $0x1, 0x60(%rsp)
jmp 0x617b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x67(%rsp), %rdi
callq 0x3280
jmp 0x6199
movq 0x48(%rsp), %rdi
movq 0xc0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x100(%rsp), %rcx
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x5930
movb %al, 0xf(%rsp)
jmp 0x6107
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x6123
jmp 0x6111
movb $0x0, 0x127(%rsp)
movl $0x1, 0x60(%rsp)
jmp 0x617b
movq 0xa8(%rsp), %rax
movq %rax, 0xc0(%rsp)
jmp 0x6135
leaq 0xc0(%rsp), %rdi
callq 0x6fa0
jmp 0x5d99
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rcx
leaq 0xc8(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0x63d0
jmp 0x616b
movb $0x1, 0x127(%rsp)
movl $0x1, 0x60(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x6fc0
movb 0x127(%rsp), %al
andb $0x1, %al
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xc8(%rsp), %rdi
callq 0x6fc0
movq 0xb8(%rsp), %rdi
callq 0x33c0
nopw %cs:(%rax,%rax)