// File: /workspace/github_c_makefile_star2_O3/MartinNovotny25[P]VUT-FIT-IFJ2020/semantics.c
// Function: checkCallFunction
void checkCallFunction(TDLList *L, char *id, tBSTNodePtrLocal *node) {
    
    functionData data;
    //sem bude vracat hodnotu BSTSearchLoacal
    int type = 0;
    
    //volanu funkciu nieje mozne volat, lebo je prekryta identifikatorom premennej
    if (MainStackSearch(mainstack, id, &type)) {
        error_call(ERR_SEM_OTHER, L);
        BSTDisposeGlobal(&functions);
        BSTDisposeLocal(node);
    }
    
    //ak sa vola print, treba overit iba identifikatory v symtables
    if (!strcmp(id, "print")) {
        //ak najdeme pravu zatvorku, sme na konci volania
        while (L->Act->tdata.type != t_RIGHT_BRACKET) {
            TDLLSucc(L);
            //nasli sme id, treba overit v symtable
            if (L->Act->tdata.type == t_IDENTIFIER) {
                
                //if (BSTSearchLocal(*node, L->Act->tdata.lex, &type, unneeded)) {
                if (MainStackSearch(mainstack, L->Act->tdata.lex, &type)) {
                    return;
                } else {
                    error_call(ERR_SEM_RETURN, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
            }
            
        }
        return;
    }
    
    //sem bude vracat data BSTSearchGlobal
    if (BSTSearchGlobal(functions, id, &data)) {
        //v priprade existencie navratovych typov sa nesmie volat mimo priradenia
        if (data.numOfReturns > 0) {
            if (L->Act->lptr->tdata.type == t_EOL) {
                error_call(ERR_SEM_RETURN, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
        }
        
        for (int i = 0; i < data.numOfParams; i++) {
            //posun z '(' -> 'parameter' alebo ',' -> 'parameter'
            TDLLSucc(L);
            TDLLSucc(L);
            //ak je parameter id
            if (L->Act->tdata.type == t_IDENTIFIER) {
                //musi byt v lokalnej/nadlokalnej symtable
                //nesmie to byt '_'
                if (MainStackSearch(mainstack, L->Act->tdata.lex, &type) && (strcmp(L->Act->tdata.lex, "_"))) {
                    //jeho typ musi byt rovnaky ako typ parametru na pozicii i vo funkcii
                    if (type == data.paramsType[i]) {
                        //vsetko sedi, kontroluj dalsi parameter
                        continue;
                    } else {
                        error_call(ERR_SEM_RETURN, L);
                        BSTDisposeGlobal(&functions);
                        BSTDisposeLocal(node);
                    }
                } else {
                    error_call(ERR_SEM_RETURN, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
            } else {
                //parameter nie je identifikator, je napr konstanta, retazcovy literal...
                switch (L->Act->tdata.type) {
                    case t_INT_ZERO | t_INT_NON_ZERO:
                        if (data.paramsType[i] == t_INT_ID) {
                            //parameter v poriadku, kontroluj dalsi parameter
                            continue;
                        } else {
                            error_call(ERR_SEM_RETURN, L);
                            BSTDisposeGlobal(&functions);
                            BSTDisposeLocal(node);
                        }
                        break;
                    case t_STRING:
                        if (data.paramsType[i] == t_STRING_ID) {
                            //parameter v poriadku, kontroluj dalsi parameter
                            continue;
                        } else {
                            error_call(ERR_SEM_RETURN, L);
                            BSTDisposeGlobal(&functions);
                            BSTDisposeLocal(node);
                        }
                        break;
                    case t_FLOAT:
                        if (data.paramsType[i] == t_FLOAT64) {
                            //parameter v poriadku, kontroluj dalsi parameter
                            continue;
                        } else {
                            error_call(ERR_SEM_RETURN, L);
                            BSTDisposeGlobal(&functions);
                            BSTDisposeLocal(node);
                        }
                        break;
                        
                    default:
                        break;
                }
            }
        }
        //aktivny prvok by mal byt na poslednom predavanom parametry
        //ak vpravo od neho neni '(' znamena to nespravny pocet parametrov
        if (strcmp(L->Act->rptr->tdata.lex, ")")) {
            //fix pre pripad nula parametrov
            if (L->Act->tdata.type == t_IDENTIFIER && L->Act->rptr->rptr->tdata.type == t_RIGHT_BRACKET) {
                return;
            }
            error_call(ERR_SEM_RETURN, L);
        }
    }else{
        error_call(ERR_SEM_UNDEF, L);
        BSTDisposeGlobal(&functions);
        BSTDisposeLocal(node);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f58, %rsp           # imm = 0x2F58
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1f4c(%rsp), %rax
movl $0x0, (%rax)
leaq 0x7658(%rip), %rsi      # 0xc488
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %rax, %rsi
callq 0x46d5
testb %al, %al
je 0x4e6b
movl $0x7, %edi
movq %rbx, %rsi
callq 0x42d0
leaq 0x9572(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %r14, %rdi
callq 0x4662
leaq 0x36ad(%rip), %rsi      # 0x851f
movq %r15, %rdi
callq 0x1080
testl %eax, %eax
je 0x4fd0
movq 0x9547(%rip), %rdi      # 0xe3d0
leaq 0x1f50(%rsp), %rdx
movq %r15, %rsi
callq 0x43b0
testb %al, %al
je 0x5057
cmpl $0x0, 0x1f54(%rsp)
jle 0x4eda
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x4eda
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42d0
leaq 0x9503(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %r14, %rdi
callq 0x4662
cmpl $0x0, 0x1f50(%rsp)
jle 0x507a
xorl %ebp, %ebp
leaq 0x7597(%rip), %r15      # 0xc488
leaq 0x1f4c(%rsp), %r12
leaq 0x94d0(%rip), %r13      # 0xe3d0
movq %rbx, %rdi
callq 0x1691
movq %rbx, %rdi
callq 0x1691
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
cmpl $0x28, %eax
jg 0x4f6e
cmpl $0x11, %eax
je 0x4fb8
cmpl $0x13, %eax
jne 0x4f9f
movq (%rcx), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r12, %rsi
callq 0x46d5
testb %al, %al
je 0x4f82
movq 0x8(%rbx), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4f5c
cmpb $0x0, 0x1(%rax)
je 0x4f82
movl 0x1f4c(%rsp), %eax
cmpl 0x2758(%rsp,%rbp,4), %eax
jne 0x4f82
jmp 0x4f9f
cmpl $0x29, %eax
je 0x4fc4
cmpl $0x2b, %eax
jne 0x4f9f
cmpl $0x14, 0x2758(%rsp,%rbp,4)
je 0x4f9f
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42d0
movq %r13, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
incq %rbp
movslq 0x1f50(%rsp), %rax
cmpq %rax, %rbp
jl 0x4f00
jmp 0x507a
cmpl $0x18, 0x2758(%rsp,%rbp,4)
jne 0x4f82
jmp 0x4f9f
cmpl $0x17, 0x2758(%rsp,%rbp,4)
jne 0x4f82
jmp 0x4f9f
movq 0x8(%rbx), %rax
cmpl $0x7, 0x8(%rax)
je 0x50ad
leaq 0x74a3(%rip), %r15      # 0xc488
leaq 0x1f4c(%rsp), %r12
leaq 0x93dc(%rip), %r13      # 0xe3d0
movq %rbx, %rdi
callq 0x1691
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
cmpl $0x13, %eax
jne 0x5050
movq (%rcx), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r12, %rsi
callq 0x46d5
testb %al, %al
jne 0x50ad
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42d0
movq %r13, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movq 0x8(%rbx), %rax
movl 0x8(%rax), %eax
cmpl $0x7, %eax
jne 0x4ff4
jmp 0x50ad
movl $0x3, %edi
movq %rbx, %rsi
callq 0x42d0
leaq 0x9365(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %r14, %rdi
callq 0x4662
jmp 0x50ad
movq 0x8(%rbx), %rcx
movq 0x18(%rcx), %rax
movq (%rax), %rdx
cmpb $0x29, (%rdx)
jne 0x5090
cmpb $0x0, 0x1(%rdx)
je 0x50ad
cmpl $0x13, 0x8(%rcx)
jne 0x50a0
movq 0x18(%rax), %rax
cmpl $0x7, 0x8(%rax)
je 0x50ad
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42d0
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq