Skip to content

Commit a669c00

Browse files
authored
flambda-backend: Refactor Debuginfo.t (#1724)
* Make Debuginfo.t abstract * Debuginfo.t is a record
1 parent 91ab70a commit a669c00

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

asmcomp/emitaux.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ let emit_frames a =
177177
in
178178
let debuginfos = Label_table.create 7 in
179179
let label_debuginfos rs dbg =
180-
let rdbg = List.rev dbg in
181-
let key = (rs, rdbg) in
180+
let key = (rs, dbg) in
182181
try Label_table.find debuginfos key
183182
with Not_found ->
184183
let lbl = Cmm.new_label () in
@@ -261,7 +260,8 @@ let emit_frames a =
261260
(add (shift_left (of_int kind) 1)
262261
(of_int has_next)))))
263262
in
264-
let emit_debuginfo (rs, rdbg) lbl =
263+
let emit_debuginfo (rs, dbg) lbl =
264+
let rdbg = dbg |> Debuginfo.to_list |> List.rev in
265265
(* Due to inlined functions, a single debuginfo may have multiple locations.
266266
These are represented sequentially in memory (innermost frame first),
267267
with the low bit of the packed debuginfo being 0 on the last entry. *)
@@ -346,6 +346,7 @@ let reset_debug_info () =
346346
(* We only display .file if the file has not been seen before. We
347347
display .loc for every instruction. *)
348348
let emit_debug_info_gen dbg file_emitter loc_emitter =
349+
let dbg = Debuginfo.to_list dbg in
349350
if is_cfi_enabled () &&
350351
(!Clflags.debug || Config.with_frame_pointers) then begin
351352
match List.rev dbg with

lambda/debuginfo.ml

+21-13
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ module Scoped_location = struct
8787
cons scopes Sc_method_definition str s
8888

8989
let enter_lazy ~scopes = cons scopes Sc_lazy (str scopes) ""
90-
90+
9191
let enter_partial_or_eta_wrapper ~scopes =
9292
cons scopes Sc_partial_or_eta_wrapper (dot ~no_parens:() scopes "(partial)") ""
9393

@@ -143,20 +143,21 @@ type item = {
143143
dinfo_scopes: Scoped_location.scopes;
144144
}
145145

146-
type t = item list
146+
type t = { dbg : item list; }
147147

148148
type alloc_dbginfo_item =
149149
{ alloc_words : int;
150150
alloc_dbg : t }
151151
type alloc_dbginfo = alloc_dbginfo_item list
152152

153-
let none = []
153+
let none = { dbg = []; }
154154

155-
let is_none = function
155+
let is_none { dbg } =
156+
match dbg with
156157
| [] -> true
157158
| _ :: _ -> false
158159

159-
let to_string dbg =
160+
let to_string { dbg } =
160161
match dbg with
161162
| [] -> ""
162163
| ds ->
@@ -190,12 +191,13 @@ let item_from_location ~scopes loc =
190191
}
191192

192193
let from_location = function
193-
| Scoped_location.Loc_unknown -> []
194+
| Scoped_location.Loc_unknown -> { dbg = []; }
194195
| Scoped_location.Loc_known {scopes; loc} ->
195196
assert (not (Location.is_none loc));
196-
[item_from_location ~scopes loc]
197+
{ dbg = [item_from_location ~scopes loc]; }
197198

198-
let to_location = function
199+
let to_location { dbg } =
200+
match dbg with
199201
| [] -> Location.none
200202
| d :: _ ->
201203
let loc_start =
@@ -212,15 +214,15 @@ let to_location = function
212214
} in
213215
{ loc_ghost = false; loc_start; loc_end; }
214216

215-
let inline dbg1 dbg2 =
216-
dbg1 @ dbg2
217+
let inline { dbg = dbg1; } { dbg = dbg2; } =
218+
{ dbg = dbg1 @ dbg2; }
217219

218220
(* CR-someday afrisch: FWIW, the current compare function does not seem very
219221
good, since it reverses the two lists. I don't know how long the lists are,
220222
nor if the specific currently implemented ordering is useful in other
221223
contexts, but if one wants to use Map, a more efficient comparison should
222224
be considered. *)
223-
let compare dbg1 dbg2 =
225+
let compare { dbg = dbg1; } { dbg = dbg2; } =
224226
let rec loop ds1 ds2 =
225227
match ds1, ds2 with
226228
| [], [] -> 0
@@ -245,8 +247,8 @@ let compare dbg1 dbg2 =
245247
in
246248
loop (List.rev dbg1) (List.rev dbg2)
247249

248-
let hash t =
249-
List.fold_left (fun hash item -> Hashtbl.hash (hash, item)) 0 t
250+
let hash { dbg; } =
251+
List.fold_left (fun hash item -> Hashtbl.hash (hash, item)) 0 dbg
250252

251253
let rec print_compact ppf t =
252254
let print_item item =
@@ -264,3 +266,9 @@ let rec print_compact ppf t =
264266
print_item item;
265267
Format.fprintf ppf ";";
266268
print_compact ppf t
269+
270+
let print_compact ppf { dbg; } = print_compact ppf dbg
271+
272+
let to_list { dbg; } = dbg
273+
274+
let length { dbg; } = List.length dbg

lambda/debuginfo.mli

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type item = private {
6363
dinfo_scopes: Scoped_location.scopes;
6464
}
6565

66-
type t = item list
66+
type t
6767

6868
type alloc_dbginfo_item =
6969
{ alloc_words : int;
@@ -93,3 +93,7 @@ val compare : t -> t -> int
9393
val hash : t -> int
9494

9595
val print_compact : Format.formatter -> t -> unit
96+
97+
val to_list : t -> item list
98+
99+
val length : t -> int

0 commit comments

Comments
 (0)