200200end
201201
202202"""
203- Compute line number annotations for an IRCode
203+ Compute line number annotations for an IRCode or CodeInfo.
204204
205205This functions compute three sets of annotations for each IR line. Take the following
206206example (taken from `@code_typed sin(1.0)`):
@@ -259,7 +259,7 @@ to catch up and print the intermediate scopes. Which scope is printed is indicat
259259by the indentation of the method name and by an increased thickness of the appropriate
260260line for the scope.
261261"""
262- function compute_ir_line_annotations (code:: IRCode )
262+ function compute_ir_line_annotations (code:: Union{ IRCode,CodeInfo} )
263263 loc_annotations = String[]
264264 loc_methods = String[]
265265 loc_lineno = String[]
@@ -269,7 +269,8 @@ function compute_ir_line_annotations(code::IRCode)
269269 last_printed_depth = 0
270270 debuginfo = code. debuginfo
271271 def = :var"unknown scope"
272- for idx in 1 : length (code. stmts)
272+ n = isa (code, IRCode) ? length (code. stmts) : length (code. code)
273+ for idx in 1 : n
273274 buf = IOBuffer ()
274275 print (buf, " │" )
275276 stack = buildLineInfoNode (debuginfo, def, idx)
@@ -833,7 +834,7 @@ function new_nodes_iter(compact::IncrementalCompact)
833834end
834835
835836# print only line numbers on the left, some of the method names and nesting depth on the right
836- function inline_linfo_printer (code:: IRCode )
837+ function inline_linfo_printer (code:: Union{ IRCode,CodeInfo} )
837838 loc_annotations, loc_methods, loc_lineno = compute_ir_line_annotations (code)
838839 max_loc_width = maximum (length, loc_annotations)
839840 max_lineno_width = maximum (length, loc_lineno)
@@ -902,12 +903,15 @@ function stmts_used(::IO, code::CodeInfo)
902903 return used
903904end
904905
905- function default_config (code:: IRCode ; verbose_linetable= false )
906- return IRShowConfig (verbose_linetable ? statementidx_lineinfo_printer (code)
907- : inline_linfo_printer (code);
908- bb_color= :normal )
906+ function default_config (code:: IRCode ; debuginfo = :source_inline )
907+ return IRShowConfig (get_debuginfo_printer (code, debuginfo); bb_color= :normal )
908+ end
909+ default_config (code:: CodeInfo ; debuginfo = :source ) = IRShowConfig (get_debuginfo_printer (code, debuginfo))
910+ function default_config (io:: IO , src)
911+ debuginfo = get (io, :debuginfo , nothing )
912+ debuginfo != = nothing && return default_config (src; debuginfo)
913+ return default_config (src)
909914end
910- default_config (code:: CodeInfo ) = IRShowConfig (statementidx_lineinfo_printer (code))
911915
912916function show_ir_stmts (io:: IO , ir:: Union{IRCode, CodeInfo, IncrementalCompact} , inds, config:: IRShowConfig ,
913917 sptypes:: Vector{VarState} , used:: BitSet , cfg:: CFG , bb_idx:: Int ; pop_new_node! = Returns (nothing ))
@@ -927,8 +931,7 @@ function finish_show_ir(io::IO, cfg::CFG, config::IRShowConfig)
927931 return nothing
928932end
929933
930- function show_ir (io:: IO , ir:: IRCode , config:: IRShowConfig = default_config (ir);
931- pop_new_node! = new_nodes_iter (ir))
934+ function show_ir (io:: IO , ir:: IRCode , config:: IRShowConfig = default_config (io, ir); pop_new_node! = new_nodes_iter (ir))
932935 used = stmts_used (io, ir)
933936 cfg = ir. cfg
934937 maxssaid = length (ir. stmts) + length (ir. new_nodes)
@@ -938,7 +941,7 @@ function show_ir(io::IO, ir::IRCode, config::IRShowConfig=default_config(ir);
938941 finish_show_ir (io, cfg, config)
939942end
940943
941- function show_ir (io:: IO , ci:: CodeInfo , config:: IRShowConfig = default_config (ci);
944+ function show_ir (io:: IO , ci:: CodeInfo , config:: IRShowConfig = default_config (io, ci);
942945 pop_new_node! = Returns (nothing ))
943946 used = stmts_used (io, ci)
944947 cfg = compute_basic_blocks (ci. code)
@@ -952,7 +955,7 @@ function show_ir(io::IO, ci::CodeInfo, config::IRShowConfig=default_config(ci);
952955 finish_show_ir (io, cfg, config)
953956end
954957
955- function show_ir (io:: IO , compact:: IncrementalCompact , config:: IRShowConfig = default_config (compact. ir))
958+ function show_ir (io:: IO , compact:: IncrementalCompact , config:: IRShowConfig = default_config (io, compact. ir))
956959 cfg = compact. ir. cfg
957960
958961
@@ -1154,3 +1157,35 @@ const __debuginfo = Dict{Symbol, Any}(
11541157 )
11551158const default_debuginfo = Ref {Symbol} (:none )
11561159debuginfo (sym) = sym === :default ? default_debuginfo[] : sym
1160+
1161+ const __debuginfo = Dict {Symbol, Any} (
1162+ # :full => src -> statementidx_lineinfo_printer(src), # and add variable slot information
1163+ :source => src -> statementidx_lineinfo_printer (src),
1164+ :source_inline => src -> inline_linfo_printer (src),
1165+ # :oneliner => src -> statementidx_lineinfo_printer(PartialLineInfoPrinter, src),
1166+ :none => src -> lineinfo_disabled,
1167+ )
1168+
1169+ const debuginfo_modes = [:none , :source , :source_inline ]
1170+ @assert Set (debuginfo_modes) == Set (keys (__debuginfo))
1171+
1172+ function validate_debuginfo_mode (mode:: Symbol )
1173+ in (mode, debuginfo_modes) && return true
1174+ throw (ArgumentError (" `debuginfo` must be one of the following: $(join ([repr (mode) for mode in debuginfo_modes], " , " )) " ))
1175+ end
1176+
1177+ const default_debuginfo_mode = Ref {Symbol} (:none )
1178+ function expand_debuginfo_mode (mode:: Symbol , default = default_debuginfo_mode[])
1179+ if mode === :default
1180+ mode = default
1181+ end
1182+ validate_debuginfo_mode (mode)
1183+ return mode
1184+ end
1185+
1186+ function get_debuginfo_printer (mode:: Symbol )
1187+ mode = expand_debuginfo_mode (mode)
1188+ return __debuginfo[mode]
1189+ end
1190+
1191+ get_debuginfo_printer (src, mode:: Symbol ) = get_debuginfo_printer (mode)(src)
0 commit comments