// File: /workspace/github_cpp_makefile_star2_O3/Bartor[P]kompilator/middle/abstract_assembler/AbstractAssembler.cpp
// Function: AbstractAssembler::getVariablesFromDeclarations(bool)
void AbstractAssembler::getVariablesFromDeclarations(bool verbose) {
    scopedVariables = new ScopedVariables(
            1 // next address...
            + accumulatorNumber // ...offset by accumulators...
            + program.constants.constants.size() * 2 // ...constants times two for additional special constants (1024 = 2^10, so we may need 10 as well)
            + program.declarations.declarations.size() * 2 // ...and possible constants (array starting addresses + array starting indexes)
    );

    for (const auto &declaration : program.declarations.declarations) {
        if (auto numDecl = dynamic_cast<IdentifierDeclaration *>(declaration)) {
            NumberVariable *var = new NumberVariable(numDecl->name, *new ResolvableAddress());
            scopedVariables->pushVariableScope(var);
            if (verbose) std::cout << var->toString() << std::endl;
        } else if (auto arrDecl = dynamic_cast<ArrayDeclaration *>(declaration)) {
            NumberArrayVariable *var = new NumberArrayVariable(arrDecl->name, *new ResolvableAddress(), arrDecl->start, arrDecl->end);
            scopedVariables->pushVariableScope(var);
            if (verbose) std::cout << var->toString() << std::endl;

            // this is an array start address; it must be included in generated constants to use with indirect
            // variable addressing mode (e.g. a[b])
            // constants are to be generated in the next step, based on program constants
            // this isn't the best solutions and should be changed one day
            program.constants.constants.push_back(var->getAddress().getAddress());
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, -0x2c(%rbp)
movq %rdi, %rbx
movl $0x20, %edi
callq 0x51f0
movq 0x20(%rbx), %rcx
movq (%rcx), %rdx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
subq (%rcx), %rsi
sarq $0x2, %rsi
addq (%rbx), %rsi
movq 0x10(%rdx), %rcx
subq 0x8(%rdx), %rcx
sarq $0x2, %rcx
addq %rsi, %rcx
incq %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %rbx, -0x58(%rbp)
movq %rax, 0x28(%rbx)
movq 0x8(%rdx), %rbx
movq 0x10(%rdx), %r14
cmpq %r14, %rbx
je 0xef9d
leaq 0x17962(%rip), %rax     # 0x266d0
addq $0x10, %rax
movq %rax, -0x60(%rbp)
movq (%rbx), %r12
testq %r12, %r12
je 0xef90
movq %r12, %rdi
leaq 0x178f4(%rip), %r13     # 0x26680
movq %r13, %rsi
leaq 0x177d2(%rip), %rdx     # 0x26568
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0xee73
movq %rax, %r15
movl $0x30, %edi
callq 0x51f0
movq %rax, %r13
movq 0x8(%r15), %r15
movl $0x10, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%r13)
xorl %eax, %eax
movb %al, 0x10(%r13)
movq %r15, 0x18(%r13)
movb %al, 0x28(%r13)
movq -0x60(%rbp), %rax
movq %rax, (%r13)
movq $0x1, 0x20(%r13)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rdi
movq %r13, %rsi
callq 0x17afc
cmpb $0x0, -0x2c(%rbp)
je 0xef90
movq (%r13), %rax
leaq -0x50(%rbp), %rdi
movq %r13, %rsi
callq *(%rax)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x181a6(%rip), %rdi     # 0x26fc8
callq 0x5250
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x51d0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5140
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xef90
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x5200
jmp 0xef90
movq %r12, %rdi
movq %r13, %rsi
leaq 0x17730(%rip), %rdx     # 0x265b0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0xef90
movq %rax, %r12
movl $0x48, %edi
callq 0x51f0
movq %rax, %r13
movq 0x8(%r12), %r15
movl $0x10, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x10(%r12), %rcx
movq 0x18(%r12), %r8
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
xorl %r9d, %r9d
callq 0x1627a
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rdi
movq %r13, %rsi
callq 0x17afc
cmpb $0x0, -0x2c(%rbp)
je 0xef4a
movq (%r13), %rax
leaq -0x50(%rbp), %rdi
movq %r13, %rsi
callq *(%rax)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x180c6(%rip), %rdi     # 0x26fc8
callq 0x5250
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x51d0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5140
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xef4a
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %r12
movq %r13, %rdi
callq 0x17e78
movq %rax, %rdi
callq 0x17aec
movq %rax, -0x50(%rbp)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0xef84
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r12)
jmp 0xef90
movq %r12, %rdi
leaq -0x50(%rbp), %rdx
callq 0x167a8
addq $0x8, %rbx
cmpq %r14, %rbx
jne 0xed76
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xefc5
movq %rax, %rbx
movl $0x48, %esi
jmp 0xefc0
movq %rax, %rbx
movl $0x30, %esi
movq %r13, %rdi
jmp 0xefdc
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xefe1
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x5200
movq %rbx, %rdi
callq 0x53d0
nop