// File: taurheim[P]AgNES/codeGenerator.cpp
// Function: CodeGenerator::generateBeginFunc(TAC)
void CodeGenerator::generateBeginFunc(TAC tac) {
    std::string funcName = tac.first;
    int paramSize = stoi(tac.second);

    code << "; beginfunc " << funcName << nl;
    
    // Allocate space for the locals
    code << "; Allocating " << paramSize << " for locals" << nl;
    allocateStackSpace(paramSize);

    // Create a map for the local param names
    currentLocalOffset = 1;
    currentParamMap.clear();
    int paramCount = 1;
    STEntry * functionScope = symbolTable->lookupScope(funcName);
    STEntry * currentEntry = functionScope->next;
    std::list<std::string> paramNames;
    while (currentEntry != nullptr) {
        std::string name = currentEntry->name;
        paramNames.push_front(name); 
        currentEntry = currentEntry->next;
    }
    for (auto & name : paramNames) {
        currentParamMap[name] = (paramCount++);
        code << "; arg " << name << " has stack offset " << currentParamMap[name] << nl; 
    } 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rsi, -0xe0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
addq $0x8, %rsi
leaq -0x30(%rbp), %rdi
callq 0x3140
movq -0xe0(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x21c70
movl %eax, -0xd4(%rbp)
jmp 0x1fe53
movq -0xe8(%rbp), %rdi
movl -0xd4(%rbp), %eax
movl %eax, -0x34(%rbp)
subq $-0x80, %rdi
leaq 0x4ce2(%rip), %rsi      # 0x24b50
callq 0x32e0
movq %rax, -0xf0(%rbp)
jmp 0x1fe7c
movq -0xf0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x32a0
movq %rax, -0xf8(%rbp)
jmp 0x1fe95
movq -0xe8(%rbp), %rsi
movq -0xf8(%rbp), %rdi
callq 0x32a0
jmp 0x1feaa
movq -0xe8(%rbp), %rdi
subq $-0x80, %rdi
leaq 0x4ca1(%rip), %rsi      # 0x24b5d
callq 0x32e0
movq %rax, -0x100(%rbp)
jmp 0x1feca
movq -0x100(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x3500
movq %rax, -0x108(%rbp)
jmp 0x1fee2
movq -0x108(%rbp), %rdi
leaq 0x4c7b(%rip), %rsi      # 0x24b6b
callq 0x32e0
movq %rax, -0x110(%rbp)
jmp 0x1fefe
movq -0xe8(%rbp), %rsi
movq -0x110(%rbp), %rdi
callq 0x32a0
jmp 0x1ff13
movq -0xe8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x219d0
jmp 0x1ff24
movq -0xe8(%rbp), %rdi
movl $0x1, 0x298(%rdi)
addq $0x258, %rdi            # imm = 0x258
callq 0x21cb0
movq -0xe8(%rbp), %rax
movl $0x1, -0x48(%rbp)
movq 0x288(%rax), %rax
movq %rax, -0x118(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3140
jmp 0x1ff6c
movq -0x118(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1bc30
movq %rax, -0x120(%rbp)
jmp 0x1ff85
leaq -0x70(%rbp), %rdi
callq 0x31d0
movq -0x120(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x21cd0
cmpq $0x0, -0x78(%rbp)
je 0x2005a
movq -0x78(%rbp), %rsi
addq $0x38, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x3140
jmp 0x1ffd2
leaq -0x90(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x21cf0
jmp 0x1ffe7
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x31d0
jmp 0x1ffb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x201e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31d0
jmp 0x201e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x201dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x31d0
jmp 0x201dc
leaq -0x90(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x21d30
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x21d60
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x21d90
testb $0x1, %al
jne 0x200aa
jmp 0x201be
leaq -0xc0(%rbp), %rdi
callq 0x21dc0
movq -0xe8(%rbp), %rdi
movq %rax, -0xd0(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x12c(%rbp)
incl %eax
movl %eax, -0x48(%rbp)
addq $0x258, %rdi            # imm = 0x258
movq -0xd0(%rbp), %rsi
callq 0x21b90
movq %rax, -0x128(%rbp)
jmp 0x200ee
movq -0xe8(%rbp), %rdi
movq -0x128(%rbp), %rax
movl -0x12c(%rbp), %ecx
movl %ecx, (%rax)
subq $-0x80, %rdi
leaq 0x4a68(%rip), %rsi      # 0x24b77
callq 0x32e0
movq %rax, -0x138(%rbp)
jmp 0x2011d
movq -0x138(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x32a0
movq %rax, -0x140(%rbp)
jmp 0x20139
movq -0x140(%rbp), %rdi
leaq 0x4a37(%rip), %rsi      # 0x24b7e
callq 0x32e0
movq %rax, -0x148(%rbp)
jmp 0x20155
movq -0xe8(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
movq -0xd0(%rbp), %rsi
callq 0x21b90
movq %rax, -0x150(%rbp)
jmp 0x20178
movq -0x148(%rbp), %rdi
movq -0x150(%rbp), %rax
movl (%rax), %esi
callq 0x3500
movq %rax, -0x158(%rbp)
jmp 0x20196
movq -0xe8(%rbp), %rsi
movq -0x158(%rbp), %rdi
callq 0x32a0
jmp 0x201ab
jmp 0x201ad
leaq -0xc0(%rbp), %rdi
callq 0x21de0
jmp 0x2008e
leaq -0x90(%rbp), %rdi
callq 0x21e00
leaq -0x30(%rbp), %rdi
callq 0x31d0
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x90(%rbp), %rdi
callq 0x21e00
leaq -0x30(%rbp), %rdi
callq 0x31d0
movq -0x40(%rbp), %rdi
callq 0x3520
nopw (%rax,%rax)