123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391(****************************************************************************)(* Sail *)(* *)(* Sail and the Sail architecture models here, comprising all files and *)(* directories except the ASL-derived Sail code in the aarch64 directory, *)(* are subject to the BSD two-clause licence below. *)(* *)(* The ASL derived parts of the ARMv8.3 specification in *)(* aarch64/no_vector and aarch64/full are copyright ARM Ltd. *)(* *)(* Copyright (c) 2013-2021 *)(* 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 *)(* Alastair Reid (Arm Ltd) *)(* *)(* All rights reserved. *)(* *)(* This work was partially supported by EPSRC grant EP/K008528/1 <a *)(* href="http://www.cl.cam.ac.uk/users/pes20/rems">REMS: Rigorous *)(* Engineering for Mainstream Systems</a>, an ARM iCASE award, EPSRC IAA *)(* KTF funding, and donations from Arm. This project has received *)(* funding from the European Research Council (ERC) under the European *)(* Union’s Horizon 2020 research and innovation programme (grant *)(* agreement No 789108, ELVER). *)(* *)(* This software was developed by SRI International and the University of *)(* Cambridge Computer Laboratory (Department of Computer Science and *)(* Technology) under DARPA/AFRL contracts FA8650-18-C-7809 ("CIFV") *)(* and FA8750-10-C-0237 ("CTSRD"). *)(* *)(* 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. *)(****************************************************************************)(**************************************************************************)(* Lem *)(* *)(* Dominic Mulligan, University of Cambridge *)(* Francesco Zappa Nardelli, INRIA Paris-Rocquencourt *)(* Gabriel Kerneis, University of Cambridge *)(* Kathy Gray, University of Cambridge *)(* Peter Boehm, University of Cambridge (while working on Lem) *)(* Peter Sewell, University of Cambridge *)(* Scott Owens, University of Kent *)(* Thomas Tuerk, University of Cambridge *)(* *)(* The Lem sources are copyright 2010-2013 *)(* by the UK authors above and Institut National de Recherche en *)(* Informatique et en Automatique (INRIA). *)(* *)(* All files except ocaml-lib/pmap.{ml,mli} and ocaml-libpset.{ml,mli} *)(* are distributed under the license below. The former are distributed *)(* under the LGPLv2, as in the LICENSE file. *)(* *)(* *)(* 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. *)(* 3. The names of the authors may not be used to endorse or promote *)(* products derived from this software without specific prior written *)(* permission. *)(* *)(* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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. *)(**************************************************************************)letopt_warnings=reftrueletopt_all_warnings=reffalseletopt_backtrace_length=ref10typepos_or_loc=LocofParse_ast.l|PosofLexing.positionletfix_endlinestr=ifstr.[String.lengthstr-1]='\n'thenString.substr0(String.lengthstr-1)elsestrletprint_err_internalhintp_lm1m2=letopenError_formatinprerr_endline(m1^":");beginmatchp_lwith|Locl->format_message(Location("",hint,l,Line(fix_endlinem2)))err_formatter|Posp->format_message(Location("",hint,Parse_ast.Range(p,p),Line(fix_endlinem2)))err_formatterendletloc_to_stringl=letopenError_formatinletb=Buffer.create160informat_message(Location("",None,l,Line""))(buffer_formatterb);Buffer.contentsbletrecsimp_loc=function|Parse_ast.Unknown->None|Parse_ast.Unique(_,l)->simp_locl|Parse_ast.Generatedl->simp_locl|Parse_ast.Hint(_,l1,l2)->beginmatchsimp_locl1withNone->simp_locl2|pos->posend|Parse_ast.Range(p1,p2)->Some(p1,p2)letloc_range_to_src(p1:Lexing.position)(p2:Lexing.position)=(funcontents->String.subcontentsp1.pos_cnum(p2.pos_cnum-p1.pos_cnum))(Util.read_whole_filep1.pos_fname)letrecmap_loc_rangef=function|Parse_ast.Unknown->Parse_ast.Unknown|Parse_ast.Unique(n,l)->Parse_ast.Unique(n,map_loc_rangefl)|Parse_ast.Generatedl->Parse_ast.Generated(map_loc_rangefl)|Parse_ast.Hint(hint,l1,l2)->Parse_ast.Hint(hint,l1,map_loc_rangefl2)|Parse_ast.Range(p1,p2)->letp1,p2=fp1p2inParse_ast.Range(p1,p2)letrecloc_file=function|Parse_ast.Unknown->None|Parse_ast.Unique(_,l)->loc_filel|Parse_ast.Generatedl->loc_filel|Parse_ast.Hint(_,_,l)->loc_filel|Parse_ast.Range(p1,_)->Somep1.pos_fnameletrecstart_loc=function|Parse_ast.Unknown->Parse_ast.Unknown|Parse_ast.Unique(u,l)->Parse_ast.Unique(u,start_locl)|Parse_ast.Generatedl->Parse_ast.Generated(start_locl)|Parse_ast.Hint(hint,l1,l2)->Parse_ast.Hint(hint,start_locl1,start_locl2)|Parse_ast.Range(p1,_)->Parse_ast.Range(p1,p1)letshort_loc_to_stringl=matchsimp_loclwith|None->"unknown location"|Some(p1,p2)->Printf.sprintf"%s:%d.%d-%d.%d"p1.pos_fnamep1.pos_lnum(p1.pos_cnum-p1.pos_bol)p2.pos_lnum(p2.pos_cnum-p2.pos_bol)letprint_errlm1m2=print_err_internalNone(Locl)m1m2typeerror=|Err_generalofParse_ast.l*string|Err_unreachableofParse_ast.l*(string*int*int*int)*Printexc.raw_backtrace*string|Err_todoofParse_ast.l*string|Err_syntaxofLexing.position*string|Err_syntax_locofParse_ast.l*string|Err_lexofLexing.position*string|Err_typeofParse_ast.l*stringoption*stringletissues="\nPlease report this as an issue on GitHub at https://github.com/rems-project/sail/issues"letdest_err?(interactive=false)=function|Err_general(l,m)->(Util.("Error"|>yellow|>clear),None,Locl,m)|Err_unreachable(l,(file,line,_,_),backtrace,m)->ifinteractivethen("Error",None,Locl,m)else(Printf.sprintf"Internal error: Unreachable code (at \"%s\" line %d)"fileline,None,Locl,m^"\n\n"^Printexc.raw_backtrace_to_stringbacktrace^issues)|Err_todo(l,m)->("Todo",None,Locl,m)|Err_syntax(p,m)->(Util.("Syntax error"|>yellow|>clear),None,Posp,m)|Err_syntax_loc(l,m)->(Util.("Syntax error"|>yellow|>clear),None,Locl,m)|Err_lex(p,s)->(Util.("Lexical error"|>yellow|>clear),None,Posp,s)|Err_type(l,hint,m)->(Util.("Type error"|>yellow|>clear),hint,Locl,m)exceptionFatal_erroroferror(* Abbreviations for the very common cases *)leterr_todolm=Fatal_error(Err_todo(l,m))leterr_unreachablelocaml_posm=letbacktrace=Printexc.get_callstack!opt_backtrace_lengthinFatal_error(Err_unreachable(l,ocaml_pos,backtrace,m))leterr_generallm=Fatal_error(Err_general(l,m))leterr_typ?hintlm=Fatal_error(Err_type(l,hint,m))leterr_syntaxpm=Fatal_error(Err_syntax(p,m))leterr_syntax_loclm=Fatal_error(Err_syntax_loc(l,m))leterr_lexpm=Fatal_error(Err_lex(p,m))letunreachablelposmsg=raise(err_unreachablelposmsg)letforbid_errorsocaml_posfx=tryfxwith|Fatal_error(Err_general(l,m))->raise(err_unreachablelocaml_posm)|Fatal_error(Err_todo(l,m))->raise(err_unreachablelocaml_posm)|Fatal_error(Err_syntax(p,m))->raise(err_unreachable(Range(p,p))ocaml_posm)|Fatal_error(Err_syntax_loc(l,m))->raise(err_unreachablelocaml_posm)|Fatal_error(Err_lex(p,m))->raise(err_unreachable(Range(p,p))ocaml_posm)|Fatal_error(Err_type(l,_,m))->raise(err_unreachablelocaml_posm)letprint_error?(interactive=false)e=letm1,hint,pos_l,m2=dest_err~interactiveeinprint_err_internalhintpos_lm1m2letprint_type_error?hintlmsg=print_err_internalhint(Locl)Util.("Type error"|>yellow|>clear)msg(* Warnings *)moduleStringSet=Set.Make(String)letpos_comparep1p2=letopenLexinginmatchString.comparep1.pos_fnamep2.pos_fnamewith|0->beginmatchcomparep1.pos_lnump2.pos_lnumwith|0->beginmatchcomparep1.pos_bolp2.pos_bolwith0->comparep1.pos_cnump2.pos_cnum|n->nend|n->nend|n->nmoduleRange=structtypet=Lexing.position*Lexing.positionletcompare(p1,p2)(p3,p4)=letc=pos_comparep1p3inifc=0thenpos_comparep2p4elsecendmoduleRangeMap=Map.Make(Range)letignored_files=refStringSet.emptyletsuppress_warnings_for_filef=ignored_files:=StringSet.addf!ignored_filesletseen_warnings=refRangeMap.emptyletonce_from_warnings=refStringSet.emptyletsuppressed_warnings=ref0letsuppressed_warning_info()=if!suppressed_warnings>0then(prerr_endline(Util.("Warning"|>yellow|>clear)^": "^string_of_int!suppressed_warnings^" warnings have been suppressed. Use --all-warnings to display them.");suppressed_warnings:=0)letwarn?once_fromshort_strlexplanation=letalready_shown=matchonce_fromwith|Some(file,lnum,cnum,enum)whennot!opt_all_warnings->letkey=Printf.sprintf"%d:%d:%d:%s"lnumcnumenumfileinifStringSet.memkey!once_from_warningsthen(incrsuppressed_warnings;true)else(once_from_warnings:=StringSet.addkey!once_from_warnings;false)|_->falseinif!opt_warnings&¬already_shownthen(matchsimp_loclwith|Some(p1,p2)whennot(StringSet.memp1.pos_fname!ignored_files)->letshorts=RangeMap.find_opt(p1,p2)!seen_warnings|>Option.value~default:[]inifnot(List.exists(funs->s=short_str)shorts)then(prerr_endline(Util.("Warning"|>yellow|>clear)^": "^short_str^(ifshort_str<>""then" "else"")^loc_to_stringl^explanation^"\n");seen_warnings:=RangeMap.add(p1,p2)(short_str::shorts)!seen_warnings)|_->prerr_endline(Util.("Warning"|>yellow|>clear)^": "^short_str^"\n"^explanation^"\n"))letformat_warn?once_fromshort_strlexplanation=letalready_shown=matchonce_fromwith|Some(file,lnum,cnum,enum)whennot!opt_all_warnings->letkey=Printf.sprintf"%d:%d:%d:%s"lnumcnumenumfileinifStringSet.memkey!once_from_warningsthen(incrsuppressed_warnings;true)else(once_from_warnings:=StringSet.addkey!once_from_warnings;false)|_->falseinif!opt_warnings&¬already_shownthen(matchsimp_loclwith|Some(p1,p2)whennot(StringSet.memp1.pos_fname!ignored_files)->letshorts=RangeMap.find_opt(p1,p2)!seen_warnings|>Option.value~default:[]inifnot(List.exists(funs->s=short_str)shorts)then(letopenError_formatinprerr_endline(Util.("Warning"|>yellow|>clear)^": "^short_str);format_message(Location("",None,l,explanation))err_formatter;seen_warnings:=RangeMap.add(p1,p2)(short_str::shorts)!seen_warnings)|_->prerr_endline(Util.("Warning"|>yellow|>clear)^": "^short_str^"\n"))letsimple_warnstr=warnstrParse_ast.Unknown""letget_sail_dirdefault_sail_dir=matchSys.getenv_opt"SAIL_DIR"with|Somepath->path|None->ifSys.file_existsdefault_sail_dirthendefault_sail_direlseraise(err_generalParse_ast.Unknown("Sail share directory "^default_sail_dir^" does not exist. Make sure Sail is installed correctly, or try setting the SAIL_DIR environment variable"))letsystem_checked?loc:(l=Parse_ast.Unknown)cmd=matchUnix.systemcmdwith|WEXITED0->()|WEXITEDn->raise(err_generall(Printf.sprintf"Command %s failed with exit code %d"cmdn))|WSTOPPEDn->raise(err_generall(Printf.sprintf"Command %s stopped by signal %d"cmdn))|WSIGNALEDn->raise(err_generall(Printf.sprintf"Command %s killed by signal %d"cmdn))modulePosition=structopenLexinglettrim_positionstrp=letlen=String.lengthstrinletrecgonp=ifn<len&&(str.[n]=' '||str.[n]='\t')thengo(n+1){pwithpos_cnum=p.pos_cnum+1}elseifn<len&&str.[n]='\n'thengo(n+1){pwithpos_lnum=p.pos_lnum+1;pos_bol=p.pos_cnum+1;pos_cnum=p.pos_cnum+1}elsepingo0pletstring_positionstrimafterstrp=letstr,p=iftrimthen(String.trimstr,trim_positionstrp)else(str,p)inletlnum=refp.pos_lnuminletbol=refp.pos_bolinletcnum=refp.pos_cnuminfori=0toString.lengthstr-1doifstr.[i]='\n'then(incrlnum;incrcnum;bol:=!cnum)elseincrcnumdone;(p,{pwithpos_lnum=!lnum;pos_bol=!bol;pos_cnum=!cnum+after})letadvance_position?(after=0)~trimstrp=snd(string_positionstrimafterstrp)letstring_location~trim~start:sstr=lets,e=string_positionstrim0strsinParse_ast.Range(s,e)end