// File: /workspace/github_c_makefile_star2_O3/gregtour[P]minimal-compiler/typechecker.c
// Function: CheckExpressionType
int CheckExpressionType(TYPE type, EXPR* expr, FUNC_SEGMENT* segment)
{
    AssertS(expr, "Attempting to check null expression.");

    // check tokens //
    if (expr->production == 0)
    {
        // <identifier>
        if (expr->token == gSymbolIdentifier)
        {
            const char* identifier = expr->string;
            VARLIST* variable;
            variable = GetVariableFromList(segment->locals, identifier);
            if (variable == NULL) {
                variable = GetVariableFromList(segment->params, identifier);
            } if (variable == NULL) {
                variable = GetVariableFromList(gPrgrmVariables, identifier);
            }

            if (variable == NULL)
            {
                CompilerError("Undefined variable in expression.");
                return 1;
            } else {
                return (CanCoerceToType(type, variable->type));
            }
        // <integer>
        } else if (expr->token == gSymbolInteger) {
            TYPE integer;
            integer.basic = TYPE_INTEGER;
            integer.size = 4;
            return (CanCoerceToType(type, integer));
        // <string>
        } else if (expr->token == gSymbolString) {
            TYPE string;
            string.basic = TYPE_STRING;
            string.size = 4;
            return (CanCoerceToType(type, string));
        }
    }

    // check booleans //
    if (expr->production == PROD_FINAL_BOOLEAN) {
        TYPE boolType;
        boolType.basic = TYPE_INTEGER;
        boolType.size = 4;
        return (CanCoerceToType(type, boolType));
    }

    // check binary operators //
    if (IsBinaryOperator(expr->production))
    {
        EXPR* subExprA;
        EXPR* subExprB;
        TYPE nonvoid;

        subExprA = (EXPR*)(expr->children[0]);
        subExprB = (EXPR*)(expr->children[2]);

        if (IsIntegerOnlyOperator(expr->production))
        {
            TYPE intType;
            intType.basic = TYPE_INTEGER;
            intType.size = 4;
            if (CheckExpressionType(intType, subExprA, segment) != 0)
            {
                CompilerError("Attempting to use non-integer in integer operations.");
                return 1;
            }
            if (CheckExpressionType(intType, subExprB, segment) != 0)
            {
                CompilerError("Attempting to use non-integer in integer operations.");
                return 1;
            }

            return 0;
        }

        nonvoid.basic = TYPE_NON_VOID;
        nonvoid.size = 4;

        if (CheckExpressionType(nonvoid, subExprA, segment) != 0)
        {
            CompilerError("Attempting to use void value in expression.");
            return 1;
        }
        if (CheckExpressionType(nonvoid, subExprB, segment) != 0)
        {
            CompilerError("Attempting to use void value in expression.");
            return 1;
        }

        return 0;
    }

    // check unary operators //
    if (IsUnaryOperator(expr->production))
    {
        TYPE nonvoid;
        TYPE integer;
        EXPR* subExpr = (EXPR*)(expr->children[1]);

        nonvoid.basic = TYPE_NON_VOID;
        nonvoid.size = 4;

        integer.basic = TYPE_INTEGER;
        integer.size = 4;

        if (expr->production == PROD_LOGIC_NOT
            || expr->production == PROD_FACTOR_NOT)
        {
            return CheckExpressionType(nonvoid, subExpr, segment);
        }

        return CheckExpressionType(integer, subExpr, segment);
    }

    // check function invocation //
    if (expr->production == PROD_FINAL_FUNCTION_CALL)
    {
        // <final> ::= <identifier> ( <arg*> )
        ARG_LIST* arguments;
        const char* identifier;
        FUNCTION* method;
        VARLIST* paramIterator;

        identifier = expr->children[0]->string;
        arguments = (ARG_LIST*)(expr->children[2]);

        method = GetFunctionFromList(gPrgrmFunctions, identifier);

        if (method == NULL) {
            CompilerError("Error calling undeclared function.");
            return 1;
        }

        // check argument types used as parameters, and the argument count
        if (arguments->production == PROD_ARG_STAR) {
            arguments = NULL;
        }
        // begin loop
        paramIterator = method->parameters;
        while (paramIterator && arguments)
        {
            EXPR* argExpr;
            TYPE  ptype;

            //<arg*> ::= <expr>
            //<arg*> ::= <expr> , <arg*>
            //<arg*> ::= <epsilon>
            argExpr = (EXPR*)(arguments->children[0]);
            ptype = paramIterator->type;

            if (CheckExpressionType(ptype, argExpr, segment) != 0)
            {
                CompilerError("Illegal argument type.");
                return 1;
            }

            // advance the parameter iterator
            paramIterator = paramIterator->next;
            // advance the argument iterator
            if (arguments->production == PROD_ARG_STAR_EXPR_B) {
                arguments = (ARG_LIST*)(arguments->children[2]);
                if (arguments->production == PROD_ARG_STAR) {
                    arguments = NULL;
                }
            } else {
                arguments = NULL;
            }
        }

        // check the argument vs. parameter count
        if (paramIterator != NULL || arguments != NULL)
        {
            CompilerError("Illegal argument count.");
            return 1;
        }

        // check function return type
        if (CanCoerceToType(type, method->type) != 0)
        {
            CompilerError("Type error: use of function with invalid return type.");
            return 1;
        }

        return 0;

    }

    // unhandled case : should throw an exception
    AssertS(0, "Control flow error.");
    CompilerError("Unexpected compiler state.");
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rdi, %rbx
movabsq $0x800000004, %r12      # imm = 0x800000004
movabsq $0x100000004, %r15      # imm = 0x100000004
leaq 0x51f9(%rip), %rax      # 0xc0cc
movl (%rax), %edx
leaq 0x51e4(%rip), %rax      # 0xc0c0
movl (%rax), %ecx
leaq 0x51e3(%rip), %rax      # 0xc0c8
movl (%rax), %eax
leaq 0x19ae(%rip), %rdi      # 0x889c
movl 0x4(%rsi), %r8d
leal -0x24(%r8), %r9d
cmpl $0x1a, %r9d
ja 0x6fa8
movslq (%rdi,%r9,4), %r9
addq %rdi, %r9
jmpq *%r9
movq 0x28(%rsi), %rsi
cmpl $0x38, %r8d
movq %r15, %rbx
cmoveq %r12, %rbx
movq 0x8(%rsi), %rsi
cmpl $0x27, %r8d
cmoveq %r12, %rbx
jmp 0x6eee
movq 0x28(%rsi), %rax
movq (%rax), %rsi
movq 0x10(%rax), %rbx
cmpl $0x35, %r8d
ja 0x7004
movl %r8d, %eax
movabsq $0x3a780000000000, %rcx # imm = 0x3A780000000000
btq %rax, %rcx
jae 0x7004
movq %r15, %rdi
movq %r14, %rdx
callq 0x6ea4
testl %eax, %eax
je 0x6f7a
leaq 0x1d66(%rip), %rdi      # 0x8ccf
leaq 0x1a86(%rip), %rsi      # 0x89f6
movl $0x23c, %edx            # imm = 0x23C
jmp 0x7145
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6ea4
testl %eax, %eax
je 0x71a7
leaq 0x1d38(%rip), %rdi      # 0x8ccf
leaq 0x1a58(%rip), %rsi      # 0x89f6
movl $0x241, %edx            # imm = 0x241
jmp 0x7145
testl %r8d, %r8d
jne 0x7132
movl (%rsi), %edi
cmpl %edx, %edi
jne 0x7102
movq 0x8(%rsi), %r15
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x647b
testq %rax, %rax
jne 0x6ffc
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x647b
testq %rax, %rax
jne 0x6ffc
leaq 0x12388(%rip), %rax     # 0x19370
movq (%rax), %rdi
movq %r15, %rsi
callq 0x647b
testq %rax, %rax
je 0x71c0
movq (%rax), %rsi
jmp 0x711c
movq %r12, %rdi
movq %r14, %rdx
callq 0x6ea4
testl %eax, %eax
je 0x70c2
leaq 0x1ce6(%rip), %rdi      # 0x8d04
leaq 0x19d1(%rip), %rsi      # 0x89f6
movl $0x24d, %edx            # imm = 0x24D
jmp 0x7145
movq 0x28(%rsi), %rax
movq (%rax), %rcx
movq 0x10(%rax), %r12
movq 0x8(%rcx), %rsi
leaq 0x12333(%rip), %rax     # 0x19378
movq (%rax), %rdi
callq 0x64b0
testq %rax, %rax
je 0x70ed
movq %rax, %r15
cmpl $0x22, 0x4(%r12)
setne %cl
movq 0x10(%rax), %r13
testq %r12, %r12
setne %al
andb %cl, %al
testq %r13, %r13
setne %cl
je 0x717b
testb %al, %al
je 0x717b
movq 0x28(%r12), %rax
movq (%rax), %rsi
movq (%r13), %rdi
movq %r14, %rdx
callq 0x6ea4
testl %eax, %eax
jne 0x715e
movq 0x18(%r13), %r13
cmpl $0x21, 0x4(%r12)
jne 0x7173
movq 0x28(%r12), %rax
movq 0x10(%rax), %r12
cmpl $0x22, 0x4(%r12)
setne %cl
jmp 0x7066
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6ea4
testl %eax, %eax
je 0x71a7
leaq 0x1c25(%rip), %rdi      # 0x8d04
leaq 0x1910(%rip), %rsi      # 0x89f6
movl $0x252, %edx            # imm = 0x252
jmp 0x7145
leaq 0x1c3c(%rip), %rdi      # 0x8d30
leaq 0x18fb(%rip), %rsi      # 0x89f6
movl $0x27e, %edx            # imm = 0x27E
jmp 0x7145
cmpl %ecx, %edi
jne 0x710e
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x711f
cmpl %eax, %edi
jne 0x7132
movabsq $0x200000004, %rsi      # imm = 0x200000004
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7251
leaq 0x1c7f(%rip), %rdi      # 0x8db8
leaq 0x18b6(%rip), %rsi      # 0x89f6
movl $0x2ba, %edx            # imm = 0x2BA
callq 0x4a64
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bee(%rip), %rdi      # 0x8d53
leaq 0x188a(%rip), %rsi      # 0x89f6
movl $0x295, %edx            # imm = 0x295
jmp 0x7145
testq %r13, %r13
setne %cl
xorl %eax, %eax
testb %cl, %cl
jne 0x71ab
testb %al, %al
jne 0x71ab
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x7251
testl %eax, %eax
je 0x71a7
leaq 0x1be9(%rip), %rdi      # 0x8d82
leaq 0x1856(%rip), %rsi      # 0x89f6
movl $0x2b0, %edx            # imm = 0x2B0
jmp 0x7145
xorl %eax, %eax
jmp 0x714f
leaq 0x1bb8(%rip), %rdi      # 0x8d6a
leaq 0x183d(%rip), %rsi      # 0x89f6
movl $0x2a9, %edx            # imm = 0x2A9
jmp 0x7145
leaq 0x1ae6(%rip), %rdi      # 0x8cad
leaq 0x1828(%rip), %rsi      # 0x89f6
movl $0x20f, %edx            # imm = 0x20F
jmp 0x7145