// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::UnaryExpression::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void UnaryExpression::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  rhs_.GenerateIR(ctx, gIRList);
  ir::Opn opn1 = ctx.opn_;
  CHECK_OPN_INT("rhs exp");

  if (opn1.type_ == OpnType::Imm) {
    int result;
    int num1 = opn1.imm_num_;
    switch (op_) {
      case ADD:
        result = num1;
        break;
      case SUB:
        result = -num1;
        break;
      case NOT:
        result = !num1;
        break;
      default:
        // printf("mystery operator code: %d", op_);
        MyAssert(0);
        break;
    }
    ctx.opn_ = ir::Opn(OpnType::Imm, result);
  } else {
    IROpKind op;
    switch (op_) {
      case ADD:
        // op = IROpKind::POS;
        return;
        break;
      case SUB:
        op = IROpKind::NEG;
        break;
      case NOT:
        op = IROpKind::NOT;
        break;
      default:
        // printf("mystery operator code: %d", op_);
        MyAssert(0);
        break;
    }
    std::string rhs_temp_var = ir::NewTemp();

    // NOTE: 中间变量栈偏移-1 并且不维护scope的dynamic offset
    ir::gScopes[ctx.scope_id_].symbol_table_.insert({rhs_temp_var, {false, false}});

    ir::Opn temp = ir::Opn(OpnType::Var, rhs_temp_var, ctx.scope_id_);
    ctx.opn_ = temp;
    gIRList.push_back(new ir::IR(op, opn1, temp));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
leaq 0x8(%r15), %r14
leaq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0x1da22
movq 0x50(%r15), %rax
cmpq 0x58(%r15), %rax
je 0x11108
cmpl $0x4, 0x8(%r15)
jne 0x11108
movl 0x8(%r12), %ebp
leaq 0x10(%r15), %rsi
leaq 0x461ab(%rip), %rdx     # 0x57250
movq %rsp, %rdi
callq 0xa334
leaq 0x45cc5(%rip), %rdx     # 0x56d79
leaq 0xb0(%rsp), %rdi
movq %rsp, %rsi
callq 0xa440
leaq 0x45a1a(%rip), %rdx     # 0x56ae5
leaq 0x20(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xa440
leaq 0x20(%rsp), %rsi
movl %ebp, %edi
callq 0x1f699
leaq 0x20(%rsp), %rdi
callq 0x8578
leaq 0xb0(%rsp), %rdi
callq 0x8578
movq %rsp, %rdi
callq 0x8578
cmpl $0x1, 0x100(%rsp)
jne 0x1113b
movl 0x104(%rsp), %edx
movl 0xc(%r12), %eax
cmpl $0x102, %eax            # imm = 0x102
je 0x1116a
cmpl $0x104, %eax            # imm = 0x104
je 0x11161
cmpl $0x103, %eax            # imm = 0x103
jne 0x112e5
negl %edx
jmp 0x1116a
movl 0xc(%r12), %eax
cmpl $0x102, %eax            # imm = 0x102
je 0x112c8
cmpl $0x103, %eax            # imm = 0x103
je 0x1118e
cmpl $0x104, %eax            # imm = 0x104
jne 0x1132d
pushq $0x5
jmp 0x11190
xorl %eax, %eax
testl %edx, %edx
sete %al
movl %eax, %edx
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x1db84
leaq 0x28(%rsp), %rdi
jmp 0x112c3
pushq $0x6
popq %rbp
movq %rsp, %rdi
callq 0x1d95d
movslq (%r15), %r12
leaq 0x703a5(%rip), %rax     # 0x81548
shlq $0x6, %r12
addq (%rax), %r12
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1a960
leaq 0x20(%rsp), %rdi
movq %rsp, %rsi
leaq 0xb0(%rsp), %rdx
callq 0x1a9e2
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x1c154
leaq 0x20(%rsp), %rdi
callq 0x1aa18
leaq 0xb0(%rsp), %rdi
callq 0x1aa2e
leaq 0x90(%rsp), %rdi
movq %rsp, %rsi
callq 0x8390
movl (%r15), %ecx
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
xorl %esi, %esi
callq 0x1a8d4
leaq 0x90(%rsp), %rdi
callq 0x8578
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x1daba
movl $0xe0, %edi
callq 0x8250
movq %rax, %r14
leaq 0x180(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x1da22
leaq 0x140(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movb $0x1, %r15b
leaq 0x180(%rsp), %rdx
leaq 0x140(%rsp), %rcx
movq %r14, %rdi
movl %ebp, %esi
callq 0x1aa52
leaq 0xb0(%rsp), %rsi
movq %r14, (%rsi)
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xe358
leaq 0x148(%rsp), %rdi
callq 0x8578
leaq 0x188(%rsp), %rdi
callq 0x8578
leaq 0x28(%rsp), %rdi
callq 0x8578
movq %rsp, %rdi
callq 0x8578
leaq 0x108(%rsp), %rdi
callq 0x8578
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x6fcfc(%rip), %rdi     # 0x80fe8
leaq 0x44df5(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x457f4(%rip), %rsi     # 0x56af3
movq %rax, %rdi
callq 0x8240
leaq 0x46012(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x13c, %esi            # imm = 0x13C
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x11373
movq 0x6fcb4(%rip), %rdi     # 0x80fe8
leaq 0x44dad(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x457ac(%rip), %rsi     # 0x56af3
movq %rax, %rdi
callq 0x8240
leaq 0x45fca(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x14f, %esi            # imm = 0x14F
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
jmp 0x11408
jmp 0x11408
movq %rax, %rbx
movb $0x1, %r15b
jmp 0x1141d
movq %rax, %rbx
jmp 0x1142f
jmp 0x1139c
movq %rax, %rbx
jmp 0x1143c
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x11441
jmp 0x11403
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x1aa18
jmp 0x113c8
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x1aa2e
jmp 0x11446
jmp 0x11403
jmp 0x11408
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x11449
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8578
jmp 0x113f9
jmp 0x11408
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x11441
movq %rax, %rbx
jmp 0x11446
movq %rax, %rbx
jmp 0x1144e
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x8578
leaq 0x188(%rsp), %rdi
callq 0x8578
testb %r15b, %r15b
je 0x1143c
movl $0xe0, %esi
movq %r14, %rdi
callq 0x8260
leaq 0x28(%rsp), %rdi
callq 0x8578
movq %rsp, %rdi
callq 0x8578
leaq 0x108(%rsp), %rdi
callq 0x8578
movq %rbx, %rdi
callq 0x84d0
nop