// File: /workspace/github_c_makefile_star2_O2/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseExprList
void parseExprList(Compiler *comp, const Type **type, Const *constant)
{
    const Type *inferredType = *type;
    if (inferredType && typeExprListStruct(inferredType) && inferredType->numItems > 0)
        *type = inferredType->field[0]->type;

    parseExpr(comp, type, constant);

    if (comp->lex.tok.kind == TOK_COMMA)
    {
        // Expression list (syntactic sugar - actually a structure literal)
        Const fieldConstantBuf[MAX_IDENTS_IN_LIST], *fieldConstant = NULL;
        if (constant)
        {
            fieldConstantBuf[0] = *constant;
            fieldConstant = &fieldConstantBuf[0];
        }

        const Type *fieldType = *type;

        Type *exprListType = typeAdd(&comp->types, &comp->blocks, TYPE_STRUCT);
        exprListType->isExprList = true;

        // Evaluate expressions and get the total structure size
        while (1)
        {
            // Convert field to the desired type if necessary and possible (no error is thrown anyway)
            if (inferredType && typeExprListStruct(inferredType) && inferredType->numItems > exprListType->numItems)
            {
                const Type *inferredFieldType = inferredType->field[exprListType->numItems]->type;
                doImplicitTypeConv(comp, inferredFieldType, &fieldType, fieldConstant);
                if (typeCompatible(inferredFieldType, fieldType))
                    fieldType = inferredFieldType;
            }

            if (typeExprListStruct(fieldType))
                comp->error.handler(comp->error.context, "Nested expression lists are not allowed");

            if (exprListType->numItems >= MAX_IDENTS_IN_LIST)
                comp->error.handler(comp->error.context, "Too many expressions in list");

            typeAddField(&comp->types, exprListType, fieldType, NULL);

            if (comp->lex.tok.kind != TOK_COMMA)
                break;

            fieldConstant = constant ? &fieldConstantBuf[exprListType->numItems] : NULL;

            lexNext(&comp->lex);

            fieldType = NULL;
            if (inferredType && typeExprListStruct(inferredType) && inferredType->numItems > exprListType->numItems)
                fieldType = inferredType->field[exprListType->numItems]->type;

            parseExpr(comp, &fieldType, fieldConstant);
        }

        *type = exprListType;

        // Allocate structure
        const Ident *exprList = NULL;
        if (constant)
            constant->ptrVal = storageAdd(&comp->storage, typeSize(&comp->types, *type));
        else
        {
            exprList = identAllocTempVar(&comp->idents, &comp->types, &comp->modules, &comp->blocks, *type, false);
            doZeroVar(comp, exprList);
        }

        // Assign expressions
        for (int i = (*type)->numItems - 1; i >= 0; i--)
        {
            const Field *field = (*type)->field[i];
            int fieldSize = typeSize(&comp->types, field->type);

            if (constant)
                constAssign(&comp->consts, (char *)constant->ptrVal + field->offset, &fieldConstantBuf[i], field->type->kind, fieldSize);
            else
            {
                genPushLocalPtr(&comp->gen, exprList->offset + field->offset);
                genSwapChangeRefCntAssign(&comp->gen, field->type);
            }
        }

        if (!constant)
            doPushVarPtr(comp, exprList);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x838, %rsp            # imm = 0x838
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, 0x10(%rsp)
movq (%rsi), %r13
testq %r13, %r13
je 0xd7f2
cmpl $0x16, (%r13)
jne 0xd7f2
cmpb $0x1, 0x14(%r13)
jne 0xd7f2
cmpl $0x0, 0x10(%r13)
jle 0xd7f2
movq 0x28(%r13), %rax
movq (%rax), %rax
movq 0x108(%rax), %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0xd3a5
cmpl $0x3f, 0x4bd0(%rbx)
jne 0xdacb
testq %r14, %r14
je 0xd821
movq (%r14), %rax
leaq 0x30(%rsp), %r15
movq %rax, (%r15)
jmp 0xd824
xorl %r15d, %r15d
leaq 0x4ba8(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x4e18(%rbx), %r12
leaq 0x4220(%rbx), %rsi
pushq $0x16
popq %rdx
movq %r12, %rdi
movq %rsi, 0x28(%rsp)
callq 0x14da9
movq %rax, %rbp
movb $0x1, 0x14(%rax)
testq %r13, %r13
je 0xd8c0
cmpl $0x16, (%r13)
jne 0xd8c0
cmpb $0x1, 0x14(%r13)
jne 0xd8c0
movl 0x10(%r13), %eax
testl %eax, %eax
jle 0xd8c0
movslq 0x10(%rbp), %rcx
cmpl %ecx, %eax
jle 0xd8c0
movq 0x28(%r13), %rax
movq (%rax,%rcx,8), %rax
movq 0x108(%rax), %rsi
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
movq %r15, %rcx
movq %rsi, %r15
callq 0x8d6a
movq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x155ab
testb %al, %al
je 0xd8c0
movq %r15, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpl $0x16, (%rax)
jne 0xd8ec
cmpb $0x1, 0x14(%rax)
jne 0xd8ec
cmpl $0x0, 0x10(%rax)
jle 0xd8ec
movq 0x5118(%rbx), %rdi
leaq 0x5ceda(%rip), %rsi     # 0x6a7be
xorl %eax, %eax
callq *0x50f8(%rbx)
cmpl $0x100, 0x10(%rbp)      # imm = 0x100
jl 0xd90b
movq 0x5118(%rbx), %rdi
leaq 0x5cee3(%rip), %rsi     # 0x6a7e6
xorl %eax, %eax
callq *0x50f8(%rbx)
movq 0x8(%rsp), %rdx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x159f0
cmpl $0x3f, 0x4bd0(%rbx)
jne 0xd998
testq %r14, %r14
je 0xd939
movslq 0x10(%rbp), %rax
leaq (%rsp,%rax,8), %r15
addq $0x30, %r15
jmp 0xd93c
xorl %r15d, %r15d
movq 0x18(%rsp), %rdi
callq 0x10f6e
andq $0x0, 0x8(%rsp)
testq %r13, %r13
je 0xd983
cmpl $0x16, (%r13)
jne 0xd983
cmpb $0x1, 0x14(%r13)
jne 0xd983
movl 0x10(%r13), %eax
testl %eax, %eax
jle 0xd983
movslq 0x10(%rbp), %rcx
cmpl %ecx, %eax
jle 0xd983
movq 0x28(%r13), %rax
movq (%rax,%rcx,8), %rax
movq 0x108(%rax), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0xd3a5
jmp 0xd865
movq 0x10(%rsp), %rax
movq %rbp, (%rax)
testq %r14, %r14
movq %r14, 0x18(%rsp)
je 0xd9dc
movq %r14, %r13
leaq 0xf0(%rbx), %r15
movq %r12, %r14
movq %r12, %rdi
movq %rbp, %rsi
callq 0x15124
movslq %eax, %rsi
movq %r15, %rdi
callq 0x4144
movq %rax, (%r13)
movq $0x0, 0x20(%rsp)
jmp 0xda10
leaq 0x4e38(%rbx), %rdi
leaq 0x100(%rbx), %rdx
movq %r12, %r14
movq %r12, %rsi
movq 0x28(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x10abb
movq %rbx, %rdi
movq %rax, 0x20(%rsp)
movq %rax, %rsi
callq 0x124c6
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl 0x10(%rax), %r12d
leaq 0x4e68(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x4e70(%rbx), %rbp
leaq (%rsp,%r12,8), %r15
addq $0x28, %r15
testl %r12d, %r12d
jle 0xdab6
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x8(%rax,%r12,8), %r13
movq 0x108(%r13), %rsi
movq %r14, %rdi
callq 0x15124
movslq 0x110(%r13), %rsi
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0xda8b
addq (%rcx), %rsi
movq 0x108(%r13), %rcx
movl (%rcx), %ecx
movq 0x28(%rsp), %rdi
movq %r15, %rdx
movl %eax, %r8d
callq 0x61ea
jmp 0xdaad
movq 0x20(%rsp), %rax
addl 0x120(%rax), %esi
movq %rbp, %rdi
callq 0xeb38
movq 0x108(%r13), %rsi
movq %rbp, %rdi
callq 0xf122
addq $-0x8, %r15
decq %r12
jmp 0xda37
cmpq $0x0, 0x18(%rsp)
jne 0xdacb
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x8c92
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq