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

	stream_funcs << name << ":\n"; 
	
	// RESULT SPACE ALLOCATION MUST BE DONE BY CALLER
	
	symb_tracker.frame_offset = size - 1;
	symb_tracker.sp = 0;

	this->stream_funcs << 
		";Function " << name << "\n" <<
		"MOV R7 RF\n" << // TMP STORE RF
		"MOV RF RS\n";

	symb_tracker.push("R7", stream_funcs);
	symb_tracker.push("RE", stream_funcs);


	// Arguents will be stored on the stack like this:
	// foo(a, b, c) -> [a b c]

	// TREAT ARGMENTS
	size_t s = symb_tracker.check();
	size_t args_size = 0;
	for(u32 i = 0; i < node->argc; i++){
		crl::Node *arg = node->get_child(2 + i);
		std::string type = get_token(arg->get_child(0)).str;
		std::string name = get_token(arg->get_child(1)).str;

		bool is_mut = arg->annotation == "mut";
		u32 arg_size = is_mut? 1: size_of(type);
		args_size += arg_size;
		
		symb_tracker.arg_create(name, arg_size, is_mut);
	}

	u32 rollback = symb_tracker.frame_offset;	
	this->stream_funcs <<
		"MVI R1 " << rollback << "\n" <<
		"ADD RF R1\n"; // Rollback frame

	crl::Node *block = node->get_child(2+node->argc);
	ASSERT(block->type == crl::Node::Type::BLOCK, "Funcs must have a block");
	

	this->block(block);
	symb_tracker.clean(s);

	this->stream_funcs << 
		"; End of function\n" <<
		name << "-end:\n" <<
		"MOV RS RF\n" <<
		"MVI R1 " << args_size + 1 + size << "\n" <<
		"SUB RS R1\n";
		
	symb_tracker.pop("RE", stream_funcs);
	symb_tracker.pop("RF", stream_funcs);

	this->stream_funcs <<
		"MVI R2 2\n"
		"SUB R1 R2\n" <<
		"ADD RS R1\n" <<
		"RET\n\n";
		// The rest is the result (on stack)
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rsi, %r12
movq %rdi, -0x30(%rbp)
movq (%rsi), %rax
movq %rsi, %rdi
xorl %esi, %esi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0xaa6c
movq (%r12), %rax
movq %r12, %rdi
movl $0x1, %esi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0xaad9
movq (%r12), %rax
movq %r12, %rdi
xorl %esi, %esi
callq *0x18(%rax)
movq %rax, %rbx
leaq -0x98(%rbp), %r13
movq %r13, -0x10(%r13)
movq 0x58(%rax), %rsi
movq 0x60(%rax), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
callq 0x4d4a
movl 0x80(%rbx), %eax
movl %eax, 0x28(%r14)
movq 0x78(%rbx), %rax
movq %rax, 0x20(%r14)
leaq -0x68(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rax
movq %r13, %r14
cmpq %r13, %rax
je 0xa279
movq %rax, -0x78(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0xa281
movups (%r14), %xmm0
movups %xmm0, (%r15)
leaq -0xa8(%rbp), %rdi
movq 0x8(%rdi), %rax
movq %rax, -0x70(%rbp)
movq %r14, (%rdi)
movq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
callq 0x192b0
movq (%r12), %rax
movq %r12, -0xe0(%rbp)
movq %r12, %rdi
movl $0x1, %esi
callq *0x18(%rax)
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
movq %r14, (%rdi)
movq 0x58(%rax), %rsi
movq 0x60(%rax), %rdx
addq %rsi, %rdx
callq 0x4d4a
movl 0x80(%rbx), %eax
movl %eax, -0x80(%rbp)
movq 0x78(%rbx), %rax
movq %rax, -0x88(%rbp)
leaq -0x40(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0xa8(%rbp), %rax
cmpq %r14, %rax
je 0xa311
movq %rax, -0x50(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0xa31a
movups (%r14), %xmm0
movups %xmm0, (%r12)
leaq -0xa8(%rbp), %rdi
movq 0x8(%rdi), %rax
leaq -0x50(%rbp), %rbx
movq %rax, 0x8(%rbx)
movq %r14, (%rdi)
movq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
callq 0x192b0
movq -0x30(%rbp), %rax
leaq 0x4a0(%rax), %rdi
movq %rbx, %rsi
callq 0x3150
leaq -0x170(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
addq %rsi, %rdx
leaq -0x180(%rbp), %rdi
callq 0x4d4a
leaq -0x180(%rbp), %rdi
callq 0x93d7
movq %rax, -0xd8(%rbp)
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0xa3a4
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x30(%rbp), %rax
leaq 0x328(%rax), %r13
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rdi
callq 0x3280
leaq 0x110de(%rip), %rsi     # 0x1b4a4
movl $0x2, %edx
movq %rax, %rdi
callq 0x3280
movq -0xd8(%rbp), %rax
decl %eax
movq -0x30(%rbp), %rcx
movl %eax, 0x5b0(%rcx)
movl $0x0, 0x5ac(%rcx)
leaq 0x11032(%rip), %rsi     # 0x1b429
movl $0xa, %edx
movq %r13, %rdi
callq 0x3280
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rdi
callq 0x3280
movq %rax, %rbx
leaq 0x110c5(%rip), %rsi     # 0x1b4e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x11002(%rip), %rsi     # 0x1b434
movl $0xa, %edx
movq %rbx, %rdi
callq 0x3280
leaq 0x10ff9(%rip), %rsi     # 0x1b43f
movl $0xa, %edx
movq %rbx, %rdi
callq 0x3280
movq -0x30(%rbp), %rax
leaq 0x318(%rax), %rdx
leaq 0x530(%rax), %rdi
leaq -0x1f0(%rbp), %r14
movq %r14, -0x10(%r14)
movw $0x3752, (%r14)         # imm = 0x3752
movq $0x2, -0x8(%r14)
movb $0x0, 0x2(%r14)
leaq -0x200(%rbp), %rsi
movq %rdi, -0x58(%rbp)
movq %rdx, -0xb0(%rbp)
callq 0xdf7e
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0xa4b5
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x3230
leaq -0x1d0(%rbp), %r14
movq %r14, -0x10(%r14)
movw $0x4552, (%r14)         # imm = 0x4552
movq $0x2, -0x8(%r14)
movb $0x0, 0x2(%r14)
leaq -0x1e0(%rbp), %rsi
movq -0x58(%rbp), %rdi
movq -0xb0(%rbp), %rdx
callq 0xdf7e
movq -0x1e0(%rbp), %rdi
cmpq %r14, %rdi
je 0xa505
movq -0x1d0(%rbp), %rsi
incq %rsi
callq 0x3230
movq %r13, -0x150(%rbp)
movq -0x30(%rbp), %rax
movq 0x5a0(%rax), %rax
xorl %ecx, %ecx
movq -0xe0(%rbp), %r12
cmpl $0x0, 0x50(%r12)
movq %rax, -0x148(%rbp)
je 0xa80f
leaq -0xf0(%rbp), %rbx
xorl %r14d, %r14d
movq %r14, -0x160(%rbp)
movq %rcx, -0x158(%rbp)
leal 0x2(%rcx), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
movq %rax, %r12
movq (%rax), %rax
movq %r12, %rdi
xorl %esi, %esi
callq *0x18(%rax)
leaq -0x98(%rbp), %r13
movq %rax, %r14
movq %r13, -0xa8(%rbp)
movq 0x58(%rax), %rsi
movq 0x60(%rax), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %rdi
callq 0x4d4a
movl 0x80(%r14), %eax
leaq -0x88(%rbp), %rcx
movl %eax, 0x8(%rcx)
movq 0x78(%r14), %rax
movq %rax, (%rcx)
leaq -0xc0(%rbp), %rcx
movq %rcx, -0xd0(%rbp)
movq -0xa8(%rbp), %rax
cmpq %r13, %rax
je 0xa5d7
movq %rax, -0xd0(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0xa5df
movups (%r13), %xmm0
movups %xmm0, (%rcx)
leaq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq %r13, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
movb $0x0, -0x98(%rbp)
movq %rdi, %r15
callq 0x192b0
movq (%r12), %rax
movq %r12, %rdi
movl $0x1, %esi
callq *0x18(%rax)
movq %rax, %r14
movq %r13, -0xa8(%rbp)
movq 0x58(%rax), %rsi
movq 0x60(%rax), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x4d4a
movl 0x80(%r14), %eax
leaq -0x88(%rbp), %rcx
movl %eax, 0x8(%rcx)
movq 0x78(%r14), %rax
movq %rax, (%rcx)
movq %rbx, -0x100(%rbp)
movq -0xa8(%rbp), %rax
cmpq %r13, %rax
je 0xa683
movq %rax, -0x100(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0xa68b
movups (%r13), %xmm0
movups %xmm0, (%rbx)
leaq -0xa8(%rbp), %rdi
leaq -0x140(%rbp), %r15
movq -0xa0(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq %r13, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
movb $0x0, -0x98(%rbp)
callq 0x192b0
addq $0x30, %r12
movq %r12, %rdi
leaq 0x10d7d(%rip), %rsi     # 0x1b450
callq 0x30a0
movl %eax, %r14d
testl %eax, %eax
je 0xa722
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0x4d4a
leaq -0x130(%rbp), %r13
leaq -0x120(%rbp), %rdi
callq 0x93d7
movl %eax, %r12d
jmp 0xa72f
movl $0x1, %r12d
leaq -0x130(%rbp), %r13
testl %r14d, %r14d
je 0xa756
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0xa756
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x3230
movq %r13, -0x140(%rbp)
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x4d4a
xorl %ecx, %ecx
testl %r14d, %r14d
sete %cl
movq -0x58(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0xdfd8
movq -0x140(%rbp), %rdi
cmpq %r13, %rdi
movq -0x160(%rbp), %r14
je 0xa7af
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x100(%rbp), %rdi
cmpq %rbx, %rdi
je 0xa7ca
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xa7ec
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3230
movl %r12d, %eax
addq %rax, %r14
movq -0x158(%rbp), %rcx
incl %ecx
movq -0xe0(%rbp), %r12
cmpl 0x50(%r12), %ecx
jb 0xa53d
jmp 0xa812
xorl %r14d, %r14d
movq %r14, %rbx
movq -0x30(%rbp), %rax
movl 0x5b0(%rax), %r14d
leaq 0x10b56(%rip), %rsi     # 0x1b37d
movl $0x7, %edx
movq -0x150(%rbp), %r13
movq %r13, %rdi
callq 0x3280
movq %r13, %rdi
movq %r14, %rsi
callq 0x3170
movq %rax, %r14
leaq 0x10c93(%rip), %rsi     # 0x1b4e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x10bf0(%rip), %rsi     # 0x1b454
movl $0xa, %edx
movq %r14, %rdi
callq 0x3280
movl 0x50(%r12), %esi
addl $0x2, %esi
movq (%r12), %rax
movq %r12, %rdi
leaq -0x68(%rbp), %r15
leaq -0x40(%rbp), %r12
callq *0x18(%rax)
cmpl $0x5, 0x8(%rax)
jne 0xab43
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0xadb6
movq -0x58(%rbp), %rdi
movq -0x148(%rbp), %rsi
callq 0xe1d2
leaq 0x10bd5(%rip), %rsi     # 0x1b48d
movl $0x12, %edx
movq %r13, %rdi
callq 0x3280
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rdi
callq 0x3280
movq %rax, %r14
leaq 0x10bc1(%rip), %rsi     # 0x1b4a0
movl $0x6, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x10bb4(%rip), %rsi     # 0x1b4a7
movl $0xa, %edx
movq %r14, %rdi
callq 0x3280
leaq 0x10a76(%rip), %rsi     # 0x1b37d
movl $0x7, %edx
movq %r14, %rdi
callq 0x3280
movl -0xd8(%rbp), %eax
leaq (%rax,%rbx), %rsi
incq %rsi
movq %r14, %rdi
callq 0x3170
movq %rax, %rbx
leaq 0x10bb0(%rip), %rsi     # 0x1b4e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x10b6b(%rip), %rsi     # 0x1b4b2
movl $0xa, %edx
movq %rbx, %rdi
callq 0x3280
leaq -0x1b0(%rbp), %r14
movq %r14, -0x10(%r14)
movw $0x4552, (%r14)         # imm = 0x4552
movq $0x2, -0x8(%r14)
movb $0x0, 0x2(%r14)
leaq -0x1c0(%rbp), %rsi
movq -0x58(%rbp), %rdi
movq -0xb0(%rbp), %rdx
callq 0xe178
movq -0x1c0(%rbp), %rdi
cmpq %r14, %rdi
je 0xa9a4
movq -0x1b0(%rbp), %rsi
incq %rsi
callq 0x3230
leaq -0x190(%rbp), %r14
movq %r14, -0x10(%r14)
movw $0x4652, (%r14)         # imm = 0x4652
movq $0x2, -0x8(%r14)
movb $0x0, 0x2(%r14)
leaq -0x1a0(%rbp), %rsi
movq -0x58(%rbp), %rdi
movq -0xb0(%rbp), %rdx
callq 0xe178
movq -0x1a0(%rbp), %rdi
cmpq %r14, %rdi
je 0xa9f4
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x3230
leaq 0x10ac5(%rip), %rsi     # 0x1b4c0
movl $0x13, %edx
movq %r13, %rdi
callq 0x3280
leaq 0x10ac5(%rip), %rsi     # 0x1b4d4
movl $0xa, %edx
movq %r13, %rdi
callq 0x3280
leaq 0x10abc(%rip), %rsi     # 0x1b4df
movl $0x5, %edx
movq %r13, %rdi
callq 0x3280
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0xaa45
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0xaa5a
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3230
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x50, %edi
callq 0x3120
movq %rax, %r14
leaq -0x250(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1093a(%rip), %rsi     # 0x1b3c6
leaq 0x10963(%rip), %rdx     # 0x1b3f6
leaq -0x260(%rbp), %rdi
callq 0x433a
movb $0x1, %r15b
leaq -0x260(%rbp), %rcx
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5470
xorl %r15d, %r15d
leaq 0x182b3(%rip), %rsi     # 0x22d78
leaq -0x54e6(%rip), %rdx     # 0x55e6
movq %r14, %rdi
callq 0x33d0
jmp 0xabab
movl $0x50, %edi
callq 0x3120
movq %rax, %r14
leaq -0x230(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x108fe(%rip), %rsi     # 0x1b3f7
leaq 0x10928(%rip), %rdx     # 0x1b428
leaq -0x240(%rbp), %rdi
callq 0x433a
movb $0x1, %r15b
leaq -0x240(%rbp), %rcx
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5470
xorl %r15d, %r15d
leaq 0x18246(%rip), %rsi     # 0x22d78
leaq -0x5553(%rip), %rdx     # 0x55e6
movq %r14, %rdi
callq 0x33d0
jmp 0xabab
movl $0x50, %edi
callq 0x3120
movq %rax, %r14
leaq -0x210(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x10982(%rip), %rsi     # 0x1b4e5
leaq 0x10992(%rip), %rdx     # 0x1b4fc
leaq -0x220(%rbp), %rdi
callq 0x433a
movb $0x1, %r15b
leaq -0x220(%rbp), %rcx
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5470
xorl %r15d, %r15d
leaq 0x181dc(%rip), %rsi     # 0x22d78
leaq -0x55bd(%rip), %rdx     # 0x55e6
movq %r14, %rdi
callq 0x33d0
movq %rax, %r13
movq -0x220(%rbp), %rdi
cmpq %r12, %rdi
je 0xabd1
movq -0x210(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xabd1
movq %rax, %r13
movb $0x1, %r15b
testb %r15b, %r15b
je 0xad7b
movq %r14, %rdi
callq 0x31a0
jmp 0xad7b
movq %rax, %r13
movq -0x240(%rbp), %rdi
cmpq %r12, %rdi
je 0xac27
movq -0x230(%rbp), %rsi
jmp 0xac17
jmp 0xac21
movq %rax, %r13
movq -0x260(%rbp), %rdi
cmpq %r12, %rdi
je 0xac27
movq -0x250(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xac27
movq %rax, %r13
movb $0x1, %r15b
testb %r15b, %r15b
je 0xadad
movq %r14, %rdi
callq 0x31a0
jmp 0xadad
movq %rax, %r13
movq -0x1a0(%rbp), %rdi
cmpq %r14, %rdi
je 0xad7b
movq -0x190(%rbp), %rsi
jmp 0xad73
movq %rax, %r13
movq -0x1c0(%rbp), %rdi
cmpq %r14, %rdi
je 0xad7b
movq -0x1b0(%rbp), %rsi
jmp 0xad73
movq %rax, %r13
movq -0x1e0(%rbp), %rdi
cmpq %r14, %rdi
je 0xad83
movq -0x1d0(%rbp), %rsi
jmp 0xaccd
movq %rax, %r13
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0xad83
movq -0x1f0(%rbp), %rsi
jmp 0xaccd
movq %rax, %r13
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0xad83
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xad83
jmp 0xad07
movq %rax, %r13
jmp 0xad98
jmp 0xad51
jmp 0xad07
movq %rax, %r13
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0xad34
movq -0x110(%rbp), %rsi
jmp 0xad27
movq %rax, %r13
jmp 0xad83
jmp 0xad51
movq %r13, %rcx
movq %rax, %r13
movq -0x140(%rbp), %rdi
cmpq %rcx, %rdi
je 0xad34
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xad34
movq %rax, %r13
movq -0x100(%rbp), %rdi
cmpq %rbx, %rdi
je 0xad59
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xad59
movq %rax, %r13
jmp 0xad7b
movq %rax, %r13
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xad7b
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3230
leaq -0x68(%rbp), %r15
leaq -0x40(%rbp), %r12
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0xad98
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0xadad
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3230
movq %r13, %rdi
callq 0x33f0
nop