// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: doPtrToInterfaceConv
static void doPtrToInterfaceConv(Compiler *comp, const Type *dest, const Type **src, Const *constant)
{
    if (constant)
    {
        // Special case: any(null) is allowed in constant expressions
        if (typeEquivalent(dest, comp->anyType) && typeEquivalent(*src, comp->ptrNullType))
            constant->ptrVal = storageAdd(&comp->storage, typeSize(&comp->types, dest));
        else
            comp->error.handler(comp->error.context, "Conversion to interface is not allowed in constant expressions");
    }
    else
    {
        int destOffset = identAllocStack(&comp->idents, &comp->types, &comp->blocks, dest);

        // Assign to #self
        genPushLocalPtr(&comp->gen, destOffset);                                // Push dest.#self pointer
        genSwapAssign(&comp->gen, TYPE_PTR, 0);                                 // Assign to dest.#self

        // Assign to #selftype (RTTI)
        const Field *selfType = typeAssertFindField(&comp->types, dest, "#selftype", NULL);

        genPushGlobalPtr(&comp->gen, (Type *)(*src));                           // Push src type
        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 Type *rcvType = (*src)->base;
            if (rcvType->kind == TYPE_NULL)
                genPushIntConst(&comp->gen, 0);                                 // Allow assigning null to a non-empty interface
            else
            {
                int rcvTypeModule = rcvType->typeIdent ? rcvType->typeIdent->module : -1;

                const Ident *srcMethod = identFind(&comp->idents, &comp->modules, &comp->blocks, rcvTypeModule, name, *src, true);
                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);
                }

                genPushIntConst(&comp->gen, srcMethod->offset);                 // Push src value
            }

            genPushLocalPtr(&comp->gen, destOffset + dest->field[i]->offset);   // Push dest.method pointer
            genSwapAssign(&comp->gen, TYPE_FN, 0);                              // Assign to dest.method
        }

        genPushLocalPtr(&comp->gen, destOffset);
    }

    *src = dest;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
testq %rcx, %rcx
je 0x11c7f
movq %rcx, %r12
movq 0x52a8(%rbx), %rsi
movq %r14, %rdi
callq 0x19f7d
testb %al, %al
je 0x11c64
movq (%r15), %rdi
movq 0x52a0(%rbx), %rsi
callq 0x19f7d
testb %al, %al
je 0x11c64
leaq 0xf0(%rbx), %r13
addq $0x4e18, %rbx           # imm = 0x4E18
movq %rbx, %rdi
movq %r14, %rsi
callq 0x19e29
movslq %eax, %rsi
movq %r13, %rdi
callq 0x7a70
movq %rax, (%r12)
jmp 0x11ebf
movq 0x5118(%rbx), %rdi
leaq 0x64c4e(%rip), %rsi     # 0x768c0
xorl %eax, %eax
callq *0x50f8(%rbx)
jmp 0x11ebf
leaq 0x4e38(%rbx), %rdi
leaq 0x4e18(%rbx), %r13
leaq 0x4220(%rbx), %rdx
movq %rdi, 0x48(%rsp)
movq %r13, %rsi
movq %rdx, 0x40(%rsp)
movq %r14, %rcx
callq 0x15267
movl %eax, %ebp
movq %rbx, 0x20(%rsp)
leaq 0x4e70(%rbx), %r12
movq %r12, %rdi
movl %eax, %esi
callq 0x12f97
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x133bf
leaq 0x606ff(%rip), %rdx     # 0x723d6
movq %r13, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x1a657
movq %rax, %r13
movq (%r15), %rsi
movq %r12, %rdi
callq 0x12f6d
movl %ebp, %esi
addl 0x110(%r13), %esi
movq %r12, %rdi
callq 0x12f97
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x133bf
cmpl $0x3, 0x10(%r14)
jl 0x11eb5
movq 0x20(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x38(%rsp)
movl $0x2, %ebx
movq %r15, 0x30(%rsp)
movl %ebp, 0x2c(%rsp)
movq (%r15), %r9
movq 0x8(%r9), %rax
cmpl $0x3, (%rax)
jne 0x11d4e
xorl %esi, %esi
jmp 0x11e76
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x11d5f
movl 0x110(%rax), %ecx
jmp 0x11d64
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %r13
movl $0x1, (%rsp)
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r13, %r8
callq 0x14a71
movq %rax, %rbp
testq %rax, %rax
jne 0x11def
movq 0x20(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x5118(%rax), %rax
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
leaq 0x150(%rsp), %rsi
callq 0x19e8a
movq %rax, %r15
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x19e8a
movq 0x10(%rsp), %rdi
leaq 0x64b24(%rip), %rsi     # 0x768ff
movq %r15, %rdx
movq 0x30(%rsp), %r15
movq %rax, %rcx
movq %r13, %r8
xorl %eax, %eax
callq *0x18(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
movq 0x108(%rax), %rdi
movq 0x108(%rbp), %rsi
callq 0x1a220
testb %al, %al
jne 0x11e6b
movq 0x20(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x5118(%rax), %rax
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
leaq 0x150(%rsp), %rsi
callq 0x19e8a
movq %rax, %r15
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x19e8a
movq 0x10(%rsp), %rdi
leaq 0x64ade(%rip), %rsi     # 0x76935
movq %r15, %rdx
movq 0x30(%rsp), %r15
movq %rax, %rcx
movq %r13, %r8
xorl %eax, %eax
callq *0x18(%rsp)
movq 0x120(%rbp), %rsi
movl 0x2c(%rsp), %ebp
movq %r12, %rdi
callq 0x12ef1
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
movl 0x110(%rax), %esi
addl %ebp, %esi
movq %r12, %rdi
callq 0x12f97
movq %r12, %rdi
movl $0x1a, %esi
xorl %edx, %edx
callq 0x133bf
incq %rbx
movslq 0x10(%r14), %rax
cmpq %rax, %rbx
jl 0x11d3b
movq %r12, %rdi
movl %ebp, %esi
callq 0x12f97
movq %r14, (%r15)
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq