// File: /workspace/github_cpp_makefile_star2_O1/ConsoleTVs[P]VirtualMachine/sample/main.cpp
// Function: main
int main()
{
    auto virtual_machine = new VM::Instance();

    // We can manually add the opcodes using
    // the virtual_machine->add_op_code() function
    // or pass the whole code as a parameter
    auto program = new VM::Program();

    // Let's define the opcodes of our program

    // Multiply 10 by 10
    // SHould print this:
    // 100.000000
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(10));
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(10));
    program->add_op_code(VM::OP_MUL);
    program->add_op_code(VM::OP_PRINT);

    // Multiply a string by 2
    // SHould print this:
    // ABCABC
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(new std::string("ABC")));
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(2));
    program->add_op_code(VM::OP_MUL);
    program->add_op_code(VM::OP_PRINT);

    // Print a list constant (a bit messy I know)
    // Should print this:
    // [1, Hello, 0, 5.100000, [1, 2, 3]]
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(new std::vector<VM::Value *>({
        new VM::Value(1),
        new VM::Value(new std::string("Hello")),
        new VM::Value(false),
        new VM::Value((float) 5.10),
        new VM::Value(new std::vector<VM::Value *>({ new VM::Value(1), new VM::Value(2), new VM::Value(3), })),
    })));
    program->add_op_code(VM::OP_PRINT);

    // Add a string with another type
    // SHould print this:
    // ABC100
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(new std::string("ABC")));
    program->add_op_code(VM::OP_CONSTANT);
    program->add_constant_code(new VM::Value(100));
    program->add_op_code(VM::OP_ADD);
    program->add_op_code(VM::OP_PRINT);

    // Very important to add the OP_EXIT if we want our VM to exit when done
    program->add_op_code(VM::OP_EXIT);

    // Se the program to the virtual machine
    virtual_machine->program = program;

    // You can also debug the program defined in the screen to know what is happening
    printf("\nProgram defined:\n\n");
    program->debug();

    // Now we can run the virtual machine :D
    printf("\nStart the virtual machine...\n\n");
    virtual_machine->run();

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl $0x818, %edi            # imm = 0x818
callq 0x10c0
movq %rax, 0x20(%rsp)
movl $0x0, 0x800(%rax)
movl $0x30, %edi
callq 0x10c0
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
xorl %ebx, %ebx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movb %bl, (%rax)
movl $0xa, %ebp
movl %ebp, 0x8(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x235a
movq %r15, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movb %bl, (%rax)
movl %ebp, 0x8(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x235a
movq %r15, %rdi
movl $0x3, %esi
callq 0x2330
movq %r15, %rdi
movl $0x5, %esi
callq 0x2330
movq %r15, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movq %rax, %rbx
movl $0x20, %edi
callq 0x10c0
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
movb $0x1, %bpl
leaq 0x17b8(%rip), %rsi      # 0x3034
leaq 0x17b4(%rip), %rdx      # 0x3037
movq %r14, %rdi
callq 0x1c78
movb $0x3, (%rbx)
movq %r14, 0x8(%rbx)
xorl %ebp, %ebp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x235a
movq %r15, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x2, 0x8(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x235a
movq %r15, %rdi
movl $0x3, %esi
callq 0x2330
movq %r15, %rdi
movl $0x5, %esi
callq 0x2330
movq %r15, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movq %rax, %rbx
movl $0x18, %edi
callq 0x10c0
movq %rax, 0x8(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x1, 0x8(%rax)
movq %rax, 0x40(%rsp)
movl $0x10, %edi
callq 0x10c0
movq %rax, %r13
movl $0x20, %edi
movq %rax, 0x18(%rsp)
callq 0x10c0
movq %rax, %rbp
addq $0x10, %rax
movq %rax, (%rbp)
movb $0x1, %al
movl %eax, (%rsp)
leaq 0x16e4(%rip), %rsi      # 0x3038
leaq 0x16e2(%rip), %rdx      # 0x303d
movb $0x1, %r14b
movq %rbp, %rdi
movq %rbp, 0x10(%rsp)
callq 0x1c78
movb $0x3, (%r13)
movq %rbp, 0x8(%r13)
movq %r13, 0x48(%rsp)
xorl %r14d, %r14d
movl $0x10, %edi
callq 0x10c0
movb $0x2, (%rax)
movb $0x0, 0x8(%rax)
movq %rax, 0x50(%rsp)
xorl %r14d, %r14d
movl $0x10, %edi
callq 0x10c0
movb $0x1, (%rax)
movl $0x40a33333, 0x8(%rax)  # imm = 0x40A33333
movq %rax, 0x58(%rsp)
xorl %r14d, %r14d
movl $0x10, %edi
callq 0x10c0
movq %rax, %r12
movl $0x18, %edi
callq 0x10c0
movq %rax, %r14
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x1, 0x8(%rax)
movq %rax, 0x28(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x2, 0x8(%rax)
movq %rax, 0x30(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x3, 0x8(%rax)
leaq 0x28(%rsp), %rsi
movq %rax, 0x10(%rsi)
leaq 0x7(%rsp), %rcx
movl $0x3, %edx
movq %r14, %rdi
callq 0x1c3a
movb $0x4, (%r12)
movq %r14, 0x8(%r12)
leaq 0x40(%rsp), %rsi
movq %r12, 0x20(%rsi)
movb $0x1, %bpl
leaq 0x6(%rsp), %rcx
movl $0x5, %edx
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x1c3a
movb $0x4, (%rbx)
movq %r13, 0x8(%rbx)
xorl %ebp, %ebp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x235a
movq %r15, %rdi
movl $0x5, %esi
callq 0x2330
movq %r15, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movq %rax, %rbx
movl $0x20, %edi
callq 0x10c0
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
movb $0x1, %bpl
leaq 0x1585(%rip), %rsi      # 0x3034
leaq 0x1581(%rip), %rdx      # 0x3037
movq %r14, %rdi
callq 0x1c78
movb $0x3, (%rbx)
movq %r14, 0x8(%rbx)
xorl %ebp, %ebp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x235a
movq %r15, %rdi
xorl %esi, %esi
callq 0x2330
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x64, 0x8(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x235a
movq %r15, %rdi
movl $0x1, %esi
callq 0x2330
movq %r15, %rdi
movl $0x5, %esi
callq 0x2330
movq %r15, %rdi
movl $0x6, %esi
callq 0x2330
movq 0x20(%rsp), %rbx
movq %r15, 0x808(%rbx)
leaq 0x1564(%rip), %rdi      # 0x3099
callq 0x1110
movq %r15, %rdi
callq 0x23da
leaq 0x1563(%rip), %rdi      # 0x30ac
callq 0x1110
movq %rbx, %rdi
callq 0x1dd8
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1b92
jmp 0x1bc8
movq %rax, %r13
movb $0x1, %r15b
movb $0x1, %al
movl %eax, (%rsp)
jmp 0x1be0
movq %rax, %r13
movb $0x1, %r14b
movb $0x1, %al
movl %eax, (%rsp)
jmp 0x1c04
movq %rax, %r13
movb $0x1, %al
movl %eax, (%rsp)
jmp 0x1c23
movq %rax, %r13
movb $0x1, %bpl
jmp 0x1bc1
jmp 0x1bb1
movl %ebp, (%rsp)
movq %rax, %r13
xorl %r15d, %r15d
jmp 0x1bd3
movq %rax, %r13
movb $0x1, %al
movl %eax, (%rsp)
jmp 0x1c13
movq %rax, %r13
testb %bpl, %bpl
je 0x1bc1
movq %r14, %rdi
callq 0x10b0
testb %bpl, %bpl
jne 0x1c29
jmp 0x1c31
movq %rax, %r13
movb $0x1, %r15b
movb $0x1, %al
movl %eax, (%rsp)
testb %r15b, %r15b
je 0x1be0
movq %r14, %rdi
callq 0x10b0
testb %r15b, %r15b
je 0x1bed
movq %r12, %rdi
callq 0x10b0
xorl %r14d, %r14d
jmp 0x1bf5
movq %rax, %r13
testb %r14b, %r14b
je 0x1c04
movq 0x10(%rsp), %rdi
callq 0x10b0
testb %r14b, %r14b
je 0x1c13
movq 0x18(%rsp), %rdi
callq 0x10b0
cmpb $0x0, (%rsp)
je 0x1c23
movq 0x8(%rsp), %rdi
callq 0x10b0
cmpb $0x0, (%rsp)
je 0x1c31
movq %rbx, %rdi
callq 0x10b0
movq %r13, %rdi
callq 0x1130
nop