DLLEXPORT jl_value_t *jl_compress_ast(jl_value_t *ast) { ios_t dest; jl_ios_mem(&dest, 0); int en = jl_gc_is_enabled(); jl_gc_disable(); tree_literal_values = jl_alloc_cell_1d(0); jl_serialize_value(&dest, ast); //ios_printf(ios_stderr, "%d bytes, %d values\n", dest.size, vals->length); jl_value_t *v = (jl_value_t*)jl_takebuf_array(&dest); if (tree_literal_values->length == 0) tree_literal_values = (jl_array_t*)jl_an_empty_cell; v = (jl_value_t*)jl_tuple(4, v, tree_literal_values, jl_lam_body((jl_expr_t*)ast)->etype, jl_lam_capt((jl_expr_t*)ast)); tree_literal_values = NULL; if (en) jl_gc_enable(); return v; }
DLLEXPORT jl_lambda_info_t *jl_new_lambda_info(jl_value_t *ast, jl_tuple_t *sparams) { jl_lambda_info_t *li = (jl_lambda_info_t*)newobj((jl_value_t*)jl_lambda_info_type, LAMBDA_INFO_NW); li->ast = ast; li->file = null_sym; li->line = 0; if (ast != NULL && jl_is_expr(ast)) { jl_expr_t *body1 = (jl_expr_t*)jl_exprarg(jl_lam_body((jl_expr_t*)ast),0); if (jl_is_expr(body1) && ((jl_expr_t*)body1)->head == line_sym) { li->file = (jl_sym_t*)jl_exprarg(body1, 1); li->line = jl_unbox_long(jl_exprarg(body1, 0)); } } li->module = jl_current_module; li->sparams = sparams; li->tfunc = (jl_value_t*)jl_null; li->fptr = &jl_trampoline; li->roots = NULL; li->functionObject = NULL; li->cFunctionObject = NULL; li->specTypes = NULL; li->inferred = 0; li->inInference = 0; li->inCompile = 0; li->unspecialized = NULL; li->specializations = NULL; li->name = anonymous_sym; li->def = li; li->capt = NULL; return li; }
DLLEXPORT jl_value_t *jl_compress_ast(jl_lambda_info_t *li, jl_value_t *ast) { ios_t dest; ios_mem(&dest, 0); int en = jl_gc_is_enabled(); jl_gc_disable(); if (li->roots == NULL) li->roots = jl_alloc_cell_1d(0); tree_literal_values = li->roots; jl_serialize_value(&dest, ast); //JL_PRINTF(JL_STDERR, "%d bytes, %d values\n", dest.size, vals->length); jl_value_t *v = (jl_value_t*)jl_takebuf_array(&dest); if (jl_array_len(tree_literal_values) == 0) { tree_literal_values = (jl_array_t*)jl_an_empty_cell; li->roots = NULL; } v = (jl_value_t*)jl_tuple(4, v, tree_literal_values, jl_lam_body((jl_expr_t*)ast)->etype, jl_lam_capt((jl_expr_t*)ast)); tree_literal_values = NULL; if (en) jl_gc_enable(); return v; }
jl_value_t *jl_interpret_toplevel_thunk_with(jl_lambda_info_t *lam, jl_value_t **loc, size_t nl) { jl_expr_t *ast = (jl_expr_t*)lam->ast; jl_array_t *stmts = jl_lam_body(ast)->args; jl_array_t *l = jl_lam_locals(ast); size_t llength = jl_array_len(l); jl_value_t **names = &((jl_value_t**)l->data)[0]; nl += llength; jl_value_t **locals; JL_GC_PUSHARGS(locals, nl*2); jl_value_t *r = (jl_value_t*)jl_null; size_t i=0; for(i=0; i < llength; i++) { locals[i*2] = names[i]; locals[i*2+1] = NULL; } for(; i < nl; i++) { locals[i*2] = loc[(i-llength)*2]; locals[i*2+1] = loc[(i-llength)*2+1]; } r = eval_body(stmts, locals, nl, 0); JL_GC_POP(); return r; }
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); if (!ewc) { jl_array_t *vinfos = jl_lam_vinfo((jl_expr_t*)thk->ast); int i; for(i=0; i < vinfos->length; i++) { if (jl_vinfo_capt((jl_array_t*)jl_cellref(vinfos,i))) { // interpreter doesn't handle closure environment ewc = 1; break; } } } } 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); result = jl_apply((jl_function_t*)thunk, NULL, 0); } else { result = jl_interpret_toplevel_thunk(thk); } JL_GC_POP(); return result; }
void jl_lambda_info_set_ast(jl_lambda_info_t *li, jl_value_t *ast) { assert(jl_is_expr(ast)); jl_array_t *body = jl_lam_body((jl_expr_t*)ast)->args; li->code = body; jl_gc_wb(li, li->code); if (has_meta(body, pure_sym)) li->pure = 1; jl_array_t *vis = jl_lam_vinfo((jl_expr_t*)ast); size_t nslots = jl_array_len(vis); jl_value_t *ssavalue_types = jl_lam_ssavalues((jl_expr_t*)ast); assert(jl_is_long(ssavalue_types)); size_t nssavalue = jl_unbox_long(ssavalue_types); li->slotnames = jl_alloc_cell_1d(nslots); jl_gc_wb(li, li->slotnames); li->slottypes = jl_nothing; li->slotflags = jl_alloc_array_1d(jl_array_uint8_type, nslots); jl_gc_wb(li, li->slotflags); li->ssavaluetypes = jl_box_long(nssavalue); jl_gc_wb(li, li->ssavaluetypes); int i; for(i=0; i < nslots; i++) { jl_value_t *vi = jl_cellref(vis, i); jl_sym_t *name = (jl_sym_t*)jl_cellref(vi, 0); assert(jl_is_symbol(name)); char *str = jl_symbol_name(name); if (i > 0 && name != unused_sym) { if (str[0] == '#') { // convention for renamed variables: #...#original_name char *nxt = strchr(str + 1, '#'); if (nxt) name = jl_symbol(nxt+1); else if (str[1] == 's') // compiler-generated temporaries, #sXXX name = compiler_temp_sym; } } jl_cellset(li->slotnames, i, name); jl_array_uint8_set(li->slotflags, i, jl_unbox_long(jl_cellref(vi, 2))); } jl_array_t *args = jl_lam_args((jl_expr_t*)ast); size_t narg = jl_array_len(args); li->nargs = narg; li->isva = narg > 0 && jl_is_rest_arg(jl_cellref(args, narg - 1)); }
DLLEXPORT jl_lambda_info_t *jl_new_lambda_info(jl_value_t *ast, jl_svec_t *sparams, jl_module_t *ctx) { jl_lambda_info_t *li = (jl_lambda_info_t*)newobj((jl_value_t*)jl_lambda_info_type, NWORDS(sizeof(jl_lambda_info_t))); li->ast = ast; li->file = null_sym; li->line = 0; if (ast != NULL && jl_is_expr(ast)) { jl_value_t *body1 = skip_meta(jl_lam_body((jl_expr_t*)ast)->args); if (jl_is_linenode(body1)) { li->file = (jl_sym_t*)jl_fieldref(body1, 0); li->line = jl_unbox_long(jl_fieldref(body1, 1)); } } li->module = ctx; li->sparams = sparams; li->tfunc = jl_nothing; li->fptr = &jl_trampoline; li->roots = NULL; li->functionObject = NULL; li->specFunctionObject = NULL; li->cFunctionList = NULL; li->functionID = 0; li->specFunctionID = 0; li->specTypes = NULL; li->inferred = 0; li->inInference = 0; li->inCompile = 0; li->unspecialized = NULL; li->specializations = NULL; li->name = anonymous_sym; li->def = li; li->capt = NULL; return li; }
jl_value_t *jl_toplevel_eval_flex(jl_value_t *e, int fast, int *plineno) { //jl_show(ex); //JL_PRINTF(JL_STDOUT, "\n"); if (!jl_is_expr(e)) return jl_interpret_toplevel_expr(e); jl_expr_t *ex = (jl_expr_t*)e; if (ex->head == null_sym || ex->head == error_sym) { // expression types simple enough not to need expansion return jl_interpret_toplevel_expr(e); } if (ex->head == module_sym) { return jl_eval_module_expr(ex, plineno); } // handle import, export toplevel-only forms if (ex->head == importall_sym) { jl_module_t *m = eval_import_path(ex->args); jl_sym_t *name = (jl_sym_t*)jl_cellref(ex->args, ex->args->length-1); assert(jl_is_symbol(name)); m = (jl_module_t*)jl_eval_global_var(m, name); if (!jl_is_module(m)) jl_errorf("invalid import statement"); jl_module_importall(jl_current_module, m); return jl_nothing; } if (ex->head == import_sym) { jl_module_t *m = eval_import_path(ex->args); jl_sym_t *name = (jl_sym_t*)jl_cellref(ex->args, ex->args->length-1); assert(jl_is_symbol(name)); jl_module_import(jl_current_module, m, name); return jl_nothing; } if (ex->head == export_sym) { for(size_t i=0; i < ex->args->length; i++) { jl_module_export(jl_current_module, (jl_sym_t*)jl_cellref(ex->args, i)); } return jl_nothing; } jl_value_t *thunk=NULL; jl_value_t *result; jl_lambda_info_t *thk=NULL; int ewc = 0; JL_GC_PUSH(&thunk, &thk, &ex); if (ex->head != body_sym && ex->head != thunk_sym) { // not yet expanded ex = (jl_expr_t*)jl_expand(e); } if (jl_is_expr(ex) && ex->head == thunk_sym) { thk = (jl_lambda_info_t*)jl_exprarg(ex,0); assert(jl_is_lambda_info(thk)); ewc = jl_eval_with_compiler_p(jl_lam_body((jl_expr_t*)thk->ast), fast); if (!ewc) { jl_array_t *vinfos = jl_lam_vinfo((jl_expr_t*)thk->ast); int i; for(i=0; i < vinfos->length; i++) { if (jl_vinfo_capt((jl_array_t*)jl_cellref(vinfos,i))) { // interpreter doesn't handle closure environment ewc = 1; break; } } } } else { if (jl_is_expr(ex) && jl_eval_with_compiler_p((jl_expr_t*)ex, fast)) { thk = jl_wrap_expr((jl_value_t*)ex); ewc = 1; } else { result = jl_interpret_toplevel_expr((jl_value_t*)ex); JL_GC_POP(); return result; } } if (ewc) { thunk = (jl_value_t*)jl_new_closure(NULL, (jl_value_t*)jl_null, thk); if (!jl_in_inference) { jl_type_infer(thk, jl_tuple_type, thk); } result = jl_apply((jl_function_t*)thunk, NULL, 0); } else { result = jl_interpret_toplevel_thunk(thk); } JL_GC_POP(); return result; }
jl_value_t *jl_toplevel_eval_flex(jl_value_t *e, int fast, volatile size_t *plineno) { //jl_show(ex); //ios_printf(ios_stdout, "\n"); if (!jl_is_expr(e)) return jl_interpret_toplevel_expr(e); jl_expr_t *ex = (jl_expr_t*)e; if (ex->head == null_sym || ex->head == error_sym) { // expression types simple enough not to need expansion return jl_interpret_toplevel_expr(e); } if (ex->head == module_sym) { return jl_eval_module_expr(ex, plineno); } jl_value_t *thunk=NULL; jl_value_t *result; jl_lambda_info_t *thk=NULL; int ewc = 0; JL_GC_PUSH(&thunk, &thk, &ex); if (ex->head == body_sym || ex->head == thunk_sym) { // already expanded } else { ex = (jl_expr_t*)jl_expand(e); } if (jl_is_expr(ex) && ex->head == thunk_sym) { thk = (jl_lambda_info_t*)jl_exprarg(ex,0); assert(jl_is_lambda_info(thk)); ewc = eval_with_compiler_p(jl_lam_body((jl_expr_t*)thk->ast), fast); if (!ewc) { jl_array_t *vinfos = jl_lam_vinfo((jl_expr_t*)thk->ast); int i; for(i=0; i < vinfos->length; i++) { if (jl_vinfo_capt((jl_array_t*)jl_cellref(vinfos,i))) { // interpreter doesn't handle closure environment ewc = 1; break; } } } } else { if (jl_is_expr(ex) && eval_with_compiler_p((jl_expr_t*)ex, fast)) { thk = jl_wrap_expr((jl_value_t*)ex); ewc = 1; } else { result = jl_interpret_toplevel_expr((jl_value_t*)ex); JL_GC_POP(); return result; } } if (ewc) { thunk = jl_new_closure_internal(thk, (jl_value_t*)jl_null); if (fast && !jl_in_inference) { jl_type_infer(thk, jl_tuple_type, thk); } result = jl_apply((jl_function_t*)thunk, NULL, 0); } else { result = jl_interpret_toplevel_thunk(thk); } JL_GC_POP(); return result; }
jl_value_t *jl_toplevel_eval_flex(jl_value_t *e, int fast) { //jl_show(ex); //jl_printf(JL_STDOUT, "\n"); if (!jl_is_expr(e)) return jl_interpret_toplevel_expr(e); jl_expr_t *ex = (jl_expr_t*)e; if (ex->head == null_sym || ex->head == error_sym) { // expression types simple enough not to need expansion return jl_interpret_toplevel_expr(e); } if (ex->head == module_sym) { return jl_eval_module_expr(ex); } // handle import, using, importall, export toplevel-only forms if (ex->head == importall_sym) { jl_module_t *m = eval_import_path(ex->args); if (m==NULL) return jl_nothing; jl_sym_t *name = (jl_sym_t*)jl_cellref(ex->args, jl_array_len(ex->args)-1); if (!jl_is_symbol(name)) jl_error("syntax: malformed \"importall\" statement"); m = (jl_module_t*)jl_eval_global_var(m, name); if (!jl_is_module(m)) jl_errorf("invalid %s statement: name exists but does not refer to a module", ex->head->name); jl_module_importall(jl_current_module, m); return jl_nothing; } if (ex->head == using_sym) { jl_module_t *m = eval_import_path(ex->args); if (m==NULL) return jl_nothing; jl_sym_t *name = (jl_sym_t*)jl_cellref(ex->args, jl_array_len(ex->args)-1); if (!jl_is_symbol(name)) jl_error("syntax: malformed \"using\" statement"); jl_module_t *u = (jl_module_t*)jl_eval_global_var(m, name); if (jl_is_module(u)) { jl_module_using(jl_current_module, u); } else { jl_module_use(jl_current_module, m, name); } return jl_nothing; } if (ex->head == import_sym) { jl_module_t *m = eval_import_path(ex->args); if (m==NULL) return jl_nothing; jl_sym_t *name = (jl_sym_t*)jl_cellref(ex->args, jl_array_len(ex->args)-1); if (!jl_is_symbol(name)) jl_error("syntax: malformed \"import\" statement"); jl_module_import(jl_current_module, m, name); return jl_nothing; } if (ex->head == export_sym) { for(size_t i=0; i < jl_array_len(ex->args); i++) { jl_sym_t *name = (jl_sym_t*)jl_cellref(ex->args, i); if (!jl_is_symbol(name)) jl_error("syntax: malformed \"export\" statement"); jl_module_export(jl_current_module, name); } return jl_nothing; } if (ex->head == toplevel_sym) { int i=0; jl_value_t *res=jl_nothing; for(i=0; i < jl_array_len(ex->args); i++) { res = jl_toplevel_eval_flex(jl_cellref(ex->args, i), fast); } return res; } jl_value_t *thunk=NULL; jl_value_t *result; jl_lambda_info_t *thk=NULL; int ewc = 0; JL_GC_PUSH3(&thunk, &thk, &ex); if (ex->head != body_sym && ex->head != thunk_sym && ex->head != return_sym && ex->head != method_sym) { // not yet expanded ex = (jl_expr_t*)jl_expand(e); } jl_sym_t *head = jl_is_expr(ex) ? ex->head : NULL; if (head == toplevel_sym) { int i=0; jl_value_t *res=jl_nothing; for(i=0; i < jl_array_len(ex->args); i++) { res = jl_toplevel_eval_flex(jl_cellref(ex->args, i), fast); } JL_GC_POP(); return res; } if (head == thunk_sym) { thk = (jl_lambda_info_t*)jl_exprarg(ex,0); assert(jl_is_lambda_info(thk)); assert(jl_is_expr(thk->ast)); ewc = jl_eval_with_compiler_p((jl_expr_t*)thk->ast, jl_lam_body((jl_expr_t*)thk->ast), fast, jl_current_module); if (!ewc) { if (jl_lam_vars_captured((jl_expr_t*)thk->ast)) { // interpreter doesn't handle closure environment ewc = 1; } } } else { if (head && jl_eval_with_compiler_p(NULL, (jl_expr_t*)ex, fast, jl_current_module)) { thk = jl_wrap_expr((jl_value_t*)ex); ewc = 1; } else { if (head == body_sym) { result = jl_toplevel_eval_body(ex->args); } else if (jl_is_toplevel_only_expr((jl_value_t*)ex)) { result = jl_toplevel_eval((jl_value_t*)ex); } else { result = jl_interpret_toplevel_expr((jl_value_t*)ex); } JL_GC_POP(); return result; } } if (ewc) { thunk = (jl_value_t*)jl_new_closure(NULL, (jl_value_t*)jl_emptysvec, thk); if (!jl_in_inference) { jl_type_infer(thk, (jl_tupletype_t*)jl_typeof(jl_emptytuple), thk); } result = jl_apply((jl_function_t*)thunk, NULL, 0); } else { result = jl_interpret_toplevel_thunk(thk); } JL_GC_POP(); return result; }
JL_DLLEXPORT jl_lambda_info_t *jl_new_lambda_info(jl_value_t *ast, jl_svec_t *tvars, jl_svec_t *sparams, jl_module_t *ctx) { jl_lambda_info_t *li = (jl_lambda_info_t*)newobj((jl_value_t*)jl_lambda_info_type, NWORDS(sizeof(jl_lambda_info_t))); li->ast = ast; li->rettype = (jl_value_t*)jl_any_type; li->file = null_sym; li->module = ctx; li->sparam_syms = tvars; li->sparam_vals = sparams; li->tfunc = jl_nothing; li->fptr = NULL; li->jlcall_api = 0; li->roots = NULL; li->functionObjects.functionObject = NULL; li->functionObjects.specFunctionObject = NULL; li->functionObjects.cFunctionList = NULL; li->functionID = 0; li->specFunctionID = 0; li->specTypes = NULL; li->inferred = 0; li->inInference = 0; li->inCompile = 0; li->unspecialized = NULL; li->specializations = NULL; li->name = anonymous_sym; li->def = li; li->line = 0; li->pure = 0; li->called = 0xff; li->needs_sparam_vals_ducttape = 0; if (ast && jl_is_expr(ast)) { jl_array_t *body = jl_lam_body((jl_expr_t*)ast)->args; if (has_meta(body, pure_sym)) li->pure = 1; jl_value_t *body1 = skip_meta(body); if (jl_is_linenode(body1)) { li->file = jl_linenode_file(body1); li->line = jl_linenode_line(body1); } else if (jl_is_expr(body1) && ((jl_expr_t*)body1)->head == line_sym) { li->file = (jl_sym_t*)jl_exprarg(body1, 1); li->line = jl_unbox_long(jl_exprarg(body1, 0)); } jl_array_t *vis = jl_lam_vinfo((jl_expr_t*)li->ast); jl_array_t *args = jl_lam_args((jl_expr_t*)li->ast); size_t narg = jl_array_len(args); uint8_t called=0; int i, j=0; for(i=1; i < narg && i <= 8; i++) { jl_value_t *ai = jl_cellref(args,i); if (ai == (jl_value_t*)unused_sym || !jl_is_symbol(ai)) continue; jl_value_t *vj; do { vj = jl_cellref(vis, j++); } while (jl_cellref(vj,0) != ai); if (jl_unbox_long(jl_cellref(vj,2))&64) called |= (1<<(i-1)); } li->called = called; if (tvars != jl_emptysvec) if (jl_has_intrinsics(li, (jl_expr_t*)ast, ctx)) li->needs_sparam_vals_ducttape = 1; } return li; }