// File: 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:
subq $0x388, %rsp            # imm = 0x388
movq %rdi, 0x380(%rsp)
movq %rsi, 0x378(%rsp)
movq %rdx, 0x370(%rsp)
movq 0x380(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x10(%rax), %rdi
movq 0x378(%rsp), %rsi
movq 0x370(%rsp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x378(%rsp), %rsi
addq $0x8, %rsi
leaq 0x330(%rsp), %rdi
callq 0x43130
movq 0x378(%rsp), %rdi
addq $0x50, %rdi
callq 0x10a80
testb $0x1, %al
jne 0x23f84
movq 0x378(%rsp), %rax
cmpl $0x4, 0x8(%rax)
jne 0x23f84
movq 0x88(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x84(%rsp)
movq 0x378(%rsp), %rsi
addq $0x10, %rsi
leaq 0xd740e(%rip), %rdx     # 0xfb280
leaq 0x2d0(%rsp), %rdi
callq 0xc720
jmp 0x23e81
leaq 0xd6f08(%rip), %rdx     # 0xfad90
leaq 0x2f0(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0xcbd0
jmp 0x23e9f
leaq 0xd6c56(%rip), %rdx     # 0xfaafc
leaq 0x310(%rsp), %rdi
leaq 0x2f0(%rsp), %rsi
callq 0xcbd0
jmp 0x23ebd
movl 0x84(%rsp), %edi
leaq 0x310(%rsp), %rsi
callq 0x47ce0
jmp 0x23ed3
leaq 0x310(%rsp), %rdi
callq 0x8728
leaq 0x2f0(%rsp), %rdi
callq 0x8728
leaq 0x2d0(%rsp), %rdi
callq 0x8728
jmp 0x23f84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x24525
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x23f72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x23f65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x310(%rsp), %rdi
callq 0x8728
leaq 0x2f0(%rsp), %rdi
callq 0x8728
leaq 0x2d0(%rsp), %rdi
callq 0x8728
jmp 0x24525
cmpl $0x1, 0x330(%rsp)
jne 0x24109
movq 0x88(%rsp), %rax
movl 0x334(%rsp), %ecx
movl %ecx, 0x2bc(%rsp)
movl 0xc(%rax), %eax
movl %eax, 0x80(%rsp)
subl $0x102, %eax            # imm = 0x102
je 0x23fdb
jmp 0x23fbb
movl 0x80(%rsp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x23fee
jmp 0x23fcb
movl 0x80(%rsp), %eax
subl $0x104, %eax            # imm = 0x104
je 0x24003
jmp 0x2401e
movl 0x2bc(%rsp), %eax
movl %eax, 0x2c0(%rsp)
jmp 0x2409b
xorl %eax, %eax
subl 0x2bc(%rsp), %eax
movl %eax, 0x2c0(%rsp)
jmp 0x2409b
cmpl $0x0, 0x2bc(%rsp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x2c0(%rsp)
jmp 0x2409b
movq 0x127fc3(%rip), %rdi    # 0x14bfe8
leaq 0xd60cc(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, 0x78(%rsp)
jmp 0x24038
movq 0x78(%rsp), %rdi
leaq 0xd6ac6(%rip), %rsi     # 0xfab0a
callq 0x8330
movq %rax, 0x70(%rsp)
jmp 0x24050
movq 0x70(%rsp), %rdi
leaq 0xd738e(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, 0x68(%rsp)
jmp 0x24068
movq 0x68(%rsp), %rdi
movl $0x13c, %esi            # imm = 0x13C
callq 0x8650
movq %rax, 0x60(%rsp)
jmp 0x2407e
movq 0x60(%rsp), %rdi
movq 0x127ef6(%rip), %rsi    # 0x14bf80
callq 0x8380
jmp 0x24091
movl $0xff, %edi
callq 0x8460
movl 0x2c0(%rsp), %edx
leaq 0x278(%rsp), %rdi
movl $0x1, %esi
callq 0x1f160
jmp 0x240b6
movq 0x378(%rsp), %rdi
addq $0x8, %rdi
leaq 0x278(%rsp), %rsi
callq 0x433c0
jmp 0x240d1
leaq 0x278(%rsp), %rdi
callq 0x1f210
jmp 0x24505
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x278(%rsp), %rdi
callq 0x1f210
jmp 0x24525
movq 0x88(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x5c(%rsp)
subl $0x102, %eax            # imm = 0x102
je 0x2413b
jmp 0x24121
movl 0x5c(%rsp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x2414b
jmp 0x2412e
movl 0x5c(%rsp), %eax
subl $0x104, %eax            # imm = 0x104
je 0x2415b
jmp 0x24168
movl $0x1, 0x270(%rsp)
jmp 0x24510
movl $0x6, 0x274(%rsp)
jmp 0x241e5
movl $0x5, 0x274(%rsp)
jmp 0x241e5
movq 0x127e79(%rip), %rdi    # 0x14bfe8
leaq 0xd5f82(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, 0x50(%rsp)
jmp 0x24182
movq 0x50(%rsp), %rdi
leaq 0xd697c(%rip), %rsi     # 0xfab0a
callq 0x8330
movq %rax, 0x48(%rsp)
jmp 0x2419a
movq 0x48(%rsp), %rdi
leaq 0xd7244(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, 0x40(%rsp)
jmp 0x241b2
movq 0x40(%rsp), %rdi
movl $0x14f, %esi            # imm = 0x14F
callq 0x8650
movq %rax, 0x38(%rsp)
jmp 0x241c8
movq 0x38(%rsp), %rdi
movq 0x127dac(%rip), %rsi    # 0x14bf80
callq 0x8380
jmp 0x241db
movl $0xff, %edi
callq 0x8460
leaq 0x250(%rsp), %rdi
callq 0x43010
jmp 0x241f4
movq 0x378(%rsp), %rax
movslq (%rax), %rsi
leaq 0x128422(%rip), %rdi    # 0x14c628
callq 0x183c0
movq %rax, 0x30(%rsp)
leaq 0x190(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x366c0
jmp 0x24223
leaq 0x1e0(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0x367a0
jmp 0x24242
movq 0x30(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x36670
movb %dl, 0x27(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x2425f
movb 0x27(%rsp), %al
movq 0x28(%rsp), %rcx
movq %rcx, 0x180(%rsp)
movb %al, 0x188(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x36810
leaq 0x190(%rsp), %rdi
callq 0x36840
leaq 0x120(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x81a0
jmp 0x242a8
movq 0x378(%rsp), %rax
movl (%rax), %ecx
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
leaq 0x120(%rsp), %rdx
callq 0x36480
jmp 0x242cb
leaq 0x120(%rsp), %rdi
callq 0x8728
movq 0x378(%rsp), %rdi
addq $0x8, %rdi
leaq 0x140(%rsp), %rsi
callq 0x43230
jmp 0x242f3
movq 0x370(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, 0x18(%rsp)
jmp 0x24311
movb $0x1, 0x97(%rsp)
movl 0x274(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0x330(%rsp), %rsi
callq 0x43130
jmp 0x2433b
leaq 0x98(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x43130
jmp 0x24352
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
leaq 0x98(%rsp), %rcx
callq 0x36880
jmp 0x24372
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
movb $0x0, 0x97(%rsp)
movq %rax, 0x118(%rsp)
leaq 0x118(%rsp), %rsi
callq 0x185b0
jmp 0x2439b
leaq 0x98(%rsp), %rdi
callq 0x1f210
leaq 0xd8(%rsp), %rdi
callq 0x1f210
leaq 0x140(%rsp), %rdi
callq 0x1f210
leaq 0x250(%rsp), %rdi
callq 0x8728
jmp 0x24505
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x244f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x24424
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x36810
leaq 0x190(%rsp), %rdi
callq 0x36840
jmp 0x244f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x8728
jmp 0x244f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x244e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x244cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x244bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f210
leaq 0xd8(%rsp), %rdi
callq 0x1f210
testb $0x1, 0x97(%rsp)
jne 0x244d8
jmp 0x244e7
movq 0x18(%rsp), %rdi
movl $0xe0, %esi
callq 0x8350
jmp 0x244e9
leaq 0x140(%rsp), %rdi
callq 0x1f210
leaq 0x250(%rsp), %rdi
callq 0x8728
jmp 0x24525
movl $0x0, 0x270(%rsp)
leaq 0x330(%rsp), %rdi
callq 0x1f210
addq $0x388, %rsp            # imm = 0x388
retq
leaq 0x330(%rsp), %rdi
callq 0x1f210
movq 0x2c8(%rsp), %rdi
callq 0x8670
nop