// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x3958, %rsp           # imm = 0x3958
movq %rdi, 0x3948(%rsp)
movq %rsi, 0x3940(%rsp)
movq %rdx, 0x3938(%rsp)
movq %rcx, 0x3930(%rsp)
movq 0x3938(%rsp), %rdi
leaq 0x15afa(%rip), %rsi     # 0x2af50
movl $0x3, %edx
callq 0x2080
cmpl $0x0, %eax
jne 0x15479
movq 0x3938(%rsp), %rax
addq $0x3, %rax
movq %rax, 0x3938(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq 0x3948(%rsp), %rsi
movq 0x3938(%rsp), %rdx
movq 0x3940(%rsp), %rcx
movq 0x3930(%rsp), %r8
callq 0x15820
movq 0x3948(%rsp), %rax
movq 0x120(%rax), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x58(%rsp), %rcx
movq 0x3948(%rsp), %rax
movq %rcx, 0x120(%rax)
movq 0x3940(%rsp), %rax
cmpq $0x0, 0x80(%rax)
jne 0x15500
movq 0x3948(%rsp), %rdi
movq 0x3940(%rsp), %rsi
callq 0x4890
movq 0x3940(%rsp), %rax
movq 0x80(%rax), %rax
movq 0x38(%rax), %rdi
movq 0x3948(%rsp), %rsi
callq 0x3100
movq 0x3940(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x54(%rsp)
movq 0x3940(%rsp), %rax
movl 0x48(%rax), %eax
movl %eax, 0x50(%rsp)
movq 0x3940(%rsp), %rax
movl 0x58(%rax), %eax
movl %eax, 0x4c(%rsp)
movl $0xfffffffe, 0x28(%rsp) # imm = 0xFFFFFFFE
movq 0x3940(%rsp), %rax
movq 0x80(%rax), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x58(%rsp), %rax
leaq 0x28(%rsp), %rcx
movq %rcx, 0x18c0(%rax)
movq 0x58(%rsp), %rdi
callq 0x15990
movq 0x58(%rsp), %rdi
callq 0x15990
movq 0x58(%rsp), %rdi
callq 0x15f40
movq 0x58(%rsp), %rdi
movl $0x2d, %esi
callq 0x15f50
testb $0x1, %al
jne 0x155b9
jmp 0x1563e
movq 0x3940(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x155dd
movq 0x58(%rsp), %rdi
leaq 0x15980(%rip), %rsi     # 0x2af54
movb $0x0, %al
callq 0x15f90
jmp 0x1563c
movq 0x58(%rsp), %rdi
movl $0x47, %esi
leaq 0x15983(%rip), %rdx     # 0x2af71
callq 0x160a0
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x3948(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x1c(%rsp), %edx
callq 0x2760
movq %rax, %rcx
movq 0x3940(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x58(%rsp), %rdi
callq 0x16110
jmp 0x1563e
jmp 0x15640
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x15f50
xorb $-0x1, %al
testb $0x1, %al
jne 0x15657
jmp 0x1566d
movq 0x58(%rsp), %rdi
callq 0x16140
movq 0x58(%rsp), %rdi
callq 0x15f40
jmp 0x15640
movq 0x58(%rsp), %rdi
callq 0x16240
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x38c8(%rcx), %eax
jge 0x1574a
movq 0x58(%rsp), %rax
addq $0x18c8, %rax           # imm = 0x18C8
movslq 0x18(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x4(%rsp)
movq 0x3940(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
callq 0x69a0
movl %eax, (%rsp)
cmpl $-0x1, (%rsp)
je 0x15708
movq 0x58(%rsp), %rdi
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rsp), %rax
movl (%rax), %edx
movl (%rsp), %ecx
callq 0x16270
jmp 0x15738
movq 0x58(%rsp), %rax
movb $0x0, 0x91(%rax)
movq 0x58(%rsp), %rdi
movq 0x10(%rsp), %rax
movl 0x1c(%rax), %esi
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r8
leaq 0x15860(%rip), %rdx     # 0x2af91
movb $0x0, %al
callq 0x162a0
jmp 0x1573a
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x1567f
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x3948(%rsp), %rax
movq %rcx, 0x120(%rax)
movq 0x58(%rsp), %rax
testb $0x1, 0x90(%rax)
je 0x157a8
movl 0x54(%rsp), %ecx
movq 0x3940(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x3940(%rsp), %rax
movl %ecx, 0x28(%rax)
movl 0x50(%rsp), %ecx
movq 0x3940(%rsp), %rax
movl %ecx, 0x48(%rax)
movl 0x4c(%rsp), %ecx
movq 0x3940(%rsp), %rax
movl %ecx, 0x58(%rax)
movq 0x58(%rsp), %rax
testb $0x1, 0x90(%rax)
je 0x157ff
movq 0x58(%rsp), %rax
cmpq $0x0, 0x98(%rax)
je 0x157f2
movq 0x58(%rsp), %rax
movq 0x98(%rax), %rax
testb $0x1, 0x1(%rax)
je 0x157f2
movq 0x58(%rsp), %rax
testb $0x1, 0x91(%rax)
je 0x157f2
movl $0x1, 0x3954(%rsp)
jmp 0x1580a
movl $0x2, 0x3954(%rsp)
jmp 0x1580a
movl $0x0, 0x3954(%rsp)
movl 0x3954(%rsp), %eax
addq $0x3958, %rsp           # imm = 0x3958
retq
nopl (%rax)