// File: /workspace/github_c_makefile_star2_O3/anuwu[P]ERPLAG-Compiler/typeChecker.c
// Function: assignmentTypeCheck
void assignmentTypeCheck (moduleST *baseModule, astNode *assignopASTNode)
{
	varST *searchedVarLeft, *searchedVarRight ;
	tokenID typeLeft, typeRight ;

	if (assignopASTNode->child->next->child == NULL && assignopASTNode->child->next->id == TK_ID) // Simple assignment with RHS also as ID
	{	
		typeLeft = validateVar (baseModule, assignopASTNode->child, &searchedVarLeft) ;
		typeRight = validateVar (baseModule, assignopASTNode->child->next, &searchedVarRight) ;

		if (typeLeft == TK_ARRAY && typeRight == TK_ARRAY) 	// Array assignment
		{
			if (searchedVarLeft->arrayIndices->type == searchedVarRight->arrayIndices->type)
			{
				if (isVarStaticArr (searchedVarLeft) && isVarStaticArr (searchedVarRight))
				{
					int leftLim1, rightLim1, leftLim2, rightLim2 ;	
					leftLim1 = atoi (searchedVarLeft->arrayIndices->tokLeft->lexeme) ;
					rightLim1 = atoi (searchedVarLeft->arrayIndices->tokRight->lexeme) ;
					leftLim2 = atoi (searchedVarRight->arrayIndices->tokLeft->lexeme) ;
					rightLim2 = atoi (searchedVarRight->arrayIndices->tokRight->lexeme) ;

					if (leftLim1 != leftLim2 || rightLim1 != rightLim2)
					{
						errSemantic () ;
						#ifdef _WIN64
							printf ("In module \"%s\" at line %d, array variables \"%s\" and \"%s\" do not have matching bounds for assignment\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
						#endif
						#if defined __linux__ || defined __MACH__
							printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variables " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " and " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " do not have matching bounds for assignment\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
						#endif
						realBase->semanticError = 1 ;
					}
				}
			}
			else
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, array variables \"%s\" and \"%s\" do not have the same base type\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variables " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " and " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " do not have the same base type\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
			}
		}
		else if (typeLeft != 0 && typeRight != 0)
		{
			if (typeLeft != TK_ARRAY && typeRight == TK_ARRAY)
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, array variable \"%s\" cannot be assigned to primitive variable \"%s\"\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be assigned to " ANSI_BOLD ANSI_BLACK "primitive" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET "\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
			}
			else if (typeRight != TK_ARRAY && typeLeft == TK_ARRAY)
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, primitive variable \"%s\" cannot be assigned to array variable \"%s\"\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "primitive" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be assigned to " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET "\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				realBase->semanticError = 1 ;

			}
			else if (typeLeft != typeRight)
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, assignment of \"%s\" to \"%s\" are in type conflict\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", assignment of " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " to " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " are in type conflict\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
			}

			tinkerVar (baseModule, searchedVarLeft, assignopASTNode->child) ;
		}

		return ;
	}
	else if (assignopASTNode->child->next->child != NULL && assignopASTNode->child->next->child->next == NULL && (assignopASTNode->child->next->id == TK_PLUS || assignopASTNode->child->next->id == TK_MINUS)) // <U>
		typeRight = getExpressionType (baseModule, assignopASTNode->child->next->child) ;
	else	// expression_new
		typeRight = getExpressionType (baseModule, assignopASTNode->child->next) ;

	/* ------------------------------------------------------------------------------------------------------------------*/

	typeLeft = validateVar (baseModule, assignopASTNode->child, &searchedVarLeft) ;
	if (!typeLeft)
		return ;
	else
	{ 
		tinkerVar (baseModule, searchedVarLeft, assignopASTNode->child) ;
		if (typeLeft == TK_ARRAY)
		{
			errSemantic () ;
			#ifdef _WIN64
				printf ("In module \"%s\" at line %d, the assignment of an expression to array variable \"%s\" cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, assignopASTNode->child->tok->lexeme) ;
			#endif
			#if defined __linux__ || defined __MACH__
				printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", the assignment of an expression to " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, assignopASTNode->child->tok->lexeme) ;
			#endif
			realBase->semanticError = 1 ;
			return ;
		}
	}

	if (!typeRight)
		return ;
	else if (typeLeft != typeRight)
	{
		errSemantic () ;
		#ifdef _WIN64
			printf ("In module \"%s\" at line %d, assignment of expression of type \"%s\" to the variable \"%s\" of type \"%s\" cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, typeIDToString(typeRight), assignopASTNode->child->tok->lexeme, typeIDToString(typeLeft)) ;
		#endif
		#if defined __linux__ || defined __MACH__
			printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", assignment of expression of type " ANSI_BOLD ANSI_GREEN "%s" ANSI_RESET " to the variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " of type " ANSI_BOLD ANSI_GREEN "%s" ANSI_RESET " cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, typeIDToString(typeRight), assignopASTNode->child->tok->lexeme, typeIDToString(typeLeft)) ;
		#endif
		realBase->semanticError = 1 ;
		return ;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x30(%rsi), %rax
movq 0x38(%rax), %rcx
movq 0x30(%rcx), %rsi
testq %rsi, %rsi
je 0xe5c9
cmpq $0x0, 0x38(%rsi)
jne 0xe6c0
movl (%rcx), %eax
addl $-0x57, %eax
cmpl $0x2, %eax
jae 0xe6c0
jmp 0xe6c3
cmpl $0x40, (%rcx)
jne 0xe6c0
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0xe1a5
movl %eax, %r15d
movq 0x30(%rbx), %rax
movq 0x38(%rax), %rsi
leaq -0x38(%rbp), %rdx
movq %r14, %rdi
callq 0xe1a5
cmpl $0x3b, %r15d
jne 0xe7a6
cmpl $0x3b, %eax
jne 0xe7a6
movq -0x30(%rbp), %rdi
movq 0x10(%rdi), %r12
movl (%r12), %eax
movq -0x38(%rbp), %r15
movq 0x10(%r15), %r13
cmpl (%r13), %eax
jne 0xe7fa
callq 0xe01a
testl %eax, %eax
je 0xe843
movq %r15, %rdi
callq 0xe01a
testl %eax, %eax
je 0xe843
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x1140
movl %eax, %r15d
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x1140
cmpl %eax, %r15d
jne 0xe690
movq 0x10(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x1140
movl %eax, %r15d
movq 0x10(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x1140
cmpl %r15d, %eax
je 0xe843
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rsi
movq (%rsi), %r8
leaq 0x4d44(%rip), %rdi      # 0x133ff
jmp 0xe825
movq %rcx, %rsi
movq %r14, %rdi
callq 0xe366
movl %eax, %r15d
movq 0x30(%rbx), %rsi
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
callq 0xe1a5
testl %eax, %eax
je 0xe843
movl %eax, %r12d
movq -0x30(%rbp), %rsi
movq 0x30(%rbx), %rdx
movq %r14, %rdi
callq 0xca24
cmpl $0x3b, %r12d
jne 0xe737
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq 0x8(%rbx), %rcx
movq 0x30(%rbx), %rsi
movl 0x10(%rcx), %edx
movq 0x8(%rsi), %rcx
movq 0x8(%rcx), %rcx
leaq 0x4fb8(%rip), %rdi      # 0x136e0
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
jmp 0xe82f
testl %r15d, %r15d
sete %al
cmpl %r15d, %r12d
sete %cl
orb %al, %cl
jne 0xe843
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq %rax, %r14
movq 0x8(%rbx), %rax
movl 0x10(%rax), %r13d
movl %r15d, %edi
callq 0xc9b3
movq %rax, %r15
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rbx
movl %r12d, %edi
callq 0xc9b3
leaq 0x4fe2(%rip), %rdi      # 0x1376d
movq %r14, %rsi
movl %r13d, %edx
movq %r15, %rcx
movq %rbx, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10b0
jmp 0xe82f
testl %r15d, %r15d
je 0xe843
testl %eax, %eax
je 0xe843
cmpl $0x3b, %r15d
je 0xe852
cmpl $0x3b, %eax
jne 0xe852
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x4d34(%rip), %rdi      # 0x13529
jmp 0xe8ba
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rsi
movq (%rsi), %r8
leaq 0x4c75(%rip), %rdi      # 0x1349a
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x227f2(%rip), %rax     # 0x31028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3b, %r15d
jne 0xe88a
cmpl $0x3b, %eax
je 0xe88a
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x4d42(%rip), %rdi      # 0x135ca
jmp 0xe8ba
cmpl %eax, %r15d
je 0xe8d8
xorl %eax, %eax
callq 0xe989
movq %r14, %rdi
callq 0xc0ec
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x4db1(%rip), %rdi      # 0x1366b
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x2275d(%rip), %rax     # 0x31028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq -0x30(%rbp), %rsi
movq 0x30(%rbx), %rdx
movq %r14, %rdi
callq 0xca24
jmp 0xe843
nopl (%rax)