Skip to content

Commit

Permalink
Add file field to LineNumberNode
Browse files Browse the repository at this point in the history
replacing (:line #lno file) Exprs
  • Loading branch information
ihnorton committed Aug 20, 2015
1 parent 387e62c commit 9ec72c0
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 83 deletions.
3 changes: 2 additions & 1 deletion base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#end

#immutable LineNumberNode
# file::Symbol
# line::Int
#end

Expand Down Expand Up @@ -284,13 +285,13 @@ TypeConstructor(p::ANY, t::ANY) = ccall(:jl_new_type_constructor, Any, (Any, Any
Expr(args::ANY...) = _expr(args...)

_new(typ::Symbol, argty::Symbol) = eval(:(Core.call(::Type{$typ}, n::$argty) = $(Expr(:new, typ, :n))))
_new(:LineNumberNode, :Int)
_new(:LabelNode, :Int)
_new(:GotoNode, :Int)
_new(:TopNode, :Symbol)
_new(:NewvarNode, :Symbol)
_new(:QuoteNode, :ANY)
_new(:GenSym, :Int)
eval(:(Core.call(::Type{LineNumberNode}, f::Symbol, l::Int) = $(Expr(:new, :LineNumberNode, :f, :l))))
eval(:(Core.call(::Type{GlobalRef}, m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))))

Module(name::Symbol=:anonymous, std_imports::Bool=true) = ccall(:jl_f_new_module, Any, (Any, Bool), name, std_imports)::Module
Expand Down
2 changes: 1 addition & 1 deletion base/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function poplinenum(ex::Expr)
if ex.head == :block
if length(ex.args) == 1
return ex.args[1]
elseif length(ex.args) == 2 && ex.args[1].head == :line
elseif length(ex.args) == 2 && isa(ex.args[1], LineNumberNode)
return ex.args[2]
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ isexpr(x, ts...) = any(T->isa(T, Type) && isa(x, T), ts)

function unblock(ex)
isexpr(ex, :block) || return ex
exs = filter(ex->!isexpr(ex, :line), ex.args)
exs = filter(ex -> !(isa(ex, LineNumberNode) || isexpr(ex, :line)), ex.args)
length(exs) == 1 || return ex
return unblock(exs[1])
end
Expand Down
2 changes: 1 addition & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ call(T::Type{UTF8String}, d::Array{UInt8,1}) = Core.call(T, d)
call(T::Type{TypeVar}, args...) = Core.call(T, args...)
call(T::Type{TypeConstructor}, args...) = Core.call(T, args...)
call(T::Type{Expr}, args::ANY...) = _expr(args...)
call(T::Type{LineNumberNode}, n::Int) = Core.call(T, n)
call(T::Type{LineNumberNode}, f::Symbol, n::Int) = Core.call(T, f, n)
call(T::Type{LabelNode}, n::Int) = Core.call(T, n)
call(T::Type{GotoNode}, n::Int) = Core.call(T, n)
call(T::Type{QuoteNode}, x::ANY) = Core.call(T, x)
Expand Down
4 changes: 2 additions & 2 deletions base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const UNKNOWN = LineInfo("?", "?", -1, "?", -1, true, 0)
# If the LineInfo has function and line information, we consider two of them the same
# if they share the same function/line information. For unknown functions, line==ip
# so we never actually need to consider the .ip field.
#
#
==(a::LineInfo, b::LineInfo) = a.line == b.line && a.fromC == b.fromC && a.func == b.func && a.file == b.file

function hash(li::LineInfo, h::UInt)
Expand All @@ -136,7 +136,7 @@ maxlen_data() = convert(Int, ccall(:jl_profile_maxlen_data, Csize_t, ()))

function lookup(ip::Ptr{Void})
info = ccall(:jl_lookup_code_address, Any, (Ptr{Void},Cint), ip, false)
if length(info) == 7
if length(info) == 7
return LineInfo(string(info[1]), string(info[2]), Int(info[3]), string(info[4]), Int(info[5]), info[6], Int64(info[7]))
else
return UNKNOWN
Expand Down
10 changes: 2 additions & 8 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
is_expr(ex, head::Symbol, n::Int) = is_expr(ex, head) && length(ex.args) == n

is_linenumber(ex::LineNumberNode) = true
is_linenumber(ex::Expr) = is(ex.head, :line)
is_linenumber(ex) = false

is_quoted(ex) = false
Expand Down Expand Up @@ -351,8 +350,7 @@ end

emphasize(io, str::AbstractString) = have_color ? print_with_color(:red, io, str) : print(io, uppercase(str))

show_linenumber(io::IO, line) = print(io," # line ",line,':')
show_linenumber(io::IO, line, file) = print(io," # ",file,", line ",line,':')
show_linenumber(io::IO, file, line) = print(io," # ", file,", line ",line,':')

# show a block, e g if/for/etc
function show_block(io::IO, head, args::Vector, body, indent::Int)
Expand Down Expand Up @@ -421,7 +419,7 @@ end
## AST printing ##

show_unquoted(io::IO, sym::Symbol, ::Int, ::Int) = print(io, sym)
show_unquoted(io::IO, ex::LineNumberNode, ::Int, ::Int) = show_linenumber(io, ex.line)
show_unquoted(io::IO, ex::LineNumberNode, ::Int, ::Int) = show_linenumber(io, ex.file, ex.line)
show_unquoted(io::IO, ex::LabelNode, ::Int, ::Int) = print(io, ex.label, ": ")
show_unquoted(io::IO, ex::GotoNode, ::Int, ::Int) = print(io, "goto ", ex.label)
show_unquoted(io::IO, ex::TopNode, ::Int, ::Int) = print(io,"top(",ex.name,')')
Expand Down Expand Up @@ -652,10 +650,6 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
print(io, "typealias ")
show_list(io, args, ' ', indent)

elseif is(head, :line) && 1 <= nargs <= 2
show_type = false
show_linenumber(io, args...)

elseif is(head, :if) && nargs == 3 # if/else
show_block(io, "if", args[1], args[2], indent)
show_block(io, "else", args[3], indent)
Expand Down
6 changes: 3 additions & 3 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ jl_lambda_info_t *jl_new_lambda_info(jl_value_t *ast, jl_svec_t *sparams, jl_mod
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_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));
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;
Expand Down
11 changes: 8 additions & 3 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,11 @@ static jl_value_t *scm_to_julia_(value_t e, int eo)

e = cdr_(e);
if (!eo) {
if (sym == line_sym && n==1) {
if (sym == line_sym && n==2) {
return jl_new_struct(jl_linenumbernode_type,
scm_to_julia_(car_(e),0));
scm_to_julia_(car_(cdr_(e)),0), // filename
scm_to_julia_(car_(e),0) // line
);
}
if (sym == label_sym) {
return jl_new_struct(jl_labelnode_type,
Expand Down Expand Up @@ -460,7 +462,10 @@ static value_t julia_to_scm_(jl_value_t *v)
return scmv;
}
if (jl_typeis(v, jl_linenumbernode_type))
return julia_to_list2((jl_value_t*)line_sym, jl_fieldref(v,0));
return fl_cons(julia_to_scm_((jl_value_t*)line_sym),
fl_cons(julia_to_scm_(jl_fieldref(v,1)),
fl_cons(julia_to_scm_(jl_fieldref(v,0)), FL_NIL))
);
if (jl_typeis(v, jl_labelnode_type))
return julia_to_list2((jl_value_t*)label_sym, jl_fieldref(v,0));
if (jl_typeis(v, jl_gotonode_type))
Expand Down
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ size_t jl_static_show_x(JL_STREAM *out, jl_value_t *v, int depth)
n += jl_printf(out, ")");
}
else if (jl_is_linenode(v)) {
n += jl_printf(out, "# line %" PRIuPTR, jl_linenode_line(v));
n += jl_printf(out, "# line %"PRIuPTR" %s", jl_linenode_line(v), jl_linenode_file(v)->name);
}
else if (jl_is_expr(v)) {
jl_expr_t *e = (jl_expr_t*)v;
Expand Down
81 changes: 26 additions & 55 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4055,22 +4055,7 @@ static Function *emit_function(jl_lambda_info_t *lam)
// look for initial (line num filename) node
if (jl_is_linenode(stmt)) {
lno = jl_linenode_line(stmt);
}
else if (jl_is_expr(stmt) && ((jl_expr_t*)stmt)->head == line_sym &&
jl_array_dim0(((jl_expr_t*)stmt)->args) > 0) {
jl_value_t *a1 = jl_exprarg(stmt,0);
if (jl_is_long(a1))
lno = jl_unbox_long(a1);
if (jl_array_dim0(((jl_expr_t*)stmt)->args) > 1) {
a1 = jl_exprarg(stmt,1);
if (jl_is_symbol(a1))
filename = ((jl_sym_t*)a1)->name;
if (jl_array_dim0(((jl_expr_t*)stmt)->args) > 2) {
a1 = jl_exprarg(stmt,2);
if (jl_is_symbol(a1))
dbgFuncName = ((jl_sym_t*)a1)->name;
}
}
filename = jl_linenode_file(stmt)->name;
}
ctx.lineno = lno;

Expand Down Expand Up @@ -4575,64 +4560,50 @@ static Function *emit_function(jl_lambda_info_t *lam)
int prevlno = -1;
for(i=0; i < stmtslen; i++) {
jl_value_t *stmt = jl_cellref(stmts,i);
/*
if (jl_is_linenode(stmt)) {
lno = jl_linenode_line(stmt);
if (ctx.debug_enabled)
builder.SetCurrentDebugLocation(DebugLoc::get(lno, 1, (MDNode*)SP, NULL));
if (do_coverage)
coverageVisitLine(filename, lno);
ctx.lineno = lno;
}
*/
if (jl_is_expr(stmt) && ((jl_expr_t*)stmt)->head == line_sym) {
// get the file and line number associated with this :line
lno = jl_unbox_long(jl_exprarg(stmt, 0));
#ifdef LLVM37
jl_sym_t *file = jl_linenode_file(stmt);

# ifdef LLVM37
DIFile *dfil = NULL;
#else
# else
MDNode *dfil = NULL;
#endif
if (jl_array_dim0(((jl_expr_t*)stmt)->args) > 1) {
jl_value_t *a1 = jl_exprarg(stmt,1);
if (jl_is_symbol(a1)) {
jl_sym_t *file = (jl_sym_t*)a1;
// If the string is not empty
if (*file->name != '\0') {
#ifdef LLVM37
std::map<jl_sym_t *, DIFile *>::iterator it = filescopes.find(file);
#else
std::map<jl_sym_t *, MDNode *>::iterator it = filescopes.find(file);
#endif
if (it != filescopes.end()) {
dfil = it->second;
}
else {
#ifdef LLVM37
dfil = filescopes[file] = (DIFile*)dbuilder.createFile(file->name, ".");
#else
dfil = filescopes[file] = (MDNode*)dbuilder.createFile(file->name, ".");
#endif
}
}
# endif

// If the string is not empty
if (*file->name != '\0') {
# ifdef LLVM37
std::map<jl_sym_t *, DIFile *>::iterator it = filescopes.find(file);
# else
std::map<jl_sym_t *, MDNode *>::iterator it = filescopes.find(file);
# endif
if (it != filescopes.end()) {
dfil = it->second;
} else {
# ifdef LLVM37
dfil = (DIFile*)dbuilder.createFile(file->name, ".");
# else
dfil = (MDNode*)dbuilder.createFile(file->name, ".");
# endif
}
}
// emit debug information
DebugLoc loc;
MDNode *funcscope = (MDNode*)dbuilder.createLexicalBlockFile(SP,topfil);
if (ctx.debug_enabled) {
MDNode *scope;
if (dfil == topfil || dfil == NULL) {
// emit location for the current top-level line
loc = DebugLoc::get(lno, 1, SP, NULL);
// update top-level line number
ctx.lineno = lno;
} else {
// otherwise, we are compiling code from another file,
// so create a location for the top-level line, and
// set the DebugLoc "inlinedAt" parameter.
#ifdef LLVM37
scope = (MDNode*)dbuilder.createLexicalBlockFile(SP,dfil);
#else
scope = (MDNode*)dbuilder.createLexicalBlockFile(SP,DIFile(dfil));
#endif
// inlining against toplevel location
MDNode *inlineLocMd = DebugLoc::get(ctx.lineno, 1, funcscope, NULL).getAsMDNode(jl_LLVMContext);
loc = DebugLoc::get(lno, 1, scope, inlineLocMd);
}
Expand Down
7 changes: 4 additions & 3 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,11 @@ DLLEXPORT jl_function_t *jl_instantiate_staged(jl_methlist_t *m, jl_tupletype_t
func = with_appended_env(m->func, env);
jl_expr_t *body = jl_exprn(jl_symbol("block"), 2);
jl_cellset(ex->args, 1, body);
jl_expr_t *linenode = jl_exprn(line_sym, 2);
jl_value_t *linenode = jl_new_struct(jl_linenumbernode_type,
m->func->linfo->file,
jl_box_long(m->func->linfo->line)
);
jl_cellset(body->args, 0, linenode);
jl_cellset(linenode->args, 0, jl_box_long(m->func->linfo->line));
jl_cellset(linenode->args, 1, m->func->linfo->file);
jl_cellset(body->args, 1, jl_apply(func, jl_svec_data(tt->parameters), jl_nparams(tt)));
if (m->tvars != jl_emptysvec) {
// mark this function as having the same static parameters as the generator
Expand Down
4 changes: 2 additions & 2 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3345,8 +3345,8 @@ void jl_init_types(void)

jl_linenumbernode_type =
jl_new_datatype(jl_symbol("LineNumberNode"), jl_any_type, jl_emptysvec,
jl_svec(1, jl_symbol("line")),
jl_svec(1, jl_long_type), 0, 0, 1);
jl_svec(2, jl_symbol("file"), jl_symbol("line")),
jl_svec(2, jl_symbol_type, jl_long_type), 0, 0, 1);

jl_labelnode_type =
jl_new_datatype(jl_symbol("LabelNode"), jl_any_type, jl_emptysvec,
Expand Down
3 changes: 2 additions & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,8 @@ STATIC_INLINE jl_value_t *jl_cellset(void *a, size_t i, void *x)

#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)[0])
#define jl_linenode_file(x) ((jl_sym_t*)jl_fieldref(x,0))
#define jl_linenode_line(x) (((ptrint_t*)jl_fieldref(x,1))[0])
#define jl_labelnode_label(x) (((ptrint_t*)x)[0])
#define jl_gotonode_label(x) (((ptrint_t*)x)[0])
#define jl_globalref_mod(s) ((jl_module_t*)jl_fieldref(s,0))
Expand Down
2 changes: 1 addition & 1 deletion test/backtrace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ have_backtrace = false
for l in bt
lkup = ccall(:jl_lookup_code_address, Any, (Ptr{Void},), l)
if lkup[1] == :backtrace
@test lkup[5] == false # fromC
@test lkup[6] == false # fromC
have_backtrace = true
break
end
Expand Down

0 comments on commit 9ec72c0

Please sign in to comment.