// File: /workspace/github_cpp_makefile_star2_O2/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 $0x70, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x1e8(%rsi), %r15
leaq 0x6d09f(%rip), %rdi     # 0x81560
movq %r15, %rsi
callq 0xe8da
testq %rax, %rax
je 0x1450d
cmpl $0x122, 0x28(%rax)      # imm = 0x122
movq 0x10(%r12), %rdi
jne 0x14532
testq %rdi, %rdi
je 0x14583
movl 0x8(%r12), %ebp
leaq 0x42790(%rip), %rsi     # 0x56c81
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x1ae0c
movl %ebp, %edi
movq %r14, %rsi
callq 0x1f699
jmp 0x14577
leaq 0x4273f(%rip), %rsi     # 0x56c53
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0xa292
leaq 0x10(%rsp), %rdi
callq 0x1f6ea
jmp 0x145d7
testq %rdi, %rdi
je 0x145b1
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x10(%rax)
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
je 0x145ee
movl 0x8(%r12), %ebp
leaq 0x42747(%rip), %rsi     # 0x56ca4
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x1ae0c
movl %ebp, %edi
movq %r14, %rsi
callq 0x1f699
leaq 0x10(%rsp), %rdi
callq 0x8578
jmp 0x145ab
movl $0xe0, %edi
callq 0x8250
movq %rax, %r15
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x1ada6
leaq 0x10(%rsp), %rsi
movq %r15, (%rsi)
movq %r14, %rdi
callq 0xe358
movb $0x1, 0x48(%rbx)
jmp 0x145e1
movl 0x8(%r12), %ebx
leaq 0x426e7(%rip), %rsi     # 0x56ca4
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x1ae0c
movl %ebx, %edi
movq %r14, %rsi
callq 0x1f699
leaq 0x10(%rsp), %rdi
callq 0x8578
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0xe0, %edi
callq 0x8250
movq %rax, %r15
leaq 0x8(%rbx), %rsi
leaq 0x30(%rsp), %rdi
callq 0x1da22
movb $0x1, %bpl
pushq $0xa
popq %rsi
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
callq 0x1abbe
leaq 0x10(%rsp), %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq %r14, %rdi
callq 0xe358
leaq 0x38(%rsp), %rdi
jmp 0x1457c
jmp 0x1463c
jmp 0x14666
movq %rax, %rbx
jmp 0x14655
jmp 0x14666
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x14678
movl $0xe0, %esi
movq %r15, %rdi
callq 0x8260
jmp 0x14678
jmp 0x14666
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8578
jmp 0x14678
movq %rax, %rbx
movq %rbx, %rdi
callq 0x84d0