// File: /workspace/github_cpp_makefile_star2_O1/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 0x8a68
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 0x861b
movq 0x8(%rax), %rdx
leaq 0x9efa(%rip), %rdi      # 0x1250c
xorl %eax, %eax
callq 0x479d
jmp 0x8629
leaq 0x9f09(%rip), %rdi      # 0x1252b
xorl %eax, %eax
callq 0x479d
movq 0x8(%rsp), %rsi
movq 0x60(%rsi), %rbx
testq %rbx, %rbx
je 0x86b5
movq 0x58(%rsi), %r13
shlq $0x3, %rbx
xorl %ebp, %ebp
movq (%r13,%rbp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x83c8
testb %al, %al
jne 0x86a7
leaq 0x113dc(%rip), %rax     # 0x19a38
cmpb $0x1, 0x3(%rax)
jne 0x8681
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq (%r15), %rdx
leaq 0x9ed5(%rip), %rdi      # 0x1254a
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 0xec6e
addq $0x8, %rbp
cmpq %rbp, %rbx
movq 0x8(%rsp), %rsi
jne 0x8641
movq 0x30(%rsi), %r15
testq %r15, %r15
je 0x879c
movq 0x28(%rsi), %r13
shlq $0x3, %r15
xorl %ebp, %ebp
movq (%r13,%rbp), %rbx
movq %r12, 0x10(%rbx)
leaq 0x1135c(%rip), %rax     # 0x19a38
cmpb $0x1, 0x3(%rax)
jne 0x86ff
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq (%rbx), %rdx
leaq 0x9e55(%rip), %rdi      # 0x1254a
movq %r12, %rcx
xorl %eax, %eax
callq 0x2030
leaq 0xa6b9(%rip), %rdi      # 0x12dbf
xorl %eax, %eax
callq 0x479d
movq 0x8(%rbx), %rax
movq 0x18(%rsp), %rcx
cmpq 0x8(%rcx), %rax
je 0x874f
cmpq 0x10(%rcx), %rax
je 0x8764
cmpq 0x18(%rcx), %rax
je 0x8779
testq %rax, %rax
je 0x8a7a
cmpl $0x4, 0x4(%rax)
jne 0x8a7a
movq 0x8(%rax), %rsi
leaq 0x9e74(%rip), %rdi      # 0x125ba
xorl %eax, %eax
callq 0x479d
jmp 0x8787
leaq 0x88e0(%rip), %rdi      # 0x11036
xorl %eax, %eax
callq 0x479d
movl $0x1, %eax
jmp 0x878c
leaq 0x88ce(%rip), %rdi      # 0x11039
xorl %eax, %eax
callq 0x479d
movl $0x4, %eax
jmp 0x878c
leaq 0x88bd(%rip), %rdi      # 0x1103d
xorl %eax, %eax
callq 0x479d
movl $0x8, %eax
addq %rax, %r12
addq $0x8, %rbp
cmpq %rbp, %r15
jne 0x86cc
leaq 0x9e23(%rip), %rdi      # 0x125c6
xorl %eax, %eax
callq 0x479d
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 0x9df7(%rip), %rdi      # 0x125ca
movq %rbp, %rdx
xorl %eax, %eax
callq 0x479d
testq %rbp, %rbp
je 0x8848
addq $0x8, %rbx
movq -0x8(%rbx), %r13
movq (%rbx), %r15
leaq 0x9df2(%rip), %rdi      # 0x125e6
xorl %eax, %eax
callq 0x479d
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x4b96
movq (%r13), %rdx
leaq 0x9de1(%rip), %rdi      # 0x125f4
movq %r15, %rsi
xorl %eax, %eax
callq 0x479d
cmpq $0x1, %rbp
je 0x8831
leaq 0x9a63(%rip), %rdi      # 0x1228d
xorl %eax, %eax
callq 0x479d
leaq 0x8ce0(%rip), %rdi      # 0x11518
xorl %eax, %eax
callq 0x479d
addq $0x10, %rbx
decq %rbp
jne 0x87e6
leaq 0x8cfc(%rip), %rdi      # 0x1154b
xorl %eax, %eax
callq 0x479d
movq 0x8(%rsp), %rcx
movq 0x88(%rcx), %rax
testq %rax, %rax
je 0x8a99
movq 0x8(%rax), %r13
testq %r13, %r13
je 0x8a5d
movq 0x10(%rax), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
cmpq $0x1, %r13
jne 0x88b0
movq (%rbx), %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x85c8
jmp 0x8a58
leaq 0x1308(%r14), %r15
movq 0x1308(%r14), %rsi
leaq 0x60(%rsp), %rbp
movaps %xmm0, (%rbp)
movq $0x0, 0x10(%rbp)
movq %rbp, %rdi
callq 0xf684
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, 0x30(%rsp)
movq (%rbp), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x28(%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 0x8922
movq %rbx, 0x58(%rsp)
movq (%rbx), %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x85c8
movq %r13, %rbx
leaq -0x1(%r13), %rbp
movl $0x1, %r13d
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r15)
movaps 0x20(%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 0x898a
movq 0x58(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq 0x18(%rsp), %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x85c8
movq 0x1318(%r14), %rdi
testq %rdi, %rdi
je 0x89db
callq 0x2130
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
cmpq %rbp, %r13
je 0x8a4c
movq 0x1308(%r14), %rsi
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x40(%rsp), %rdi
callq 0xf684
movq 0x50(%rsp), %rdi
movq 0x1310(%r14), %rdx
movq 0x1318(%r14), %rsi
shlq $0x4, %rdx
callq 0x20e0
movq 0x1310(%r14), %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x28(%rsp)
incq %r13
cmpq %rbx, %r13
jne 0x896e
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 0x9ad5(%rip), %rdi      # 0x12556
leaq 0x9a06(%rip), %rsi      # 0x1248e
leaq 0x9ace(%rip), %rcx      # 0x1255d
movl $0x156, %edx            # imm = 0x156
callq 0x20b0
leaq 0x9b65(%rip), %rdi      # 0x12605
leaq 0x99e7(%rip), %rsi      # 0x1248e
leaq 0x9aaf(%rip), %rcx      # 0x1255d
movl $0x173, %edx            # imm = 0x173
callq 0x20b0