// File: /workspace/github_cpp_makefile_star2_O2/JameyWoo[P]csmile/assembly.cpp
// Function: genCall(TreeNode*, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>)
void genCall(TreeNode* call, map<string, string> var2stack) {
    // ! 结果在 %eax中
    if (call == NULL) return;
    // debug("call: " + call->child[0]->nodekind);
    // 解析 Call 的参数
    // call->child[0] 是这个函数的名字
    // call->child[1] 如果不为 NULL 的话, 是 Args 类型的
    // 解析args, arg可以是expression, 种类比较多. 考虑是不是要综合一个整体的
    TreeNode* args = call->child[1];
    if (args == NULL) {
        // TODO: call 没有参数的情况
        assout << "\tcall\t" << call->child[0]->name << endl;
        return;
    }
    // debug("args: " + args->child[0]->nodekind);
    // debug("args: " + args->child[0]->sibling->nodekind);

    // 传递参数
    stack<TreeNode*> call_stack;
    TreeNode* arg = args->child[0];
    int cnt       = 0;
    while (arg != NULL) {
        cnt += 1;
        call_stack.push(arg);
        arg = arg->sibling;
    }
    // 调整栈的位置, 防止参数过多
    cnt -= 1;
    assout << "\tsubl\t$" << 4 * cnt << ", %esp" << endl;
    while (not call_stack.empty()) {
        string stack_loc = to_string(4 * cnt) + "(%esp)";
        TreeNode* tmp    = call_stack.top();
        // debug("arg type: " + tmp->nodekind);
        string left = "";
        if (tmp->nodekind == "Const") {
            left += "$" + to_string(tmp->val);
        } else if (tmp->nodekind == "Id") {
            assout << "\tmovl\t" << var2stack[tmp->name] << ", %edx" << endl;
            left += "%edx";
        } else if (tmp->nodekind == "PMOp" || tmp->nodekind == "MDOp") {
            // 一个计算的表达式
            genCalc(tmp, var2stack);
            left += "%edx";
        }
        assout << "\tmovl\t" << left << ", " << stack_loc << endl;
        // TODO: 支持更多的参数类型

        call_stack.pop();
        cnt -= 1;
    }
    // 调用函数
    assout << "\tcall\t" << call->child[0]->name << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, (%rsp)
testq %rdi, %rdi
je 0xaeaf
movq 0x8(%rdi), %r15
testq %r15, %r15
movq %rdi, %rbx
je 0xaec1
movq %rbx, 0x10(%rsp)
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0xcec0
pushq $-0x4
popq %rbp
leaq 0x8(%rsp), %r14
movq (%r15), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xac3b
movq %rbx, %rdi
movq %r14, %rsi
callq 0xdfce
movq 0x8(%rsp), %r15
addq $0x20, %r15
addl $0x4, %ebp
jmp 0xac15
leaq 0xbf9e(%rip), %rdi      # 0x16be0
leaq 0x4b88(%rip), %rsi      # 0xf7d1
callq 0x21b0
movq %rax, %rdi
movl %ebp, %esi
callq 0x22f0
leaq 0x4b7a(%rip), %rsi      # 0xf7d9
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20f0
leaq 0xa8(%rsp), %r15
leaq 0xc8(%rsp), %r13
leaq 0xe8(%rsp), %r14
movq 0x48(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0xae75
movq %r15, %rdi
movl %ebp, %esi
callq 0x7cbf
movq %r13, %rdi
movq %r15, %rsi
leaq 0x4aa1(%rip), %rdx      # 0xf74f
callq 0x7c7c
movq %r15, %rdi
callq 0x2358
movq 0x48(%rsp), %rax
cmpq 0x50(%rsp), %rax
jne 0xacd8
movq 0x60(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %r12
movq %r15, %rdi
leaq 0x4a2f(%rip), %rsi      # 0xf715
leaq 0x88(%rsp), %rdx
callq 0x2efc
leaq 0x30(%r12), %rbx
movq %rbx, %rdi
leaq 0x4563(%rip), %rsi      # 0xf265
callq 0x368e
testb %al, %al
je 0xad5c
movl 0xb8(%r12), %esi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x7cbf
leaq 0x88(%rsp), %r12
movq %r12, %rdi
leaq 0x4aa5(%rip), %rsi      # 0xf7d7
movq %rbx, %rdx
callq 0x7c9b
movq %r15, %rdi
movq %r12, %rsi
callq 0x2340
movq %r12, %rdi
callq 0x2358
leaq 0x68(%rsp), %rdi
callq 0x2358
jmp 0xae13
movq %rbx, %rdi
leaq 0x44c2(%rip), %rsi      # 0xf228
callq 0x368e
testb %al, %al
je 0xadbc
leaq 0xbe6a(%rip), %rdi      # 0x16be0
leaq 0x4999(%rip), %rsi      # 0xf716
callq 0x21b0
movq %rax, %rbx
addq $0xc0, %r12
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x7c16
movq %rbx, %rdi
movq %rax, %rsi
callq 0x21a0
movq %rax, %rdi
leaq 0x4aab(%rip), %rsi      # 0xf858
callq 0x21b0
movq %rax, %rdi
callq 0x20f0
jmp 0xae04
movq %rbx, %rdi
leaq 0x44aa(%rip), %rsi      # 0xf270
callq 0x368e
testb %al, %al
jne 0xade2
movq %rbx, %rdi
leaq 0x4492(%rip), %rsi      # 0xf26b
callq 0x368e
testb %al, %al
je 0xae13
movq %r14, %rdi
movq (%rsp), %rsi
callq 0xd3ce
movq %r12, %rdi
movq %r14, %rbx
movq %r14, %rsi
callq 0xc22d
movq %rbx, %rdi
callq 0x7b9c
movq %r15, %rdi
leaq 0x4a4c(%rip), %rsi      # 0xf85a
callq 0x2320
leaq 0xbdc6(%rip), %rdi      # 0x16be0
leaq 0x48f5(%rip), %rsi      # 0xf716
callq 0x21b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movq %rax, %rdi
leaq 0x49dc(%rip), %rsi      # 0xf817
callq 0x21b0
movq %rax, %rdi
movq %r13, %rsi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
callq 0xe202
movq %r15, %rdi
callq 0x2358
movq %r13, %rdi
callq 0x2358
addl $-0x4, %ebp
jmp 0xac87
leaq 0xbd64(%rip), %rdi      # 0x16be0
leaq 0x4947(%rip), %rsi      # 0xf7ca
callq 0x21b0
movl $0xc0, %esi
movq 0x10(%rsp), %rcx
addq (%rcx), %rsi
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
callq 0xd552
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbd18(%rip), %rdi      # 0x16be0
leaq 0x48fb(%rip), %rsi      # 0xf7ca
callq 0x21b0
movl $0xc0, %esi
addq (%rbx), %rsi
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x7b9c
jmp 0xaf50
jmp 0xaf48
jmp 0xaf4d
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2358
jmp 0xaf28
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x2358
jmp 0xaf50
jmp 0xaf48
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0xaf65
movq %rax, %rbx
jmp 0xaf5d
movq %rax, %rbx
jmp 0xaf6a
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x2358
leaq 0xc8(%rsp), %rdi
callq 0x2358
leaq 0x18(%rsp), %rdi
callq 0xd552
movq %rbx, %rdi
callq 0x2300