// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/convert_ssa.cpp
// Function: ConvertSSA::ResolveOpn2Value(ir::Opn*, SSABasicBlock*)
Value* ConvertSSA::ResolveOpn2Value(Opn* opn, SSABasicBlock* ssabb) {
  // std::cout << "Resolve Begin:" << std::endl;
  Value* res = nullptr;
  auto type = opn->type_;
  if (type == Opn::Type::Null) {
    MyAssert(0);
  } else if (type == Opn::Type::Imm) {  // new一个常量value直接返回
    res = new ConstantInt(opn->imm_num_);
  } else if (type == Opn::Type::Func || 0 == opn->scope_id_) {
    auto comp_name = opn->GetCompName();
    res = this->FindValueFromCompName(comp_name, true);
    MyAssert(nullptr != res && res->GetType()->IsPointer());

    if (type != Opn::Type::Func) {  // var & array
      // maintain used_glob_var
      ssabb->GetFunction()->AddUsedGlobVar(dynamic_cast<GlobalVariable*>(res));
    }

    // new load load出的结果不需要记录 全局量每次都要load
    if (type == Opn::Type::Var) {
      static int glob_temp_index = 1;
      res = new LoadInst(new Type(Type::IntegerTyID), "glotmp-" + std::to_string(glob_temp_index++), res, ssabb);
    }  // 全局数组直接返回i32 pointer

  } else {  // label标签 或 局部变量数组 局部变量可能未定义 局部数组只会返回i32 pointer类型的value
    auto comp_name = opn->GetCompName();
    res = this->FindValueFromCompName(comp_name, false);

    if (nullptr == res) {  // 未定义变量
      // std::cout << "Undefined variable:" << std::string(*opn) << std::endl;
      MyAssert(type == Opn::Type::Var);
      // new undefval
      res = new UndefVariable(new Type(Type::IntegerTyID), comp_name);
      work_map[comp_name] = res;
    }
  }
  MyAssert(nullptr != res);
  return res;
  // std::cout << "Resolve End." << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rsi, %r12
movl (%rsi), %ebp
cmpl $0x1, %ebp
je 0x328f4
movq %rdx, %r14
movq %rdi, %r15
cmpl $0x3, %ebp
je 0x3287e
cmpl $0x4, %ebp
je 0x329ea
cmpl $0x0, 0x28(%r12)
je 0x3287e
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1dc16
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3269c
movq %rax, %rbx
testq %rax, %rax
jne 0x328ea
testl %ebp, %ebp
jne 0x32a38
pushq $0x48
popq %rdi
callq 0x8250
movq %rax, %rbx
pushq $0x10
popq %rdi
callq 0x8250
leaq 0x4e041(%rip), %rcx     # 0x80890
movq %rcx, (%rax)
movl $0x2, 0x8(%rax)
movq %rsp, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34d34
addq $0xa8, %r15
movq %rsp, %rsi
movq %r15, %rdi
callq 0x36e30
movq %rbx, (%rax)
jmp 0x328ea
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1dc16
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3269c
movq %rax, %r15
testq %rax, %rax
je 0x329a4
movq 0x8(%r15), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
testb %al, %al
je 0x329a4
cmpl $0x3, %ebp
je 0x328e7
movq (%r14), %rbx
leaq 0x4dbb9(%rip), %rsi     # 0x80480
leaq 0x4dbc2(%rip), %rdx     # 0x80490
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x82c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2824e
testl %ebp, %ebp
je 0x3291c
movq %r15, %rbx
movq %rsp, %rdi
callq 0x8578
jmp 0x3290c
pushq $0x50
popq %rdi
callq 0x8250
movq %rax, %rbx
movl 0x4(%r12), %esi
movq %rax, %rdi
callq 0x34c2c
movq %rbx, %rax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x68
popq %rdi
callq 0x8250
movq %rax, %rbx
pushq $0x10
popq %rdi
callq 0x8250
movq %rax, %r12
leaq 0x4df57(%rip), %rax     # 0x80890
movq %rax, (%r12)
movl $0x2, 0x8(%r12)
movl 0x4e98c(%rip), %esi     # 0x812d8
leal 0x1(%rsi), %eax
movl %eax, 0x4e983(%rip)     # 0x812d8
leaq 0x20(%rsp), %rdi
callq 0xa36c
leaq 0x26dbb(%rip), %rsi     # 0x59721
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0xa41c
leaq 0x40(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rcx
movq %r14, %r8
callq 0x34cbe
leaq 0x40(%rsp), %rdi
callq 0x8578
leaq 0x20(%rsp), %rdi
callq 0x8578
jmp 0x328ea
movq 0x4e63d(%rip), %rdi     # 0x80fe8
leaq 0x23736(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x26d4a(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x24953(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x45
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x32a2e
movq 0x4e5f7(%rip), %rdi     # 0x80fe8
leaq 0x236f0(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x26d04(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x2490d(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x3f
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
movq 0x4e5a9(%rip), %rdi     # 0x80fe8
leaq 0x236a2(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x26cb6(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x248bf(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x58
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x32a2e
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x8578
jmp 0x32a90
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x8578
jmp 0x32aa1
jmp 0x32a9e
movq %rax, %r14
pushq $0x68
jmp 0x32aaa
movq %rax, %r14
pushq $0x48
popq %rsi
movq %rbx, %rdi
callq 0x8260
jmp 0x32aca
movq %rax, %r14
pushq $0x50
popq %rsi
movq %rbx, %rdi
callq 0x8260
jmp 0x32ad2
jmp 0x32ac7
movq %rax, %r14
movq %rsp, %rdi
callq 0x8578
movq %r14, %rdi
callq 0x84d0