// File: 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:
subq $0x188, %rsp            # imm = 0x188
movq %rdi, 0x180(%rsp)
movq %rsi, 0x178(%rsp)
movq %rdx, 0x170(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq $0x0, 0x168(%rsp)
movq 0x178(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x164(%rsp)
cmpl $0x4, 0x164(%rsp)
jne 0x8776d
movq 0xc48cb(%rip), %rdi     # 0x14bfe8
leaq 0x729d4(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, %rdi
leaq 0x76172(%rip), %rsi     # 0xfd8a5
callq 0x8330
movq %rax, %rdi
leaq 0x73ca8(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, %rdi
movl $0x3f, %esi
callq 0x8650
movq %rax, %rdi
movq 0xc4822(%rip), %rsi     # 0x14bf80
callq 0x8380
movl $0xff, %edi
callq 0x8460
cmpl $0x1, 0x164(%rsp)
jne 0x877e1
movl $0x50, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xb8(%rsp)
movq 0x178(%rsp), %rax
movl 0x4(%rax), %esi
callq 0x8cfa0
jmp 0x877a1
movq 0xb8(%rsp), %rax
movq %rax, 0x168(%rsp)
jmp 0x87d4e
movq 0xb8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
movl $0x50, %esi
callq 0x8350
jmp 0x87dc3
cmpl $0x3, 0x164(%rsp)
je 0x877fe
movq 0x178(%rsp), %rcx
xorl %eax, %eax
cmpl 0x28(%rcx), %eax
jne 0x87b58
movq 0x178(%rsp), %rsi
leaq 0x130(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x43590
movq 0xc0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movl $0x1, %edx
callq 0x87430
movq %rax, 0xb0(%rsp)
jmp 0x8783f
movq 0xb0(%rsp), %rax
movq %rax, 0x168(%rsp)
xorl %eax, %eax
cmpq 0x168(%rsp), %rax
je 0x8789d
movq 0x168(%rsp), %rdi
callq 0x5e940
movq %rax, 0xa0(%rsp)
jmp 0x87872
movq 0xa0(%rsp), %rdi
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
movb %al, 0x9f(%rsp)
jmp 0x8788c
movb 0x9f(%rsp), %al
testb $0x1, %al
jne 0x87945
jmp 0x8789d
movq 0xc4744(%rip), %rdi     # 0x14bfe8
leaq 0x7284d(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, 0x90(%rsp)
jmp 0x878ba
movq 0x90(%rsp), %rdi
leaq 0x75fdc(%rip), %rsi     # 0xfd8a5
callq 0x8330
movq %rax, 0x88(%rsp)
jmp 0x878d8
movq 0x88(%rsp), %rdi
leaq 0x73b03(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, 0x80(%rsp)
jmp 0x878f6
movq 0x80(%rsp), %rdi
movl $0x45, %esi
callq 0x8650
movq %rax, 0x78(%rsp)
jmp 0x8790f
movq 0x78(%rsp), %rdi
movq 0xc4665(%rip), %rsi     # 0x14bf80
callq 0x8380
jmp 0x87922
movl $0xff, %edi
callq 0x8460
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x87b46
cmpl $0x3, 0x164(%rsp)
je 0x879b5
movq 0x170(%rsp), %rdi
callq 0x5e2c0
movq %rax, 0x70(%rsp)
jmp 0x87963
movq 0x168(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
je 0x87999
movq 0x68(%rsp), %rdi
leaq 0xc3b4e(%rip), %rsi     # 0x14b4d0
leaq 0xc3b57(%rip), %rdx     # 0x14b4e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x60(%rsp)
jmp 0x879a2
xorl %eax, %eax
movq %rax, 0x60(%rsp)
jmp 0x879a2
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x614c0
jmp 0x879b3
jmp 0x879b5
cmpl $0x0, 0x164(%rsp)
jne 0x87b34
movl $0x68, %edi
callq 0x8340
movq %rax, 0x58(%rsp)
jmp 0x879d4
movb $0x1, 0xef(%rsp)
movl $0x10, %edi
callq 0x8340
movq %rax, 0x50(%rsp)
jmp 0x879ed
movq 0x50(%rsp), %rdi
movl $0x2, %esi
callq 0x5e410
jmp 0x879fe
movl 0xc49ac(%rip), %esi     # 0x14c3b0
movl %esi, %eax
incl %eax
movl %eax, 0xc49a2(%rip)     # 0x14c3b0
leaq 0xf0(%rsp), %rdi
callq 0xc7b0
jmp 0x87a1d
leaq 0x75e9a(%rip), %rsi     # 0xfd8be
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0xc970
jmp 0x87a3b
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdi
movq 0x168(%rsp), %rcx
movq 0x170(%rsp), %r8
leaq 0x110(%rsp), %rdx
callq 0x8d0b0
jmp 0x87a64
movq 0x58(%rsp), %rax
movb $0x0, 0xef(%rsp)
movq %rax, 0x168(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x8728
leaq 0xf0(%rsp), %rdi
callq 0x8728
jmp 0x87b34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x87b17
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
movl $0x10, %esi
callq 0x8350
jmp 0x87b17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x87b0a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x8728
leaq 0xf0(%rsp), %rdi
callq 0x8728
testb $0x1, 0xef(%rsp)
jne 0x87b23
jmp 0x87b32
movq 0x58(%rsp), %rdi
movl $0x68, %esi
callq 0x8350
jmp 0x87b46
leaq 0x130(%rsp), %rdi
callq 0x8728
jmp 0x87d4c
leaq 0x130(%rsp), %rdi
callq 0x8728
jmp 0x87dc3
movq 0x178(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x43590
movq 0xc0(%rsp), %rdi
movq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x87430
movq %rax, 0x48(%rsp)
jmp 0x87b8d
movq 0x48(%rsp), %rax
movq %rax, 0x168(%rsp)
xorl %eax, %eax
cmpq 0x168(%rsp), %rax
jne 0x87d2e
cmpl $0x0, 0x164(%rsp)
je 0x87c4e
movq 0xc4429(%rip), %rdi     # 0x14bfe8
leaq 0x72532(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, 0x38(%rsp)
jmp 0x87bd2
movq 0x38(%rsp), %rdi
leaq 0x75cc7(%rip), %rsi     # 0xfd8a5
callq 0x8330
movq %rax, 0x30(%rsp)
jmp 0x87bea
movq 0x30(%rsp), %rdi
leaq 0x737f4(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, 0x28(%rsp)
jmp 0x87c02
movq 0x28(%rsp), %rdi
movl $0x58, %esi
callq 0x8650
movq %rax, 0x20(%rsp)
jmp 0x87c18
movq 0x20(%rsp), %rdi
movq 0xc435c(%rip), %rsi     # 0x14bf80
callq 0x8380
jmp 0x87c2b
movl $0xff, %edi
callq 0x8460
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x87d3d
movl $0x48, %edi
callq 0x8340
movq %rax, 0x18(%rsp)
jmp 0x87c5f
movl $0x10, %edi
callq 0x8340
movq %rax, 0x10(%rsp)
jmp 0x87c70
movq 0x10(%rsp), %rdi
movl $0x2, %esi
callq 0x5e410
jmp 0x87c81
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0x8d180
jmp 0x87c9a
movq 0xc0(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x168(%rsp), %rax
movq %rax, (%rsp)
addq $0xa8, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x8cd60
movq %rax, 0x8(%rsp)
jmp 0x87cd6
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
jmp 0x87d2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x87d1d
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
movl $0x10, %esi
callq 0x8350
movq 0x18(%rsp), %rdi
movl $0x48, %esi
callq 0x8350
jmp 0x87d3d
leaq 0xc8(%rsp), %rdi
callq 0x8728
jmp 0x87d4c
leaq 0xc8(%rsp), %rdi
callq 0x8728
jmp 0x87dc3
jmp 0x87d4e
jmp 0x87d50
xorl %eax, %eax
cmpq 0x168(%rsp), %rax
jne 0x87db3
movq 0xc4285(%rip), %rdi     # 0x14bfe8
leaq 0x7238e(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, %rdi
leaq 0x75b2c(%rip), %rsi     # 0xfd8a5
callq 0x8330
movq %rax, %rdi
leaq 0x73662(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, %rdi
movl $0x5e, %esi
callq 0x8650
movq %rax, %rdi
movq 0xc41dc(%rip), %rsi     # 0x14bf80
callq 0x8380
movl $0xff, %edi
callq 0x8460
movq 0x168(%rsp), %rax
addq $0x188, %rsp            # imm = 0x188
retq
movq 0x158(%rsp), %rdi
callq 0x8670