Skip to content
This repository
tree: 71d75fb7d4
Fetching contributors…

Cannot retrieve contributors at this time

file 1104 lines (963 sloc) 38.774 kb
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
#ifndef JULIA_H
#define JULIA_H

#if (defined(_WIN32) || defined (_MSC_VER)) && !defined(__WIN32__)
#define __WIN32__
#endif

#include "libsupport.h"
#include <stdint.h>
#include "uv.h"

#define JL_GC_MARKSWEEP

#include "htable.h"
#include "arraylist.h"

#include <setjmp.h>
#if defined(__FreeBSD__)
# define jl_jmp_buf sigjmp_buf
#else
# define jl_jmp_buf jmp_buf
#endif

// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif

// Check GCC
#if __GNUC__
#define NORETURN __attribute__ ((noreturn))
#if __x86_64__ || __ppc64__
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#else
#define NORETURN
#endif


#define JL_STRUCT_TYPE \
struct _jl_type_t *type;

typedef struct _jl_value_t {
    JL_STRUCT_TYPE
} jl_value_t;

typedef struct _jl_sym_t {
    JL_STRUCT_TYPE
    struct _jl_sym_t *left;
    struct _jl_sym_t *right;
    uptrint_t hash; // precomputed hash value
    union {
        char name[1];
        void *_pad; // ensure field aligned to pointer size
    };
} jl_sym_t;

typedef struct {
    JL_STRUCT_TYPE
    size_t length;
    jl_value_t *data[1];
} jl_tuple_t;

typedef struct {
    JL_STRUCT_TYPE
    size_t length;
    jl_value_t *data[2];
} jl_tuple2_t;

// pseudo-object to track managed malloc pointers
// currently only referenced from an array's data owner field
typedef struct _jl_mallocptr_t {
    struct _jl_mallocptr_t *next;
    size_t sz;
    void *ptr;
} jl_mallocptr_t;

// how much space we're willing to waste if an array outgrows its
// original object
#define ARRAY_INLINE_NBYTES (2048*sizeof(void*))

/*
array data is allocated in two ways: either inline in the array object,
(in _space), or with malloc with data owner pointing to a jl_mallocptr_t.
data owner can also point to another array, if the original data was
allocated inline.
*/
typedef struct {
    JL_STRUCT_TYPE
    void *data;
    size_t length;

    unsigned short ndims:14;
    unsigned short ptrarray:1; // representation is pointer array
    unsigned short ismalloc:1; // data owner is a jl_mallocptr_t
    uint16_t elsize;
    uint32_t offset; // for 1-d only. does not need to get big.

    size_t nrows;
    union {
        // 1d
        size_t maxsize;
        // Nd
        size_t ncols;
    };
    // other dim sizes go here for ndims > 2

    union {
        char _space[1];
        void *_pad;
    };
} jl_array_t;

#define jl_array_len(a) (((jl_array_t*)(a))->length)
#define jl_array_data(a) ((void*)((jl_array_t*)(a))->data)
#define jl_array_dim(a,i) ((&((jl_array_t*)(a))->nrows)[i])
#define jl_array_ndims(a) ((int32_t)(((jl_array_t*)a)->ndims))
#define jl_array_data_owner(a) (*((jl_value_t**)jl_array_inline_data_area(a)))

// compute # of extra words needed to store dimensions
static inline int jl_array_ndimwords(uint32_t ndims)
{
#ifdef __LP64__
    // on 64-bit, ndimwords must be even to give 16-byte alignment
    return (ndims == 0 ? 0 : ((ndims-1) & -2));
#else
    // on 32-bit, ndimwords must = 4k+1 to give 16-byte alignment
    return (ndims & -4) + 1;
#endif
}

static inline void *jl_array_inline_data_area(jl_array_t *a)
{
    return &a->_space[0] + jl_array_ndimwords(jl_array_ndims(a))*sizeof(size_t);
}

typedef struct _jl_type_t {
    JL_STRUCT_TYPE
} jl_type_t;

typedef jl_value_t *(*jl_fptr_t)(jl_value_t*, jl_value_t**, uint32_t);

typedef struct _jl_lambda_info_t {
    JL_STRUCT_TYPE
    // this holds the static data for a function:
    // a syntax tree, static parameters, and (if it has been compiled)
    // a function pointer.
    // this is the stuff that's shared among different instantiations
    // (different environments) of a closure.
    jl_value_t *ast;
    // sparams is a tuple (symbol, value, symbol, value, ...)
    jl_tuple_t *sparams;
    jl_value_t *tfunc;
    jl_sym_t *name; // for error reporting
    jl_array_t *roots; // pointers in generated code
    jl_value_t *specTypes; // argument types this is specialized for
    // a slower-but-works version of this function as a fallback
    struct _jl_function_t *unspecialized;
    // pairlist of all lambda infos with code generated from this one
    jl_array_t *specializations;
    int8_t inferred;
    jl_value_t *file;
    ptrint_t line;
    struct _jl_module_t *module;

    // hidden fields:
    jl_fptr_t fptr;
    void *functionObject;
    // flag telling if inference is running on this function
    // used to avoid infinite recursion
    uptrint_t inInference : 1;
    uptrint_t inCompile : 1;
} jl_lambda_info_t;

#define LAMBDA_INFO_NW (NWORDS(sizeof(jl_lambda_info_t))-1)

#define JL_FUNC_FIELDS \
jl_fptr_t fptr; \
jl_value_t *env; \
jl_lambda_info_t *linfo;

typedef struct _jl_function_t {
    JL_STRUCT_TYPE
    JL_FUNC_FIELDS
} jl_function_t;

typedef struct {
    JL_STRUCT_TYPE
    jl_tuple_t *parameters;
    jl_type_t *body;
} jl_typector_t;

typedef struct {
    JL_STRUCT_TYPE
    jl_sym_t *name;
    struct _jl_module_t *module;
    // if this is the name of a parametric type, this field points to the
    // original type.
    // a type alias, for example, might make a type constructor that is
    // not the original.
    jl_value_t *primary;
    jl_tuple_t *cache;
} jl_typename_t;

typedef struct {
    JL_STRUCT_TYPE
    jl_tuple_t *types;
} jl_uniontype_t;

typedef struct _jl_tag_type_t {
    JL_STRUCT_TYPE
    JL_FUNC_FIELDS
    jl_typename_t *name;
    struct _jl_tag_type_t *super;
    jl_tuple_t *parameters;
} jl_tag_type_t;

typedef struct {
    uint16_t offset; // offset relative to data start, excluding type tag
    uint16_t size:15;
    uint16_t isptr:1;
} jl_fielddesc_t;

typedef struct {
    JL_STRUCT_TYPE
    JL_FUNC_FIELDS
    jl_typename_t *name;
    jl_tag_type_t *super;
    jl_tuple_t *parameters;
    jl_tuple_t *names;
    jl_tuple_t *types;
    // to create a set of constructors for this sort of type
    jl_value_t *ctor_factory;
    jl_value_t *instance; // for singletons
    // hidden fields:
    uint32_t uid;
    uint32_t size;
    uint32_t alignment; // strictest alignment over all fields
    jl_fielddesc_t fields[1];
} jl_struct_type_t;

#define jl_field_offset(st,i) (((jl_struct_type_t*)st)->fields[i].offset)
#define jl_field_size(st,i) (((jl_struct_type_t*)st)->fields[i].size)

typedef struct {
    JL_STRUCT_TYPE
    JL_FUNC_FIELDS
    jl_typename_t *name;
    jl_tag_type_t *super;
    jl_tuple_t *parameters;
    int32_t nbits;
    // hidden fields:
    uint32_t uid;
} jl_bits_type_t;

typedef struct {
    JL_STRUCT_TYPE
    jl_sym_t *name;
    jl_value_t *lb; // lower bound
    jl_value_t *ub; // upper bound
    uptrint_t bound; // part of a constraint environment
} jl_tvar_t;

typedef struct {
    JL_STRUCT_TYPE
    jl_value_t *value;
} jl_weakref_t;

typedef struct {
    // not first-class
    jl_sym_t *name;
    jl_value_t *value;
    jl_type_t *type;
    struct _jl_module_t *owner; // for individual imported bindings
    int constp:1;
    int exportp:1;
} jl_binding_t;

typedef struct _jl_module_t {
    JL_STRUCT_TYPE
    jl_sym_t *name;
    jl_value_t *parent;
    htable_t bindings;
    arraylist_t imports; // modules with all bindings imported
} jl_module_t;

typedef struct _jl_methlist_t {
    JL_STRUCT_TYPE
    jl_tuple_t *sig;
    int8_t va;
    jl_tuple_t *tvars;
    jl_function_t *func;
    // cache of specializations of this method for invoke(), i.e.
    // cases where this method was called even though it was not necessarily
    // the most specific for the argument types.
    struct _jl_methtable_t *invokes;
    // TODO: pointer from specialized to original method
    //jl_function_t *orig_method;
    struct _jl_methlist_t *next;
} jl_methlist_t;

//#define JL_GF_PROFILE

typedef struct _jl_methtable_t {
    JL_STRUCT_TYPE
    jl_sym_t *name;
    jl_methlist_t *defs;
    jl_methlist_t *cache;
    jl_array_t *cache_arg1;
    jl_array_t *cache_targ;
    ptrint_t max_args; // max # of non-vararg arguments in a signature
#ifdef JL_GF_PROFILE
    int ncalls;
#endif
} jl_methtable_t;

typedef struct {
    JL_STRUCT_TYPE
    jl_sym_t *head;
    jl_array_t *args;
    jl_value_t *etype;
} jl_expr_t;

extern jl_tag_type_t *jl_any_type;
extern jl_tag_type_t *jl_type_type;
extern jl_tvar_t *jl_typetype_tvar;
extern jl_tag_type_t *jl_typetype_type;
extern jl_value_t *jl_ANY_flag;
extern jl_tag_type_t *jl_undef_type;
extern jl_struct_type_t *jl_typename_type;
extern jl_struct_type_t *jl_typector_type;
extern jl_struct_type_t *jl_sym_type;
extern jl_struct_type_t *jl_symbol_type;
extern jl_tuple_t *jl_tuple_type;
extern jl_tag_type_t *jl_ntuple_type;
extern jl_typename_t *jl_ntuple_typename;
extern jl_struct_type_t *jl_tvar_type;
extern jl_struct_type_t *jl_task_type;

extern jl_struct_type_t *jl_union_kind;
extern jl_struct_type_t *jl_tag_kind;
extern jl_struct_type_t *jl_tag_type_type;
extern DLLEXPORT jl_struct_type_t *jl_struct_kind;
extern jl_struct_type_t *jl_bits_kind;

extern jl_type_t *jl_bottom_type;
extern jl_value_t *jl_top_type;
extern jl_struct_type_t *jl_lambda_info_type;
extern DLLEXPORT jl_struct_type_t *jl_module_type;
extern jl_tag_type_t *jl_seq_type;
extern jl_struct_type_t *jl_function_type;
extern jl_tag_type_t *jl_abstractarray_type;
extern jl_struct_type_t *jl_array_type;
extern jl_typename_t *jl_array_typename;
extern jl_struct_type_t *jl_weakref_type;
extern DLLEXPORT jl_struct_type_t *jl_ascii_string_type;
extern DLLEXPORT jl_struct_type_t *jl_utf8_string_type;
extern DLLEXPORT jl_struct_type_t *jl_errorexception_type;
extern jl_struct_type_t *jl_typeerror_type;
extern DLLEXPORT jl_struct_type_t *jl_loaderror_type;
extern DLLEXPORT jl_struct_type_t *jl_backtrace_type;
extern jl_value_t *jl_stackovf_exception;
extern jl_value_t *jl_memory_exception;
extern jl_value_t *jl_divbyzero_exception;
extern jl_value_t *jl_domain_exception;
extern jl_value_t *jl_overflow_exception;
extern jl_value_t *jl_inexact_exception;
extern jl_value_t *jl_undefref_exception;
extern jl_value_t *jl_interrupt_exception;
extern jl_value_t *jl_an_empty_cell;

extern jl_struct_type_t *jl_box_type;
extern jl_type_t *jl_box_any_type;
extern jl_typename_t *jl_box_typename;

extern jl_bits_type_t *jl_bool_type;
extern jl_bits_type_t *jl_char_type;
extern jl_bits_type_t *jl_int8_type;
extern jl_bits_type_t *jl_uint8_type;
extern jl_bits_type_t *jl_int16_type;
extern jl_bits_type_t *jl_uint16_type;
extern jl_bits_type_t *jl_int32_type;
extern jl_bits_type_t *jl_uint32_type;
extern jl_bits_type_t *jl_int64_type;
extern jl_bits_type_t *jl_uint64_type;
extern jl_bits_type_t *jl_float32_type;
extern jl_bits_type_t *jl_float64_type;

extern jl_bits_type_t *jl_pointer_type;

extern jl_type_t *jl_array_uint8_type;
extern jl_type_t *jl_array_any_type;
extern jl_type_t *jl_array_symbol_type;
extern DLLEXPORT jl_struct_type_t *jl_expr_type;
extern jl_struct_type_t *jl_symbolnode_type;
extern jl_struct_type_t *jl_getfieldnode_type;
extern jl_struct_type_t *jl_linenumbernode_type;
extern jl_struct_type_t *jl_labelnode_type;
extern jl_struct_type_t *jl_gotonode_type;
extern jl_struct_type_t *jl_quotenode_type;
extern jl_struct_type_t *jl_topnode_type;
extern jl_bits_type_t *jl_intrinsic_type;
extern jl_struct_type_t *jl_methtable_type;
extern jl_struct_type_t *jl_method_type;
extern jl_struct_type_t *jl_task_type;

extern jl_tuple_t *jl_null;
#define JL_NULL ((void*)jl_null)
extern jl_value_t *jl_true;
extern jl_value_t *jl_false;
DLLEXPORT extern jl_value_t *jl_nothing;

extern jl_function_t *jl_method_missing_func;
extern jl_function_t *jl_unprotect_stack_func;
extern jl_function_t *jl_bottom_func;

extern uv_lib_t *jl_dl_handle;
#if defined(__WIN32__) || defined (_WIN32)
extern uv_lib_t *jl_ntdll_handle;
extern uv_lib_t *jl_kernel32_handle;
extern uv_lib_t *jl_crtdll_handle;
extern uv_lib_t *jl_winsock_handle;
#endif

// some important symbols
extern jl_sym_t *call_sym;
extern jl_sym_t *call1_sym;
extern jl_sym_t *dots_sym;
extern jl_sym_t *quote_sym;
extern jl_sym_t *top_sym;
extern jl_sym_t *line_sym; extern jl_sym_t *toplevel_sym;
extern DLLEXPORT jl_sym_t *jl_continue_sym;
extern jl_sym_t *error_sym; extern jl_sym_t *amp_sym;
extern jl_sym_t *module_sym; extern jl_sym_t *colons_sym;
extern jl_sym_t *export_sym; extern jl_sym_t *import_sym;
extern jl_sym_t *importall_sym;
extern jl_sym_t *goto_sym; extern jl_sym_t *goto_ifnot_sym;
extern jl_sym_t *label_sym; extern jl_sym_t *return_sym;
extern jl_sym_t *lambda_sym; extern jl_sym_t *assign_sym;
extern jl_sym_t *null_sym; extern jl_sym_t *body_sym;
extern jl_sym_t *macro_sym; extern jl_sym_t *method_sym;
extern jl_sym_t *enter_sym; extern jl_sym_t *leave_sym;
extern jl_sym_t *exc_sym; extern jl_sym_t *new_sym;
extern jl_sym_t *static_typeof_sym;
extern jl_sym_t *const_sym; extern jl_sym_t *thunk_sym;
extern jl_sym_t *anonymous_sym; extern jl_sym_t *underscore_sym;
extern jl_sym_t *abstracttype_sym; extern jl_sym_t *bitstype_sym;
extern jl_sym_t *compositetype_sym; extern jl_sym_t *type_goto_sym;
extern jl_sym_t *global_sym;

#ifdef __LP64__
#define NWORDS(sz) (((sz)+7)>>3)
#else
#define NWORDS(sz) (((sz)+3)>>2)
#endif

#ifdef JL_GC_MARKSWEEP
void *allocb(size_t sz);
void *allocobj(size_t sz);
#else
#define allocb(nb) malloc(nb)
#define allocobj(nb) malloc(nb)
#endif

#define jl_tupleref(t,i) (((jl_value_t**)(t))[2+(i)])
#define jl_tupleset(t,i,x) ((((jl_value_t**)(t))[2+(i)])=(jl_value_t*)(x))
#define jl_t0(t) jl_tupleref(t,0)
#define jl_t1(t) jl_tupleref(t,1)

#define jl_cellref(a,i) (((jl_value_t**)((jl_array_t*)a)->data)[(i)])
#define jl_cellset(a,i,x) ((((jl_value_t**)((jl_array_t*)a)->data)[(i)])=((jl_value_t*)(x)))

#define jl_exprarg(e,n) jl_cellref(((jl_expr_t*)(e))->args,n)

#define jl_fieldref(s,i) jl_get_nth_field(((jl_value_t*)s),i)

#define jl_symbolnode_sym(s) ((jl_sym_t*)jl_fieldref(s,0))
#define jl_symbolnode_type(s) (jl_fieldref(s,1))
#define jl_linenode_line(x) (((ptrint_t*)x)[1])
#define jl_labelnode_label(x) (((ptrint_t*)x)[1])
#define jl_gotonode_label(x) (((ptrint_t*)x)[1])
#define jl_getfieldnode_val(s) (jl_fieldref(s,0))
#define jl_getfieldnode_name(s) ((jl_sym_t*)jl_fieldref(s,1))
#define jl_getfieldnode_type(s) (jl_fieldref(s,2))

#define jl_tparam0(t) jl_tupleref(((jl_tag_type_t*)(t))->parameters, 0)
#define jl_tparam1(t) jl_tupleref(((jl_tag_type_t*)(t))->parameters, 1)

#define jl_typeof(v) (((jl_value_t*)(v))->type)
#define jl_typeis(v,t) (jl_typeof(v)==(jl_type_t*)(t))

#define jl_is_null(v) (((jl_value_t*)(v)) == ((jl_value_t*)jl_null))
#define jl_is_tuple(v) jl_typeis(v,jl_tuple_type)
#define jl_is_tag_type(v) jl_typeis(v,jl_tag_kind)
#define jl_is_some_tag_type(v) (jl_is_tag_type(v)||jl_is_struct_type(v)||jl_is_bits_type(v))
#define jl_is_bits_type(v) jl_typeis(v,jl_bits_kind)
#define jl_bitstype_nbits(t) (((jl_bits_type_t*)t)->nbits)
#define jl_is_struct_type(v) jl_typeis(v,jl_struct_kind)
#define jl_is_union_type(v) jl_typeis(v,jl_union_kind)
#define jl_is_typevar(v) jl_typeis(v,jl_tvar_type)
#define jl_is_typector(v) jl_typeis(v,jl_typector_type)
#define jl_is_TypeConstructor(v) jl_typeis(v,jl_typector_type)
#define jl_is_typename(v) jl_typeis(v,jl_typename_type)
#define jl_is_int32(v) jl_typeis(v,jl_int32_type)
#define jl_is_int64(v) jl_typeis(v,jl_int64_type)
#define jl_is_uint32(v) jl_typeis(v,jl_uint32_type)
#define jl_is_uint64(v) jl_typeis(v,jl_uint64_type)
#define jl_is_float32(v) jl_typeis(v,jl_float32_type)
#define jl_is_float64(v) jl_typeis(v,jl_float64_type)
#define jl_is_bool(v) jl_typeis(v,jl_bool_type)
#define jl_is_symbol(v) jl_typeis(v,jl_sym_type)
#define jl_is_expr(v) jl_typeis(v,jl_expr_type)
#define jl_is_symbolnode(v) jl_typeis(v,jl_symbolnode_type)
#define jl_is_getfieldnode(v) jl_typeis(v,jl_getfieldnode_type)
#define jl_is_labelnode(v) jl_typeis(v,jl_labelnode_type)
#define jl_is_gotonode(v) jl_typeis(v,jl_gotonode_type)
#define jl_is_quotenode(v) jl_typeis(v,jl_quotenode_type)
#define jl_is_topnode(v) jl_typeis(v,jl_topnode_type)
#define jl_is_linenode(v) jl_typeis(v,jl_linenumbernode_type)
#define jl_is_lambda_info(v) jl_typeis(v,jl_lambda_info_type)
#define jl_is_module(v) jl_typeis(v,jl_module_type)
#define jl_is_mtable(v) jl_typeis(v,jl_methtable_type)
#define jl_is_task(v) jl_typeis(v,jl_task_type)
#define jl_is_func(v) (jl_typeis(v,jl_function_type) || jl_is_struct_type(v))
#define jl_is_function(v) jl_is_func(v)
#define jl_is_ascii_string(v) jl_typeis(v,jl_ascii_string_type)
#define jl_is_utf8_string(v) jl_typeis(v,jl_utf8_string_type)
#define jl_is_byte_string(v) (jl_is_ascii_string(v) || jl_is_utf8_string(v))
#define jl_is_cpointer(v) jl_is_cpointer_type(jl_typeof(v))
#define jl_is_pointer(v) jl_is_cpointer_type(jl_typeof(v))
#define jl_is_gf(f) (((jl_function_t*)(f))->fptr==jl_apply_generic)

#define jl_tuple_len(t) (((jl_tuple_t*)(t))->length)
#define jl_tuple_set_len_unsafe(t,n) (((jl_tuple_t*)(t))->length=(n))
#define jl_cell_data(a) ((jl_value_t**)((jl_array_t*)a)->data)
#define jl_string_data(s) ((char*)((jl_array_t*)((jl_value_t**)(s))[1])->data)
#define jl_iostr_data(s) ((char*)((jl_array_t*)((jl_value_t**)(s))[1])->data)

#define jl_gf_mtable(f) ((jl_methtable_t*)((jl_function_t*)(f))->env)
#define jl_gf_name(f) (jl_gf_mtable(f)->name)

// get a pointer to the data in a value of bits type
#define jl_bits_data(v) (&((void**)(v))[1])

static inline int jl_is_array_type(void *t)
{
    return (jl_is_struct_type(t) &&
            ((jl_struct_type_t*)(t))->name == jl_array_typename);
}

static inline int jl_is_array(void *v)
{
    jl_type_t *t = jl_typeof(v);
    return jl_is_array_type(t);
}

static inline int jl_is_box(void *v)
{
    jl_type_t *t = jl_typeof(v);
    return (jl_is_struct_type(t) &&
            ((jl_struct_type_t*)(t))->name == jl_box_typename);
}

static inline int jl_is_cpointer_type(void *t)
{
    return (jl_is_bits_type(t) &&
            ((jl_bits_type_t*)(t))->name == jl_pointer_type->name);
}

static inline int jl_is_seq_type(jl_value_t *v)
{
    return (jl_is_tag_type(v) &&
            ((jl_tag_type_t*)(v))->name == jl_seq_type->name);
}

static inline int jl_is_ntuple_type(jl_value_t *v)
{
    return (jl_is_tag_type(v) &&
            ((jl_tag_type_t*)v)->name == jl_ntuple_typename);
}

static inline int jl_is_nontuple_type(jl_value_t *v)
{
    return (jl_typeis(v, jl_union_kind) ||
            jl_typeis(v, jl_struct_kind) ||
            jl_typeis(v, jl_tag_kind) ||
            jl_typeis(v, jl_bits_kind) ||
            jl_typeis(v, jl_typector_type));
}

static inline int jl_is_type_type(jl_value_t *v)
{
    return (jl_is_tag_type(v) &&
            ((jl_tag_type_t*)(v))->name == jl_type_type->name);
}

// type info accessors
jl_value_t *jl_full_type(jl_value_t *v);

// type predicates
int jl_is_type(jl_value_t *v);
DLLEXPORT int jl_is_leaf_type(jl_value_t *v);
int jl_has_typevars(jl_value_t *v);
int jl_tuple_subtype(jl_value_t **child, size_t cl,
                     jl_value_t **parent, size_t pl, int ta, int morespecific);
int jl_subtype(jl_value_t *a, jl_value_t *b, int ta);
int jl_type_morespecific(jl_value_t *a, jl_value_t *b, int ta);
int jl_subtype_invariant(jl_value_t *a, jl_value_t *b, int ta);
DLLEXPORT jl_value_t *jl_type_match(jl_value_t *a, jl_value_t *b);
jl_value_t *jl_type_match_morespecific(jl_value_t *a, jl_value_t *b);
DLLEXPORT int jl_types_equal(jl_value_t *a, jl_value_t *b);
int jl_types_equal_generic(jl_value_t *a, jl_value_t *b);
jl_value_t *jl_type_union(jl_tuple_t *types);
jl_value_t *jl_type_intersection_matching(jl_value_t *a, jl_value_t *b,
                                          jl_tuple_t **penv, jl_tuple_t *tvars);
DLLEXPORT jl_value_t *jl_type_intersection(jl_value_t *a, jl_value_t *b);
int jl_args_morespecific(jl_value_t *a, jl_value_t *b);

// type constructors
jl_typename_t *jl_new_typename(jl_sym_t *name);
jl_tvar_t *jl_new_typevar(jl_sym_t *name,jl_value_t *lb,jl_value_t *ub);
jl_typector_t *jl_new_type_ctor(jl_tuple_t *params, jl_type_t *body);
jl_value_t *jl_apply_type(jl_value_t *tc, jl_tuple_t *params);
jl_value_t *jl_apply_type_(jl_value_t *tc, jl_value_t **params, size_t n);
jl_type_t *jl_instantiate_type_with(jl_type_t *t, jl_value_t **env, size_t n);
jl_uniontype_t *jl_new_uniontype(jl_tuple_t *types);
jl_tag_type_t *jl_new_tagtype(jl_value_t *name, jl_tag_type_t *super,
                              jl_tuple_t *parameters);
jl_struct_type_t *jl_new_uninitialized_struct_type(size_t nfields);
jl_struct_type_t *jl_new_struct_type(jl_sym_t *name, jl_tag_type_t *super,
                                     jl_tuple_t *parameters,
                                     jl_tuple_t *fnames, jl_tuple_t *ftypes);
jl_bits_type_t *jl_new_bits_type(jl_value_t *name, jl_tag_type_t *super,
                                 jl_tuple_t *parameters, size_t nbits);
jl_tag_type_t *jl_wrap_Type(jl_value_t *t); // x -> Type{x}
void jl_set_tag_type_super(jl_tag_type_t *tt, jl_value_t *super);

// constructors
jl_value_t *jl_new_bits(jl_bits_type_t *bt, void *data);
void jl_assign_bits(void *dest, jl_value_t *bits);
DLLEXPORT jl_value_t *jl_new_struct(jl_struct_type_t *type, ...);
DLLEXPORT jl_value_t *jl_new_struct_uninit(jl_struct_type_t *type);
DLLEXPORT jl_value_t *jl_new_structt(jl_struct_type_t *type, jl_tuple_t *t);
jl_function_t *jl_new_closure(jl_fptr_t proc, jl_value_t *env,
                              jl_lambda_info_t *li);
jl_lambda_info_t *jl_new_lambda_info(jl_value_t *ast, jl_tuple_t *sparams);
jl_tuple_t *jl_tuple(size_t n, ...);
jl_tuple_t *jl_tuple1(void *a);
jl_tuple_t *jl_tuple2(void *a, void *b);
jl_tuple_t *jl_alloc_tuple(size_t n);
jl_tuple_t *jl_alloc_tuple_uninit(size_t n);
jl_tuple_t *jl_tuple_append(jl_tuple_t *a, jl_tuple_t *b);
jl_tuple_t *jl_tuple_fill(size_t n, jl_value_t *v);
DLLEXPORT jl_sym_t *jl_symbol(const char *str);
DLLEXPORT jl_sym_t *jl_symbol_lookup(const char *str);
DLLEXPORT jl_sym_t *jl_symbol_n(const char *str, int32_t len);
DLLEXPORT jl_sym_t *jl_gensym(void);
DLLEXPORT jl_sym_t *jl_tagged_gensym(const char *str, int32_t len);
jl_sym_t *jl_get_root_symbol(void);
jl_expr_t *jl_exprn(jl_sym_t *head, size_t n);
jl_function_t *jl_new_generic_function(jl_sym_t *name);
void jl_initialize_generic_function(jl_function_t *f, jl_sym_t *name);
void jl_add_method(jl_function_t *gf, jl_tuple_t *types, jl_function_t *meth,
                   jl_tuple_t *tvars);
jl_value_t *jl_method_def(jl_sym_t *name, jl_value_t **bp, jl_binding_t *bnd,
                          jl_tuple_t *argtypes, jl_function_t *f,
                          jl_tuple_t *tvars);
jl_value_t *jl_box_bool(int8_t x);
jl_value_t *jl_box_int8(int32_t x);
jl_value_t *jl_box_uint8(uint32_t x);
jl_value_t *jl_box_int16(int16_t x);
jl_value_t *jl_box_uint16(uint16_t x);
DLLEXPORT jl_value_t *jl_box_int32(int32_t x);
jl_value_t *jl_box_uint32(uint32_t x);
jl_value_t *jl_box_char(uint32_t x);
DLLEXPORT jl_value_t *jl_box_int64(int64_t x);
jl_value_t *jl_box_uint64(uint64_t x);
jl_value_t *jl_box_float32(float x);
jl_value_t *jl_box_float64(double x);
jl_value_t *jl_box8 (jl_bits_type_t *t, int8_t x);
jl_value_t *jl_box16(jl_bits_type_t *t, int16_t x);
jl_value_t *jl_box32(jl_bits_type_t *t, int32_t x);
jl_value_t *jl_box64(jl_bits_type_t *t, int64_t x);
int8_t jl_unbox_bool(jl_value_t *v);
int8_t jl_unbox_int8(jl_value_t *v);
uint8_t jl_unbox_uint8(jl_value_t *v);
int16_t jl_unbox_int16(jl_value_t *v);
uint16_t jl_unbox_uint16(jl_value_t *v);
int32_t jl_unbox_int32(jl_value_t *v);
uint32_t jl_unbox_uint32(jl_value_t *v);
int64_t jl_unbox_int64(jl_value_t *v);
uint64_t jl_unbox_uint64(jl_value_t *v);
float jl_unbox_float32(jl_value_t *v);
double jl_unbox_float64(jl_value_t *v);

#ifdef __LP64__
#define jl_box_long(x) jl_box_int64(x)
#define jl_unbox_long(x) jl_unbox_int64(x)
#define jl_is_long(x) jl_is_int64(x)
#define jl_long_type jl_int64_type
#else
#define jl_box_long(x) jl_box_int32(x)
#define jl_unbox_long(x) jl_unbox_int32(x)
#define jl_is_long(x) jl_is_int32(x)
#define jl_long_type jl_int32_type
#endif

// structs
void jl_compute_struct_offsets(jl_struct_type_t *st);
int jl_field_index(jl_struct_type_t *t, jl_sym_t *fld, int err);
DLLEXPORT jl_value_t *jl_get_nth_field(jl_value_t *v, size_t i);
jl_value_t *jl_set_nth_field(jl_value_t *v, size_t i, jl_value_t *rhs);

// arrays
DLLEXPORT jl_array_t *jl_new_array(jl_type_t *atype, jl_tuple_t *dims);
DLLEXPORT jl_array_t *jl_new_arrayv(jl_type_t *atype, ...);
jl_array_t *jl_new_array_(jl_type_t *atype, uint32_t ndims, size_t *dims);
DLLEXPORT jl_array_t *jl_reshape_array(jl_type_t *atype, jl_array_t *data,
                                       jl_tuple_t *dims);
DLLEXPORT jl_array_t *jl_ptr_to_array_1d(jl_type_t *atype, void *data,
                                         size_t nel, int own_buffer);
DLLEXPORT jl_array_t *jl_ptr_to_array(jl_type_t *atype, void *data,
                                      jl_tuple_t *dims, int own_buffer);

DLLEXPORT jl_array_t *jl_alloc_array_1d(jl_type_t *atype, size_t nr);
DLLEXPORT jl_array_t *jl_alloc_array_2d(jl_type_t *atype, size_t nr, size_t nc);
DLLEXPORT jl_array_t *jl_alloc_array_3d(jl_type_t *atype, size_t nr, size_t nc,
                                        size_t z);
DLLEXPORT jl_array_t *jl_pchar_to_array(char *str, size_t len);
DLLEXPORT jl_value_t *jl_pchar_to_string(char *str, size_t len);
DLLEXPORT jl_value_t *jl_cstr_to_string(char *str);
DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a);
DLLEXPORT jl_array_t *jl_alloc_cell_1d(size_t n);
DLLEXPORT jl_value_t *jl_arrayref(jl_array_t *a, size_t i); // 0-indexed
DLLEXPORT void jl_arrayset(jl_array_t *a, size_t i, jl_value_t *v); // 0-indexed
DLLEXPORT void *jl_array_ptr(jl_array_t *a);
DLLEXPORT void jl_array_grow_end(jl_array_t *a, size_t inc);
DLLEXPORT void jl_array_del_end(jl_array_t *a, size_t dec);
DLLEXPORT void jl_array_grow_beg(jl_array_t *a, size_t inc);
DLLEXPORT void jl_array_del_beg(jl_array_t *a, size_t dec);
void jl_cell_1d_push(jl_array_t *a, jl_value_t *item);

// system information
DLLEXPORT int jl_errno(void);
DLLEXPORT jl_value_t *jl_strerror(int errnum);
DLLEXPORT int32_t jl_stat(const char* path, char* statbuf);

// environment entries
DLLEXPORT jl_value_t *jl_environ(int i);

// child process status
DLLEXPORT int jl_process_exited(int status);
DLLEXPORT int jl_process_signaled(int status);
DLLEXPORT int jl_process_stopped(int status);

DLLEXPORT int jl_process_exit_status(int status);
DLLEXPORT int jl_process_term_signal(int status);
DLLEXPORT int jl_process_stop_signal(int status);

// access to std filehandles
DLLEXPORT int jl_stdin(void);
DLLEXPORT int jl_stdout(void);
DLLEXPORT int jl_stderr(void);

// exceptions
void jl_error(const char *str);
void jl_errorf(const char *fmt, ...);
void jl_too_few_args(const char *fname, int min);
void jl_too_many_args(const char *fname, int max);
void jl_type_error(const char *fname, jl_value_t *expected, jl_value_t *got);
void jl_type_error_rt(const char *fname, const char *context,
                      jl_value_t *ty, jl_value_t *got);
jl_value_t *jl_no_method_error(jl_function_t *f, jl_value_t **args, size_t na);
void jl_check_type_tuple(jl_tuple_t *t, jl_sym_t *name, const char *ctx);

// initialization functions
DLLEXPORT void julia_init(char *imageFile);
DLLEXPORT
int julia_trampoline(int argc, char *argv[], int (*pmain)(int ac,char *av[]));
void jl_init_types(void);
void jl_init_box_caches(void);
void jl_init_frontend(void);
void jl_init_primitives(void);
void jl_init_codegen(void);
void jl_init_intrinsic_functions(void);
void jl_init_tasks(void *stack, size_t ssize);
void jl_init_serializer(void);

void jl_save_system_image(char *fname, char *startscriptname);
void jl_restore_system_image(char *fname);

// front end interface
DLLEXPORT jl_value_t *jl_parse_input_line(const char *str);
void jl_start_parsing_file(const char *fname);
void jl_stop_parsing();
jl_value_t *jl_parse_next();
DLLEXPORT void jl_load_file_string(const char *text);
DLLEXPORT jl_value_t *jl_expand(jl_value_t *expr);
jl_lambda_info_t *jl_wrap_expr(jl_value_t *expr);

// some useful functions
DLLEXPORT void jl_show(jl_value_t *stream, jl_value_t *v);
void jl_show_tuple(jl_value_t *st, jl_tuple_t *t, char opn, char cls, int comma_one);
DLLEXPORT jl_value_t *jl_stdout_obj();
DLLEXPORT jl_value_t *jl_stderr_obj();
DLLEXPORT int jl_egal(jl_value_t *a, jl_value_t *b);
DLLEXPORT uptrint_t jl_object_id(jl_value_t *v);

// modules
extern DLLEXPORT jl_module_t *jl_main_module;
extern DLLEXPORT jl_module_t *jl_core_module;
extern DLLEXPORT jl_module_t *jl_base_module;
extern DLLEXPORT jl_module_t *jl_current_module;
jl_module_t *jl_new_module(jl_sym_t *name);
// get binding for reading
DLLEXPORT jl_binding_t *jl_get_binding(jl_module_t *m, jl_sym_t *var);
// get binding for assignment
jl_binding_t *jl_get_binding_wr(jl_module_t *m, jl_sym_t *var);
DLLEXPORT int jl_boundp(jl_module_t *m, jl_sym_t *var);
DLLEXPORT int jl_is_const(jl_module_t *m, jl_sym_t *var);
DLLEXPORT jl_value_t *jl_get_global(jl_module_t *m, jl_sym_t *var);
DLLEXPORT void jl_set_global(jl_module_t *m, jl_sym_t *var, jl_value_t *val);
DLLEXPORT void jl_set_const(jl_module_t *m, jl_sym_t *var, jl_value_t *val);
void jl_checked_assignment(jl_binding_t *b, jl_value_t *rhs);
void jl_declare_constant(jl_binding_t *b);
void jl_module_importall(jl_module_t *to, jl_module_t *from);
void jl_module_import(jl_module_t *to, jl_module_t *from, jl_sym_t *s);
DLLEXPORT void jl_module_export(jl_module_t *from, jl_sym_t *s);

// external libraries
DLLEXPORT uv_lib_t *jl_load_dynamic_library(char *fname);
DLLEXPORT void *jl_dlsym(uv_lib_t *handle, char *symbol);

// compiler
void jl_compile(jl_function_t *f);
void jl_generate_fptr(jl_function_t *f);
DLLEXPORT jl_value_t *jl_toplevel_eval(jl_value_t *v);
jl_value_t *jl_eval_global_var(jl_module_t *m, jl_sym_t *e);
DLLEXPORT void jl_load(const char *fname);
void jl_parse_eval_all(char *fname);
jl_value_t *jl_interpret_toplevel_thunk(jl_lambda_info_t *lam);
jl_value_t *jl_interpret_toplevel_expr(jl_value_t *e);
jl_value_t *jl_interpret_toplevel_expr_with(jl_value_t *e,
                                            jl_value_t **locals, size_t nl);
jl_value_t *jl_interpret_toplevel_expr_in(jl_module_t *m, jl_value_t *e,
                                          jl_value_t **locals, size_t nl);
void jl_type_infer(jl_lambda_info_t *li, jl_tuple_t *argtypes,
                   jl_lambda_info_t *def);

DLLEXPORT void jl_show_method_table(jl_value_t *outstr, jl_function_t *gf);
jl_lambda_info_t *jl_add_static_parameters(jl_lambda_info_t *l, jl_tuple_t *sp);
jl_function_t *jl_method_lookup_by_type(jl_methtable_t *mt, jl_tuple_t *types,
                                        int cache);
jl_function_t *jl_method_lookup(jl_methtable_t *mt, jl_value_t **args, size_t nargs, int cache);
jl_value_t *jl_gf_invoke(jl_function_t *gf, jl_tuple_t *types,
                         jl_value_t **args, size_t nargs);

// AST access
jl_array_t *jl_lam_args(jl_expr_t *l);
jl_array_t *jl_lam_locals(jl_expr_t *l);
jl_array_t *jl_lam_vinfo(jl_expr_t *l);
jl_array_t *jl_lam_capt(jl_expr_t *l);
jl_expr_t *jl_lam_body(jl_expr_t *l);
jl_sym_t *jl_decl_var(jl_value_t *ex);
DLLEXPORT int jl_is_rest_arg(jl_value_t *ex);

jl_value_t *jl_prepare_ast(jl_lambda_info_t *li, jl_tuple_t *sparams);

jl_value_t *jl_compress_ast(jl_lambda_info_t *li, jl_value_t *ast);
jl_value_t *jl_uncompress_ast(jl_tuple_t *data);

static inline int jl_vinfo_capt(jl_array_t *vi)
{
    return (jl_unbox_long(jl_cellref(vi,2))&1)!=0;
}

static inline int jl_vinfo_assigned(jl_array_t *vi)
{
    return (jl_unbox_long(jl_cellref(vi,2))&2)!=0;
}

static inline int jl_vinfo_assigned_inner(jl_array_t *vi)
{
    return (jl_unbox_long(jl_cellref(vi,2))&4)!=0;
}

// for writing julia functions in C
#define JL_CALLABLE(name) \
jl_value_t *name(jl_value_t *F, jl_value_t **args, uint32_t nargs)

static inline
jl_value_t *jl_apply(jl_function_t *f, jl_value_t **args, uint32_t nargs)
{
    return f->fptr((jl_value_t*)f, args, nargs);
}

#define JL_NARGS(fname, min, max) \
if (nargs < min) jl_too_few_args(#fname, min); \
else if (nargs > max) jl_too_many_args(#fname, max);

#define JL_NARGSV(fname, min) \
if (nargs < min) jl_too_few_args(#fname, min);

#define JL_TYPECHK(fname, type, v) \
if (!jl_is_##type(v)) { \
jl_type_error(#fname, (jl_value_t*)jl_##type##_type, (v)); \
}

// gc

#ifdef JL_GC_MARKSWEEP
typedef struct _jl_gcframe_t {
    jl_value_t ***roots;
    size_t nroots;
    int indirect;
    struct _jl_gcframe_t *prev;
} jl_gcframe_t;

// NOTE: it is the caller's responsibility to make sure arguments are
// rooted. foo(f(), g()) will not work, and foo can't do anything about it,
// so the caller must do
// jl_value_t *x, *y; JL_GC_PUSH(&x, &y);
// x = f(); y = g(); foo(x, y)

extern DLLEXPORT jl_gcframe_t *jl_pgcstack;

#define JL_GC_PUSH(...) \
void *__gc_rts[] = {__VA_ARGS__}; \
jl_gcframe_t __gc_stkf_ = { (jl_value_t***)__gc_rts, VA_NARG(__VA_ARGS__), \
1, jl_pgcstack }; \
jl_pgcstack = &__gc_stkf_;

#define JL_GC_PUSHARGS(rts,n) \
jl_gcframe_t __gc_stkf2_ = { (jl_value_t***)rts, (n), \
0, jl_pgcstack }; \
jl_pgcstack = &__gc_stkf2_;

#define JL_GC_POP() (jl_pgcstack = jl_pgcstack->prev)

void jl_gc_init(void);
void jl_gc_setmark(jl_value_t *v);
DLLEXPORT void jl_gc_enable(void);
DLLEXPORT void jl_gc_disable(void);
DLLEXPORT int jl_gc_is_enabled(void);
void jl_gc_ephemeral_on(void);
void jl_gc_ephemeral_off(void);
DLLEXPORT void jl_gc_collect(void);
void jl_gc_preserve(jl_value_t *v);
void jl_gc_unpreserve(void);
int jl_gc_n_preserved_values(void);
DLLEXPORT void jl_gc_add_finalizer(jl_value_t *v, jl_function_t *f);
jl_weakref_t *jl_gc_new_weakref(jl_value_t *value);
jl_mallocptr_t *jl_gc_acquire_buffer(void *b, size_t sz);
jl_mallocptr_t *jl_gc_managed_malloc(size_t sz);
void *alloc_2w(void);
void *alloc_3w(void);
void *alloc_4w(void);

#else

#define JL_GC_PUSH(...) ;
#define JL_GC_PUSHARGS(rts,n) ;
#define JL_GC_POP()

#define jl_gc_preserve(v) ((void)(v))
#define jl_gc_unpreserve()
#define jl_gc_n_preserved_values() (0)

static inline void *alloc_2w() { return allocobj(2*sizeof(void*)); }
static inline void *alloc_3w() { return allocobj(3*sizeof(void*)); }
static inline void *alloc_4w() { return allocobj(4*sizeof(void*)); }
#endif

// asynch signal handling

#include <signal.h>

DLLEXPORT extern volatile sig_atomic_t jl_signal_pending;
DLLEXPORT extern volatile sig_atomic_t jl_defer_signal;

#define JL_SIGATOMIC_BEGIN() (jl_defer_signal++)
#define JL_SIGATOMIC_END() \
do { \
jl_defer_signal--; \
if (jl_defer_signal == 0 && jl_signal_pending != 0) \
raise(jl_signal_pending); \
} while(0)

// tasks and exceptions

// context that needs to be restored around a try block
typedef struct _jl_savestate_t {
    // eh_task is who I yield to for exception handling
    struct _jl_task_t *eh_task;
    // eh_ctx is where I go to handle an exception yielded to me
    jl_jmp_buf *eh_ctx;
    ptrint_t err : 1;
    ptrint_t bt : 1; // whether exceptions caught here build a backtrace
#ifdef JL_GC_MARKSWEEP
    jl_gcframe_t *gcstack;
#endif
    struct _jl_savestate_t *prev;
} jl_savestate_t;

typedef struct _jl_task_t {
    JL_STRUCT_TYPE
    struct _jl_task_t *on_exit;
    struct _jl_task_t *last;
    jl_value_t *tls;
    jl_value_t *consumers;
    int8_t done;
    int8_t runnable;
    jl_jmp_buf ctx;
    union {
        void *stackbase;
        void *stack;
    };
    jl_jmp_buf base_ctx;
    size_t bufsz;
    void *stkbuf;
    size_t ssize;
    jl_function_t *start;
    jl_value_t *result;
    // exception state and per-task dynamic parameters
    jl_savestate_t state;
} jl_task_t;

extern DLLEXPORT jl_task_t * volatile jl_current_task;
extern DLLEXPORT jl_task_t *jl_root_task;
extern DLLEXPORT jl_value_t *jl_exception_in_transit;

jl_task_t *jl_new_task(jl_function_t *start, size_t ssize);
jl_value_t *jl_switchto(jl_task_t *t, jl_value_t *arg);
DLLEXPORT void jl_raise(jl_value_t *e);
DLLEXPORT void jl_register_toplevel_eh(void);

DLLEXPORT jl_array_t *jl_takebuf_array(ios_t *s);
DLLEXPORT jl_value_t *jl_takebuf_string(ios_t *s);
DLLEXPORT void *jl_takebuf_raw(ios_t *s);
DLLEXPORT jl_value_t *jl_readuntil(ios_t *s, uint8_t delim);
DLLEXPORT void jl_free2(void *p, void *hint);

DLLEXPORT int jl_cpu_cores(void);

#define JL_STDOUT ios_stdout
#define JL_STDERR ios_stderr
#define JL_PRINTF ios_printf
#define JL_PUTC ios_putc
#define JL_PUTS ios_puts
#define JL_WRITE ios_write
#define jl_exit exit
#define JL_STREAM ios_t

static inline void jl_eh_restore_state(jl_savestate_t *ss)
{
    JL_SIGATOMIC_BEGIN();
    jl_current_task->state.eh_task = ss->eh_task;
    jl_current_task->state.eh_ctx = ss->eh_ctx;
    jl_current_task->state.bt = ss->bt;
    jl_current_task->state.prev = ss->prev;
#ifdef JL_GC_MARKSWEEP
    jl_pgcstack = ss->gcstack;
#endif
    JL_SIGATOMIC_END();
}

DLLEXPORT void jl_enter_handler(jl_savestate_t *ss, jl_jmp_buf *handlr);
DLLEXPORT void jl_pop_handler(int n);

#if defined(__WIN32__)
#define jl_setjmp_f _setjmp
#define jl_setjmp(a,b) setjmp(a)
#define jl_longjmp(a,b) longjmp(a,b)
#else
// determine actual entry point name
#if defined(sigsetjmp)
#define jl_setjmp_f __sigsetjmp
#else
#define jl_setjmp_f sigsetjmp
#endif
#define jl_setjmp(a,b) sigsetjmp(a,b)
#define jl_longjmp(a,b) siglongjmp(a,b)
#endif

#define JL_TRY \
int i__tr, i__ca; jl_savestate_t __ss; jl_jmp_buf __handlr; \
jl_enter_handler(&__ss, &__handlr); \
if (!jl_setjmp(__handlr,0)) \
for (i__tr=1; i__tr; i__tr=0, jl_eh_restore_state(&__ss))

#define JL_EH_POP() jl_eh_restore_state(&__ss)

#define JL_CATCH \
else \
for (i__ca=1, jl_current_task->state.err = 0, \
jl_eh_restore_state(&__ss); i__ca; i__ca=0)

#endif
Something went wrong with that request. Please try again.