// File: /workspace/github_c_makefile_star2_O1/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 0x16c48
movq %rcx, %r12
movq 0x52a8(%rbx), %rsi
movq %r14, %rdi
callq 0x1ef64
testb %al, %al
je 0x16c2d
movq (%r15), %rdi
movq 0x52a0(%rbx), %rsi
callq 0x1ef64
testb %al, %al
je 0x16c2d
leaq 0xf0(%rbx), %r13
addq $0x4e18, %rbx           # imm = 0x4E18
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1ede7
movslq %eax, %rsi
movq %r13, %rdi
callq 0xcca8
movq %rax, (%r12)
jmp 0x16e88
movq 0x5118(%rbx), %rdi
leaq 0x60c85(%rip), %rsi     # 0x778c0
xorl %eax, %eax
callq *0x50f8(%rbx)
jmp 0x16e88
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 0x1a2d1
movl %eax, %ebp
movq %rbx, 0x20(%rsp)
leaq 0x4e70(%rbx), %r12
movq %r12, %rdi
movl %eax, %esi
callq 0x17f78
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x183d5
leaq 0x5c736(%rip), %rdx     # 0x733d6
movq %r13, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x1f6a2
movq %rax, %r13
movq (%r15), %rsi
movq %r12, %rdi
callq 0x17f4c
movl %ebp, %esi
addl 0x110(%r13), %esi
movq %r12, %rdi
callq 0x17f78
movq %r12, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x183d5
cmpl $0x3, 0x10(%r14)
jl 0x16e7e
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 0x16d17
xorl %esi, %esi
jmp 0x16e3f
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x16d28
movl 0x110(%rax), %ecx
jmp 0x16d2d
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 0x19a9d
movq %rax, %rbp
testq %rax, %rax
jne 0x16db8
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 0x1ee46
movq %rax, %r15
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1ee46
movq 0x10(%rsp), %rdi
leaq 0x60b5b(%rip), %rsi     # 0x778ff
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 0x1f24f
testb %al, %al
jne 0x16e34
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 0x1ee46
movq %rax, %r15
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1ee46
movq 0x10(%rsp), %rdi
leaq 0x60b15(%rip), %rsi     # 0x77935
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 0x17eca
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
movl 0x110(%rax), %esi
addl %ebp, %esi
movq %r12, %rdi
callq 0x17f78
movq %r12, %rdi
movl $0x1a, %esi
xorl %edx, %edx
callq 0x183d5
incq %rbx
movslq 0x10(%r14), %rax
cmpq %rax, %rbx
jl 0x16d04
movq %r12, %rdi
movl %ebp, %esi
callq 0x17f78
movq %r14, (%r15)
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq