// File: /workspace/github_c_makefile_star2_O3/anuwu[P]ERPLAG-Compiler/symbolTable.c
// Function: fillSymbolTable
baseST * fillSymbolTable (astNode * thisASTNode , int depthSTPrint) 
{
	astNode * currentASTNode = thisASTNode ;
	realBase = createBaseST () ;

	//****************************************************************
	astNode * moduleDECS = currentASTNode->child ;
	currentASTNode = moduleDECS->child ;
	while (currentASTNode) 
	{
		varST * searchResult ;
		if((searchResult =  searchVarInbaseST (currentASTNode->tok->lexeme))) 
		{
			errSemantic () ;
			#ifdef _WIN64
				printf ("In line %d, \"%s\" module already declared.\n" , currentASTNode->tok->lineNumber, searchResult->lexeme ) ;
			#endif
			#if defined __linux__ || __MACH__
				printf ("In line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " module already declared.\n" , currentASTNode->tok->lineNumber, searchResult->lexeme ) ;
			#endif
			realBase->semanticError = 1 ;
		}
		else {
			varST * tmp = createVarST (currentASTNode->tok->lexeme , realBase , VAR_MODULE, TK_ID) ;
			insertVarSTInbaseST (tmp) ;
		}

		currentASTNode = currentASTNode->next ;
	}

	//***************************************************************
	astNode * otherMODS = moduleDECS->next ;
	moduleST *searchCond ;
	
	for (int otherMODS_Count = 1 ; otherMODS_Count <= 2 ; otherMODS_Count++)
	{
		currentASTNode = otherMODS->child ;
		//currentASTNode is now a modulef
		while (currentASTNode) 
		{
			searchCond = searchModuleInbaseST (currentASTNode->child->tok->lexeme) ;
			
			if (searchCond == NULL) {
				// need to create and insert
				moduleST * moduleToInsert = createModuleST ((void *)realBase, currentASTNode->child->tok->lexeme, 16) ;

				// filling input_plist and output_plist
				astNode * input_plistAST = currentASTNode->child->next ;
				astNode * iplAST = input_plistAST->child ;

				while (iplAST) 
				{

					if (searchVarModule (moduleToInsert , iplAST->child->tok->lexeme) != NULL ) 
					{
						errSemantic () ;
						#ifdef _WIN64
							printf ("In the signature of \"%s\" at line %d, input variable \"%s\" already declared\n", moduleToInsert->lexeme,iplAST->child->tok->lineNumber, iplAST->child->tok->lexeme) ;
						#endif
						#if defined __linux__ || __MACH__
							printf ("In the signature of " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "input" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " already declared\n", moduleToInsert->lexeme,iplAST->child->tok->lineNumber, iplAST->child->tok->lexeme) ;
						#endif
						realBase->semanticError = 1 ;
					}
					else{
						varST * tmp = createVarST (iplAST->child->tok->lexeme, moduleToInsert , VAR_INPUT, -9999) ;
						int retSize ;
					
						if ( iplAST->child->next->dtTag == PRIMITIVE) 
						{
							tmp->datatype = iplAST->child->next->dt->pType ;

							// Setting and increasing offset
							retSize = getSize (tmp) ;
							tmp->offset = -moduleToInsert->currOffset ;
							moduleToInsert->currOffset += retSize ;
							moduleToInsert->inputSize += retSize ;
						}
						else{
							// array 
							tmp->datatype = TK_ARRAY ;
							tmp->arrayIndices = iplAST->child->next->dt->arrType ;

							retSize = getSize (tmp) ;		// Return will be positive or -1	

							if (retSize > 0)
							{
								tmp->offset = -moduleToInsert->currOffset ;
								tmp->size = retSize ;
								moduleToInsert->currOffset += retSize ;
								moduleToInsert->inputSize += retSize ;
							}
							else
								tmp->offset = retSize ;
						}

						insertVar (moduleToInsert , tmp, INSERT_INPUT) ;
					}
				
					iplAST = iplAST->next ;
				}

				astNode * retAST = input_plistAST->next ;
				astNode * oplAST = retAST->child ;
				

				// Handle exact offsets later

				while (oplAST) 
				{
					// Input and output parameters CANNOT share identifiers
					varST *searchedInput, *searchedOutput ;
					searchedInput = searchVarModuleList (moduleToInsert , oplAST->child->tok->lexeme, SEARCH_INPUT) ;
					searchedOutput = searchVarModuleList (moduleToInsert , oplAST->child->tok->lexeme, SEARCH_OUTPUT) ;

					if (searchedInput != NULL) 
					{
						errSemantic () ;
						#ifdef _WIN64
							printf ("In the signature of \"%s\" at line %d, input  variable \"%s\" is already declared\n", moduleToInsert->lexeme,oplAST->child->tok->lineNumber, oplAST->child->tok->lexeme) ;
						#endif
						#if defined __linux__ || __MACH__
							printf ("In the signature of " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "input " ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " is already declared\n", moduleToInsert->lexeme,oplAST->child->tok->lineNumber, oplAST->child->tok->lexeme) ;
						#endif
						realBase->semanticError = 1 ;
					}
					else if (searchedOutput != NULL)
					{
						errSemantic () ;
						#ifdef _WIN64
							printf ("In the signature of \"%s\" at line %d, output variable \"%s\" is already declared\n", moduleToInsert->lexeme,oplAST->child->tok->lineNumber, oplAST->child->tok->lexeme) ;
						#endif
						#if defined __linux__ || __MACH__
							printf ("In the signature of " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "output" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " is already declared\n", moduleToInsert->lexeme,oplAST->child->tok->lineNumber, oplAST->child->tok->lexeme) ;
						#endif
						realBase->semanticError = 1 ;
					}
					else
					{
						varST * tmp = createVarST ( oplAST->child->tok->lexeme , moduleToInsert, VAR_OUTPUT, oplAST->child->next->id) ;
						int retSize = getSize (tmp) ;

						tmp->offset = -moduleToInsert->currOffset ;
						tmp->size = retSize ;
						moduleToInsert->currOffset += retSize ;
						moduleToInsert->outputSize += retSize ;
						insertVar (moduleToInsert, tmp, INSERT_OUTPUT) ;
					}
					
					oplAST = oplAST->next ;
				}

				moduleToInsert->currOffset = 0 ;	//resetting for input values
				insertModuleSTInbaseST (moduleToInsert) ;

			}
			else {
				// Only valid when otherMODS_Count = 1
				errSemantic () ;
				#ifdef _WIN64
					printf ("In line %d, module \"%s\" already defined\n", currentASTNode->child->tok->lineNumber, currentASTNode->child->tok->lexeme) ;
				#endif
				#if defined __linux__ || __MACH__
					printf ("In line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", module " ANSI_BOLD "%s" ANSI_RESET " already defined\n", currentASTNode->child->tok->lineNumber, currentASTNode->child->tok->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
				currentASTNode->child->prev = currentASTNode->child ;		// Encoding to be removed later
			}

			currentASTNode = currentASTNode->next ;
		}

		if (otherMODS->next != NULL)
			otherMODS = otherMODS->next->next ;
	}

	// ------------------------------------------------- fillModuleST --------------------------------------

	otherMODS = moduleDECS->next ;


	for (int otherMODS_Count = 1 ; otherMODS_Count <= 2 ; otherMODS_Count++)
	{
		currentASTNode = otherMODS->child ;

		//currentASTNode is now a module
		while (currentASTNode)
		{
			if (currentASTNode->child->prev == currentASTNode->child)		// For invalid nodes
			{
				currentASTNode->child->prev = NULL ;
				currentASTNode = currentASTNode->next ;
				continue ;
			}
			
			moduleST *moduleToInsert = searchModuleInbaseST (currentASTNode->child->tok->lexeme) ;
			fillModuleST (moduleToInsert , currentASTNode->child->next->next->next, depthSTPrint) ;

			if (!checkAllOutputsTinkered(moduleToInsert))
			{
				errWarning () ;
				#ifdef _WIN64
					printf ("In the signature of \"%s\", the following output variables are not assigned - ", moduleToInsert->lexeme) ;
				#endif
				#if defined __linux__ || __MACH__
					printf ("In the signature of " ANSI_BOLD "%s" ANSI_RESET ", the following " ANSI_BOLD ANSI_BLACK "output" ANSI_RESET " variables are not assigned - ", moduleToInsert->lexeme) ;
				#endif
				printOutputsNotTinkered (moduleToInsert) ;
				printf ("\n") ;
			}

			moduleToInsert->filledMod = 1 ;
			printModuleST (moduleToInsert, depthSTPrint) ;

			currentASTNode = currentASTNode->next ;
		}

		if (otherMODS->next != NULL)
		{
			astNode * driverMODS = otherMODS->next ;
			moduleST * driverST = createDriverST() ;
			realBase->driverST = driverST ;
			
			fillModuleST (driverST , driverMODS->child, depthSTPrint) ;
			printModuleST (driverST, depthSTPrint) ;
			
			otherMODS = otherMODS->next->next ;
		}
	}

	//*****************************************************************
			
	return realBase ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, -0x2c(%rbp)
movq %rdi, %rbx
movl $0xe0, %edi
callq 0x1120
movq %rax, %r14
movl $0xdc, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x10c0
movq %r14, 0x23674(%rip)     # 0x31028
movq 0x30(%rbx), %rax
movq %rax, -0x38(%rbp)
movq 0x30(%rax), %r15
testq %r15, %r15
je 0xda60
leaq 0x5380(%rip), %rbx      # 0x12d50
movq 0x8(%r15), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
callq 0xb709
testq %rax, %rax
je 0xda17
movq %rax, %r12
xorl %eax, %eax
callq 0xe989
movq 0x8(%r15), %rax
movl 0x10(%rax), %esi
movq (%r12), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movq 0x2361d(%rip), %rax     # 0x31028
movl $0x1, 0xd8(%rax)
jmp 0xda53
movq 0x2360a(%rip), %r12     # 0x31028
movl $0x38, %edi
callq 0x1120
movq %r14, (%rax)
movl $0x40, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl $0x0, 0x20(%rax)
movq %r12, 0x28(%rax)
movl $0x5, 0x30(%rax)
movq %rax, %rdi
callq 0xb58c
movq 0x38(%r15), %r15
testq %r15, %r15
jne 0xd9d0
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rdx
movl $0x1, %eax
xorl %r12d, %r12d
movl %eax, -0x30(%rbp)
movq %rdx, -0x40(%rbp)
movq 0x30(%rdx), %r15
testq %r15, %r15
je 0xddf0
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0xb792
testq %rax, %rax
je 0xdadf
xorl %eax, %eax
callq 0xe989
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %esi
movq 0x8(%rax), %rdx
leaq 0x5443(%rip), %rdi      # 0x12efd
xorl %eax, %eax
callq 0x10b0
movq 0x23560(%rip), %rax     # 0x31028
movl $0x1, 0xd8(%rax)
movq 0x30(%r15), %rax
movq %rax, 0x40(%rax)
jmp 0xdde7
movq 0x23542(%rip), %r14     # 0x31028
movl $0x118, %edi            # imm = 0x118
callq 0x1120
movq %rax, %r13
movq %rbx, (%rax)
movl $0x1, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movups %xmm0, 0x48(%rax)
movups %xmm0, 0x58(%rax)
movups %xmm0, 0x68(%rax)
movq %r12, 0x78(%rax)
movups %xmm0, 0x88(%rax)
movups %xmm0, 0x98(%rax)
movups %xmm0, 0xa8(%rax)
movups %xmm0, 0xb8(%rax)
movups %xmm0, 0xc8(%rax)
movups %xmm0, 0xd8(%rax)
movups %xmm0, 0xe8(%rax)
movq %r12, 0xf8(%rax)
movq %r14, 0x10(%rax)
movl $0x10, 0x10c(%rax)
movq %r12, 0x100(%rax)
movl %r12d, 0x108(%rax)
movl %r12d, 0x110(%rax)
movq %r15, -0x50(%rbp)
movq 0x30(%r15), %rax
movq 0x38(%rax), %rax
movq %rax, -0x48(%rbp)
movq 0x30(%rax), %r15
testq %r15, %r15
je 0xdcaa
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0xb8a1
testq %rax, %rax
je 0xdbee
xorl %eax, %eax
callq 0xe989
movq (%r13), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x51c2(%rip), %rdi      # 0x12d93
xorl %eax, %eax
callq 0x10b0
movq 0x23449(%rip), %rax     # 0x31028
movl $0x1, 0xd8(%rax)
jmp 0xdca1
movl $0x38, %edi
callq 0x1120
movq %rax, %rbx
movq %r14, (%rax)
movl $0xffffd8f1, 0x8(%rax)  # imm = 0xFFFFD8F1
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl %r12d, 0x20(%rax)
movq %r13, 0x28(%rax)
movl %r12d, 0x30(%rax)
movq 0x30(%r15), %rax
movq 0x38(%rax), %rax
cmpl %r12d, 0x10(%rax)
je 0xdc60
movl $0x3b, 0x8(%rbx)
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%rbx)
movq %rbx, %rdi
callq 0xc569
testl %eax, %eax
jle 0xdc91
movl 0x10c(%r13), %ecx
movl %eax, %edx
addl %ecx, %edx
negl %ecx
movl %ecx, 0x1c(%rbx)
movl %eax, 0x18(%rbx)
movl %edx, 0x10c(%r13)
jmp 0xdc88
movq 0x18(%rax), %rax
movl (%rax), %eax
movl %eax, 0x8(%rbx)
movq %rbx, %rdi
callq 0xc569
movl 0x10c(%r13), %ecx
movl %ecx, %edx
negl %edx
movl %edx, 0x1c(%rbx)
addl %eax, %ecx
movl %ecx, 0x10c(%r13)
addl %eax, 0x104(%r13)
jmp 0xdc94
movl %eax, 0x1c(%rbx)
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xb68b
movq 0x38(%r15), %r15
jmp 0xdb8b
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x30(%rax), %r15
testq %r15, %r15
je 0xddcd
movq 0x30(%r15), %r12
movq 0x8(%r12), %rax
movq 0x8(%rax), %rbx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xb823
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0xb823
testq %r14, %r14
je 0xdd14
xorl %eax, %eax
callq 0xe989
movq (%r13), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x50f7(%rip), %rdi      # 0x12e09
jmp 0xdd3a
testq %rax, %rax
je 0xdd5b
xorl %eax, %eax
callq 0xe989
movq (%r13), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x5149(%rip), %rdi      # 0x12e83
xorl %eax, %eax
callq 0x10b0
movq 0x232e0(%rip), %rax     # 0x31028
movl $0x1, 0xd8(%rax)
movq 0x38(%r15), %r15
jmp 0xdcb6
movq 0x38(%r12), %rax
movl (%rax), %r12d
movl $0x38, %edi
callq 0x1120
movq %rax, %r14
movq %rbx, (%rax)
movl %r12d, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl $0x0, 0x20(%rax)
movq %r13, 0x28(%rax)
movl $0x2, 0x30(%rax)
movq %rax, %rdi
callq 0xc569
movl 0x10c(%r13), %ecx
movl %ecx, %edx
negl %edx
movl %edx, 0x1c(%r14)
movl %eax, 0x18(%r14)
addl %eax, %ecx
movl %ecx, 0x10c(%r13)
addl %eax, 0x108(%r13)
movq %r13, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xb68b
jmp 0xdd52
movl $0x0, 0x10c(%r13)
movq %r13, %rdi
callq 0xb511
xorl %r12d, %r12d
movq -0x50(%rbp), %r15
movq 0x38(%r15), %r15
jmp 0xda7b
movq -0x40(%rbp), %rdx
movq 0x38(%rdx), %rax
testq %rax, %rax
movl -0x30(%rbp), %ecx
je 0xde04
movq 0x38(%rax), %rdx
incl %ecx
cmpl $0x3, %ecx
movl %ecx, %eax
jne 0xda70
movq -0x38(%rbp), %rax
movq 0x38(%rax), %r15
movl $0x1, %r12d
movq 0x30(%r15), %r13
testq %r13, %r13
je 0xdeca
movq 0x30(%r13), %r14
cmpq %r14, 0x40(%r14)
je 0xde78
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
callq 0xb792
movq %rax, %rbx
movq 0x38(%r14), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rsi
movq %rbx, %rdi
movl -0x2c(%rbp), %edx
callq 0xcd55
movq 0x90(%rbx), %rax
testq %rax, %rax
je 0xdeac
movq (%rax), %rcx
cmpl $0x0, 0x20(%rcx)
je 0xde82
movq 0x8(%rax), %rax
jmp 0xde64
movq $0x0, 0x40(%r14)
jmp 0xdec1
xorl %eax, %eax
callq 0xe9b8
movq (%rbx), %rsi
leaq 0x50a6(%rip), %rdi      # 0x12f39
xorl %eax, %eax
callq 0x10b0
movq %rbx, %rdi
callq 0xcb25
movl $0xa, %edi
callq 0x1040
movl $0x1, 0x100(%rbx)
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
callq 0xba4c
movq 0x38(%r13), %r13
jmp 0xde23
movq 0x38(%r15), %r13
testq %r13, %r13
je 0xdf92
movq 0x2314a(%rip), %r14     # 0x31028
movl $0x118, %edi            # imm = 0x118
callq 0x1120
movq %rax, %rbx
leaq 0x3832(%rip), %rax      # 0x11724
movq %rax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
movups %xmm0, 0x28(%rbx)
movups %xmm0, 0x38(%rbx)
movups %xmm0, 0x48(%rbx)
movups %xmm0, 0x58(%rbx)
movups %xmm0, 0x68(%rbx)
movl $0x0, %eax
movq %rax, 0x78(%rbx)
movups %xmm0, 0x88(%rbx)
movups %xmm0, 0x98(%rbx)
movups %xmm0, 0xa8(%rbx)
movups %xmm0, 0xb8(%rbx)
movups %xmm0, 0xc8(%rbx)
movups %xmm0, 0xd8(%rbx)
movups %xmm0, 0xe8(%rbx)
movq %rax, 0xf8(%rbx)
movq %r14, 0x10(%rbx)
movl %eax, 0x8(%rbx)
movl %eax, 0x110(%rbx)
movups %xmm0, 0x100(%rbx)
movq %rbx, 0xd0(%r14)
movq 0x30(%r13), %rsi
movq %rbx, %rdi
movl -0x2c(%rbp), %r14d
movl %r14d, %edx
callq 0xcd55
movq %rbx, %rdi
movl %r14d, %esi
callq 0xba4c
movq 0x38(%r15), %rax
movq 0x38(%rax), %r15
incl %r12d
cmpl $0x3, %r12d
jne 0xde1f
movq 0x23082(%rip), %rax     # 0x31028
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)