-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
rtutils.c
1412 lines (1323 loc) · 48 KB
/
rtutils.c
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
utility functions used by the runtime system, generated code, and Base library
*/
#include "platform.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#if defined(_OS_WINDOWS_)
#include <malloc.h>
#else
#include <unistd.h>
#endif
#include <ctype.h>
#include "julia.h"
#include "julia_internal.h"
#include "julia_assert.h"
#ifdef __cplusplus
extern "C" {
#endif
// exceptions -----------------------------------------------------------------
JL_DLLEXPORT void JL_NORETURN jl_error(const char *str)
{
if (jl_errorexception_type == NULL) {
jl_printf(JL_STDERR, "ERROR: %s\n", str);
jl_exit(1);
}
jl_value_t *msg = jl_pchar_to_string((char*)str, strlen(str));
JL_GC_PUSH1(&msg);
jl_throw(jl_new_struct(jl_errorexception_type, msg));
}
extern int vasprintf(char **str, const char *fmt, va_list ap);
jl_value_t *jl_vexceptionf(jl_datatype_t *exception_type,
const char *fmt, va_list args)
{
if (exception_type == NULL) {
jl_printf(JL_STDERR, "ERROR: ");
jl_vprintf(JL_STDERR, fmt, args);
jl_printf(JL_STDERR, "\n");
jl_exit(1);
}
char *str = NULL;
int ok = vasprintf(&str, fmt, args);
jl_value_t *msg;
if (ok < 0) { // vasprintf failed
msg = jl_cstr_to_string("internal error: could not display error message");
}
else {
msg = jl_pchar_to_string(str, strlen(str));
free(str);
}
JL_GC_PUSH1(&msg);
jl_value_t *e = jl_new_struct(exception_type, msg);
JL_GC_POP();
return e;
}
JL_DLLEXPORT void JL_NORETURN jl_errorf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
jl_value_t *e = jl_vexceptionf(jl_errorexception_type, fmt, args);
va_end(args);
jl_throw(e);
}
JL_DLLEXPORT void JL_NORETURN jl_exceptionf(jl_datatype_t *exception_type,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
jl_value_t *e = jl_vexceptionf(exception_type, fmt, args);
va_end(args);
jl_throw(e);
}
jl_value_t *jl_get_exceptionf(jl_datatype_t *exception_type,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
jl_value_t *e = jl_vexceptionf(exception_type, fmt, args);
va_end(args);
return e;
}
JL_DLLEXPORT void JL_NORETURN jl_too_few_args(const char *fname, int min)
{
jl_exceptionf(jl_argumenterror_type, "%s: too few arguments (expected %d)", fname, min);
}
JL_DLLEXPORT void JL_NORETURN jl_too_many_args(const char *fname, int max)
{
jl_exceptionf(jl_argumenterror_type, "%s: too many arguments (expected %d)", fname, max);
}
// with function name / location description, plus extra context
JL_DLLEXPORT void JL_NORETURN jl_type_error_rt(const char *fname, const char *context,
jl_value_t *expected JL_MAYBE_UNROOTED,
jl_value_t *got JL_MAYBE_UNROOTED)
{
jl_value_t *ctxt=NULL;
JL_GC_PUSH3(&ctxt, &expected, &got);
ctxt = jl_pchar_to_string((char*)context, strlen(context));
jl_value_t *ex = jl_new_struct(jl_typeerror_type, jl_symbol(fname), ctxt, expected, got);
jl_throw(ex);
}
// with function name or description only
JL_DLLEXPORT void JL_NORETURN jl_type_error(const char *fname,
jl_value_t *expected JL_MAYBE_UNROOTED,
jl_value_t *got JL_MAYBE_UNROOTED)
{
jl_type_error_rt(fname, "", expected, got);
}
JL_DLLEXPORT void JL_NORETURN jl_undefined_var_error(jl_sym_t *var)
{
jl_throw(jl_new_struct(jl_undefvarerror_type, var));
}
JL_DLLEXPORT void JL_NORETURN jl_atomic_error(char *str) // == jl_exceptionf(jl_atomicerror_type, "%s", str)
{
jl_value_t *msg = jl_pchar_to_string((char*)str, strlen(str));
JL_GC_PUSH1(&msg);
jl_throw(jl_new_struct(jl_atomicerror_type, msg));
}
JL_DLLEXPORT void JL_NORETURN jl_bounds_error(jl_value_t *v, jl_value_t *t)
{
JL_GC_PUSH2(&v, &t); // root arguments so the caller doesn't need to
jl_throw(jl_new_struct((jl_datatype_t*)jl_boundserror_type, v, t));
}
JL_DLLEXPORT void JL_NORETURN jl_bounds_error_v(jl_value_t *v, jl_value_t **idxs, size_t nidxs)
{
jl_value_t *t = NULL;
// items in idxs are assumed to already be rooted
JL_GC_PUSH2(&v, &t); // root v so the caller doesn't need to
t = jl_f_tuple(NULL, idxs, nidxs);
jl_throw(jl_new_struct((jl_datatype_t*)jl_boundserror_type, v, t));
}
JL_DLLEXPORT void JL_NORETURN jl_bounds_error_tuple_int(jl_value_t **v, size_t nv, size_t i)
{
// values in v are expected to already be gc-rooted
jl_bounds_error_int(jl_f_tuple(NULL, v, nv), i);
}
JL_DLLEXPORT void JL_NORETURN jl_bounds_error_unboxed_int(void *data, jl_value_t *vt, size_t i)
{
jl_value_t *t = NULL, *v = NULL;
// data is expected to be gc-safe (either gc-rooted, or alloca)
// vt is expected to be gc-rooted (in a linfo-root probably)
JL_GC_PUSH2(&v, &t);
v = jl_new_bits(vt, data);
t = jl_box_long(i);
jl_throw(jl_new_struct((jl_datatype_t*)jl_boundserror_type, v, t));
}
JL_DLLEXPORT void JL_NORETURN jl_bounds_error_int(jl_value_t *v JL_MAYBE_UNROOTED, size_t i)
{
jl_value_t *t = NULL;
JL_GC_PUSH2(&v, &t); // root arguments so the caller doesn't need to
t = jl_box_long(i);
jl_throw(jl_new_struct((jl_datatype_t*)jl_boundserror_type, v, t));
}
JL_DLLEXPORT void JL_NORETURN jl_bounds_error_ints(jl_value_t *v JL_MAYBE_UNROOTED,
size_t *idxs, size_t nidxs)
{
size_t i;
jl_value_t *t = NULL;
JL_GC_PUSH2(&v, &t); // root arguments so the caller doesn't need to
t = (jl_value_t*)jl_alloc_svec(nidxs);
for (i = 0; i < nidxs; i++) {
jl_svecset(t, i, jl_box_long(idxs[i]));
}
t = jl_f_tuple(NULL, jl_svec_data(t), nidxs);
jl_throw(jl_new_struct((jl_datatype_t*)jl_boundserror_type, v, t));
}
JL_DLLEXPORT void JL_NORETURN jl_eof_error(void)
{
jl_datatype_t *eof_error =
(jl_datatype_t*)jl_get_global(jl_base_module, jl_symbol("EOFError"));
assert(eof_error != NULL);
jl_throw(jl_new_struct(eof_error));
}
JL_DLLEXPORT void jl_typeassert(jl_value_t *x, jl_value_t *t)
{
if (!jl_isa(x,t))
jl_type_error("typeassert", t, x);
}
#ifndef HAVE_SSP
JL_DLLEXPORT uintptr_t __stack_chk_guard = (uintptr_t)0xBAD57ACCBAD67ACC; // 0xBADSTACKBADSTACK
JL_DLLEXPORT void __stack_chk_fail(void)
{
/* put your panic function or similar in here */
fprintf(stderr, "fatal error: stack corruption detected\n");
jl_gc_debug_critical_error();
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function, there's no going back now
}
#endif
// exceptions -----------------------------------------------------------------
JL_DLLEXPORT void jl_enter_handler(jl_handler_t *eh)
{
jl_task_t *ct = jl_current_task;
// Must have no safepoint
eh->prev = ct->eh;
eh->gcstack = ct->gcstack;
eh->gc_state = jl_atomic_load_relaxed(&ct->ptls->gc_state);
eh->locks_len = ct->ptls->locks.len;
eh->defer_signal = ct->ptls->defer_signal;
eh->world_age = ct->world_age;
ct->eh = eh;
#ifdef ENABLE_TIMINGS
eh->timing_stack = ct->ptls->timing_stack;
#endif
}
// Restore thread local state to saved state in error handler `eh`.
// This is executed in two circumstances:
// * We leave a try block through normal control flow
// * An exception causes a nonlocal jump to the catch block. In this case
// there's additional cleanup required, eg pushing the exception stack.
JL_DLLEXPORT void jl_eh_restore_state(jl_handler_t *eh)
{
jl_task_t *ct = jl_current_task;
#ifdef _OS_WINDOWS_
if (ct->ptls->needs_resetstkoflw) {
_resetstkoflw();
ct->ptls->needs_resetstkoflw = 0;
}
#endif
// `eh` may be not equal to `ct->eh`. See `jl_pop_handler`
// This function should **NOT** have any safepoint before the ones at the
// end.
sig_atomic_t old_defer_signal = ct->ptls->defer_signal;
int8_t old_gc_state = jl_atomic_load_relaxed(&ct->ptls->gc_state);
ct->eh = eh->prev;
ct->gcstack = eh->gcstack;
small_arraylist_t *locks = &ct->ptls->locks;
int unlocks = locks->len > eh->locks_len;
if (unlocks) {
for (size_t i = locks->len; i > eh->locks_len; i--)
jl_mutex_unlock_nogc((jl_mutex_t*)locks->items[i - 1]);
locks->len = eh->locks_len;
}
ct->world_age = eh->world_age;
ct->ptls->defer_signal = eh->defer_signal;
if (old_gc_state != eh->gc_state) {
jl_atomic_store_release(&ct->ptls->gc_state, eh->gc_state);
if (old_gc_state) {
jl_gc_safepoint_(ct->ptls);
}
}
if (old_defer_signal && !eh->defer_signal) {
jl_sigint_safepoint(ct->ptls);
}
if (jl_atomic_load_relaxed(&jl_gc_have_pending_finalizers) &&
unlocks && eh->locks_len == 0) {
jl_gc_run_pending_finalizers(ct);
}
}
JL_DLLEXPORT void jl_pop_handler(int n)
{
jl_task_t *ct = jl_current_task;
if (__unlikely(n <= 0))
return;
jl_handler_t *eh = ct->eh;
while (--n > 0)
eh = eh->prev;
jl_eh_restore_state(eh);
}
JL_DLLEXPORT size_t jl_excstack_state(void) JL_NOTSAFEPOINT
{
jl_task_t *ct = jl_current_task;
jl_excstack_t *s = ct->excstack;
return s ? s->top : 0;
}
JL_DLLEXPORT void jl_restore_excstack(size_t state) JL_NOTSAFEPOINT
{
jl_task_t *ct = jl_current_task;
jl_excstack_t *s = ct->excstack;
if (s) {
assert(s->top >= state);
s->top = state;
}
}
static void jl_copy_excstack(jl_excstack_t *dest, jl_excstack_t *src) JL_NOTSAFEPOINT
{
assert(dest->reserved_size >= src->top);
memcpy(jl_excstack_raw(dest), jl_excstack_raw(src), sizeof(jl_bt_element_t)*src->top);
dest->top = src->top;
}
static void jl_reserve_excstack(jl_excstack_t **stack JL_REQUIRE_ROOTED_SLOT,
size_t reserved_size)
{
jl_excstack_t *s = *stack;
if (s && s->reserved_size >= reserved_size)
return;
size_t bufsz = sizeof(jl_excstack_t) + sizeof(uintptr_t)*reserved_size;
jl_task_t *ct = jl_current_task;
jl_excstack_t *new_s = (jl_excstack_t*)jl_gc_alloc_buf(ct->ptls, bufsz);
new_s->top = 0;
new_s->reserved_size = reserved_size;
if (s)
jl_copy_excstack(new_s, s);
*stack = new_s;
}
void jl_push_excstack(jl_excstack_t **stack JL_REQUIRE_ROOTED_SLOT JL_ROOTING_ARGUMENT,
jl_value_t *exception JL_ROOTED_ARGUMENT,
jl_bt_element_t *bt_data, size_t bt_size)
{
jl_reserve_excstack(stack, (*stack ? (*stack)->top : 0) + bt_size + 2);
jl_excstack_t *s = *stack;
jl_bt_element_t *rawstack = jl_excstack_raw(s);
memcpy(rawstack + s->top, bt_data, sizeof(jl_bt_element_t)*bt_size);
s->top += bt_size + 2;
rawstack[s->top-2].uintptr = bt_size;
rawstack[s->top-1].jlvalue = exception;
}
// conversion -----------------------------------------------------------------
JL_DLLEXPORT void *(jl_symbol_name)(jl_sym_t *s)
{
return jl_symbol_name(s);
}
// WARNING: THIS FUNCTION IS NEVER CALLED BUT INLINE BY CCALL
JL_DLLEXPORT void *jl_array_ptr(jl_array_t *a)
{
return a->data;
}
JL_DLLEXPORT jl_value_t *jl_value_ptr(jl_value_t *a)
{
return a;
}
// optimization of setfield which bypasses boxing of the idx (and checking field type validity)
JL_DLLEXPORT void jl_set_nth_field(jl_value_t *v, size_t idx0, jl_value_t *rhs)
{
jl_datatype_t *st = (jl_datatype_t*)jl_typeof(v);
if (!st->name->mutabl)
jl_errorf("setfield!: immutable struct of type %s cannot be changed", jl_symbol_name(st->name->name));
if (idx0 >= jl_datatype_nfields(st))
jl_bounds_error_int(v, idx0 + 1);
//jl_value_t *ft = jl_field_type(st, idx0);
//if (!jl_isa(rhs, ft)) {
// jl_type_error("setfield!", ft, rhs);
//}
//int isatomic = jl_field_isatomic(st, idx0);
//if (isatomic) ...
set_nth_field(st, v, idx0, rhs, 0);
}
// parsing --------------------------------------------------------------------
static int substr_isspace(char *p, char *pend)
{
while (p != pend) {
if (!isspace((unsigned char)*p)) {
return 0;
}
p++;
}
return 1;
}
JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, size_t len)
{
char *p;
char *bstr = str+offset;
char *pend = bstr+len;
char *tofree = NULL;
int hasvalue = 0;
errno = 0;
if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) {
// confusing data outside substring. must copy.
char *newstr;
if (len + 1 < jl_page_size) {
newstr = (char*)alloca(len + 1);
}
else {
newstr = tofree = (char*)malloc_s(len + 1);
}
memcpy(newstr, bstr, len);
newstr[len] = 0;
bstr = newstr;
pend = bstr+len;
}
double out = jl_strtod_c(bstr, &p);
if (errno==ERANGE && (out==0 || out==HUGE_VAL || out==-HUGE_VAL)) {
hasvalue = 0;
}
else if (p == bstr) {
hasvalue = 0;
}
else {
// Deal with case where the substring might be something like "1 ",
// which is OK, and "1 X", which we don't allow.
hasvalue = substr_isspace(p, pend) ? 1 : 0;
}
if (__unlikely(tofree))
free(tofree);
jl_nullable_float64_t ret = {(uint8_t)hasvalue, out};
return ret;
}
JL_DLLEXPORT int jl_substrtod(char *str, size_t offset, size_t len, double *out)
{
jl_nullable_float64_t nd = jl_try_substrtod(str, offset, len);
if (0 != nd.hasvalue) {
*out = nd.value;
return 0;
}
return 1;
}
// MSVC pre-2013 did not define HUGE_VALF
#ifndef HUGE_VALF
#define HUGE_VALF (1e25f * 1e25f)
#endif
JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, size_t len)
{
char *p;
char *bstr = str+offset;
char *pend = bstr+len;
char *tofree = NULL;
int hasvalue = 0;
errno = 0;
if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) {
// confusing data outside substring. must copy.
char *newstr;
if (len + 1 < jl_page_size) {
newstr = (char*)alloca(len + 1);
}
else {
newstr = tofree = (char*)malloc_s(len + 1);
}
memcpy(newstr, bstr, len);
newstr[len] = 0;
bstr = newstr;
pend = bstr+len;
}
#if defined(_OS_WINDOWS_) && !defined(_COMPILER_GCC_)
float out = (float)jl_strtod_c(bstr, &p);
#else
float out = jl_strtof_c(bstr, &p);
#endif
if (errno==ERANGE && (out==0 || out==HUGE_VALF || out==-HUGE_VALF)) {
hasvalue = 0;
}
else if (p == bstr) {
hasvalue = 0;
}
else {
// Deal with case where the substring might be something like "1 ",
// which is OK, and "1 X", which we don't allow.
hasvalue = substr_isspace(p, pend) ? 1 : 0;
}
if (__unlikely(tofree))
free(tofree);
jl_nullable_float32_t ret = {(uint8_t)hasvalue, out};
return ret;
}
JL_DLLEXPORT int jl_substrtof(char *str, int offset, size_t len, float *out)
{
jl_nullable_float32_t nf = jl_try_substrtof(str, offset, len);
if (0 != nf.hasvalue) {
*out = nf.value;
return 0;
}
return 1;
}
// showing --------------------------------------------------------------------
JL_DLLEXPORT void jl_flush_cstdio(void) JL_NOTSAFEPOINT
{
fflush(stdout);
fflush(stderr);
}
JL_DLLEXPORT jl_value_t *jl_stdout_obj(void) JL_NOTSAFEPOINT
{
if (jl_base_module == NULL)
return NULL;
jl_binding_t *stdout_obj = jl_get_module_binding(jl_base_module, jl_symbol("stdout"));
return stdout_obj ? jl_atomic_load_relaxed(&stdout_obj->value) : NULL;
}
JL_DLLEXPORT jl_value_t *jl_stderr_obj(void) JL_NOTSAFEPOINT
{
if (jl_base_module == NULL)
return NULL;
jl_binding_t *stderr_obj = jl_get_module_binding(jl_base_module, jl_symbol("stderr"));
return stderr_obj ? jl_atomic_load_relaxed(&stderr_obj->value) : NULL;
}
// toys for debugging ---------------------------------------------------------
static size_t jl_show_svec(JL_STREAM *out, jl_svec_t *t, const char *head, const char *opn, const char *cls) JL_NOTSAFEPOINT
{
size_t i, n=0, len = jl_svec_len(t);
n += jl_printf(out, "%s", head);
n += jl_printf(out, "%s", opn);
for (i = 0; i < len; i++) {
jl_value_t *v = jl_svecref(t,i);
n += jl_static_show(out, v);
if (i != len-1)
n += jl_printf(out, ", ");
}
n += jl_printf(out, "%s", cls);
return n;
}
struct recur_list {
struct recur_list *prev;
jl_value_t *v;
};
static size_t jl_static_show_x(JL_STREAM *out, jl_value_t *v, struct recur_list *depth) JL_NOTSAFEPOINT;
static size_t jl_static_show_next_(JL_STREAM *out, jl_value_t *v, jl_value_t *prev, struct recur_list *depth) JL_NOTSAFEPOINT;
JL_DLLEXPORT int jl_id_start_char(uint32_t wc) JL_NOTSAFEPOINT;
JL_DLLEXPORT int jl_id_char(uint32_t wc) JL_NOTSAFEPOINT;
JL_DLLEXPORT int jl_is_identifier(char *str) JL_NOTSAFEPOINT
{
size_t i = 0;
uint32_t wc = u8_nextchar(str, &i);
if (!jl_id_start_char(wc))
return 0;
while ((wc = u8_nextchar(str, &i)) != 0) {
if (!jl_id_char(wc))
return 0;
}
return 1;
}
static jl_datatype_t *nth_arg_datatype(jl_value_t *a JL_PROPAGATES_ROOT, int n) JL_NOTSAFEPOINT
{
if (jl_is_datatype(a)) {
if (n == 0)
return (jl_datatype_t*)a;
if (jl_is_tuple_type(a)) {
if (jl_nparams(a) < n)
return NULL;
return nth_arg_datatype(jl_tparam(a, n - 1), 0);
}
return NULL;
}
else if (jl_is_typevar(a)) {
return nth_arg_datatype(((jl_tvar_t*)a)->ub, n);
}
else if (jl_is_unionall(a)) {
return nth_arg_datatype(((jl_unionall_t*)a)->body, n);
}
else if (jl_is_uniontype(a)) {
jl_uniontype_t *u = (jl_uniontype_t*)a;
jl_datatype_t *d1 = nth_arg_datatype(u->a, n);
if (d1 == NULL) return NULL;
jl_datatype_t *d2 = nth_arg_datatype(u->b, n);
if (d2 == NULL || d1->name != d2->name)
return NULL;
return d1;
}
return NULL;
}
// get DataType of first tuple element (if present), or NULL if cannot be determined
jl_datatype_t *jl_nth_argument_datatype(jl_value_t *argtypes JL_PROPAGATES_ROOT, int n) JL_NOTSAFEPOINT
{
return nth_arg_datatype(argtypes, n);
}
// get DataType implied by a single given type, or `nothing`
JL_DLLEXPORT jl_value_t *jl_argument_datatype(jl_value_t *argt JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT
{
jl_datatype_t *dt = nth_arg_datatype(argt, 0);
if (dt == NULL)
return jl_nothing;
return (jl_value_t*)dt;
}
static int is_globname_binding(jl_value_t *v, jl_datatype_t *dv) JL_NOTSAFEPOINT
{
jl_sym_t *globname = dv->name->mt != NULL ? dv->name->mt->name : NULL;
if (globname && dv->name->module && jl_binding_resolved_p(dv->name->module, globname)) {
jl_binding_t *b = jl_get_module_binding(dv->name->module, globname);
if (b && b->constp) {
jl_value_t *bv = jl_atomic_load_relaxed(&b->value);
// The `||` makes this function work for both function instances and function types.
if (bv == v || jl_typeof(bv) == v)
return 1;
}
}
return 0;
}
static int is_globfunction(jl_value_t *v, jl_datatype_t *dv, jl_sym_t **globname_out) JL_NOTSAFEPOINT
{
jl_sym_t *globname = dv->name->mt != NULL ? dv->name->mt->name : NULL;
*globname_out = globname;
if (globname && !strchr(jl_symbol_name(globname), '#') && !strchr(jl_symbol_name(globname), '@')) {
return 1;
}
return 0;
}
static size_t jl_static_show_x_sym_escaped(JL_STREAM *out, jl_sym_t *name) JL_NOTSAFEPOINT
{
size_t n = 0;
char *sn = jl_symbol_name(name);
int hidden = 0;
if (!(jl_is_identifier(sn) || jl_is_operator(sn))) {
hidden = 1;
}
if (hidden) {
n += jl_printf(out, "var\"");
}
n += jl_printf(out, "%s", sn);
if (hidden) {
n += jl_printf(out, "\"");
}
return n;
}
// `jl_static_show()` cannot call `jl_subtype()`, for the GC reasons
// explained in the comment on `jl_static_show_x_()`, below.
// This function checks if `vt <: Function` without triggering GC.
static int jl_static_is_function_(jl_datatype_t *vt) JL_NOTSAFEPOINT {
if (!jl_function_type) { // Make sure there's a Function type defined.
return 0;
}
int _iter_count = 0; // To prevent infinite loops from corrupt type objects.
while (vt != jl_any_type) {
if (vt == NULL) {
return 0;
} else if (_iter_count > 10000) {
// We are very likely stuck in a cyclic datastructure, so we assume this is
// _not_ a Function.
return 0;
} else if (vt == jl_function_type) {
return 1;
}
vt = vt->super;
_iter_count += 1;
}
return 0;
}
// `v` might be pointing to a field inlined in a structure therefore
// `jl_typeof(v)` may not be the same with `vt` and only `vt` should be
// used to determine the type of the value.
// This is necessary to make sure that this function doesn't allocate any
// memory through the Julia GC
static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt,
struct recur_list *depth) JL_NOTSAFEPOINT
{
size_t n = 0;
if ((uintptr_t)vt < 4096U) {
n += jl_printf(out, "<?#%p::%p>", (void*)v, (void*)vt);
}
else if ((uintptr_t)v < 4096U) {
n += jl_printf(out, "<?#%p::", (void*)v);
n += jl_static_show_x(out, (jl_value_t*)vt, depth);
n += jl_printf(out, ">");
}
// These need to be special cased because they
// exist only by pointer identity in early startup
else if (v == (jl_value_t*)jl_simplevector_type) {
n += jl_printf(out, "Core.SimpleVector");
}
else if (v == (jl_value_t*)jl_typename_type) {
n += jl_printf(out, "Core.TypeName");
}
else if (v == (jl_value_t*)jl_symbol_type) {
n += jl_printf(out, "Symbol");
}
else if (v == (jl_value_t*)jl_methtable_type) {
n += jl_printf(out, "Core.MethodTable");
}
else if (v == (jl_value_t*)jl_any_type) {
n += jl_printf(out, "Any");
}
else if (v == (jl_value_t*)jl_type_type) {
n += jl_printf(out, "Type");
}
else if (vt == jl_method_type) {
jl_method_t *m = (jl_method_t*)v;
n += jl_static_show_func_sig(out, m->sig);
}
else if (vt == jl_method_instance_type) {
jl_method_instance_t *li = (jl_method_instance_t*)v;
if (jl_is_method(li->def.method)) {
n += jl_static_show_func_sig(out, li->specTypes);
n += jl_printf(out, " from ");
n += jl_static_show_func_sig(out, li->def.method->sig);
}
else {
n += jl_static_show_x(out, (jl_value_t*)li->def.module, depth);
n += jl_printf(out, ".<toplevel thunk> -> ");
n += jl_static_show_x(out, li->uninferred, depth);
}
}
else if (vt == jl_typename_type) {
n += jl_static_show_x(out, jl_unwrap_unionall(((jl_typename_t*)v)->wrapper), depth);
n += jl_printf(out, ".name");
}
else if (vt == jl_simplevector_type) {
n += jl_show_svec(out, (jl_svec_t*)v, "svec", "(", ")");
}
else if (v == (jl_value_t*)jl_unionall_type) {
// avoid printing `typeof(Type)` for `UnionAll`.
n += jl_printf(out, "UnionAll");
}
else if (vt == jl_vararg_type) {
jl_vararg_t *vm = (jl_vararg_t*)v;
n += jl_printf(out, "Vararg");
if (vm->T) {
n += jl_printf(out, "{");
n += jl_static_show_x(out, vm->T, depth);
if (vm->N) {
n += jl_printf(out, ", ");
n += jl_static_show_x(out, vm->N, depth);
}
n += jl_printf(out, "}");
}
}
else if (vt == jl_datatype_type) {
// typeof(v) == DataType, so v is a Type object.
// Types are printed as a fully qualified name, with parameters, e.g.
// `Base.Set{Int}`, and function types are printed as e.g. `typeof(Main.f)`
jl_datatype_t *dv = (jl_datatype_t*)v;
jl_sym_t *globname;
int globfunc = is_globname_binding(v, dv) && is_globfunction(v, dv, &globname);
jl_sym_t *sym = globfunc ? globname : dv->name->name;
char *sn = jl_symbol_name(sym);
size_t quote = 0;
if (dv->name == jl_tuple_typename) {
if (dv == jl_tuple_type)
return jl_printf(out, "Tuple");
int taillen = 1, tlen = jl_nparams(dv), i;
for (i = tlen-2; i >= 0; i--) {
if (jl_tparam(dv, i) == jl_tparam(dv, tlen-1))
taillen++;
else
break;
}
if (taillen == tlen && taillen > 3) {
n += jl_printf(out, "NTuple{%d, ", tlen);
n += jl_static_show_x(out, jl_tparam0(dv), depth);
n += jl_printf(out, "}");
}
else {
n += jl_printf(out, "Tuple{");
for (i = 0; i < (taillen > 3 ? tlen-taillen : tlen); i++) {
if (i > 0)
n += jl_printf(out, ", ");
n += jl_static_show_x(out, jl_tparam(dv, i), depth);
}
if (taillen > 3) {
n += jl_printf(out, ", Vararg{");
n += jl_static_show_x(out, jl_tparam(dv, tlen-1), depth);
n += jl_printf(out, ", %d}", taillen);
}
n += jl_printf(out, "}");
}
return n;
}
if (globfunc) {
n += jl_printf(out, "typeof(");
}
if (jl_core_module && (dv->name->module != jl_core_module || !jl_module_exports_p(jl_core_module, sym))) {
n += jl_static_show_x(out, (jl_value_t*)dv->name->module, depth);
n += jl_printf(out, ".");
size_t i = 0;
if (globfunc && !jl_id_start_char(u8_nextchar(sn, &i))) {
n += jl_printf(out, ":(");
quote = 1;
}
}
n += jl_static_show_x_sym_escaped(out, sym);
if (globfunc) {
n += jl_printf(out, ")");
if (quote) {
n += jl_printf(out, ")");
}
}
if (dv->parameters && (jl_value_t*)dv != dv->name->wrapper) {
size_t j, tlen = jl_nparams(dv);
if (tlen > 0) {
n += jl_printf(out, "{");
for (j = 0; j < tlen; j++) {
jl_value_t *p = jl_tparam(dv,j);
n += jl_static_show_x(out, p, depth);
if (j != tlen-1)
n += jl_printf(out, ", ");
}
n += jl_printf(out, "}");
}
}
}
else if (vt == jl_intrinsic_type) {
int f = *(uint32_t*)jl_data_ptr(v);
n += jl_printf(out, "#<intrinsic #%d %s>", f, jl_intrinsic_name(f));
}
else if (vt == jl_int64_type) {
n += jl_printf(out, "%" PRId64, *(int64_t*)v);
}
else if (vt == jl_int32_type) {
n += jl_printf(out, "%" PRId32, *(int32_t*)v);
}
else if (vt == jl_int16_type) {
n += jl_printf(out, "%" PRId16, *(int16_t*)v);
}
else if (vt == jl_int8_type) {
n += jl_printf(out, "%" PRId8, *(int8_t*)v);
}
else if (vt == jl_uint64_type) {
n += jl_printf(out, "0x%016" PRIx64, *(uint64_t*)v);
}
else if (vt == jl_uint32_type) {
n += jl_printf(out, "0x%08" PRIx32, *(uint32_t*)v);
}
else if (vt == jl_uint16_type) {
n += jl_printf(out, "0x%04" PRIx16, *(uint16_t*)v);
}
else if (vt == jl_uint8_type) {
n += jl_printf(out, "0x%02" PRIx8, *(uint8_t*)v);
}
else if (jl_pointer_type && jl_is_cpointer_type((jl_value_t*)vt)) {
#ifdef _P64
n += jl_printf(out, "0x%016" PRIx64, *(uint64_t*)v);
#else
n += jl_printf(out, "0x%08" PRIx32, *(uint32_t*)v);
#endif
}
else if (vt == jl_float32_type) {
n += jl_printf(out, "%gf", *(float*)v);
}
else if (vt == jl_float64_type) {
n += jl_printf(out, "%g", *(double*)v);
}
else if (vt == jl_bool_type) {
n += jl_printf(out, "%s", *(uint8_t*)v ? "true" : "false");
}
else if (v == jl_nothing || (jl_nothing && (jl_value_t*)vt == jl_typeof(jl_nothing))) {
n += jl_printf(out, "nothing");
}
else if (vt == jl_string_type) {
n += jl_printf(out, "\"");
jl_uv_puts(out, jl_string_data(v), jl_string_len(v)); n += jl_string_len(v);
n += jl_printf(out, "\"");
}
else if (v == jl_bottom_type) {
n += jl_printf(out, "Union{}");
}
else if (vt == jl_uniontype_type) {
n += jl_printf(out, "Union{");
while (jl_is_uniontype(v)) {
// tail-recurse on b to flatten the printing of the Union structure in the common case
n += jl_static_show_x(out, ((jl_uniontype_t*)v)->a, depth);
n += jl_printf(out, ", ");
v = ((jl_uniontype_t*)v)->b;
}
n += jl_static_show_x(out, v, depth);
n += jl_printf(out, "}");
}
else if (vt == jl_unionall_type) {
jl_unionall_t *ua = (jl_unionall_t*)v;
n += jl_static_show_x(out, ua->body, depth);
n += jl_printf(out, " where ");
n += jl_static_show_x(out, (jl_value_t*)ua->var, depth->prev);
}
else if (vt == jl_typename_type) {
n += jl_printf(out, "typename(");
n += jl_static_show_x(out, jl_unwrap_unionall(((jl_typename_t*)v)->wrapper), depth);
n += jl_printf(out, ")");
}
else if (vt == jl_tvar_type) {
// show type-var bounds only if they aren't going to be printed by UnionAll later
jl_tvar_t *var = (jl_tvar_t*)v;
struct recur_list *p;
int showbounds = 1;
for (p = depth; p != NULL; p = p->prev) {
if (jl_is_unionall(p->v) && ((jl_unionall_t*)p->v)->var == var) {
showbounds = 0;
break;
}
}
jl_value_t *lb = var->lb, *ub = var->ub;
if (showbounds && lb != jl_bottom_type) {
// show type-var lower bound if it is defined
int ua = jl_is_unionall(lb);
if (ua)
n += jl_printf(out, "(");
n += jl_static_show_x(out, lb, depth);
if (ua)
n += jl_printf(out, ")");
n += jl_printf(out, "<:");
}
n += jl_static_show_x_sym_escaped(out, var->name);
if (showbounds && (ub != (jl_value_t*)jl_any_type || lb != jl_bottom_type)) {
// show type-var upper bound if it is defined, or if we showed the lower bound
int ua = jl_is_unionall(ub);
n += jl_printf(out, "<:");
if (ua)
n += jl_printf(out, "(");
n += jl_static_show_x(out, ub, depth);
if (ua)
n += jl_printf(out, ")");
}
}
else if (vt == jl_module_type) {
jl_module_t *m = (jl_module_t*)v;
if (m->parent != m && m->parent != jl_main_module) {
n += jl_static_show_x(out, (jl_value_t*)m->parent, depth);
n += jl_printf(out, ".");
}
n += jl_printf(out, "%s", jl_symbol_name(m->name));
}
else if (vt == jl_symbol_type) {
char *sn = jl_symbol_name((jl_sym_t*)v);
int quoted = !jl_is_identifier(sn) && jl_operator_precedence(sn) == 0;
if (quoted)
n += jl_printf(out, "Symbol(\"");
else
n += jl_printf(out, ":");
n += jl_printf(out, "%s", sn);
if (quoted)
n += jl_printf(out, "\")");
}
else if (vt == jl_ssavalue_type) {
n += jl_printf(out, "SSAValue(%" PRIuPTR ")",
(uintptr_t)((jl_ssavalue_t*)v)->id);
}
else if (vt == jl_globalref_type) {
n += jl_static_show_x(out, (jl_value_t*)jl_globalref_mod(v), depth);
char *name = jl_symbol_name(jl_globalref_name(v));
n += jl_printf(out, jl_is_identifier(name) ? ".%s" : ".:(%s)", name);
}
else if (vt == jl_gotonode_type) {
n += jl_printf(out, "goto %" PRIuPTR, jl_gotonode_label(v));
}
else if (vt == jl_quotenode_type) {
jl_value_t *qv = *(jl_value_t**)v;
if (!jl_is_symbol(qv)) {
n += jl_printf(out, "quote ");
}
else {
n += jl_printf(out, ":(");
}
n += jl_static_show_x(out, qv, depth);
if (!jl_is_symbol(qv)) {
n += jl_printf(out, " end");
}
else {