// File: /workspace/github_cpp_makefile_star2_O3/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 0xaa03
movq (%r12), %rax
movq %r12, %rdi
movl $0x1, %esi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0xaa6f
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 0x4d52
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 0xa215
movq %rax, -0x78(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0xa21d
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 0x191f8
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 0x4d52
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 0xa2ad
movq %rax, -0x50(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0xa2b6
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 0x191f8
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 0x4d52
leaq -0x180(%rbp), %rdi
callq 0x9395
movq %rax, -0xd8(%rbp)
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0xa340
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 0x11142(%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 0x11096(%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 0x11129(%rip), %rsi     # 0x1b4e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x11066(%rip), %rsi     # 0x1b434
movl $0xa, %edx
movq %rbx, %rdi
callq 0x3280
leaq 0x1105d(%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 0xdf3e
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0xa451
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 0xdf3e
movq -0x1e0(%rbp), %rdi
cmpq %r14, %rdi
je 0xa4a1
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 0xa7a6
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 0x4d52
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 0xa573
movq %rax, -0xd0(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0xa57b
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 0x191f8
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 0x4d52
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 0xa61f
movq %rax, -0x100(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0xa627
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 0x191f8
addq $0x30, %r12
movq %r12, %rdi
leaq 0x10de1(%rip), %rsi     # 0x1b450
callq 0x30a0
movl %eax, %r14d
testl %eax, %eax
je 0xa6e0
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0x4d52
leaq -0x130(%rbp), %r13
leaq -0x120(%rbp), %rdi
callq 0x9395
movl %eax, %r12d
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0xa6ed
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xa6ed
movl $0x1, %r12d
leaq -0x130(%rbp), %r13
movq %r13, -0x140(%rbp)
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x4d52
xorl %ecx, %ecx
testl %r14d, %r14d
sete %cl
movq -0x58(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0xdf98
movq -0x140(%rbp), %rdi
cmpq %r13, %rdi
movq -0x160(%rbp), %r14
je 0xa746
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x100(%rbp), %rdi
cmpq %rbx, %rdi
je 0xa761
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xa783
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 0xa4d9
jmp 0xa7a9
xorl %r14d, %r14d
movq %r14, %rbx
movq -0x30(%rbp), %rax
movl 0x5b0(%rax), %r14d
leaq 0x10bbf(%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 0x10cfc(%rip), %rsi     # 0x1b4e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x10c59(%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 0xaad8
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0xad52
movq -0x58(%rbp), %rdi
movq -0x148(%rbp), %rsi
callq 0xe184
leaq 0x10c3e(%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 0x10c2a(%rip), %rsi     # 0x1b4a0
movl $0x6, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x10c1d(%rip), %rsi     # 0x1b4a7
movl $0xa, %edx
movq %r14, %rdi
callq 0x3280
leaq 0x10adf(%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 0x10c19(%rip), %rsi     # 0x1b4e3
movl $0x1, %edx
movq %rax, %rdi
callq 0x3280
leaq 0x10bd4(%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 0xe12a
movq -0x1c0(%rbp), %rdi
cmpq %r14, %rdi
je 0xa93b
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 0xe12a
movq -0x1a0(%rbp), %rdi
cmpq %r14, %rdi
je 0xa98b
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x3230
leaq 0x10b2e(%rip), %rsi     # 0x1b4c0
movl $0x13, %edx
movq %r13, %rdi
callq 0x3280
leaq 0x10b2e(%rip), %rsi     # 0x1b4d4
movl $0xa, %edx
movq %r13, %rdi
callq 0x3280
leaq 0x10b25(%rip), %rsi     # 0x1b4df
movl $0x5, %edx
movq %r13, %rdi
callq 0x3280
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0xa9dc
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0xa9f1
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), %r15
movq %r15, -0x10(%r15)
leaq 0x109a4(%rip), %rsi     # 0x1b3c6
leaq 0x109cd(%rip), %rdx     # 0x1b3f6
leaq -0x260(%rbp), %rdi
callq 0x4336
movb $0x1, %r12b
leaq -0x260(%rbp), %rcx
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5470
xorl %r12d, %r12d
leaq 0x1831d(%rip), %rsi     # 0x22d78
leaq -0x547c(%rip), %rdx     # 0x55e6
movq %r14, %rdi
callq 0x33d0
jmp 0xab3f
movl $0x50, %edi
callq 0x3120
movq %rax, %r14
leaq -0x230(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x10969(%rip), %rsi     # 0x1b3f7
leaq 0x10993(%rip), %rdx     # 0x1b428
leaq -0x240(%rbp), %rdi
callq 0x4336
movb $0x1, %r12b
leaq -0x240(%rbp), %rcx
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5470
xorl %r12d, %r12d
leaq 0x182b1(%rip), %rsi     # 0x22d78
leaq -0x54e8(%rip), %rdx     # 0x55e6
movq %r14, %rdi
callq 0x33d0
jmp 0xab3f
movl $0x50, %edi
callq 0x3120
movq %rax, %r14
leaq -0x210(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x109ee(%rip), %rsi     # 0x1b4e5
leaq 0x109fe(%rip), %rdx     # 0x1b4fc
leaq -0x220(%rbp), %rdi
callq 0x4336
movb $0x1, %r12b
leaq -0x220(%rbp), %rcx
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5470
xorl %r12d, %r12d
leaq 0x18248(%rip), %rsi     # 0x22d78
leaq -0x5551(%rip), %rdx     # 0x55e6
movq %r14, %rdi
callq 0x33d0
movq %rax, %r13
movq -0x220(%rbp), %rdi
cmpq %r15, %rdi
je 0xab5d
movq -0x210(%rbp), %rsi
incq %rsi
callq 0x3230
testb %r12b, %r12b
leaq -0x68(%rbp), %r15
leaq -0x40(%rbp), %r12
jne 0xab7a
jmp 0xad1f
movq %rax, %r13
leaq -0x68(%rbp), %r15
leaq -0x40(%rbp), %r12
movq %r14, %rdi
callq 0x31a0
jmp 0xad1f
movq %rax, %r13
movq -0x240(%rbp), %rdi
cmpq %r15, %rdi
je 0xabbf
movq -0x230(%rbp), %rsi
jmp 0xabb7
jmp 0xabc9
movq %rax, %r13
movq -0x260(%rbp), %rdi
cmpq %r15, %rdi
je 0xabbf
movq -0x250(%rbp), %rsi
incq %rsi
callq 0x3230
testb %r12b, %r12b
jne 0xabcc
jmp 0xad49
movq %rax, %r13
movq %r14, %rdi
callq 0x31a0
jmp 0xad49
movq %rax, %r13
movq -0x1a0(%rbp), %rdi
cmpq %r14, %rdi
je 0xad17
movq -0x190(%rbp), %rsi
jmp 0xad0f
movq %rax, %r13
movq -0x1c0(%rbp), %rdi
cmpq %r14, %rdi
je 0xad17
movq -0x1b0(%rbp), %rsi
jmp 0xad0f
movq %rax, %r13
movq -0x1e0(%rbp), %rdi
cmpq %r14, %rdi
je 0xad1f
movq -0x1d0(%rbp), %rsi
jmp 0xac69
movq %rax, %r13
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0xad1f
movq -0x1f0(%rbp), %rsi
jmp 0xac69
movq %rax, %r13
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0xad1f
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xad1f
jmp 0xaca3
movq %rax, %r13
jmp 0xad34
jmp 0xaced
jmp 0xaca3
movq %rax, %r13
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0xacd0
movq -0x110(%rbp), %rsi
jmp 0xacc3
movq %rax, %r13
jmp 0xad1f
jmp 0xaced
movq %r13, %rcx
movq %rax, %r13
movq -0x140(%rbp), %rdi
cmpq %rcx, %rdi
je 0xacd0
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xacd0
movq %rax, %r13
movq -0x100(%rbp), %rdi
cmpq %rbx, %rdi
je 0xacf5
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x3230
jmp 0xacf5
movq %rax, %r13
jmp 0xad17
movq %rax, %r13
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xad17
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3230
leaq -0x68(%rbp), %r15
leaq -0x40(%rbp), %r12
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0xad34
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3230
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0xad49
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3230
movq %r13, %rdi
callq 0x33f0
nop