main.c 18.9 KB
Newer Older
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
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

Damien's avatar
Damien committed
27
#include <stdint.h>
xbe's avatar
xbe committed
28
#include <stdbool.h>
Damien's avatar
Damien committed
29
30
#include <stdio.h>
#include <string.h>
Edd Barrett's avatar
Edd Barrett committed
31
#include <stdlib.h>
32
#include <stdarg.h>
33
#include <unistd.h>
34
#include <ctype.h>
xbe's avatar
xbe committed
35
36
#include <sys/stat.h>
#include <sys/types.h>
37
#include <errno.h>
Damien's avatar
Damien committed
38

39
#include "py/mpstate.h"
40
41
42
43
44
45
46
#include "py/nlr.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "py/repl.h"
#include "py/gc.h"
#include "py/stackctrl.h"
47
#include "genhdr/mpversion.h"
48
#include MICROPY_HAL_H
Dave Hylands's avatar
Dave Hylands committed
49
#include "input.h"
Damien's avatar
Damien committed
50

51
// Command line options, with their defaults
52
53
54
STATIC bool compile_only = false;
STATIC uint emit_opt = MP_EMIT_OPT_NONE;
mp_uint_t mp_verbose_flag = 0;
55

56
#if MICROPY_ENABLE_GC
57
// Heap size of GC heap (if enabled)
58
// Make it larger on a 64 bit machine, because pointers are larger.
59
long heap_size = 1024*1024 * (sizeof(mp_uint_t) / 4);
60
#endif
61

62
63
64
65
66
67
68
STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
    (void)env;
    fwrite(str, len, 1, stderr);
}

const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};

69
#define FORCED_EXIT (0x100)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// If exc is SystemExit, return value where FORCED_EXIT bit set,
// and lower 8 bits are SystemExit value. For all other exceptions,
// return 1.
STATIC int handle_uncaught_exception(mp_obj_t exc) {
    // check for SystemExit
    if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
        // None is an exit value of 0; an int is its value; anything else is 1
        mp_obj_t exit_val = mp_obj_exception_get_value(exc);
        mp_int_t val = 0;
        if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) {
            val = 1;
        }
        return FORCED_EXIT | (val & 255);
    }

    // Report all other exceptions
86
    mp_obj_print_exception(&mp_stderr_print, exc);
87
88
89
90
91
    return 1;
}

// Returns standard error codes: 0 for success, 1 for all other errors,
// except if FORCED_EXIT bit is set then script raised SystemExit and the
92
// value of the exit is in the lower 8 bits of the return value
93
STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
94
    if (lex == NULL) {
95
        printf("MemoryError: lexer could not allocate memory\n");
96
        return 1;
97
98
    }

99
    mp_hal_set_interrupt_char(CHAR_CTRL_C);
100

101
102
    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
103
104
105
106
107
108
109
110
        qstr source_name = lex->source_name;

        #if MICROPY_PY___FILE__
        if (input_kind == MP_PARSE_FILE_INPUT) {
            mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
        }
        #endif

111
        mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
112
113
114

        /*
        printf("----------------\n");
115
        mp_parse_node_print(parse_tree.root, 0);
116
117
118
        printf("----------------\n");
        */

119
        mp_obj_t module_fun = mp_compile(&parse_tree, source_name, emit_opt, is_repl);
120
121
122
123
124
125

        if (!compile_only) {
            // execute it
            mp_call_function_0(module_fun);
        }

126
        mp_hal_set_interrupt_char(-1);
127
        nlr_pop();
128
        return 0;
129

130
131
    } else {
        // uncaught exception
132
        mp_hal_set_interrupt_char(-1);
133
        return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
134
135
136
    }
}

Damien George's avatar
Damien George committed
137
138
139
#if MICROPY_USE_READLINE == 1
#include "lib/mp-readline/readline.h"
#else
140
STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien's avatar
Damien committed
141
142
    int l1 = strlen(s1);
    int l2 = strlen(s2);
143
    char *s = malloc(l1 + l2 + 2);
Damien's avatar
Damien committed
144
145
146
147
148
149
    memcpy(s, s1, l1);
    if (sep_char != 0) {
        s[l1] = sep_char;
        l1 += 1;
    }
    memcpy(s + l1, s2, l2);
150
    s[l1 + l2] = 0;
Damien's avatar
Damien committed
151
152
    return s;
}
Damien George's avatar
Damien George committed
153
#endif
Damien's avatar
Damien committed
154

155
STATIC int do_repl(void) {
156
    mp_hal_stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
157

Damien George's avatar
Damien George committed
158
159
160
161
162
163
164
    #if MICROPY_USE_READLINE == 1

    // use MicroPython supplied readline

    vstr_t line;
    vstr_init(&line, 16);
    for (;;) {
165
166
        mp_hal_stdio_mode_raw();

Damien George's avatar
Damien George committed
167
168
169
    input_restart:
        vstr_reset(&line);
        int ret = readline(&line, ">>> ");
170
        mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
Damien George's avatar
Damien George committed
171
172
173
174
175
176
177

        if (ret == CHAR_CTRL_D) {
            // EOF
            printf("\n");
            mp_hal_stdio_mode_orig();
            vstr_clear(&line);
            return 0;
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
        } else if (ret == CHAR_CTRL_E) {
            // paste mode
            mp_hal_stdout_tx_str("\npaste mode; CTRL-C to cancel, CTRL-D to finish\n=== ");
            vstr_reset(&line);
            for (;;) {
                char c = mp_hal_stdin_rx_chr();
                if (c == CHAR_CTRL_C) {
                    // cancel everything
                    mp_hal_stdout_tx_str("\n");
                    goto input_restart;
                } else if (c == CHAR_CTRL_D) {
                    // end of input
                    mp_hal_stdout_tx_str("\n");
                    break;
                } else {
                    // add char to buffer and echo
                    vstr_add_byte(&line, c);
                    if (c == '\r') {
                        mp_hal_stdout_tx_str("\n=== ");
                    } else {
                        mp_hal_stdout_tx_strn(&c, 1);
                    }
                }
            }
            parse_input_kind = MP_PARSE_FILE_INPUT;
Damien George's avatar
Damien George committed
203
204
205
206
        } else if (line.len == 0) {
            if (ret != 0) {
                printf("\n");
            }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
            goto input_restart;
        } else {
            // got a line with non-zero length, see if it needs continuing
            while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
                vstr_add_byte(&line, '\n');
                ret = readline(&line, "... ");
                if (ret == CHAR_CTRL_C) {
                    // cancel everything
                    printf("\n");
                    goto input_restart;
                } else if (ret == CHAR_CTRL_D) {
                    // stop entering compound statement
                    break;
                }
Damien George's avatar
Damien George committed
221
222
            }
        }
223

Damien George's avatar
Damien George committed
224
225
226
        mp_hal_stdio_mode_orig();

        mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, false);
227
        ret = execute_from_lexer(lex, parse_input_kind, true);
Damien George's avatar
Damien George committed
228
229
230
231
232
233
234
235
236
        if (ret & FORCED_EXIT) {
            return ret;
        }
    }

    #else

    // use GNU or simple readline

Damien's avatar
Damien committed
237
    for (;;) {
238
        char *line = prompt(">>> ");
Damien's avatar
Damien committed
239
240
        if (line == NULL) {
            // EOF
241
            return 0;
Damien's avatar
Damien committed
242
        }
243
244
245
246
        while (mp_repl_continue_with_input(line)) {
            char *line2 = prompt("... ");
            if (line2 == NULL) {
                break;
Damien's avatar
Damien committed
247
            }
248
249
250
251
            char *line3 = strjoin(line, '\n', line2);
            free(line);
            free(line2);
            line = line3;
Damien's avatar
Damien committed
252
        }
253

254
        mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
255
256
257
258
        int ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
        if (ret & FORCED_EXIT) {
            return ret;
        }
Paul Sokolovsky's avatar
Paul Sokolovsky committed
259
        free(line);
Damien's avatar
Damien committed
260
    }
Damien George's avatar
Damien George committed
261
262

    #endif
Damien's avatar
Damien committed
263
264
}

265
STATIC int do_file(const char *file) {
266
    mp_lexer_t *lex = mp_lexer_new_from_file(file);
267
    return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
268
}
Damien's avatar
Damien committed
269

270
STATIC int do_str(const char *str) {
271
    mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
272
    return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien's avatar
Damien committed
273
}
Damien's avatar
Damien committed
274

275
STATIC int usage(char **argv) {
276
    printf(
277
278
279
"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
"Options:\n"
"-v : verbose (trace various operations); can be multiple\n"
280
"-O[N] : apply bytecode optimizations of level N\n"
281
"\n"
282
"Implementation specific options:\n", argv[0]
283
284
);
    int impl_opts_cnt = 0;
285
    printf(
286
"  compile-only                 -- parse and compile only\n"
287
288
289
"  emit={bytecode,native,viper} -- set the default code emitter\n"
);
    impl_opts_cnt++;
290
291
#if MICROPY_ENABLE_GC
    printf(
292
293
"  heapsize=<n> -- set the heap size for the GC (default %ld)\n"
, heap_size);
294
295
296
297
298
299
300
    impl_opts_cnt++;
#endif

    if (impl_opts_cnt == 0) {
        printf("  (none)\n");
    }

301
302
303
    return 1;
}

304
// Process options which set interpreter init options
305
STATIC void pre_process_options(int argc, char **argv) {
306
307
308
309
    for (int a = 1; a < argc; a++) {
        if (argv[a][0] == '-') {
            if (strcmp(argv[a], "-X") == 0) {
                if (a + 1 >= argc) {
310
                    exit(usage(argv));
311
                }
312
                if (0) {
313
314
                } else if (strcmp(argv[a + 1], "compile-only") == 0) {
                    compile_only = true;
315
                } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
316
                    emit_opt = MP_EMIT_OPT_BYTECODE;
317
318
319
320
                } else if (strcmp(argv[a + 1], "emit=native") == 0) {
                    emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
                } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
                    emit_opt = MP_EMIT_OPT_VIPER;
321
#if MICROPY_ENABLE_GC
322
                } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
323
324
325
                    char *end;
                    heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, &end, 0);
                    // Don't bring unneeded libc dependencies like tolower()
326
327
328
329
330
331
332
333
334
335
                    // If there's 'w' immediately after number, adjust it for
                    // target word size. Note that it should be *before* size
                    // suffix like K or M, to avoid confusion with kilowords,
                    // etc. the size is still in bytes, just can be adjusted
                    // for word size (taking 32bit as baseline).
                    bool word_adjust = false;
                    if ((*end | 0x20) == 'w') {
                        word_adjust = true;
                        end++;
                    }
336
337
338
339
340
                    if ((*end | 0x20) == 'k') {
                        heap_size *= 1024;
                    } else if ((*end | 0x20) == 'm') {
                        heap_size *= 1024 * 1024;
                    }
341
342
343
                    if (word_adjust) {
                        heap_size = heap_size * BYTES_PER_WORD / 4;
                    }
344
#endif
345
                } else {
346
                    exit(usage(argv));
347
                }
348
349
350
351
352
353
                a++;
            }
        }
    }
}

354
STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
355
356
357
358
359
    for (int i = start_arg; i < argc; i++) {
        mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
    }
}

360
361
362
363
364
365
#ifdef _WIN32
#define PATHLIST_SEP_CHAR ';'
#else
#define PATHLIST_SEP_CHAR ':'
#endif

Damien's avatar
Damien committed
366
int main(int argc, char **argv) {
367
    mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
368

369
370
    pre_process_options(argc, argv);

371
#if MICROPY_ENABLE_GC
372
373
    char *heap = malloc(heap_size);
    gc_init(heap, heap + heap_size);
374
375
#endif

Damien George's avatar
Damien George committed
376
    mp_init();
Damien's avatar
Damien committed
377

378
    #ifndef _WIN32
379
    // create keyboard interrupt object
380
    MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
381
    #endif
382

383
384
385
    char *home = getenv("HOME");
    char *path = getenv("MICROPYPATH");
    if (path == NULL) {
386
387
388
        #ifdef MICROPY_PY_SYS_PATH_DEFAULT
        path = MICROPY_PY_SYS_PATH_DEFAULT;
        #else
389
        path = "~/.micropython/lib:/usr/lib/micropython";
390
        #endif
391
    }
392
    mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
393
    for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
394
395
396
397
398
        path_num++;
        if (p != NULL) {
            p++;
        }
    }
399
    mp_obj_list_init(mp_sys_path, path_num);
400
    mp_obj_t *path_items;
Damien George's avatar
Damien George committed
401
    mp_obj_list_get(mp_sys_path, &path_num, &path_items);
402
    path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
403
    {
404
    char *p = path;
405
    for (mp_uint_t i = 1; i < path_num; i++) {
406
        char *p1 = strchr(p, PATHLIST_SEP_CHAR);
407
408
409
410
411
412
413
        if (p1 == NULL) {
            p1 = p + strlen(p);
        }
        if (p[0] == '~' && p[1] == '/' && home != NULL) {
            // Expand standalone ~ to $HOME
            CHECKBUF(buf, PATH_MAX);
            CHECKBUF_APPEND(buf, home, strlen(home));
414
            CHECKBUF_APPEND(buf, p + 1, (size_t)(p1 - p - 1));
415
            path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
416
        } else {
417
            path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
418
419
420
        }
        p = p1 + 1;
    }
421
    }
422

423
    mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovsky's avatar
Paul Sokolovsky committed
424

425
426
427
428
429
430
431
    #if defined(MICROPY_UNIX_COVERAGE)
    {
        MP_DECLARE_CONST_FUN_OBJ(extra_coverage_obj);
        mp_store_global(QSTR_FROM_STR_STATIC("extra_coverage"), (mp_obj_t)&extra_coverage_obj);
    }
    #endif

432
433
434
435
436
437
438
    // Here is some example code to create a class and instance of that class.
    // First is the Python, then the C code.
    //
    // class TestClass:
    //     pass
    // test_obj = TestClass()
    // test_obj.attr = 42
439
440
441
442
443
    //
    // mp_obj_t test_class_type, test_class_instance;
    // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
    // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
    // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
444

445
446
447
448
449
450
451
    /*
    printf("bytes:\n");
    printf("    total %d\n", m_get_total_bytes_allocated());
    printf("    cur   %d\n", m_get_current_bytes_allocated());
    printf("    peak  %d\n", m_get_peak_bytes_allocated());
    */

452
453
    const int NOTHING_EXECUTED = -2;
    int ret = NOTHING_EXECUTED;
454
455
456
457
    for (int a = 1; a < argc; a++) {
        if (argv[a][0] == '-') {
            if (strcmp(argv[a], "-c") == 0) {
                if (a + 1 >= argc) {
458
                    return usage(argv);
459
                }
460
                ret = do_str(argv[a + 1]);
461
462
463
                if (ret & FORCED_EXIT) {
                    break;
                }
464
                a += 1;
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
            } else if (strcmp(argv[a], "-m") == 0) {
                if (a + 1 >= argc) {
                    return usage(argv);
                }
                mp_obj_t import_args[4];
                import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1]), false);
                import_args[1] = import_args[2] = mp_const_none;
                // Ask __import__ to handle imported module specially - set its __name__
                // to __main__, and also return this leaf module, not top-level package
                // containing it.
                import_args[3] = mp_const_false;
                // TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m :
                // "the first element of sys.argv will be the full path to
                // the module file (while the module file is being located,
                // the first element will be set to "-m")."
                set_sys_argv(argv, argc, a + 1);

                mp_obj_t mod;
                nlr_buf_t nlr;
                if (nlr_push(&nlr) == 0) {
                    mod = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args);
                    nlr_pop();
                } else {
                    // uncaught exception
489
                    return handle_uncaught_exception((mp_obj_t)nlr.ret_val) & 0xff;
490
491
492
493
494
495
496
497
498
                }

                if (mp_obj_is_package(mod)) {
                    // TODO
                    fprintf(stderr, "%s: -m for packages not yet implemented\n", argv[0]);
                    exit(1);
                }
                ret = 0;
                break;
499
500
            } else if (strcmp(argv[a], "-X") == 0) {
                a += 1;
501
502
            } else if (strcmp(argv[a], "-v") == 0) {
                mp_verbose_flag++;
503
504
            } else if (strncmp(argv[a], "-O", 2) == 0) {
                if (isdigit(argv[a][2])) {
505
                    MP_STATE_VM(mp_optimise_value) = argv[a][2] & 0xf;
506
                } else {
507
508
                    MP_STATE_VM(mp_optimise_value) = 0;
                    for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++);
509
                }
510
            } else {
511
                return usage(argv);
512
            }
513
        } else {
514
515
            char *pathbuf = malloc(PATH_MAX);
            char *basedir = realpath(argv[a], pathbuf);
516
            if (basedir == NULL) {
517
                fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
518
519
                perror("");
                // CPython exits with 2 in such case
520
521
                ret = 2;
                break;
522
            }
523
524
525
526

            // Set base dir of the script as first entry in sys.path
            char *p = strrchr(basedir, '/');
            path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
527
            free(pathbuf);
528

529
            set_sys_argv(argv, argc, a);
530
            ret = do_file(argv[a]);
531
            break;
532
        }
Damien's avatar
Damien committed
533
    }
534

535
    if (ret == NOTHING_EXECUTED) {
536
537
538
539
540
541
542
543
        if (isatty(0)) {
            prompt_read_history();
            ret = do_repl();
            prompt_write_history();
        } else {
            mp_lexer_t *lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false);
            ret = execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
        }
544
545
    }

546
    #if MICROPY_PY_MICROPYTHON_MEM_INFO
547
    if (mp_verbose_flag) {
548
        mp_micropython_mem_info(0, NULL);
549
    }
550
    #endif
551

Damien George's avatar
Damien George committed
552
    mp_deinit();
Damien's avatar
Damien committed
553

554
555
556
557
558
559
#if MICROPY_ENABLE_GC && !defined(NDEBUG)
    // We don't really need to free memory since we are about to exit the
    // process, but doing so helps to find memory leaks.
    free(heap);
#endif

Damien's avatar
Damien committed
560
    //printf("total bytes = %d\n", m_get_total_bytes_allocated());
561
    return ret & 0xff;
Damien's avatar
Damien committed
562
}
563

564
565
566
567
568
569
570
571
572
573
574
uint mp_import_stat(const char *path) {
    struct stat st;
    if (stat(path, &st) == 0) {
        if (S_ISDIR(st.st_mode)) {
            return MP_IMPORT_STAT_DIR;
        } else if (S_ISREG(st.st_mode)) {
            return MP_IMPORT_STAT_FILE;
        }
    }
    return MP_IMPORT_STAT_NO_EXIST;
}
575
576
577
578
579
580
581
582

int DEBUG_printf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int ret = vfprintf(stderr, fmt, ap);
    va_end(ap);
    return ret;
}
583
584
585
586
587

void nlr_jump_fail(void *val) {
    printf("FATAL: uncaught NLR %p\n", val);
    exit(1);
}