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
|
(**************************************************************************)
(* Sail *)
(* *)
(* Copyright (c) 2013-2017 *)
(* Kathyrn Gray *)
(* Shaked Flur *)
(* Stephen Kell *)
(* Gabriel Kerneis *)
(* Robert Norton-Wright *)
(* Christopher Pulte *)
(* Peter Sewell *)
(* Alasdair Armstrong *)
(* Brian Campbell *)
(* Thomas Bauereiss *)
(* Anthony Fox *)
(* Jon French *)
(* Dominic Mulligan *)
(* Stephen Kell *)
(* Mark Wassell *)
(* *)
(* All rights reserved. *)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory as part of the Rigorous Engineering of Mainstream Systems *)
(* (REMS) project, funded by EPSRC grant EP/K008528/1. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions *)
(* are met: *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in *)
(* the documentation and/or other materials provided with the *)
(* distribution. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' *)
(* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *)
(* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *)
(* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR *)
(* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *)
(* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF *)
(* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *)
(* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, *)
(* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *)
(* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *)
(* SUCH DAMAGE. *)
(**************************************************************************)
let opt_new_parser = ref false
let opt_lem_sequential = ref false
let opt_lem_mwords = ref false
type out_type =
| Lem_ast_out
| Lem_out of string option
let get_lexbuf f =
let in_chan = open_in f in
let lexbuf = Lexing.from_channel in_chan in
lexbuf.Lexing.lex_curr_p <- { Lexing.pos_fname = f;
Lexing.pos_lnum = 1;
Lexing.pos_bol = 0;
Lexing.pos_cnum = 0; };
lexbuf, in_chan
let parse_file (f : string) : Parse_ast.defs =
if not !opt_new_parser
then
let scanbuf, in_chan = get_lexbuf f in
let type_names =
try
Pre_parser.file Pre_lexer.token scanbuf
with
| Parsing.Parse_error ->
let pos = Lexing.lexeme_start_p scanbuf in
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_syntax (pos, "pre")))
| Parse_ast.Parse_error_locn(l,m) ->
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_syntax_locn (l, m)))
| Lexer.LexError(s,p) ->
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_lex (p, s)))
in
close_in in_chan;
Lexer.custom_type_names := !Lexer.custom_type_names @ type_names
else ();
let lexbuf, in_chan = get_lexbuf f in
try
let ast =
if !opt_new_parser
then Parser2.file Lexer2.token lexbuf
else Parser.file Lexer.token lexbuf
in
close_in in_chan; ast
with
| Parser2.Error ->
let pos = Lexing.lexeme_start_p lexbuf in
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_syntax (pos, "no information")))
| Parsing.Parse_error ->
let pos = Lexing.lexeme_start_p lexbuf in
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_syntax (pos, "main")))
| Parse_ast.Parse_error_locn(l,m) ->
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_syntax_locn (l, m)))
| Lexer.LexError(s,p) ->
raise (Reporting_basic.Fatal_error (Reporting_basic.Err_lex (p, s)))
let convert_ast (order : Ast.order) (defs : Parse_ast.defs) : unit Ast.defs = Initial_check.process_ast order defs
let load_file_no_check order f = convert_ast order (parse_file f)
let load_file order env f =
let ast = convert_ast order (parse_file f) in
Type_check.check env ast
let opt_just_check = ref false
let opt_ddump_tc_ast = ref false
let opt_ddump_rewrite_ast = ref None
let opt_dno_cast = ref false
let check_ast (defs : unit Ast.defs) : Type_check.tannot Ast.defs * Type_check.Env.t =
let ienv = if !opt_dno_cast then Type_check.Env.no_casts Type_check.initial_env else Type_check.initial_env in
let ast, env = Type_check.check ienv defs in
let () = if !opt_ddump_tc_ast then Pretty_print_sail2.pp_defs stdout ast else () in
let () = if !opt_just_check then exit 0 else () in
(ast, env)
let opt_ddump_raw_mono_ast = ref false
let opt_dmono_analysis = ref 0
let opt_auto_mono = ref false
let monomorphise_ast locs type_env ast =
let ast = Monomorphise.monomorphise (!opt_lem_mwords) (!opt_auto_mono) (!opt_dmono_analysis)
locs type_env ast in
let () = if !opt_ddump_raw_mono_ast then Pretty_print.pp_defs stdout ast else () in
let ienv = Type_check.Env.no_casts Type_check.initial_env in
Type_check.check ienv ast
let open_output_with_check file_name =
let (temp_file_name, o) = Filename.open_temp_file "ll_temp" "" in
let o' = Format.formatter_of_out_channel o in
(o', (o, temp_file_name, file_name))
let open_output_with_check_unformatted file_name =
let (temp_file_name, o) = Filename.open_temp_file "ll_temp" "" in
(o, temp_file_name, file_name)
let always_replace_files = ref true
let close_output_with_check (o, temp_file_name, file_name) =
let _ = close_out o in
let do_replace = !always_replace_files || (not (Util.same_content_files temp_file_name file_name)) in
let _ = if (not do_replace) then Sys.remove temp_file_name
else Util.move_file temp_file_name file_name in
()
let generated_line f =
Printf.sprintf "Generated by Sail from %s." f
let output_lem filename libs defs =
let generated_line = generated_line filename in
let seq_suffix = if !opt_lem_sequential then "_sequential" else "" in
let types_module = (filename ^ "_embed_types" ^ seq_suffix) in
let monad_module = if !opt_lem_sequential then "State" else "Prompt" in
let operators_module = if !opt_lem_mwords then "Sail_operators_mwords" else "Sail_operators" in
let libs = List.map (fun lib -> lib ^ seq_suffix) libs in
let base_imports = [
"Pervasives_extra";
"Sail_impl_base";
"Sail_values";
operators_module;
monad_module
] in
let ((ot,_, _) as ext_ot) =
open_output_with_check_unformatted (filename ^ "_embed_types" ^ seq_suffix ^ ".lem") in
let ((o,_, _) as ext_o) =
open_output_with_check_unformatted (filename ^ "_embed" ^ seq_suffix ^ ".lem") in
(Pretty_print.pp_defs_lem !opt_lem_sequential !opt_lem_mwords
(ot, base_imports)
(o, base_imports @ (String.capitalize types_module :: libs))
defs generated_line);
close_output_with_check ext_ot;
close_output_with_check ext_o
let rec iterate (f : int -> unit) (n : int) : unit =
if n = 0 then ()
else (f n; iterate f (n - 1))
let output1 libpath out_arg filename defs =
let f' = Filename.basename (Filename.chop_extension filename) in
match out_arg with
| Lem_ast_out ->
begin
let (o, ext_o) = open_output_with_check (f' ^ ".lem") in
Format.fprintf o "(* %s *)@\n" (generated_line filename);
Format.fprintf o "open import Interp_ast@\n";
Format.fprintf o "open import Pervasives@\n";
Format.fprintf o "(*Supply common numeric constants at the right type to alleviate repeated calls to typeclass macro*)\n";
iterate (fun n -> Format.fprintf o "let int%i : integer = integerFromNat %i\n" (n - 1) (n - 1)) 129;
Format.fprintf o "let defs = ";
Pretty_print.pp_lem_defs o defs;
close_output_with_check ext_o
end
| Lem_out None ->
output_lem f' [] defs
| Lem_out (Some lib) ->
output_lem f' [lib] defs
let output libpath out_arg files =
List.iter
(fun (f, defs) ->
output1 libpath out_arg f defs)
files
let rewrite_step defs (name,rewriter) =
let defs = rewriter defs in
let _ = match !(opt_ddump_rewrite_ast) with
| Some (f, i) ->
begin
let filename = f ^ "_rewrite_" ^ string_of_int i ^ "_" ^ name ^ ".sail" in
(* output "" Lem_ast_out [filename, defs]; *)
let ((ot,_, _) as ext_ot) = open_output_with_check_unformatted filename in
Pretty_print_sail2.pp_defs ot defs;
close_output_with_check ext_ot;
opt_ddump_rewrite_ast := Some (f, i + 1)
end
| _ -> () in
defs
let rewrite rewriters defs =
try List.fold_left rewrite_step defs rewriters with
| Type_check.Type_error (l, err) ->
raise (Reporting_basic.err_typ l (Type_check.string_of_type_error err))
let rewrite_ast = rewrite [("initial", Rewriter.rewrite_defs)]
let rewrite_undefined = rewrite [("undefined", fun x -> Rewrites.rewrite_undefined !opt_lem_mwords x)]
let rewrite_ast_lem = rewrite Rewrites.rewrite_defs_lem
let rewrite_ast_ocaml = rewrite Rewrites.rewrite_defs_ocaml
let rewrite_ast_interpreter = rewrite Rewrites.rewrite_defs_interpreter
let rewrite_ast_sil = rewrite Rewrites.rewrite_defs_sil
let rewrite_ast_check = rewrite Rewrites.rewrite_defs_check
|