// File: /workspace/github_c_makefile_star2_O2/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, %rbp
movq %rsi, %r14
movq %rdi, %rbx
testq %rcx, %rcx
je 0xdb6d
movq %rcx, %r15
movq 0x52a8(%rbx), %rsi
movq %r14, %rdi
callq 0x15262
testb %al, %al
je 0xdb52
movq (%rbp), %rdi
movq 0x52a0(%rbx), %rsi
callq 0x15262
testb %al, %al
je 0xdb52
leaq 0xf0(%rbx), %r12
addq $0x4e18, %rbx           # imm = 0x4E18
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15124
movslq %eax, %rsi
movq %r12, %rdi
callq 0x4144
movq %rax, (%r15)
jmp 0xdda6
movq 0x5118(%rbx), %rdi
leaq 0x5cd04(%rip), %rsi     # 0x6a864
xorl %eax, %eax
callq *0x50f8(%rbx)
jmp 0xdda6
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 0x10996
movl %eax, %r15d
leaq 0x4e70(%rbx), %r12
movq %r12, %rdi
movl %eax, %esi
callq 0xeb38
pushq $0x10
popq %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xeee9
leaq 0x58817(%rip), %rdx     # 0x663d6
movq %r13, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x159b0
movq %rax, %r13
movq (%rbp), %rsi
movq %r12, %rdi
callq 0xeb11
movl %r15d, 0x14(%rsp)
addl 0x110(%r13), %r15d
movq %r12, %rdi
movl %r15d, %esi
callq 0xeb38
movq %r12, %rdi
pushq $0x10
popq %rsi
xorl %edx, %edx
callq 0xeee9
movq %rbx, 0x28(%rsp)
leaq 0x100(%rbx), %rax
movq %rax, 0x38(%rsp)
pushq $0x2
popq %rbx
pushq $0x1a
popq %r15
movq %rbp, 0x30(%rsp)
movslq 0x10(%r14), %rax
cmpq %rax, %rbx
jge 0xdd9a
movq (%rbp), %r9
movq 0x8(%r9), %rax
cmpl $0x3, (%rax)
jne 0xdc3d
xorl %esi, %esi
jmp 0xdd63
movq 0x18(%rax), %rax
pushq $-0x1
popq %rcx
testq %rax, %rax
je 0xdc4f
movl 0x110(%rax), %ecx
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 0x101df
movq %rax, %r15
testq %rax, %rax
jne 0xdcdb
movq 0x28(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x5118(%rax), %rax
movq %rax, 0x18(%rsp)
movq (%rbp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x15180
movq %rax, %rbp
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x15180
movq 0x18(%rsp), %rdi
leaq 0x5cbdc(%rip), %rsi     # 0x6a8a3
movq %rbp, %rdx
movq 0x30(%rsp), %rbp
movq %rax, %rcx
movq %r13, %r8
xorl %eax, %eax
callq *0x20(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
movq 0x108(%rax), %rdi
movq 0x108(%r15), %rsi
callq 0x155ab
testb %al, %al
jne 0xdd58
movq 0x28(%rsp), %rax
movq 0x50f8(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x5118(%rax), %rax
movq %rax, 0x18(%rsp)
movq (%rbp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x15180
movq %rax, %rbp
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x15180
movq 0x18(%rsp), %rdi
leaq 0x5cb95(%rip), %rsi     # 0x6a8d9
movq %rbp, %rdx
movq 0x30(%rsp), %rbp
movq %rax, %rcx
movq %r13, %r8
xorl %eax, %eax
callq *0x20(%rsp)
movq 0x120(%r15), %rsi
pushq $0x1a
popq %r15
movq %r12, %rdi
callq 0xea9e
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
movl 0x110(%rax), %esi
addl 0x14(%rsp), %esi
movq %r12, %rdi
callq 0xeb38
movq %r12, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xeee9
incq %rbx
jmp 0xdc1c
movq %r12, %rdi
movl 0x14(%rsp), %esi
callq 0xeb38
movq %r14, (%rbp)
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq