// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x15, 0x4(%rax)
jne 0x9cdf
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x1006, (%rax)         # imm = 0x1006
je 0x9b83
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x9b65
movq -0x30(%rbp), %rdi
callq 0x98e0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x9bc2
leaq 0x1e43(%rip), %rdi      # 0xb9e8
leaq 0x1d75(%rip), %rsi      # 0xb921
movl $0xa7, %edx
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0x9d87
jmp 0x9bc4
movq -0x38(%rbp), %rax
cmpl $0x1005, (%rax)         # imm = 0x1005
jne 0x9cda
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
leaq 0x12772(%rip), %rax     # 0x1c360
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x9260
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x9c29
leaq 0x1e06(%rip), %rdi      # 0xba12
leaq 0x1d0e(%rip), %rsi      # 0xb921
movl $0xba, %edx
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0x9d87
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x9260
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x9c69
leaq 0x1df3(%rip), %rdi      # 0xba3f
leaq 0x1cce(%rip), %rsi      # 0xb921
movl $0xc4, %edx
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0x9d87
movq -0x28(%rbp), %rdi
callq 0x9730
movq %rax, %rsi
movq -0x50(%rbp), %rdx
leaq 0x1de5(%rip), %rdi      # 0xba65
movb $0x0, %al
callq 0x2070
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rsi
callq 0x8e30
cmpl $0x0, %eax
jne 0x9cc5
leaq 0x1ddd(%rip), %rdi      # 0xba85
leaq 0x1c72(%rip), %rsi      # 0xb921
movl $0xce, %edx
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0x9d87
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x9bc4
jmp 0x9d80
movq -0x10(%rbp), %rax
cmpl $0x18, 0x4(%rax)
jne 0x9d56
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rdx
leaq -0x1e0(%rip), %rsi      # 0x9b20
callq 0x8c10
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movl $0x19, 0x4(%rax)
movb $0x1, %al
testb $0x1, %al
jne 0x9d20
jmp 0x9d54
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x22f(%rip), %rsi      # 0x9b20
callq 0x8c10
jmp 0x9d7e
movq -0x10(%rbp), %rax
cmpl $0x1b, 0x4(%rax)
jne 0x9d7c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rdx
leaq -0x257(%rip), %rsi      # 0x9b20
callq 0x8c10
jmp 0x9d7e
jmp 0x9d80
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq