// File: RuiDGPires[P]CrimLang/src/generator.cpp
// Function: gc_Tracker::basic_expression(crl::Node*, std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char>>&)
u32 _gc_Tracker::basic_expression(crl::Node *node, std::stringstream &stream){
	size_t size = node->children.size();
	size_t i = 0;
	bool negate = false;

	if (node->get_child(0)->type == crl::Node::Type::LEAF){
		crl::Token t = get_token(node->get_child(0));
		if (t.type == crl::Token::Type::MINUS){
			negate = true;
			i++;
		}else if (t.type == crl::Token::Type::PLUS) i++;
	}
	u32 reg1 = this->term(node->get_child(i++), stream);
	std::string reg_name = reg_tracker.reg_name(reg1);

	if (negate){
		u32 aux = reg_tracker.alloc();
		stream <<
			"MVI " << reg_tracker.reg_name(aux) << " 0\n" <<
			"SUB " << reg_tracker.reg_name(aux) << " " << reg_name << "\n";
		reg_tracker.free(reg1);
		std::string name1 = reg_tracker.reg_name(aux);
		reg1 = aux;
	}

	u32 reg2;
	for (; i < size; i += 2){
		// TODO : division
		reg2 = this->term(node->get_child(i+1), stream);
		if (get_token(node->get_child(i)).type == crl::Token::Type::PLUS)
			stream << "ADD " << reg_name << " " << reg_tracker.reg_name(reg2) << "\n";
		else
			stream << "SUB " << reg_name << " " << reg_tracker.reg_name(reg2) << "\n";
		reg_tracker.free(reg2);
	}
	return reg1; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x51d0
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0xbd6e
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
xorl %esi, %esi
callq *0x18(%rax)
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0xa2e0
cmpl $0x17, -0x40(%rbp)
jne 0xbd51
movb $0x1, -0x29(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xbd65
cmpl $0x16, -0x40(%rbp)
jne 0xbd63
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xbd65
leaq -0x60(%rbp), %rdi
callq 0x285a0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rbp)
movl %eax, %esi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x180(%rbp), %rdi
movq %rax, %rsi
movq -0x18(%rbp), %rdx
callq 0xb7d0
movq -0x180(%rbp), %rsi
movl %eax, -0x64(%rbp)
addq $0x500, %rsi            # imm = 0x500
movl -0x64(%rbp), %edx
leaq -0x88(%rbp), %rdi
callq 0x13040
testb $0x1, -0x29(%rbp)
je 0xbfce
movq -0x180(%rbp), %rdi
addq $0x500, %rdi            # imm = 0x500
callq 0x12ee0
movl %eax, -0x184(%rbp)
jmp 0xbde1
movl -0x184(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
leaq 0x1f44d(%rip), %rsi     # 0x2b249
callq 0x42e0
movq %rax, -0x190(%rbp)
jmp 0xbe0a
movq -0x180(%rbp), %rsi
addq $0x500, %rsi            # imm = 0x500
movl -0x8c(%rbp), %edx
leaq -0xc0(%rbp), %rdi
callq 0x13040
jmp 0xbe2c
movq -0x190(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x42c0
movq %rax, -0x198(%rbp)
jmp 0xbe48
movq -0x198(%rbp), %rdi
leaq 0x1f46a(%rip), %rsi     # 0x2b2c0
callq 0x42e0
movq %rax, -0x1a0(%rbp)
jmp 0xbe64
movq -0x1a0(%rbp), %rdi
leaq 0x1f452(%rip), %rsi     # 0x2b2c4
callq 0x42e0
movq %rax, -0x1a8(%rbp)
jmp 0xbe80
movq -0x180(%rbp), %rsi
addq $0x500, %rsi            # imm = 0x500
movl -0x8c(%rbp), %edx
leaq -0xe0(%rbp), %rdi
callq 0x13040
jmp 0xbea2
movq -0x1a8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x42c0
movq %rax, -0x1b0(%rbp)
jmp 0xbebe
movq -0x1b0(%rbp), %rdi
leaq 0x1f139(%rip), %rsi     # 0x2b005
callq 0x42e0
movq %rax, -0x1b8(%rbp)
jmp 0xbeda
movq -0x1b8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x42c0
movq %rax, -0x1c0(%rbp)
jmp 0xbef6
movq -0x1c0(%rbp), %rdi
leaq 0x1f5df(%rip), %rsi     # 0x2b4e3
callq 0x42e0
jmp 0xbf0b
leaq -0xe0(%rbp), %rdi
callq 0x41e0
leaq -0xc0(%rbp), %rdi
callq 0x41e0
movq -0x180(%rbp), %rdi
addq $0x500, %rdi            # imm = 0x500
movl -0x64(%rbp), %esi
callq 0x13240
jmp 0xbf3b
movq -0x180(%rbp), %rsi
addq $0x500, %rsi            # imm = 0x500
movl -0x8c(%rbp), %edx
leaq -0x100(%rbp), %rdi
callq 0x13040
jmp 0xbf5d
movl -0x8c(%rbp), %eax
movl %eax, -0x64(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x41e0
jmp 0xbfce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xc27d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xbfbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x41e0
leaq -0xc0(%rbp), %rdi
callq 0x41e0
jmp 0xc27d
jmp 0xbfd0
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xc259
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
incl %esi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movq %rax, -0x1c8(%rbp)
jmp 0xbff9
movq -0x1c8(%rbp), %rsi
movq -0x180(%rbp), %rdi
movq -0x18(%rbp), %rdx
callq 0xb7d0
movl %eax, -0x1cc(%rbp)
jmp 0xc018
movl -0x1cc(%rbp), %eax
movl %eax, -0x104(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movq %rax, -0x1d8(%rbp)
jmp 0xc03d
movq -0x1d8(%rbp), %rsi
leaq -0x138(%rbp), %rdi
callq 0xa2e0
jmp 0xc052
movl -0x118(%rbp), %eax
movl %eax, -0x1dc(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x285a0
movl -0x1dc(%rbp), %eax
cmpl $0x16, %eax
jne 0xc155
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
leaq 0x1f241(%rip), %rsi     # 0x2b2c9
callq 0x42e0
movq %rax, -0x1e8(%rbp)
jmp 0xc096
movq -0x1e8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x42c0
movq %rax, -0x1f0(%rbp)
jmp 0xc0b2
movq -0x1f0(%rbp), %rdi
leaq 0x1ef45(%rip), %rsi     # 0x2b005
callq 0x42e0
movq %rax, -0x1f8(%rbp)
jmp 0xc0ce
movq -0x180(%rbp), %rsi
addq $0x500, %rsi            # imm = 0x500
movl -0x104(%rbp), %edx
leaq -0x158(%rbp), %rdi
callq 0x13040
jmp 0xc0f0
movq -0x1f8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x42c0
movq %rax, -0x200(%rbp)
jmp 0xc10c
movq -0x200(%rbp), %rdi
leaq 0x1f3c9(%rip), %rsi     # 0x2b4e3
callq 0x42e0
jmp 0xc121
leaq -0x158(%rbp), %rdi
callq 0x41e0
jmp 0xc22b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x41e0
jmp 0xc27d
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
leaq 0x1f160(%rip), %rsi     # 0x2b2c4
callq 0x42e0
movq %rax, -0x208(%rbp)
jmp 0xc172
movq -0x208(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x42c0
movq %rax, -0x210(%rbp)
jmp 0xc18e
movq -0x210(%rbp), %rdi
leaq 0x1ee69(%rip), %rsi     # 0x2b005
callq 0x42e0
movq %rax, -0x218(%rbp)
jmp 0xc1aa
movq -0x180(%rbp), %rsi
addq $0x500, %rsi            # imm = 0x500
movl -0x104(%rbp), %edx
leaq -0x178(%rbp), %rdi
callq 0x13040
jmp 0xc1cc
movq -0x218(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x42c0
movq %rax, -0x220(%rbp)
jmp 0xc1e8
movq -0x220(%rbp), %rdi
leaq 0x1f2ed(%rip), %rsi     # 0x2b4e3
callq 0x42e0
jmp 0xc1fd
leaq -0x178(%rbp), %rdi
callq 0x41e0
jmp 0xc22b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x41e0
jmp 0xc27d
movq -0x180(%rbp), %rdi
addq $0x500, %rdi            # imm = 0x500
movl -0x104(%rbp), %esi
callq 0x13240
jmp 0xc246
jmp 0xc248
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
jmp 0xbfd0
movl -0x64(%rbp), %eax
movl %eax, -0x224(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x41e0
movl -0x224(%rbp), %eax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
leaq -0x88(%rbp), %rdi
callq 0x41e0
movq -0x98(%rbp), %rdi
callq 0x4580
nopw %cs:(%rax,%rax)