// File: /workspace/github_c_makefile_star2_O1/atomlang[P]atomlang/src/pk_compiler.c
// Function: compile
PkResult compile(PKVM* vm, Script* script, const char* source,
                 const PkCompileOptions* options) {

  // Skip utf8 BOM if there is any.
  if (strncmp(source, "\xEF\xBB\xBF", 3) == 0) source += 3;

  Compiler _compiler;
  Compiler* compiler = &_compiler; //< Compiler pointer for quick access.
  compilerInit(compiler, vm, source, script, options);

  // If compiling for an imported script the vm->compiler would be the compiler
  // of the script that imported this script. Add the all the compilers into a
  // link list.
  compiler->next_compiler = vm->compiler;
  vm->compiler = compiler;

  // If the script doesn't has a body by default, it's probably was created by
  // the native api function (pkNewModule() that'll return a module without a
  // main function) so just create and add the function here.
  if (script->body == NULL) scriptAddMain(vm, script);

  // If we're compiling for a script that was already compiled (when running
  // REPL or evaluating an expression) we don't need the old main anymore.
  // just use the globals and functions of the script and use a new body func.
  pkByteBufferClear(&script->body->fn->opcodes, vm);

  // Remember the count of the globals, functions and types, If the compilation
  // failed discard all the globals and functions added by the compilation.
  uint32_t globals_count = script->globals.count;
  uint32_t functions_count = script->functions.count;
  uint32_t types_count = script->classes.count;

  Func curr_fn;
  curr_fn.depth = DEPTH_SCRIPT;
  curr_fn.ptr = script->body;
  curr_fn.outer_func = NULL;
  compiler->func = &curr_fn;

  // Lex initial tokens. current <-- next.
  lexToken(compiler);
  lexToken(compiler);
  skipNewLines(compiler);

  if (match(compiler, TK_MODULE)) {

    // If the script running a REPL or compiled multiple times by hosting
    // application module attribute might already set. In that case make it
    // Compile error.
    if (script->module != NULL) {
      parseError(compiler, "Module name already defined.");

    } else {
      consume(compiler, TK_NAME, "Expected a name for the module.");
      const char* name = compiler->previous.start;
      uint32_t len = compiler->previous.length;
      script->module = newStringLength(vm, name, len);
      consumeEndStatement(compiler);
    }
  }

  while (!match(compiler, TK_EOF)) {
    compileTopLevelStatement(compiler);
    skipNewLines(compiler);
  }

  emitFunctionEnd(compiler);

  // Resolve forward names (function names that are used before defined).
  for (int i = 0; i < compiler->forwards_count; i++) {
    ForwardName* forward = &compiler->forwards[i];
    const char* name = forward->name;
    int length = forward->length;
    int index = scriptGetFunc(script, name, (uint32_t)length);
    if (index != -1) {
      patchForward(compiler, forward->func, forward->instruction, index);
    } else {
      // need_more_lines is only true for unexpected EOF errors. For syntax
      // errors it'll be false by now but. Here it's a semantic errors, so
      // we're overriding it to false.
      compiler->need_more_lines = false;
      resolveError(compiler, forward->line, "Name '%.*s' is not defined.",
                   length, name);
    }
  }

  vm->compiler = compiler->next_compiler;

  // If compilation failed, discard all the invalid functions and globals.
  if (compiler->has_errors) {
    script->globals.count = script->global_names.count = globals_count;
    script->functions.count = functions_count;
    script->classes.count = types_count;
  }

#if DEBUG_DUMP_COMPILED_CODE
  pkByteBuffer buff;
  pkByteBufferInit(&buff);
  dumpFunctionCode(compiler->vm, script->body, &buff);
  printf("%s", buff.data);
  pkByteBufferClear(&buff, vm);
#endif

  // Return the compilation result.
  if (compiler->has_errors) {
    if (compiler->options && compiler->options->repl_mode &&
        compiler->need_more_lines) {
      return PK_RESULT_UNEXPECTED_EOF;
    }
    return PK_RESULT_COMPILE_ERROR;
  }
  return PK_RESULT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3938, %rsp           # imm = 0x3938
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
leaq 0xdb19(%rip), %rsi      # 0x1ddc0
movl $0x3, %edx
movq %r14, %rdi
callq 0x2090
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leaq (%rcx,%rcx,2), %rax
addq %r14, %rax
leaq 0x68(%rsp), %rcx
movq %r15, (%rcx)
xorl %edx, %edx
movq %rdx, 0x8(%rcx)
movq %rax, 0x10(%rcx)
movq %r13, 0x18b0(%rcx)
movq %rax, 0x18(%rcx)
movb %dl, 0x90(%rcx)
movb %dl, 0x91(%rcx)
movq %rbx, 0x98(%rcx)
movq %rax, 0x20(%rcx)
movl $0x1, 0x28(%rcx)
movl %edx, 0x70(%rcx)
movq %rdx, 0x78(%rcx)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x80(%rcx)
movabsq $0x7ffd000000000000, %rax # imm = 0x7FFD000000000000
movq %rax, 0x88(%rcx)
movl $0xffffffff, 0xa0(%rcx) # imm = 0xFFFFFFFF
movq %rdx, 0x18a8(%rcx)
movl %edx, 0x38c8(%rcx)
movb %dl, 0x38cc(%rcx)
movb %dl, 0x38ce(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18b8(%rcx)
movq 0x120(%r15), %rax
movq %rax, 0x8(%rcx)
movq %rcx, 0x120(%r15)
cmpq %rdx, 0x80(%r13)
jne 0x10379
movq %r15, %rdi
movq %r13, %rsi
callq 0x91b6
movq 0x80(%r13), %rax
movq 0x38(%rax), %rdi
movq %r15, %rsi
callq 0x858c
movl 0x28(%r13), %eax
movl %eax, 0x14(%rsp)
movl 0x48(%r13), %eax
movl %eax, 0x10(%rsp)
movl 0x58(%r13), %eax
movl %eax, 0xc(%rsp)
leaq 0x48(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x80(%r13), %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x18(%rax)
leaq 0x68(%rsp), %rbx
movq %rax, 0x18c0(%rbx)
movq %rbx, %rdi
callq 0x10b76
movq %rbx, %rdi
callq 0x10b76
movq %rbx, %rdi
callq 0x11e66
cmpl $0x2d, 0x50(%rbx)
jne 0x10451
leaq 0x68(%rsp), %rdi
callq 0x10b76
cmpq $0x0, 0x10(%r13)
je 0x10406
leaq 0xd9c0(%rip), %rsi      # 0x1ddc4
jmp 0x10445
leaq 0xd9d4(%rip), %rdx      # 0x1dde1
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movl $0x47, %esi
callq 0x11171
movq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
movq %r15, %rdi
callq 0x7fc8
movq %rax, 0x10(%r13)
movq %rbx, %rdi
callq 0x11f83
testb %al, %al
jne 0x10451
leaq 0xe0d4(%rip), %rsi      # 0x1e519
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
callq 0x110cb
movq %r15, 0x18(%rsp)
cmpl $0x1, 0xb8(%rsp)
jne 0x1055c
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x10b76
movq %rbx, %rdi
callq 0x111d2
cmpl $0x0, 0x38c8(%rbx)
jle 0x104ea
leaq 0x194c(%rsp), %r15
leaq 0x68(%rsp), %rbx
xorl %r12d, %r12d
movq -0xc(%r15), %r14
movl -0x4(%r15), %ebp
movq %r13, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xa54c
cmpl $-0x1, %eax
je 0x104bc
movq -0x14(%r15), %rcx
movslq -0x1c(%r15), %rdx
movq (%rcx), %rcx
movb %al, (%rcx,%rdx)
jmp 0x104d6
movb $0x0, 0xf9(%rsp)
movl (%r15), %esi
movq %rbx, %rdi
movl %ebp, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x111f8
incq %r12
movslq 0x3930(%rsp), %rax
addq $0x20, %r15
cmpq %rax, %r12
jl 0x10492
movq 0x70(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, 0x120(%rcx)
xorl %eax, %eax
cmpb $0x0, 0xf8(%rsp)
je 0x1054a
movl 0x14(%rsp), %eax
movl %eax, 0x38(%r13)
movl %eax, 0x28(%r13)
movl 0x10(%rsp), %eax
movl %eax, 0x48(%r13)
movl 0xc(%rsp), %eax
movl %eax, 0x58(%r13)
movq 0x100(%rsp), %rax
testq %rax, %rax
je 0x10545
cmpb $0x1, 0x1(%rax)
jne 0x10545
movl $0x1, %eax
cmpb $0x0, 0xf9(%rsp)
jne 0x1054a
movl $0x2, %eax
addq $0x3938, %rsp           # imm = 0x3938
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68(%rsp), %r14
movl $0xff, %ebp
movq %r13, 0x20(%rsp)
movb $0x0, 0x3936(%rsp)
cmpl $0x2e, 0xb8(%rsp)
jne 0x10818
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
movl $0x47, %esi
leaq 0xdfe1(%rip), %rdx      # 0x1e579
callq 0x11171
movq 0xa0(%rsp), %rbx
movl 0xa8(%rsp), %ebp
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x116bc
testl %eax, %eax
je 0x105d3
movq %r14, %rdi
leaq 0xd912(%rip), %rsi      # 0x1ded9
movl %ebp, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110cb
movq 0x68(%rsp), %rdi
movq 0x1918(%rsp), %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x94aa
movq %rax, %r13
movq 0x20(%rax), %rax
movl $0x0, 0x20(%rax)
movq 0x1918(%rsp), %rax
movl 0x48(%rax), %ebp
decl %ebp
cmpl $0x100, %ebp            # imm = 0x100
jne 0x10623
movq %r14, %rdi
leaq 0xd8eb(%rip), %rsi      # 0x1df02
movl $0x100, %edx            # imm = 0x100
xorl %eax, %eax
callq 0x110cb
movq 0x1918(%rsp), %rax
movl 0x58(%rax), %ebx
decl %ebx
cmpl $0xff, %ebx
jne 0x1064e
movq %r14, %rdi
leaq 0xdf4d(%rip), %rsi      # 0x1e58f
movl $0xff, %edx
xorl %eax, %eax
callq 0x110cb
movq 0x1918(%rsp), %rcx
movq 0x1928(%rsp), %rax
movq 0x8(%rax), %rdx
cmpq 0x80(%rcx), %rdx
jne 0x10b67
movq 0x20(%r13), %rcx
movq %rax, 0x40(%rsp)
movq %rcx, 0x30(%rsp)
movl 0x108(%rsp), %eax
movl %eax, 0x28(%rsp)
movl %ebp, 0x38(%rsp)
leaq 0x28(%rsp), %rcx
movq %rcx, 0x1928(%rsp)
incl %eax
movl %eax, 0x108(%rsp)
movq %r14, %rdi
movl $0x8, %esi
callq 0x1135b
movq %r14, %rdi
xorl %esi, %esi
callq 0x113b1
movq %r14, %rdi
movl %ebx, %esi
callq 0x1135b
movq %r14, %rdi
callq 0x11e66
movq %r13, %rbx
addq $0x28, %rbx
movl 0xb8(%rsp), %eax
cmpl $0x1, %eax
je 0x10831
cmpl $0x35, %eax
je 0x10831
movq %r14, %rbp
movq %r14, %rdi
movl $0x47, %esi
leaq 0xde7c(%rip), %rdx      # 0x1e579
callq 0x11171
movq 0x68(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
movq 0x1918(%rsp), %rdi
callq 0x93bb
movl %eax, 0x8(%rsp)
cmpl $0x0, 0x30(%r13)
je 0x107a2
movq 0x1918(%rsp), %rax
movq 0x60(%rax), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %r14
leaq 0x1c(%r14), %r12
xorl %r15d, %r15d
movq 0x1918(%rsp), %rax
movq 0x60(%rax), %rax
movq (%rbx), %rcx
movl (%rcx,%r15,4), %ecx
movq (%rax,%rcx,8), %rsi
movl 0x10(%r14), %eax
cmpl 0x10(%rsi), %eax
jne 0x10796
movl 0x14(%rsi), %edx
cmpl %edx, 0x14(%r14)
jne 0x10796
addq $0x1c, %rsi
movq %r12, %rdi
callq 0x22d0
testl %eax, %eax
jne 0x10796
movq %rbp, %rdi
leaq 0xde39(%rip), %rsi      # 0x1e5c5
movq %r12, %rdx
xorl %eax, %eax
callq 0x110cb
incq %r15
movl 0x30(%r13), %eax
cmpq %rax, %r15
jb 0x10749
movq 0x68(%rsp), %rsi
movq %rbx, %rdi
movl 0x8(%rsp), %edx
callq 0x851b
movq %rbp, %r14
movq %rbp, %rdi
movl $0x1a, %esi
leaq 0xde2b(%rip), %rdx      # 0x1e5f0
callq 0x11171
movq %rbp, %rdi
movl $0x1, %esi
callq 0x12323
movq %rbp, %rdi
callq 0x11f83
testb %al, %al
jne 0x107f4
movq %r14, %rdi
leaq 0xdd2c(%rip), %rsi      # 0x1e519
xorl %eax, %eax
callq 0x110cb
movq %r14, %rdi
movl $0xb, %esi
callq 0x1135b
movq %r14, %rdi
xorl %esi, %esi
callq 0x113b1
movq %r14, %rdi
callq 0x11e66
jmp 0x106d2
cmpl $0x33, 0xb8(%rsp)
jne 0x10875
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
xorl %esi, %esi
jmp 0x1088f
movq %r14, %rdi
movl $0x35, %esi
leaq 0xddd9(%rip), %rdx      # 0x1e619
callq 0x11171
movq %r14, %rdi
callq 0x11986
movq %r14, %rdi
callq 0x111d2
movq 0x1928(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x1928(%rsp)
movq 0x20(%rsp), %r13
movl $0xff, %ebp
jmp 0x10894
cmpl $0x32, 0xb8(%rsp)
jne 0x108af
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
movl $0x1, %esi
callq 0x113e6
movq %r14, %rdi
callq 0x11e66
cmpl $0x1, 0xb8(%rsp)
jne 0x1056b
jmp 0x10464
cmpl $0x2f, 0xb8(%rsp)
jne 0x10a2b
movq %r14, %rdi
callq 0x10b76
cmpl $-0x1, 0x108(%rsp)
jne 0x10b6c
movq %r14, %rdi
callq 0x12b40
movq %rax, %rbx
movq %r14, %rdi
movl $0x30, %esi
leaq 0xdd69(%rip), %rdx      # 0x1e656
callq 0x11171
cmpl $0x17, 0xb8(%rsp)
jne 0x10930
movq %r14, %rdi
callq 0x10b76
testq %rbx, %rbx
je 0x10914
movq %r14, %rdi
movq %rbx, %rsi
callq 0x12f79
movq %r14, %rdi
movl $0x25, %esi
callq 0x1135b
movq %r14, %rdi
xorl %esi, %esi
callq 0x113b1
jmp 0x10b16
movq %r14, %rdi
movl $0x47, %esi
leaq 0xdd32(%rip), %rdx      # 0x1e671
callq 0x11171
movq 0x68(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
movq 0x1918(%rsp), %rdi
callq 0x93bb
movl %eax, %ebx
movq %r14, %rdi
movl $0x31, %esi
callq 0x1135b
movq %r14, %rdi
xorl %esi, %esi
callq 0x113b1
movl %ebx, %esi
shrl $0x8, %esi
andl %ebp, %esi
movq %r14, %rdi
callq 0x1135b
andl %ebp, %ebx
movq %r14, %rdi
movl %ebx, %esi
callq 0x1135b
cmpl $0x31, 0xb8(%rsp)
jne 0x109bf
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
movl $0x47, %esi
leaq 0xdcd2(%rip), %rdx      # 0x1e68c
callq 0x11171
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
movl 0xac(%rsp), %esi
movq %r14, %rdi
callq 0x13056
cmpl $-0x1, %eax
je 0x109f1
movq %r14, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x123e6
movq %r14, %rdi
movl $0x25, %esi
callq 0x1135b
movq %r14, %rdi
xorl %esi, %esi
callq 0x113b1
cmpl $0x5, 0xb8(%rsp)
jne 0x10914
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
callq 0x11e66
jmp 0x10930
cmpl $0x30, 0xb8(%rsp)
jne 0x10b32
movq %r14, %rdi
callq 0x10b76
cmpl $-0x1, 0x108(%rsp)
je 0x10a60
jmp 0x10b71
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
callq 0x11e66
movq %r14, %rdi
callq 0x12b40
movq %rax, %rbx
cmpl $0x31, 0xb8(%rsp)
jne 0x10aa2
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
movl $0x47, %esi
leaq 0xdc00(%rip), %rdx      # 0x1e68c
callq 0x11171
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
jmp 0x10abc
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x10acb
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
je 0x10acb
movl 0x14(%rdx), %ecx
addq $0x1c, %rdx
movl 0xac(%rsp), %esi
movq %r14, %rdi
callq 0x13056
cmpl $-0x1, %eax
je 0x10ae1
movq %r14, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x123e6
jmp 0x10af1
testq %rbx, %rbx
je 0x10af1
movq %r14, %rdi
movq %rbx, %rsi
callq 0x12f79
movq %r14, %rdi
movl $0x25, %esi
callq 0x1135b
movq %r14, %rdi
xorl %esi, %esi
callq 0x113b1
cmpl $0x5, 0xb8(%rsp)
je 0x10a50
movq %r14, %rdi
callq 0x11f83
testb %al, %al
jne 0x10894
movq %r14, %rdi
leaq 0xd9e9(%rip), %rsi      # 0x1e519
jmp 0x10b4e
cmpl $0x2d, 0xb8(%rsp)
jne 0x10b5a
movq %r14, %rdi
callq 0x10b76
movq %r14, %rdi
leaq 0xd9f4(%rip), %rsi      # 0x1e542
xorl %eax, %eax
callq 0x110cb
jmp 0x10894
movq %r14, %rdi
callq 0x11a11
jmp 0x10894
callq 0x4b6f
callq 0x4b39
callq 0x4b03