Skip to content

Commit

Permalink
lowering: avoid copying the entire module back to flisp just to get i…
Browse files Browse the repository at this point in the history
…ts name (JuliaLang#49898)
  • Loading branch information
vtjnash authored May 21, 2023
1 parent 9cbee21 commit 0495278
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ static value_t julia_to_scm_(fl_context_t *fl_ctx, jl_value_t *v, int check_vali
// GC Note: jl_fieldref(v, 0) allocates for GotoNode
// but we don't need a GC root here because julia_to_list2_noalloc
// shouldn't allocate in this case.
if (jl_typetagis(v, jl_linenumbernode_type)) {
if (jl_is_linenode(v)) {
jl_value_t *file = jl_fieldref_noalloc(v,1);
jl_value_t *line = jl_fieldref(v,0);
value_t args = julia_to_list2_noalloc(fl_ctx, line, file, check_valid);
Expand Down Expand Up @@ -834,7 +834,7 @@ JL_DLLEXPORT jl_value_t *jl_fl_parse(const char *text, size_t text_len,
}

// returns either an expression or a thunk
jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule)
static jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule)
{
jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule);
fl_context_t *fl_ctx = &ctx->fl;
Expand All @@ -847,8 +847,8 @@ jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module
return result;
}

static jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr,
jl_module_t *inmodule, const char *file, int line)
jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr,
jl_module_t *inmodule, const char *file, int line)
{
jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule);
fl_context_t *fl_ctx = &ctx->fl;
Expand Down Expand Up @@ -1011,7 +1011,7 @@ static jl_value_t *jl_invoke_julia_macro(jl_array_t *args, jl_module_t *inmodule
// __source__ argument
jl_value_t *lno = jl_array_ptr_ref(args, 1);
margs[1] = lno;
if (!jl_typetagis(lno, jl_linenumbernode_type)) {
if (!jl_is_linenode(lno)) {
margs[1] = jl_new_struct(jl_linenumbernode_type, jl_box_long(0), jl_nothing);
}
margs[2] = (jl_value_t*)inmodule;
Expand Down
11 changes: 3 additions & 8 deletions src/jlfrontend.scm
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,9 @@

;; construct default definitions of `eval` for non-bare modules
;; called by jl_eval_module_expr
(define (module-default-defs e)
(define (module-default-defs name file line)
(jl-expand-to-thunk
(let* ((name (caddr e))
(body (cadddr e))
(loc (if (null? (cdr body)) () (cadr body)))
(loc (if (and (pair? loc) (eq? (car loc) 'line))
(list loc)
'()))
(let* ((loc (if (and (eq? file 'none) (eq? line 0)) '() `((line ,line ,file))))
(x (if (eq? name 'x) 'y 'x))
(mex (if (eq? name 'mapexpr) 'map_expr 'mapexpr)))
`(block
Expand All @@ -202,7 +197,7 @@
(block
,@loc
(call (core _call_latest) (top include) ,mex ,name ,x)))))
'none 0))
file line))

; run whole frontend on a string. useful for testing.
(define (fe str)
Expand Down
3 changes: 2 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ jl_value_t *jl_interpret_toplevel_expr_in(jl_module_t *m, jl_value_t *e,
jl_code_info_t *src,
jl_svec_t *sparam_vals);
JL_DLLEXPORT int jl_is_toplevel_only_expr(jl_value_t *e) JL_NOTSAFEPOINT;
jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule);
jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr,
jl_module_t *inmodule, const char *file, int line);

jl_method_instance_t *jl_method_lookup(jl_value_t **args, size_t nargs, size_t world);

Expand Down
15 changes: 13 additions & 2 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,28 @@ static jl_value_t *jl_eval_module_expr(jl_module_t *parent_module, jl_expr_t *ex
size_t last_age = ct->world_age;

// add standard imports unless baremodule
jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 2))->args;
int lineno = 0;
const char *filename = "none";
if (jl_array_len(exprs) > 0) {
jl_value_t *lineex = jl_array_ptr_ref(exprs, 0);
if (jl_is_linenode(lineex)) {
lineno = jl_linenode_line(lineex);
jl_value_t *file = jl_linenode_file(lineex);
if (jl_is_symbol(file))
filename = jl_symbol_name((jl_sym_t*)file);
}
}
if (std_imports) {
if (jl_base_module != NULL) {
jl_add_standard_imports(newm);
}
// add `eval` function
form = jl_call_scm_on_ast("module-default-defs", (jl_value_t*)ex, newm);
form = jl_call_scm_on_ast_and_loc("module-default-defs", (jl_value_t*)name, newm, filename, lineno);
jl_toplevel_eval_flex(newm, form, 0, 1);
form = NULL;
}

jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 2))->args;
for (int i = 0; i < jl_array_len(exprs); i++) {
// process toplevel form
ct->world_age = jl_atomic_load_acquire(&jl_world_counter);
Expand Down

0 comments on commit 0495278

Please sign in to comment.