// File: luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::ReturnStatement::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ReturnStatement::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  // [return]语句必然出现在函数定义中
  auto &func_name = ctx.current_func_name_;
  const auto &func_iter = ir::gFuncTable.find(func_name);
  if (func_iter == ir::gFuncTable.end()) {
    ir::RuntimeError("没有维护好上下文信息中的函数名");
  } else {
    // 返回类型检查
    int ret_type = (*func_iter).second.ret_type_;
    if (ret_type == VOID) {
      // return ;
      if (nullptr == this->value_) {
        // genir (ret,-,-,-)
        gIRList.push_back(new ir::IR(IROpKind::RET));
      } else {
        ir::SemanticError(this->line_no_, "function ret type should be VOID: " + func_name);
      }
    } else {
      // return exp(int);
      if (nullptr == this->value_) {
        ir::SemanticError(this->line_no_, "function ret type should be INT: " + func_name);
        return;
      }
      this->value_->GenerateIR(ctx, gIRList);
      if (ctx.shape_.empty()) {
        // genir (ret,opn,-,-)
        gIRList.push_back(new ir::IR(IROpKind::RET, ctx.opn_));
      } else {
        ir::SemanticError(this->line_no_, "function ret type should be INT: " + func_name);
      }
    }
    ctx.has_return = true;
    return;
  }
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
movq %rdi, 0x190(%rsp)
movq %rsi, 0x188(%rsp)
movq %rdx, 0x180(%rsp)
movq 0x190(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x188(%rsp), %rax
addq $0x1e8, %rax            # imm = 0x1E8
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rsi
leaq 0x1221bf(%rip), %rdi    # 0x14c640
callq 0x18680
movq %rax, 0x168(%rsp)
leaq 0x168(%rsp), %rax
movq %rax, 0x170(%rsp)
movq 0x170(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x12218e(%rip), %rdi    # 0x14c640
callq 0x36170
movq 0x68(%rsp), %rdi
movq %rax, 0x160(%rsp)
leaq 0x160(%rsp), %rsi
callq 0x36150
testb $0x1, %al
jne 0x2a4da
jmp 0x2a57e
leaq 0x13f(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x8680
movq 0x58(%rsp), %rdx
leaq 0xd0772(%rip), %rsi     # 0xfac6a
leaq 0x140(%rsp), %rdi
callq 0xc570
jmp 0x2a507
leaq 0x140(%rsp), %rdi
callq 0x47d50
jmp 0x2a516
leaq 0x140(%rsp), %rdi
callq 0x8728
leaq 0x13f(%rsp), %rdi
callq 0x83c0
jmp 0x2a8ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
jmp 0x2a56c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x8728
leaq 0x13f(%rsp), %rdi
callq 0x83c0
jmp 0x2a8c2
movq 0x170(%rsp), %rdi
callq 0x186b0
movl 0x20(%rax), %eax
movl %eax, 0x128(%rsp)
cmpl $0x122, 0x128(%rsp) # imm = 0x122
jne 0x2a6a4
movq 0x60(%rsp), %rcx
xorl %eax, %eax
cmpq 0x10(%rcx), %rax
jne 0x2a62d
movq 0x180(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movl $0xa, %esi
callq 0x373d0
jmp 0x2a5e1
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rax
movq %rax, 0x120(%rsp)
leaq 0x120(%rsp), %rsi
callq 0x185b0
jmp 0x2a69f
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
movl $0xe0, %esi
callq 0x8350
jmp 0x2a8c2
movq 0x60(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0x178(%rsp), %rdx
leaq 0xd0650(%rip), %rsi     # 0xfac98
leaq 0x100(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x37520
movl 0x3c(%rsp), %edi
movq 0x40(%rsp), %rsi
callq 0x47ce0
jmp 0x2a66a
leaq 0x100(%rsp), %rdi
callq 0x8728
jmp 0x2a69f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x8728
jmp 0x2a8c2
jmp 0x2a8ae
movq 0x60(%rsp), %rcx
xorl %eax, %eax
cmpq 0x10(%rcx), %rax
jne 0x2a726
movq 0x60(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x178(%rsp), %rdx
leaq 0xd05ef(%rip), %rsi     # 0xfacbb
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x37520
movl 0x2c(%rsp), %edi
movq 0x30(%rsp), %rsi
callq 0x47ce0
jmp 0x2a6ee
leaq 0xe0(%rsp), %rdi
callq 0x8728
jmp 0x2a8ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x8728
jmp 0x2a8c2
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x188(%rsp), %rsi
movq 0x180(%rsp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x188(%rsp), %rdi
addq $0x50, %rdi
callq 0x10a80
testb $0x1, %al
jne 0x2a75f
jmp 0x2a846
movq 0x180(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, 0x20(%rsp)
movb $0x1, 0x97(%rsp)
movq 0x188(%rsp), %rsi
addq $0x8, %rsi
leaq 0x98(%rsp), %rdi
callq 0x43130
jmp 0x2a79e
movq 0x20(%rsp), %rdi
movl $0xa, %esi
leaq 0x98(%rsp), %rdx
callq 0x36c00
jmp 0x2a7b7
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rax
movb $0x0, 0x97(%rsp)
movq %rax, 0xd8(%rsp)
leaq 0xd8(%rsp), %rsi
callq 0x185b0
jmp 0x2a7e0
leaq 0x98(%rsp), %rdi
callq 0x1f210
jmp 0x2a8ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
jmp 0x2a829
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f210
testb $0x1, 0x97(%rsp)
jne 0x2a835
jmp 0x2a844
movq 0x20(%rsp), %rdi
movl $0xe0, %esi
callq 0x8350
jmp 0x2a8c2
movq 0x60(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x178(%rsp), %rdx
leaq 0xd045a(%rip), %rsi     # 0xfacbb
leaq 0x70(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x37520
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %rsi
callq 0x47ce0
jmp 0x2a880
leaq 0x70(%rsp), %rdi
callq 0x8728
jmp 0x2a8ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x8728
jmp 0x2a8c2
jmp 0x2a8ae
movq 0x188(%rsp), %rax
movb $0x1, 0x48(%rax)
addq $0x198, %rsp            # imm = 0x198
retq
movq 0x130(%rsp), %rdi
callq 0x8670
nop