Skip to content

Commit 9b75678

Browse files
committed
Toplevel: consume debug events
1 parent 1af45ee commit 9b75678

File tree

6 files changed

+100
-74
lines changed

6 files changed

+100
-74
lines changed

compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ let split_primitives p =
1616
let () =
1717
let global = J.pure_js_expr "globalThis" in
1818
(* this needs to stay synchronized with toplevel.js *)
19-
let toplevel_compile (s : bytes array) : unit -> J.t =
19+
let toplevel_compile (s : bytes array) (debug : Instruct.debug_event list array) :
20+
unit -> J.t =
2021
let s = String.concat ~sep:"" (List.map ~f:Bytes.to_string (Array.to_list s)) in
2122
let prims = split_primitives (Symtable.data_primitive_names ()) in
22-
let output_program = Driver.from_string prims s in
23+
let output_program = Driver.from_string ~prims ~debug s in
2324
let b = Buffer.create 100 in
2425
output_program (Pretty_print.to_buffer b);
2526
Format.(pp_print_flush std_formatter ());

compiler/lib/driver.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ let f
543543
d
544544
p
545545

546-
let from_string prims s formatter =
547-
let p, d = Parse_bytecode.from_string prims s in
546+
let from_string ~prims ~debug s formatter =
547+
let p, d = Parse_bytecode.from_string ~prims ~debug s in
548548
full
549549
~standalone:false
550550
~wrap_with_fun:`Anonymous

compiler/lib/driver.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ val f :
3232
-> Code.program
3333
-> unit
3434

35-
val from_string : string array -> string -> Pretty_print.t -> unit
35+
val from_string :
36+
prims:string array
37+
-> debug:Instruct.debug_event list array
38+
-> string
39+
-> Pretty_print.t
40+
-> unit
3641

3742
val profiles : (int * profile) list
3843

compiler/lib/parse_bytecode.ml

Lines changed: 81 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ module Debug : sig
5353

5454
val mem : t -> Code.Addr.t -> bool
5555

56+
val read_event :
57+
paths:string list
58+
-> crcs:(string, string option) Hashtbl.t
59+
-> orig:int
60+
-> t
61+
-> Instruct.debug_event
62+
-> unit
63+
5664
val read :
5765
t -> crcs:(string * string option) list -> includes:string list -> in_channel -> unit
5866

@@ -125,6 +133,67 @@ end = struct
125133
| Some _ as x -> x
126134
| None -> Fs.find_in_path paths (name ^ ".ml")
127135

136+
let read_event
137+
~paths
138+
~crcs
139+
~orig
140+
{ events_by_pc; units; pos_fname_to_source; names; enabled; include_cmis = _ }
141+
ev =
142+
let pos_fname =
143+
match ev.ev_loc.Location.loc_start.Lexing.pos_fname with
144+
| "_none_" -> None
145+
| x -> Some x
146+
in
147+
let ev_module = ev.ev_module in
148+
let unit =
149+
try Hashtbl.find units (ev_module, pos_fname)
150+
with Not_found ->
151+
let crc = try Hashtbl.find crcs ev_module with Not_found -> None in
152+
let source : path option =
153+
(* First search the source based on [pos_fname] because the
154+
filename of the source might be unreleased to the
155+
module name. (e.g. pos_fname = list.ml, module = Stdlib__list) *)
156+
let from_pos_fname =
157+
match pos_fname with
158+
| None -> None
159+
| Some pos_fname -> (
160+
match Fs.find_in_path paths pos_fname with
161+
| Some _ as x -> x
162+
| None -> Fs.find_in_path paths (Filename.basename pos_fname))
163+
in
164+
match from_pos_fname with
165+
| None -> find_ml_in_paths paths ev_module
166+
| Some _ as x -> x
167+
in
168+
let source =
169+
match source with
170+
| None -> None
171+
| Some source -> Some (Fs.absolute_path source)
172+
in
173+
if debug_sourcemap ()
174+
then
175+
Format.eprintf
176+
"module:%s - source:%s - name:%s\n%!"
177+
ev_module
178+
(match source with
179+
| None -> "NONE"
180+
| Some x -> x)
181+
(match pos_fname with
182+
| None -> "NONE"
183+
| Some x -> x);
184+
let u = { module_name = ev_module; crc; source; paths } in
185+
(match pos_fname, source with
186+
| None, _ | _, None -> ()
187+
| Some pos_fname, Some source ->
188+
String_table.add pos_fname_to_source pos_fname source);
189+
Hashtbl.add units (ev_module, pos_fname) u;
190+
u
191+
in
192+
relocate_event orig ev;
193+
if enabled || names
194+
then Int_table.add events_by_pc ev.ev_pos { event = ev; source = unit.source };
195+
()
196+
128197
let read_event_list =
129198
let rewrite_path path =
130199
if Filename.is_relative path
@@ -135,73 +204,15 @@ end = struct
135204
| None -> path
136205
in
137206
let read_paths ic : string list = List.map (input_value ic) ~f:rewrite_path in
138-
fun { events_by_pc; units; pos_fname_to_source; names; enabled; include_cmis = _ }
139-
~crcs
140-
~includes
141-
~orig
142-
ic ->
207+
fun debug ~crcs ~includes ~orig ic ->
143208
let crcs =
144209
let t = Hashtbl.create 17 in
145210
List.iter crcs ~f:(fun (m, crc) -> Hashtbl.add t m crc);
146211
t
147212
in
148213
let evl : debug_event list = input_value ic in
149214
let paths = read_paths ic @ includes in
150-
List.iter evl ~f:(fun ev ->
151-
let pos_fname =
152-
match ev.ev_loc.Location.loc_start.Lexing.pos_fname with
153-
| "_none_" -> None
154-
| x -> Some x
155-
in
156-
let ev_module = ev.ev_module in
157-
let unit =
158-
try Hashtbl.find units (ev_module, pos_fname)
159-
with Not_found ->
160-
let crc = try Hashtbl.find crcs ev_module with Not_found -> None in
161-
let source : path option =
162-
(* First search the source based on [pos_fname] because the
163-
filename of the source might be unreleased to the
164-
module name. (e.g. pos_fname = list.ml, module = Stdlib__list) *)
165-
let from_pos_fname =
166-
match pos_fname with
167-
| None -> None
168-
| Some pos_fname -> (
169-
match Fs.find_in_path paths pos_fname with
170-
| Some _ as x -> x
171-
| None -> Fs.find_in_path paths (Filename.basename pos_fname))
172-
in
173-
match from_pos_fname with
174-
| None -> find_ml_in_paths paths ev_module
175-
| Some _ as x -> x
176-
in
177-
let source =
178-
match source with
179-
| None -> None
180-
| Some source -> Some (Fs.absolute_path source)
181-
in
182-
if debug_sourcemap ()
183-
then
184-
Format.eprintf
185-
"module:%s - source:%s - name:%s\n%!"
186-
ev_module
187-
(match source with
188-
| None -> "NONE"
189-
| Some x -> x)
190-
(match pos_fname with
191-
| None -> "NONE"
192-
| Some x -> x);
193-
let u = { module_name = ev_module; crc; source; paths } in
194-
(match pos_fname, source with
195-
| None, _ | _, None -> ()
196-
| Some pos_fname, Some source ->
197-
String_table.add pos_fname_to_source pos_fname source);
198-
Hashtbl.add units (ev_module, pos_fname) u;
199-
u
200-
in
201-
relocate_event orig ev;
202-
if enabled || names
203-
then Int_table.add events_by_pc ev.ev_pos { event = ev; source = unit.source };
204-
())
215+
List.iter evl ~f:(read_event ~paths ~crcs ~orig debug)
205216

206217
let find_source { pos_fname_to_source; _ } pos_fname =
207218
match pos_fname with
@@ -675,7 +686,7 @@ module State = struct
675686
| Some loc -> Var.loc v (pi_of_loc debug loc));
676687
Var.name v nm;
677688
name_rec debug (i + 1) lrem srem summary
678-
| (j, _, _) :: _, _ :: srem when i < j -> name_rec debug (i + 1) l srem summary
689+
| (j, _nm, _) :: _, _ :: srem when i < j -> name_rec debug (i + 1) l srem summary
679690
| _ -> assert false
680691

681692
let name_vars st debug pc =
@@ -2455,8 +2466,13 @@ let from_exe
24552466
{ code; cmis; debug = debug_data }
24562467

24572468
(* As input: list of primitives + size of global table *)
2458-
let from_bytes primitives (code : bytecode) =
2459-
let debug_data = Debug.create ~include_cmis:false false in
2469+
let from_bytes ~prims ~debug (code : bytecode) =
2470+
let debug_data = Debug.create ~include_cmis:false true in
2471+
if Debug.names debug_data
2472+
then
2473+
Array.iter debug ~f:(fun l ->
2474+
List.iter l ~f:(fun ev ->
2475+
Debug.read_event ~paths:[] ~crcs:(Hashtbl.create 17) ~orig:0 debug_data ev));
24602476
let ident_table =
24612477
let t = Hashtbl.create 17 in
24622478
if Debug.names debug_data
@@ -2466,7 +2482,7 @@ let from_bytes primitives (code : bytecode) =
24662482
(Symtable.current_state ());
24672483
t
24682484
in
2469-
let globals = make_globals 0 [||] primitives in
2485+
let globals = make_globals 0 [||] prims in
24702486
let p = parse_bytecode code globals debug_data in
24712487
let gdata = Var.fresh_n "global_data" in
24722488
let need_gdata = ref false in
@@ -2500,7 +2516,7 @@ let from_bytes primitives (code : bytecode) =
25002516
in
25012517
prepend p body, debug_data
25022518

2503-
let from_string primitives (code : string) = from_bytes primitives code
2519+
let from_string ~prims ~debug (code : string) = from_bytes ~prims ~debug code
25042520

25052521
module Reloc = struct
25062522
let gen_patch_int buff pos n =

compiler/lib/parse_bytecode.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ val from_channel :
7676
in_channel
7777
-> [ `Cmo of Cmo_format.compilation_unit | `Cma of Cmo_format.library | `Exe ]
7878

79-
val from_string : string array -> string -> Code.program * Debug.t
79+
val from_string :
80+
prims:string array
81+
-> debug:Instruct.debug_event list array
82+
-> string
83+
-> Code.program * Debug.t
8084

8185
val predefined_exceptions : unit -> Code.program

runtime/toplevel.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ function caml_get_section_table () {
5353
//Version: < 4.08
5454
function caml_reify_bytecode (code, _sz) {
5555
if(globalThis.toplevelCompile)
56-
return globalThis.toplevelCompile([0,code]);
56+
return globalThis.toplevelCompile([0,code], [0]);
5757
else caml_failwith("Toplevel not initialized (toplevelCompile)")
5858
}
5959

6060
//Provides: caml_reify_bytecode
6161
//Requires: caml_failwith
6262
//Version: >= 4.08
63-
function caml_reify_bytecode (code, _sz,_) {
63+
function caml_reify_bytecode (code, debug,_digest) {
6464
if(globalThis.toplevelCompile)
65-
return [0, 0, globalThis.toplevelCompile(code)];
65+
return [0, 0, globalThis.toplevelCompile(code,debug)];
6666
else caml_failwith("Toplevel not initialized (toplevelCompile)")
6767
}
6868

0 commit comments

Comments
 (0)