-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathasmgen.ml
557 lines (503 loc) · 21.2 KB
/
asmgen.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* From lambda to assembly code *)
[@@@ocaml.warning "+a-4-9-40-41-42"]
open Format
open Config
open Clflags
open Misc
open Cmm
module String = Misc.Stdlib.String
type error =
| Assembler_error of string
| Mismatched_for_pack of Compilation_unit.Prefix.t
| Asm_generation of string * Emitaux.error
exception Error of error
let cmm_invariants ppf fd_cmm =
let print_fundecl =
if !Clflags.dump_cmm then Printcmm.fundecl
else fun ppf fdecl -> Format.fprintf ppf "%s" fdecl.fun_name
in
if !Clflags.cmm_invariants && Cmm_invariants.run ppf fd_cmm then
Misc.fatal_errorf "Cmm invariants failed on following fundecl:@.%a@."
print_fundecl fd_cmm;
fd_cmm
let liveness phrase = Liveness.fundecl phrase; phrase
let dump_if ppf flag message phrase =
if !flag then Printmach.phase message ppf phrase
let pass_dump_if ppf flag message phrase =
dump_if ppf flag message phrase; phrase
let pass_dump_linear_if ppf flag message phrase =
if !flag then fprintf ppf "*** %s@.%a@." message Printlinear.fundecl phrase;
phrase
let pass_dump_cfg_if ppf flag message c =
if !flag then
fprintf ppf "*** %s@.%a@." message (Cfg_with_layout.dump ~msg:"") c;
c
let start_from_emit = ref true
let should_save_before_emit () =
should_save_ir_after Compiler_pass.Scheduling && (not !start_from_emit)
let should_save_cfg_before_emit () =
should_save_ir_after Compiler_pass.Simplify_cfg && (not !start_from_emit)
let linear_unit_info =
{ Linear_format.unit = Compilation_unit.dummy;
items = [];
}
let new_cfg_unit_info () =
{ Cfg_format.unit = Compilation_unit.dummy;
items = [];
}
let cfg_unit_info = new_cfg_unit_info ()
module Compiler_pass_map = Map.Make(Compiler_pass)
let (pass_to_cfg : Cfg_format.cfg_unit_info Compiler_pass_map.t) =
Compiler_pass_map.empty
|> Compiler_pass_map.add Compiler_pass.Selection (new_cfg_unit_info ())
let reset () =
Checkmach.reset_unit_info ();
start_from_emit := false;
Compiler_pass_map.iter (fun pass (cfg_unit_info : Cfg_format.cfg_unit_info) ->
if should_save_ir_after pass then begin
cfg_unit_info.unit <- Compilation_unit.get_current_or_dummy ();
cfg_unit_info.items <- [];
end)
pass_to_cfg;
if should_save_before_emit () then begin
linear_unit_info.unit <- Compilation_unit.get_current_or_dummy ();
linear_unit_info.items <- [];
end;
if should_save_cfg_before_emit () then begin
cfg_unit_info.unit <- Compilation_unit.get_current_or_dummy ();
cfg_unit_info.items <- [];
end
let save_data dl =
Compiler_pass_map.iter (fun pass (cfg_unit_info: Cfg_format.cfg_unit_info) ->
if should_save_ir_after pass && (not !start_from_emit) then begin
cfg_unit_info.items <- Cfg_format.(Data dl) :: cfg_unit_info.items
end)
pass_to_cfg;
if should_save_before_emit () then begin
linear_unit_info.items <- Linear_format.(Data dl) :: linear_unit_info.items
end;
if should_save_cfg_before_emit () then begin
cfg_unit_info.items <- Cfg_format.(Data dl) :: cfg_unit_info.items
end;
dl
let save_linear f =
if should_save_before_emit () then begin
linear_unit_info.items <- Linear_format.(Func f) :: linear_unit_info.items
end;
f
let save_cfg f =
if should_save_cfg_before_emit () then begin
cfg_unit_info.items <- Cfg_format.(Cfg f) :: cfg_unit_info.items
end;
f
let save_mach_as_cfg pass f =
if should_save_ir_after pass && (not !start_from_emit) then begin
let cfg =
Cfgize.fundecl f ~before_register_allocation:false ~preserve_orig_labels:false ~simplify_terminators:true
in
let cfg_unit_info = Compiler_pass_map.find pass pass_to_cfg in
cfg_unit_info.items <- Cfg_format.(Cfg cfg) :: cfg_unit_info.items
end;
f
let write_ir prefix =
Compiler_pass_map.iter (fun pass (cfg_unit_info : Cfg_format.cfg_unit_info) ->
if should_save_ir_after pass && (not !start_from_emit) then begin
let filename = Compiler_pass.(to_output_filename pass ~prefix) in
cfg_unit_info.items <- List.rev cfg_unit_info.items;
Cfg_format.save filename cfg_unit_info end)
pass_to_cfg;
if should_save_before_emit () then begin
let filename = Compiler_pass.(to_output_filename Scheduling ~prefix) in
linear_unit_info.items <- List.rev linear_unit_info.items;
Linear_format.save filename linear_unit_info
end;
if should_save_cfg_before_emit () then begin
if not !Flambda_backend_flags.use_ocamlcfg then
Misc.fatal_error "Flag '-save-ir-after simplify_cfg' requires '-ocamlcfg'";
let filename = Compiler_pass.(to_output_filename Simplify_cfg ~prefix) in
cfg_unit_info.items <- List.rev cfg_unit_info.items;
Cfg_format.save filename cfg_unit_info
end
let should_emit () =
not (should_stop_after Compiler_pass.Scheduling)
let should_use_linscan fd =
!use_linscan ||
List.mem Cmm.Use_linscan_regalloc fd.Mach.fun_codegen_options
let if_emit_do f x = if should_emit () then f x else ()
let emit_begin_assembly unix = if_emit_do Emit.begin_assembly unix
let emit_end_assembly filename () =
if_emit_do (fun () ->
try Emit.end_assembly ()
with Emitaux.Error e ->
raise (Error (Asm_generation(filename, e))))
()
let emit_data dl = if_emit_do Emit.data dl
let emit_fundecl f =
if_emit_do
(fun (fundecl : Linear.fundecl) ->
try
Profile.record ~accumulate:true "emit" Emit.fundecl fundecl
with Emitaux.Error e ->
raise (Error (Asm_generation(fundecl.Linear.fun_name, e))))
f
let rec regalloc ~ppf_dump round fd =
if round > 50 then
fatal_error(fd.Mach.fun_name ^
": function too complex, cannot complete register allocation");
dump_if ppf_dump dump_live "Liveness analysis" fd;
let num_stack_slots =
if should_use_linscan fd then begin
(* Linear Scan *)
Interval.build_intervals fd;
if !dump_interval then Printmach.intervals ppf_dump ();
Linscan.allocate_registers()
end else begin
(* Graph Coloring *)
Interf.build_graph fd;
if !dump_interf then Printmach.interferences ppf_dump ();
if !dump_prefer then Printmach.preferences ppf_dump ();
Coloring.allocate_registers()
end
in
dump_if ppf_dump dump_regalloc "After register allocation" fd;
let (newfd, redo_regalloc) = Reload.fundecl fd num_stack_slots in
dump_if ppf_dump dump_reload "After insertion of reloading code" newfd;
if redo_regalloc then begin
Reg.reinit(); Liveness.fundecl newfd; regalloc ~ppf_dump (round + 1) newfd
end else begin
(* Ensure the hooks are called only once. *)
Compiler_hooks.execute Compiler_hooks.Mach_reload newfd;
newfd
end
let (++) x f = f x
let ocamlcfg_verbose =
match Sys.getenv_opt "OCAMLCFG_VERBOSE" with
| Some "1" -> true
| Some _ | None -> false
let reorder_blocks_random ppf_dump cl =
match !Flambda_backend_flags.reorder_blocks_random with
| None -> cl
| Some seed ->
(* Initialize random state based on user-provided seed and function name.
Per-function random state (instead of per call to ocamlopt)
is good for debugging: it gives us deterministic builds
for each user-provided seed, regardless
of the order of files on the command line. *)
let fun_name = (Cfg_with_layout.cfg cl).fun_name in
let random_state = Random.State.make [| seed; Hashtbl.hash fun_name |] in
Cfg_with_layout.reorder_blocks_random ~random_state cl;
pass_dump_cfg_if ppf_dump Flambda_backend_flags.dump_cfg
"After reorder_blocks_random" cl
let cfgize (f : Mach.fundecl) : Cfg_with_layout.t =
if ocamlcfg_verbose then begin
Format.eprintf "Asmgen.cfgize on function %s...\n%!" f.Mach.fun_name;
end;
Cfgize.fundecl
f
~before_register_allocation:true
~preserve_orig_labels:false
~simplify_terminators:true
type register_allocator =
| Upstream
| IRC
let register_allocator : register_allocator =
match Sys.getenv_opt "REGISTER_ALLOCATOR" with
| None -> Upstream
| Some id ->
match String.lowercase_ascii id with
| "irc" -> IRC
| "" | "upstream" -> Upstream
| _ -> Misc.fatal_errorf "unknown register allocator %S" id
let compile_fundecl ~ppf_dump ~funcnames fd_cmm =
Proc.init ();
Reg.reset();
fd_cmm
++ Profile.record ~accumulate:true "cmm_invariants" (cmm_invariants ppf_dump)
++ Profile.record ~accumulate:true "selection"
(Selection.fundecl ~future_funcnames:funcnames)
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_sel
++ pass_dump_if ppf_dump dump_selection "After instruction selection"
++ Profile.record ~accumulate:true "save_mach_as_cfg"
(save_mach_as_cfg Compiler_pass.Selection)
++ Profile.record ~accumulate:true "polling"
(Polling.instrument_fundecl ~future_funcnames:funcnames)
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_polling
++ Profile.record ~accumulate:true "comballoc" Comballoc.fundecl
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_combine
++ pass_dump_if ppf_dump dump_combine "After allocation combining"
++ Profile.record ~accumulate:true "cse" CSE.fundecl
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_cse
++ pass_dump_if ppf_dump dump_cse "After CSE"
++ Profile.record ~accumulate:true "checkmach" (Checkmach.fundecl ppf_dump)
++ Profile.record ~accumulate:true "regalloc" (fun (fd : Mach.fundecl) ->
let force_linscan = should_use_linscan fd in
match force_linscan, register_allocator with
| false, IRC ->
fd
++ Profile.record ~accumulate:true "irc" (fun fd ->
let cfg =
fd
++ Profile.record ~accumulate:true "cfgize" cfgize
++ Cfg_with_liveness.make
++ Profile.record ~accumulate:true "cfg_deadcode" Cfg_deadcode.run
in
let cfg_description =
Profile.record ~accumulate:true "cfg_create_description"
Cfg_regalloc_validate.Description.create (Cfg_with_liveness.cfg_with_layout cfg)
in
cfg
++ Profile.record ~accumulate:true "cfg_irc" Cfg_irc.run
++ Cfg_with_liveness.cfg_with_layout
++ Profile.record ~accumulate:true "cfg_validate_description" (Cfg_regalloc_validate.run cfg_description)
++ Profile.record ~accumulate:true "cfg_simplify" Cfg_regalloc_utils.simplify_cfg
++ Profile.record ~accumulate:true "save_cfg" save_cfg
++ Profile.record ~accumulate:true "cfg_reorder_blocks"
(reorder_blocks_random ppf_dump)
++ Profile.record ~accumulate:true "cfg_to_linear" Cfg_to_linear.run)
| true, _ | false, Upstream ->
fd
++ Profile.record ~accumulate:true "default" (fun fd ->
fd
++ Profile.record ~accumulate:true "liveness" liveness
++ Profile.record ~accumulate:true "deadcode" Deadcode.fundecl
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_live
++ pass_dump_if ppf_dump dump_live "Liveness analysis"
++ Profile.record ~accumulate:true "spill" Spill.fundecl
++ Profile.record ~accumulate:true "liveness" liveness
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_spill
++ pass_dump_if ppf_dump dump_spill "After spilling"
++ Profile.record ~accumulate:true "split" Split.fundecl
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Mach_split
++ pass_dump_if ppf_dump dump_split "After live range splitting"
++ Profile.record ~accumulate:true "liveness" liveness
++ Profile.record ~accumulate:true "regalloc" (regalloc ~ppf_dump 1)
++ Profile.record ~accumulate:true "available_regs" Available_regs.fundecl
++ Profile.record ~accumulate:true "mach to linear" (fun (fd : Mach.fundecl) ->
if !Flambda_backend_flags.use_ocamlcfg then begin
fd
++ Profile.record ~accumulate:true "cfgize"
(Cfgize.fundecl
~before_register_allocation:false
~preserve_orig_labels:false
~simplify_terminators:true)
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Cfg
++ pass_dump_cfg_if ppf_dump Flambda_backend_flags.dump_cfg "After linear_to_cfg"
++ Profile.record ~accumulate:true "save_cfg" save_cfg
++ Profile.record ~accumulate:true "cfg_reorder_blocks"
(reorder_blocks_random ppf_dump)
++ Profile.record ~accumulate:true "cfg_to_linear" Cfg_to_linear.run
end else begin
fd
++ Profile.record ~accumulate:true "linearize" Linearize.fundecl
end))
++ pass_dump_linear_if ppf_dump dump_linear "Linearized code")
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Linear
++ Profile.record ~accumulate:true "scheduling" Scheduling.fundecl
++ pass_dump_linear_if ppf_dump dump_scheduling "After instruction scheduling"
++ Profile.record ~accumulate:true "save_linear" save_linear
++ Profile.record ~accumulate:true "emit_fundecl" emit_fundecl
let compile_data dl =
dl
++ save_data
++ emit_data
let compile_phrases ~ppf_dump ps =
let funcnames =
List.fold_left (fun s p ->
match p with
| Cfunction fd -> String.Set.add fd.fun_name s
| Cdata _ -> s)
String.Set.empty ps
in
let rec compile ~funcnames ps =
match ps with
| [] -> ()
| p :: ps ->
if !dump_cmm then fprintf ppf_dump "%a@." Printcmm.phrase p;
match p with
| Cfunction fd ->
compile_fundecl ~ppf_dump ~funcnames fd;
compile ~funcnames:(String.Set.remove fd.fun_name funcnames) ps
| Cdata dl ->
compile_data dl;
compile ~funcnames ps
in
compile ~funcnames ps
let compile_phrase ~ppf_dump p =
compile_phrases ~ppf_dump [p]
(* For the native toplevel: generates generic functions unless
they are already available in the process *)
let compile_genfuns ~ppf_dump f =
List.iter
(function
| (Cfunction {fun_name = name}) as ph when f name ->
compile_phrase ~ppf_dump ph
| _ -> ())
(Cmm_helpers.generic_functions true
(Cmm_helpers.Generic_fns_tbl.of_fns
(Compilenv.current_unit_infos ()).ui_generic_fns))
let compile_unit ~output_prefix ~asm_filename ~keep_asm ~obj_filename ~may_reduce_heap
~ppf_dump gen =
reset ();
let create_asm = should_emit () &&
(keep_asm || not !Emitaux.binary_backend_available) in
Emitaux.create_asm_file := create_asm;
Misc.try_finally
~exceptionally:(fun () -> remove_file obj_filename)
(fun () ->
if create_asm then Emitaux.output_channel := open_out asm_filename;
Misc.try_finally
(fun () ->
gen ();
Checkmach.record_unit_info ppf_dump;
write_ir output_prefix)
~always:(fun () ->
if create_asm then close_out !Emitaux.output_channel)
~exceptionally:(fun () ->
if create_asm && not keep_asm then remove_file asm_filename);
if should_emit () then begin
if may_reduce_heap then
Emitaux.reduce_heap_size ~reset:(fun () ->
reset ();
(* note: we need to preserve the persistent env, because it is
used to populate fields of the record written as the cmx file
afterwards. *)
Typemod.reset ~preserve_persistent_env:true;
Emitaux.reset ();
Reg.reset ());
let assemble_result =
Profile.record "assemble"
(Proc.assemble_file asm_filename) obj_filename
in
if assemble_result <> 0
then raise(Error(Assembler_error asm_filename));
end;
if create_asm && not keep_asm then remove_file asm_filename
)
let end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile make_cmm =
Emitaux.Dwarf_helpers.init ~disable_dwarf:false sourcefile;
emit_begin_assembly unix;
make_cmm ()
++ Compiler_hooks.execute_and_pipe Compiler_hooks.Cmm
++ Profile.record "compile_phrases" (compile_phrases ~ppf_dump)
++ (fun () -> ());
(match toplevel with None -> () | Some f -> compile_genfuns ~ppf_dump f);
(* We add explicit references to external primitive symbols. This
is to ensure that the object files that define these symbols,
when part of a C library, won't be discarded by the linker.
This is important if a module that uses such a symbol is later
dynlinked. *)
compile_phrase ~ppf_dump
(Cmm_helpers.reference_symbols
(List.filter_map (fun prim ->
if not (Primitive.native_name_is_external prim) then None
else Some (Primitive.native_name prim))
!Translmod.primitive_declarations));
emit_end_assembly sourcefile ()
let end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile clambda =
end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile (fun () ->
Profile.record "cmm" Cmmgen.compunit clambda)
type middle_end =
backend:(module Backend_intf.S)
-> filename:string
-> prefixname:string
-> ppf_dump:Format.formatter
-> Lambda.program
-> Clambda.with_constants
let asm_filename output_prefix =
if !keep_asm_file || !Emitaux.binary_backend_available
then output_prefix ^ ext_asm
else Filename.temp_file "camlasm" ext_asm
let compile_implementation unix ?toplevel ~backend ~filename ~prefixname
~middle_end ~ppf_dump (program : Lambda.program) =
compile_unit ~ppf_dump ~output_prefix:prefixname
~asm_filename:(asm_filename prefixname) ~keep_asm:!keep_asm_file
~obj_filename:(prefixname ^ ext_obj)
~may_reduce_heap:(Option.is_none toplevel)
(fun () ->
Compilation_unit.Set.iter Compilenv.require_global
program.required_globals;
let clambda_with_constants =
middle_end ~backend ~filename ~prefixname ~ppf_dump program
in
end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile:filename
clambda_with_constants)
let compile_implementation_flambda2 unix ?toplevel ?(keep_symbol_tables=true)
~filename ~prefixname ~size:module_block_size_in_words ~compilation_unit
~module_initializer ~flambda2 ~ppf_dump ~required_globals () =
compile_unit ~ppf_dump ~output_prefix:prefixname
~asm_filename:(asm_filename prefixname) ~keep_asm:!keep_asm_file
~obj_filename:(prefixname ^ ext_obj)
~may_reduce_heap:(Option.is_none toplevel)
(fun () ->
Compilation_unit.Set.iter Compilenv.require_global
required_globals;
let cmm_phrases =
flambda2 ~ppf_dump ~prefixname ~filename ~compilation_unit
~module_block_size_in_words ~module_initializer
~keep_symbol_tables
in
end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile:filename
(fun () -> cmm_phrases))
let linear_gen_implementation unix filename =
let open Linear_format in
let linear_unit_info, _ = restore filename in
let current_package = Compilation_unit.Prefix.from_clflags () in
let saved_package =
Compilation_unit.for_pack_prefix linear_unit_info.unit
in
if not (Compilation_unit.Prefix.equal current_package saved_package)
then raise(Error(Mismatched_for_pack saved_package));
let emit_item = function
| Data dl -> emit_data dl
| Func f -> emit_fundecl f
in
start_from_emit := true;
Emitaux.Dwarf_helpers.init ~disable_dwarf:false filename;
emit_begin_assembly unix;
Profile.record "Emit" (List.iter emit_item) linear_unit_info.items;
emit_end_assembly filename ()
let compile_implementation_linear unix output_prefix ~progname =
compile_unit ~may_reduce_heap:true ~output_prefix
~asm_filename:(asm_filename output_prefix) ~keep_asm:!keep_asm_file
~obj_filename:(output_prefix ^ ext_obj)
(fun () ->
linear_gen_implementation unix progname)
(* Error report *)
let report_error ppf = function
| Assembler_error file ->
fprintf ppf "Assembler error, input left in file %a"
Location.print_filename file
| Mismatched_for_pack saved ->
let msg prefix =
if Compilation_unit.Prefix.is_empty prefix
then "without -for-pack"
else "with -for-pack " ^ Compilation_unit.Prefix.to_string prefix
in
fprintf ppf
"This input file cannot be compiled %s: it was generated %s."
(msg (Compilation_unit.Prefix.from_clflags ()))
(msg saved)
| Asm_generation(fn, err) ->
fprintf ppf
"Error producing assembly code for %s: %a"
fn Emitaux.report_error err
let () =
Location.register_error_of_exn
(function
| Error err -> Some (Location.error_of_printer_file report_error err)
| _ -> None
)