// File: luooofan[P]hblzbd_compiler/src/Pass/generate_arm_from_ssa.cpp
// Function: GenerateArmFromSSA::GenImmLdrStrInst(ArmBasicBlock*, arm::LdrStr::OpKind, arm::Reg*, arm::Reg*, int, bool)
void GenerateArmFromSSA::GenImmLdrStrInst(ArmBasicBlock* armbb, LdrStr::OpKind opkind, Reg* rd, Reg* rn, int imm,
                                          bool record) {
  Instruction* inst = nullptr;
  if (record) {
    // NOTE: sp_arg_fixup待补充的最后的栈空间大小+被调用者保护的寄存器的总大小
    auto vreg = NewVirtualReg();
    inst = new LdrPseudo(Cond::AL, vreg, imm, armbb);
    new LdrStr(opkind, rd, rn, new Operand2(vreg), armbb);
    this->sp_arg_fixup.insert(inst);
  } else {
    if (LdrStr::CheckImm12(imm)) {
      new LdrStr(opkind, rd, rn, imm, armbb);
    } else {
      auto vreg = NewVirtualReg();
      if (Operand2::CheckImm8m(imm)) {  // mov
        new Move(vreg, new Operand2(imm), armbb);
      } else if (imm < 0 && Operand2::CheckImm8m(-imm - 1)) {  // mvn
        new Move(vreg, new Operand2(-imm - 1), armbb, true);
      } else {  // ldr-pseudo
        new LdrPseudo(vreg, imm, armbb);
      }
      new LdrStr(opkind, rd, rn, new Operand2(vreg), armbb);
    }
  }
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movb 0x120(%rsp), %al
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movl %edx, 0x104(%rsp)
movq %rcx, 0xf8(%rsp)
movq %r8, 0xf0(%rsp)
movl %r9d, 0xec(%rsp)
andb $0x1, %al
movb %al, 0xeb(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq $0x0, 0xe0(%rsp)
testb $0x1, 0xeb(%rsp)
je 0xab5f2
movq 0xa8(%rsp), %rdi
callq 0xaaf80
movq %rax, 0xd8(%rsp)
movl $0x48, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xa0(%rsp)
movq 0xd8(%rsp), %rdx
movl 0xec(%rsp), %ecx
movq 0x108(%rsp), %r8
xorl %esi, %esi
movl $0x1, %r9d
callq 0x78010
jmp 0xab493
movq 0xa0(%rsp), %rax
movq %rax, 0xe0(%rsp)
movl $0x40, %edi
callq 0x8340
movq %rax, 0x78(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x84(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x90(%rsp)
movl $0x18, %edi
callq 0x8340
movq %rax, 0x98(%rsp)
jmp 0xab4f4
movq 0x98(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x78090
jmp 0xab50b
movq 0x98(%rsp), %r8
movq 0x90(%rsp), %rcx
movq 0x88(%rsp), %rdx
movl 0x84(%rsp), %esi
movq 0x78(%rsp), %rdi
movq 0x108(%rsp), %r9
movq %rsp, %rax
movl $0x1, (%rax)
callq 0xb21c0
jmp 0xab547
movq 0xa8(%rsp), %rdi
addq $0x98, %rdi
leaq 0xe0(%rsp), %rsi
callq 0xd470
movq %rax, 0xb8(%rsp)
movb %dl, 0xc0(%rsp)
jmp 0xab97f
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x48, %esi
callq 0x8350
jmp 0xab987
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0xab5de
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x18, %esi
callq 0x8350
movq 0x78(%rsp), %rdi
movl $0x40, %esi
callq 0x8350
jmp 0xab987
movl 0xec(%rsp), %edi
callq 0x77b80
testb $0x1, %al
jne 0xab604
jmp 0xab67d
movl $0x40, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
movl 0x104(%rsp), %esi
movq 0xf8(%rsp), %rdx
movq 0xf0(%rsp), %rcx
movl 0xec(%rsp), %r8d
movq 0x108(%rsp), %r9
movq %rsp, %rax
movl $0x1, (%rax)
callq 0xb2280
jmp 0xab650
jmp 0xab97d
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0xab987
movq 0xa8(%rsp), %rdi
callq 0xaaf80
movq %rax, 0xb0(%rsp)
movl 0xec(%rsp), %edi
callq 0xa450
testb $0x1, %al
jne 0xab6a7
jmp 0xab760
movl $0x28, %edi
callq 0x8340
movq %rax, 0x58(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x18, %edi
callq 0x8340
movq %rax, 0x68(%rsp)
jmp 0xab6d4
movq 0x68(%rsp), %rdi
movl 0xec(%rsp), %esi
callq 0x77f00
jmp 0xab6e7
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdi
movq 0x108(%rsp), %rcx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xb2060
jmp 0xab70e
jmp 0xab8a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0xab74c
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x18, %esi
callq 0x8350
movq 0x58(%rsp), %rdi
movl $0x28, %esi
callq 0x8350
jmp 0xab987
cmpl $0x0, 0xec(%rsp)
jge 0xab843
xorl %edi, %edi
subl 0xec(%rsp), %edi
subl $0x1, %edi
callq 0xa450
testb $0x1, %al
jne 0xab788
jmp 0xab843
movl $0x28, %edi
callq 0x8340
movq %rax, 0x40(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x18, %edi
callq 0x8340
movq %rax, 0x50(%rsp)
jmp 0xab7b5
movq 0x50(%rsp), %rdi
movl 0xec(%rsp), %esi
notl %esi
callq 0x77f00
jmp 0xab7ca
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
movq 0x108(%rsp), %rcx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0xb2060
jmp 0xab7f1
jmp 0xab8a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0xab82f
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x18, %esi
callq 0x8350
movq 0x40(%rsp), %rdi
movl $0x28, %esi
callq 0x8350
jmp 0xab987
movl $0x48, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq 0xb0(%rsp), %rsi
movl 0xec(%rsp), %edx
movq 0x108(%rsp), %rcx
movl $0x1, %r8d
callq 0xb2140
jmp 0xab87c
jmp 0xab8a6
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x48, %esi
callq 0x8350
jmp 0xab987
jmp 0xab8a8
movl $0x40, %edi
callq 0x8340
movq %rax, 0x10(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x18, %edi
callq 0x8340
movq %rax, 0x30(%rsp)
jmp 0xab8ed
movq 0x30(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x78090
jmp 0xab901
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rdx
movl 0x1c(%rsp), %esi
movq 0x10(%rsp), %rdi
movq 0x108(%rsp), %r9
movq %rsp, %rax
movl $0x1, (%rax)
callq 0xb21c0
jmp 0xab931
jmp 0xab97d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0xab96c
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x18, %esi
callq 0x8350
movq 0x10(%rsp), %rdi
movl $0x40, %esi
callq 0x8350
jmp 0xab987
jmp 0xab97f
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xd0(%rsp), %rdi
callq 0x8670
nopw %cs:(%rax,%rax)