// File: /workspace/github_c_makefile_star2_O2/masyagin1998[P]GCI/src/bytecode-generator/bytecode-generator.c
// Function: dump_bytecode_to_xml_file
void dump_bytecode_to_xml_file(FILE*f, const bytecode_type_t bc)
{
    size_t i;
    
    fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    fprintf(f, "<bytecode>\n");

    fprintf(f, "\t<constant_pool>\n");
    for (i = 0; i < bc->constant_pool_len; i++) {
        switch (bc->constant_pool[i].type) {
        case CONSTANT_TYPE_INTEGER:
            fprintf(f, "\t\t<int_cnst idx=\"%zu\">%lld</int_cnst>\n", i, bc->constant_pool[i].int_cnst);
            break;
        case CONSTANT_TYPE_DOUBLE:
            fprintf(f, "\t\t<double_cnst idx=\"%zu\">%lf</double_cnst>\n", i, bc->constant_pool[i].double_cnst);
            break;
        case CONSTANT_TYPE_FIELDREF:
            fprintf(f, "\t\t<fieldref_cnst idx=\"%zu\">%s</fieldref_cnst>\n", i, bc->constant_pool[i].str_cnst);
            break;
        case CONSTANT_TYPE_FUNCTIONREF:
            fprintf(f, "\t\t<functionref_cnst idx=\"%zu\">%s</functionref_cnst>\n", i, bc->constant_pool[i].str_cnst);
            break;
        }
    }
    fprintf(f, "\t</constant_pool>\n");

    fprintf(f, "\t<op_codes>\n");
    
    i = 0;
    while (i < bc->op_codes_len) {
        size_t j, n;
        
        switch (bc->op_codes[i]) {
        case BC_OP_POP:
            fprintf(f, "\t\t<op>POP<op>\n");
            break;

        case BC_OP_CONSTANT:
            i++;
            fprintf(f, "\t\t<op>CONSTANT %zu</op>\n", bc->op_codes[i]);
            break;

        case BC_OP_GET_LOCAL:
            i++;
            fprintf(f, "\t\t<op>GET_LOCAL %zu</op>\n", bc->op_codes[i]);
            break;
        case BC_OP_SET_LOCAL:
            i++;
            fprintf(f, "\t\t<op>SET_LOCAL %zu</op>\n", bc->op_codes[i]);
            break;

        case BC_OP_CREATE_OBJ:
            i++;
            fprintf(f, "\t\t<op>CREATE_OBJ %zu</op>\n", bc->op_codes[i]);
            break;
        case BC_OP_INIT_OBJ_PROP:
            i++;
            fprintf(f, "\t\t<op>INIT_OBJ_PROP %zu</op>\n", bc->op_codes[i]);
            break;

        case BC_OP_CREATE_ARR:
            i++;
            fprintf(f, "\t\t<op>CREATE_ARR %zu</op>\n", bc->op_codes[i]);
            break;

        case BC_OP_GET_HEAP:
        case BC_OP_SET_HEAP:
            if (bc->op_codes[i] == BC_OP_GET_HEAP) {
                i++;
                fprintf(f, "\t\t<op>GET_HEAP %zu", bc->op_codes[i]);
            } else if (bc->op_codes[i] == BC_OP_SET_HEAP) {
                i++;
                fprintf(f, "\t\t<op>SET_HEAP %zu", bc->op_codes[i]);
            }

            i++;
            n = bc->op_codes[i];
            i++;
            for (j = 0; j < n; j++) {
                if (bc->op_codes[i] == BC_OBJECT_FIELD) {
                    i++;
                    fprintf(f, " field(%zu)", bc->op_codes[i]);
                } else if (bc->op_codes[i] == BC_ARRAY_INDEX) {
                    i++;
                    fprintf(f, " index(%zu)", bc->op_codes[i]);
                }
                i++;
            }
            i--;
            fprintf(f, "</op>\n");
            break;

        case BC_OP_APPEND:
        case BC_OP_DELETE:
            break;
            
        case BC_OP_LOGICAL_OR:
            fprintf(f, "\t\t<op>LOGICAL_OR</op>\n");
            break;
        case BC_OP_LOGICAL_AND:
            fprintf(f, "\t\t<op>LOGICAL_AND</op>\n");
            break;

        case BC_OP_EQ_EQEQ:
            fprintf(f, "\t\t<op>EQ_EQEQ</op>\n");
            break;
        case BC_OP_EQ_NEQ:
            fprintf(f, "\t\t<op>EQ_NEQ</op>\n");
            break;

        case BC_OP_REL_LT:
            fprintf(f, "\t\t<op>REL_LT</op>\n");            
            break;
        case BC_OP_REL_GT:
            fprintf(f, "\t\t<op>REL_GT</op>\n");            
            break;
        case BC_OP_REL_LE:
            fprintf(f, "\t\t<op>REL_LE</op>\n");            
            break;
        case BC_OP_REL_GE:
            fprintf(f, "\t\t<op>REL_GE</op>\n");            
            break;

        case BC_OP_ADDITIVE_PLUS:
            fprintf(f, "\t\t<op>ADDITIVE_PLUS</op>\n");
            break;
        case BC_OP_ADDITIVE_MINUS:
            fprintf(f, "\t\t<op>ADDITIVE_MINUS</op>\n");
            break;

        case BC_OP_MULTIPLICATIVE_MUL:
            fprintf(f, "\t\t<op>MULTIPLICATIVE_MUL</op>\n");
            break;
        case BC_OP_MULTIPLICATIVE_DIV:
            fprintf(f, "\t\t<op>MULTIPLICATIVE_DIV</op>\n");
            break;
        case BC_OP_MULTIPLICATIVE_MOD:
            fprintf(f, "\t\t<op>MULTIPLICATIVE_MOD</op>\n");
            break;

        case BC_OP_NEGATE:
            fprintf(f, "\t\t<op>NEGATE</op>\n");
            break;

        case BC_OP_LEN:
            fprintf(f, "\t\t<op>LEN</op>\n");
            break;
        case BC_OP_HAS_PROPERTY:
            i++;
            fprintf(f, "\t\t<op>HAS_PROPERTY %zu</op>\n", bc->op_codes[i]);
            break;            

        case BC_OP_JUMP_IF_FALSE:
            i++;
            fprintf(f, "\t\t<op>JUMP_IF_FALSE %d</op>\n", (int) bc->op_codes[i]);
            break;
        case BC_OP_JUMP:
            i++;
            fprintf(f, "\t\t<op>JUMP %d</op>\n", (int) bc->op_codes[i]);
            break;

        case BC_OP_RETURN:
            fprintf(f, "\t\t<op>RETURN</op>\n");
            break;
        }

        i++;
    }
    fprintf(f, "\t</op_codes>\n");    

    fprintf(f, "</bytecode>\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x52c2(%rip), %rdi      # 0xe62c
pushq $0x27
popq %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1190
leaq 0x5fc1(%rip), %rdi      # 0xf344
pushq $0xb
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1190
leaq 0x5fb8(%rip), %rdi      # 0xf350
pushq $0x11
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1190
leaq 0x5f1f(%rip), %r12      # 0xf2cc
leaq 0x6030(%rip), %rbp      # 0xf3e4
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq 0x20(%r14), %r15
jae 0x9429
movq 0x18(%r14), %rcx
movl 0x20(%rcx,%r13), %eax
cmpq $0x3, %rax
ja 0x9420
addq %r13, %rcx
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rcx), %rcx
movq %rbx, %rdi
leaq 0x5f7a(%rip), %rsi      # 0xf362
jmp 0x93fc
movq %rbx, %rdi
leaq 0x5fc1(%rip), %rsi      # 0xf3b5
jmp 0x93fc
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1100
jmp 0x9420
movsd (%rcx), %xmm0
movq %rbx, %rdi
leaq 0x5f73(%rip), %rsi      # 0xf389
movq %r15, %rdx
movb $0x1, %al
callq 0x1100
incq %r15
addq $0x28, %r13
jmp 0x93ba
leaq 0x5fe9(%rip), %rdi      # 0xf419
pushq $0x12
popq %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1190
leaq 0x5fe3(%rip), %rdi      # 0xf42c
pushq $0xc
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1190
xorl %r15d, %r15d
leaq 0x6088(%rip), %r12      # 0xf4e9
leaq 0x5de8(%rip), %r13      # 0xf250
cmpq 0x8(%r14), %r15
jae 0x9696
movq (%r14), %rax
movq (%rax,%r15,8), %rcx
cmpq $0x1e, %rcx
ja 0x9673
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
movq %r12, %rsi
jmpq *%rcx
leaq 0x5fa2(%rip), %rdi      # 0xf439
pushq $0xe
jmp 0x9667
leaq 0x613e(%rip), %rdi      # 0xf5e3
pushq $0x1a
jmp 0x9667
leaq 0x6189(%rip), %rdi      # 0xf63c
jmp 0x965a
movl 0x8(%rax,%r15,8), %edx
incq %r15
leaq 0x61d4(%rip), %rsi      # 0xf69b
jmp 0x968a
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x61a3(%rip), %rsi      # 0xf67e
jmp 0x961f
leaq 0x6083(%rip), %rdi      # 0xf56a
jmp 0x9665
leaq 0x6063(%rip), %rdi      # 0xf556
pushq $0x13
jmp 0x9667
leaq 0x615a(%rip), %rdi      # 0xf65b
jmp 0x9665
leaq 0x60f1(%rip), %rdi      # 0xf5fe
jmp 0x965a
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x5fad(%rip), %rsi      # 0xf4ce
jmp 0x961f
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x5f46(%rip), %rsi      # 0xf47b
jmp 0x961f
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x5f4c(%rip), %rsi      # 0xf495
jmp 0x961f
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x5eeb(%rip), %rsi      # 0xf448
jmp 0x961f
leaq 0x6105(%rip), %rdi      # 0xf66e
pushq $0xf
jmp 0x9667
leaq 0x5fb0(%rip), %rdi      # 0xf527
pushq $0x16
jmp 0x9667
leaq 0x5f77(%rip), %rsi      # 0xf4fc
movq 0x8(%rax,%r15,8), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
movq (%r14), %rax
movq 0x10(%rax,%r15,8), %rbp
addq $0x3, %r15
subq $0x1, %rbp
jb 0x95df
movq (%r14), %rax
movq (%rax,%r15,8), %rcx
cmpq $0x1, %rcx
je 0x95c1
testq %rcx, %rcx
jne 0x95da
leaq 0x5f5c(%rip), %rsi      # 0xf51b
jmp 0x95c8
leaq 0x5f47(%rip), %rsi      # 0xf50f
movq 0x8(%rax,%r15,8), %rdx
incq %r15
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
incq %r15
jmp 0x95a0
decq %r15
leaq 0x5929(%rip), %rdi      # 0xef12
pushq $0x6
jmp 0x9667
leaq 0x5f9c(%rip), %rdi      # 0xf590
jmp 0x9665
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x5e5c(%rip), %rsi      # 0xf461
jmp 0x961f
leaq 0x5f95(%rip), %rdi      # 0xf5a3
jmp 0x9665
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x5e91(%rip), %rsi      # 0xf4b0
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
jmp 0x9673
leaq 0x5f97(%rip), %rdi      # 0xf5c9
pushq $0x19
jmp 0x9667
leaq 0x5f01(%rip), %rdi      # 0xf53e
pushq $0x17
jmp 0x9667
leaq 0x5f6e(%rip), %rdi      # 0xf5b6
jmp 0x9665
leaq 0x5f2c(%rip), %rdi      # 0xf57d
jmp 0x9665
leaq 0x5fc3(%rip), %rdi      # 0xf61d
pushq $0x1e
jmp 0x9667
leaq 0x6067(%rip), %rdi      # 0xf6cc
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1190
incq %r15
jmp 0x9468
movl 0x8(%rax,%r15,8), %edx
incq %r15
leaq 0x602e(%rip), %rsi      # 0xf6b8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
jmp 0x9673
leaq 0x6042(%rip), %rdi      # 0xf6df
pushq $0xd
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1190
leaq 0x6037(%rip), %rdi      # 0xf6ed
pushq $0xc
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1190