// File: brianquinlan[P]brainfuck-jit/bf_jit.cpp
// Function: BrainfuckJIT::run(char (*)(void*), void*, bool (*)(void*, char), void*, void*)
void* BrainfuckJIT::run(BrainfuckReader reader,
                        void* reader_arg,
                        BrainfuckWriter writer,
                        void* writer_arg,
                        void* memory) {
  uint8_t* byte_memory = reinterpret_cast<uint8_t *>(memory);
  // When processing a "[", push the position of that command onto a stack so
  // that we can quickly return to the start of the block when then "]" is
  // interpreted.
  stack<string::const_iterator> return_stack;

  for (string::const_iterator it = start_; it != end_;) {
    switch (*it) {
      case '<':
        --byte_memory;
         ++it;
        break;
      case '>':
        ++byte_memory;
         ++it;
        break;
      case '-':
        *byte_memory -= 1;
         ++it;
        break;
      case '+':
        *byte_memory += 1;
         ++it;
        break;
      case ',':
        *byte_memory = reader(reader_arg);
         ++it;
        break;
      case '.':
        writer(writer_arg, *byte_memory);
         ++it;
        break;
      case '[':
        {
          Loop &loop = loop_start_to_loop_[it];

          if (loop.compiled == nullptr &&
              loop.condition_evaluation_count > kLoopCompilationThreshold) {
            shared_ptr<BrainfuckCompileAndGo> compiled(
              new BrainfuckCompileAndGo());
            string::const_iterator compilation_end(loop.after_end);

            if (!compiled->init(it, compilation_end)) {
              fprintf(stderr,
                      "Unable to compile code: %s\n",
                      string(it, compilation_end).c_str());
            } else {
              loop.compiled = compiled;
            }
          }

          if (loop.compiled) {
            byte_memory = reinterpret_cast<uint8_t *>(
                loop.compiled->run(reader,
                                   reader_arg,
                                   writer,
                                   writer_arg,
                                   byte_memory));
            it = loop_start_to_loop_[it].after_end;
          } else {
            ++loop.condition_evaluation_count;
            if (*byte_memory) {
              return_stack.push(it);
              ++it;
            } else {
              it = loop_start_to_loop_[it].after_end;
            }
          }
        }
        break;
      case ']':
        if (return_stack.size() != 0) {
          it = return_stack.top();
          return_stack.pop();
        } else {
          ++it;
        }
        break;
      default:
        ++it;
        break;
    }
  }
  return byte_memory;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x170(%rsp)
movq %rsi, 0x168(%rsp)
movq %rdx, 0x160(%rsp)
movq %rcx, 0x158(%rsp)
movq %r8, 0x150(%rsp)
movq %r9, 0x148(%rsp)
movq 0x170(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x140(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x8c60
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xe8(%rsp)
movq 0x68(%rsp), %rsi
addq $0x10, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x6db0
testb $0x1, %al
jne 0xb8b1
jmp 0xbdb0
leaq 0xe8(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
addl $-0x2b, %eax
movl %eax, %ecx
movq %rcx, 0x60(%rsp)
subl $0x32, %eax
ja 0xbd9e
movq 0x60(%rsp), %rax
leaq 0x2c9c(%rip), %rcx      # 0xe57c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x140(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x140(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
movq 0x140(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x140(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
movq 0x140(%rsp), %rax
movzbl (%rax), %ecx
subl $0x1, %ecx
movb %cl, (%rax)
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
movq 0x140(%rsp), %rax
movzbl (%rax), %ecx
addl $0x1, %ecx
movb %cl, (%rax)
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
movq 0x168(%rsp), %rax
movq 0x160(%rsp), %rdi
callq *%rax
movb %al, 0x5f(%rsp)
jmp 0xb991
movb 0x5f(%rsp), %cl
movq 0x140(%rsp), %rax
movb %cl, (%rax)
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
jmp 0xbdd5
movq 0x158(%rsp), %rax
movq 0x150(%rsp), %rdi
movq 0x140(%rsp), %rcx
movsbl (%rcx), %esi
callq *%rax
jmp 0xb9e9
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
movq 0x68(%rsp), %rdi
addq $0x18, %rdi
leaq 0xe8(%rsp), %rsi
callq 0xbe40
movq %rax, 0x50(%rsp)
jmp 0xba18
movq 0x50(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xbfa0
testb $0x1, %al
jne 0xba43
jmp 0xbc21
movq 0xd0(%rsp), %rax
cmpq $0x14, 0x8(%rax)
jbe 0xbc21
movl $0x20, %edi
callq 0x3250
movq %rax, 0x48(%rsp)
jmp 0xba67
movq 0x48(%rsp), %rdi
callq 0x6790
jmp 0xba73
movq 0x48(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xbfd0
jmp 0xba87
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xc000
movq %rax, %rdi
movq 0xe8(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
movb %al, 0x47(%rsp)
jmp 0xbae8
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0xbbe7
jmp 0xbaf6
movq 0x84e3(%rip), %rax      # 0x13fe0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x77(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x33d0
movq 0x38(%rsp), %rcx
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %rdx
leaq 0x88(%rsp), %rdi
callq 0x6f10
jmp 0xbb52
leaq 0x88(%rsp), %rdi
callq 0x3100
movq 0x30(%rsp), %rdi
movq %rax, %rdx
leaq 0x2aea(%rip), %rsi      # 0xe658
movb $0x0, %al
callq 0x3360
leaq 0x88(%rsp), %rdi
callq 0x3160
leaq 0x77(%rsp), %rdi
callq 0x3280
jmp 0xbc00
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
callq 0x3210
jmp 0xbdd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
jmp 0xbc0f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0x77(%rsp), %rdi
callq 0x3280
jmp 0xbc0f
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
leaq 0xc0(%rsp), %rsi
callq 0xc010
leaq 0xc0(%rsp), %rdi
callq 0xc040
jmp 0xbc21
leaq 0xc0(%rsp), %rdi
callq 0xc040
jmp 0xbdd5
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
callq 0xc050
testb $0x1, %al
jne 0xbc3b
jmp 0xbcc3
movq 0xd0(%rsp), %rdi
addq $0x10, %rdi
callq 0xc000
movq %rax, %rdi
movq 0x168(%rsp), %rsi
movq 0x160(%rsp), %rdx
movq 0x158(%rsp), %rcx
movq 0x150(%rsp), %r8
movq 0x140(%rsp), %r9
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0xbc87
movq 0x68(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x140(%rsp)
addq $0x18, %rdi
leaq 0xe8(%rsp), %rsi
callq 0xbe40
movq %rax, 0x20(%rsp)
jmp 0xbcb1
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
jmp 0xbd39
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x140(%rsp), %rax
cmpb $0x0, (%rax)
je 0xbd0a
leaq 0xf0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x8c90
jmp 0xbcfb
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbd37
movq 0x68(%rsp), %rdi
addq $0x18, %rdi
leaq 0xe8(%rsp), %rsi
callq 0xbe40
movq %rax, 0x18(%rsp)
jmp 0xbd27
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
jmp 0xbd39
jmp 0xbdab
leaq 0xf0(%rsp), %rdi
callq 0x8cc0
movq %rax, 0x10(%rsp)
jmp 0xbd4f
movq 0x10(%rsp), %rax
cmpq $0x0, %rax
je 0xbd8f
leaq 0xf0(%rsp), %rdi
callq 0x8cd0
movq %rax, 0x8(%rsp)
jmp 0xbd6e
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x8dd0
jmp 0xbd8d
jmp 0xbd9c
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xbdab
leaq 0xe8(%rsp), %rdi
callq 0x6fa0
jmp 0xb892
movq 0x140(%rsp), %rax
movq %rax, (%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x8de0
movq (%rsp), %rax
addq $0x178, %rsp            # imm = 0x178
retq
leaq 0xf0(%rsp), %rdi
callq 0x8de0
movq 0xe0(%rsp), %rdi
callq 0x33c0
nop