// File: /workspace/github_cpp_makefile_star2_O2/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 0x10d0
movq %rax, %rbx
andl $0x0, 0x800(%rax)
pushq $0x30
popq %rdi
callq 0x10d0
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 0x1fc4
pushq $0x10
popq %r15
movq %r15, %rdi
callq 0x10d0
movb %bpl, (%rax)
pushq $0xa
popq %r12
movl %r12d, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1fd6
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fc4
movq %r15, %rdi
callq 0x10d0
movb %bpl, (%rax)
movl %r12d, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1fd6
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0x1fc4
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0x1fc4
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fc4
movq %r15, %rdi
callq 0x10d0
movq %rax, %r15
pushq $0x20
popq %rdi
callq 0x10d0
movq %rax, %r12
leaq 0x1927(%rip), %rsi      # 0x303d
leaq 0x8(%rsp), %rdx
movq %rax, %rdi
callq 0x1050
movb $0x3, (%r15)
movq %r12, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1fd6
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fc4
pushq $0x10
popq %r15
movq %r15, %rdi
callq 0x10d0
movb $0x0, (%rax)
movl $0x2, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1fd6
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0x1fc4
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0x1fc4
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fc4
movq %r15, %rdi
callq 0x10d0
movq %rax, %r15
pushq $0x18
popq %rdi
callq 0x10d0
movq %rax, %r12
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x0, (%rax)
movl $0x1, 0x8(%rax)
movq %rax, 0x8(%rsp)
pushq $0x10
popq %rdi
callq 0x10d0
movq %rax, %r13
pushq $0x20
popq %rdi
callq 0x10d0
movq %rax, %rbp
leaq 0x1876(%rip), %rsi      # 0x3041
leaq 0x7(%rsp), %rdx
movq %rax, %rdi
callq 0x1050
movb $0x3, (%r13)
movq %rbp, 0x8(%r13)
movq %r13, 0x10(%rsp)
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x2, (%rax)
movb $0x0, 0x8(%rax)
movq %rax, 0x18(%rsp)
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x1, (%rax)
movl $0x40a33333, 0x8(%rax)  # imm = 0x40A33333
movq %rax, 0x20(%rsp)
pushq $0x10
popq %rdi
callq 0x10d0
movq %rax, %r13
pushq $0x18
popq %rdi
callq 0x10d0
movq %rax, %rbp
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x0, (%rax)
movl $0x1, 0x8(%rax)
movq %rax, 0x30(%rsp)
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x0, (%rax)
movl $0x2, 0x8(%rax)
movq %rax, 0x38(%rsp)
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x0, (%rax)
movl $0x3, 0x8(%rax)
leaq 0x30(%rsp), %rsi
movq %rax, 0x10(%rsi)
pushq $0x3
popq %rdx
leaq 0x6(%rsp), %rcx
movq %rbp, %rdi
callq 0x19dc
movb $0x4, (%r13)
movq %rbp, 0x8(%r13)
leaq 0x8(%rsp), %rsi
movq %r13, 0x20(%rsi)
pushq $0x5
popq %rdx
leaq 0x5(%rsp), %rcx
movq %r12, %rdi
callq 0x19dc
movb $0x4, (%r15)
movq %r12, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1fd6
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0x1fc4
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fc4
pushq $0x10
popq %rdi
callq 0x10d0
movq %rax, %r15
pushq $0x20
popq %rdi
callq 0x10d0
movq %rax, %r12
leaq 0x1756(%rip), %rsi      # 0x303d
leaq 0x8(%rsp), %rdx
movq %rax, %rdi
callq 0x1050
movb $0x3, (%r15)
movq %r12, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1fd6
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fc4
pushq $0x10
popq %rdi
callq 0x10d0
movb $0x0, (%rax)
movl $0x64, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1fd6
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x1fc4
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0x1fc4
pushq $0x6
popq %rsi
movq %r14, %rdi
callq 0x1fc4
movq %r14, 0x808(%rbx)
leaq 0x171b(%rip), %rdi      # 0x3078
callq 0x1130
movq %r14, %rdi
callq 0x201a
leaq 0x171a(%rip), %rdi      # 0x308b
callq 0x1130
movq %rbx, %rdi
callq 0x1afc
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x19c0
jmp 0x19a4
jmp 0x19c0
jmp 0x19ab
jmp 0x199b
jmp 0x19ab
movq %rax, %rbx
jmp 0x19b6
jmp 0x19a4
jmp 0x19c0
movq %rax, %rbx
jmp 0x19cb
jmp 0x19c0
movq %rax, %rbx
movq %rbp, %rdi
callq 0x10c0
movq %r13, %rdi
callq 0x10c0
jmp 0x19c3
movq %rax, %rbx
movq %r12, %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x10c0
movq %rbx, %rdi
callq 0x1160
nop