// File: taurheim[P]AgNES/main.cpp
// Function: main
int main(int argc, char * argv[]) {
    if (argc < 3) {
        std::cout << "Did not provide enough arguments. Usage: AgNES input_file.c output_file.s\n";
        return 1;
    }
    char * inputFileName = argv[1];
    char * outputFileName = argv[2];

    // First we run Lexical Analysis, to turn the stream of characters into a list of tokens
    // Input: file
    // Output: list of tokens
    std::ifstream file (inputFileName);
    std::list<Token> tokens;
    if (file.is_open()) {
        Scanner scanner = Scanner(file);
        tokens = scanner.scan();
    } else {
        std::cout << "Could not open file \n";
        return 1;
    }

    // Next we run Syntax Analysis/Parsing, where we generate a parse tree
    // Input: list of tokens
    // Output: parse tree
    Parser parser = Parser(tokens);
    ASTNode * astRoot = parser.parse();

    //Now we make a SemanticAnalyzer!
    std::cout << "Running semantic analysis..." << std::endl;
    SemanticAnalyzer analyzer = SemanticAnalyzer(astRoot);
    SymbolTable * symbolTable = analyzer.analyze();

    // Generate intermediate code
    std::cout << "Running IR Generator..." << std::endl;
    IRGenerator irGenerator = IRGenerator(astRoot);
    std::list<TAC> intermediateCode = irGenerator.generate();

    //Generate machine code
    std::cout << "Running Code Generator..." << std::endl;
    CodeGenerator codeGenerator = CodeGenerator(intermediateCode, symbolTable);
    std::string machineCode = codeGenerator.generate();
    std::cout << machineCode;

    // Sandwich our machine code in the middle of the header and footer
    std::cout << "Writing to " << outputFileName << std::endl;
    std::ifstream header("NES_HEADER.s");
    std::string headerStr((std::istreambuf_iterator<char>(header)), std::istreambuf_iterator<char>());
    std::ifstream footer("NES_FOOTER.s");
    std::string footerStr((std::istreambuf_iterator<char>(footer)), std::istreambuf_iterator<char>());
    std::remove(outputFileName);
    std::ofstream outputFile;
    outputFile.open(outputFileName);

    outputFile << headerStr;
    outputFile << machineCode;
    outputFile << footerStr;

    header.close();
    footer.close();
    outputFile.close();

    std::cout << "Finished Compilation." << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd10, %rsp            # imm = 0xD10
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x6ffe
movq 0x2dfe2(%rip), %rdi     # 0x34fc8
leaq 0x1d0ff(%rip), %rsi     # 0x240ec
callq 0x32e0
movl $0x1, -0x4(%rbp)
jmp 0x753b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x228(%rbp), %rdi
movq %rdi, -0xcc0(%rbp)
movl $0x8, %edx
callq 0x3570
leaq -0x240(%rbp), %rdi
callq 0x82f0
movq -0xcc0(%rbp), %rdi
callq 0x34a0
movb %al, -0xcb1(%rbp)
jmp 0x7052
movb -0xcb1(%rbp), %al
testb $0x1, %al
jne 0x705e
jmp 0x70c0
leaq -0x268(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0xd2d0
jmp 0x7073
leaq -0x280(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0xdb70
jmp 0x7088
leaq -0x240(%rbp), %rdi
leaq -0x280(%rbp), %rsi
callq 0x8310
leaq -0x280(%rbp), %rdi
callq 0x8340
jmp 0x70eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x76d2
movq 0x2df01(%rip), %rdi     # 0x34fc8
leaq 0x1d069(%rip), %rsi     # 0x24137
callq 0x32e0
jmp 0x70d5
movl $0x1, -0x4(%rbp)
movl $0x1, -0x284(%rbp)
jmp 0x7523
leaq -0x2d0(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x11210
jmp 0x7100
leaq -0x2d0(%rbp), %rdi
callq 0x112b0
movq %rax, -0xcc8(%rbp)
jmp 0x7115
movq -0xcc8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq 0x2de9e(%rip), %rdi     # 0x34fc8
leaq 0x1d01c(%rip), %rsi     # 0x2414d
callq 0x32e0
movq %rax, -0xcd0(%rbp)
jmp 0x713f
movq -0xcd0(%rbp), %rdi
movq 0x2de6b(%rip), %rsi     # 0x34fb8
callq 0x3330
jmp 0x7154
movq -0x2d8(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
callq 0x18370
jmp 0x7169
leaq -0x2e8(%rbp), %rdi
callq 0x183e0
movq %rax, -0xcd8(%rbp)
jmp 0x717e
movq -0xcd8(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq 0x2de35(%rip), %rdi     # 0x34fc8
leaq 0x1cfd0(%rip), %rsi     # 0x2416a
callq 0x32e0
movq %rax, -0xce0(%rbp)
jmp 0x71a8
movq -0xce0(%rbp), %rdi
movq 0x2de02(%rip), %rsi     # 0x34fb8
callq 0x3330
jmp 0x71bd
movq -0x2d8(%rbp), %rsi
leaq -0x320(%rbp), %rdi
callq 0x1c3f0
jmp 0x71d2
leaq -0x338(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x1c430
jmp 0x71e7
movq 0x2ddda(%rip), %rdi     # 0x34fc8
leaq 0x1cf8d(%rip), %rsi     # 0x24182
callq 0x32e0
movq %rax, -0xce8(%rbp)
jmp 0x7203
movq -0xce8(%rbp), %rdi
movq 0x2dda7(%rip), %rsi     # 0x34fb8
callq 0x3330
jmp 0x7218
leaq -0x5f0(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x8360
jmp 0x722d
movq -0x2f0(%rbp), %rdx
leaq -0x5d8(%rbp), %rdi
leaq -0x5f0(%rbp), %rsi
callq 0x1f110
jmp 0x7249
leaq -0x5f0(%rbp), %rdi
callq 0x8420
leaq -0x610(%rbp), %rdi
leaq -0x5d8(%rbp), %rsi
callq 0x1f2f0
jmp 0x726a
movq 0x2dd57(%rip), %rdi     # 0x34fc8
leaq -0x610(%rbp), %rsi
callq 0x32a0
jmp 0x727f
movq 0x2dd42(%rip), %rdi     # 0x34fc8
leaq 0x1cf0f(%rip), %rsi     # 0x2419c
callq 0x32e0
movq %rax, -0xcf0(%rbp)
jmp 0x729b
movq -0xcf0(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x32e0
movq %rax, -0xcf8(%rbp)
jmp 0x72b4
movq -0xcf8(%rbp), %rdi
movq 0x2dcf6(%rip), %rsi     # 0x34fb8
callq 0x3330
jmp 0x72c9
leaq 0x1ced8(%rip), %rsi     # 0x241a8
leaq -0x818(%rbp), %rdi
movl $0x8, %edx
callq 0x3570
jmp 0x72e3
leaq -0x848(%rbp), %rdi
leaq -0x818(%rbp), %rsi
callq 0x8440
leaq -0x858(%rbp), %rdi
callq 0x84a0
leaq -0x859(%rbp), %rdi
movq %rdi, -0xd00(%rbp)
callq 0x3530
movq -0xd00(%rbp), %r9
movq -0x848(%rbp), %rsi
movl -0x840(%rbp), %edx
movq -0x858(%rbp), %rcx
movl -0x850(%rbp), %r8d
leaq -0x838(%rbp), %rdi
callq 0x84d0
jmp 0x7345
leaq -0x859(%rbp), %rdi
callq 0x3350
leaq 0x1ce5d(%rip), %rsi     # 0x241b5
leaq -0xa68(%rbp), %rdi
movl $0x8, %edx
callq 0x3570
jmp 0x736b
leaq -0xa98(%rbp), %rdi
leaq -0xa68(%rbp), %rsi
callq 0x8440
leaq -0xaa8(%rbp), %rdi
callq 0x84a0
leaq -0xaa9(%rbp), %rdi
movq %rdi, -0xd08(%rbp)
callq 0x3530
movq -0xd08(%rbp), %r9
movq -0xa98(%rbp), %rsi
movl -0xa90(%rbp), %edx
movq -0xaa8(%rbp), %rcx
movl -0xaa0(%rbp), %r8d
leaq -0xa88(%rbp), %rdi
callq 0x84d0
jmp 0x73cd
leaq -0xaa9(%rbp), %rdi
callq 0x3350
movq -0x20(%rbp), %rdi
callq 0x3560
leaq -0xcb0(%rbp), %rdi
callq 0x34f0
jmp 0x73f0
movq -0x20(%rbp), %rsi
leaq -0xcb0(%rbp), %rdi
movl $0x10, %edx
callq 0x3060
jmp 0x7407
leaq -0xcb0(%rbp), %rdi
leaq -0x838(%rbp), %rsi
callq 0x32a0
jmp 0x741c
leaq -0xcb0(%rbp), %rdi
leaq -0x610(%rbp), %rsi
callq 0x32a0
jmp 0x7431
leaq -0xcb0(%rbp), %rdi
leaq -0xa88(%rbp), %rsi
callq 0x32a0
jmp 0x7446
leaq -0x818(%rbp), %rdi
callq 0x30b0
jmp 0x7454
leaq -0xa68(%rbp), %rdi
callq 0x30b0
jmp 0x7462
leaq -0xcb0(%rbp), %rdi
callq 0x3420
jmp 0x7470
movq 0x2db51(%rip), %rdi     # 0x34fc8
leaq 0x1cd44(%rip), %rsi     # 0x241c2
callq 0x32e0
movq %rax, -0xd10(%rbp)
jmp 0x748c
movq -0xd10(%rbp), %rdi
movq 0x2db1e(%rip), %rsi     # 0x34fb8
callq 0x3330
jmp 0x74a1
leaq -0xcb0(%rbp), %rdi
callq 0x3450
leaq -0xa88(%rbp), %rdi
callq 0x31d0
leaq -0xa68(%rbp), %rdi
callq 0x30f0
leaq -0x838(%rbp), %rdi
callq 0x31d0
leaq -0x818(%rbp), %rdi
callq 0x30f0
leaq -0x610(%rbp), %rdi
callq 0x31d0
leaq -0x5d8(%rbp), %rdi
callq 0x8560
leaq -0x338(%rbp), %rdi
callq 0x8420
leaq -0x320(%rbp), %rdi
callq 0x85f0
leaq -0x2d0(%rbp), %rdi
callq 0x8610
movl $0x0, -0x284(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x8340
leaq -0x228(%rbp), %rdi
callq 0x30f0
movl -0x4(%rbp), %eax
addq $0xd10, %rsp            # imm = 0xD10
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x76c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x76ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x76ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x5f0(%rbp), %rdi
callq 0x8420
jmp 0x76ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x76a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x7696
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x859(%rbp), %rdi
callq 0x3350
jmp 0x768a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x767e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0xaa9(%rbp), %rdi
callq 0x3350
jmp 0x7672
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x7666
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0xcb0(%rbp), %rdi
callq 0x3450
leaq -0xa88(%rbp), %rdi
callq 0x31d0
leaq -0xa68(%rbp), %rdi
callq 0x30f0
leaq -0x838(%rbp), %rdi
callq 0x31d0
leaq -0x818(%rbp), %rdi
callq 0x30f0
leaq -0x610(%rbp), %rdi
callq 0x31d0
leaq -0x5d8(%rbp), %rdi
callq 0x8560
leaq -0x338(%rbp), %rdi
callq 0x8420
leaq -0x320(%rbp), %rdi
callq 0x85f0
leaq -0x2d0(%rbp), %rdi
callq 0x8610
leaq -0x240(%rbp), %rdi
callq 0x8340
leaq -0x228(%rbp), %rdi
callq 0x30f0
movq -0x248(%rbp), %rdi
callq 0x3520
nopw %cs:(%rax,%rax)