// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: doInterfaceToInterfaceConv
static void doInterfaceToInterfaceConv(Compiler *comp, const Type *dest, const Type **src, Const *constant)
{
    if (constant)
        comp->error.handler(comp->error.context, "Conversion to interface is not allowed in constant expressions");

    int destOffset = identAllocStack(&comp->idents, &comp->types, &comp->blocks, dest);

    // Assign to #self
    genDup(&comp->gen);                                                     // Duplicate src pointer
    genDeref(&comp->gen, TYPE_PTR);                                         // Get src.#self value
    genPushLocalPtr(&comp->gen, destOffset);                                // Push dest pointer
    genSwapAssign(&comp->gen, TYPE_PTR, 0);                                 // Assign to dest.#self (NULL means a dynamic type)

    // Assign to #selftype (RTTI)
    const Field *selfType = typeAssertFindField(&comp->types, dest, "#selftype", NULL);

    genDup(&comp->gen);                                                     // Duplicate src pointer
    genGetFieldPtr(&comp->gen, selfType->offset);                           // Get src.#selftype pointer
    genDeref(&comp->gen, TYPE_PTR);                                         // Get src.#selftype value
    genPushLocalPtr(&comp->gen, destOffset + selfType->offset);             // Push dest.#selftype pointer
    genSwapAssign(&comp->gen, TYPE_PTR, 0);                                 // Assign to dest.#selftype

    // Assign to methods
    for (int i = 2; i < dest->numItems; i++)
    {
        const char *name = dest->field[i]->name;
        const Field *srcMethod = typeFindField(*src, name, NULL);
        if (!srcMethod)
        {
            char srcBuf[DEFAULT_STR_LEN + 1], destBuf[DEFAULT_STR_LEN + 1];
            comp->error.handler(comp->error.context, "Cannot convert %s to %s: method %s is not implemented", typeSpelling(*src, srcBuf), typeSpelling(dest, destBuf), name);
        }

        if (!typeCompatible(dest->field[i]->type, srcMethod->type))
        {
            char srcBuf[DEFAULT_STR_LEN + 1], destBuf[DEFAULT_STR_LEN + 1];
            comp->error.handler(comp->error.context, "Cannot convert %s to %s: method %s has incompatible signature", typeSpelling(*src, srcBuf), typeSpelling(dest, destBuf), name);
        }

        genDup(&comp->gen);                                                 // Duplicate src pointer
        genGetFieldPtr(&comp->gen, srcMethod->offset);                      // Get src.method pointer
        genDeref(&comp->gen, TYPE_FN);                                      // Get src.method value (entry point)
        genPushLocalPtr(&comp->gen, destOffset + dest->field[i]->offset);   // Push dest.method pointer
        genSwapAssign(&comp->gen, TYPE_FN, 0);                              // Assign to dest.method
    }

    genPop(&comp->gen);                                                     // Remove src pointer
    genPushLocalPtr(&comp->gen, destOffset);
    *src = dest;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
testq %rcx, %rcx
je 0x16967
movq 0x5118(%r14), %rdi
leaq 0x60f62(%rip), %rsi     # 0x778c0
xorl %eax, %eax
callq *0x50f8(%r14)
leaq 0x4e38(%r14), %rdi
leaq 0x4e18(%r14), %r13
leaq 0x4220(%r14), %rdx
movq %r13, %rsi
movq %rbx, %rcx
callq 0x1a2d1
movl %eax, %ebp
movq %r14, 0x28(%rsp)
leaq 0x4e70(%r14), %r12
movq %r12, %rdi
callq 0x18271
movq %r12, %rdi
movl $0x10, %esi
callq 0x182ba
movq %r12, %rdi
movl %ebp, %esi
callq 0x17f78
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x183d5
leaq 0x5ca0c(%rip), %rdx     # 0x733d6
movq %r13, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x1f6a2
movq %rax, %r13
movq %r12, %rdi
callq 0x18271
movl 0x110(%r13), %esi
movq %r12, %rdi
callq 0x18a4f
movq %r12, %rdi
movl $0x10, %esi
callq 0x182ba
movl %ebp, 0xc(%rsp)
addl 0x110(%r13), %ebp
movq %r12, %rdi
movl %ebp, %esi
callq 0x17f78
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x183d5
cmpl $0x3, 0x10(%rbx)
jl 0x16b8f
movl $0x2, %r14d
movq %r15, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rbp
movq (%r15), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x1f5e0
movq %rax, %r13
testq %rax, %rax
jne 0x16ab6
movq 0x28(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x5118(%rax), %rbx
movq (%r15), %rdi
leaq 0x130(%rsp), %rsi
callq 0x1ee46
movq %rax, %r15
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1ee46
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
leaq 0x60e5d(%rip), %rsi     # 0x778ff
movq %r15, %rdx
movq 0x20(%rsp), %r15
movq %rax, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq *0x18(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x108(%rax), %rdi
movq 0x108(%r13), %rsi
callq 0x1f24f
testb %al, %al
jne 0x16b32
movq 0x28(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x5118(%rax), %rbx
movq (%r15), %rdi
leaq 0x130(%rsp), %rsi
callq 0x1ee46
movq %rax, %r15
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1ee46
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
leaq 0x60e17(%rip), %rsi     # 0x77935
movq %r15, %rdx
movq 0x20(%rsp), %r15
movq %rax, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq *0x18(%rsp)
movq %r12, %rdi
callq 0x18271
movl 0x110(%r13), %esi
movq %r12, %rdi
callq 0x18a4f
movq %r12, %rdi
movl $0x1a, %esi
callq 0x182ba
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rax
movl 0x110(%rax), %esi
addl 0xc(%rsp), %esi
movq %r12, %rdi
callq 0x17f78
movq %r12, %rdi
movl $0x1a, %esi
xorl %edx, %edx
callq 0x183d5
incq %r14
movslq 0x10(%rbx), %rax
cmpq %rax, %r14
jl 0x16a3c
movq %r12, %rdi
callq 0x181b1
movq %r12, %rdi
movl 0xc(%rsp), %esi
callq 0x17f78
movq %rbx, (%r15)
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq