// File: /workspace/github_c_makefile_star2_O3/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 0x409b(%rip), %rdi      # 0xf62c
movl $0x27, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
leaq 0x4d9a(%rip), %rdi      # 0x10344
movl $0xb, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
leaq 0x4d8d(%rip), %rdi      # 0x10350
movl $0x11, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
cmpq $0x0, 0x20(%r14)
je 0xb661
leaq 0x4c69(%rip), %r12      # 0x10250
leaq 0x4df6(%rip), %rbp      # 0x103e4
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x18(%r14), %rcx
movl 0x20(%rcx,%r13), %eax
cmpq $0x3, %rax
ja 0xb654
addq %r13, %rcx
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rcx), %rcx
movq %rbx, %rdi
leaq 0x4d46(%rip), %rsi      # 0x10362
jmp 0xb630
movq %rbx, %rdi
leaq 0x4d8d(%rip), %rsi      # 0x103b5
jmp 0xb630
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1100
jmp 0xb654
movsd (%rcx), %xmm0
movq %rbx, %rdi
leaq 0x4d3f(%rip), %rsi      # 0x10389
movq %r15, %rdx
movb $0x1, %al
callq 0x1100
incq %r15
addq $0x28, %r13
cmpq 0x20(%r14), %r15
jb 0xb5f4
leaq 0x4db1(%rip), %rdi      # 0x10419
movl $0x12, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
leaq 0x4dab(%rip), %rdi      # 0x1042c
movl $0xc, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
cmpq $0x0, 0x8(%r14)
je 0xb8fe
xorl %r15d, %r15d
leaq 0x4e41(%rip), %r12      # 0x104e9
leaq 0x4bb1(%rip), %r13      # 0x10260
movq (%r14), %rax
movq (%rax,%r15,8), %rcx
cmpq $0x1e, %rcx
ja 0xb8d4
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
movq %r12, %rsi
jmpq *%rcx
leaq 0x4d65(%rip), %rdi      # 0x10439
movl $0xe, %esi
jmp 0xb8c7
leaq 0x4efe(%rip), %rdi      # 0x105e3
movl $0x1a, %esi
jmp 0xb8c7
leaq 0x4f46(%rip), %rdi      # 0x1063c
jmp 0xb8b4
movl 0x8(%rax,%r15,8), %edx
incq %r15
leaq 0x4f91(%rip), %rsi      # 0x1069b
jmp 0xb8f2
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4f60(%rip), %rsi      # 0x1067e
jmp 0xb873
leaq 0x4e40(%rip), %rdi      # 0x1056a
jmp 0xb8c2
leaq 0x4e20(%rip), %rdi      # 0x10556
movl $0x13, %esi
jmp 0xb8c7
leaq 0x4f14(%rip), %rdi      # 0x1065b
jmp 0xb8c2
leaq 0x4eab(%rip), %rdi      # 0x105fe
jmp 0xb8b4
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4d67(%rip), %rsi      # 0x104ce
jmp 0xb873
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4d00(%rip), %rsi      # 0x1047b
jmp 0xb873
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4d06(%rip), %rsi      # 0x10495
jmp 0xb873
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4ca5(%rip), %rsi      # 0x10448
jmp 0xb873
leaq 0x4ebf(%rip), %rdi      # 0x1066e
movl $0xf, %esi
jmp 0xb8c7
leaq 0x4d67(%rip), %rdi      # 0x10527
movl $0x16, %esi
jmp 0xb8c7
leaq 0x4d2b(%rip), %rsi      # 0x104fc
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
testq %rbp, %rbp
je 0xb82d
movq (%r14), %rax
movq (%rax,%r15,8), %rcx
cmpq $0x1, %rcx
je 0xb80c
testq %rcx, %rcx
jne 0xb825
leaq 0x4d11(%rip), %rsi      # 0x1051b
jmp 0xb813
leaq 0x4cfc(%rip), %rsi      # 0x1050f
movq 0x8(%rax,%r15,8), %rdx
incq %r15
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
incq %r15
decq %rbp
jne 0xb7f1
decq %r15
leaq 0x46db(%rip), %rdi      # 0xff12
movl $0x6, %esi
jmp 0xb8c7
leaq 0x4d48(%rip), %rdi      # 0x10590
jmp 0xb8c2
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4c08(%rip), %rsi      # 0x10461
jmp 0xb873
leaq 0x4d41(%rip), %rdi      # 0x105a3
jmp 0xb8c2
movq 0x8(%rax,%r15,8), %rdx
incq %r15
leaq 0x4c3d(%rip), %rsi      # 0x104b0
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
jmp 0xb8d4
leaq 0x4d43(%rip), %rdi      # 0x105c9
movl $0x19, %esi
jmp 0xb8c7
leaq 0x4caa(%rip), %rdi      # 0x1053e
movl $0x17, %esi
jmp 0xb8c7
leaq 0x4d14(%rip), %rdi      # 0x105b6
jmp 0xb8c2
leaq 0x4cd2(%rip), %rdi      # 0x1057d
jmp 0xb8c2
leaq 0x4d69(%rip), %rdi      # 0x1061d
movl $0x1e, %esi
jmp 0xb8c7
leaq 0x4e0a(%rip), %rdi      # 0x106cc
movl $0x12, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
incq %r15
cmpq 0x8(%r14), %r15
jb 0xb6af
jmp 0xb8fe
movl 0x8(%rax,%r15,8), %edx
incq %r15
leaq 0x4dc6(%rip), %rsi      # 0x106b8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
jmp 0xb8d4
leaq 0x4dda(%rip), %rdi      # 0x106df
movl $0xd, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1190
leaq 0x4dcf(%rip), %rdi      # 0x106ed
movl $0xc, %esi
movl $0x1, %edx
movq %rbx, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1190