// File: /workspace/github_c_makefile_star2_O1/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, 0x23e11(%rip)     # 0x32028
movq 0x30(%rbx), %rax
movq %rax, -0x38(%rbp)
movq 0x30(%rax), %r15
testq %r15, %r15
je 0xe2c3
leaq 0x5b0d(%rip), %rbx      # 0x13d40
movq 0x8(%r15), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
callq 0xc251
testq %rax, %rax
je 0xe27a
movq %rax, %r12
xorl %eax, %eax
callq 0xf205
movq 0x8(%r15), %rax
movl 0x10(%rax), %esi
movq (%r12), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movq 0x23dba(%rip), %rax     # 0x32028
movl $0x1, 0xd8(%rax)
jmp 0xe2b6
movq 0x23da7(%rip), %r12     # 0x32028
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 0xc0d4
movq 0x38(%r15), %r15
testq %r15, %r15
jne 0xe233
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 0xe653
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0xc2da
testq %rax, %rax
je 0xe342
xorl %eax, %eax
callq 0xf205
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %esi
movq 0x8(%rax), %rdx
leaq 0x5bd0(%rip), %rdi      # 0x13eed
xorl %eax, %eax
callq 0x10b0
movq 0x23cfd(%rip), %rax     # 0x32028
movl $0x1, 0xd8(%rax)
movq 0x30(%r15), %rax
movq %rax, 0x40(%rax)
jmp 0xe64a
movq 0x23cdf(%rip), %r14     # 0x32028
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 0xe50d
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0xc3e9
testq %rax, %rax
je 0xe451
xorl %eax, %eax
callq 0xf205
movq (%r13), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x594f(%rip), %rdi      # 0x13d83
xorl %eax, %eax
callq 0x10b0
movq 0x23be6(%rip), %rax     # 0x32028
movl $0x1, 0xd8(%rax)
jmp 0xe504
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 0xe4c3
movl $0x3b, 0x8(%rbx)
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%rbx)
movq %rbx, %rdi
callq 0xcf9f
testl %eax, %eax
jle 0xe4f4
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 0xe4eb
movq 0x18(%rax), %rax
movl (%rax), %eax
movl %eax, 0x8(%rbx)
movq %rbx, %rdi
callq 0xcf9f
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 0xe4f7
movl %eax, 0x1c(%rbx)
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xc1d3
movq 0x38(%r15), %r15
jmp 0xe3ee
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x30(%rax), %r15
testq %r15, %r15
je 0xe630
movq 0x30(%r15), %r12
movq 0x8(%r12), %rax
movq 0x8(%rax), %rbx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xc36b
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0xc36b
testq %r14, %r14
je 0xe577
xorl %eax, %eax
callq 0xf205
movq (%r13), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x5884(%rip), %rdi      # 0x13df9
jmp 0xe59d
testq %rax, %rax
je 0xe5be
xorl %eax, %eax
callq 0xf205
movq (%r13), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x58d6(%rip), %rdi      # 0x13e73
xorl %eax, %eax
callq 0x10b0
movq 0x23a7d(%rip), %rax     # 0x32028
movl $0x1, 0xd8(%rax)
movq 0x38(%r15), %r15
jmp 0xe519
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 0xcf9f
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 0xc1d3
jmp 0xe5b5
movl $0x0, 0x10c(%r13)
movq %r13, %rdi
callq 0xc059
xorl %r12d, %r12d
movq -0x50(%rbp), %r15
movq 0x38(%r15), %r15
jmp 0xe2de
movq -0x40(%rbp), %rdx
movq 0x38(%rdx), %rax
testq %rax, %rax
movl -0x30(%rbp), %ecx
je 0xe667
movq 0x38(%rax), %rdx
incl %ecx
cmpl $0x3, %ecx
movl %ecx, %eax
jne 0xe2d3
movq -0x38(%rbp), %rax
movq 0x38(%rax), %r12
movl $0x1, %r13d
movq 0x30(%r12), %rbx
testq %rbx, %rbx
je 0xe73a
movq 0x30(%rbx), %r14
cmpq %r14, 0x40(%r14)
je 0xe729
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
callq 0xc2da
movq %rax, %r15
movq 0x38(%r14), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rsi
movq %r15, %rdi
movl -0x2c(%rbp), %edx
callq 0xd73b
movq 0x90(%r15), %rcx
testq %rcx, %rcx
sete %al
je 0xe6e3
movq (%rcx), %rdx
cmpl $0x0, 0x20(%rdx)
je 0xe6e3
movq 0x8(%rcx), %rcx
jmp 0xe6cc
testb %al, %al
jne 0xe711
xorl %eax, %eax
callq 0xf234
movq (%r15), %rsi
leaq 0x5831(%rip), %rdi      # 0x13f29
xorl %eax, %eax
callq 0x10b0
movq %r15, %rdi
callq 0xd513
movl $0xa, %edi
callq 0x1040
movl $0x1, 0x100(%r15)
movq %r15, %rdi
movl -0x2c(%rbp), %esi
callq 0xc582
jmp 0xe731
movq $0x0, 0x40(%r14)
movq 0x38(%rbx), %rbx
jmp 0xe687
movq 0x38(%r12), %r15
testq %r15, %r15
je 0xe804
movq 0x238d9(%rip), %r14     # 0x32028
movl $0x118, %edi            # imm = 0x118
callq 0x1120
movq %rax, %rbx
leaq 0x3fb1(%rip), %rax      # 0x12714
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(%r15), %rsi
movq %rbx, %rdi
movl -0x2c(%rbp), %r14d
movl %r14d, %edx
callq 0xd73b
movq %rbx, %rdi
movl %r14d, %esi
callq 0xc582
movq 0x38(%r12), %rax
movq 0x38(%rax), %r12
incl %r13d
cmpl $0x3, %r13d
jne 0xe682
movq 0x23810(%rip), %rax     # 0x32028
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop