forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.c
1387 lines (1264 loc) · 39.6 KB
/
builtins.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
/*
implementations of some built-in functions and utilities
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <assert.h>
#include <sys/types.h>
#include <limits.h>
#include <errno.h>
#include <math.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef BOEHM_GC
#include <gc.h>
#endif
#include "llt.h"
#include "julia.h"
#include "builtin_proto.h"
// --- exceptions ---
extern char *julia_home;
void jl_error(const char *str)
{
jl_value_t *msg = jl_pchar_to_string((char*)str, strlen(str));
JL_GC_PUSH(&msg);
jl_raise(jl_new_struct(jl_errorexception_type, msg));
}
void jl_errorf(const char *fmt, ...)
{
char buf[1024];
va_list args;
va_start(args, fmt);
int nc = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
jl_value_t *msg = jl_pchar_to_string(buf, nc);
JL_GC_PUSH(&msg);
jl_raise(jl_new_struct(jl_errorexception_type, msg));
}
void jl_too_few_args(const char *fname, int min)
{
// TODO: ArgumentError
jl_errorf("%s: too few arguments (expected %d)", fname, min);
}
void jl_too_many_args(const char *fname, int max)
{
jl_errorf("%s: too many arguments (expected %d)", fname, max);
}
void jl_type_error(const char *fname, jl_value_t *expected, jl_value_t *got)
{
jl_value_t *ex = jl_new_struct(jl_typeerror_type, jl_symbol(fname),
jl_an_empty_string, expected, got);
jl_raise(ex);
}
void jl_type_error_rt(const char *fname, const char *context,
jl_value_t *ty, jl_value_t *got)
{
jl_value_t *ctxt=NULL;
JL_GC_PUSH(&ctxt, &got);
ctxt = jl_pchar_to_string((char*)context, strlen(context));
jl_value_t *ex = jl_new_struct(jl_typeerror_type, jl_symbol(fname),
ctxt, ty, got);
jl_raise(ex);
}
JL_CALLABLE(jl_f_throw)
{
JL_NARGS(throw, 1, 1);
jl_raise(args[0]);
return (jl_value_t*)jl_null;
}
void jl_enter_handler(jl_savestate_t *ss, jmp_buf *handlr)
{
jl_eh_save_state(ss);
jl_current_task->state.prev = ss;
jl_current_task->state.eh_task = jl_current_task;
jl_current_task->state.eh_ctx = handlr;
}
void jl_pop_handler(int n)
{
while (n > 0) {
jl_eh_restore_state(jl_current_task->state.prev);
n--;
}
}
// --- primitives ---
JL_CALLABLE(jl_f_is)
{
JL_NARGS(is, 2, 2);
if (args[0] == args[1])
return jl_true;
return jl_false;
}
JL_CALLABLE(jl_f_no_function)
{
jl_error("function not defined");
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_typeof)
{
JL_NARGS(typeof, 1, 1);
return jl_full_type(args[0]);
}
JL_CALLABLE(jl_f_subtype)
{
JL_NARGS(subtype, 2, 2);
if (!jl_is_typector(args[0]) && !jl_is_typevar(args[0]))
JL_TYPECHK(subtype, type, args[0]);
if (!jl_is_typector(args[1]) && !jl_is_typevar(args[1]))
JL_TYPECHK(subtype, type, args[1]);
return (jl_subtype(args[0],args[1],0) ? jl_true : jl_false);
}
JL_CALLABLE(jl_f_isa)
{
JL_NARGS(isa, 2, 2);
if (!jl_is_typector(args[1]))
JL_TYPECHK(isa, type, args[1]);
return (jl_subtype(args[0],args[1],1) ? jl_true : jl_false);
}
JL_CALLABLE(jl_f_typeassert)
{
JL_NARGS(typeassert, 2, 2);
if (!jl_is_typector(args[1]))
JL_TYPECHK(typeassert, type, args[1]);
if (!jl_subtype(args[0],args[1],1))
jl_type_error("typeassert", args[1], args[0]);
return args[0];
}
JL_CALLABLE(jl_f_apply)
{
JL_NARGSV(apply, 1);
JL_TYPECHK(apply, function, args[0]);
if (nargs == 2 && jl_is_tuple(args[1])) {
return jl_apply((jl_function_t*)args[0], &jl_tupleref(args[1],0),
((jl_tuple_t*)args[1])->length);
}
size_t n=0, i, j;
for(i=1; i < nargs; i++) {
if (jl_is_tuple(args[i])) {
n += ((jl_tuple_t*)args[i])->length;
}
else if (jl_typeis(args[i], jl_array_any_type)) {
n += jl_array_len(args[i]);
}
else {
if (jl_append_any_func == NULL) {
// error if append_any not available
JL_TYPECHK(apply, tuple, args[i]);
}
goto fancy_apply;
}
}
jl_value_t **newargs = alloca(n * sizeof(jl_value_t*));
n = 0;
for(i=1; i < nargs; i++) {
if (jl_is_tuple(args[i])) {
jl_tuple_t *t = (jl_tuple_t*)args[i];
for(j=0; j < t->length; j++)
newargs[n++] = jl_tupleref(t, j);
}
else {
size_t al = jl_array_len(args[i]);
for(j=0; j < al; j++)
newargs[n++] = jl_cellref(args[i], j);
}
}
return jl_apply((jl_function_t*)args[0], newargs, n);
fancy_apply: ;
jl_value_t *argarr = jl_apply(jl_append_any_func, &args[1], nargs-1);
JL_GC_PUSH(&argarr);
assert(jl_typeis(argarr, jl_array_any_type));
jl_value_t *result = jl_apply((jl_function_t*)args[0],
jl_cell_data(argarr), jl_array_len(argarr));
JL_GC_POP();
return result;
}
// eval -----------------------------------------------------------------------
static int is_intrinsic(jl_sym_t *s)
{
jl_value_t **bp = jl_get_bindingp(jl_system_module, s);
return (*bp != NULL && jl_typeof(*bp)==(jl_type_t*)jl_intrinsic_type);
}
static int has_intrinsics(jl_expr_t *e)
{
if (e->head == call_sym && jl_is_symbol(jl_exprarg(e,0)) &&
is_intrinsic((jl_sym_t*)jl_exprarg(e,0)))
return 1;
int i;
for(i=0; i < e->args->length; i++) {
jl_value_t *a = jl_exprarg(e,i);
if (jl_is_expr(a) && has_intrinsics((jl_expr_t*)a))
return 1;
}
return 0;
}
// heuristic for whether a top-level input should be evaluated with
// the compiler or the interpreter.
static int eval_with_compiler_p(jl_expr_t *expr, int compileloops)
{
if (expr->head==body_sym) {
jl_array_t *body = expr->args;
size_t i;
for(i=0; i < body->length; i++) {
jl_value_t *stmt = jl_cellref(body,i);
if (jl_is_expr(stmt)) {
// TODO: only backward branches
if (compileloops &&
(((jl_expr_t*)stmt)->head == goto_sym ||
((jl_expr_t*)stmt)->head == goto_ifnot_sym)) {
return 1;
}
}
}
}
if (has_intrinsics(expr))
return 1;
return 0;
}
jl_value_t *jl_new_closure_internal(jl_lambda_info_t *li, jl_value_t *env);
jl_value_t *jl_toplevel_eval_flex(jl_value_t *ex, int fast)
{
//jl_show(ex);
//ios_printf(ios_stdout, "\n");
jl_lambda_info_t *thk;
int ewc = 0;
if (jl_typeof(ex) != (jl_type_t*)jl_lambda_info_type) {
if (jl_is_expr(ex) && eval_with_compiler_p((jl_expr_t*)ex, fast)) {
thk = jl_wrap_expr(ex);
ewc = 1;
}
else {
return jl_interpret_toplevel_expr(ex);
}
}
else {
thk = (jl_lambda_info_t*)ex;
ewc = eval_with_compiler_p(jl_lam_body((jl_expr_t*)thk->ast), fast);
}
jl_value_t *thunk=NULL;
jl_function_t *gf=NULL;
jl_value_t *result;
JL_GC_PUSH(&thunk, &gf, &thk);
if (ewc) {
thunk = jl_new_closure_internal(thk, (jl_value_t*)jl_null);
// use a generic function so type inference runs
gf = jl_new_generic_function(lambda_sym);
jl_add_method(gf, jl_null, (jl_function_t*)thunk);
result = jl_apply(gf, NULL, 0);
}
else {
result = jl_interpret_toplevel_thunk(thk);
}
JL_GC_POP();
return result;
}
jl_value_t *jl_toplevel_eval(jl_value_t *v)
{
return jl_toplevel_eval_flex(v, 1);
}
int asprintf(char **strp, const char *fmt, ...);
// load toplevel expressions, from (file ...)
void jl_load_file_expr(char *fname, jl_value_t *ast)
{
jl_array_t *b = ((jl_expr_t*)ast)->args;
size_t i;
volatile size_t lineno=0;
JL_TRY {
// handle syntax error
if (((jl_expr_t*)ast)->head == error_sym) {
jl_interpret_toplevel_expr(ast);
}
jl_value_t *form=NULL;
JL_GC_PUSH(&form);
for(i=0; i < b->length; i++) {
// process toplevel form
form = jl_cellref(b, i);
if (jl_is_expr(form) && ((jl_expr_t*)form)->head == line_sym) {
lineno = jl_unbox_int32(jl_exprarg(form, 0));
}
else {
if (jl_is_expr(form) &&
((jl_expr_t*)form)->head == unexpanded_sym) {
// delayed expansion, for macro calls
form = jl_exprarg(form, 0);
form = jl_expand(form);
}
//(void)jl_interpret_toplevel_thunk(form);
(void)jl_toplevel_eval_flex(form, 0);
}
}
JL_GC_POP();
}
JL_CATCH {
jl_value_t *fn=NULL, *ln=NULL;
JL_GC_PUSH(&fn, &ln);
fn = jl_pchar_to_string(fname, strlen(fname));
ln = jl_box_int32(lineno);
jl_raise(jl_new_struct(jl_loaderror_type, fn, ln,
jl_exception_in_transit));
}
}
void jl_load(const char *fname)
{
char *fpath = (char*)fname;
int fid = open (fpath, O_RDONLY);
// try adding just .j
if (fid == -1) {
asprintf(&fpath, "%s.j", fname);
fid = open (fpath, O_RDONLY);
}
// try adding julia home and .j
if (fid == -1 && julia_home && fname[0] != '/') {
asprintf(&fpath, "%s/%s", julia_home, fname);
fid = open (fpath, O_RDONLY);
if (fid == -1) {
asprintf(&fpath, "%s/%s.j", julia_home, fname);
fid = open (fpath, O_RDONLY);
}
}
if (fid == -1) {
jl_errorf("could not open file %s", fpath);
}
close(fid);
jl_value_t *ast = jl_parse_file(fpath);
if (ast == (jl_value_t*)jl_null)
jl_errorf("could not open file %s", fpath);
JL_GC_PUSH(&ast);
jl_load_file_expr(fpath, ast);
JL_GC_POP();
if (fpath != fname) free(fpath);
}
JL_CALLABLE(jl_f_top_eval)
{
JL_NARGS(eval, 1, 1);
jl_value_t *e = args[0];
if (!jl_is_expr(e))
return jl_interpret_toplevel_expr(e);
jl_expr_t *ex = (jl_expr_t*)e;
if (ex->head == symbol_sym || ex->head == top_sym ||
ex->head == quote_sym || ex->head == null_sym ||
ex->head == unbound_sym || ex->head == error_sym) {
// expression types simple enough not to need expansion
return jl_interpret_toplevel_expr(e);
}
jl_value_t *exex = NULL;
JL_GC_PUSH(&exex);
exex = jl_expand(e);
jl_value_t *result = jl_toplevel_eval(exex);
JL_GC_POP();
return result;
}
JL_CALLABLE(jl_f_isbound)
{
JL_NARGS(isbound, 1, 1);
JL_TYPECHK(isbound, symbol, args[0]);
return jl_boundp(jl_system_module, (jl_sym_t*)args[0]) ? jl_true : jl_false;
}
// tuples ---------------------------------------------------------------------
JL_CALLABLE(jl_f_tuple)
{
size_t i;
if (nargs == 0) return (jl_value_t*)jl_null;
jl_tuple_t *t = jl_alloc_tuple_uninit(nargs);
for(i=0; i < nargs; i++) {
jl_tupleset(t, i, args[i]);
}
return (jl_value_t*)t;
}
JL_CALLABLE(jl_f_tupleref)
{
JL_NARGS(tupleref, 2, 2);
JL_TYPECHK(tupleref, tuple, args[0]);
JL_TYPECHK(tupleref, int32, args[1]);
jl_tuple_t *t = (jl_tuple_t*)args[0];
size_t i = jl_unbox_int32(args[1])-1;
if (i >= t->length)
jl_error("tupleref: index out of range");
return jl_tupleref(t, i);
}
JL_CALLABLE(jl_f_tuplelen)
{
JL_NARGS(tuplelen, 1, 1);
JL_TYPECHK(tuplelen, tuple, args[0]);
return jl_box_int32(((jl_tuple_t*)args[0])->length);
}
// structs --------------------------------------------------------------------
static size_t field_offset(jl_struct_type_t *t, jl_sym_t *fld, int err)
{
jl_tuple_t *fn = t->names;
size_t i;
for(i=0; i < fn->length; i++) {
if (jl_tupleref(fn,i) == (jl_value_t*)fld) {
if (t == jl_struct_kind || t == jl_bits_kind || t == jl_tag_kind)
i += 3;
return i;
}
}
if (err)
jl_errorf("type %s has no field %s", t->name->name->name, fld->name);
return -1;
}
size_t jl_field_offset(jl_struct_type_t *t, jl_sym_t *fld)
{
return field_offset(t, fld, 0);
}
JL_CALLABLE(jl_f_get_field)
{
JL_NARGS(getfield, 2, 2);
JL_TYPECHK(getfield, symbol, args[1]);
jl_value_t *v = args[0];
if (!jl_is_struct_type(jl_typeof(v)))
// TODO: string is leaked
jl_errorf("getfield: argument must be a struct, got %s",
jl_show_to_string(v));
size_t i = field_offset((jl_struct_type_t*)jl_typeof(v),
(jl_sym_t*)args[1], 1);
return ((jl_value_t**)v)[1+i];
}
JL_CALLABLE(jl_f_set_field)
{
JL_NARGS(setfield, 3, 3);
JL_TYPECHK(setfield, symbol, args[1]);
jl_value_t *v = args[0];
if (!jl_is_struct_type(jl_typeof(v)))
// TODO: string is leaked
jl_errorf("setfield: argument must be a struct, got %s",
jl_show_to_string(v));
jl_struct_type_t *st = (jl_struct_type_t*)jl_typeof(v);
size_t i = field_offset(st, (jl_sym_t*)args[1], 1);
((jl_value_t**)v)[1+i] = jl_convert((jl_type_t*)jl_tupleref(st->types,i),
args[2]);
return v;
}
// --- conversions ---
static jl_tuple_t *convert_tuple(jl_tuple_t *to, jl_tuple_t *x)
{
if (to == jl_tuple_type)
return x;
size_t i, cl=x->length, pl=to->length;
jl_tuple_t *out = jl_alloc_tuple(cl);
JL_GC_PUSH(&out);
jl_value_t *ce, *pe;
int pseq=0;
for(i=0; i < cl; i++) {
ce = jl_tupleref(x,i);
if (pseq) {
}
else if (i < pl) {
pe = jl_tupleref(to,i);
if (jl_is_seq_type(pe)) {
pe = jl_tparam0(pe);
pseq = 1;
}
}
else {
out = NULL;
break;
}
jl_tupleset(out, i, jl_convert((jl_type_t*)pe, ce));
}
JL_GC_POP();
return out;
}
jl_function_t *jl_convert_gf;
jl_value_t *jl_convert(jl_type_t *to, jl_value_t *x)
{
jl_value_t *args[2];
args[0] = (jl_value_t*)to; args[1] = x;
return jl_apply(jl_convert_gf, args, 2);
}
JL_CALLABLE(jl_f_convert)
{
JL_NARGS(convert, 2, 2);
if (!jl_is_typector(args[0]))
JL_TYPECHK(convert, type, args[0]);
jl_type_t *to = (jl_type_t*)args[0];
jl_value_t *x = args[1];
jl_value_t *out;
if (jl_is_tuple(x) && jl_is_tuple(to)) {
out = (jl_value_t*)convert_tuple((jl_tuple_t*)to, (jl_tuple_t*)x);
if (out == NULL)
jl_error("convert: invalid tuple conversion");
return out;
}
if (jl_subtype(x, (jl_value_t*)to, 1))
return x;
// TODO: string is leaked
jl_errorf("cannot convert %s to %s",
jl_show_to_string(x), jl_show_to_string((jl_value_t*)to));
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_convert_to_ptr)
{
JL_NARGS(convert, 2, 2);
assert(jl_is_cpointer_type(args[0]));
jl_value_t *v = args[1];
jl_value_t *elty = jl_tparam0(args[0]);
void *p=NULL;
if (v == (jl_value_t*)jl_null) {
p = NULL;
}
else if (jl_is_cpointer(v)) {
p = jl_unbox_pointer(v);
}
else if (jl_is_array(v) && (elty == (jl_value_t*)jl_bottom_type ||
jl_tparam0(jl_typeof(v)) == elty)) {
p = ((jl_array_t*)v)->data;
}
else if (jl_is_symbol(v) && elty == (jl_value_t*)jl_uint8_type) {
p = ((jl_sym_t*)v)->name;
}
else {
// TODO: string is leaked
jl_errorf("cannot convert %s to %s",
jl_show_to_string(v), jl_show_to_string(args[0]));
}
return jl_box_pointer((jl_bits_type_t*)args[0], p);
}
// --- printing ---
jl_function_t *jl_print_gf;
JL_CALLABLE(jl_f_print_array_uint8)
{
ios_t *s = jl_current_output_stream();
jl_array_t *b = (jl_array_t*)args[0];
ios_write(s, (char*)b->data, b->length);
return (jl_value_t*)jl_null;
}
// --- showing ---
jl_function_t *jl_show_gf;
void jl_show(jl_value_t *v)
{
jl_apply(jl_show_gf, &v, 1);
}
char *jl_show_to_string(jl_value_t *v)
{
ios_t tmp, *dest=NULL;
jl_value_t *ioo = NULL;
// make a proper IOStream object if available, otherwise go underneath
if (jl_memio_func != NULL) {
ioo = jl_apply(jl_memio_func, NULL, 0);
}
// use try/catch to reset the current output stream
// if an error occurs during printing.
JL_TRY {
if (ioo) {
jl_set_current_output_stream_obj(ioo);
dest = jl_current_output_stream();
}
else {
ios_mem(&tmp, 0);
dest = &tmp;
jl_current_task->state.current_output_stream = dest;
}
jl_show(v);
}
JL_CATCH {
jl_raise(jl_exception_in_transit);
}
size_t n;
assert(dest != NULL);
return ios_takebuf(dest, &n);
}
// comma_one prints a comma for 1 element, e.g. "(x,)"
static void show_tuple(jl_tuple_t *t, char opn, char cls, int comma_one)
{
ios_t *s = jl_current_output_stream();
ios_putc(opn, s);
size_t i, n=t->length;
for(i=0; i < n; i++) {
jl_show(jl_tupleref(t, i));
if ((i < n-1) || (n==1 && comma_one))
ios_putc(',', s);
}
ios_putc(cls, s);
}
static void show_function(jl_value_t *v)
{
ios_t *s = jl_current_output_stream();
if (jl_is_gf(v)) {
ios_putc('(', s);
ios_puts(jl_gf_name(v)->name, s);
ios_putc(')', s);
}
else {
ios_puts("#<function>", s);
}
}
void jl_show_full_function(jl_value_t *v)
{
ios_t *s = jl_current_output_stream();
if (jl_is_gf(v)) {
ios_puts("Methods for generic function ", s);
ios_puts(jl_gf_name(v)->name, s);
ios_putc('\n', s);
jl_show_method_table((jl_function_t*)v);
}
else {
show_function(v);
}
}
static void show_type(jl_value_t *t)
{
ios_t *s = jl_current_output_stream();
if (jl_is_func_type(t)) {
jl_show((jl_value_t*)((jl_func_type_t*)t)->from);
ios_write(s, "-->", 3);
jl_show((jl_value_t*)((jl_func_type_t*)t)->to);
}
else if (jl_is_union_type(t)) {
if (t == (jl_value_t*)jl_bottom_type) {
ios_write(s, "None", 4);
}
else {
ios_write(s, "Union", 5);
show_tuple(((jl_uniontype_t*)t)->types, '(', ')', 0);
}
}
else if (jl_is_seq_type(t)) {
jl_show(jl_tparam0(t));
ios_write(s, "...", 3);
}
else {
assert(jl_is_some_tag_type(t));
jl_tag_type_t *tt = (jl_tag_type_t*)t;
ios_puts(tt->name->name->name, s);
jl_tuple_t *p = tt->parameters;
if (p->length > 0)
show_tuple(p, '{', '}', 0);
}
}
static void show_int(void *data, int nbits)
{
ios_t *s = jl_current_output_stream();
switch (nbits) {
case 8:
ios_printf(s, "%hhd", *(int8_t*)data);
break;
case 16:
ios_printf(s, "%hd", *(int16_t*)data);
break;
case 32:
ios_printf(s, "%d", *(int32_t*)data);
break;
case 64:
ios_printf(s, "%lld", *(int64_t*)data);
break;
default:
jl_error("print: unsupported integer size");
}
}
static void show_uint(void *data, int nbits)
{
ios_t *s = jl_current_output_stream();
switch (nbits) {
case 8:
ios_printf(s, "%hhu", *(int8_t*)data);
break;
case 16:
ios_printf(s, "%hu", *(int16_t*)data);
break;
case 32:
ios_printf(s, "%u", *(int32_t*)data);
break;
case 64:
ios_printf(s, "%llu", *(int64_t*)data);
break;
default:
jl_error("print: unsupported integer size");
}
}
static void show_float64(double d, int single)
{
ios_t *s = jl_current_output_stream();
char buf[64];
int ndec = single ? 8 : 16;
if (!DFINITE(d)) {
char *rep;
if (isnan(d))
rep = "NaN";
else
rep = sign_bit(d) ? "-Inf" : "Inf";
if (single)
ios_printf(s, "float32(%s)", rep);
else
ios_puts(rep, s);
}
else if (d == 0) {
if (1/d < 0)
ios_puts("-0.0", s);
else
ios_puts("0.0", s);
}
else {
snprint_real(buf, sizeof(buf), d, 0, ndec, 3, 10);
int hasdec = (strpbrk(buf, ".eE") != NULL);
ios_puts(buf, s);
if (!hasdec) ios_puts(".0", s);
}
}
JL_CALLABLE(jl_f_show_bool)
{
ios_t *s = jl_current_output_stream();
if (jl_unbox_bool(args[0]) == 0)
ios_puts("false", s);
else
ios_puts("true", s);
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_show_char)
{
ios_t *s = jl_current_output_stream();
u_int32_t wc = *(uint32_t*)jl_bits_data(args[0]);
ios_putc('\'', s);
ios_pututf8(s, wc);
ios_putc('\'', s);
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_show_float32)
{
show_float64((double)*(float*)jl_bits_data(args[0]), 1);
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_show_float64)
{
show_float64(*(double*)jl_bits_data(args[0]), 0);
return (jl_value_t*)jl_null;
}
#define INT_SHOW_FUNC(sgn,nb) \
JL_CALLABLE(jl_f_show_##sgn##nb) \
{ \
show_##sgn(jl_bits_data(args[0]), nb); \
return (jl_value_t*)jl_null; \
}
INT_SHOW_FUNC(int,8)
INT_SHOW_FUNC(uint,8)
INT_SHOW_FUNC(int,16)
INT_SHOW_FUNC(uint,16)
INT_SHOW_FUNC(int,32)
INT_SHOW_FUNC(uint,32)
INT_SHOW_FUNC(int,64)
INT_SHOW_FUNC(uint,64)
JL_CALLABLE(jl_f_show_pointer)
{
ios_t *s = jl_current_output_stream();
void *ptr = *(void**)jl_bits_data(args[0]);
if (jl_typeis(args[0],jl_pointer_void_type))
ios_printf(s, "Ptr{Void}");
else
jl_show((jl_value_t*)jl_typeof(args[0]));
#ifdef BITS64
ios_printf(s, " @0x%016x", (uptrint_t)ptr);
#else
ios_printf(s, " @0x%08x", (uptrint_t)ptr);
#endif
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_print_symbol)
{
ios_t *s = jl_current_output_stream();
ios_puts(((jl_sym_t*)args[0])->name, s);
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_show_typevar)
{
ios_t *s = jl_current_output_stream();
jl_tvar_t *tv = (jl_tvar_t*)args[0];
if (tv->lb != (jl_value_t*)jl_bottom_type) {
jl_show((jl_value_t*)tv->lb);
ios_puts("<:", s);
}
ios_puts(tv->name->name, s);
if (tv->ub != (jl_value_t*)jl_any_type) {
ios_puts("<:", s);
jl_show((jl_value_t*)tv->ub);
}
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_show_linfo)
{
ios_t *s = jl_current_output_stream();
ios_puts("AST(", s);
jl_show(((jl_lambda_info_t*)args[0])->ast);
ios_putc(')', s);
return (jl_value_t*)jl_null;
}
JL_CALLABLE(jl_f_show_any)
{
JL_NARGS(print, 1, 1);
// fallback for printing some other builtin types
ios_t *s = jl_current_output_stream();
jl_value_t *v = args[0];
if (jl_is_tuple(v)) {
show_tuple((jl_tuple_t*)v, '(', ')', 1);
}
else if (jl_is_type(v)) {
show_type(v);
}
else if (jl_is_func(v)) {
show_function(v);
}
else if (jl_typeis(v,jl_intrinsic_type)) {
ios_printf(s, "#<intrinsic-function %d>", *(uint32_t*)jl_bits_data(v));
}
else if (v == (jl_value_t*)jl_function_type) {
ios_printf(s, "Function");
}
else {
jl_value_t *t = (jl_value_t*)jl_typeof(v);
if (jl_is_bits_type(t)) {
show_uint(jl_bits_data(v), jl_bitstype_nbits(t));
}
else {
assert(jl_is_struct_type(t));
jl_struct_type_t *st = (jl_struct_type_t*)t;
ios_puts(st->name->name->name, s);
ios_putc('(', s);
size_t i;
size_t n = st->names->length;
for(i=0; i < n; i++) {
jl_show(((jl_value_t**)v)[i+1]);
if (i < n-1)
ios_putc(',', s);
}
ios_putc(')', s);
}
}
return (jl_value_t*)jl_null;
}
// --- RTS primitives ---
JL_CALLABLE(jl_trampoline)
{
jl_function_t *f = (jl_function_t*)jl_t0(env);
assert(jl_is_func(f));
assert(f->linfo != NULL);
jl_compile(f);
jl_generate_fptr(f);
assert(f->fptr != NULL);
return jl_apply(f, args, nargs);
}
DLLEXPORT
jl_value_t *jl_new_closure_internal(jl_lambda_info_t *li, jl_value_t *env)
{
assert(jl_is_lambda_info(li));
assert(jl_is_tuple(env));
jl_function_t *f=NULL;
// note: env is pushed here to make codegen a little easier
JL_GC_PUSH(&f, &env);
if (li->fptr != NULL) {
// function has been compiled
f = jl_new_closure(li->fptr, env);
}
else {
f = jl_new_closure(jl_trampoline, NULL);
f->env = (jl_value_t*)jl_tuple2((jl_value_t*)f, env);
}
f->linfo = li;
JL_GC_POP();
return (jl_value_t*)f;
}
JL_CALLABLE(jl_f_instantiate_type)
{
JL_NARGSV(instantiate_type, 1);
if (!jl_is_some_tag_type(args[0]))
JL_TYPECHK(instantiate_type, typector, args[0]);
jl_tuple_t *tparams = (jl_tuple_t*)jl_f_tuple(NULL, &args[1], nargs-1);
JL_GC_PUSH(&tparams);
jl_value_t *v = jl_apply_type(args[0], tparams);
JL_GC_POP();
return v;
}
static int all_typevars(jl_tuple_t *p)
{
size_t i;
for(i=0; i < p->length; i++) {
if (!jl_is_typevar(jl_tupleref(p,i)))
return 0;
}
return 1;
}
static void check_supertype(jl_value_t *super, char *name)
{
if (!(/*jl_is_struct_type(super) || */jl_is_tag_type(super)) ||
super == (jl_value_t*)jl_sym_type ||
super == (jl_value_t*)jl_undef_type ||
jl_subtype(super,(jl_value_t*)jl_type_type,0) ||
jl_subtype(super,(jl_value_t*)jl_array_type,0)) {
jl_errorf("invalid subtyping in definition of %s", name);
}
}
JL_CALLABLE(jl_f_new_struct_type)
{
JL_NARGS(new_struct_type, 4, 4);
JL_TYPECHK(new_struct_type, symbol, args[0]);
JL_TYPECHK(new_struct_type, tuple, args[1]);
JL_TYPECHK(new_struct_type, tuple, args[2]);
if (args[3] != (jl_value_t*)jl_null)
JL_TYPECHK(new_struct_type, function, args[3]);
jl_sym_t *name = (jl_sym_t*)args[0];
jl_tuple_t *params = (jl_tuple_t*)args[1];
jl_tuple_t *fnames = (jl_tuple_t*)args[2];
if (!all_typevars(params))
jl_errorf("invalid type parameter list for %s", name->name);
jl_struct_type_t *nst =
jl_new_struct_type(name, jl_any_type, params, fnames, NULL);
nst->ctor_factory = args[3];
return (jl_value_t*)nst;
}
void jl_add_constructors(jl_struct_type_t *t);
JL_CALLABLE(jl_f_new_struct_fields)
{
JL_NARGS(new_struct_fields, 3, 3);
jl_value_t *super = args[1];
JL_TYPECHK(new_struct_fields, tuple, args[2]);
jl_value_t *t = args[0];
jl_tuple_t *ftypes = (jl_tuple_t*)args[2];