// File: /workspace/github_c_makefile_star2_O1/gregtour[P]minimal-compiler/typechecker.c
// Function: CreateLocalVariables
int CreateLocalVariables(FSTMT* statement, void* funcHeader)
{
    FUNC_SEGMENT* segment = (FUNC_SEGMENT*)funcHeader;

    Assert(statement != NULL);
    Assert(funcHeader != NULL);

    // look only at type declarations
    if (statement->production == PROD_FSTMT_TYPE_DECLARATION)
    {
        // <fstmt> ::= <type-declaration> ;
        TYPE type;
        TYPE_DECL* typeTag;
        SYNTAX_TREE* declaration = statement->children[0];

        Assert(declaration->token == SYMBOL_TYPE_DECLARATION);

        // (A) <type-declaration> ::= <identifier> : <type>
        // (B) <type-declaration> ::= <identifier> , <type-declaration>
        typeTag = (TYPE_DECL*)declaration->children[2];

        while (typeTag->token != SYMBOL_TYPE) 
        {
            Assert(typeTag->token == SYMBOL_TYPE_DECLARATION);
            Assert(typeTag->numChildren == 3);
            typeTag = typeTag->children[2];
        }

        type = DeriveType(typeTag);
        if (type.basic == TYPE_VOID)
        {
            CompilerError("Attempting to define variable(s) as void.");
            return 1;
        }

        // LinkVariable(<type>, <identifier>)
        while (declaration->token == SYMBOL_TYPE_DECLARATION)
        {
            VARLIST* existing;
            const char* identifier;

            Assert(declaration->children);
            Assert(declaration->children[0]);

            identifier = declaration->children[0]->string;

            // check for duplicate list with global vars
            existing = GetVariableFromList(gPrgrmVariables, identifier);
            if (existing != NULL)
            {
                CompilerError("Global variable redefined in local function.");
                Assert(0);

                return 1;
            }

            // check for duplicate list with parameters
            existing = GetVariableFromList(segment->params, identifier);
            if (existing != NULL)
            {
                CompilerError("Parameter redefined in function body.");
                Assert(0);

                return 1;
            }

            //LinkVariableRoot(type, identifier);
            printf("Linking local variable %s %s. \n", TypeString(type), identifier);
            if (AddVariableToList(&segment->locals, type, identifier) == 0)
            {
                CompilerError("Local variable redefined.");
                return 1;
            }

            declaration = declaration->children[2];
        }
    }
    else if (statement->production == PROD_FSTMT_IF)
    {
        // <fstmt> ::= if ( <expr> ) <block> <else>
        ProcessCodeBlock(statement->children[4], CreateLocalVariables, funcHeader);
        if (statement->children[5]->production = PROD_ELSE_ELSE)
        {
            // <else> ::= else <block>
            SYNTAX_TREE* elseBranch;
            SYNTAX_BLOCK* elseBlock;
            elseBranch = statement->children[5];
            elseBlock = (SYNTAX_BLOCK*)(elseBranch->children[1]);
            ProcessCodeBlock(elseBlock, CreateLocalVariables, funcHeader);
        }
    }
    else if (statement->production == PROD_FSTMT_WHILE)
    {
        // <fstmt> ::= while ( <expr> ) <block>
        ProcessCodeBlock(statement->children[4], CreateLocalVariables, funcHeader);
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %r15
movl 0x4(%rdi), %eax
xorl %ebx, %ebx
cmpl $0x1b, %eax
je 0x6acf
cmpl $0x18, %eax
je 0x6aa1
cmpl $0x15, %eax
jne 0x6af6
movq 0x28(%r15), %rax
movq (%rax), %r12
leaq 0x18(%r14), %rax
movq %rax, -0x30(%rbp)
leaq 0x12991(%rip), %rbx     # 0x19370
leaq 0x2070(%rip), %r13      # 0x8a56
cmpl $0x1005, (%r12)         # imm = 0x1005
jne 0x6aed
movq 0x28(%r12), %rax
movq (%rax), %rax
movq 0x8(%rax), %r15
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x6370
testq %rax, %rax
je 0x6a21
leaq 0x2106(%rip), %rdi      # 0x8b1d
movq %r13, %rsi
movl $0xba, %edx
jmp 0x6a41
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x6370
testq %rax, %rax
je 0x6a51
leaq 0x2111(%rip), %rdi      # 0x8b4a
movq %r13, %rsi
movl $0xc4, %edx
callq 0x4a54
xorl %eax, %eax
testb %al, %al
jne 0x69e6
jmp 0x6af1
leaq 0x2118(%rip), %rdi      # 0x8b70
leaq 0x1fad(%rip), %rsi      # 0x8a0c
movq %r15, %rdx
xorl %eax, %eax
callq 0x2090
movq -0x30(%rbp), %rdi
movabsq $0x100000004, %rsi      # imm = 0x100000004
movq %r15, %rdx
callq 0x611f
testl %eax, %eax
je 0x6a90
movq 0x28(%r12), %rax
movq 0x10(%rax), %r12
movb $0x1, %al
jmp 0x6a48
leaq 0x20f9(%rip), %rdi      # 0x8b90
movq %r13, %rsi
movl $0xce, %edx
jmp 0x6a41
movq 0x28(%r15), %rax
movq 0x20(%rax), %rdi
leaq -0x11b(%rip), %rsi      # 0x6995
movq %r14, %rdx
callq 0x5ffb
movq 0x28(%r15), %rax
movq 0x28(%rax), %r15
movl $0x19, 0x4(%r15)
movl $0x8, %eax
jmp 0x6ad4
movl $0x20, %eax
movq 0x28(%r15), %rcx
movq (%rcx,%rax), %rdi
leaq -0x14e(%rip), %rsi      # 0x6995
movq %r14, %rdx
callq 0x5ffb
jmp 0x6af6
xorl %ebx, %ebx
jmp 0x6af6
movl $0x1, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq