// File: /workspace/github_cpp_makefile_star2_O3/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 $0x268, %rsp            # imm = 0x268
testq %rsi, %rsi
je 0x894f
movq %r8, %r14
movq %rdx, %r12
movq %rsi, %rax
movq 0x8(%rsi), %rsi
movq %rax, 0x8(%rsp)
movq 0x78(%rax), %rax
testq %rax, %rax
movq %rdi, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
je 0x8515
movq 0x8(%rax), %rdx
leaq 0xaf70(%rip), %rdi      # 0x1347c
xorl %eax, %eax
callq 0x47a5
jmp 0x8523
leaq 0xaf7f(%rip), %rdi      # 0x1349b
xorl %eax, %eax
callq 0x47a5
movq 0x8(%rsp), %rsi
movq 0x60(%rsi), %rbx
testq %rbx, %rbx
je 0x85af
movq 0x58(%rsi), %r13
shlq $0x3, %rbx
xorl %ebp, %ebp
movq (%r13,%rbp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x82af
testb %al, %al
jne 0x85a1
leaq 0x124e2(%rip), %rax     # 0x1aa38
cmpb $0x1, 0x3(%rax)
jne 0x857b
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq (%r15), %rdx
leaq 0xaf4b(%rip), %rdi      # 0x134ba
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x2030
movq 0x10(%rsp), %rax
movq %rax, 0x48(%r15)
addq $0x8, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xf49a
addq $0x8, %rbp
cmpq %rbp, %rbx
movq 0x8(%rsp), %rsi
jne 0x853b
movq 0x30(%rsi), %r15
testq %r15, %r15
je 0x8696
movq 0x28(%rsi), %r13
shlq $0x3, %r15
xorl %ebp, %ebp
movq (%r13,%rbp), %rbx
movq %r12, 0x10(%rbx)
leaq 0x12462(%rip), %rax     # 0x1aa38
cmpb $0x1, 0x3(%rax)
jne 0x85f9
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq (%rbx), %rdx
leaq 0xaecb(%rip), %rdi      # 0x134ba
movq %r12, %rcx
xorl %eax, %eax
callq 0x2030
leaq 0xb730(%rip), %rdi      # 0x13d30
xorl %eax, %eax
callq 0x47a5
movq 0x8(%rbx), %rax
movq 0x18(%rsp), %rcx
cmpq 0x8(%rcx), %rax
je 0x8649
cmpq 0x10(%rcx), %rax
je 0x865e
cmpq 0x18(%rcx), %rax
je 0x8673
testq %rax, %rax
je 0x8961
cmpl $0x4, 0x4(%rax)
jne 0x8961
movq 0x8(%rax), %rsi
leaq 0xaeea(%rip), %rdi      # 0x1352a
xorl %eax, %eax
callq 0x47a5
jmp 0x8681
leaq 0x9962(%rip), %rdi      # 0x11fb2
xorl %eax, %eax
callq 0x47a5
movl $0x1, %eax
jmp 0x8686
leaq 0x9950(%rip), %rdi      # 0x11fb5
xorl %eax, %eax
callq 0x47a5
movl $0x4, %eax
jmp 0x8686
leaq 0x993f(%rip), %rdi      # 0x11fb9
xorl %eax, %eax
callq 0x47a5
movl $0x8, %eax
addq %rax, %r12
addq $0x8, %rbp
cmpq %rbp, %r15
jne 0x85c6
leaq 0xae99(%rip), %rdi      # 0x13536
xorl %eax, %eax
callq 0x47a5
movq 0x8(%rsp), %rax
movq %r12, 0x80(%rax)
movq 0x1310(%r14), %rbp
movq 0x1318(%r14), %rbx
movq %rbp, 0x70(%rax)
movq 0x8(%rax), %rsi
leaq 0xae6d(%rip), %rdi      # 0x1353a
movq %rbp, %rdx
xorl %eax, %eax
callq 0x47a5
testq %rbp, %rbp
je 0x8742
addq $0x8, %rbx
movq -0x8(%rbx), %r13
movq (%rbx), %r15
leaq 0xae68(%rip), %rdi      # 0x13556
xorl %eax, %eax
callq 0x47a5
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x4b9e
movq (%r13), %rdx
leaq 0xae57(%rip), %rdi      # 0x13564
movq %r15, %rsi
xorl %eax, %eax
callq 0x47a5
cmpq $0x1, %rbp
je 0x872b
leaq 0xaa31(%rip), %rdi      # 0x13155
xorl %eax, %eax
callq 0x47a5
leaq 0x9d62(%rip), %rdi      # 0x12494
xorl %eax, %eax
callq 0x47a5
addq $0x10, %rbx
decq %rbp
jne 0x86e0
leaq 0x9d7e(%rip), %rdi      # 0x124c7
xorl %eax, %eax
callq 0x47a5
movq 0x8(%rsp), %rcx
movq 0x88(%rcx), %rax
testq %rax, %rax
je 0x8980
movq 0x8(%rax), %r13
testq %r13, %r13
je 0x8944
movq 0x10(%rax), %rbx
cmpq $0x1, %r13
jne 0x8799
movq (%rbx), %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x84c2
jmp 0x893f
leaq 0x1308(%r14), %r15
movq 0x1308(%r14), %rsi
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rbp
movaps %xmm0, (%rbp)
movq $0x0, 0x10(%rbp)
movq %rbp, %rdi
callq 0xfe9c
movq 0x10(%rbp), %rdi
movq 0x1310(%r14), %rdx
movq 0x1318(%r14), %rsi
shlq $0x4, %rdx
callq 0x20e0
movq 0x1310(%r14), %rax
movq %rax, 0x8(%rbp)
movq 0x10(%rbp), %rax
movq %rax, 0x50(%rsp)
movq (%rbp), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x48(%rsp)
movl $0x98, %eax
leaq 0x68(%rsp), %rcx
movq -0x8(%r14,%rax), %rdx
movq %rdx, (%rcx)
movq (%r14,%rax), %rdx
movq %rdx, -0x8(%rcx)
addq $0x98, %rax
addq $0x10, %rcx
cmpq $0x1398, %rax           # imm = 0x1398
jne 0x880e
movq %rbx, 0x58(%rsp)
movq (%rbx), %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x84c2
movq %r13, %rbx
leaq -0x1(%r13), %rbp
movl $0x1, %r13d
movq 0x50(%rsp), %rax
movq %rax, 0x10(%r15)
movaps 0x40(%rsp), %xmm0
movups %xmm0, (%r15)
movl $0x98, %eax
leaq 0x68(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, -0x8(%r14,%rax)
movq -0x8(%rcx), %rdx
movq %rdx, (%r14,%rax)
addq $0x98, %rax
addq $0x10, %rcx
cmpq $0x1398, %rax           # imm = 0x1398
jne 0x8876
movq 0x58(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x84c2
movq 0x1318(%r14), %rdi
testq %rdi, %rdi
je 0x88c7
callq 0x2130
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
cmpq %rbp, %r13
je 0x8933
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0xfe9c
movq 0x30(%rsp), %rdi
movq 0x1310(%r14), %rdx
movq 0x1318(%r14), %rsi
shlq $0x4, %rdx
callq 0x20e0
movq 0x1310(%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x48(%rsp)
incq %r13
cmpq %rbx, %r13
jne 0x885a
movq 0x8(%rsp), %rcx
movq $0x0, 0x88(%rcx)
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xab5e(%rip), %rdi      # 0x134c6
leaq 0xaa8f(%rip), %rsi      # 0x133fe
leaq 0xab57(%rip), %rcx      # 0x134cd
movl $0x156, %edx            # imm = 0x156
callq 0x20b0
leaq 0xabee(%rip), %rdi      # 0x13575
leaq 0xaa70(%rip), %rsi      # 0x133fe
leaq 0xab38(%rip), %rcx      # 0x134cd
movl $0x173, %edx            # imm = 0x173
callq 0x20b0
nop