// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x1e8(%rsi), %r15
leaq 0x74bc7(%rip), %rdi     # 0x8f590
movq %r15, %rsi
callq 0x116e2
testq %rax, %rax
je 0x1aa16
cmpl $0x122, 0x28(%rax)      # imm = 0x122
movq 0x10(%r12), %rdi
jne 0x1aa4f
testq %rdi, %rdi
je 0x1aab3
movl 0x8(%r12), %ebp
leaq 0x54328(%rip), %rsi     # 0x6ed21
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x25a7d
movl %ebp, %edi
movq %r14, %rsi
callq 0x2b6aa
jmp 0x1aa96
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x542cd(%rip), %rsi     # 0x6ecf3
leaq 0x542f3(%rip), %rdx     # 0x6ed20
movq %rsp, %rdi
callq 0xc772
movq %rsp, %rdi
callq 0x2b6fb
movq (%rsp), %rdi
cmpq %r14, %rdi
jne 0x1ab13
jmp 0x1ab20
testq %rdi, %rdi
je 0x1aae1
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x10(%rax)
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
je 0x1ab2d
movl 0x8(%r12), %ebp
leaq 0x542c6(%rip), %rsi     # 0x6ed44
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x25a7d
movl %ebp, %edi
movq %r14, %rsi
callq 0x2b6aa
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1aadb
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x1aadb
movl $0xe0, %edi
callq 0x8290
movq %rax, %r15
movq %rax, %rdi
movl $0xa, %esi
callq 0x259da
movq %rsp, %rsi
movq %r15, (%rsi)
movq %r14, %rdi
callq 0x11182
movb $0x1, 0x48(%rbx)
jmp 0x1ab20
movl 0x8(%r12), %ebx
leaq 0x54257(%rip), %rsi     # 0x6ed44
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x25a7d
movl %ebx, %edi
movq %r14, %rsi
callq 0x2b6aa
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1ab20
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0xe0, %edi
callq 0x8290
movq %rax, %r15
leaq 0x8(%rbx), %rsi
leaq 0x20(%rsp), %rdi
callq 0x28816
movb $0x1, %bpl
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movl $0xa, %esi
callq 0x2574c
movq %rsp, %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x11182
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1aadb
movq 0x38(%rsp), %rsi
jmp 0x1aaa9
jmp 0x1ab8d
jmp 0x1abc1
movq %rax, %rbx
jmp 0x1abb7
jmp 0x1abc1
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1abb2
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %bpl, %bpl
je 0x1abf2
movl $0xe0, %esi
movq %r15, %rdi
jmp 0x1abe8
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x1abe0
jmp 0x1abf2
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x1abf2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x1abf2
movq %rax, %rbx
movq %rbx, %rdi
callq 0x8540