// File: /workspace/github_c_makefile_star2_O2/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
callq 0xa82a
movq %rax, 0x237ca(%rip)     # 0x30018
movq 0x30(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq 0x30(%rax), %rbx
pushq $0x40
popq %r14
leaq 0x54db(%rip), %r15      # 0x11d40
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xc8d7
movq 0x8(%rbx), %rax
movq 0x8(%rax), %r13
movq %r13, %rdi
callq 0xaabd
testq %rax, %rax
je 0xc8b4
movq %rax, %r12
xorl %eax, %eax
callq 0xd671
movq 0x8(%rbx), %rax
movl 0x10(%rax), %esi
movq (%r12), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x10b0
movq 0x23770(%rip), %rax     # 0x30018
movl $0x1, 0xd8(%rax)
jmp 0xc8d1
movq 0x2375d(%rip), %rsi     # 0x30018
movq %r13, %rdi
pushq $0x5
popq %rdx
movl %r14d, %ecx
callq 0xa95c
movq %rax, %rdi
callq 0xa9dc
addq $0x38, %rbx
jmp 0xc865
movq -0x50(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x38(%rbp)
pushq $0x1
popq %rax
leaq 0x5496(%rip), %r14      # 0x11d83
cmpl $0x3, %eax
je 0xcbd6
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
leaq 0x30(%rax), %r15
movq (%r15), %r15
testq %r15, %r15
je 0xcbb7
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0xab11
testq %rax, %rax
je 0xc968
xorl %eax, %eax
callq 0xd671
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %esi
movq 0x8(%rax), %rdx
leaq 0x55aa(%rip), %rdi      # 0x11eed
xorl %eax, %eax
callq 0x10b0
movq 0x236c7(%rip), %rax     # 0x30018
movl $0x1, 0xd8(%rax)
movq 0x30(%r15), %rax
movq %rax, 0x40(%rax)
jmp 0xcbae
movq 0x236a9(%rip), %rdi     # 0x30018
movq %rbx, %rsi
pushq $0x10
popq %rdx
callq 0xa86e
movq %rax, %rbx
movq %r15, -0x40(%rbp)
movq 0x30(%r15), %rax
movq 0x38(%rax), %r12
leaq 0x30(%r12), %r15
movq (%r15), %r15
testq %r15, %r15
je 0xca97
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xabd6
testq %rax, %rax
je 0xc9ef
xorl %eax, %eax
callq 0xd671
movq (%rbx), %rsi
movq 0x30(%r15), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
movq %r14, %rdi
xorl %eax, %eax
callq 0x10b0
movq 0x23638(%rip), %rax     # 0x30018
movl $0x1, 0xd8(%rax)
jmp 0xca8e
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0xffffd8f1, %ecx       # imm = 0xFFFFD8F1
callq 0xa95c
movq %rax, %r13
movq 0x30(%r15), %rax
movq 0x38(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0xca4d
movl $0x3b, 0x8(%r13)
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%r13)
movq %r13, %rdi
callq 0xb793
testl %eax, %eax
jle 0xca7d
movl 0x10c(%rbx), %ecx
movl %eax, %edx
addl %ecx, %edx
negl %ecx
movl %ecx, 0x1c(%r13)
movl %eax, 0x18(%r13)
movl %edx, 0x10c(%rbx)
jmp 0xca75
movq 0x18(%rax), %rax
movl (%rax), %eax
movl %eax, 0x8(%r13)
movq %r13, %rdi
callq 0xb793
movl 0x10c(%rbx), %ecx
movl %ecx, %edx
negl %edx
movl %edx, 0x1c(%r13)
addl %eax, %ecx
movl %ecx, 0x10c(%rbx)
addl %eax, 0x104(%rbx)
jmp 0xca81
movl %eax, 0x1c(%r13)
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xaa65
addq $0x38, %r15
jmp 0xc98e
movq 0x38(%r12), %r14
addq $0x30, %r14
movq (%r14), %r14
testq %r14, %r14
je 0xcb94
movq 0x30(%r14), %r12
movq 0x8(%r12), %rax
movq 0x8(%rax), %r13
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xab6f
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0xab6f
testq %r15, %r15
je 0xcafe
xorl %eax, %eax
callq 0xd671
movq (%rbx), %rsi
movq 0x30(%r14), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x52fd(%rip), %rdi      # 0x11df9
jmp 0xcb23
testq %rax, %rax
je 0xcb44
xorl %eax, %eax
callq 0xd671
movq (%rbx), %rsi
movq 0x30(%r14), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq 0x8(%rax), %rcx
leaq 0x5350(%rip), %rdi      # 0x11e73
xorl %eax, %eax
callq 0x10b0
movq 0x234e7(%rip), %rax     # 0x30018
movl $0x1, 0xd8(%rax)
addq $0x38, %r14
jmp 0xcaa0
movq 0x38(%r12), %rax
movl (%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
pushq $0x2
popq %rdx
callq 0xa95c
movq %rax, %r15
movq %rax, %rdi
callq 0xb793
movl 0x10c(%rbx), %ecx
movl %ecx, %edx
negl %edx
movl %edx, 0x1c(%r15)
movl %eax, 0x18(%r15)
addl %eax, %ecx
movl %ecx, 0x10c(%rbx)
addl %eax, 0x108(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0xaa65
jmp 0xcb3b
andl $0x0, 0x10c(%rbx)
movq %rbx, %rdi
callq 0xa99c
leaq 0x51d9(%rip), %r14      # 0x11d83
movq -0x40(%rbp), %r15
addq $0x38, %r15
jmp 0xc901
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
testq %rax, %rax
je 0xcbcc
movq 0x38(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x44(%rbp), %eax
incl %eax
jmp 0xc8ed
movq -0x50(%rbp), %rax
movq 0x38(%rax), %r13
pushq $0x1
popq %rax
pushq $0xa
popq %r14
cmpl $0x3, %eax
je 0xcced
movq %r13, -0x40(%rbp)
addq $0x30, %r13
movl %eax, %ebx
movq (%r13), %r13
testq %r13, %r13
je 0xcc9a
movq 0x30(%r13), %r12
cmpq %r12, 0x40(%r12)
je 0xcc8b
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
callq 0xab11
movq %rax, %r15
movq 0x38(%r12), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rsi
movq %r15, %rdi
movl -0x2c(%rbp), %r12d
movl %r12d, %edx
callq 0xbe88
movq %r15, %rdi
callq 0xbca4
testl %eax, %eax
jne 0xcc71
xorl %eax, %eax
callq 0xd68e
movq (%r15), %rsi
leaq 0x52cf(%rip), %rdi      # 0x11f29
xorl %eax, %eax
callq 0x10b0
movq %r15, %rdi
callq 0xbcce
movl %r14d, %edi
callq 0x1040
movl $0x1, 0x100(%r15)
movq %r15, %rdi
movl %r12d, %esi
callq 0xad90
movl %ebx, %eax
jmp 0xcc91
andq $0x0, 0x40(%r12)
addq $0x38, %r13
jmp 0xcbf8
movq -0x40(%rbp), %r13
movq 0x38(%r13), %r12
testq %r12, %r12
je 0xcce6
callq 0xa8ef
movq %rax, %r15
movq 0x23362(%rip), %rax     # 0x30018
movq %r15, 0xd0(%rax)
movq 0x30(%r12), %rsi
movq %r15, %rdi
movl -0x2c(%rbp), %r12d
movl %r12d, %edx
callq 0xbe88
movq %r15, %rdi
movl %r12d, %esi
callq 0xad90
movq 0x38(%r13), %rax
movq 0x38(%rax), %r13
movl %ebx, %eax
incl %eax
jmp 0xcbe5
movq 0x23324(%rip), %rax     # 0x30018
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop