// File: /workspace/github_cpp_makefile_star2_O2/AndrewAlbert[P]Compiler-Theory/SourceCode/codeGenerator.cpp
// Function: codeGenerator::mm2reg[abi:cxx11](int, int, int, bool, int, bool, int, bool)
string codeGenerator::mm2reg(int memType, int memSize, int FPoffset, bool isGlobal, int index, bool indirect, int indirect_type, bool useSP){
	if( !ShouldGenerate() ) return "";
	string reg, mem, pointer;
	
	// Set comment to indicate if this is Global/Local MM to Reg
	if( isGlobal ) comment("Global MM to Reg");
	else comment("Local MM to Reg");

	// Get either the FP or SP register based on 
	if(useSP) pointer = "SP_reg";
	else pointer = "FP_reg";

	// Ensure source size is at least 1 for loops
	if( memSize == 0 ) memSize++;

	// Indirect off of the top register
	if( indirect ){
		string indirect_reg = popStack('E', indirect_type);
		comment("indirect off " + indirect_reg);
		fprintf( oFile, "   TP_reg = %s + %d;\n", pointer.c_str(), FPoffset );
		
		reg = newRegister(memType);
		pushStack(reg, 'E', memType);
		fprintf( oFile, "   %s = MM[TP_reg + %s];\n", reg.c_str(), indirect_reg.c_str() );
	} 
	else{
		if( index < 0){
			for( index = 0; index < memSize; index++ ){
				reg = newRegister(memType);
				pushStack(reg, 'E', memType);
			
				if( isGlobal ) mem = "MM[" + to_string(FPoffset + index) + "]";
				else mem = "MM[" + pointer + " + " + to_string(FPoffset + index) + "]";
			
				if( memType == TYPE_FLOAT) fprintf( oFile, "   memcpy( &%s, &%s, sizeof(int) );\n", reg.c_str(), mem.c_str() );
				else fprintf( oFile, "   %s = %s;\n", reg.c_str(), mem.c_str() );
			}
		}
		else{
			reg = newRegister(memType);
			pushStack(reg, 'E', memType);
		
			if( isGlobal ) mem = "MM[" + to_string(FPoffset + index) + "]";
			else mem = "MM[" + pointer + " + " + to_string(FPoffset + index) + "]";
		
			if( memType == TYPE_FLOAT) fprintf( oFile, "   memcpy( &%s, &%s, sizeof(int) );\n", reg.c_str(), mem.c_str() );
			else fprintf( oFile, "   %s = %s;\n", reg.c_str(), mem.c_str() );
		}
	}
	return "";
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movl %r8d, 0x4(%rsp)
movl %edx, (%rsp)
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
cmpb $0x0, 0x330(%rsi)
je 0xdc66
movl %r9d, %ebp
movl %ecx, %r13d
movq %rbx, 0x78(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0xb0(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
leaq 0x118(%rsp), %rsi
movq %rsi, -0x10(%rsi)
andq $0x0, -0x8(%rsi)
movb %cl, (%rax)
movb %cl, (%rdx)
movb %cl, (%rsi)
testb %bpl, %bpl
je 0xdc82
leaq 0x5290(%rip), %rsi      # 0x12eda
leaq 0x1c8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x36b8
leaq 0x1c8(%rsp), %rbx
jmp 0xdca3
leaq 0x4cd0(%rip), %rsi      # 0x1293d
leaq 0x80(%rsp), %rdx
movq %rbx, %rdi
callq 0x36b8
jmp 0xe216
leaq 0x5262(%rip), %rsi      # 0x12eeb
leaq 0x1a8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x36b8
leaq 0x1a8(%rsp), %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xbf98
movq %rbx, %rdi
callq 0x20f0
leaq 0x4cf0(%rip), %rax      # 0x129b1
leaq 0x4ce2(%rip), %rsi      # 0x129aa
cmpb $0x0, 0x238(%rsp)
cmovneq %rax, %rsi
leaq 0x108(%rsp), %rdi
callq 0x2370
cmpl $0x1, %r13d
adcl $0x0, %r13d
cmpb $0x0, 0x228(%rsp)
je 0xddff
movl 0x230(%rsp), %ecx
leaq 0x58(%rsp), %rdi
pushq $0x45
popq %rdx
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0xc92c
leaq 0x51e1(%rip), %rsi      # 0x12efb
leaq 0x188(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x970c
leaq 0x188(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xbf98
movq %rbx, %rdi
callq 0x20f0
movq 0x308(%r14), %rdi
movq 0x108(%rsp), %rdx
leaq 0x51aa(%rip), %rsi      # 0x12f09
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x2280
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
movl (%rsp), %edx
callq 0xd4fe
leaq 0x80(%rsp), %rbx
leaq 0x38(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2240
movq %r15, %rdi
callq 0x20f0
leaq 0x148(%rsp), %rdi
movq %rbx, %rsi
callq 0x2270
leaq 0x148(%rsp), %rsi
pushq $0x45
popq %rdx
movq %r14, %rdi
movl (%rsp), %ecx
callq 0xc8a8
leaq 0x148(%rsp), %rdi
callq 0x20f0
movq 0x308(%r14), %rdi
movq 0x80(%rsp), %rdx
leaq 0x58(%rsp), %rbx
movq (%rbx), %rcx
leaq 0x5134(%rip), %rsi      # 0x12f1f
xorl %eax, %eax
callq 0x2280
movq %rbx, %rdi
callq 0x20f0
jmp 0xe1d4
movl 0x220(%rsp), %ebx
testl %ebx, %ebx
js 0xdef0
leaq 0x58(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl (%rsp), %edx
callq 0xd4fe
leaq 0x80(%rsp), %r12
leaq 0x58(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x2240
movq %r13, %rdi
callq 0x20f0
leaq 0x128(%rsp), %rdi
movq %r12, %rsi
callq 0x2270
leaq 0x128(%rsp), %rsi
pushq $0x45
popq %rdx
movq 0x8(%rsp), %rdi
movl (%rsp), %ecx
callq 0xc8a8
leaq 0x128(%rsp), %rdi
callq 0x20f0
testb %bpl, %bpl
je 0xe0d8
addl 0x4(%rsp), %ebx
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x96b3
movq 0x8(%rsp), %r14
leaq 0x509f(%rip), %rsi      # 0x12f39
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x968f
leaq 0x3216(%rip), %rdx      # 0x110c6
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x9670
leaq 0xa0(%rsp), %rdi
leaq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0x2240
movq %rbx, %rdi
callq 0x20f0
leaq 0x38(%rsp), %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
jmp 0xe198
movl %ebp, 0x14(%rsp)
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
cmpl $0x10b, (%rsp)          # imm = 0x10B
leaq 0x5036(%rip), %rax      # 0x12f41
leaq 0x4f4e(%rip), %rcx      # 0x12e60
cmoveq %rax, %rcx
movq %rcx, 0xc0(%rsp)
leaq 0x58(%rsp), %r12
leaq 0x168(%rsp), %rbx
movq 0x8(%rsp), %rbp
subl $0x1, %r13d
jb 0xe1d4
movq %r12, %rdi
movq %rbp, %rsi
movl (%rsp), %edx
callq 0xd4fe
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2240
movq %r12, %rdi
callq 0x20f0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2270
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
pushq $0x45
popq %rdx
movl (%rsp), %ecx
callq 0xc8a8
movq %rbx, %rdi
callq 0x20f0
cmpb $0x0, 0x14(%rsp)
je 0xdff7
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x96b3
leaq 0x38(%rsp), %r15
movq %r15, %rdi
leaq 0x4f86(%rip), %rsi      # 0x12f39
movq %r14, %rdx
callq 0x968f
movq %r12, %rdi
movq %r15, %rsi
leaq 0x30fe(%rip), %rdx      # 0x110c6
callq 0x9670
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x2240
movq %r12, %rdi
callq 0x20f0
movq %r15, %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
jmp 0xe0a4
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
leaq 0x4f30(%rip), %rsi      # 0x12f39
leaq 0x108(%rsp), %rdx
callq 0x970c
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
leaq 0x4f18(%rip), %rdx      # 0x12f3d
callq 0x9670
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
movl 0x4(%rsp), %esi
callq 0x96b3
leaq 0x38(%rsp), %r14
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x9871
movq %r12, %rdi
movq %r14, %rsi
leaq 0x3066(%rip), %rdx      # 0x110c6
callq 0x9670
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x2240
movq %r12, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x20f0
leaq 0xc8(%rsp), %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
callq 0x20f0
leaq 0xe8(%rsp), %rdi
callq 0x20f0
movq 0x308(%rbp), %rdi
movq 0x80(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0xc0(%rsp), %rsi
xorl %eax, %eax
callq 0x2280
incl 0x4(%rsp)
jmp 0xdf30
leaq 0x4e5a(%rip), %rsi      # 0x12f39
leaq 0xe8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x970c
movq 0x8(%rsp), %r14
leaq 0x4e3d(%rip), %rdx      # 0x12f3d
leaq 0x18(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x9670
addl 0x4(%rsp), %ebx
leaq 0xc8(%rsp), %rdi
movl %ebx, %esi
callq 0x96b3
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x9871
leaq 0x2f83(%rip), %rdx      # 0x110c6
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x9670
leaq 0xa0(%rsp), %rdi
leaq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0x2240
movq %rbx, %rdi
callq 0x20f0
leaq 0x38(%rsp), %rdi
callq 0x20f0
leaq 0xc8(%rsp), %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
callq 0x20f0
leaq 0xe8(%rsp), %rdi
callq 0x20f0
movq 0x308(%r14), %rdi
movq 0x80(%rsp), %rdx
movq 0xa0(%rsp), %rcx
cmpl $0x10b, (%rsp)          # imm = 0x10B
jne 0xe1c6
leaq 0x4d7d(%rip), %rsi      # 0x12f41
jmp 0xe1cd
leaq 0x4c93(%rip), %rsi      # 0x12e60
xorl %eax, %eax
callq 0x2280
leaq 0x4762(%rip), %rsi      # 0x1293d
leaq 0x58(%rsp), %rdx
movq 0x78(%rsp), %rdi
callq 0x36b8
leaq 0x108(%rsp), %rdi
callq 0x20f0
leaq 0xa0(%rsp), %rdi
callq 0x20f0
leaq 0x80(%rsp), %rdi
callq 0x20f0
movq 0x78(%rsp), %rbx
movq %rbx, %rax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xe288
jmp 0xe2a1
jmp 0xe283
jmp 0xe2c7
jmp 0xe29c
jmp 0xe297
jmp 0xe2dd
jmp 0xe2dd
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0xe2ed
jmp 0xe2dd
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x20f0
jmp 0xe278
jmp 0xe275
jmp 0xe2dd
jmp 0xe2dd
jmp 0xe2dd
jmp 0xe2dd
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0xe2ed
jmp 0xe2dd
jmp 0xe2dd
movq %rax, %rbx
jmp 0xe2b1
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20f0
jmp 0xe2a4
movq %rax, %rbx
jmp 0xe2d4
movq %rax, %rbx
jmp 0xe2bb
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
callq 0x20f0
leaq 0xe8(%rsp), %rdi
jmp 0xe2ed
jmp 0xe2dd
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20f0
leaq 0x18(%rsp), %rdi
jmp 0xe2ed
jmp 0xe2dd
movq %rax, %rbx
jmp 0xe2f2
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x20f0
leaq 0x108(%rsp), %rdi
callq 0x20f0
leaq 0xa0(%rsp), %rdi
callq 0x20f0
leaq 0x80(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2310
nop