// File: 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
subq $0x380, %rsp            # imm = 0x380
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x260(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
je 0xfd8d
movb $0x1, -0x4d(%rbp)
movl $0x50, %edi
callq 0x4190
movq %rax, -0x270(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x4590
movq -0x268(%rbp), %rdx
leaq 0x1b61d(%rip), %rsi     # 0x2b31e
leaq -0x38(%rbp), %rdi
callq 0x52e0
jmp 0xfd0c
movq -0x270(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x38(%rbp), %rcx
movl %edx, %esi
callq 0x9140
jmp 0xfd25
movq -0x270(%rbp), %rdi
movb $0x0, -0x4d(%rbp)
leaq 0x2a049(%rip), %rsi     # 0x39d80
leaq -0x6a2e(%rip), %rdx     # 0x9310
callq 0x4550
jmp 0x105f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xfd6b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x41e0
leaq -0x39(%rbp), %rdi
callq 0x4360
testb $0x1, -0x4d(%rbp)
jne 0xfd7c
jmp 0xfd88
movq -0x270(%rbp), %rdi
callq 0x4230
jmp 0x105e8
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl $0x1, %esi
callq *%rax
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0xa2e0
movq -0x280(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x41d0
movq -0x280(%rbp), %rdi
callq 0x285a0
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %esi, %esi
callq *%rax
movq %rax, -0x278(%rbp)
jmp 0xfde9
movq -0x278(%rbp), %rax
cmpl $0x19, 0x8(%rax)
jne 0x10164
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %esi, %esi
callq *%rax
movq %rax, -0x288(%rbp)
jmp 0xfe12
movq -0x288(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl $0x1, %esi
callq *%rax
movq %rax, -0x290(%rbp)
jmp 0xfe3e
movq -0x290(%rbp), %rax
cmpl $0x10, 0x8(%rax)
je 0xff37
movb $0x1, -0xca(%rbp)
movl $0x50, %edi
callq 0x4190
movq %rax, -0x2a0(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x4590
movq -0x298(%rbp), %rdx
leaq 0x1b6c2(%rip), %rsi     # 0x2b54a
leaq -0xc8(%rbp), %rdi
callq 0x52e0
jmp 0xfe96
movq -0x2a0(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0xc8(%rbp), %rcx
movl %edx, %esi
callq 0x9140
jmp 0xfeb2
movq -0x2a0(%rbp), %rdi
movb $0x0, -0xca(%rbp)
leaq 0x29eb9(%rip), %rsi     # 0x39d80
leaq -0x6bbe(%rip), %rdx     # 0x9310
callq 0x4550
jmp 0x105f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x105df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xff0f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x41e0
leaq -0xc9(%rbp), %rdi
callq 0x4360
testb $0x1, -0xca(%rbp)
jne 0xff26
jmp 0xff32
movq -0x2a0(%rbp), %rdi
callq 0x4230
jmp 0x105df
movq -0xa8(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %esi, %esi
callq *%rax
movq %rax, -0x2a8(%rbp)
jmp 0xff52
movq -0x2a8(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0xa2e0
jmp 0xff67
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
leaq -0x120(%rbp), %rsi
callq 0x41d0
movq -0x2b8(%rbp), %rdi
callq 0xdc30
movl %eax, -0x2ac(%rbp)
jmp 0xff95
leaq -0xf0(%rbp), %rdi
callq 0x41e0
leaq -0x120(%rbp), %rdi
callq 0x285a0
movl -0x2ac(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq -0xa8(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl $0x1, %esi
callq *%rax
movq %rax, -0x2c0(%rbp)
jmp 0xffd7
movq -0x2c0(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xa2e0
jmp 0xffec
leaq -0x150(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x4140
movq %rax, %rdi
callq 0x44b0
movq -0x2d0(%rbp), %rdi
movl -0xd0(%rbp), %ecx
imull %ecx, %eax
movl %eax, -0x14(%rbp)
callq 0x285a0
movq -0x260(%rbp), %rax
addq $0x530, %rax            # imm = 0x530
movq %rax, -0x2c8(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4150
jmp 0x10045
movq -0x2c8(%rbp), %rdi
movl -0x14(%rbp), %edx
incl %edx
leaq -0x170(%rbp), %rsi
callq 0x13680
jmp 0x1005f
leaq -0x170(%rbp), %rdi
callq 0x41e0
movq -0x260(%rbp), %rdi
addq $0x328, %rdi            # imm = 0x328
leaq 0x1b4f9(%rip), %rsi     # 0x2b579
callq 0x42e0
movq %rax, -0x2d8(%rbp)
jmp 0x1008e
movq -0x2d8(%rbp), %rdi
leaq 0x1b2e1(%rip), %rsi     # 0x2b37d
callq 0x42e0
movq %rax, -0x2e0(%rbp)
jmp 0x100aa
movq -0x2e0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x4420
movq %rax, -0x2e8(%rbp)
jmp 0x100c2
movq -0x2e8(%rbp), %rdi
leaq 0x1b413(%rip), %rsi     # 0x2b4e3
callq 0x42e0
movq %rax, -0x2f0(%rbp)
jmp 0x100de
movq -0x2f0(%rbp), %rdi
leaq 0x1b3c6(%rip), %rsi     # 0x2b4b2
callq 0x42e0
movq %rax, -0x2f8(%rbp)
jmp 0x100fa
movq -0x2f8(%rbp), %rdi
leaq 0x1b43a(%rip), %rsi     # 0x2b542
callq 0x42e0
jmp 0x1010f
movl $0x1, -0x174(%rbp)
jmp 0x105c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x41e0
leaq -0x120(%rbp), %rdi
callq 0x285a0
jmp 0x105df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x41e0
jmp 0x105df
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %esi, %esi
callq *%rax
movq %rax, -0x300(%rbp)
jmp 0x1017c
movq -0x300(%rbp), %rax
cmpl $0x10, 0x8(%rax)
je 0x10264
movb $0x1, -0x19a(%rbp)
movl $0x50, %edi
callq 0x4190
movq %rax, -0x310(%rbp)
leaq -0x199(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0x4590
movq -0x308(%rbp), %rdx
leaq 0x1b133(%rip), %rsi     # 0x2b2f9
leaq -0x198(%rbp), %rdi
callq 0x52e0
jmp 0x101d4
movq -0x310(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x198(%rbp), %rcx
movl %edx, %esi
callq 0x9140
jmp 0x101f0
movq -0x310(%rbp), %rdi
movb $0x0, -0x19a(%rbp)
leaq 0x29b7b(%rip), %rsi     # 0x39d80
leaq -0x6efc(%rip), %rdx     # 0x9310
callq 0x4550
jmp 0x105f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1023c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x41e0
leaq -0x199(%rbp), %rdi
callq 0x4360
testb $0x1, -0x19a(%rbp)
jne 0x10253
jmp 0x1025f
movq -0x310(%rbp), %rdi
callq 0x4230
jmp 0x105df
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %esi, %esi
callq *%rax
movq %rax, -0x318(%rbp)
jmp 0x1027c
movq -0x318(%rbp), %rsi
leaq -0x1f0(%rbp), %rdi
callq 0xa2e0
jmp 0x10291
leaq -0x1c0(%rbp), %rdi
movq %rdi, -0x320(%rbp)
leaq -0x1f0(%rbp), %rsi
movq %rsi, -0x328(%rbp)
callq 0x41d0
movq -0x328(%rbp), %rdi
callq 0x285a0
movq -0x320(%rbp), %rsi
leaq -0x210(%rbp), %rdi
callq 0x4150
jmp 0x102d3
leaq -0x210(%rbp), %rdi
callq 0xdc30
movl %eax, -0x32c(%rbp)
jmp 0x102e7
movl -0x32c(%rbp), %eax
movl %eax, -0x14(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x41e0
movq -0x260(%rbp), %rax
addq $0x530, %rax            # imm = 0x530
movq %rax, -0x338(%rbp)
leaq -0x230(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4150
jmp 0x10322
movq -0x338(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq -0x230(%rbp), %rsi
callq 0x13680
jmp 0x1033a
leaq -0x230(%rbp), %rdi
callq 0x41e0
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x51d0
cmpq $0x3, %rax
jne 0x10505
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl $0x2, %esi
callq *%rax
movq %rax, -0x340(%rbp)
jmp 0x10378
movq -0x340(%rbp), %rax
cmpl $0x6, 0x8(%rax)
je 0x104ab
movb $0x1, -0x252(%rbp)
movl $0x50, %edi
callq 0x4190
movq %rax, -0x350(%rbp)
leaq -0x251(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x4590
movq -0x348(%rbp), %rdx
leaq 0x1af96(%rip), %rsi     # 0x2b358
leaq -0x250(%rbp), %rdi
callq 0x52e0
jmp 0x103d0
movq -0x350(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x250(%rbp), %rcx
movl %edx, %esi
callq 0x9140
jmp 0x103ec
movq -0x350(%rbp), %rdi
movb $0x0, -0x252(%rbp)
leaq 0x2997f(%rip), %rsi     # 0x39d80
leaq -0x70f8(%rip), %rdx     # 0x9310
callq 0x4550
jmp 0x105f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x105d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x41e0
jmp 0x105d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x41e0
jmp 0x105d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x10483
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x41e0
leaq -0x251(%rbp), %rdi
callq 0x4360
testb $0x1, -0x252(%rbp)
jne 0x1049a
jmp 0x104a6
movq -0x350(%rbp), %rdi
callq 0x4230
jmp 0x105d3
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl $0x2, %esi
callq *%rax
movq %rax, -0x358(%rbp)
jmp 0x104c6
movq -0x358(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
xorl %esi, %esi
callq *%rax
movq %rax, -0x360(%rbp)
jmp 0x104e1
movq -0x360(%rbp), %rsi
movq -0x260(%rbp), %rdi
movq %rdi, %rdx
addq $0x318, %rdx            # imm = 0x318
callq 0xd440
jmp 0x10500
jmp 0x1058f
movq -0x260(%rbp), %rdi
addq $0x328, %rdi            # imm = 0x328
leaq 0x1b05f(%rip), %rsi     # 0x2b579
callq 0x42e0
movq %rax, -0x368(%rbp)
jmp 0x10528
movq -0x368(%rbp), %rdi
leaq 0x1ae47(%rip), %rsi     # 0x2b37d
callq 0x42e0
movq %rax, -0x370(%rbp)
jmp 0x10544
movq -0x370(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x4420
movq %rax, -0x378(%rbp)
jmp 0x1055c
movq -0x378(%rbp), %rdi
leaq 0x1af79(%rip), %rsi     # 0x2b4e3
callq 0x42e0
movq %rax, -0x380(%rbp)
jmp 0x10578
movq -0x380(%rbp), %rdi
leaq 0x1af2c(%rip), %rsi     # 0x2b4b2
callq 0x42e0
jmp 0x1058d
jmp 0x1058f
movq -0x260(%rbp), %rdi
addq $0x328, %rdi            # imm = 0x328
leaq 0x1af3f(%rip), %rsi     # 0x2b4e3
callq 0x42e0
jmp 0x105ab
leaq -0x1c0(%rbp), %rdi
callq 0x41e0
movl $0x0, -0x174(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x41e0
addq $0x380, %rsp            # imm = 0x380
popq %rbp
retq
leaq -0x1c0(%rbp), %rdi
callq 0x41e0
leaq -0x70(%rbp), %rdi
callq 0x41e0
movq -0x48(%rbp), %rdi
callq 0x4580
nopw %cs:(%rax,%rax)