aboutsummaryrefslogtreecommitdiff
path: root/src/compiler.c
blob: 8c3266c0373e17181f2449e598c7a95ef7c9a9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include "../include/compiler.h"
#include "../include/parser.h"
#include "../include/ssa/ssa.h"
#include "../include/bytecode/bytecode.h"
#include "../include/std/log.h"
#include "../include/std/mem.h"
#include "../include/std/hash.h"
#include "../include/std/file.h"
#include "../include/std/alloc.h"
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>

static void amal_compiler_deinit(amal_compiler *self);

static usize strnlen(const char *str, usize max_length) {
    usize len;
    len = 0;
    while(len < max_length && *str != '\0') {
        ++len;
        ++str;
    }
    return len;
}

/* TODO: Allow to specify size and members? */
static CHECK_RESULT int create_default_type(amal_compiler *compiler, const char *name, LhsExpr **lhs_expr) {
    StructDecl *struct_decl;
    Ast *expr;

    return_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(StructDecl), (void**)&struct_decl));
    return_if_error(structdecl_init(struct_decl, &compiler->root_scope, &compiler->allocator));

    return_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(LhsExpr), (void**)lhs_expr));
    lhsexpr_init(*lhs_expr, bool_true, bool_true, bool_true, create_buffer_view(name, strnlen(name, PATH_MAX)));
    return_if_error(ast_create(&compiler->allocator, struct_decl, AST_STRUCT_DECL, &(*lhs_expr)->rhs_expr));
    return_if_error(ast_create(&compiler->allocator, *lhs_expr, AST_LHS, &expr));
    expr->resolve_data.type = *lhs_expr;
    expr->resolve_data.status = AST_RESOLVED;
    return scope_add_child(&compiler->root_scope, expr);
}

static CHECK_RESULT int init_default_types(amal_compiler *compiler) {
    return_if_error(create_default_type(compiler, "i8", (LhsExpr**)&compiler->default_types.i8));
    return_if_error(create_default_type(compiler, "i16", (LhsExpr**)&compiler->default_types.i16));
    return_if_error(create_default_type(compiler, "i32", (LhsExpr**)&compiler->default_types.i32));
    return_if_error(create_default_type(compiler, "i64", (LhsExpr**)&compiler->default_types.i64));
    return_if_error(create_default_type(compiler, "u8", (LhsExpr**)&compiler->default_types.u8));
    return_if_error(create_default_type(compiler, "u16", (LhsExpr**)&compiler->default_types.u16));
    return_if_error(create_default_type(compiler, "u32", (LhsExpr**)&compiler->default_types.u32));
    return_if_error(create_default_type(compiler, "u64", (LhsExpr**)&compiler->default_types.u64));
    return_if_error(create_default_type(compiler, "isize", (LhsExpr**)&compiler->default_types.isize));
    return_if_error(create_default_type(compiler, "usize", (LhsExpr**)&compiler->default_types.usize));
    return_if_error(create_default_type(compiler, "f32", (LhsExpr**)&compiler->default_types.f32));
    return_if_error(create_default_type(compiler, "f64", (LhsExpr**)&compiler->default_types.f64));
    return_if_error(create_default_type(compiler, "str", (LhsExpr**)&compiler->default_types.str));

    compiler->default_types.arithmetic_types[0] = compiler->default_types.i8;
    compiler->default_types.arithmetic_types[1] = compiler->default_types.u8;
    compiler->default_types.arithmetic_types[2] = compiler->default_types.i16;
    compiler->default_types.arithmetic_types[3] = compiler->default_types.u16;
    compiler->default_types.arithmetic_types[4] = compiler->default_types.i32;
    compiler->default_types.arithmetic_types[5] = compiler->default_types.u32;
    compiler->default_types.arithmetic_types[6] = compiler->default_types.i64;
    compiler->default_types.arithmetic_types[7] = compiler->default_types.u64;
    compiler->default_types.arithmetic_types[8] = compiler->default_types.isize;
    compiler->default_types.arithmetic_types[9] = compiler->default_types.usize;

    compiler->default_types.i8->is_signed = bool_true;
    compiler->default_types.u8->is_signed = bool_false;
    compiler->default_types.i16->is_signed = bool_true;
    compiler->default_types.u16->is_signed = bool_false;
    compiler->default_types.i32->is_signed = bool_true;
    compiler->default_types.u32->is_signed = bool_false;
    compiler->default_types.i64->is_signed = bool_true;
    compiler->default_types.u64->is_signed = bool_false;
    compiler->default_types.isize->is_signed = bool_true;
    compiler->default_types.usize->is_signed = bool_false;
    compiler->default_types.f32->is_signed = bool_true;
    compiler->default_types.f64->is_signed = bool_true;
    compiler->default_types.str->is_signed = bool_false;
    return 0;
}

bool is_arithmetic_type(LhsExpr *expr, amal_compiler *compiler) {
    usize i;
    const amal_default_types *default_types;
    default_types = &compiler->default_types;

    for(i = 0; i < NUM_ARITHMETIC_TYPES; ++i) {
        if(expr == (LhsExpr*)default_types->arithmetic_types[i])
            return bool_true;
    }
    return bool_false;
}

void amal_compiler_options_init(amal_compiler_options *self) {
    self->error_callback = NULL;
    self->error_callback_userdata = NULL;
    self->num_threads = 0;
}

static CHECK_RESULT int amal_compiler_init(amal_compiler *self, const amal_compiler_options *options, amal_program *program) {
    int i;

    self->usable_thread_count = options ? options->num_threads : 0;
    if(self->usable_thread_count == 0) {
        self->usable_thread_count = amal_get_usable_thread_count();
        if(self->usable_thread_count == 0) {
            amal_log_warning("Unable to get the number of threads available on the system, using 1 thread.");
            amal_log_warning("You can override the number of threads used by setting compiler option for number of thread to use.");
            self->usable_thread_count = 1;
        }
    }
    amal_log_info("Using %d threads", self->usable_thread_count);

    am_memset(&self->allocator, 0, sizeof(self->allocator));
    am_memset(&self->root_scope, 0, sizeof(self->root_scope));
    if(options)
        self->options = *options;
    else
        amal_compiler_options_init(&self->options);
    self->program = program;
    self->started = bool_false;
    self->generic_work_object_index = 0;
    amal_mutex_init(&self->mutex);

    return_if_error(arena_allocator_init(&self->allocator));
    cleanup_if_error(scope_init(&self->root_scope, NULL, &self->allocator));
    cleanup_if_error(buffer_init(&self->parsers, &self->allocator));
    cleanup_if_error(buffer_init(&self->queued_files, &self->allocator));
    cleanup_if_error(hash_map_init(&self->file_scopes, &self->allocator, sizeof(FileScopeReference*), hash_map_compare_string, amal_hash_string));
    cleanup_if_error(arena_allocator_alloc(&self->allocator,
                                            self->usable_thread_count * sizeof(ParserThreadData),
                                            (void**)&self->threads));
    for(i = 0; i < self->usable_thread_count; ++i)
        cleanup_if_error(parser_thread_data_init(&self->threads[i]));
    cleanup_if_error(init_default_types(self));
    return AMAL_COMPILER_OK;

    cleanup:
    amal_compiler_deinit(self);
    return AMAL_COMPILER_ERR;
}

void amal_compiler_deinit(amal_compiler *self) {
    int i;
    for(i = 0; i < self->usable_thread_count; ++i) {
        parser_thread_data_deinit(&self->threads[i]);
    }

    amal_mutex_deinit(&self->mutex);
    arena_allocator_deinit(&self->allocator);
}

typedef enum {
    THREAD_WORK_PARSE,
    THREAD_WORK_RESOLVE_AST,
    THREAD_WORK_GENERATE_SSA,
    THREAD_WORK_GENERATE_BYTECODE
} ThreadWorkType;

typedef struct {
    amal_compiler *compiler;
    ParserThreadData *parser_thread_data;
    FileScopeReference *file_scope;
} CompilerParserThreadUserData;

typedef struct {
    amal_compiler *compiler;
    ParserThreadData *parser_thread_data;
    Parser *parser;
    ThreadWorkType work_type;
} CompilerGenericThreadUserData;

typedef struct {
    union {
        FileScopeReference *file_scope;
        Parser *parser;
    } value;
    ThreadWorkType type;
} ThreadWorkData;

static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compiler, FileScopeReference *file_scope, ArenaAllocator *allocator) {
    Parser *parser;
    int result;
    BufferView filepath;
    result = AMAL_COMPILER_ERR;

    filepath = create_buffer_view(file_scope->canonical_path.data, file_scope->canonical_path.size);
    amal_log_info("Started parsing %.*s", filepath.size, filepath.data);
    return_if_error(arena_allocator_alloc(allocator, sizeof(Parser), (void**)&parser));
    return_if_error(parser_init(parser, compiler, allocator));
    file_scope->parser = parser;
    return_if_error(parser_parse_file(parser, filepath));
    cleanup_if_error(amal_mutex_lock(&compiler->mutex, "amal_compiler_load_in_this_thread"));
    cleanup_if_error(buffer_append(&compiler->parsers, &parser, sizeof(parser)));
    amal_log_info("Finished parsing %.*s", filepath.size, filepath.data);
    result = AMAL_COMPILER_OK;

    cleanup:
    amal_mutex_tryunlock(&compiler->mutex);
    return result;
}

/* TODO: Handle errors (stop parsing in all other threads and report errors/warnings) */
static void* thread_callback_parse_file(void *userdata) {
    FileScopeReference *file_scope;
    CompilerParserThreadUserData compiler_parser_userdata;
    void *result;
    assert(!amal_thread_is_main());
    
    am_memcpy(&compiler_parser_userdata, userdata, sizeof(compiler_parser_userdata));
    am_free(userdata);
    file_scope = compiler_parser_userdata.file_scope;
    result = (void*)AMAL_COMPILER_ERR;
    for(;;) {
        int has_next;
        cleanup_if_error(amal_compiler_load_in_this_thread(compiler_parser_userdata.compiler, 
                                                           file_scope,
                                                           &compiler_parser_userdata.parser_thread_data->allocator));
        cleanup_if_error(amal_mutex_lock(&compiler_parser_userdata.compiler->mutex, "thread_callback_parse_file"));
        has_next = buffer_pop(&compiler_parser_userdata.compiler->queued_files, &file_scope, sizeof(FileScopeReference*));
        amal_mutex_tryunlock(&compiler_parser_userdata.compiler->mutex);
        if(has_next != 0)
            break;
    }
    result = NULL;

    cleanup:
    /*
        To stop all other parsers from working cleanly, we simply clear the file queue,
        and the other threads will stop when they are done with the file they are currently parsing.
    */
    if(result != NULL) {
        ignore_result_int(amal_mutex_lock(&compiler_parser_userdata.compiler->mutex, "thread_callback_parse_file"));
        buffer_clear(&compiler_parser_userdata.compiler->queued_files);
    }
    /*
        There can be a data race between writing to this and when this is read in @amal_compiler_load_file_join_threads.
        This is intentional and it's ok, because the join_threads function checks against this status is guaranteed at this point
        to not be a certain value that it checks for. Writing to an int is atomic.
        TODO: Verify this is ok on all platforms.
    */
    compiler_parser_userdata.parser_thread_data->status = PARSER_THREAD_STATUS_IDLE;
    amal_mutex_tryunlock(&compiler_parser_userdata.compiler->mutex);
    return result;
}

static CHECK_RESULT int thread_resolve_ast(amal_compiler *compiler, Parser *parser) {
    AstCompilerContext compiler_context;
    int result;
    compiler_context.compiler = compiler;
    compiler_context.parser = parser;
    compiler_context.scope = NULL;
    result = setjmp(compiler_context.env);
    if(result == 0) {
        amal_log_debug("Resolving AST for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data);
        scope_resolve(&parser->struct_decl.body, &compiler_context);
    }
    return result;
}

static CHECK_RESULT int thread_generate_ssa(Parser *parser) {
    SsaCompilerContext compiler_context;
    int result;

    return_if_error(arena_allocator_alloc(parser->allocator, sizeof(Ssa), (void**)&compiler_context.ssa));
    return_if_error(ssa_init(compiler_context.ssa, parser->allocator));
    compiler_context.compiler = parser->compiler;
    parser->ssa = compiler_context.ssa;
    amal_log_debug("Generating SSA for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data);
    result = setjmp(compiler_context.env);

    if(result == 0)
        scope_generate_ssa(&parser->struct_decl.body, &compiler_context);

    return result;
}

static CHECK_RESULT int thread_generate_bytecode(Parser *parser) {
    BytecodeCompilerContext compiler_context;
    int result;

    return_if_error(bytecode_init(&compiler_context.bytecode, parser->allocator));
    compiler_context.parser = parser;
    amal_log_debug("Generating bytecode for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data);
    result = setjmp(compiler_context.env);

    if(result == 0) {
        generate_bytecode_from_ssa(&compiler_context);
        parser->bytecode = compiler_context.bytecode;
    }

    return result;
}

/* TODO: Handle errors (stop work in all other threads and report errors/warnings) */
static void* thread_callback_generic(void *userdata) {
    CompilerGenericThreadUserData compiler_userdata;
    Parser *parser;
    void *result;
    assert(!amal_thread_is_main());
    
    am_memcpy(&compiler_userdata, userdata, sizeof(compiler_userdata));
    am_free(userdata);
    parser = compiler_userdata.parser;
    result = (void*)AMAL_COMPILER_ERR;
    for(;;) {
        /* TODO: stop work in all other threads on failure */
        switch(compiler_userdata.work_type) {
            case THREAD_WORK_PARSE: {
                assert(bool_false && "Thread work type can't ge 'parse' for generic work");
                break;
            }
            case THREAD_WORK_RESOLVE_AST:
                cleanup_if_error(thread_resolve_ast(compiler_userdata.compiler, parser));
                break;
            case THREAD_WORK_GENERATE_SSA:
                cleanup_if_error(thread_generate_ssa(parser));
                break;
            case THREAD_WORK_GENERATE_BYTECODE:
                cleanup_if_error(thread_generate_bytecode(parser));
                break;
        }
        cleanup_if_error(amal_mutex_lock(&compiler_userdata.compiler->mutex, "thread_callback_generic"));
        if(compiler_userdata.compiler->generic_work_object_index + 1 >= (int)buffer_get_size(&compiler_userdata.compiler->parsers, Parser*))
            break;
        ++compiler_userdata.compiler->generic_work_object_index;
        parser = *(Parser**)buffer_get(&compiler_userdata.compiler->parsers, compiler_userdata.compiler->generic_work_object_index, sizeof(parser));
        amal_mutex_tryunlock(&compiler_userdata.compiler->mutex);
    }
    result = NULL;

    cleanup:
    /*
    To stop all other worker threads cleanly, we simply say we are done with all work in the queue,
    and the other threads will stop when they are done with the work they are currently working on.
    */
    if(result != NULL) {
        cleanup_if_error(amal_mutex_lock(&compiler_userdata.compiler->mutex, "thread_callback_generic"));
        compiler_userdata.compiler->generic_work_object_index = (int)buffer_get_size(&compiler_userdata.compiler->parsers, Parser*);
    }
    compiler_userdata.parser_thread_data->status = PARSER_THREAD_STATUS_IDLE;
    amal_mutex_tryunlock(&compiler_userdata.compiler->mutex);
    return result;
}

static CHECK_RESULT int amal_compiler_select_thread_for_work(amal_compiler *self, ThreadWorkData work_data, ParserThreadData **thread_selected) {
    int i;
    int result;
    ParserThreadData *parser_thread_data;
    void *thread_user_data;
    thread_user_data = NULL;
    *thread_selected = NULL;
    result = AMAL_COMPILER_OK;

    cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_select_thread_for_work"));
    for(i = 0; i < self->usable_thread_count; ++i) {
        parser_thread_data = &self->threads[i];
        if(parser_thread_data->status == PARSER_THREAD_STATUS_RUNNING)
            continue;

        switch(work_data.type) {
            case THREAD_WORK_PARSE: {
                CompilerParserThreadUserData *userdata;
                cleanup_if_error(am_malloc(sizeof(CompilerParserThreadUserData), (void**)&userdata));
                thread_user_data = userdata;
                userdata->compiler = self;
                userdata->parser_thread_data = parser_thread_data;
                userdata->file_scope = work_data.value.file_scope;
                result = parser_thread_data_start(parser_thread_data, thread_callback_parse_file, userdata);
                break;
            }
            case THREAD_WORK_RESOLVE_AST:
            case THREAD_WORK_GENERATE_SSA:
            case THREAD_WORK_GENERATE_BYTECODE: {
                CompilerGenericThreadUserData *userdata;
                cleanup_if_error(am_malloc(sizeof(CompilerGenericThreadUserData), (void**)&userdata));
                thread_user_data = userdata;
                userdata->compiler = self;
                userdata->parser_thread_data = parser_thread_data;
                userdata->parser = work_data.value.parser;
                userdata->work_type = work_data.type;
                ++self->generic_work_object_index;
                result = parser_thread_data_start(parser_thread_data, thread_callback_generic, userdata);
                break;
            }
        }
        *thread_selected = parser_thread_data;
        break;
    }

    cleanup:
    if(result != 0)
        am_free(thread_user_data);
    amal_mutex_tryunlock(&self->mutex);
    return result;
}

static CHECK_RESULT bool amal_compiler_check_all_threads_done(amal_compiler *self) {
    int i;
    bool result;
    result = bool_false;

    cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_check_all_threads_done"));
    for(i = 0; i < self->usable_thread_count; ++i) {
        ParserThreadData *parser_thread_data;
        parser_thread_data = &self->threads[i];
        if(parser_thread_data->status == PARSER_THREAD_STATUS_RUNNING) {
            goto cleanup;
        }
    }

    result = bool_true;
    cleanup:
    amal_mutex_tryunlock(&self->mutex);
    return result;
}

static CHECK_RESULT int amal_compiler_load_file_join_threads(amal_compiler *self) {
    int i;
    int result;
    void *thread_return_data;
    ParserThreadData *parser_thread_data;
    bool work_failed;

    assert(amal_thread_is_main());
    thread_return_data = NULL;
    work_failed = bool_false;
    for(;;) {
        bool done;
        /*
        Joining running threads. After checking one running thread another one might start up,
        so this is mostly to wait for threads to finish and to sleep without doing work.
        The check after that (amal_compiler_all_threads_done) check that all threads are done correctly
        */
        for(i = 0; i < self->usable_thread_count; ++i) {
            result = amal_mutex_lock(&self->mutex, "amal_compiler_load_file_join_threads, waiting for workers");
            parser_thread_data = &self->threads[i];
            amal_mutex_tryunlock(&self->mutex);
            if(result != 0)
                goto cleanup;
            /* TODO: Cleanup remaining threads if join fails */
            cleanup_if_error(parser_thread_data_join(parser_thread_data, &thread_return_data));
            if(thread_return_data != NULL) {
                /* TODO: Somehow exit running jobs */
                amal_log_error("Failed, waiting for jobs to finish");
                work_failed = bool_true;
            }
        }

        done = amal_compiler_check_all_threads_done(self);
        if(done)
            break;
    }

    result = AMAL_COMPILER_OK;
    cleanup:
    if(work_failed)
        result = AMAL_COMPILER_ERR;
    return result;
}

static CHECK_RESULT int amal_compiler_dispatch_generic(amal_compiler *self, ThreadWorkType work_type) {
    Parser **parser;
    Parser **parser_end;
    parser = buffer_begin(&self->parsers);
    parser_end = buffer_end(&self->parsers);
    self->generic_work_object_index = 0;
    for(; parser != parser_end; ++parser) {
        ParserThreadData *thread_selected;
        ThreadWorkData thread_work_data;
        thread_work_data.type = work_type;
        thread_work_data.value.parser = *parser;
        return_if_error(amal_compiler_select_thread_for_work(self, thread_work_data, &thread_selected));
        /* After all threads have been used, they will handle using the remaining parsers or stop if there is an error */
        if(!thread_selected)
            break;
    }
    return amal_compiler_load_file_join_threads(self);
}

static CHECK_RESULT int amal_compiler_generate_program(amal_compiler *self) {
    /*
        TODO: Copying the bytecode to the program can be done using multiple threads.
        Use self->threads for that.
    */
    Parser **parser;
    Parser **parser_end;
    parser = buffer_begin(&self->parsers);
    parser_end = buffer_end(&self->parsers);
    for(; parser != parser_end; ++parser) {
        return_if_error(amal_program_append_bytecode(self->program, &(*parser)->bytecode));
    }
    return 0;
}

static CHECK_RESULT int try_create_file_scope(amal_compiler *compiler, const char *filepath, FileScopeReference **file_scope, bool *new_entry) {
    int ret;
    char *result_path;
    usize result_path_size;
    BufferView path_view;

    ret = -1;
    result_path = NULL;
    *new_entry = bool_false;

    /* TODO: Optimize. No need to allocate everytime... */
    return_if_error(file_get_canonical_path(filepath, &result_path, &result_path_size));
    path_view = create_buffer_view(result_path, result_path_size);
    cleanup_if_error(amal_mutex_lock(&compiler->mutex, "try_create_file_scope"));
    if(!hash_map_get(&compiler->file_scopes, path_view, file_scope)) {
        cleanup_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(FileScopeReference), (void**)file_scope));
        /* @(*file_scope)->canonical_path won't change after this, so it's fine if allocator belongs to non-thread safe compiler instance */
        cleanup_if_error(file_scope_reference_init(*file_scope, path_view, &compiler->allocator));
        cleanup_if_error(hash_map_insert(&compiler->file_scopes, path_view, file_scope));
        *new_entry = bool_true;
    }

    ret = 0;
    cleanup:
    amal_mutex_tryunlock(&compiler->mutex);
    am_free(result_path);
    return ret;
}

int amal_compiler_load_file(amal_compiler_options *options, amal_program *program, const char *filepath) {
    amal_compiler compiler;
    FileScopeReference *file_scope;
    int result;
    assert(program);
    assert(filepath);
    return_if_error(amal_compiler_init(&compiler, options, program));
    result = amal_compiler_internal_load_file(&compiler, filepath, &file_scope);
    amal_compiler_deinit(&compiler);
    return result;
}

int amal_compiler_internal_load_file(amal_compiler *self, const char *filepath, FileScopeReference **file_scope) {
    int result;
    BufferView filepath_view;
    ParserThreadData *parser_thread_data;
    ThreadWorkData thread_work_data;
    bool main_job;
    bool new_entry;

    return_if_error(try_create_file_scope(self, filepath, file_scope, &new_entry));
    assert(file_scope && *file_scope && (*file_scope)->canonical_path.data);
    filepath_view = create_buffer_view((*file_scope)->canonical_path.data, (*file_scope)->canonical_path.size);
    if(!new_entry) {
        amal_log_info("amal_compiler_load_file: file already parsed: %.*s", filepath_view.size, filepath_view.data);
        return 0;
    }

    result = AMAL_COMPILER_ERR;
    thread_work_data.type = THREAD_WORK_PARSE;
    thread_work_data.value.file_scope = *file_scope;
    main_job = bool_false;

    /* The first time we get here, this will run single-threaded so this part doesn't need mutex */
    if(!self->started) {
        self->started = bool_true;
        main_job = bool_true;
    }

    return_if_error(amal_compiler_select_thread_for_work(self, thread_work_data, &parser_thread_data));

    if(main_job) {
        /*doc(Compiler flow)
            (Tokenize&parse -> Resolve AST -> Generate SSA -> Generate bytecode) -> Generate program\
            Each step except the last is done using multiple threads in parallel and the output of each step is used
            in the next step. The last step is not done in parallel because the last step is combining all bytecode
            and writing it to a file, which is an IO bottlenecked operation and it won't benefit from multithreading
            and may even lose performance because of it.
        */

        return_if_error(amal_compiler_load_file_join_threads(self));
        assert(amal_compiler_check_all_threads_done(self));
        amal_log_info("Finished parsing all files, resolving AST");

        return_if_error(amal_compiler_dispatch_generic(self, THREAD_WORK_RESOLVE_AST));
        assert(amal_compiler_check_all_threads_done(self));
        amal_log_info("Finished resolving AST, generating SSA");

        return_if_error(amal_compiler_dispatch_generic(self, THREAD_WORK_GENERATE_SSA));
        assert(amal_compiler_check_all_threads_done(self));
        amal_log_info("Finished generating SSA");
        
        return_if_error(amal_compiler_dispatch_generic(self, THREAD_WORK_GENERATE_BYTECODE));
        assert(amal_compiler_check_all_threads_done(self));
        amal_log_info("Finished generating bytecode");

        return_if_error(amal_compiler_generate_program(self));
        amal_log_info("Finished generating program");

        return AMAL_COMPILER_OK;
    }

    if(parser_thread_data)
        return AMAL_COMPILER_OK;

    cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_file"));
    cleanup_if_error(buffer_append(&self->queued_files, file_scope, sizeof(FileScopeReference*)));
    result = AMAL_COMPILER_OK;

    cleanup:
    amal_mutex_tryunlock(&self->mutex);
    return result;
}