// File: /workspace/github_cpp_makefile_star2_O2/rfotino[P]consolite-compiler/src/parser.cpp
// Function: Parser::output(char*)
bool Parser::output(char *filename) {
  // Start by opening the output file.
  _outfile.open(filename, std::ofstream::out | std::ofstream::trunc);
  if (!_outfile.good()) {
    _error("Unable to open output file.");
    return false;
  }
  // Next assign labels for all globals and functions.
  for (auto global : _globals) {
    this->addLabel(global->name());
  }
  for (auto function : _functions) {
    this->addLabel(function->name());
  }
  // Output the "bootloader". This sets the stack pointer, calls main,
  // then goes into an infinite loop to prevent attempting to execute
  // code that wasn't meant to be executed.
  std::string stackLabel = this->getUnusedLabel("stack");
  this->writeInst("MOVI SP " + stackLabel);
  this->writeInst("CALL main");
  std::string finishedLabel = this->getUnusedLabel("program_finished");
  this->writeln(finishedLabel + ":");
  this->writeInst("JMPI " + finishedLabel);
  // Output global variables.
  for (auto global : _globals) {
    global->output(this);
  }
  // Output functions.
  for (auto function: _functions) {
    function->output(this);
  }
  // Output the stack position.
  this->writeln(stackLabel + ":");
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
addq $0x38, %rdi
pushq $0x30
popq %rdx
callq 0x7050
movq 0x38(%rbx), %rax
movq -0x18(%rax), %rax
movl 0x58(%rbx,%rax), %ebp
testl %ebp, %ebp
je 0x8cb3
leaq 0x2043d(%rip), %rsi     # 0x290cf
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x9502
leaq 0x58(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0x285f3
jmp 0x8f34
movl %ebp, 0x54(%rsp)
movq 0x8(%rbx), %r15
movq 0x10(%rbx), %rbp
leaq 0x18(%rsp), %r14
leaq 0x10(%rsp), %r12
leaq 0x58(%rsp), %r13
cmpq %rbp, %r15
je 0x8d10
movq %r12, %rdi
movq %r15, %rsi
callq 0xa22a
movq 0x10(%rsp), %rsi
addq $0x70, %rsi
movq %r13, %rdi
callq 0x73e0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8fe8
movq %r13, %rdi
callq 0x75c8
movq %r14, %rdi
callq 0x7a34
addq $0x10, %r15
jmp 0x8cce
movq 0x20(%rbx), %r15
movq 0x28(%rbx), %rbp
leaq 0x18(%rsp), %r14
leaq 0x10(%rsp), %r12
leaq 0x58(%rsp), %r13
cmpq %rbp, %r15
je 0x8d69
movq %r12, %rdi
movq %r15, %rsi
callq 0xa23e
movq 0x10(%rsp), %rsi
addq $0x58, %rsi
movq %r13, %rdi
callq 0x73e0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8fe8
movq %r13, %rdi
callq 0x75c8
movq %r14, %rdi
callq 0x7a34
addq $0x10, %r15
jmp 0x8d27
leaq 0x2037b(%rip), %rsi     # 0x290eb
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x9502
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rbx, %rsi
callq 0x901a
leaq 0x10(%rsp), %rdi
callq 0x75c8
leaq 0x2034f(%rip), %rsi     # 0x290f1
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x9980
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x914a
leaq 0x10(%rsp), %rdi
callq 0x75c8
leaq 0x2032b(%rip), %rsi     # 0x290fa
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x9502
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x914a
leaq 0x10(%rsp), %rdi
callq 0x75c8
leaq 0x20308(%rip), %rsi     # 0x29104
leaq 0x30(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x9502
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movq %rbx, %rsi
callq 0x901a
leaq 0x30(%rsp), %rdi
callq 0x75c8
leaq 0x22802(%rip), %rdx     # 0x2b630
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x99f6
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x93f2
leaq 0x30(%rsp), %rdi
callq 0x75c8
leaq 0x202ba(%rip), %rsi     # 0x29115
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x9980
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x914a
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x75c8
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %r13
leaq 0x38(%rsp), %r15
cmpq %r13, %r12
je 0x8ebc
movq %r14, %rdi
movq %r12, %rsi
callq 0xa22a
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x12670
movq %r15, %rdi
callq 0x7a34
addq $0x10, %r12
jmp 0x8e91
movq 0x20(%rbx), %r15
movq 0x28(%rbx), %r13
leaq 0x38(%rsp), %r14
leaq 0x30(%rsp), %r12
cmpq %r13, %r15
je 0x8ef9
movq %r12, %rdi
movq %r15, %rsi
callq 0xa23e
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x14158
movq %r14, %rdi
callq 0x7a34
addq $0x10, %r15
jmp 0x8ece
leaq 0x22730(%rip), %rdx     # 0x2b630
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x99f6
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x93f2
movl 0x54(%rsp), %ebp
leaq 0x30(%rsp), %rdi
callq 0x75c8
leaq 0x10(%rsp), %rdi
callq 0x75c8
leaq 0x58(%rsp), %rdi
callq 0x75c8
testl %ebp, %ebp
sete %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8f5a
jmp 0x8f7b
jmp 0x8f5a
jmp 0x8f7b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x75c8
jmp 0x8fab
jmp 0x8f7b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x8fb0
jmp 0x8f8e
jmp 0x8f7b
jmp 0x8f8e
movq %rax, %rbx
jmp 0x8fab
jmp 0x8f8e
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x8fba
jmp 0x8f93
movq %rax, %rbx
jmp 0x8fb5
movq %rax, %rbx
jmp 0x8fdf
movq %rax, %rbx
movq %r14, %rdi
jmp 0x8fa6
movq %rax, %rbx
movq %r15, %rdi
callq 0x7a34
leaq 0x10(%rsp), %rdi
callq 0x75c8
leaq 0x58(%rsp), %rdi
callq 0x75c8
jmp 0x8fdf
jmp 0x8fc5
jmp 0x8fca
movq %rax, %rbx
jmp 0x8fd7
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x75c8
movq %r14, %rdi
callq 0x7a34
movq %rbx, %rdi
callq 0x7510
nop