// File: /workspace/github_cpp_makefile_star2_O2/RuiDGPires[P]CrimLang/src/generator.cpp
// Function: gc_Tracker::init(crl::Node*)
void _gc_Tracker::init(crl::Node *node){
	u32 size = 0;
	ASSERT(node->get_child(1)->type == crl::Node::Type::LEAF, "Var declaration must have a name as second token");
	std::string name = get_token(node->get_child(1)).str;

	if (node->get_child(0)->type == crl::Node::Type::VECTOR){
		crl::Node *vec = node->get_child(0);
		ASSERT(vec->get_child(1)->type == crl::Node::Type::LEAF, "Vector needs to have it's size as second child");

		// Vectors will be laid on the stack like so: [1st element, 2nd, 3rd, ..., last element, size]
		// When roll-backing the SF, one just needs to peek at the top of the stack
		// When assigning values, the var location will be the first element	
		
		u32 element_size = size_of(get_token(vec->get_child(0)).str);
		size = atoi(get_token(vec->get_child(1)).str.c_str())*element_size;

		symb_tracker.local_create(name, size + 1);

		this->stream_funcs <<
			"; Advance the stack (variable is unitialized)\n" << 
			"MVI R1 " << size << "\n" <<
			"SUB RS R1\n" <<
			"PSH R1\n";

		return;
	}

	// ELSE....
	ASSERT(node->get_child(0)->type == crl::Node::Type::LEAF, "Var declaration must start with type");
	std::string type = get_token(node->get_child(0)).str;
	size = size_of(type);
	
	symb_tracker.local_create(name, size);

	if (node->children.size() == 3){
		ASSERT(node->get_child(2)->type == crl::Node::Type::ASSIGN, "Third child must be assign");
		this->expression(node->get_child(2)->get_child(0), this->stream_funcs);
		// The result of the expression will be stored on the stack, that is our variable
	}else
		this->stream_funcs << 
			"; Advance the stack (variable is unitialized)\n" << 
			"MVI R1 " << size << "\n" <<
			"SUB RS R1\n";
	
	this->stream_funcs << "\n";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rax
pushq $0x1
popq %rsi
movq %r14, %rdi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0x91b8
movq (%r14), %rax
pushq $0x1
popq %rsi
movq %r14, %rdi
callq *0x18(%rax)
leaq 0x58(%rax), %rsi
leaq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0xb24a
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%r15), %rcx
leaq -0x40(%rbp), %r12
cmpq %r12, %rcx
je 0x8e58
movq %rcx, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x8e60
movups (%r12), %xmm0
movups %xmm0, (%rax)
leaq -0x50(%rbp), %rdi
movq 0x8(%rdi), %rax
movq %rax, -0x68(%rbp)
movq %r12, (%rdi)
andq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
callq 0x135f8
movq (%r14), %rax
movq %r14, %rdi
xorl %esi, %esi
callq *0x18(%rax)
cmpl $0x19, 0x8(%rax)
movq (%r14), %rax
movq 0x18(%rax), %rax
jne 0x8eff
movq %r14, %rdi
xorl %esi, %esi
callq *%rax
movq %rax, %r14
movq (%rax), %rax
pushq $0x1
popq %rsi
movq %r14, %rdi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0x9266
movq (%r14), %rax
movq %r14, %rdi
xorl %esi, %esi
callq *0x18(%rax)
addq $0x58, %rax
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0xb24a
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
movq -0x50(%rbp), %rcx
cmpq %r12, %rcx
je 0x9096
movq %rcx, -0xb0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x909e
movq %r14, %rdi
xorl %esi, %esi
callq *%rax
cmpl $0x10, 0x8(%rax)
jne 0x920f
movq (%r14), %rax
movq %r14, %rdi
xorl %esi, %esi
callq *0x18(%rax)
addq $0x58, %rax
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0xb24a
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
movq -0x50(%rbp), %rcx
cmpq %r12, %rcx
je 0x8f4d
movq %rcx, -0x90(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x8f55
movups (%r12), %xmm0
movups %xmm0, (%rax)
leaq -0x50(%rbp), %rdi
movq 0x8(%rdi), %rax
leaq -0x90(%rbp), %r15
movq %rax, 0x8(%r15)
movq %r12, (%rdi)
andq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
callq 0x135f8
leaq -0xf0(%rbp), %rdi
movq %r15, %rsi
callq 0x32f0
leaq -0xf0(%rbp), %rdi
callq 0x7909
movl %eax, %r15d
leaq -0xf0(%rbp), %rdi
callq 0x3110
leaq -0xd0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x32f0
leaq 0x530(%rbx), %rdi
leaq -0xd0(%rbp), %rsi
movl %r15d, %edx
callq 0xae64
leaq -0xd0(%rbp), %rdi
callq 0x3110
movq 0x18(%r14), %rax
subq 0x10(%r14), %rax
cmpq $0x18, %rax
jne 0x9024
movq (%r14), %rax
pushq $0x2
popq %rsi
movq %r14, %rdi
callq *0x18(%rax)
cmpl $0x6, 0x8(%rax)
jne 0x92ba
movq (%r14), %rax
pushq $0x2
popq %rsi
movq %r14, %rdi
callq *0x18(%rax)
movq (%rax), %rcx
movq %rax, %rdi
xorl %esi, %esi
callq *0x18(%rcx)
leaq 0x318(%rbx), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x73e0
jmp 0x906f
leaq 0x328(%rbx), %rdi
leaq 0xc547(%rip), %rsi      # 0x15579
callq 0x3230
leaq 0xc33f(%rip), %rsi      # 0x1537d
movq %rax, %rdi
callq 0x3230
movl %r15d, %esi
movq %rax, %rdi
callq 0x3180
leaq 0xc48b(%rip), %rsi      # 0x154e3
movq %rax, %rdi
callq 0x3230
leaq 0xc44b(%rip), %rsi      # 0x154b2
movq %rax, %rdi
callq 0x3230
addq $0x328, %rbx            # imm = 0x328
leaq 0xc466(%rip), %rsi      # 0x154e3
movq %rbx, %rdi
callq 0x3230
leaq -0x90(%rbp), %rdi
callq 0x3110
jmp 0x919f
movups (%r12), %xmm0
movups %xmm0, (%rax)
movq -0x48(%rbp), %rax
leaq -0xb0(%rbp), %rdi
movq %rax, 0x8(%rdi)
movq %r12, -0x50(%rbp)
andq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
callq 0x7909
movl %eax, %r15d
leaq -0xb0(%rbp), %rdi
callq 0x3110
leaq -0x50(%rbp), %rdi
callq 0x135f8
movq (%r14), %rax
pushq $0x1
popq %rsi
movq %r14, %rdi
callq *0x18(%rax)
addq $0x58, %rax
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0xb24a
leaq -0x50(%rbp), %r12
movq (%r12), %rdi
callq 0x3350
movl %eax, %r14d
movq %r12, %rdi
callq 0x135f8
leaq -0x110(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x32f0
imull %r15d, %r14d
leaq 0x530(%rbx), %rdi
leal 0x1(%r14), %edx
leaq -0x110(%rbp), %rsi
callq 0xae64
leaq -0x110(%rbp), %rdi
callq 0x3110
addq $0x328, %rbx            # imm = 0x328
leaq 0xc429(%rip), %rsi      # 0x15579
movq %rbx, %rdi
callq 0x3230
leaq 0xc21e(%rip), %rsi      # 0x1537d
movq %rax, %rdi
callq 0x3230
movl %r14d, %esi
movq %rax, %rdi
callq 0x3180
leaq 0xc36a(%rip), %rsi      # 0x154e3
movq %rax, %rdi
callq 0x3230
leaq 0xc32a(%rip), %rsi      # 0x154b2
movq %rax, %rdi
callq 0x3230
leaq 0xc3ab(%rip), %rsi      # 0x15542
movq %rax, %rdi
callq 0x3230
leaq -0x70(%rbp), %rdi
callq 0x3110
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x50
popq %rdi
callq 0x3140
movq %rax, %r14
leaq 0xc154(%rip), %rsi      # 0x1531e
leaq -0x190(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x3be4
movb $0x1, %r15b
pushq $-0x1
popq %rdx
leaq -0x190(%rbp), %rcx
movq %r14, %rdi
movl %edx, %esi
callq 0x4e88
xorl %r15d, %r15d
leaq 0x14b95(%rip), %rsi     # 0x1dd90
leaq -0x42ca(%rip), %rdx     # 0x4f38
movq %r14, %rdi
callq 0x33f0
jmp 0x930c
pushq $0x50
popq %rdi
callq 0x3140
movq %rax, %r14
leaq 0xc0d8(%rip), %rsi      # 0x152f9
leaq -0x150(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x3be4
movb $0x1, %r15b
pushq $-0x1
popq %rdx
leaq -0x150(%rbp), %rcx
movq %r14, %rdi
movl %edx, %esi
callq 0x4e88
xorl %r15d, %r15d
leaq 0x14b3e(%rip), %rsi     # 0x1dd90
leaq -0x4321(%rip), %rdx     # 0x4f38
movq %r14, %rdi
callq 0x33f0
jmp 0x930c
pushq $0x50
popq %rdi
callq 0x3140
movq %rax, %r14
leaq 0xc2d2(%rip), %rsi      # 0x1554a
leaq -0x170(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x3be4
movb $0x1, %r15b
pushq $-0x1
popq %rdx
leaq -0x170(%rbp), %rcx
movq %r14, %rdi
movl %edx, %esi
callq 0x4e88
xorl %r15d, %r15d
leaq 0x14ae7(%rip), %rsi     # 0x1dd90
leaq -0x4378(%rip), %rdx     # 0x4f38
movq %r14, %rdi
callq 0x33f0
jmp 0x930c
pushq $0x50
popq %rdi
callq 0x3140
movq %rax, %r14
leaq 0xc08c(%rip), %rsi      # 0x15358
leaq -0x130(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x3be4
movb $0x1, %r15b
pushq $-0x1
popq %rdx
leaq -0x130(%rbp), %rcx
movq %r14, %rdi
movl %edx, %esi
callq 0x4e88
xorl %r15d, %r15d
leaq 0x14a93(%rip), %rsi     # 0x1dd90
leaq -0x43cc(%rip), %rdx     # 0x4f38
movq %r14, %rdi
callq 0x33f0
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x3110
testb %r15b, %r15b
jne 0x9328
jmp 0x93e4
movq %rax, %rbx
movq %r14, %rdi
callq 0x31b0
jmp 0x93e4
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
jmp 0x934d
jmp 0x935c
movq %rax, %rbx
leaq -0x150(%rbp), %rdi
callq 0x3110
testb %r15b, %r15b
jne 0x935f
jmp 0x93f0
movq %rax, %rbx
movq %r14, %rdi
callq 0x31b0
jmp 0x93f0
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
jmp 0x93eb
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x93a8
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3110
leaq -0x50(%rbp), %rdi
callq 0x135f8
jmp 0x93f0
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x3110
jmp 0x93e4
movq %rax, %rbx
leaq -0x190(%rbp), %rdi
callq 0x3110
testb %r15b, %r15b
jne 0x93c8
jmp 0x93f9
movq %rax, %rbx
movq %r14, %rdi
callq 0x31b0
jmp 0x93f9
jmp 0x93dc
jmp 0x93dc
jmp 0x93dc
jmp 0x93dc
jmp 0x93dc
movq %rax, %rbx
jmp 0x93f0
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x3110
leaq -0x70(%rbp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3410
nop