// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_compiler.c
// Function: compilerInit
void compilerInit(Compiler *comp, const char *fileName, const char *sourceString, int stackSize, int argc, char **argv, bool fileSystemEnabled, bool implLibsEnabled)
{
#ifdef _WIN32
    comp->originalInputCodepage = GetConsoleCP();
    comp->originalOutputCodepage = GetConsoleOutputCP();
    SetConsoleCP(CP_UTF8);
    SetConsoleOutputCP(CP_UTF8);
#endif

    compilerSetAPI(comp);

    storageInit  (&comp->storage, &comp->error);
    moduleInit   (&comp->modules, &comp->storage, implLibsEnabled, &comp->error);
    blocksInit   (&comp->blocks, &comp->error);
    externalInit (&comp->externals, &comp->storage);
    typeInit     (&comp->types, &comp->storage, &comp->error);
    identInit    (&comp->idents, &comp->storage, &comp->debug, &comp->error);
    constInit    (&comp->consts, &comp->error);
    genInit      (&comp->gen, &comp->storage, &comp->debug, &comp->error);
    vmInit       (&comp->vm, &comp->storage, stackSize, fileSystemEnabled, &comp->error);

    vmReset(&comp->vm, comp->gen.code, comp->gen.debugPerInstr);

    comp->lex.fileName = "<unknown>";
    comp->lex.tok.line = 1;
    comp->lex.tok.pos = 1;
    comp->debug.fnName = "<unknown>";

    char filePath[DEFAULT_STR_LEN + 1] = "";
    moduleAssertRegularizePath(&comp->modules, fileName, comp->modules.curFolder, filePath, DEFAULT_STR_LEN + 1);

    comp->lex.fileName = filePath;

    lexInit(&comp->lex, &comp->storage, &comp->debug, filePath, sourceString, false, &comp->error);

    comp->argc  = argc;
    comp->argv  = argv;

    comp->blocks.module = moduleAdd(&comp->modules, "#universe");

    compilerDeclareBuiltinTypes (comp);
    compilerDeclareBuiltinIdents(comp);
    compilerDeclareExternalFuncs(comp, fileSystemEnabled);

    // Command-line-arguments
    Type *argvType = typeAdd(&comp->types, &comp->blocks, TYPE_ARRAY);
    argvType->base = comp->strType;
    typeResizeArray(argvType, comp->argc);

    Ident *rtlargv = identAllocVar(&comp->idents, &comp->types, &comp->modules, &comp->blocks, "rtlargv", argvType, true);
    char **argArray = (char **)rtlargv->ptr;

    for (int i = 0; i < comp->argc; i++)
    {
        argArray[i] = storageAddStr(&comp->storage, strlen(comp->argv[i]));
        strcpy(argArray[i], comp->argv[i]);
    }

    // Embedded standard library modules
    const int numRuntimeModules = sizeof(runtimeModuleSources) / sizeof(runtimeModuleSources[0]);
    for (int i = 0; i < numRuntimeModules; i++)
    {
        char runtimeModulePath[DEFAULT_STR_LEN + 1] = "";
        moduleAssertRegularizePath(&comp->modules, runtimeModuleNames[i], comp->modules.curFolder, runtimeModulePath, DEFAULT_STR_LEN + 1);

        const bool runtimeModuleTrusted = strcmp(runtimeModuleNames[i], "std.um") == 0;
        moduleAddSource(&comp->modules, runtimeModulePath, runtimeModuleSources[i], runtimeModuleTrusted);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movl %ecx, -0x27c(%rbp)
movq %rdx, %rax
movl -0x27c(%rbp), %edx
movq %rax, -0x278(%rbp)
movq %rsi, %rax
movq -0x278(%rbp), %rsi
movq %rax, -0x270(%rbp)
movq %rdi, %r10
movq -0x270(%rbp), %rdi
movb 0x18(%rbp), %al
movb 0x10(%rbp), %cl
movq %r10, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
andb $0x1, %cl
movb %cl, -0x29(%rbp)
andb $0x1, %al
movb %al, -0x2a(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6750
movq -0x8(%rbp), %rdi
addq $0xf0, %rdi
movq -0x8(%rbp), %rsi
addq $0x50f8, %rsi           # imm = 0x50F8
callq 0x4be0
movq -0x8(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
movb -0x2a(%rbp), %al
movq -0x8(%rbp), %rcx
addq $0x50f8, %rcx           # imm = 0x50F8
andb $0x1, %al
movzbl %al, %edx
callq 0x4e70
movq -0x8(%rbp), %rdi
addq $0x4220, %rdi           # imm = 0x4220
movq -0x8(%rbp), %rsi
addq $0x50f8, %rsi           # imm = 0x50F8
callq 0x5e00
movq -0x8(%rbp), %rdi
addq $0x4b98, %rdi           # imm = 0x4B98
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
callq 0x6050
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
movq -0x8(%rbp), %rdx
addq $0x50f8, %rdx           # imm = 0x50F8
callq 0x21910
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
movq -0x8(%rbp), %rdx
addq $0x50e0, %rdx           # imm = 0x50E0
movq -0x8(%rbp), %rcx
addq $0x50f8, %rcx           # imm = 0x50F8
callq 0x19470
movq -0x8(%rbp), %rdi
addq $0x4e68, %rdi           # imm = 0x4E68
movq -0x8(%rbp), %rsi
addq $0x50f8, %rsi           # imm = 0x50F8
callq 0x8660
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
movq -0x8(%rbp), %rdx
addq $0x50e0, %rdx           # imm = 0x50E0
movq -0x8(%rbp), %rcx
addq $0x50f8, %rcx           # imm = 0x50F8
callq 0x16630
movq -0x8(%rbp), %rdi
addq $0x5058, %rdi           # imm = 0x5058
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
movl -0x1c(%rbp), %edx
movb -0x29(%rbp), %al
movq -0x8(%rbp), %r8
addq $0x50f8, %r8            # imm = 0x50F8
andb $0x1, %al
movzbl %al, %ecx
callq 0x24890
movq -0x8(%rbp), %rdi
addq $0x5058, %rdi           # imm = 0x5058
movq -0x8(%rbp), %rax
movq 0x4e70(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x5040(%rax), %rdx
callq 0x25790
movq -0x8(%rbp), %rax
leaq 0x1cdff4(%rip), %rcx    # 0x1d43b5
movq %rcx, 0x4ba8(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x4ce0(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x4ce4(%rax)
movq -0x8(%rbp), %rax
leaq 0x1cdfc6(%rip), %rcx    # 0x1d43b5
movq %rcx, 0x50e8(%rax)
leaq -0x130(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3190
movq -0x8(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0x100, %rdx            # imm = 0x100
addq $0x4008, %rdx           # imm = 0x4008
leaq -0x130(%rbp), %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x5d90
leaq -0x130(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x4ba8(%rax)
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movq -0x8(%rbp), %rsi
addq $0xf0, %rsi
movq -0x8(%rbp), %rdx
addq $0x50e0, %rdx           # imm = 0x50E0
leaq -0x130(%rbp), %rcx
movq -0x18(%rbp), %r8
movq -0x8(%rbp), %rax
addq $0x50f8, %rax           # imm = 0x50F8
xorl %r9d, %r9d
movq %rax, (%rsp)
callq 0x1a9e0
movl -0x20(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x52b8(%rax)
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x52c0(%rax)
movq -0x8(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0x1cdf01(%rip), %rsi    # 0x1d43bf
callq 0x5400
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x4b88(%rax)
movq -0x8(%rbp), %rdi
callq 0x6950
movq -0x8(%rbp), %rdi
callq 0x6e90
movq -0x8(%rbp), %rdi
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x7d70
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rsi
addq $0x4220, %rsi           # imm = 0x4220
movl $0x12, %edx
callq 0x21950
movq %rax, -0x138(%rbp)
movq -0x8(%rbp), %rax
movq 0x5288(%rax), %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x138(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x52b8(%rax), %esi
callq 0x8120
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0x4e18, %rsi           # imm = 0x4E18
movq -0x8(%rbp), %rdx
addq $0x100, %rdx            # imm = 0x100
movq -0x8(%rbp), %rcx
addq $0x4220, %rcx           # imm = 0x4220
movq -0x138(%rbp), %r9
leaq 0x1cde4a(%rip), %r8     # 0x1d43c9
movl $0x1, (%rsp)
callq 0x1a450
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq 0x120(%rax), %rax
movq %rax, -0x148(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x52b8(%rcx), %eax
jge 0x6658
movq -0x8(%rbp), %rax
addq $0xf0, %rax
movq %rax, -0x288(%rbp)
movq -0x8(%rbp), %rax
movq 0x52c0(%rax), %rax
movslq -0x14c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3120
movq -0x288(%rbp), %rdi
movq %rax, %rsi
callq 0x4c60
movq %rax, %rdx
movq -0x148(%rbp), %rax
movslq -0x14c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x148(%rbp), %rax
movslq -0x14c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x8(%rbp), %rax
movq 0x52c0(%rax), %rax
movslq -0x14c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x30a0
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x65b1
movl $0x4, -0x150(%rbp)
movl $0x0, -0x154(%rbp)
cmpl $0x4, -0x154(%rbp)
jge 0x6741
leaq -0x260(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3190
movq -0x8(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
movslq -0x154(%rbp), %rcx
leaq 0x1dfb5b(%rip), %rax    # 0x1e6200
movq (%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rdx
addq $0x100, %rdx            # imm = 0x100
addq $0x4008, %rdx           # imm = 0x4008
leaq -0x260(%rbp), %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x5d90
movslq -0x154(%rbp), %rcx
leaq 0x1dfb25(%rip), %rax    # 0x1e6200
movq (%rax,%rcx,8), %rdi
leaq 0x1cdceb(%rip), %rsi    # 0x1d43d1
callq 0x3220
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movb %al, -0x261(%rbp)
movq -0x8(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq -0x260(%rbp), %rsi
movslq -0x154(%rbp), %rcx
leaq 0x1dfb07(%rip), %rax    # 0x1e6220
movq (%rax,%rcx,8), %rdx
movb -0x261(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x5910
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x666c
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
nopw (%rax,%rax)