-
Notifications
You must be signed in to change notification settings - Fork 114
Save cfg to file #257
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Save cfg to file #257
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4bffc08
Magic number for Cfg files
gretay-js a975d18
Cfg file format
gretay-js 5dd649c
Update dune for Cfg_format
gretay-js 701aa7f
Save Cfg before Emit
gretay-js 6a0ff72
Add Cfg to compiler passes in Clflags and driver
gretay-js cc41f63
Update testsuite
gretay-js 36357bc
Update magic number for cfg to use 2021 instead of 1999
gretay-js 2074329
Use Digest.input and Digest.output
gretay-js 8ecfc84
Rearrange dune file
gretay-js File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
(**************************************************************************) | ||
(* *) | ||
(* OCaml *) | ||
(* *) | ||
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) | ||
(* Greta Yorsh, Jane Street Europe *) | ||
(* *) | ||
(* Copyright 1996 Institut National de Recherche en Informatique et *) | ||
(* en Automatique. *) | ||
(* Copyright 2019 Jane Street Group LLC *) | ||
(* *) | ||
(* 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. *) | ||
(* *) | ||
(**************************************************************************) | ||
|
||
(* Marshal and unmarshal a compilation unit in Cfg format *) | ||
type cfg_item_info = | ||
| Cfg of Cfg_with_layout.t | ||
| Data of Cmm.data_item list | ||
|
||
type cfg_unit_info = | ||
{ | ||
mutable unit_name : string; | ||
mutable items : cfg_item_info list; | ||
mutable for_pack : string option | ||
} | ||
|
||
type error = | ||
| Wrong_format of string | ||
| Wrong_version of string | ||
| Corrupted of string | ||
| Marshal_failed of string | ||
|
||
exception Error of error | ||
|
||
let save filename cfg_unit_info = | ||
let ch = open_out_bin filename in | ||
Misc.try_finally (fun () -> | ||
output_string ch Config.cfg_magic_number; | ||
output_value ch cfg_unit_info; | ||
(* Saved because Emit depends on Cmm.label. *) | ||
output_value ch (Cmm.cur_label ()); | ||
(* Compute digest of the contents and append it to the file. *) | ||
flush ch; | ||
let crc = Digest.file filename in | ||
Digest.output ch crc | ||
) | ||
~always:(fun () -> close_out ch) | ||
~exceptionally:(fun () -> raise (Error (Marshal_failed filename))) | ||
|
||
let restore filename = | ||
let ic = open_in_bin filename in | ||
Misc.try_finally | ||
(fun () -> | ||
let magic = Config.cfg_magic_number in | ||
let buffer = really_input_string ic (String.length magic) in | ||
if String.equal buffer magic then begin | ||
try | ||
let cfg_unit_info = (input_value ic : cfg_unit_info) in | ||
let last_label = (input_value ic : Cmm.label) in | ||
Cmm.reset (); | ||
Cmm.set_label last_label; | ||
let crc = Digest.input ic in | ||
cfg_unit_info, crc | ||
with End_of_file | Failure _ -> raise (Error (Corrupted filename)) | ||
| Error e -> raise (Error e) | ||
end | ||
else if String.sub buffer 0 9 = String.sub magic 0 9 then | ||
raise (Error (Wrong_version filename)) | ||
else | ||
raise (Error (Wrong_format filename)) | ||
) | ||
~always:(fun () -> close_in ic) | ||
|
||
(* Error report *) | ||
|
||
open Format | ||
|
||
let report_error ppf = function | ||
| Wrong_format filename -> | ||
fprintf ppf "Expected Cfg format. Incompatible file %a" | ||
Location.print_filename filename | ||
| Wrong_version filename -> | ||
fprintf ppf | ||
"%a@ is not compatible with this version of OCaml" | ||
Location.print_filename filename | ||
| Corrupted filename -> | ||
fprintf ppf "Corrupted format@ %a" | ||
Location.print_filename filename | ||
| Marshal_failed filename -> | ||
fprintf ppf "Failed to marshal Cfg to file@ %a" | ||
Location.print_filename filename | ||
|
||
let () = | ||
Location.register_error_of_exn | ||
(function | ||
| Error err -> Some (Location.error_of_printer_file report_error err) | ||
| _ -> None | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(**************************************************************************) | ||
(* *) | ||
(* OCaml *) | ||
(* *) | ||
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) | ||
(* Greta Yorsh, Jane Street Europe *) | ||
(* *) | ||
(* Copyright 1996 Institut National de Recherche en Informatique et *) | ||
(* en Automatique. *) | ||
(* Copyright 2019 Jane Street Group LLC *) | ||
(* *) | ||
(* 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. *) | ||
(* *) | ||
(**************************************************************************) | ||
|
||
(* Format of .cmir-cfg files *) | ||
|
||
(* Compiler can optionally save Cfg representation of a compilation unit, | ||
along with other information required to emit assembly. *) | ||
type cfg_item_info = | ||
| Cfg of Cfg_with_layout.t | ||
| Data of Cmm.data_item list | ||
|
||
type cfg_unit_info = | ||
{ | ||
mutable unit_name : string; | ||
mutable items : cfg_item_info list; | ||
mutable for_pack : string option | ||
} | ||
|
||
(* Marshal and unmarshal a compilation unit in Cfg format. | ||
It includes saving and restoring global state required for Emit, | ||
that currently consists of Cmm.label_counter. | ||
*) | ||
val save : string -> cfg_unit_info -> unit | ||
val restore : string -> cfg_unit_info * Digest.t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ocaml/testsuite/tests/tool-ocamlopt-save-ir/save_ir_after_typing.compilers.reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
wrong argument 'typing'; option '-save-ir-after' expects one of: scheduling. | ||
wrong argument 'typing'; option '-save-ir-after' expects one of: scheduling simplify_cfg. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.