// File: /workspace/github_c_makefile_star2_O3/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 0x1199e
movq 0x5118(%r14), %rdi
leaq 0x64f2b(%rip), %rsi     # 0x768c0
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 0x15267
movl %eax, %ebp
movq %r14, 0x28(%rsp)
leaq 0x4e70(%r14), %r12
movq %r12, %rdi
callq 0x13273
movq %r12, %rdi
movl $0x10, %esi
callq 0x132bc
movq %r12, %rdi
movl %ebp, %esi
callq 0x12f97
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x133bf
leaq 0x609d5(%rip), %rdx     # 0x723d6
movq %r13, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x1a657
movq %rax, %r13
movq %r12, %rdi
callq 0x13273
movl 0x110(%r13), %esi
movq %r12, %rdi
callq 0x13a27
movq %r12, %rdi
movl $0x10, %esi
callq 0x132bc
movl %ebp, 0xc(%rsp)
addl 0x110(%r13), %ebp
movq %r12, %rdi
movl %ebp, %esi
callq 0x12f97
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x133bf
cmpl $0x3, 0x10(%rbx)
jl 0x11bc6
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 0x1a5a1
movq %rax, %r13
testq %rax, %rax
jne 0x11aed
movq 0x28(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x5118(%rax), %rbx
movq (%r15), %rdi
leaq 0x130(%rsp), %rsi
callq 0x19e8a
movq %rax, %r15
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x19e8a
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
leaq 0x64e26(%rip), %rsi     # 0x768ff
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 0x1a220
testb %al, %al
jne 0x11b69
movq 0x28(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x5118(%rax), %rbx
movq (%r15), %rdi
leaq 0x130(%rsp), %rsi
callq 0x19e8a
movq %rax, %r15
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x19e8a
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
leaq 0x64de0(%rip), %rsi     # 0x76935
movq %r15, %rdx
movq 0x20(%rsp), %r15
movq %rax, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq *0x18(%rsp)
movq %r12, %rdi
callq 0x13273
movl 0x110(%r13), %esi
movq %r12, %rdi
callq 0x13a27
movq %r12, %rdi
movl $0x1a, %esi
callq 0x132bc
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rax
movl 0x110(%rax), %esi
addl 0xc(%rsp), %esi
movq %r12, %rdi
callq 0x12f97
movq %r12, %rdi
movl $0x1a, %esi
xorl %edx, %edx
callq 0x133bf
incq %r14
movslq 0x10(%rbx), %rax
cmpq %rax, %r14
jl 0x11a73
movq %r12, %rdi
callq 0x131c1
movq %r12, %rdi
movl 0xc(%rsp), %esi
callq 0x12f97
movq %rbx, (%r15)
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq