// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movq (%rsi), %r13
testq %r13, %r13
je 0x1662d
cmpl $0x16, (%r13)
jne 0x1662d
cmpb $0x1, 0x14(%r13)
jne 0x1662d
cmpl $0x0, 0x10(%r13)
jle 0x1662d
cmpl $0x0, 0x10(%r13)
jle 0x1662d
movq 0x28(%r13), %rax
movq (%rax), %rax
movq 0x108(%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x1617b
cmpl $0x3f, 0x4bd0(%rbx)
jne 0x1691f
testq %r14, %r14
je 0x1665c
movq (%r14), %rax
leaq 0x30(%rsp), %r15
movq %rax, (%r15)
jmp 0x1665f
xorl %r15d, %r15d
leaq 0x4ba8(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rsp, %rcx
movq %rax, (%rcx)
leaq 0x4e18(%rbx), %r12
leaq 0x4220(%rbx), %rsi
movq %r12, %rdi
movq %rsi, 0x10(%rsp)
movl $0x16, %edx
callq 0x1ea48
movq %rax, %rbp
movb $0x1, 0x14(%rax)
testq %r13, %r13
je 0x166fe
cmpl $0x16, (%r13)
jne 0x166fe
cmpb $0x1, 0x14(%r13)
jne 0x166fe
cmpl $0x0, 0x10(%r13)
jle 0x166fe
movslq 0x10(%rbp), %rax
cmpl %eax, 0x10(%r13)
jle 0x166fe
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rax
movq 0x108(%rax), %rsi
movq %rbx, %rdi
movq %rsp, %rdx
movq %r15, %rcx
movq %rsi, %r15
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x11c15
movq (%rsp), %rsi
movq %r15, %rdi
callq 0x1f24f
testb %al, %al
je 0x166fe
movq %r15, (%rsp)
movq (%rsp), %rax
cmpl $0x16, (%rax)
jne 0x16729
cmpb $0x1, 0x14(%rax)
jne 0x16729
cmpl $0x0, 0x10(%rax)
jle 0x16729
movq 0x5118(%rbx), %rdi
leaq 0x610f9(%rip), %rsi     # 0x7781a
xorl %eax, %eax
callq *0x50f8(%rbx)
cmpl $0x100, 0x10(%rbp)      # imm = 0x100
jl 0x16748
movq 0x5118(%rbx), %rdi
leaq 0x61102(%rip), %rsi     # 0x77842
xorl %eax, %eax
callq *0x50f8(%rbx)
movq (%rsp), %rdx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x1f6e2
cmpl $0x3f, 0x4bd0(%rbx)
jne 0x167d4
testq %r14, %r14
je 0x16775
movslq 0x10(%rbp), %rax
leaq (%rsp,%rax,8), %r15
addq $0x30, %r15
jmp 0x16778
xorl %r15d, %r15d
movq 0x18(%rsp), %rdi
callq 0x1a984
movq $0x0, (%rsp)
testq %r13, %r13
je 0x167c1
cmpl $0x16, (%r13)
jne 0x167c1
cmpb $0x1, 0x14(%r13)
jne 0x167c1
cmpl $0x0, 0x10(%r13)
jle 0x167c1
movslq 0x10(%rbp), %rax
cmpl %eax, 0x10(%r13)
jle 0x167c1
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rax
movq 0x108(%rax), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %rsp, %rsi
movq %r15, %rdx
callq 0x1617b
jmp 0x166a0
movq 0x8(%rsp), %rax
movq %rbp, (%rax)
testq %r14, %r14
je 0x1680c
leaq 0xf0(%rbx), %r15
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1ede7
movslq %eax, %rsi
movq %r15, %rdi
callq 0xcca8
movq %rax, (%r14)
movq $0x0, 0x10(%rsp)
jmp 0x1683d
leaq 0x4e38(%rbx), %rdi
leaq 0x100(%rbx), %rdx
movq %r12, %rsi
movq 0x10(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x1a429
movq %rbx, %rdi
movq %rax, 0x10(%rsp)
movq %rax, %rsi
callq 0x1c086
movq 0x8(%rsp), %rax
movq (%rax), %rax
movslq 0x10(%rax), %r13
testq %r13, %r13
jle 0x1690d
leaq 0x4e68(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x4e70(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq (%rsp,%r13,8), %r15
addq $0x28, %r15
incq %r13
movq %rbx, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x10(%rax,%r13,8), %rbp
movq 0x108(%rbp), %rsi
movq %r12, %rdi
callq 0x1ede7
movslq 0x110(%rbp), %rsi
testq %r14, %r14
je 0x168c4
addq (%r14), %rsi
movq 0x108(%rbp), %rcx
movl (%rcx), %ecx
movq 0x18(%rsp), %rdi
movq %r15, %rdx
movl %eax, %r8d
callq 0xef72
jmp 0x168fc
movq 0x10(%rsp), %rax
addl 0x120(%rax), %esi
movq %r14, %rbx
movq %r12, %r14
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x17f78
movq 0x108(%rbp), %rsi
movq %r12, %rdi
movq %r14, %r12
movq %rbx, %r14
movq 0x20(%rsp), %rbx
callq 0x18659
decq %r13
addq $-0x8, %r15
cmpq $0x1, %r13
jg 0x1687a
testq %r14, %r14
jne 0x1691f
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x11b32
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq