// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xfd03
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x52a8(%rax), %rsi
callq 0x22250
testb $0x1, %al
jne 0xfc80
jmp 0xfcdd
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x52a0(%rax), %rsi
callq 0x22250
testb $0x1, %al
jne 0xfc9d
jmp 0xfcdd
movq -0x8(%rbp), %rax
addq $0xf0, %rax
movq %rax, -0x468(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rsi
callq 0x21fe0
movq -0x468(%rbp), %rdi
movslq %eax, %rsi
callq 0x4b20
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0xfcfe
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
leaq 0x1c8cae(%rip), %rsi    # 0x1d89a8
movb $0x0, %al
callq *%rcx
jmp 0x10034
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0x4e18, %rsi           # imm = 0x4E18
movq -0x8(%rbp), %rdx
addq $0x4220, %rdx           # imm = 0x4220
movq -0x10(%rbp), %rcx
callq 0x1a320
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl -0x24(%rbp), %esi
callq 0x169a0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x10, %esi
xorl %edx, %edx
callq 0x17120
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rsi
leaq 0x1c466e(%rip), %rdx    # 0x1d43de
xorl %eax, %eax
movl %eax, %ecx
callq 0x23000
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x16950
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl -0x24(%rbp), %esi
movq -0x30(%rbp), %rax
addl 0x110(%rax), %esi
callq 0x169a0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x10, %esi
xorl %edx, %edx
callq 0x17120
movl $0x2, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x10021
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpl $0x3, (%rax)
jne 0xfe24
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
xorl %eax, %eax
movl %eax, %esi
callq 0x16850
jmp 0xffd3
movq -0x48(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xfe45
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x110(%rax), %eax
movl %eax, -0x46c(%rbp)
jmp 0xfe52
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x46c(%rbp)
jmp 0xfe52
movl -0x46c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0x100, %rsi            # imm = 0x100
movq -0x8(%rbp), %rdx
addq $0x4220, %rdx           # imm = 0x4220
movl -0x4c(%rbp), %ecx
movq -0x40(%rbp), %r8
movq -0x18(%rbp), %rax
movq (%rax), %r9
movl $0x1, (%rsp)
callq 0x19580
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xff17
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rax
movq %rax, -0x478(%rbp)
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rax
movq %rax, -0x488(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x160(%rbp), %rsi
callq 0x22070
movq %rax, -0x480(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x22070
movq -0x488(%rbp), %rdi
movq -0x480(%rbp), %rdx
movq -0x478(%rbp), %r9
movq %rax, %rcx
movq -0x40(%rbp), %r8
leaq 0x1c8ad5(%rip), %rsi    # 0x1d89e7
movb $0x0, %al
callq *%r9
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x108(%rax), %rdi
movq -0x58(%rbp), %rax
movq 0x108(%rax), %rsi
callq 0x22790
testb $0x1, %al
jne 0xffb8
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rax
movq %rax, -0x490(%rbp)
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rax
movq %rax, -0x4a0(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x360(%rbp), %rsi
callq 0x22070
movq %rax, -0x498(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0x22070
movq -0x4a0(%rbp), %rdi
movq -0x498(%rbp), %rdx
movq -0x490(%rbp), %r9
movq %rax, %rcx
movq -0x40(%rbp), %r8
leaq 0x1c8a6a(%rip), %rsi    # 0x1d8a1d
movb $0x0, %al
callq *%r9
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x58(%rbp), %rax
movq 0x120(%rax), %rsi
callq 0x16850
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl -0x24(%rbp), %esi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addl 0x110(%rax), %esi
callq 0x169a0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x1a, %esi
xorl %edx, %edx
callq 0x17120
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xfdcf
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl -0x24(%rbp), %esi
callq 0x169a0
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
nopl (%rax,%rax)