// File: /workspace/github_cpp_makefile_star2_O1/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 0x75a85(%rip), %rdi     # 0x90590
movq %r15, %rsi
callq 0x11a14
testq %rax, %rax
je 0x1ab58
cmpl $0x122, 0x28(%rax)      # imm = 0x122
movq 0x10(%r12), %rdi
jne 0x1ab91
testq %rdi, %rdi
je 0x1abf5
movl 0x8(%r12), %ebp
leaq 0x551e6(%rip), %rsi     # 0x6fd21
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x253b1
movl %ebp, %edi
movq %r14, %rsi
callq 0x2b12a
jmp 0x1abd8
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x5518b(%rip), %rsi     # 0x6fcf3
leaq 0x551b1(%rip), %rdx     # 0x6fd20
movq %rsp, %rdi
callq 0xcaa0
movq %rsp, %rdi
callq 0x2b17b
movq (%rsp), %rdi
cmpq %r14, %rdi
jne 0x1ac55
jmp 0x1ac62
testq %rdi, %rdi
je 0x1ac23
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x10(%rax)
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
je 0x1ac6f
movl 0x8(%r12), %ebp
leaq 0x55184(%rip), %rsi     # 0x6fd44
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x253b1
movl %ebp, %edi
movq %r14, %rsi
callq 0x2b12a
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1ac1d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x1ac1d
movl $0xe0, %edi
callq 0x8290
movq %rax, %r15
movq %rax, %rdi
movl $0xa, %esi
callq 0x2530e
movq %rsp, %rsi
movq %r15, (%rsi)
movq %r14, %rdi
callq 0x114b8
movb $0x1, 0x48(%rbx)
jmp 0x1ac62
movl 0x8(%r12), %ebx
leaq 0x55115(%rip), %rsi     # 0x6fd44
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x253b1
movl %ebx, %edi
movq %r14, %rsi
callq 0x2b12a
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1ac62
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 0x28186
movb $0x1, %bpl
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movl $0xa, %esi
callq 0x25080
movq %rsp, %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x114b8
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1ac1d
movq 0x38(%rsp), %rsi
jmp 0x1abeb
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x1acfa
jmp 0x1ad09
movq %rax, %rbx
jmp 0x1acff
jmp 0x1ad09
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1acfa
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %bpl, %bpl
je 0x1ad3a
movl $0xe0, %esi
movq %r15, %rdi
jmp 0x1ad30
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x1ad28
jmp 0x1ad3a
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x1ad3a
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x1ad3a
movq %rax, %rbx
movq %rbx, %rdi
callq 0x8540