git.haldean.org ubik / e2d2d42 libubik / codegen / compile.c
e2d2d42

Tree @e2d2d42 (Download .tar.gz)

compile.c @e2d2d42raw · history · blame

  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
612
613
614
615
616
617
618
619
620
621
622
/*
 * compile.c: ubik compilation
 * Copyright (C) 2016, Haldean Brown
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "ubik/adt.h"
#include "ubik/assert.h"
#include "ubik/assign.h"
#include "ubik/compile.h"
#include "ubik/feedback.h"
#include "ubik/gen.h"
#include "ubik/import.h"
#include "ubik/infer.h"
#include "ubik/interfaces.h"
#include "ubik/literate.h"
#include "ubik/hooks.h"
#include "ubik/parse.h"
#include "ubik/patterns.h"
#include "ubik/resolve.h"
#include "ubik/string.h"
#include "ubik/testing.h"
#include "ubik/typesystem.h"
#include "ubik/util.h"

#define dprint(...) do { if (cenv->debug) printf(__VA_ARGS__); } while (0)

/* Returns an error if not all imports for the given job are in the compiled
 * list in the provided environment. */
no_ignore static ubik_error
ensure_imports_ready(
        struct ubik_compile_env *cenv,
        struct ubik_compile_job *job);

no_ignore ubik_error
ubik_compile_env_default(
        struct ubik_compile_env *cenv,
        struct ubik_workspace *ws)
{
        char *scratch_dir;
        char *include_dirs;
        char *debug_var;
        bool debug;
        ubik_error err;

        ubik_alloc_start(&cenv->region);

        debug_var = getenv("UBIK_DEBUG");
        if (debug_var == NULL)
                debug = false;
        else
        {
                if (strcmp(debug_var, "0") == 0)
                        debug = false;
                else if (strcmp(debug_var, "no") == 0)
                        debug = false;
                else
                        debug = true;
        }
        cenv->debug = debug;

        ubik_ralloc((void **) &scratch_dir, 512, sizeof(char), &cenv->region);
        if (getcwd(scratch_dir, 500) == NULL)
        {
                perror("could not open current directory");
                return ubik_raise(ERR_UNEXPECTED_FAILURE, "getcwd");
        }
        strcat(scratch_dir, "/ubik-build");
        cenv->scratch_dir = scratch_dir;

        include_dirs = getenv("UBIK_INCLUDE");
        if (include_dirs == NULL)
        {
                cenv->include_dirs = NULL;
                cenv->n_include_dirs = 0;
        }
        else
        {
                err = ubik_string_split(
                        &cenv->include_dirs,
                        &cenv->n_include_dirs,
                        include_dirs,
                        strlen(include_dirs),
                        ':',
                        &cenv->region);
                if (err != OK)
                        return err;
        }

        cenv->to_compile.region = &cenv->region;
        cenv->compiled.region = &cenv->region;

        err = ubik_typesystem_init(&cenv->type_system, &cenv->region, ws);
        if (err != OK)
                return err;

        return OK;
}

static void
free_req(struct ubik_compile_request *req)
{
        ubik_alloc_free(&req->region);
        free(req);
}

/* Frees the parts of a job object that are uniquely owned by the job. This
 * doesn't free the request or the AST, as those are present in the request
 * holder as well. */
no_ignore ubik_error
ubik_compile_env_free(struct ubik_compile_env *cenv)
{
        struct ubik_compile_result *res;
        struct ubik_compile_job *job;
        size_t i;

        for (i = 0; i < cenv->compiled.n; i++)
        {
                res = cenv->compiled.elems[i];
                free_req(res->request);
        }

        for (i = 0; i < cenv->to_compile.n; i++)
        {
                job = cenv->to_compile.elems[i];
                free_req(job->request);
        }

        ubik_alloc_free(&cenv->region);

        return OK;
}

no_ignore static ubik_error
create_import_request(
        struct ubik_compile_request *res,
        struct ubik_compile_env *cenv,
        char *name,
        struct ubik_compile_request *parent,
        struct ubik_ast_loc *loc)
{
        struct dirent *test_f;
        struct ubik_stream in_stream;
        struct ubik_ast *test_ast;
        struct ubik_ast_loc err_loc = {0};
        DIR *test_dir;
        char *fullpath;
        size_t i;
        ubik_error err;

        err = OK;

        dprint("searching for source for \"%s\"\n", name);

        for (i = 0; i < cenv->n_include_dirs; i++)
        {
                dprint("\tchecking directory \"%s\"\n", cenv->include_dirs[i]);
                test_dir = opendir(cenv->include_dirs[i]);
                if (test_dir == NULL)
                {
                        perror("couldn't open include directory");
                        return ubik_raise(ERR_SYSTEM, "open include directory");
                }

                while ((test_f = readdir(test_dir)) != NULL)
                {
                        ubik_local_region(r);
                        dprint("\tchecking \"%s\"\n", test_f->d_name);

                        if (!ubik_string_endswith(test_f->d_name, ".uk"))
                                continue;

                        err = ubik_string_path_concat(
                                &fullpath, cenv->include_dirs[i],
                                test_f->d_name, &r);
                        if (err != OK)
                                return err;

                        err = ubik_stream_rfile(&in_stream, fullpath);
                        if (err != OK)
                                return err;

                        err = ubik_parse(
                                &test_ast, &r, NULL, fullpath, &in_stream);
                        if (err != OK)
                        {
                                err_loc.source_name = fullpath;
                                ubik_feedback_header(
                                        parent->feedback,
                                        UBIK_FEEDBACK_WARN,
                                        &err_loc,
                                        "skipping potential import, failed to "
                                        "parse file");
                                free(err);
                                continue;
                        }

                        if (strcmp(test_ast->package_name, name) == 0)
                        {
                                dprint("found source for %s at %s\n",
                                       name, test_f->d_name);
                                ubik_stream_reset(&in_stream);
                                ubik_alloc_start(&res->region);
                                res->source_name = ubik_strdup(
                                        fullpath, &res->region);
                                res->package_name = ubik_strdup(
                                        name, &res->region);
                                res->source = in_stream;
                                res->reason = LOAD_IMPORTED;
                                res->cb = NULL;
                                res->feedback = parent->feedback;
                                res->workspace = parent->workspace;

                                closedir(test_dir);
                                return OK;
                        }
                }

                closedir(test_dir);
        }

        ubik_feedback_line(
                parent->feedback, UBIK_FEEDBACK_ERR, loc,
                "could not find source for imported package %s", name);
        return ubik_raise(ERR_ABSENT, "couldn't find import source");
}

no_ignore static ubik_error
load_ast(struct ubik_compile_env *cenv, struct ubik_compile_job *job)
{
        ubik_error err;
        struct ubik_ast_import_list *import;
        struct ubik_compile_request *req;

        err = ubik_literate_weave(
                &job->woven, &job->request->source, job->request->source_name,
                &job->request->region);
        if (err != OK)
                return err;

        err = ubik_parse(
                &job->ast, &job->request->region, job->request->feedback,
                job->request->source_name, &job->woven);
        if (err != OK)
                return err;

        if (job->request->package_name == NULL)
                job->request->package_name = job->ast->package_name;

        import = job->ast->imports;
        job->status = COMPILE_READY;
        while (import != NULL)
        {
                job->status = COMPILE_WAIT_FOR_IMPORTS;
                req = calloc(1, sizeof(struct ubik_compile_request));
                err = create_import_request(
                        req, cenv, import->canonical, job->request,
                        &import->loc);
                if (err != OK)
                {
                        free(req);
                        return err;
                }
                err = ubik_compile_enqueue(cenv, req);
                free_req(req);
                if (err != OK)
                        return err;
                import = import->next;
        }

        dprint("AST loaded for %s, new status is %d\n",
               job->request->package_name, job->status);
        return OK;
}

no_ignore static ubik_error
compile_job(
        struct ubik_compile_env *cenv,
        struct ubik_compile_job *job)
{
        struct ubik_compile_result *res;
        local(infer_context) struct ubik_infer_context infer_ctx = {0};
        ubik_error err;

        dprint("compiling package %s\n", job->ast->package_name);

        err = ubik_import_add_all(cenv, job->ast, &job->request->region);
        if (err != OK)
                return err;
        if (cenv->debug)
        {
                printf("\nsplats\n");
                err = ubik_ast_print(job->ast);
                if (err != OK)
                        return err;
        }

        err = ubik_typesystem_load(
                cenv->type_system, job->ast, job->request);
        if (err != OK)
                return err;
        if (cenv->debug)
                ubik_typesystem_dump(cenv->type_system);

        err = ubik_resolve(job->ast, job->request);
        if (err != OK)
                return err;
        if (cenv->debug)
        {
                printf("\npre-resolve\n");
                err = ubik_ast_print(job->ast);
                if (err != OK)
                        return err;
        }

        /* Needs to happen after resolve, so that the package_names are set on
         * everything in the stack. */
        ubik_dbgsym_attach(job->ast, &job->request->region);

        infer_ctx.req = job->request;
        infer_ctx.debug = cenv->debug;
        infer_ctx.type_system = cenv->type_system;
        err = ubik_infer(job->ast, &infer_ctx);
        if (err != OK)
                return err;

        err = ubik_interfaces_compile_all(job->ast, job->request);
        if (err != OK)
                return err;
        if (cenv->debug)
        {
                printf("\ninterfaces\n");
                err = ubik_ast_print(job->ast);
                if (err != OK)
                        return err;
        }

        err = ubik_patterns_compile_all(job->ast, job->request);
        if (err != OK)
                return err;

        if (cenv->debug)
        {
                printf("\npatterns\n");
                err = ubik_ast_print(job->ast);
                if (err != OK)
                        return err;
        }

        err = ubik_adt_bind_all_to_ast(job->ast, job->request);
        if (err != OK)
                return err;

        if (cenv->debug)
        {
                printf("\nadt bindings\n");
                err = ubik_ast_print(job->ast);
                if (err != OK)
                        return err;
        }

        err = ubik_resolve(job->ast, job->request);
        if (err != OK)
                return err;
        if (cenv->debug)
        {
                printf("\npost-resolved\n");
                err = ubik_ast_print(job->ast);
                if (err != OK)
                        return err;
        }

        err = ubik_gen_graphs(job->ast, job->request);
        if (err != OK)
                return err;

        ubik_alloc1(&res, struct ubik_compile_result, &job->request->region);
        res->request = job->request;
        res->ast = job->ast;

        err = ubik_testing_run(res);
        if (err != OK)
                return err;

        if (job->request->cb != NULL)
        {
                err = job->request->cb(res);
                if (err != OK)
                        return err;
        }

        job->status = COMPILE_DONE;
        err = ubik_vector_append(&cenv->compiled, res);
        if (err != OK)
                return err;
        return OK;
}

/* Moves a job to the top of the request stack, along with all of its
 * dependencies. When we attempt to reenqueue a job that has already been
 * enqueued,  we need to pull it one to the top of the stack; if module A
 * depends on modules B and C, after loading A's AST you have:
 *
 *      [bottom] A B C [top]
 *
 * If, in the process of loading C's AST, we discover it depends on B, it is
 * not sufficient to leave the stack as-is, because it will lead to us
 * attempting to compile C before B, which will raise an error. It means we
 * need to bring B back up to the top, to make the stack:
 *
 *      [bottom] A C B [top]
 */
void
requeue(struct ubik_compile_env *cenv, size_t i)
{
        struct ubik_compile_job *job;
        struct ubik_compile_job *check;
        struct ubik_ast_import_list *import;
        size_t j;
        ubik_error err;

        job = cenv->to_compile.elems[i];
        for (j = i + 1; j < cenv->to_compile.n; j++)
                cenv->to_compile.elems[j - 1] = cenv->to_compile.elems[j];
        cenv->to_compile.elems[cenv->to_compile.n - 1] = job;

        /* If we haven't loaded the AST yet, we'll end up making sure the stack
         * is correct when we load its imports at some point in the future.
         * There's nothing we can do now. */
        if (job->status == COMPILE_WAIT_FOR_AST)
                return;

        /* If all of this job's imports are ready, then we don't have to do
         * anything to fix this. Otherwise, we need to requeue all of its
         * dependencies. */
        err = ensure_imports_ready(cenv, job);
        if (err == OK)
                return;
        free(err);

        import = job->ast->imports;
        while (import != NULL)
        {
                for (j = 0; j < cenv->to_compile.n; j++)
                {
                        check = cenv->to_compile.elems[j];
                        if (check->request->package_name == NULL)
                                continue;
                        if (strcmp(check->request->package_name,
                                   import->canonical))
                        {
                                requeue(cenv, j);
                                break;
                        }
                }
                import = import->next;
        }
}

no_ignore ubik_error
ubik_compile_enqueue(
        struct ubik_compile_env *cenv,
        struct ubik_compile_request *userreq)
{
        struct ubik_compile_request *req;
        struct ubik_compile_job *job;
        struct ubik_compile_job *check_job;
        struct ubik_compile_result *check_res;
        char *src;
        size_t i;

        src = userreq->source_name;
        for (i = 0; i < cenv->to_compile.n; i++)
        {
                check_job = (struct ubik_compile_job *)
                        cenv->to_compile.elems[i];
                if (strcmp(check_job->request->source_name, src) != 0)
                        continue;
                requeue(cenv, i);
                return OK;
        }
        for (i = 0; i < cenv->compiled.n; i++)
        {
                check_res = (struct ubik_compile_result *)
                        cenv->compiled.elems[i];
                if (strcmp(check_res->request->source_name, src) == 0)
                        return OK;
        }

        if (userreq->feedback == NULL)
                printf("warning: compilation feedback unavailable, no feedback "
                       "stream provided\n");
        ubik_assert(userreq->workspace != NULL);

        req = calloc(1, sizeof(struct ubik_compile_request));
        if (req == NULL)
                return ubik_raise(ERR_NO_MEMORY, "enqueue compilation request");
        ubik_alloc_start(&req->region);

        req->source_name = ubik_strdup(userreq->source_name, &req->region);
        if (userreq->package_name != NULL)
                req->package_name = ubik_strdup(
                        userreq->package_name, &req->region);
        else
                req->package_name = NULL;
        req->source = userreq->source;
        req->reason = userreq->reason;
        req->cb = userreq->cb;
        req->feedback = userreq->feedback;
        req->type_system = cenv->type_system;
        req->workspace = userreq->workspace;

        ubik_alloc1(&job, struct ubik_compile_job, &req->region);
        job->request = req;
        job->ast = NULL;
        job->status = COMPILE_WAIT_FOR_AST;

        return ubik_vector_append(&cenv->to_compile, job);
}

no_ignore static ubik_error
ensure_imports_ready(
        struct ubik_compile_env *cenv,
        struct ubik_compile_job *job)
{
        struct ubik_ast_import_list *import;
        struct ubik_compile_result *result;
        bool found;
        char *expect;
        char *check;
        size_t i;

        dprint("checking imports for %s\n", job->request->package_name);
        import = job->ast->imports;

        while (import != NULL)
        {
                expect = import->canonical;
                dprint("    checking %s\n", expect);
                result = NULL;
                found = false;
                for (i = 0; i < cenv->compiled.n; i++)
                {
                        result = cenv->compiled.elems[i];
                        check = result->request->package_name;
                        if (check == NULL)
                                continue;
                        if (strcmp(check, expect) == 0)
                        {
                                found = true;
                                break;
                        }
                }
                if (!found)
                        return ubik_raise(
                                ERR_UNEXPECTED_FAILURE,
                                "imports were not satisfied in compilation");
                import = import->next;
        }

        job->status = COMPILE_READY;
        return OK;
}

no_ignore ubik_error
ubik_compile_run(struct ubik_compile_env *cenv)
{
        struct ubik_compile_job *job;
        ubik_error err;

        while (cenv->to_compile.n > 0)
        {
                job = cenv->to_compile.elems[cenv->to_compile.n - 1];
                dprint("resume job for %s\n", job->request->source_name);

                switch (job->status)
                {
                case COMPILE_WAIT_FOR_AST:
                        err = load_ast(cenv, job);
                        if (err != OK)
                                return err;
                        break;

                case COMPILE_WAIT_FOR_IMPORTS:
                        err = ensure_imports_ready(cenv, job);
                        if (err != OK)
                                return err;
                        break;

                case COMPILE_READY:
                        err = compile_job(cenv, job);
                        if (err != OK)
                                return err;
                        break;

                case COMPILE_DONE:
                        cenv->to_compile.n--;
                        break;
                }
        }

        return OK;
}