// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movq %rdi, %r14
movl $0x20, %edi
callq 0x51f0
movq 0x20(%r14), %rcx
movq 0x10(%rcx), %rdx
movq 0x8(%rdx), %rsi
subq (%rdx), %rsi
sarq $0x2, %rsi
addq (%r14), %rsi
movq (%rcx), %rdx
movq 0x10(%rdx), %rdi
subq 0x8(%rdx), %rdi
sarq $0x2, %rdi
leaq (%rdi,%rsi), %rdx
incq %rdx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rdx, 0x18(%rax)
movq %rax, 0x28(%r14)
movq (%rcx), %rax
movq 0x8(%rax), %rbx
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
cmpq %rax, %rbx
je 0xf0ba
leaq 0x18854(%rip), %rax     # 0x276d0
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq %r14, -0x50(%rbp)
movq (%rbx), %r15
testq %r15, %r15
je 0xeead
movq %r15, %rdi
leaq 0x18786(%rip), %rsi     # 0x27620
leaq 0x1868f(%rip), %rdx     # 0x27530
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r12
jmp 0xeeb0
xorl %r12d, %r12d
testq %r12, %r12
je 0xef7d
movl $0x30, %edi
callq 0x51f0
movq %rax, %r15
movq 0x8(%r12), %r12
movl $0x10, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%r15)
xorl %eax, %eax
movb %al, 0x10(%r15)
movq %r12, 0x18(%r15)
movb %al, 0x28(%r15)
movq -0x58(%rbp), %rax
movq %rax, (%r15)
movq $0x1, 0x20(%r15)
movq 0x28(%r14), %rdi
movq %r15, %rsi
callq 0x17d40
testb %r13b, %r13b
je 0xf0ac
movq (%r15), %rax
leaq -0x48(%rbp), %rdi
movq %r15, %rsi
callq *(%rax)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x1909c(%rip), %rdi     # 0x27fc8
callq 0x5250
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x51d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5140
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xf0ac
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x5200
jmp 0xf0ac
testq %r15, %r15
je 0xef9f
movq %r15, %rdi
leaq 0x18694(%rip), %rsi     # 0x27620
leaq 0x185e5(%rip), %rdx     # 0x27578
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r12
jmp 0xefa2
xorl %r12d, %r12d
testq %r12, %r12
je 0xf0ac
movl %r13d, %r14d
movl $0x48, %edi
callq 0x51f0
movq %rax, %r15
movq 0x8(%r12), %r13
movl $0x10, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x10(%r12), %rcx
movq 0x18(%r12), %r8
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
xorl %r9d, %r9d
callq 0x1650e
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rdi
movq %r15, %rsi
callq 0x17d40
movl %r14d, %r13d
testb %r13b, %r13b
je 0xf066
movq (%r15), %rax
leaq -0x48(%rbp), %rdi
movq %r15, %rsi
callq *(%rax)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x18faa(%rip), %rdi     # 0x27fc8
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 -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xf066
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0x50(%rbp), %r14
movq 0x20(%r14), %rax
movq 0x10(%rax), %r12
movq %r15, %rdi
callq 0x180e8
movq %rax, %rdi
callq 0x17d30
movq %rax, -0x48(%rbp)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0xf0a0
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r12)
jmp 0xf0ac
movq %r12, %rdi
leaq -0x48(%rbp), %rdx
callq 0x16a36
addq $0x8, %rbx
cmpq -0x60(%rbp), %rbx
jne 0xee88
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x48, %esi
jmp 0xf0dd
jmp 0xf0e2
movq %rax, %rbx
movl $0x30, %esi
movq %r15, %rdi
jmp 0xf0f9
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xf0fe
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x5200
movq %rbx, %rdi
callq 0x53d0