// File: /workspace/github_cpp_makefile_star2_O2/baziotis[P]minijava-cpp/typecheck.cpp
// Function: TypeTable::compute_and_print_offsets_for_type(IdType*, unsigned long, unsigned long, VirtualTable*)
void TypeTable::compute_and_print_offsets_for_type(IdType *type, size_t start_fields, size_t start_methods, VirtualTable *vtable) {
    if (type == NULL) {
      return;
    }
    IdType *parent = type->parent;
    if (parent) {
        // Inherits, so include the parent at the start of the type.
        emit("%%class.%s = type { %%class.%s", type->id, parent->id);
    } else {
        // The first parent in the inheritance tree - create
        // a vptr.
        emit("%%class.%s = type { i8 (...)**", type->id);
    }

    // Process methods first to know how many methods we have so we
    // can emit the respective virtual pointer.
    int num_methods = start_methods / 8;
    size_t running_offset = start_methods;
    for (Method *method: type->methods) {
        if (!check_method_for_overriding(method, type, vtable)) {
            if (config.offsets) {
                printf("%s.%s: %zd\n", type->id, method->id, running_offset);
            }
            method->offset = running_offset;
            running_offset += 8;
            ++num_methods;
            vtable->insert(method, type->id);
        }
    }
    size_t methods_size = running_offset - start_methods;

    // Generate offsets for fields and also emit the fields.
    running_offset = start_fields;
    for (Local *field : type->fields) {
        field->offset = running_offset;
        if (config.offsets) {
            printf("%s.%s: %zd\n", type->id, field->id, field->offset);
        }
        emit(", ");
        if (field->type == this->bool_type) {
            emit("i1");
            running_offset += 1;
        } else if (field->type == this->int_type) {
            emit("i32");
            running_offset += 4;
        } else if (field->type == this->int_arr_type) {
            emit("i32*");
            running_offset += 8;
        } else {
            IdType *idtype = field->type->is_IdType();
            assert(idtype);
            emit("%%class.%s*", idtype->id);
            running_offset += 8;
        }
    }
    emit(" }\n");

    size_t fields_size = running_offset - start_fields;

    start_fields = start_fields + fields_size;
    type->__sizeof = start_fields;
    start_methods = start_methods + methods_size;

    Buf<VMethod> vmethods = vtable->vmethods;
    type->vmethods_len = vmethods.len;
    emit("@.%s = global [%d x i8*]\n[\n", type->id, vmethods.len);
    ssize_t i = 0;
    for (ssize_t i = 0; i < vmethods.len; ++i) {
        VMethod vmethod = vmethods[i];
        emit("i8* bitcast (");
        emit_vmethod_signature(type, vmethod.method); 
        emit(" @%s__%s to i8*)", vmethod.enclosing, vmethod.method->id);
        if (i != vmethods.len - 1) {
            emit(",");
        }
        emit("\n");
    }
    emit("]\n");
    
    assert(type->children);
    auto children = *(type->children);
    size_t len = children.len;
    if (len) {
        Buf<VMethod> deep_copy;
        // We only need deep copy for more than one children.
        RestorePair save[VirtualTable::nbuckets];
        if (len > 1) {
            deep_copy = vtable->vmethods.deep_copy();
            vtable->save_first_buckets(save);
        }
        compute_and_print_offsets_for_type(children[0], start_fields, start_methods, vtable);
        if (len > 1) {
            for (ssize_t index = 1; index < len; ++index) {
                vtable->vmethods = deep_copy;
                vtable->restore_first_buckets(save);
                IdType *child = children[index];
                compute_and_print_offsets_for_type(child, start_fields, start_methods, vtable);
                vtable->vmethods.free();
                if (index != len - 1) {
                    deep_copy = vtable->vmethods.deep_copy();
                }
            }
        }
    }

    type->invalidate_children();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rcx, 0x8(%rsp)
movq %rdx, (%rsp)
testq %rsi, %rsi
je 0x793e
movq %r8, %r12
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
movq 0x8(%rsi), %rsi
movq 0x78(%rbx), %rax
testq %rax, %rax
je 0x768d
movq 0x8(%rax), %rdx
leaq 0x8de8(%rip), %rdi      # 0x1046c
xorl %eax, %eax
callq 0x42fe
jmp 0x769b
leaq 0x8df7(%rip), %rdi      # 0x1048b
xorl %eax, %eax
callq 0x42fe
movq 0x58(%rbx), %r15
movq 0x60(%rbx), %r13
shlq $0x3, %r13
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x7712
movq (%r15,%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x747f
testb %al, %al
jne 0x770c
leaq 0x1136d(%rip), %rax     # 0x18a38
cmpb $0x1, 0x3(%rax)
jne 0x76eb
movq 0x8(%rbx), %rsi
movq (%r14), %rdx
leaq 0x8dcb(%rip), %rdi      # 0x104aa
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x2030
movq 0x8(%rsp), %rax
movq %rax, 0x48(%r14)
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rbx), %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0xc98e
addq $0x8, %rbp
jmp 0x76a9
movq %r12, 0x10(%rsp)
movq 0x28(%rbx), %r14
movq 0x30(%rbx), %r13
shlq $0x3, %r13
leaq 0x95f6(%rip), %rbp      # 0x10d20
xorl %r15d, %r15d
movq %rbx, %r12
cmpq %r15, %r13
je 0x7801
movq (%r14,%r15), %rbx
movq (%rsp), %rax
movq %rax, 0x10(%rbx)
leaq 0x112ec(%rip), %rax     # 0x18a38
cmpb $0x1, 0x3(%rax)
jne 0x776c
movq 0x8(%r12), %rsi
movq (%rbx), %rdx
leaq 0x8d49(%rip), %rdi      # 0x104aa
movq (%rsp), %rcx
xorl %eax, %eax
callq 0x2030
movq %rbp, %rdi
xorl %eax, %eax
callq 0x42fe
movq 0x8(%rbx), %rax
movq 0x18(%rsp), %rcx
cmpq 0x8(%rcx), %rax
je 0x77bb
cmpq 0x10(%rcx), %rax
movq %r12, %rbx
je 0x77d1
cmpq 0x18(%rcx), %rax
je 0x77e3
testq %rax, %rax
je 0x7a39
cmpl $0x4, 0x4(%rax)
jne 0x7a39
movq 0x8(%rax), %rsi
leaq 0x8d68(%rip), %rdi      # 0x1051a
xorl %eax, %eax
callq 0x42fe
jmp 0x77f1
leaq 0x77c4(%rip), %rdi      # 0xef86
xorl %eax, %eax
callq 0x42fe
pushq $0x1
popq %rax
movq %r12, %rbx
jmp 0x77f4
leaq 0x77b1(%rip), %rdi      # 0xef89
xorl %eax, %eax
callq 0x42fe
pushq $0x4
jmp 0x77f3
leaq 0x77a3(%rip), %rdi      # 0xef8d
xorl %eax, %eax
callq 0x42fe
pushq $0x8
popq %rax
addq %rax, (%rsp)
addq $0x8, %r15
jmp 0x7730
leaq 0x8d1e(%rip), %rdi      # 0x10526
xorl %eax, %eax
callq 0x42fe
movq (%rsp), %rax
movq %rax, 0x80(%rbx)
movq %rbx, %rax
movq 0x10(%rsp), %rcx
movq 0x1310(%rcx), %rbx
movq 0x1318(%rcx), %r14
movq %rbx, 0x70(%rax)
movq 0x8(%rax), %rsi
leaq 0x8ceb(%rip), %rdi      # 0x1052a
movq %rbx, %rdx
xorl %eax, %eax
callq 0x42fe
addq $0x8, %r14
movq %rbx, %r13
subq $0x1, %r13
jb 0x78b6
movq -0x8(%r14), %rbp
movq (%r14), %r15
leaq 0x8ce2(%rip), %rdi      # 0x10546
xorl %eax, %eax
callq 0x42fe
movq %r12, %rdi
movq %rbp, %rsi
callq 0x469f
movq (%rbp), %rdx
leaq 0x8cd3(%rip), %rdi      # 0x10554
movq %r15, %rsi
xorl %eax, %eax
callq 0x42fe
cmpq $0x1, %rbx
je 0x789f
leaq 0x894d(%rip), %rdi      # 0x101e5
xorl %eax, %eax
callq 0x42fe
leaq 0x7bc2(%rip), %rdi      # 0xf468
xorl %eax, %eax
callq 0x42fe
addq $0x10, %r14
movq %r13, %rbx
jmp 0x7850
leaq 0x7bde(%rip), %rdi      # 0xf49b
xorl %eax, %eax
callq 0x42fe
movq 0x88(%r12), %rax
testq %rax, %rax
je 0x7a58
movups (%rax), %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x48(%rsp), %r13
testq %r13, %r13
je 0x7935
movq %r12, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
cmpq $0x1, %r13
jne 0x7950
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0xca6a
movq (%rax), %rsi
movq 0x18(%rsp), %rdi
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %r8
callq 0x763e
movq 0x60(%rsp), %r12
andq $0x0, 0x88(%r12)
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rbx
leaq 0x1308(%rbx), %rbp
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0xca00
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
callq 0xca3a
xorl %r15d, %r15d
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0xca6a
movq (%rax), %rsi
movq 0x18(%rsp), %rdi
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x763e
leaq -0x1(%r13), %r12
addq $-0x2, %r13
movq 0x10(%rsp), %rbx
cmpq %r15, %r12
je 0x7930
leaq 0x1(%r15), %r14
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rbp)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rbp)
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
callq 0xca9a
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0xca6a
movq (%rax), %rsi
movq 0x18(%rsp), %rdi
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x763e
movq %rbp, %rdi
callq 0xcaca
cmpq %r15, %r13
je 0x7a31
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0xca00
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x68(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq %r14, %r15
jmp 0x79ae
leaq 0x8a76(%rip), %rdi      # 0x104b6
leaq 0x89a7(%rip), %rsi      # 0x103ee
leaq 0x8a6f(%rip), %rcx      # 0x104bd
movl $0x156, %edx            # imm = 0x156
callq 0x20b0
leaq 0x8b06(%rip), %rdi      # 0x10565
leaq 0x8988(%rip), %rsi      # 0x103ee
leaq 0x8a50(%rip), %rcx      # 0x104bd
movl $0x173, %edx            # imm = 0x173
callq 0x20b0
nop