// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl $0x818, %edi            # imm = 0x818
callq 0x10c0
movq %rax, %rbx
movl $0x0, 0x800(%rax)
movl $0x30, %edi
callq 0x10c0
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movb %bpl, (%rax)
movl $0xa, %r15d
movl %r15d, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x2236
movq %r14, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movb %bpl, (%rax)
movl %r15d, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x2236
movq %r14, %rdi
movl $0x3, %esi
callq 0x220c
movq %r14, %rdi
movl $0x5, %esi
callq 0x220c
movq %r14, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movq %rax, %r15
movl $0x20, %edi
callq 0x10c0
movq %rax, %r12
addq $0x10, %rax
movq %rax, (%r12)
leaq 0x17b7(%rip), %rsi      # 0x3034
leaq 0x17b3(%rip), %rdx      # 0x3037
movq %r12, %rdi
callq 0x1bc2
movb $0x3, (%r15)
movq %r12, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2236
movq %r14, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x2, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x2236
movq %r14, %rdi
movl $0x3, %esi
callq 0x220c
movq %r14, %rdi
movl $0x5, %esi
callq 0x220c
movq %r14, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movq %rax, %r15
movl $0x18, %edi
callq 0x10c0
movq %rax, %r12
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x1, 0x8(%rax)
movq %rax, 0x20(%rsp)
movl $0x10, %edi
callq 0x10c0
movq %rax, %r13
movl $0x20, %edi
callq 0x10c0
movq %rax, %rbp
addq $0x10, %rax
movq %rax, (%rbp)
leaq 0x16f0(%rip), %rsi      # 0x3038
leaq 0x16ee(%rip), %rdx      # 0x303d
movq %rbp, %rdi
callq 0x1bc2
movb $0x3, (%r13)
movq %rbp, 0x8(%r13)
movq %r13, 0x28(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x2, (%rax)
movb $0x0, 0x8(%rax)
movq %rax, 0x30(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x1, (%rax)
movl $0x40a33333, 0x8(%rax)  # imm = 0x40A33333
movq %rax, 0x38(%rsp)
movl $0x10, %edi
callq 0x10c0
movq %rax, %r13
movl $0x18, %edi
callq 0x10c0
movq %rax, %rbp
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x1, 0x8(%rax)
movq %rax, 0x8(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x2, 0x8(%rax)
movq %rax, 0x10(%rsp)
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x3, 0x8(%rax)
leaq 0x8(%rsp), %rsi
movq %rax, 0x10(%rsi)
leaq 0x7(%rsp), %rcx
movl $0x3, %edx
movq %rbp, %rdi
callq 0x1b84
movb $0x4, (%r13)
movq %rbp, 0x8(%r13)
leaq 0x20(%rsp), %rsi
movq %r13, 0x20(%rsi)
leaq 0x6(%rsp), %rcx
movl $0x5, %edx
movq %r12, %rdi
callq 0x1b84
movb $0x4, (%r15)
movq %r12, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2236
movq %r14, %rdi
movl $0x5, %esi
callq 0x220c
movq %r14, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movq %rax, %r15
movl $0x20, %edi
callq 0x10c0
movq %rax, %r12
addq $0x10, %rax
movq %rax, (%r12)
leaq 0x15ae(%rip), %rsi      # 0x3034
leaq 0x15aa(%rip), %rdx      # 0x3037
movq %r12, %rdi
callq 0x1bc2
movb $0x3, (%r15)
movq %r12, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2236
movq %r14, %rdi
xorl %esi, %esi
callq 0x220c
movl $0x10, %edi
callq 0x10c0
movb $0x0, (%rax)
movl $0x64, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x2236
movq %r14, %rdi
movl $0x1, %esi
callq 0x220c
movq %r14, %rdi
movl $0x5, %esi
callq 0x220c
movq %r14, %rdi
movl $0x6, %esi
callq 0x220c
movq %r14, 0x808(%rbx)
leaq 0x1593(%rip), %rdi      # 0x3099
callq 0x1110
movq %r14, %rdi
callq 0x22be
leaq 0x1592(%rip), %rdi      # 0x30ac
callq 0x1110
movq %rbx, %rdi
callq 0x1d1c
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1b69
jmp 0x1b4d
jmp 0x1b69
jmp 0x1b54
jmp 0x1b44
jmp 0x1b54
movq %rax, %rbx
jmp 0x1b5f
jmp 0x1b4d
jmp 0x1b69
movq %rax, %rbx
jmp 0x1b74
jmp 0x1b69
movq %rax, %rbx
movq %rbp, %rdi
callq 0x10b0
movq %r13, %rdi
callq 0x10b0
jmp 0x1b6c
movq %rax, %rbx
movq %r12, %rdi
callq 0x10b0
movq %r15, %rdi
callq 0x10b0
movq %rbx, %rdi
callq 0x1130