// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseMapLiteral
static void parseMapLiteral(Compiler *comp, const Type **type, Const *constant)
{
    lexEat(&comp->lex, TOK_LBRACE);

    if (constant)
        comp->error.handler(comp->error.context, "Map literals are not allowed for constants");

    // Allocate map
    const Ident *mapIdent = identAllocTempVar(&comp->idents, &comp->types, &comp->modules, &comp->blocks, *type, false);
    doZeroVar(comp, mapIdent);

    doPushVarPtr(comp, mapIdent);
    genCallTypedBuiltin(&comp->gen, *type, BUILTIN_MAKE);

    // Parse map
    while (comp->lex.tok.kind != TOK_RBRACE)
    {
        genDup(&comp->gen);

        // Key
        const Type *keyType = typeMapKey(*type);
        parseExpr(comp, &keyType, NULL);
        doAssertImplicitTypeConv(comp, typeMapKey(*type), &keyType, NULL);

        lexEat(&comp->lex, TOK_COLON);

        // Get map item by key
        genGetMapPtr(&comp->gen, *type);

        // Item
        const Type *itemType = typeMapItem(*type);
        parseExpr(comp, &itemType, NULL);
        doAssertImplicitTypeConv(comp, typeMapItem(*type), &itemType, NULL);

        // Assign to map item
        if (doTryRemoveCopyResultToTempVar(comp))
        {
            // Optimization: if the right-hand side is a function call, assume its reference count to be already increased before return
            // The left-hand side will hold this additional reference, so we can remove the temporary "reference holder" variable
            genChangeLeftRefCntAssign(&comp->gen, typeMapItem(*type));
        }
        else
        {
            // General case: update reference counts for both sides
            genChangeRefCntAssign(&comp->gen, typeMapItem(*type));
        }

        if (comp->lex.tok.kind != TOK_COMMA)
            break;
        lexNext(&comp->lex);
    }

    // Allow closing brace on a new line
    if (comp->lex.tok.kind == TOK_IMPLICIT_SEMICOLON)
        lexNext(&comp->lex);

    lexEat(&comp->lex, TOK_RBRACE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x3c, %esi
callq 0x1b150
cmpq $0x0, -0x18(%rbp)
je 0x14131
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
leaq 0x1c4cd7(%rip), %rsi    # 0x1d8e04
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0x4e18, %rsi           # imm = 0x4E18
movq -0x8(%rbp), %rdx
addq $0x100, %rdx            # imm = 0x100
movq -0x8(%rbp), %rcx
addq $0x4220, %rcx           # imm = 0x4220
movq -0x10(%rbp), %rax
movq (%rax), %r8
xorl %r9d, %r9d
callq 0x1a540
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x1d5e0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xd010
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl $0x16, %edx
callq 0x182e0
movq -0x8(%rbp), %rax
cmpl $0x3d, 0x4bd0(%rax)
je 0x14306
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x16eb0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x13410
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xe8c0
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x13410
movq -0x40(%rbp), %rdi
movq %rax, %rsi
leaq -0x28(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xd6e0
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x41, %esi
callq 0x1b150
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x17b50
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x14a60
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xe8c0
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x14a60
movq -0x38(%rbp), %rdi
movq %rax, %rsi
leaq -0x30(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xd6e0
movq -0x8(%rbp), %rdi
callq 0xd0f0
testb $0x1, %al
jne 0x14294
jmp 0x142bc
movq -0x8(%rbp), %rax
addq $0x4e70, %rax           # imm = 0x4E70
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x14a60
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x17590
jmp 0x142e2
movq -0x8(%rbp), %rax
addq $0x4e70, %rax           # imm = 0x4E70
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x14a60
movq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x17470
movq -0x8(%rbp), %rax
cmpl $0x3f, 0x4bd0(%rax)
je 0x142f1
jmp 0x14306
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
callq 0x1ad90
jmp 0x141a6
movq -0x8(%rbp), %rax
cmpl $0x4a, 0x4bd0(%rax)
jne 0x14323
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
callq 0x1ad90
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x3d, %esi
callq 0x1b150
addq $0x50, %rsp
popq %rbp
retq
nop