// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x20
popq %rdi
callq 0x51c0
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
andq $0x0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rbx, -0x38(%rbp)
movq %rax, 0x28(%rbx)
movq 0x8(%rdx), %r13
movq 0x10(%rdx), %r14
leaq 0x14b80(%rip), %rbx     # 0x1f688
leaq 0x14bc9(%rip), %rax     # 0x1f6d8
addq $0x10, %rax
movq %rax, -0x40(%rbp)
cmpq %r14, %r13
je 0xacbf
movq (%r13), %r15
testq %r15, %r15
je 0xacb6
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x14a36(%rip), %rdx     # 0x1f570
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xabe7
movq %rax, %rbx
pushq $0x30
popq %rdi
callq 0x51c0
movq %rax, %r12
movq 0x8(%rbx), %rbx
pushq $0x10
popq %rdi
callq 0x51c0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x8(%r12)
xorl %eax, %eax
movb %al, 0x10(%r12)
movq %rbx, 0x18(%r12)
movb %al, 0x28(%r12)
movq -0x40(%rbp), %rax
movq %rax, (%r12)
movq $0x1, 0x20(%r12)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
movq %r12, %rsi
callq 0x12a70
cmpb $0x0, -0x2c(%rbp)
leaq 0x14adc(%rip), %rbx     # 0x1f688
je 0xacb6
movq (%r12), %rax
leaq -0x60(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq *(%rax)
movq 0x153ff(%rip), %rdi     # 0x1ffc8
movq %r15, %rsi
callq 0x5190
movq %rax, %rdi
callq 0x5100
leaq -0x60(%rbp), %rdi
callq 0x5418
jmp 0xacb6
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x149c4(%rip), %rdx     # 0x1f5b8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xacb6
movq %rax, %r15
pushq $0x48
popq %rdi
callq 0x51c0
movq %rax, %r12
movq 0x8(%r15), %rbx
pushq $0x10
popq %rdi
callq 0x51c0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x10(%r15), %rcx
movq 0x18(%r15), %r8
movq %r12, %rdi
movq %rbx, %rsi
movq %rax, %rdx
xorl %r9d, %r9d
callq 0x1165c
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
movq %r12, %rsi
callq 0x12a70
cmpb $0x0, -0x2c(%rbp)
je 0xac83
movq (%r12), %rax
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq *(%rax)
movq 0x1535e(%rip), %rdi     # 0x1ffc8
movq %rbx, %rsi
callq 0x5190
movq %rax, %rdi
callq 0x5100
leaq -0x60(%rbp), %rdi
callq 0x5418
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rbx
movq %r12, %rdi
callq 0x12e14
movq %rax, %rdi
callq 0x12a60
movq %rax, -0x60(%rbp)
movq %rbx, %rdi
leaq -0x60(%rbp), %rsi
callq 0x11992
leaq 0x149d2(%rip), %rbx     # 0x1f688
addq $0x8, %r13
jmp 0xab17
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xacd7
movq %rax, %rbx
pushq $0x48
jmp 0xacea
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x5418
jmp 0xacf3
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r12, %rdi
callq 0x51d0
movq %rbx, %rdi
callq 0x5390
nop