effect
stringclasses 48
values | original_source_type
stringlengths 0
23k
| opens_and_abbrevs
listlengths 2
92
| isa_cross_project_example
bool 1
class | source_definition
stringlengths 9
57.9k
| partial_definition
stringlengths 7
23.3k
| is_div
bool 2
classes | is_type
null | is_proof
bool 2
classes | completed_definiton
stringlengths 1
250k
| dependencies
dict | effect_flags
sequencelengths 0
2
| ideal_premises
sequencelengths 0
236
| mutual_with
sequencelengths 0
11
| file_context
stringlengths 0
407k
| interleaved
bool 1
class | is_simply_typed
bool 2
classes | file_name
stringlengths 5
48
| vconfig
dict | is_simple_lemma
null | source_type
stringlengths 10
23k
| proof_features
sequencelengths 0
1
| name
stringlengths 8
95
| source
dict | verbose_type
stringlengths 1
7.42k
| source_range
dict |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let prog = list decl & option type_refinement | let prog = | false | null | false | list decl & option type_refinement | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"Ast.decl",
"FStar.Pervasives.Native.option",
"Ast.type_refinement"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
} | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val prog : Type0 | [] | Ast.prog | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 45,
"end_line": 590,
"start_col": 11,
"start_line": 590
} |
|
Prims.Tot | val decl_with_v (d: decl) (v: decl') : decl | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } } | val decl_with_v (d: decl) (v: decl') : decl
let decl_with_v (d: decl) (v: decl') : decl = | false | null | false | { d with d_decl = { d.d_decl with v = v } } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.decl",
"Ast.decl'",
"Ast.Mkdecl",
"Ast.Mkwith_meta_t",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.with_meta_t",
"Ast.__proj__Mkdecl__item__d_decl",
"Ast.__proj__Mkdecl__item__d_exported"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported } | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val decl_with_v (d: decl) (v: decl') : decl | [] | Ast.decl_with_v | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | d: Ast.decl -> v: Ast.decl' -> Ast.decl | {
"end_col": 43,
"end_line": 580,
"start_col": 4,
"start_line": 580
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tuint32 = mk_prim_t "UINT32" | let tuint32 = | false | null | false | mk_prim_t "UINT32" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tuint32 : Ast.with_meta_t Ast.typ' | [] | Ast.tuint32 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 32,
"end_line": 779,
"start_col": 14,
"start_line": 779
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false | let is_entrypoint d = | false | null | false | match d.d_decl.v with
| Record names _ _ _ | CaseType names _ _ -> has_entrypoint (names.typedef_attributes)
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.decl",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.decl'",
"Ast.__proj__Mkdecl__item__d_decl",
"Ast.typedef_names",
"Prims.list",
"Ast.param",
"FStar.Pervasives.Native.option",
"Ast.expr",
"Ast.record",
"Ast.has_entrypoint",
"Ast.__proj__Mktypedef_names__item__typedef_attributes",
"Ast.switch_case",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val is_entrypoint : d: Ast.decl -> Prims.bool | [] | Ast.is_entrypoint | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | d: Ast.decl -> Prims.bool | {
"end_col": 14,
"end_line": 613,
"start_col": 22,
"start_line": 609
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let enum_case = ident & option (either int ident) | let enum_case = | false | null | false | ident & option (either int ident) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.tuple2",
"Ast.ident",
"FStar.Pervasives.Native.option",
"Ast.either",
"Prims.int"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
} | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val enum_case : Type0 | [] | Ast.enum_case | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 49,
"end_line": 520,
"start_col": 16,
"start_line": 520
} |
|
Prims.Tot | val has_entrypoint (l: list attribute) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l) | val has_entrypoint (l: list attribute) : Tot bool
let has_entrypoint (l: list attribute) : Tot bool = | false | null | false | Some? (List.Tot.tryFind (function
| Entrypoint -> true
| _ -> false)
l) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Prims.list",
"Ast.attribute",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.List.Tot.Base.tryFind",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *) | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val has_entrypoint (l: list attribute) : Tot bool | [] | Ast.has_entrypoint | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | l: Prims.list Ast.attribute -> Prims.bool | {
"end_col": 71,
"end_line": 599,
"start_col": 2,
"start_line": 599
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tbool = mk_prim_t "Bool" | let tbool = | false | null | false | mk_prim_t "Bool" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x} | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tbool : Ast.with_meta_t Ast.typ' | [] | Ast.tbool | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 28,
"end_line": 774,
"start_col": 12,
"start_line": 774
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tunit = mk_prim_t "unit" | let tunit = | false | null | false | mk_prim_t "unit" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec []) | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tunit : Ast.with_meta_t Ast.typ' | [] | Ast.tunit | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 28,
"end_line": 775,
"start_col": 12,
"start_line": 775
} |
|
Prims.Tot | val atomic_field_has_out_expr (f: atomic_field) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action | val atomic_field_has_out_expr (f: atomic_field) : Tot bool
let atomic_field_has_out_expr (f: atomic_field) : Tot bool = | false | null | false | let sf = f.v in
field_action_has_out_expr sf.field_action | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.atomic_field",
"Ast.field_action_has_out_expr",
"Ast.__proj__Mkatomic_field'__item__field_action",
"Ast.atomic_field'",
"Ast.__proj__Mkwith_meta_t__item__v",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val atomic_field_has_out_expr (f: atomic_field) : Tot bool | [] | Ast.atomic_field_has_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | f: Ast.atomic_field -> Prims.bool | {
"end_col": 43,
"end_line": 664,
"start_col": 60,
"start_line": 662
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tunknown = mk_prim_t "?" | let tunknown = | false | null | false | mk_prim_t "?" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tunknown : Ast.with_meta_t Ast.typ' | [] | Ast.tunknown | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 28,
"end_line": 781,
"start_col": 15,
"start_line": 781
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let to_ident' x = {modul_name=None;name=x} | let to_ident' x = | false | null | false | { modul_name = None; name = x } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Prims.string",
"Ast.Mkident'",
"FStar.Pervasives.Native.None",
"Ast.ident'"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val to_ident' : x: Prims.string -> Ast.ident' | [] | Ast.to_ident' | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: Prims.string -> Ast.ident' | {
"end_col": 41,
"end_line": 772,
"start_col": 19,
"start_line": 772
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tuint64 = mk_prim_t "UINT64" | let tuint64 = | false | null | false | mk_prim_t "UINT64" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tuint64 : Ast.with_meta_t Ast.typ' | [] | Ast.tuint64 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 32,
"end_line": 780,
"start_col": 14,
"start_line": 780
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col | let string_of_pos p = | false | null | false | Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.pos",
"FStar.Printf.sprintf",
"Ast.__proj__Mkpos__item__filename",
"Ast.__proj__Mkpos__item__line",
"Ast.__proj__Mkpos__item__col",
"Prims.string"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val string_of_pos : p: Ast.pos -> (Prims.string <: Type0) | [] | Ast.string_of_pos | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | p: Ast.pos -> (Prims.string <: Type0) | {
"end_col": 53,
"end_line": 77,
"start_col": 2,
"start_line": 77
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let comments = list string | let comments = | false | null | false | list string | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Prims.list",
"Prims.string"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val comments : Type0 | [] | Ast.comments | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 26,
"end_line": 83,
"start_col": 15,
"start_line": 83
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let range = pos * pos | let range = | false | null | false | pos * pos | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.tuple2",
"Ast.pos"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val range : Type0 | [] | Ast.range | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 21,
"end_line": 80,
"start_col": 12,
"start_line": 80
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let ident = with_meta_t ident' | let ident = | false | null | false | with_meta_t ident' | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.with_meta_t",
"Ast.ident'"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
} | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ident : Type0 | [] | Ast.ident | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 30,
"end_line": 127,
"start_col": 12,
"start_line": 127
} |
|
FStar.All.ML | val with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson" | val with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c
let with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c = | true | null | false | failwith "No reading yojson" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"FStar.All.failwith"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c | [] | Ast.with_meta_t_of_yojson | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | f: (_: a -> b) -> x: a -> FStar.All.ML c | {
"end_col": 32,
"end_line": 110,
"start_col": 4,
"start_line": 110
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q) | let string_of_range r = | false | null | false | let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)" p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s" (string_of_pos p) (string_of_pos q) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.tuple2",
"Ast.pos",
"Prims.op_Equality",
"Prims.string",
"Ast.__proj__Mkpos__item__filename",
"FStar.Printf.sprintf",
"Ast.__proj__Mkpos__item__line",
"Ast.__proj__Mkpos__item__col",
"Prims.bool",
"Ast.string_of_pos"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val string_of_range : r: (Ast.pos * Ast.pos) -> Prims.string | [] | Ast.string_of_range | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | r: (Ast.pos * Ast.pos) -> Prims.string | {
"end_col": 31,
"end_line": 92,
"start_col": 23,
"start_line": 85
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let field_bitwidth_t = either (with_meta_t int) bitfield_attr | let field_bitwidth_t = | false | null | false | either (with_meta_t int) bitfield_attr | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.either",
"Ast.with_meta_t",
"Prims.int",
"Ast.bitfield_attr"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr' | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val field_bitwidth_t : Type0 | [] | Ast.field_bitwidth_t | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 61,
"end_line": 454,
"start_col": 23,
"start_line": 454
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tuint8 = mk_prim_t "UINT8" | let tuint8 = | false | null | false | mk_prim_t "UINT8" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tuint8 : Ast.with_meta_t Ast.typ' | [] | Ast.tuint8 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 30,
"end_line": 776,
"start_col": 13,
"start_line": 776
} |
|
Prims.Tot | val atomic_action_has_out_expr (a: atomic_action) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false | val atomic_action_has_out_expr (a: atomic_action) : Tot bool
let atomic_action_has_out_expr (a: atomic_action) : Tot bool = | false | null | false | match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _ -> out_expr_is_out_type_expr write_to
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.atomic_action",
"Ast.expr",
"Ast.out_expr",
"Ast.out_expr_is_out_type_expr",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val atomic_action_has_out_expr (a: atomic_action) : Tot bool | [] | Ast.atomic_action_has_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | a: Ast.atomic_action -> Prims.bool | {
"end_col": 14,
"end_line": 631,
"start_col": 2,
"start_line": 627
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec []) | let mk_prim_t x = | false | null | false | with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec []) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Prims.string",
"Ast.with_dummy_range",
"Ast.typ'",
"Ast.Type_app",
"Ast.ident'",
"Ast.to_ident'",
"Ast.KindSpec",
"Prims.Nil",
"Ast.either",
"Ast.expr",
"Ast.out_expr",
"Ast.with_meta_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mk_prim_t : x: Prims.string -> Ast.with_meta_t Ast.typ' | [] | Ast.mk_prim_t | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: Prims.string -> Ast.with_meta_t Ast.typ' | {
"end_col": 90,
"end_line": 773,
"start_col": 18,
"start_line": 773
} |
|
Prims.Tot | val eq_typ_param (p1 p2: typ_param) : bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false | val eq_typ_param (p1 p2: typ_param) : bool
let eq_typ_param (p1 p2: typ_param) : bool = | false | null | false | match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.typ_param",
"FStar.Pervasives.Native.Mktuple2",
"Ast.either",
"Ast.expr",
"Ast.out_expr",
"Ast.eq_expr",
"Ast.eq_out_expr",
"FStar.Pervasives.Native.tuple2",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_typ_param (p1 p2: typ_param) : bool | [] | Ast.eq_typ_param | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | p1: Ast.typ_param -> p2: Ast.typ_param -> Prims.bool | {
"end_col": 14,
"end_line": 751,
"start_col": 2,
"start_line": 748
} |
Prims.Tot | val integer_type_leq (t1 t2: integer_type) : bool | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2 | val integer_type_leq (t1 t2: integer_type) : bool
let integer_type_leq (t1 t2: integer_type) : bool = | false | null | false | integer_type_lub t1 t2 = t2 | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.integer_type",
"Prims.op_Equality",
"Ast.integer_type_lub",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8 | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val integer_type_leq (t1 t2: integer_type) : bool | [] | Ast.integer_type_leq | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | t1: Ast.integer_type -> t2: Ast.integer_type -> Prims.bool | {
"end_col": 29,
"end_line": 193,
"start_col": 2,
"start_line": 193
} |
Prims.Tot | val out_expr_is_out_type_expr (lhs: out_expr) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true | val out_expr_is_out_type_expr (lhs: out_expr) : Tot bool
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool = | false | null | false | match lhs.out_expr_node.v with
| OE_star { out_expr_node = { v = OE_id i } } -> false
| _ -> true | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.out_expr",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.out_expr'",
"Ast.__proj__Mkout_expr__item__out_expr_node",
"Ast.ident",
"Ast.range",
"Ast.comments",
"FStar.Pervasives.Native.option",
"Ast.out_expr_meta_t",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val out_expr_is_out_type_expr (lhs: out_expr) : Tot bool | [] | Ast.out_expr_is_out_type_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | lhs: Ast.out_expr -> Prims.bool | {
"end_col": 13,
"end_line": 623,
"start_col": 2,
"start_line": 621
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let with_dummy_range x = with_range x dummy_range | let with_dummy_range x = | false | null | false | with_range x dummy_range | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.with_range",
"Ast.dummy_range",
"Ast.with_meta_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val with_dummy_range : x: _ -> Ast.with_meta_t _ | [] | Ast.with_dummy_range | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: _ -> Ast.with_meta_t _ | {
"end_col": 49,
"end_line": 771,
"start_col": 25,
"start_line": 771
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let dummy_range = dummy_pos, dummy_pos | let dummy_range = | false | null | false | dummy_pos, dummy_pos | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.Mktuple2",
"Ast.pos",
"Ast.dummy_pos"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val dummy_range : Ast.pos * Ast.pos | [] | Ast.dummy_range | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.pos * Ast.pos | {
"end_col": 38,
"end_line": 770,
"start_col": 18,
"start_line": 770
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let puint8 = mk_prim_t "PUINT8" | let puint8 = | false | null | false | mk_prim_t "PUINT8" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val puint8 : Ast.with_meta_t Ast.typ' | [] | Ast.puint8 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 31,
"end_line": 777,
"start_col": 13,
"start_line": 777
} |
|
FStar.All.ML | val subst_atomic_action (s: subst) (aa: atomic_action) : ML atomic_action | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa | val subst_atomic_action (s: subst) (aa: atomic_action) : ML atomic_action
let subst_atomic_action (s: subst) (aa: atomic_action) : ML atomic_action = | true | null | false | match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.atomic_action",
"Ast.expr",
"Ast.Action_return",
"Ast.subst_expr",
"Ast.out_expr",
"Ast.Action_assignment",
"Ast.ident",
"Prims.list",
"Ast.Action_call",
"FStar.List.map"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)} | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_atomic_action (s: subst) (aa: atomic_action) : ML atomic_action | [] | Ast.subst_atomic_action | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> aa: Ast.atomic_action -> FStar.All.ML Ast.atomic_action | {
"end_col": 11,
"end_line": 825,
"start_col": 2,
"start_line": 821
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let tuint16 = mk_prim_t "UINT16" | let tuint16 = | false | null | false | mk_prim_t "UINT16" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.mk_prim_t"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val tuint16 : Ast.with_meta_t Ast.typ' | [] | Ast.tuint16 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.with_meta_t Ast.typ' | {
"end_col": 32,
"end_line": 778,
"start_col": 14,
"start_line": 778
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng | let unit_atomic_field rng = | false | null | false | let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f =
{
field_dependence = false;
field_ident = dummy_identifier;
field_type = tunit;
field_array_opt = FieldScalar;
field_constraint = None;
field_bitwidth = None;
field_action = None
}
in
with_range f rng | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.range",
"Ast.with_range",
"Ast.atomic_field'",
"Ast.Mkatomic_field'",
"Ast.tunit",
"Ast.FieldScalar",
"FStar.Pervasives.Native.None",
"Ast.expr",
"Ast.field_bitwidth_t",
"FStar.Pervasives.Native.tuple2",
"Ast.action",
"Prims.bool",
"Ast.with_meta_t",
"Ast.ident'",
"Ast.to_ident'"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64" | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val unit_atomic_field : rng: Ast.range -> Ast.with_meta_t Ast.atomic_field' | [] | Ast.unit_atomic_field | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | rng: Ast.range -> Ast.with_meta_t Ast.atomic_field' | {
"end_col": 20,
"end_line": 793,
"start_col": 27,
"start_line": 782
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported | let is_entrypoint_or_export d = | false | null | false | match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ -> if has_entrypoint (names.typedef_attributes) then true else d.d_exported
| _ -> d.d_exported | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.decl",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.decl'",
"Ast.__proj__Mkdecl__item__d_decl",
"Ast.typedef_names",
"Prims.list",
"Ast.param",
"FStar.Pervasives.Native.option",
"Ast.expr",
"Ast.record",
"Ast.has_entrypoint",
"Ast.__proj__Mktypedef_names__item__typedef_attributes",
"Prims.bool",
"Ast.__proj__Mkdecl__item__d_exported",
"Ast.switch_case"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l) | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val is_entrypoint_or_export : d: Ast.decl -> Prims.bool | [] | Ast.is_entrypoint_or_export | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | d: Ast.decl -> Prims.bool | {
"end_col": 21,
"end_line": 607,
"start_col": 32,
"start_line": 601
} |
|
Prims.Pure | val maybe_bit_order_of (i: ident)
: Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y -> Some? y == Some? (maybe_as_integer_typ i))) | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None | val maybe_bit_order_of (i: ident)
: Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y -> Some? y == Some? (maybe_as_integer_typ i)))
let maybe_bit_order_of (i: ident)
: Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y -> Some? y == Some? (maybe_as_integer_typ i))) = | false | null | false | if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [] | [
"Ast.ident",
"Prims.op_disEquality",
"FStar.Pervasives.Native.option",
"Prims.string",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.ident'",
"FStar.Pervasives.Native.None",
"Ast.bitfield_bit_order",
"Prims.bool",
"Ast.__proj__Mkident'__item__name",
"FStar.Pervasives.Native.Some",
"Ast.LSBFirst",
"Ast.MSBFirst",
"Prims.l_True",
"Prims.eq2",
"FStar.Pervasives.Native.uu___is_Some",
"Ast.integer_type",
"Ast.maybe_as_integer_typ"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val maybe_bit_order_of (i: ident)
: Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y -> Some? y == Some? (maybe_as_integer_typ i))) | [] | Ast.maybe_bit_order_of | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.ident -> Prims.Pure (FStar.Pervasives.Native.option Ast.bitfield_bit_order) | {
"end_col": 15,
"end_line": 238,
"start_col": 2,
"start_line": 226
} |
Prims.Tot | val sequence_non_failing_actions (a0: action{Action_act? a0.v}) (a1: action{Action_act? a1.v})
: a: action{Action_act? a.v} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments | val sequence_non_failing_actions (a0: action{Action_act? a0.v}) (a1: action{Action_act? a1.v})
: a: action{Action_act? a.v}
let sequence_non_failing_actions (a0: action{Action_act? a0.v}) (a1: action{Action_act? a1.v})
: a: action{Action_act? a.v} = | false | null | false | let rec seq (a0: action) =
let w a = with_range_and_comments a a1.range (a0.comments @ a1.comments) in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a -> w (Action_seq a a1)
| Action_seq a0 tl -> w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k -> w (Action_let i a (seq k))
| Action_act a -> seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res) res.range res.comments | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.action",
"Prims.b2t",
"Ast.uu___is_Action_act",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.action'",
"Ast.with_range_and_comments",
"Ast.Action_act",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.with_meta_t",
"Ast.atomic_action",
"Ast.Action_seq",
"Ast.expr",
"FStar.Pervasives.Native.option",
"Ast.Action_ite",
"FStar.Pervasives.Native.Some",
"Ast.ident",
"Ast.Action_let",
"FStar.List.Tot.Base.op_At",
"Prims.string"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v}) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sequence_non_failing_actions (a0: action{Action_act? a0.v}) (a1: action{Action_act? a1.v})
: a: action{Action_act? a.v} | [] | Ast.sequence_non_failing_actions | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
a0: Ast.action{Action_act? (Mkwith_meta_t?.v a0)} ->
a1: Ast.action{Action_act? (Mkwith_meta_t?.v a1)}
-> a: Ast.action{Action_act? (Mkwith_meta_t?.v a)} | {
"end_col": 18,
"end_line": 429,
"start_col": 3,
"start_line": 397
} |
Prims.Tot | val subst_out_expr (s: subst) (o: out_expr) : out_expr | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_out_expr (s:subst) (o:out_expr) : out_expr = o | val subst_out_expr (s: subst) (o: out_expr) : out_expr
let subst_out_expr (s: subst) (o: out_expr) : out_expr = | false | null | false | o | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.subst",
"Ast.out_expr"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a) | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_out_expr (s: subst) (o: out_expr) : out_expr | [] | Ast.subst_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> o: Ast.out_expr -> Ast.out_expr | {
"end_col": 56,
"end_line": 839,
"start_col": 55,
"start_line": 839
} |
FStar.All.ML | val subst_atomic_field (s: subst) (f: atomic_field) : ML atomic_field | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec subst_field (s:subst) (ff:field) : ML field =
match ff.v with
| AtomicField f -> {ff with v = AtomicField (subst_atomic_field s f)}
| RecordField f i -> {ff with v = RecordField (subst_record s f) i}
| SwitchCaseField f i -> {ff with v = SwitchCaseField (subst_switch_case s f) i}
and subst_atomic_field (s:subst) (f:atomic_field) : ML atomic_field =
let sf = f.v in
let a =
match sf.field_action with
| None -> None
| Some (a, b) -> Some (subst_action s a, b)
in
let sf = {
sf with
field_type = subst_typ s sf.field_type;
field_array_opt = subst_field_array s sf.field_array_opt;
field_constraint = map_opt (subst_expr s) sf.field_constraint;
field_action = a
} in
{ f with v = sf }
and subst_record (s:subst) (f:record) : ML record
= List.map (subst_field s) f
and subst_case (s:subst) (c:case) : ML case =
match c with
| Case e f -> Case (subst_expr s e) (subst_field s f)
| DefaultCase f -> DefaultCase (subst_field s f)
and subst_switch_case (s:subst) (sc:switch_case) : ML switch_case =
subst_expr s (fst sc), List.map (subst_case s) (snd sc) | val subst_atomic_field (s: subst) (f: atomic_field) : ML atomic_field
let rec subst_atomic_field (s: subst) (f: atomic_field) : ML atomic_field = | true | null | false | let sf = f.v in
let a =
match sf.field_action with
| None -> None
| Some (a, b) -> Some (subst_action s a, b)
in
let sf =
{
sf with
field_type = subst_typ s sf.field_type;
field_array_opt = subst_field_array s sf.field_array_opt;
field_constraint = map_opt (subst_expr s) sf.field_constraint;
field_action = a
}
in
{ f with v = sf } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.atomic_field",
"Ast.Mkwith_meta_t",
"Ast.atomic_field'",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.Mkatomic_field'",
"Ast.__proj__Mkatomic_field'__item__field_dependence",
"Ast.__proj__Mkatomic_field'__item__field_ident",
"Ast.__proj__Mkatomic_field'__item__field_bitwidth",
"FStar.Pervasives.Native.option",
"Ast.expr",
"Ast.map_opt",
"Ast.subst_expr",
"Ast.__proj__Mkatomic_field'__item__field_constraint",
"Ast.field_array_t",
"Ast.subst_field_array",
"Ast.__proj__Mkatomic_field'__item__field_array_opt",
"Ast.typ",
"Ast.subst_typ",
"Ast.__proj__Mkatomic_field'__item__field_type",
"FStar.Pervasives.Native.tuple2",
"Ast.action",
"Prims.bool",
"Ast.__proj__Mkatomic_field'__item__field_action",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"Ast.subst_action",
"Ast.__proj__Mkwith_meta_t__item__v"
] | [
"subst_field",
"subst_atomic_field",
"subst_record",
"subst_case",
"subst_switch_case"
] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe)
let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) }
let subst_field_array (s:subst) (f:field_array_t) : ML field_array_t =
match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz)
let rec subst_field (s:subst) (ff:field) : ML field =
match ff.v with
| AtomicField f -> {ff with v = AtomicField (subst_atomic_field s f)}
| RecordField f i -> {ff with v = RecordField (subst_record s f) i} | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_atomic_field (s: subst) (f: atomic_field) : ML atomic_field | [
"mutual recursion"
] | Ast.subst_atomic_field | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> f: Ast.atomic_field -> FStar.All.ML Ast.atomic_field | {
"end_col": 19,
"end_line": 872,
"start_col": 69,
"start_line": 858
} |
FStar.All.ML | val subst_typ_param (s: subst) (p: typ_param) : ML typ_param | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe) | val subst_typ_param (s: subst) (p: typ_param) : ML typ_param
let subst_typ_param (s: subst) (p: typ_param) : ML typ_param = | true | null | false | match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.typ_param",
"Ast.expr",
"Ast.Inl",
"Ast.out_expr",
"Ast.subst_expr",
"Ast.Inr",
"Ast.subst_out_expr"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_typ_param (s: subst) (p: typ_param) : ML typ_param | [] | Ast.subst_typ_param | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> p: Ast.typ_param -> FStar.All.ML Ast.typ_param | {
"end_col": 39,
"end_line": 843,
"start_col": 2,
"start_line": 841
} |
Prims.Tot | val field_action_has_out_expr (f: option (action & bool)) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a | val field_action_has_out_expr (f: option (action & bool)) : Tot bool
let field_action_has_out_expr (f: option (action & bool)) : Tot bool = | false | null | false | match f with
| None -> false
| Some (a, _) -> action_has_out_expr a | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"Ast.action",
"Prims.bool",
"Ast.action_has_out_expr"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool)) | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val field_action_has_out_expr (f: option (action & bool)) : Tot bool | [] | Ast.field_action_has_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | f: FStar.Pervasives.Native.option (Ast.action * Prims.bool) -> Prims.bool | {
"end_col": 40,
"end_line": 659,
"start_col": 2,
"start_line": 657
} |
Prims.Tot | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let print_constant (c:constant) =
let print_tag = function
| UInt8 -> "uy"
| UInt16 -> "us"
| UInt32 -> "ul"
| UInt64 -> "uL"
in
match c with
| Unit -> "()"
| Int tag i -> Printf.sprintf "%d%s" i (print_tag tag)
| XInt tag x ->
let tag = print_tag tag in
if String.length x >= 2
&& String.sub x (String.length x - 2) 2 = tag
then x
else Printf.sprintf "%s%s" x tag
| Bool b -> Printf.sprintf "%b" b | let print_constant (c: constant) = | false | null | false | let print_tag =
function
| UInt8 -> "uy"
| UInt16 -> "us"
| UInt32 -> "ul"
| UInt64 -> "uL"
in
match c with
| Unit -> "()"
| Int tag i -> Printf.sprintf "%d%s" i (print_tag tag)
| XInt tag x ->
let tag = print_tag tag in
if String.length x >= 2 && String.sub x (String.length x - 2) 2 = tag
then x
else Printf.sprintf "%s%s" x tag
| Bool b -> Printf.sprintf "%b" b | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.constant",
"Ast.integer_type",
"Prims.int",
"FStar.Printf.sprintf",
"Prims.string",
"Prims.op_AmpAmp",
"Prims.op_GreaterThanOrEqual",
"FStar.String.length",
"Prims.op_Equality",
"FStar.String.sub",
"Prims.op_Subtraction",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe)
let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) }
let subst_field_array (s:subst) (f:field_array_t) : ML field_array_t =
match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz)
let rec subst_field (s:subst) (ff:field) : ML field =
match ff.v with
| AtomicField f -> {ff with v = AtomicField (subst_atomic_field s f)}
| RecordField f i -> {ff with v = RecordField (subst_record s f) i}
| SwitchCaseField f i -> {ff with v = SwitchCaseField (subst_switch_case s f) i}
and subst_atomic_field (s:subst) (f:atomic_field) : ML atomic_field =
let sf = f.v in
let a =
match sf.field_action with
| None -> None
| Some (a, b) -> Some (subst_action s a, b)
in
let sf = {
sf with
field_type = subst_typ s sf.field_type;
field_array_opt = subst_field_array s sf.field_array_opt;
field_constraint = map_opt (subst_expr s) sf.field_constraint;
field_action = a
} in
{ f with v = sf }
and subst_record (s:subst) (f:record) : ML record
= List.map (subst_field s) f
and subst_case (s:subst) (c:case) : ML case =
match c with
| Case e f -> Case (subst_expr s e) (subst_field s f)
| DefaultCase f -> DefaultCase (subst_field s f)
and subst_switch_case (s:subst) (sc:switch_case) : ML switch_case =
subst_expr s (fst sc), List.map (subst_case s) (snd sc)
let subst_params (s:subst) (p:list param) : ML (list param) =
List.map (fun (t, i, q) -> subst_typ s t, i, q) p
let subst_decl' (s:subst) (d:decl') : ML decl' =
match d with
| ModuleAbbrev _ _ -> d
| Define i None _ -> d
| Define i (Some t) c -> Define i (Some (subst_typ s t)) c
| TypeAbbrev t i -> TypeAbbrev (subst_typ s t) i
| Enum t i is -> Enum (subst_typ s t) i is
| Record names params where fields ->
Record names (subst_params s params) (map_opt (subst_expr s) where) (List.map (subst_field s) fields)
| CaseType names params cases ->
CaseType names (subst_params s params) (subst_switch_case s cases)
| OutputType _
| ExternType _
| ExternFn _ _ _ -> d
let subst_decl (s:subst) (d:decl) : ML decl = decl_with_v d (subst_decl' s d.d_decl.v) | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val print_constant : c: Ast.constant -> Prims.string | [] | Ast.print_constant | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | c: Ast.constant -> Prims.string | {
"end_col": 35,
"end_line": 917,
"start_col": 33,
"start_line": 900
} |
|
FStar.All.ML | val subst_params (s: subst) (p: list param) : ML (list param) | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_params (s:subst) (p:list param) : ML (list param) =
List.map (fun (t, i, q) -> subst_typ s t, i, q) p | val subst_params (s: subst) (p: list param) : ML (list param)
let subst_params (s: subst) (p: list param) : ML (list param) = | true | null | false | List.map (fun (t, i, q) -> subst_typ s t, i, q) p | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Prims.list",
"Ast.param",
"FStar.List.map",
"FStar.Pervasives.Native.tuple3",
"Ast.typ",
"Ast.ident",
"Ast.qualifier",
"FStar.Pervasives.Native.Mktuple3",
"Ast.subst_typ"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe)
let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) }
let subst_field_array (s:subst) (f:field_array_t) : ML field_array_t =
match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz)
let rec subst_field (s:subst) (ff:field) : ML field =
match ff.v with
| AtomicField f -> {ff with v = AtomicField (subst_atomic_field s f)}
| RecordField f i -> {ff with v = RecordField (subst_record s f) i}
| SwitchCaseField f i -> {ff with v = SwitchCaseField (subst_switch_case s f) i}
and subst_atomic_field (s:subst) (f:atomic_field) : ML atomic_field =
let sf = f.v in
let a =
match sf.field_action with
| None -> None
| Some (a, b) -> Some (subst_action s a, b)
in
let sf = {
sf with
field_type = subst_typ s sf.field_type;
field_array_opt = subst_field_array s sf.field_array_opt;
field_constraint = map_opt (subst_expr s) sf.field_constraint;
field_action = a
} in
{ f with v = sf }
and subst_record (s:subst) (f:record) : ML record
= List.map (subst_field s) f
and subst_case (s:subst) (c:case) : ML case =
match c with
| Case e f -> Case (subst_expr s e) (subst_field s f)
| DefaultCase f -> DefaultCase (subst_field s f)
and subst_switch_case (s:subst) (sc:switch_case) : ML switch_case =
subst_expr s (fst sc), List.map (subst_case s) (snd sc) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_params (s: subst) (p: list param) : ML (list param) | [] | Ast.subst_params | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> p: Prims.list Ast.param -> FStar.All.ML (Prims.list Ast.param) | {
"end_col": 51,
"end_line": 882,
"start_col": 2,
"start_line": 882
} |
FStar.All.ML | val subst_decl (s: subst) (d: decl) : ML decl | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_decl (s:subst) (d:decl) : ML decl = decl_with_v d (subst_decl' s d.d_decl.v) | val subst_decl (s: subst) (d: decl) : ML decl
let subst_decl (s: subst) (d: decl) : ML decl = | true | null | false | decl_with_v d (subst_decl' s d.d_decl.v) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.decl",
"Ast.decl_with_v",
"Ast.decl'",
"Ast.subst_decl'",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.__proj__Mkdecl__item__d_decl"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe)
let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) }
let subst_field_array (s:subst) (f:field_array_t) : ML field_array_t =
match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz)
let rec subst_field (s:subst) (ff:field) : ML field =
match ff.v with
| AtomicField f -> {ff with v = AtomicField (subst_atomic_field s f)}
| RecordField f i -> {ff with v = RecordField (subst_record s f) i}
| SwitchCaseField f i -> {ff with v = SwitchCaseField (subst_switch_case s f) i}
and subst_atomic_field (s:subst) (f:atomic_field) : ML atomic_field =
let sf = f.v in
let a =
match sf.field_action with
| None -> None
| Some (a, b) -> Some (subst_action s a, b)
in
let sf = {
sf with
field_type = subst_typ s sf.field_type;
field_array_opt = subst_field_array s sf.field_array_opt;
field_constraint = map_opt (subst_expr s) sf.field_constraint;
field_action = a
} in
{ f with v = sf }
and subst_record (s:subst) (f:record) : ML record
= List.map (subst_field s) f
and subst_case (s:subst) (c:case) : ML case =
match c with
| Case e f -> Case (subst_expr s e) (subst_field s f)
| DefaultCase f -> DefaultCase (subst_field s f)
and subst_switch_case (s:subst) (sc:switch_case) : ML switch_case =
subst_expr s (fst sc), List.map (subst_case s) (snd sc)
let subst_params (s:subst) (p:list param) : ML (list param) =
List.map (fun (t, i, q) -> subst_typ s t, i, q) p
let subst_decl' (s:subst) (d:decl') : ML decl' =
match d with
| ModuleAbbrev _ _ -> d
| Define i None _ -> d
| Define i (Some t) c -> Define i (Some (subst_typ s t)) c
| TypeAbbrev t i -> TypeAbbrev (subst_typ s t) i
| Enum t i is -> Enum (subst_typ s t) i is
| Record names params where fields ->
Record names (subst_params s params) (map_opt (subst_expr s) where) (List.map (subst_field s) fields)
| CaseType names params cases ->
CaseType names (subst_params s params) (subst_switch_case s cases)
| OutputType _
| ExternType _ | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_decl (s: subst) (d: decl) : ML decl | [] | Ast.subst_decl | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> d: Ast.decl -> FStar.All.ML Ast.decl | {
"end_col": 86,
"end_line": 897,
"start_col": 46,
"start_line": 897
} |
FStar.All.ML | val apply (s: subst) (id: ident) : ML expr | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e | val apply (s: subst) (id: ident) : ML expr
let apply (s: subst) (id: ident) : ML expr = | true | null | false | match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.ident",
"Ast.with_range",
"Ast.expr'",
"Ast.Identifier",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'",
"Ast.expr",
"FStar.Pervasives.Native.option",
"Hashtable.try_find",
"Ast.__proj__Mkwith_meta_t__item__v"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val apply (s: subst) (id: ident) : ML expr | [] | Ast.apply | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> id: Ast.ident -> FStar.All.ML Ast.expr | {
"end_col": 15,
"end_line": 812,
"start_col": 2,
"start_line": 810
} |
FStar.All.ML | val map_opt (f: ('a -> ML 'b)) (o: option 'a) : ML (option 'b) | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x) | val map_opt (f: ('a -> ML 'b)) (o: option 'a) : ML (option 'b)
let map_opt (f: ('a -> ML 'b)) (o: option 'a) : ML (option 'b) = | true | null | false | match o with
| None -> None
| Some x -> Some (f x) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.Some"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val map_opt (f: ('a -> ML 'b)) (o: option 'a) : ML (option 'b) | [] | Ast.map_opt | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | f: (_: 'a -> FStar.All.ML 'b) -> o: FStar.Pervasives.Native.option 'a
-> FStar.All.ML (FStar.Pervasives.Native.option 'b) | {
"end_col": 24,
"end_line": 798,
"start_col": 2,
"start_line": 796
} |
Prims.Tot | val eq_typ (t1 t2: typ) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false | val eq_typ (t1 t2: typ) : Tot bool
let rec eq_typ (t1 t2: typ) : Tot bool = | false | null | false | match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 -> eq_idents hd1 hd2 && k1 = k2 && eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 -> eq_typ t1 t2
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.typ",
"FStar.Pervasives.Native.Mktuple2",
"Ast.typ'",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.ident",
"Ast.t_kind",
"Prims.list",
"Ast.either",
"Ast.expr",
"Ast.out_expr",
"Prims.op_AmpAmp",
"Ast.eq_idents",
"Prims.op_Equality",
"Ast.eq_typ_params",
"Ast.with_meta_t",
"Ast.eq_typ",
"FStar.Pervasives.Native.tuple2",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_typ (t1 t2: typ) : Tot bool | [
"recursion"
] | Ast.eq_typ | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | t1: Ast.typ -> t2: Ast.typ -> Prims.bool | {
"end_col": 14,
"end_line": 767,
"start_col": 2,
"start_line": 760
} |
FStar.All.ML | val subst_expr (s: subst) (e: expr) : ML expr | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)} | val subst_expr (s: subst) (e: expr) : ML expr
let rec subst_expr (s: subst) (e: expr) : ML expr = | true | null | false | match e.v with
| Constant _ | This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> { e with v = App op (List.map (subst_expr s) es) } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.expr",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.expr'",
"Ast.constant",
"Ast.ident",
"Ast.apply",
"Ast.with_meta_t",
"Ast.Mkwith_meta_t",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.Static",
"Ast.subst_expr",
"Ast.op",
"Prims.list",
"Ast.App",
"FStar.List.map"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_expr (s: subst) (e: expr) : ML expr | [
"recursion"
] | Ast.subst_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> e: Ast.expr -> FStar.All.ML Ast.expr | {
"end_col": 65,
"end_line": 819,
"start_col": 2,
"start_line": 814
} |
FStar.All.ML | val subst_decl' (s: subst) (d: decl') : ML decl' | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_decl' (s:subst) (d:decl') : ML decl' =
match d with
| ModuleAbbrev _ _ -> d
| Define i None _ -> d
| Define i (Some t) c -> Define i (Some (subst_typ s t)) c
| TypeAbbrev t i -> TypeAbbrev (subst_typ s t) i
| Enum t i is -> Enum (subst_typ s t) i is
| Record names params where fields ->
Record names (subst_params s params) (map_opt (subst_expr s) where) (List.map (subst_field s) fields)
| CaseType names params cases ->
CaseType names (subst_params s params) (subst_switch_case s cases)
| OutputType _
| ExternType _
| ExternFn _ _ _ -> d | val subst_decl' (s: subst) (d: decl') : ML decl'
let subst_decl' (s: subst) (d: decl') : ML decl' = | true | null | false | match d with
| ModuleAbbrev _ _ -> d
| Define i None _ -> d
| Define i (Some t) c -> Define i (Some (subst_typ s t)) c
| TypeAbbrev t i -> TypeAbbrev (subst_typ s t) i
| Enum t i is -> Enum (subst_typ s t) i is
| Record names params where fields ->
Record names
(subst_params s params)
(map_opt (subst_expr s) where)
(List.map (subst_field s) fields)
| CaseType names params cases -> CaseType names (subst_params s params) (subst_switch_case s cases)
| OutputType _ | ExternType _ | ExternFn _ _ _ -> d | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.decl'",
"Ast.ident",
"Ast.constant",
"Ast.typ",
"Ast.Define",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.Some",
"Ast.subst_typ",
"Ast.TypeAbbrev",
"Prims.list",
"Ast.enum_case",
"Ast.Enum",
"Ast.typedef_names",
"Ast.param",
"Ast.expr",
"Ast.record",
"Ast.Record",
"FStar.List.map",
"Ast.with_meta_t",
"Ast.field'",
"Ast.subst_field",
"Ast.map_opt",
"Ast.subst_expr",
"Ast.subst_params",
"Ast.switch_case",
"Ast.CaseType",
"Ast.subst_switch_case",
"Ast.out_typ"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe)
let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) }
let subst_field_array (s:subst) (f:field_array_t) : ML field_array_t =
match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz)
let rec subst_field (s:subst) (ff:field) : ML field =
match ff.v with
| AtomicField f -> {ff with v = AtomicField (subst_atomic_field s f)}
| RecordField f i -> {ff with v = RecordField (subst_record s f) i}
| SwitchCaseField f i -> {ff with v = SwitchCaseField (subst_switch_case s f) i}
and subst_atomic_field (s:subst) (f:atomic_field) : ML atomic_field =
let sf = f.v in
let a =
match sf.field_action with
| None -> None
| Some (a, b) -> Some (subst_action s a, b)
in
let sf = {
sf with
field_type = subst_typ s sf.field_type;
field_array_opt = subst_field_array s sf.field_array_opt;
field_constraint = map_opt (subst_expr s) sf.field_constraint;
field_action = a
} in
{ f with v = sf }
and subst_record (s:subst) (f:record) : ML record
= List.map (subst_field s) f
and subst_case (s:subst) (c:case) : ML case =
match c with
| Case e f -> Case (subst_expr s e) (subst_field s f)
| DefaultCase f -> DefaultCase (subst_field s f)
and subst_switch_case (s:subst) (sc:switch_case) : ML switch_case =
subst_expr s (fst sc), List.map (subst_case s) (snd sc)
let subst_params (s:subst) (p:list param) : ML (list param) =
List.map (fun (t, i, q) -> subst_typ s t, i, q) p | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_decl' (s: subst) (d: decl') : ML decl' | [] | Ast.subst_decl' | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> d: Ast.decl' -> FStar.All.ML Ast.decl' | {
"end_col": 23,
"end_line": 896,
"start_col": 2,
"start_line": 884
} |
FStar.All.ML | val mk_subst (s: list (ident * expr)) : ML subst | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h | val mk_subst (s: list (ident * expr)) : ML subst
let mk_subst (s: list (ident * expr)) : ML subst = | true | null | false | let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Ast.ident",
"Ast.expr",
"Ast.subst",
"Prims.unit",
"FStar.List.iter",
"Ast.with_meta_t",
"Ast.ident'",
"Hashtable.insert",
"Ast.__proj__Mkwith_meta_t__item__v",
"Hashtable.t",
"Hashtable.create"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mk_subst (s: list (ident * expr)) : ML subst | [] | Ast.mk_subst | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Prims.list (Ast.ident * Ast.expr) -> FStar.All.ML Ast.subst | {
"end_col": 3,
"end_line": 808,
"start_col": 49,
"start_line": 805
} |
FStar.All.ALL | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range | let check_reserved_identifier (i: ident) = | true | null | false | let open FStar.String in
let s = i.v.name in
if length s >= 3 && sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [] | [
"Ast.ident",
"Prims.op_AmpAmp",
"Prims.op_GreaterThanOrEqual",
"FStar.String.length",
"Prims.op_Equality",
"Prims.string",
"FStar.String.sub",
"Ast.reserved_prefix",
"Ast.error",
"Prims.unit",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'",
"Prims.bool",
"Ast.__proj__Mkident'__item__name",
"Ast.__proj__Mkwith_meta_t__item__v"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val check_reserved_identifier : i: Ast.ident -> FStar.All.ALL Prims.unit | [] | Ast.check_reserved_identifier | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.ident -> FStar.All.ALL Prims.unit | {
"end_col": 60,
"end_line": 150,
"start_col": 2,
"start_line": 146
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name | let ident_to_string i = | false | null | false | Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.with_meta_t",
"Ast.ident'",
"FStar.Printf.sprintf",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Prims.string",
"Prims.op_Hat",
"Ast.__proj__Mkident'__item__name"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident' | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ident_to_string : i: Ast.with_meta_t Ast.ident' -> (Prims.string <: Type0) | [] | Ast.ident_to_string | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.with_meta_t Ast.ident' -> (Prims.string <: Type0) | {
"end_col": 10,
"end_line": 133,
"start_col": 24,
"start_line": 129
} |
|
Prims.Tot | val integer_type_lub (t1 t2: integer_type) : Tot integer_type | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8 | val integer_type_lub (t1 t2: integer_type) : Tot integer_type
let integer_type_lub (t1 t2: integer_type) : Tot integer_type = | false | null | false | match t1, t2 with
| UInt64, _ | _, UInt64 -> UInt64
| _, UInt32 | UInt32, _ -> UInt32
| _, UInt16 | UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8 | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.integer_type",
"FStar.Pervasives.Native.Mktuple2",
"Ast.UInt64",
"Ast.UInt32",
"Ast.UInt16",
"Ast.UInt8"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val integer_type_lub (t1 t2: integer_type) : Tot integer_type | [] | Ast.integer_type_lub | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | t1: Ast.integer_type -> t2: Ast.integer_type -> Ast.integer_type | {
"end_col": 25,
"end_line": 190,
"start_col": 2,
"start_line": 183
} |
Prims.Tot | val comments_buffer:comments_buffer_t | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
} | val comments_buffer:comments_buffer_t
let comments_buffer:comments_buffer_t = | false | null | false | let buffer:ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string (Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line)
cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{ push = buffer_comment; flush = flush_comments; flush_until = flush_until } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.Mkcomments_buffer_t",
"Ast.pos",
"Prims.list",
"Prims.string",
"FStar.Pervasives.Native.tuple3",
"FStar.All.op_Bar_Greater",
"FStar.List.map",
"Prims.unit",
"FStar.ST.op_Colon_Equals",
"FStar.Heap.trivial_preorder",
"FStar.Pervasives.Native.tuple2",
"FStar.List.partition",
"Prims.op_LessThanOrEqual",
"Ast.__proj__Mkpos__item__line",
"Prims.bool",
"Options.debug_print_string",
"FStar.Printf.sprintf",
"FStar.ST.op_Bang",
"FStar.Pervasives.all_post_h",
"FStar.Monotonic.Heap.heap",
"Prims.l_and",
"FStar.Monotonic.Heap.sel",
"Prims.Nil",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.Monotonic.Heap.contains",
"FStar.Monotonic.Heap.modifies",
"FStar.Set.singleton",
"Prims.nat",
"FStar.Monotonic.Heap.addr_of",
"FStar.Monotonic.Heap.equal_dom",
"Prims.eq2",
"FStar.Pervasives.result",
"FStar.List.Tot.Base.rev",
"Prims.l_True",
"FStar.Pervasives.V",
"Prims.exn",
"FStar.Pervasives.E",
"FStar.Pervasives.Err",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple3",
"FStar.String.substring",
"Prims.op_Subtraction",
"FStar.String.length",
"Ast.comments_buffer_t",
"FStar.ST.ref",
"FStar.ST.alloc",
"FStar.ST.mref"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
} | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val comments_buffer:comments_buffer_t | [] | Ast.comments_buffer | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.comments_buffer_t | {
"end_col": 3,
"end_line": 73,
"start_col": 41,
"start_line": 44
} |
Prims.Tot | val parse_int_suffix (i: string) : string * option integer_type | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None | val parse_int_suffix (i: string) : string * option integer_type
let parse_int_suffix (i: string) : string * option integer_type = | false | null | false | let l = String.length i in
if l >= 2
then
let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Prims.string",
"Prims.op_GreaterThanOrEqual",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Pervasives.Native.option",
"Ast.integer_type",
"FStar.Pervasives.Native.Some",
"Ast.UInt8",
"Ast.UInt16",
"Ast.UInt32",
"Ast.UInt64",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.tuple2",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.String.strlen",
"Prims.op_Subtraction",
"FStar.String.sub",
"Prims.bool",
"FStar.String.length"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64 | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val parse_int_suffix (i: string) : string * option integer_type | [] | Ast.parse_int_suffix | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Prims.string -> Prims.string * FStar.Pervasives.Native.option Ast.integer_type | {
"end_col": 16,
"end_line": 170,
"start_col": 64,
"start_line": 159
} |
Prims.Tot | val maybe_as_integer_typ (i: ident) : Tot (option integer_type) | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None | val maybe_as_integer_typ (i: ident) : Tot (option integer_type)
let maybe_as_integer_typ (i: ident) : Tot (option integer_type) = | false | null | false | if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.ident",
"Prims.op_disEquality",
"FStar.Pervasives.Native.option",
"Prims.string",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.ident'",
"FStar.Pervasives.Native.None",
"Ast.integer_type",
"Prims.bool",
"Ast.__proj__Mkident'__item__name",
"FStar.Pervasives.Native.Some",
"Ast.UInt8",
"Ast.UInt16",
"Ast.UInt32",
"Ast.UInt64"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2 | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val maybe_as_integer_typ (i: ident) : Tot (option integer_type) | [] | Ast.maybe_as_integer_typ | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.ident -> FStar.Pervasives.Native.option Ast.integer_type | {
"end_col": 15,
"end_line": 208,
"start_col": 2,
"start_line": 196
} |
FStar.All.ML | val as_integer_typ (i: ident) : ML integer_type | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t | val as_integer_typ (i: ident) : ML integer_type
let as_integer_typ (i: ident) : ML integer_type = | true | null | false | match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.ident",
"Ast.maybe_as_integer_typ",
"Ast.error",
"Ast.integer_type",
"Prims.op_Hat",
"Ast.ident_to_string",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val as_integer_typ (i: ident) : ML integer_type | [] | Ast.as_integer_typ | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.ident -> FStar.All.ML Ast.integer_type | {
"end_col": 15,
"end_line": 213,
"start_col": 2,
"start_line": 211
} |
FStar.All.ML | val bit_order_of (i: ident) : ML bitfield_bit_order | [
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t | val bit_order_of (i: ident) : ML bitfield_bit_order
let bit_order_of (i: ident) : ML bitfield_bit_order = | true | null | false | match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.ident",
"Ast.maybe_bit_order_of",
"Ast.error",
"Ast.bitfield_bit_order",
"Prims.op_Hat",
"Ast.ident_to_string",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bit_order_of (i: ident) : ML bitfield_bit_order | [] | Ast.bit_order_of | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.ident -> FStar.All.ML Ast.bitfield_bit_order | {
"end_col": 15,
"end_line": 243,
"start_col": 2,
"start_line": 241
} |
Prims.Tot | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst = H.t ident' expr | let subst = | false | null | false | H.t ident' expr | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Hashtable.t",
"Ast.ident'",
"Ast.expr"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
//////////////////////////////////////////////////////////////////////////////// | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst : Type0 | [] | Ast.subst | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 27,
"end_line": 804,
"start_col": 12,
"start_line": 804
} |
|
Prims.Tot | val eq_out_expr (o1 o2: out_expr) : bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false | val eq_out_expr (o1 o2: out_expr) : bool
let rec eq_out_expr (o1 o2: out_expr) : bool = | false | null | false | match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2 | OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.out_expr",
"FStar.Pervasives.Native.Mktuple2",
"Ast.out_expr'",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.__proj__Mkout_expr__item__out_expr_node",
"Ast.ident",
"Ast.eq_idents",
"Ast.eq_out_expr",
"Prims.op_AmpAmp",
"FStar.Pervasives.Native.tuple2",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_out_expr (o1 o2: out_expr) : bool | [
"recursion"
] | Ast.eq_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | o1: Ast.out_expr -> o2: Ast.out_expr -> Prims.bool | {
"end_col": 14,
"end_line": 745,
"start_col": 2,
"start_line": 739
} |
Prims.Tot | val eq_idents (i1 i2: ident) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name | val eq_idents (i1 i2: ident) : Tot bool
let eq_idents (i1 i2: ident) : Tot bool = | false | null | false | i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.ident",
"Prims.op_AmpAmp",
"Prims.op_Equality",
"FStar.Pervasives.Native.option",
"Prims.string",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.ident'",
"Ast.__proj__Mkident'__item__name",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_idents (i1 i2: ident) : Tot bool | [] | Ast.eq_idents | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i1: Ast.ident -> i2: Ast.ident -> Prims.bool | {
"end_col": 60,
"end_line": 734,
"start_col": 2,
"start_line": 734
} |
Prims.Tot | val eq_typ_params (ps1 ps2: list typ_param) : bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false | val eq_typ_params (ps1 ps2: list typ_param) : bool
let rec eq_typ_params (ps1 ps2: list typ_param) : bool = | false | null | false | match ps1, ps2 with
| [], [] -> true
| p1 :: ps1, p2 :: ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Prims.list",
"Ast.typ_param",
"FStar.Pervasives.Native.Mktuple2",
"Prims.op_AmpAmp",
"Ast.eq_typ_param",
"Ast.eq_typ_params",
"FStar.Pervasives.Native.tuple2",
"Prims.bool"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_typ_params (ps1 ps2: list typ_param) : bool | [
"recursion"
] | Ast.eq_typ_params | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | ps1: Prims.list Ast.typ_param -> ps2: Prims.list Ast.typ_param -> Prims.bool | {
"end_col": 14,
"end_line": 757,
"start_col": 2,
"start_line": 754
} |
Prims.Tot | val action_has_out_expr (a: action) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a | val action_has_out_expr (a: action) : Tot bool
let rec action_has_out_expr (a: action) : Tot bool = | false | null | false | match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl -> if atomic_action_has_out_expr hd then true else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_ then true else action_has_out_expr else_
| Action_ite _ then_ None -> action_has_out_expr then_
| Action_let _ a k -> if atomic_action_has_out_expr a then true else action_has_out_expr k
| Action_act a -> action_has_out_expr a | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.action",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.action'",
"Ast.atomic_action",
"Ast.atomic_action_has_out_expr",
"Ast.with_meta_t",
"Prims.bool",
"Ast.action_has_out_expr",
"Ast.expr",
"Ast.ident"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action | false | true | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val action_has_out_expr (a: action) : Tot bool | [
"recursion"
] | Ast.action_has_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | a: Ast.action -> Prims.bool | {
"end_col": 25,
"end_line": 652,
"start_col": 2,
"start_line": 635
} |
FStar.All.ML | val subst_typ (s: subst) (t: typ) : ML typ | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) } | val subst_typ (s: subst) (t: typ) : ML typ
let rec subst_typ (s: subst) (t: typ) : ML typ = | true | null | false | match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> { t with v = Pointer (subst_typ s t) } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.typ",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.typ'",
"Ast.ident",
"Ast.t_kind",
"Prims.list",
"Ast.either",
"Ast.expr",
"Ast.out_expr",
"Ast.Mkwith_meta_t",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.Type_app",
"FStar.List.map",
"Ast.subst_typ_param",
"Ast.with_meta_t",
"Ast.Pointer",
"Ast.subst_typ"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe) | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_typ (s: subst) (t: typ) : ML typ | [
"recursion"
] | Ast.subst_typ | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> t: Ast.typ -> FStar.All.ML Ast.typ | {
"end_col": 54,
"end_line": 847,
"start_col": 2,
"start_line": 845
} |
FStar.All.ML | val subst_field_array (s: subst) (f: field_array_t) : ML field_array_t | [
{
"abbrev": true,
"full_module": "Hashtable",
"short_module": "H"
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let subst_field_array (s:subst) (f:field_array_t) : ML field_array_t =
match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz) | val subst_field_array (s: subst) (f: field_array_t) : ML field_array_t
let subst_field_array (s: subst) (f: field_array_t) : ML field_array_t = | true | null | false | match f with
| FieldScalar -> f
| FieldArrayQualified (e, q) -> FieldArrayQualified (subst_expr s e, q)
| FieldString sz -> FieldString (map_opt (subst_expr s) sz) | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"ml"
] | [
"Ast.subst",
"Ast.field_array_t",
"Ast.expr",
"Ast.array_qualifier",
"Ast.FieldArrayQualified",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.Mktuple2",
"Ast.subst_expr",
"FStar.Pervasives.Native.option",
"Ast.FieldString",
"Ast.map_opt"
] | [] | (*
Copyright 2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field
let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16"
let tuint32 = mk_prim_t "UINT32"
let tuint64 = mk_prim_t "UINT64"
let tunknown = mk_prim_t "?"
let unit_atomic_field rng =
let dummy_identifier = with_range (to_ident' "_empty_") rng in
let f = {
field_dependence=false;
field_ident=dummy_identifier;
field_type=tunit;
field_array_opt=FieldScalar;
field_constraint=None;
field_bitwidth=None;
field_action=None
} in
with_range f rng
let map_opt (f:'a -> ML 'b) (o:option 'a) : ML (option 'b) =
match o with
| None -> None
| Some x -> Some (f x)
////////////////////////////////////////////////////////////////////////////////
// Substitutions
////////////////////////////////////////////////////////////////////////////////
module H = Hashtable
let subst = H.t ident' expr
let mk_subst (s:list (ident * expr)) : ML subst =
let h = H.create 10 in
List.iter (fun (i, e) -> H.insert h i.v e) s;
h
let apply (s:subst) (id:ident) : ML expr =
match H.try_find s id.v with
| None -> with_range (Identifier id) id.range
| Some e -> e
let rec subst_expr (s:subst) (e:expr) : ML expr =
match e.v with
| Constant _
| This -> e
| Identifier i -> apply s i
| Static e -> { e with v = Static (subst_expr s e) }
| App op es -> {e with v = App op (List.map (subst_expr s) es)}
let subst_atomic_action (s:subst) (aa:atomic_action) : ML atomic_action =
match aa with
| Action_return e -> Action_return (subst_expr s e)
| Action_assignment lhs rhs -> Action_assignment lhs (subst_expr s rhs)
| Action_call f args -> Action_call f (List.map (subst_expr s) args)
| _ -> aa //action mutable identifiers are not subject to substitution
let rec subst_action (s:subst) (a:action) : ML action =
match a.v with
| Atomic_action aa -> {a with v = Atomic_action (subst_atomic_action s aa)}
| Action_seq hd tl -> {a with v = Action_seq (subst_atomic_action s hd) (subst_action s tl) }
| Action_ite hd then_ else_ -> {a with v = Action_ite (subst_expr s hd) (subst_action s then_) (subst_action_opt s else_) }
| Action_let i aa k -> {a with v = Action_let i (subst_atomic_action s aa) (subst_action s k) }
| Action_act a -> {a with v = Action_act (subst_action s a) }
and subst_action_opt (s:subst) (a:option action) : ML (option action) =
match a with
| None -> None
| Some a -> Some (subst_action s a)
//No need to substitute in output expressions
let subst_out_expr (s:subst) (o:out_expr) : out_expr = o
let subst_typ_param (s:subst) (p:typ_param) : ML typ_param =
match p with
| Inl e -> Inl (subst_expr s e)
| Inr oe -> Inr (subst_out_expr s oe)
let rec subst_typ (s:subst) (t:typ) : ML typ =
match t.v with
| Type_app hd k ps -> { t with v = Type_app hd k (List.map (subst_typ_param s) ps) }
| Pointer t -> {t with v = Pointer (subst_typ s t) } | false | false | Ast.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val subst_field_array (s: subst) (f: field_array_t) : ML field_array_t | [] | Ast.subst_field_array | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | s: Ast.subst -> f: Ast.field_array_t -> FStar.All.ML Ast.field_array_t | {
"end_col": 61,
"end_line": 852,
"start_col": 2,
"start_line": 849
} |
Prims.Tot | val coerce (#b #a: Type) (x: a{a == b}) : b | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let coerce (#b #a:Type) (x:a{a == b}) : b = x | val coerce (#b #a: Type) (x: a{a == b}) : b
let coerce (#b #a: Type) (x: a{a == b}) : b = | false | null | false | x | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Prims.eq2"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val coerce (#b #a: Type) (x: a{a == b}) : b | [] | Spec.Agile.Hash.coerce | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x: a{a == b} -> b | {
"end_col": 52,
"end_line": 10,
"start_col": 51,
"start_line": 10
} |
Prims.Tot | val update (a:md_alg): update_t a | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update | val update (a:md_alg): update_t a
let update (a: md_alg) = | false | null | false | match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> Spec.SHA2.update a
| MD5 -> Spec.MD5.update
| SHA1 -> Spec.SHA1.update | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.md_alg",
"Spec.SHA2.update",
"Spec.MD5.update",
"Spec.SHA1.update",
"Spec.Hash.Definitions.update_t"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes. | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val update (a:md_alg): update_t a | [] | Spec.Agile.Hash.update | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Spec.Hash.Definitions.md_alg -> Spec.Hash.Definitions.update_t a | {
"end_col": 22,
"end_line": 34,
"start_col": 2,
"start_line": 28
} |
Prims.Tot | val init (a:hash_alg): init_t a | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0) | val init (a:hash_alg): init_t a
let init a = | false | null | false | match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> Spec.SHA2.init a
| MD5 -> Spec.MD5.init
| SHA1 -> Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> Lib.Sequence.create 25 (u64 0) | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.hash_alg",
"Spec.SHA2.init",
"Spec.MD5.init",
"Spec.SHA1.init",
"Spec.Blake2.blake2_init_hash",
"Spec.Blake2.Blake2S",
"Spec.Blake2.Blake2B",
"Lib.Sequence.create",
"Spec.Hash.Definitions.word",
"Lib.IntTypes.u64",
"Spec.Hash.Definitions.init_t"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val init (a:hash_alg): init_t a | [] | Spec.Agile.Hash.init | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Spec.Hash.Definitions.hash_alg -> Spec.Hash.Definitions.init_t a | {
"end_col": 36,
"end_line": 23,
"start_col": 2,
"start_line": 13
} |
Prims.Tot | val finish (a:hash_alg): Spec.Hash.Definitions.finish_t a | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let finish (a:hash_alg) (hashw:words_state a) (l: output_length a): Tot (bytes_hash' a l) =
if is_blake a then
finish_blake a hashw
else if is_keccak a then
finish_sha3 a hashw l
else
finish_md a hashw | val finish (a:hash_alg): Spec.Hash.Definitions.finish_t a
let finish (a: hash_alg) (hashw: words_state a) (l: output_length a) : Tot (bytes_hash' a l) = | false | null | false | if is_blake a
then finish_blake a hashw
else if is_keccak a then finish_sha3 a hashw l else finish_md a hashw | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.hash_alg",
"Spec.Hash.Definitions.words_state",
"Spec.Hash.Definitions.output_length",
"Spec.Hash.Definitions.is_blake",
"Spec.Agile.Hash.finish_blake",
"Prims.bool",
"Spec.Hash.Definitions.is_keccak",
"Spec.Agile.Hash.finish_sha3",
"Spec.Agile.Hash.finish_md",
"Spec.Hash.Definitions.bytes_hash'"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes.
let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update
let update_multi a hash prev blocks =
match a with
| MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B | Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes blocks (absorb_inner rateInBytes) hash
(** Extracting the hash, which we call "finish" *)
(* Unflatten the hash from the sequence of words to bytes up to the correct size *)
let finish_md (a:md_alg) (hashw:words_state a): Tot (bytes_hash a) =
let hash_final_w = S.slice hashw 0 (hash_word_length a) in
bytes_of_words a #(hash_word_length a) hash_final_w
let finish_blake (a:blake_alg) (hash:words_state a): Tot (bytes_hash a) =
let alg = to_blake_alg a in
Spec.Blake2.blake2_finish alg hash (Spec.Blake2.max_output alg)
let finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a): Tot (bytes_hash' a l) =
let rateInBytes = rate a / 8 in
match a with
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 ->
let rateInBytes = rate a / 8 in
let outputByteLen = hash_length a in
assert (not (is_shake a));
Spec.SHA3.squeeze s rateInBytes outputByteLen
| Shake128 | Shake256 ->
Spec.SHA3.squeeze s rateInBytes l | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val finish (a:hash_alg): Spec.Hash.Definitions.finish_t a | [] | Spec.Agile.Hash.finish | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Spec.Hash.Definitions.hash_alg -> Spec.Hash.Definitions.finish_t a | {
"end_col": 21,
"end_line": 77,
"start_col": 2,
"start_line": 72
} |
Prims.Tot | val finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a) : Tot (bytes_hash' a l) | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a): Tot (bytes_hash' a l) =
let rateInBytes = rate a / 8 in
match a with
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 ->
let rateInBytes = rate a / 8 in
let outputByteLen = hash_length a in
assert (not (is_shake a));
Spec.SHA3.squeeze s rateInBytes outputByteLen
| Shake128 | Shake256 ->
Spec.SHA3.squeeze s rateInBytes l | val finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a) : Tot (bytes_hash' a l)
let finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a) : Tot (bytes_hash' a l) = | false | null | false | let rateInBytes = rate a / 8 in
match a with
| SHA3_224
| SHA3_256
| SHA3_384
| SHA3_512 ->
let rateInBytes = rate a / 8 in
let outputByteLen = hash_length a in
assert (not (is_shake a));
Spec.SHA3.squeeze s rateInBytes outputByteLen
| Shake128 | Shake256 -> Spec.SHA3.squeeze s rateInBytes l | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.keccak_alg",
"Spec.Hash.Definitions.words_state",
"Spec.Hash.Definitions.output_length",
"Spec.SHA3.squeeze",
"Prims.unit",
"Prims._assert",
"Prims.b2t",
"Prims.op_Negation",
"Spec.Hash.Definitions.is_shake",
"Prims.nat",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_GreaterThan",
"Spec.Hash.Definitions.hash_length",
"Prims.int",
"Prims.op_Division",
"Spec.Hash.Definitions.rate",
"Spec.Hash.Definitions.bytes_hash'"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes.
let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update
let update_multi a hash prev blocks =
match a with
| MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B | Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes blocks (absorb_inner rateInBytes) hash
(** Extracting the hash, which we call "finish" *)
(* Unflatten the hash from the sequence of words to bytes up to the correct size *)
let finish_md (a:md_alg) (hashw:words_state a): Tot (bytes_hash a) =
let hash_final_w = S.slice hashw 0 (hash_word_length a) in
bytes_of_words a #(hash_word_length a) hash_final_w
let finish_blake (a:blake_alg) (hash:words_state a): Tot (bytes_hash a) =
let alg = to_blake_alg a in
Spec.Blake2.blake2_finish alg hash (Spec.Blake2.max_output alg) | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a) : Tot (bytes_hash' a l) | [] | Spec.Agile.Hash.finish_sha3 | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Spec.Hash.Definitions.keccak_alg ->
s: Spec.Hash.Definitions.words_state a ->
l: Spec.Hash.Definitions.output_length a
-> Spec.Hash.Definitions.bytes_hash' a l | {
"end_col": 39,
"end_line": 69,
"start_col": 96,
"start_line": 60
} |
Prims.Tot | val hash' (a:hash_alg) (input:bytes{S.length input `less_than_max_input_length` a}) (l: output_length a):
Tot (Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length' a l)) | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let hash' a input l =
if is_blake a then
Spec.Blake2.blake2 (to_blake_alg a) input 0 Seq.empty (Spec.Blake2.max_output (to_blake_alg a))
else if is_md a then
(* As defined in the NIST standard; pad, then update, then finish. *)
let padding = pad a (S.length input) in
finish_md a (update_multi a (init a) () S.(input @| padding))
else match a with
| SHA3_224 -> Spec.SHA3.sha3_224 (Seq.length input) input
| SHA3_256 -> Spec.SHA3.sha3_256 (Seq.length input) input
| SHA3_384 -> Spec.SHA3.sha3_384 (Seq.length input) input
| SHA3_512 -> Spec.SHA3.sha3_512 (Seq.length input) input
| Shake128 -> Spec.SHA3.shake128 (Seq.length input) input l
| Shake256 -> Spec.SHA3.shake256 (Seq.length input) input l | val hash' (a:hash_alg) (input:bytes{S.length input `less_than_max_input_length` a}) (l: output_length a):
Tot (Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length' a l))
let hash' a input l = | false | null | false | if is_blake a
then Spec.Blake2.blake2 (to_blake_alg a) input 0 Seq.empty (Spec.Blake2.max_output (to_blake_alg a))
else
if is_md a
then
let padding = pad a (S.length input) in
finish_md a (update_multi a (init a) () S.(input @| padding))
else
match a with
| SHA3_224 -> Spec.SHA3.sha3_224 (Seq.length input) input
| SHA3_256 -> Spec.SHA3.sha3_256 (Seq.length input) input
| SHA3_384 -> Spec.SHA3.sha3_384 (Seq.length input) input
| SHA3_512 -> Spec.SHA3.sha3_512 (Seq.length input) input
| Shake128 -> Spec.SHA3.shake128 (Seq.length input) input l
| Shake256 -> Spec.SHA3.shake256 (Seq.length input) input l | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.hash_alg",
"Spec.Hash.Definitions.bytes",
"Prims.b2t",
"Spec.Hash.Definitions.less_than_max_input_length",
"FStar.Seq.Base.length",
"Lib.IntTypes.uint8",
"Spec.Hash.Definitions.output_length",
"Spec.Hash.Definitions.is_blake",
"Spec.Blake2.blake2",
"Spec.Hash.Definitions.to_blake_alg",
"FStar.Seq.Base.empty",
"Lib.IntTypes.uint_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Spec.Blake2.max_output",
"Prims.bool",
"Spec.Hash.Definitions.is_md",
"Spec.Agile.Hash.finish_md",
"Spec.Agile.Hash.update_multi",
"Spec.Agile.Hash.init",
"FStar.Seq.Base.op_At_Bar",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.op_Addition",
"Lib.IntTypes.int_t",
"Spec.Hash.Definitions.block_length",
"Spec.Hash.MD.pad",
"Spec.SHA3.sha3_224",
"Spec.SHA3.sha3_256",
"Spec.SHA3.sha3_384",
"Spec.SHA3.sha3_512",
"Spec.SHA3.shake128",
"Spec.SHA3.shake256",
"Lib.ByteSequence.lbytes",
"Spec.Hash.Definitions.hash_length'"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes.
let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update
let update_multi a hash prev blocks =
match a with
| MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B | Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes blocks (absorb_inner rateInBytes) hash
(** Extracting the hash, which we call "finish" *)
(* Unflatten the hash from the sequence of words to bytes up to the correct size *)
let finish_md (a:md_alg) (hashw:words_state a): Tot (bytes_hash a) =
let hash_final_w = S.slice hashw 0 (hash_word_length a) in
bytes_of_words a #(hash_word_length a) hash_final_w
let finish_blake (a:blake_alg) (hash:words_state a): Tot (bytes_hash a) =
let alg = to_blake_alg a in
Spec.Blake2.blake2_finish alg hash (Spec.Blake2.max_output alg)
let finish_sha3 (a: keccak_alg) (s: words_state a) (l: output_length a): Tot (bytes_hash' a l) =
let rateInBytes = rate a / 8 in
match a with
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 ->
let rateInBytes = rate a / 8 in
let outputByteLen = hash_length a in
assert (not (is_shake a));
Spec.SHA3.squeeze s rateInBytes outputByteLen
| Shake128 | Shake256 ->
Spec.SHA3.squeeze s rateInBytes l
let finish (a:hash_alg) (hashw:words_state a) (l: output_length a): Tot (bytes_hash' a l) =
if is_blake a then
finish_blake a hashw
else if is_keccak a then
finish_sha3 a hashw l
else
finish_md a hashw
#push-options "--fuel 0 --ifuel 0"
// MD hashes are by definition the application of init / update_multi / pad / finish.
// Blake2 does its own thing, and there is a slightly more involved proof that the hash is incremental.
// Same deal with the SHA3 family. | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val hash' (a:hash_alg) (input:bytes{S.length input `less_than_max_input_length` a}) (l: output_length a):
Tot (Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length' a l)) | [] | Spec.Agile.Hash.hash' | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Spec.Hash.Definitions.hash_alg ->
input:
Spec.Hash.Definitions.bytes
{Spec.Hash.Definitions.less_than_max_input_length (FStar.Seq.Base.length input) a} ->
l: Spec.Hash.Definitions.output_length a
-> Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length' a l) | {
"end_col": 63,
"end_line": 97,
"start_col": 2,
"start_line": 85
} |
Prims.Tot | val finish_md (a: md_alg) (hashw: words_state a) : Tot (bytes_hash a) | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let finish_md (a:md_alg) (hashw:words_state a): Tot (bytes_hash a) =
let hash_final_w = S.slice hashw 0 (hash_word_length a) in
bytes_of_words a #(hash_word_length a) hash_final_w | val finish_md (a: md_alg) (hashw: words_state a) : Tot (bytes_hash a)
let finish_md (a: md_alg) (hashw: words_state a) : Tot (bytes_hash a) = | false | null | false | let hash_final_w = S.slice hashw 0 (hash_word_length a) in
bytes_of_words a #(hash_word_length a) hash_final_w | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.md_alg",
"Spec.Hash.Definitions.words_state",
"Spec.Hash.Definitions.bytes_of_words",
"Spec.Hash.Definitions.hash_word_length",
"FStar.Seq.Base.seq",
"Spec.Hash.Definitions.word",
"FStar.Seq.Base.slice",
"Spec.Hash.Definitions.bytes_hash"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes.
let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update
let update_multi a hash prev blocks =
match a with
| MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B | Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes blocks (absorb_inner rateInBytes) hash
(** Extracting the hash, which we call "finish" *) | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val finish_md (a: md_alg) (hashw: words_state a) : Tot (bytes_hash a) | [] | Spec.Agile.Hash.finish_md | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Spec.Hash.Definitions.md_alg -> hashw: Spec.Hash.Definitions.words_state a
-> Spec.Hash.Definitions.bytes_hash a | {
"end_col": 53,
"end_line": 54,
"start_col": 68,
"start_line": 52
} |
Prims.Tot | val finish_blake (a: blake_alg) (hash: words_state a) : Tot (bytes_hash a) | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let finish_blake (a:blake_alg) (hash:words_state a): Tot (bytes_hash a) =
let alg = to_blake_alg a in
Spec.Blake2.blake2_finish alg hash (Spec.Blake2.max_output alg) | val finish_blake (a: blake_alg) (hash: words_state a) : Tot (bytes_hash a)
let finish_blake (a: blake_alg) (hash: words_state a) : Tot (bytes_hash a) = | false | null | false | let alg = to_blake_alg a in
Spec.Blake2.blake2_finish alg hash (Spec.Blake2.max_output alg) | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.blake_alg",
"Spec.Hash.Definitions.words_state",
"Spec.Blake2.blake2_finish",
"Spec.Blake2.max_output",
"Spec.Blake2.alg",
"Spec.Hash.Definitions.to_blake_alg",
"Spec.Hash.Definitions.bytes_hash"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes.
let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update
let update_multi a hash prev blocks =
match a with
| MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B | Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes blocks (absorb_inner rateInBytes) hash
(** Extracting the hash, which we call "finish" *)
(* Unflatten the hash from the sequence of words to bytes up to the correct size *)
let finish_md (a:md_alg) (hashw:words_state a): Tot (bytes_hash a) =
let hash_final_w = S.slice hashw 0 (hash_word_length a) in
bytes_of_words a #(hash_word_length a) hash_final_w | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val finish_blake (a: blake_alg) (hash: words_state a) : Tot (bytes_hash a) | [] | Spec.Agile.Hash.finish_blake | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Spec.Hash.Definitions.blake_alg -> hash: Spec.Hash.Definitions.words_state a
-> Spec.Hash.Definitions.bytes_hash a | {
"end_col": 65,
"end_line": 58,
"start_col": 73,
"start_line": 56
} |
Prims.Pure | val update_multi
(a:hash_alg)
(hash:words_state a)
(prev:extra_state a)
(blocks:bytes_blocks a):
Pure (words_state a)
(requires update_multi_pre a prev blocks)
(ensures fun _ -> True) | [
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.MD",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let update_multi a hash prev blocks =
match a with
| MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B | Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes blocks (absorb_inner rateInBytes) hash | val update_multi
(a:hash_alg)
(hash:words_state a)
(prev:extra_state a)
(blocks:bytes_blocks a):
Pure (words_state a)
(requires update_multi_pre a prev blocks)
(ensures fun _ -> True)
let update_multi a hash prev blocks = | false | null | false | match a with
| MD5
| SHA1
| SHA2_224
| SHA2_256
| SHA2_384
| SHA2_512 -> Lib.UpdateMulti.mk_update_multi (block_length a) (update a) hash blocks
| Blake2B
| Blake2S ->
let nb = S.length blocks / block_length a in
let a' = to_blake_alg a in
Lib.LoopCombinators.repeati #(words_state a) nb (Spec.Blake2.blake2_update1 a' prev blocks) hash
| SHA3_224
| SHA3_256
| SHA3_384
| SHA3_512
| Shake128
| Shake256 ->
let open Spec.SHA3 in
let rateInBytes = rate a / 8 in
Lib.Sequence.repeat_blocks_multi #_
#(words_state a)
rateInBytes
blocks
(absorb_inner rateInBytes)
hash | {
"checked_file": "Spec.Agile.Hash.fst.checked",
"dependencies": [
"Spec.SHA3.fst.checked",
"Spec.SHA2.fsti.checked",
"Spec.SHA1.fsti.checked",
"Spec.MD5.fsti.checked",
"Spec.Hash.MD.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "Spec.Agile.Hash.fst"
} | [] | [
"Spec.Hash.Definitions.hash_alg",
"Spec.Hash.Definitions.words_state",
"Spec.Hash.Definitions.extra_state",
"Spec.Hash.Definitions.bytes_blocks",
"Lib.UpdateMulti.mk_update_multi",
"Spec.Hash.Definitions.block_length",
"Spec.Agile.Hash.update",
"Lib.LoopCombinators.repeati",
"Spec.Blake2.blake2_update1",
"Spec.Blake2.alg",
"Spec.Hash.Definitions.to_blake_alg",
"Prims.int",
"Prims.op_Division",
"FStar.Seq.Base.length",
"Lib.IntTypes.uint8",
"Lib.Sequence.repeat_blocks_multi",
"Spec.SHA3.absorb_inner",
"Spec.Hash.Definitions.rate"
] | [] | module Spec.Agile.Hash
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.Hash.MD
open FStar.Mul
open Lib.IntTypes
unfold let coerce (#b #a:Type) (x:a{a == b}) : b = x
let init a =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.init a
| MD5 ->
Spec.MD5.init
| SHA1 ->
Spec.SHA1.init
| Blake2S -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2S 0 32
| Blake2B -> Spec.Blake2.blake2_init_hash Spec.Blake2.Blake2B 0 64
| SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
Lib.Sequence.create 25 (u64 0)
// Intentionally restricting this one to MD hashes... we want clients to AVOID
// juggling between mk_update_multi update vs. repeati for non-MD hashes.
let update (a: md_alg) =
match a with
| SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
Spec.SHA2.update a
| MD5 ->
Spec.MD5.update
| SHA1 ->
Spec.SHA1.update | false | false | Spec.Agile.Hash.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val update_multi
(a:hash_alg)
(hash:words_state a)
(prev:extra_state a)
(blocks:bytes_blocks a):
Pure (words_state a)
(requires update_multi_pre a prev blocks)
(ensures fun _ -> True) | [] | Spec.Agile.Hash.update_multi | {
"file_name": "specs/Spec.Agile.Hash.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Spec.Hash.Definitions.hash_alg ->
hash: Spec.Hash.Definitions.words_state a ->
prev: Spec.Hash.Definitions.extra_state a ->
blocks: Spec.Hash.Definitions.bytes_blocks a
-> Prims.Pure (Spec.Hash.Definitions.words_state a) | {
"end_col": 109,
"end_line": 47,
"start_col": 2,
"start_line": 37
} |
Prims.Tot | val openBase: openBase_st cs vale_p | [
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.AEAD",
"short_module": "IAEAD"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.Hash",
"short_module": "IHash"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.HKDF",
"short_module": "IHK"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.DH",
"short_module": "IDH"
},
{
"abbrev": false,
"full_module": "Hacl.Meta.HPKE",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Agile.Hash",
"short_module": "Hash"
},
{
"abbrev": true,
"full_module": "Spec.Agile.AEAD",
"short_module": "AEAD"
},
{
"abbrev": true,
"full_module": "Spec.Agile.DH",
"short_module": "DH"
},
{
"abbrev": true,
"full_module": "Spec.Agile.HPKE",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let openBase = hpke_openBase_higher #cs vale_p IAEAD.aead_decrypt_cp32 setupBaseR | val openBase: openBase_st cs vale_p
let openBase = | false | null | false | hpke_openBase_higher #cs vale_p IAEAD.aead_decrypt_cp32 setupBaseR | {
"checked_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst.checked",
"dependencies": [
"prims.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.HPKE.Interface.HKDF.fst.checked",
"Hacl.HPKE.Interface.Hash.fst.checked",
"Hacl.HPKE.Interface.DH.fst.checked",
"Hacl.HPKE.Interface.AEAD.fsti.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst"
} | [
"total"
] | [
"Hacl.Meta.HPKE.hpke_openBase_higher",
"Hacl.HPKE.Curve64_CP32_SHA512.cs",
"Hacl.HPKE.Curve64_CP32_SHA512.vale_p",
"Hacl.HPKE.Interface.AEAD.aead_decrypt_cp32",
"Hacl.HPKE.Curve64_CP32_SHA512.setupBaseR"
] | [] | module Hacl.HPKE.Curve64_CP32_SHA512
open Hacl.Meta.HPKE
module IDH = Hacl.HPKE.Interface.DH
module IHK = Hacl.HPKE.Interface.HKDF
module IHash = Hacl.HPKE.Interface.Hash
module IAEAD = Hacl.HPKE.Interface.AEAD
friend Hacl.Meta.HPKE
#set-options "--fuel 0 --ifuel 0"
let setupBaseS = hpke_setupBaseS_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.secret_to_public_c64 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256
let setupBaseR = hpke_setupBaseR_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256 IDH.secret_to_public_c64
let sealBase = hpke_sealBase_higher #cs vale_p IAEAD.aead_encrypt_cp32 setupBaseS | false | true | Hacl.HPKE.Curve64_CP32_SHA512.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val openBase: openBase_st cs vale_p | [] | Hacl.HPKE.Curve64_CP32_SHA512.openBase | {
"file_name": "code/hpke/Hacl.HPKE.Curve64_CP32_SHA512.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Hacl.Impl.HPKE.openBase_st Hacl.HPKE.Curve64_CP32_SHA512.cs Hacl.HPKE.Curve64_CP32_SHA512.vale_p | {
"end_col": 81,
"end_line": 20,
"start_col": 15,
"start_line": 20
} |
Prims.Tot | val sealBase: sealBase_st cs vale_p | [
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.AEAD",
"short_module": "IAEAD"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.Hash",
"short_module": "IHash"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.HKDF",
"short_module": "IHK"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.DH",
"short_module": "IDH"
},
{
"abbrev": false,
"full_module": "Hacl.Meta.HPKE",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Agile.Hash",
"short_module": "Hash"
},
{
"abbrev": true,
"full_module": "Spec.Agile.AEAD",
"short_module": "AEAD"
},
{
"abbrev": true,
"full_module": "Spec.Agile.DH",
"short_module": "DH"
},
{
"abbrev": true,
"full_module": "Spec.Agile.HPKE",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let sealBase = hpke_sealBase_higher #cs vale_p IAEAD.aead_encrypt_cp32 setupBaseS | val sealBase: sealBase_st cs vale_p
let sealBase = | false | null | false | hpke_sealBase_higher #cs vale_p IAEAD.aead_encrypt_cp32 setupBaseS | {
"checked_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst.checked",
"dependencies": [
"prims.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.HPKE.Interface.HKDF.fst.checked",
"Hacl.HPKE.Interface.Hash.fst.checked",
"Hacl.HPKE.Interface.DH.fst.checked",
"Hacl.HPKE.Interface.AEAD.fsti.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst"
} | [
"total"
] | [
"Hacl.Meta.HPKE.hpke_sealBase_higher",
"Hacl.HPKE.Curve64_CP32_SHA512.cs",
"Hacl.HPKE.Curve64_CP32_SHA512.vale_p",
"Hacl.HPKE.Interface.AEAD.aead_encrypt_cp32",
"Hacl.HPKE.Curve64_CP32_SHA512.setupBaseS"
] | [] | module Hacl.HPKE.Curve64_CP32_SHA512
open Hacl.Meta.HPKE
module IDH = Hacl.HPKE.Interface.DH
module IHK = Hacl.HPKE.Interface.HKDF
module IHash = Hacl.HPKE.Interface.Hash
module IAEAD = Hacl.HPKE.Interface.AEAD
friend Hacl.Meta.HPKE
#set-options "--fuel 0 --ifuel 0"
let setupBaseS = hpke_setupBaseS_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.secret_to_public_c64 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256
let setupBaseR = hpke_setupBaseR_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256 IDH.secret_to_public_c64 | false | true | Hacl.HPKE.Curve64_CP32_SHA512.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sealBase: sealBase_st cs vale_p | [] | Hacl.HPKE.Curve64_CP32_SHA512.sealBase | {
"file_name": "code/hpke/Hacl.HPKE.Curve64_CP32_SHA512.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Hacl.Impl.HPKE.sealBase_st Hacl.HPKE.Curve64_CP32_SHA512.cs Hacl.HPKE.Curve64_CP32_SHA512.vale_p | {
"end_col": 81,
"end_line": 18,
"start_col": 15,
"start_line": 18
} |
Prims.Tot | val setupBaseS: setupBaseS_st cs vale_p | [
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.AEAD",
"short_module": "IAEAD"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.Hash",
"short_module": "IHash"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.HKDF",
"short_module": "IHK"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.DH",
"short_module": "IDH"
},
{
"abbrev": false,
"full_module": "Hacl.Meta.HPKE",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Agile.Hash",
"short_module": "Hash"
},
{
"abbrev": true,
"full_module": "Spec.Agile.AEAD",
"short_module": "AEAD"
},
{
"abbrev": true,
"full_module": "Spec.Agile.DH",
"short_module": "DH"
},
{
"abbrev": true,
"full_module": "Spec.Agile.HPKE",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let setupBaseS = hpke_setupBaseS_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.secret_to_public_c64 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256 | val setupBaseS: setupBaseS_st cs vale_p
let setupBaseS = | false | null | false | hpke_setupBaseS_higher #cs
vale_p
IHK.hkdf_expand512
IHK.hkdf_extract512
IDH.secret_to_public_c64
IDH.dh_c64
IHK.hkdf_expand256
IHK.hkdf_extract256 | {
"checked_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst.checked",
"dependencies": [
"prims.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.HPKE.Interface.HKDF.fst.checked",
"Hacl.HPKE.Interface.Hash.fst.checked",
"Hacl.HPKE.Interface.DH.fst.checked",
"Hacl.HPKE.Interface.AEAD.fsti.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst"
} | [
"total"
] | [
"Hacl.Meta.HPKE.hpke_setupBaseS_higher",
"Hacl.HPKE.Curve64_CP32_SHA512.cs",
"Hacl.HPKE.Curve64_CP32_SHA512.vale_p",
"Hacl.HPKE.Interface.HKDF.hkdf_expand512",
"Hacl.HPKE.Interface.HKDF.hkdf_extract512",
"Hacl.HPKE.Interface.DH.secret_to_public_c64",
"Hacl.HPKE.Interface.DH.dh_c64",
"Hacl.HPKE.Interface.HKDF.hkdf_expand256",
"Hacl.HPKE.Interface.HKDF.hkdf_extract256"
] | [] | module Hacl.HPKE.Curve64_CP32_SHA512
open Hacl.Meta.HPKE
module IDH = Hacl.HPKE.Interface.DH
module IHK = Hacl.HPKE.Interface.HKDF
module IHash = Hacl.HPKE.Interface.Hash
module IAEAD = Hacl.HPKE.Interface.AEAD
friend Hacl.Meta.HPKE
#set-options "--fuel 0 --ifuel 0" | false | true | Hacl.HPKE.Curve64_CP32_SHA512.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val setupBaseS: setupBaseS_st cs vale_p | [] | Hacl.HPKE.Curve64_CP32_SHA512.setupBaseS | {
"file_name": "code/hpke/Hacl.HPKE.Curve64_CP32_SHA512.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Hacl.Impl.HPKE.setupBaseS_st Hacl.HPKE.Curve64_CP32_SHA512.cs Hacl.HPKE.Curve64_CP32_SHA512.vale_p | {
"end_col": 164,
"end_line": 14,
"start_col": 17,
"start_line": 14
} |
Prims.Tot | val setupBaseR: setupBaseR_st cs vale_p | [
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.AEAD",
"short_module": "IAEAD"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.Hash",
"short_module": "IHash"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.HKDF",
"short_module": "IHK"
},
{
"abbrev": true,
"full_module": "Hacl.HPKE.Interface.DH",
"short_module": "IDH"
},
{
"abbrev": false,
"full_module": "Hacl.Meta.HPKE",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Agile.Hash",
"short_module": "Hash"
},
{
"abbrev": true,
"full_module": "Spec.Agile.AEAD",
"short_module": "AEAD"
},
{
"abbrev": true,
"full_module": "Spec.Agile.DH",
"short_module": "DH"
},
{
"abbrev": true,
"full_module": "Spec.Agile.HPKE",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HPKE",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let setupBaseR = hpke_setupBaseR_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256 IDH.secret_to_public_c64 | val setupBaseR: setupBaseR_st cs vale_p
let setupBaseR = | false | null | false | hpke_setupBaseR_higher #cs
vale_p
IHK.hkdf_expand512
IHK.hkdf_extract512
IDH.dh_c64
IHK.hkdf_expand256
IHK.hkdf_extract256
IDH.secret_to_public_c64 | {
"checked_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst.checked",
"dependencies": [
"prims.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.Meta.HPKE.fst.checked",
"Hacl.HPKE.Interface.HKDF.fst.checked",
"Hacl.HPKE.Interface.Hash.fst.checked",
"Hacl.HPKE.Interface.DH.fst.checked",
"Hacl.HPKE.Interface.AEAD.fsti.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Hacl.HPKE.Curve64_CP32_SHA512.fst"
} | [
"total"
] | [
"Hacl.Meta.HPKE.hpke_setupBaseR_higher",
"Hacl.HPKE.Curve64_CP32_SHA512.cs",
"Hacl.HPKE.Curve64_CP32_SHA512.vale_p",
"Hacl.HPKE.Interface.HKDF.hkdf_expand512",
"Hacl.HPKE.Interface.HKDF.hkdf_extract512",
"Hacl.HPKE.Interface.DH.dh_c64",
"Hacl.HPKE.Interface.HKDF.hkdf_expand256",
"Hacl.HPKE.Interface.HKDF.hkdf_extract256",
"Hacl.HPKE.Interface.DH.secret_to_public_c64"
] | [] | module Hacl.HPKE.Curve64_CP32_SHA512
open Hacl.Meta.HPKE
module IDH = Hacl.HPKE.Interface.DH
module IHK = Hacl.HPKE.Interface.HKDF
module IHash = Hacl.HPKE.Interface.Hash
module IAEAD = Hacl.HPKE.Interface.AEAD
friend Hacl.Meta.HPKE
#set-options "--fuel 0 --ifuel 0"
let setupBaseS = hpke_setupBaseS_higher #cs vale_p IHK.hkdf_expand512 IHK.hkdf_extract512 IDH.secret_to_public_c64 IDH.dh_c64 IHK.hkdf_expand256 IHK.hkdf_extract256 | false | true | Hacl.HPKE.Curve64_CP32_SHA512.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val setupBaseR: setupBaseR_st cs vale_p | [] | Hacl.HPKE.Curve64_CP32_SHA512.setupBaseR | {
"file_name": "code/hpke/Hacl.HPKE.Curve64_CP32_SHA512.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Hacl.Impl.HPKE.setupBaseR_st Hacl.HPKE.Curve64_CP32_SHA512.cs Hacl.HPKE.Curve64_CP32_SHA512.vale_p | {
"end_col": 164,
"end_line": 16,
"start_col": 17,
"start_line": 16
} |
Prims.Tot | val v (x:t) : Tot (uint_t n) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let v x = x.v | val v (x:t) : Tot (uint_t n)
let v x = | false | null | false | x.v | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [
"total"
] | [
"FStar.UInt16.t",
"FStar.UInt16.__proj__Mk__item__v",
"FStar.UInt.uint_t",
"FStar.UInt16.n"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t | false | true | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val v (x:t) : Tot (uint_t n) | [] | FStar.UInt16.v | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> FStar.UInt.uint_t FStar.UInt16.n | {
"end_col": 13,
"end_line": 28,
"start_col": 10,
"start_line": 28
} |
Prims.Pure | val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let sub_underspec a b = Mk (sub_underspec (v a) (v b)) | val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
let sub_underspec a b = | false | null | false | Mk (sub_underspec (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.sub_underspec",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c)) | [] | FStar.UInt16.sub_underspec | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 54,
"end_line": 50,
"start_col": 24,
"start_line": 50
} |
Prims.Pure | val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mul_underspec a b = Mk (mul_underspec (v a) (v b)) | val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
let mul_underspec a b = | false | null | false | Mk (mul_underspec (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.mul_underspec",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c)) | [] | FStar.UInt16.mul_underspec | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 54,
"end_line": 56,
"start_col": 24,
"start_line": 56
} |
Prims.Pure | val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let add_underspec a b = Mk (add_underspec (v a) (v b)) | val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
let add_underspec a b = | false | null | false | Mk (add_underspec (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.add_underspec",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c)) | [] | FStar.UInt16.add_underspec | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 54,
"end_line": 44,
"start_col": 24,
"start_line": 44
} |
Prims.Pure | val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let add a b = Mk (add (v a) (v b)) | val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
let add a b = | false | null | false | Mk (add (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.add",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1 | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c)) | [] | FStar.UInt16.add | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 34,
"end_line": 42,
"start_col": 14,
"start_line": 42
} |
Prims.Pure | val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let logor x y = Mk (logor (v x) (v y)) | val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
let logor x y = | false | null | false | Mk (logor (v x) (v y)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.logor",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b))
let logand x y = Mk (logand (v x) (v y))
let logxor x y = Mk (logxor (v x) (v y)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z)) | [] | FStar.UInt16.logor | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 38,
"end_line": 68,
"start_col": 16,
"start_line": 68
} |
Prims.Tot | val zero : x:t{v x = 0} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let zero = uint_to_t 0 | val zero : x:t{v x = 0}
let zero = | false | null | false | uint_to_t 0 | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [
"total"
] | [
"FStar.UInt16.uint_to_t"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = () | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val zero : x:t{v x = 0} | [] | FStar.UInt16.zero | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t{FStar.UInt16.v x = 0} | {
"end_col": 22,
"end_line": 38,
"start_col": 11,
"start_line": 38
} |
Prims.Pure | val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mul a b = Mk (mul (v a) (v b)) | val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
let mul a b = | false | null | false | Mk (mul (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.mul",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c)) | [] | FStar.UInt16.mul | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 34,
"end_line": 54,
"start_col": 14,
"start_line": 54
} |
Prims.Pure | val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let uint_to_t x = Mk x | val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
let uint_to_t x = | false | null | false | Mk x | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"FStar.UInt16.Mk",
"FStar.UInt16.t"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x)) | [] | FStar.UInt16.uint_to_t | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt.uint_t FStar.UInt16.n -> Prims.Pure FStar.UInt16.t | {
"end_col": 22,
"end_line": 30,
"start_col": 18,
"start_line": 30
} |
Prims.Tot | val one : x:t{v x = 1} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let one = uint_to_t 1 | val one : x:t{v x = 1}
let one = | false | null | false | uint_to_t 1 | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [
"total"
] | [
"FStar.UInt16.uint_to_t"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0 | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val one : x:t{v x = 1} | [] | FStar.UInt16.one | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t{FStar.UInt16.v x = 1} | {
"end_col": 21,
"end_line": 40,
"start_col": 10,
"start_line": 40
} |
Prims.Pure | val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let logxor x y = Mk (logxor (v x) (v y)) | val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
let logxor x y = | false | null | false | Mk (logxor (v x) (v y)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.logxor",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b))
let logand x y = Mk (logand (v x) (v y)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z)) | [] | FStar.UInt16.logxor | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 40,
"end_line": 66,
"start_col": 17,
"start_line": 66
} |
Prims.Pure | val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let sub a b = Mk (sub (v a) (v b)) | val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
let sub a b = | false | null | false | Mk (sub (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.sub",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c)) | [] | FStar.UInt16.sub | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 34,
"end_line": 48,
"start_col": 14,
"start_line": 48
} |
Prims.Pure | val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let lognot x = Mk (lognot (v x)) | val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
let lognot x = | false | null | false | Mk (lognot (v x)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.lognot",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b))
let logand x y = Mk (logand (v x) (v y))
let logxor x y = Mk (logxor (v x) (v y))
let logor x y = Mk (logor (v x) (v y)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z)) | [] | FStar.UInt16.lognot | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 32,
"end_line": 70,
"start_col": 15,
"start_line": 70
} |
Prims.Pure | val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let sub_mod a b = Mk (sub_mod (v a) (v b)) | val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
let sub_mod a b = | false | null | false | Mk (sub_mod (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.sub_mod",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c)) | [] | FStar.UInt16.sub_mod | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 42,
"end_line": 52,
"start_col": 18,
"start_line": 52
} |
Prims.Pure | val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let shift_right a s = Mk (shift_right (v a) (UInt32.v s)) | val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
let shift_right a s = | false | null | false | Mk (shift_right (v a) (UInt32.v s)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt32.t",
"FStar.UInt16.Mk",
"FStar.UInt.shift_right",
"FStar.UInt16.n",
"FStar.UInt16.v",
"FStar.UInt32.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b))
let logand x y = Mk (logand (v x) (v y))
let logxor x y = Mk (logxor (v x) (v y))
let logor x y = Mk (logor (v x) (v y))
let lognot x = Mk (lognot (v x)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c)) | [] | FStar.UInt16.shift_right | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 57,
"end_line": 72,
"start_col": 22,
"start_line": 72
} |
Prims.Pure | val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let add_mod a b = Mk (add_mod (v a) (v b)) | val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
let add_mod a b = | false | null | false | Mk (add_mod (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.add_mod",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c)) | [] | FStar.UInt16.add_mod | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 42,
"end_line": 46,
"start_col": 18,
"start_line": 46
} |
Prims.Pure | val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let mul_mod a b = Mk (mul_mod (v a) (v b)) | val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
let mul_mod a b = | false | null | false | Mk (mul_mod (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.mul_mod",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c)) | [] | FStar.UInt16.mul_mod | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 42,
"end_line": 58,
"start_col": 18,
"start_line": 58
} |
Prims.Pure | val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rem a b = Mk (mod (v a) (v b)) | val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
let rem a b = | false | null | false | Mk (mod (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"Prims.b2t",
"Prims.op_disEquality",
"Prims.int",
"FStar.UInt16.v",
"FStar.UInt16.Mk",
"FStar.UInt.mod",
"FStar.UInt16.n"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c)) | [] | FStar.UInt16.rem | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | {
"end_col": 34,
"end_line": 62,
"start_col": 14,
"start_line": 62
} |
Prims.Pure | val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let div a b = Mk (div (v a) (v b)) | val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
let div a b = | false | null | false | Mk (div (v a) (v b)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"Prims.b2t",
"Prims.op_disEquality",
"Prims.int",
"FStar.UInt16.v",
"FStar.UInt16.Mk",
"FStar.UInt.div",
"FStar.UInt16.n"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c)) | [] | FStar.UInt16.div | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t{FStar.UInt16.v b <> 0} -> Prims.Pure FStar.UInt16.t | {
"end_col": 34,
"end_line": 60,
"start_col": 14,
"start_line": 60
} |
Prims.Pure | val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let shift_left a s = Mk (shift_left (v a) (UInt32.v s)) | val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
let shift_left a s = | false | null | false | Mk (shift_left (v a) (UInt32.v s)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt32.t",
"FStar.UInt16.Mk",
"FStar.UInt.shift_left",
"FStar.UInt16.n",
"FStar.UInt16.v",
"FStar.UInt32.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b))
let logand x y = Mk (logand (v x) (v y))
let logxor x y = Mk (logxor (v x) (v y))
let logor x y = Mk (logor (v x) (v y))
let lognot x = Mk (lognot (v x))
let shift_right a s = Mk (shift_right (v a) (UInt32.v s))
#push-options "--z3rlimit 80 --fuel 1" //AR: working around the interleaving semantics of pragmas | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 1,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 80,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c)) | [] | FStar.UInt16.shift_left | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 55,
"end_line": 76,
"start_col": 21,
"start_line": 76
} |
Prims.Pure | val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z)) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let logand x y = Mk (logand (v x) (v y)) | val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
let logand x y = | false | null | false | Mk (logand (v x) (v y)) | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [] | [
"FStar.UInt16.t",
"FStar.UInt16.Mk",
"FStar.UInt.logand",
"FStar.UInt16.n",
"FStar.UInt16.v"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z)) | [] | FStar.UInt16.logand | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt16.t -> y: FStar.UInt16.t -> Prims.Pure FStar.UInt16.t | {
"end_col": 40,
"end_line": 64,
"start_col": 17,
"start_line": 64
} |
FStar.Pervasives.Lemma | val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b)))) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let lemma_sub_msbs a b
= from_vec_propriety (to_vec (v a)) 1;
from_vec_propriety (to_vec (v b)) 1;
from_vec_propriety (to_vec (v (sub_mod a b))) 1 | val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
let lemma_sub_msbs a b = | false | null | true | from_vec_propriety (to_vec (v a)) 1;
from_vec_propriety (to_vec (v b)) 1;
from_vec_propriety (to_vec (v (sub_mod a b))) 1 | {
"checked_file": "FStar.UInt16.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt16.fst"
} | [
"lemma"
] | [
"FStar.UInt16.t",
"FStar.UInt.from_vec_propriety",
"FStar.UInt16.n",
"FStar.UInt.to_vec",
"FStar.UInt16.v",
"FStar.UInt16.sub_mod",
"Prims.unit"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt16
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
type t : eqtype =
| Mk: v:uint_t n -> t
let v x = x.v
let uint_to_t x = Mk x
let uv_inv _ = ()
let vu_inv _ = ()
let v_inj _ _ = ()
let zero = uint_to_t 0
let one = uint_to_t 1
let add a b = Mk (add (v a) (v b))
let add_underspec a b = Mk (add_underspec (v a) (v b))
let add_mod a b = Mk (add_mod (v a) (v b))
let sub a b = Mk (sub (v a) (v b))
let sub_underspec a b = Mk (sub_underspec (v a) (v b))
let sub_mod a b = Mk (sub_mod (v a) (v b))
let mul a b = Mk (mul (v a) (v b))
let mul_underspec a b = Mk (mul_underspec (v a) (v b))
let mul_mod a b = Mk (mul_mod (v a) (v b))
let div a b = Mk (div (v a) (v b))
let rem a b = Mk (mod (v a) (v b))
let logand x y = Mk (logand (v x) (v y))
let logxor x y = Mk (logxor (v x) (v y))
let logor x y = Mk (logor (v x) (v y))
let lognot x = Mk (lognot (v x))
let shift_right a s = Mk (shift_right (v a) (UInt32.v s))
#push-options "--z3rlimit 80 --fuel 1" //AR: working around the interleaving semantics of pragmas
let shift_left a s = Mk (shift_left (v a) (UInt32.v s)) | false | false | FStar.UInt16.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 1,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 80,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b)))) | [] | FStar.UInt16.lemma_sub_msbs | {
"file_name": "ulib/FStar.UInt16.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt16.t -> b: FStar.UInt16.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt.msb (FStar.UInt16.v a) = FStar.UInt.msb (FStar.UInt16.v b) ==>
(FStar.UInt16.v a < FStar.UInt16.v b <==>
FStar.UInt.msb (FStar.UInt16.v (FStar.UInt16.sub_mod a b)))) | {
"end_col": 53,
"end_line": 81,
"start_col": 6,
"start_line": 79
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "Spec.Hash.Lemmas",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Spec.SHA2.Constants",
"short_module": "C"
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let ws = ws_aux | let ws = | false | null | false | ws_aux | {
"checked_file": "Spec.SHA2.Lemmas.fst.checked",
"dependencies": [
"Spec.SHA2.Constants.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.Loops.fst.checked",
"Spec.Hash.Lemmas.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Agile.Hash.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Spec.SHA2.Lemmas.fst"
} | [
"total"
] | [
"Spec.SHA2.Lemmas.ws_aux"
] | [] | module Spec.SHA2.Lemmas
open Lib.IntTypes
module C = Spec.SHA2.Constants
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.SHA2
open Spec.Hash.Lemmas
friend Spec.SHA2
friend Spec.Agile.Hash
#set-options "--z3rlimit 25 --fuel 0 --ifuel 0"
(* Scheduling function *)
(* Recursive Version *)
let rec ws_aux (a:sha2_alg) (b:block_w a) (t:counter{t < size_k_w a}): Tot (word a) =
if t < block_word_length a then b.[t]
else
let t16 = ws_aux a b (t - 16) in
let t15 = ws_aux a b (t - 15) in
let t7 = ws_aux a b (t - 7) in
let t2 = ws_aux a b (t - 2) in
let s1 = _sigma1 a t2 in
let s0 = _sigma0 a t15 in
(s1 +. t7 +. s0 +. t16) | false | false | Spec.SHA2.Lemmas.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 25,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val ws : a: Spec.Hash.Definitions.sha2_alg ->
b: Spec.SHA2.block_w a ->
t: Spec.SHA2.counter{t < Spec.SHA2.size_k_w a}
-> Spec.Hash.Definitions.word a | [] | Spec.SHA2.Lemmas.ws | {
"file_name": "specs/lemmas/Spec.SHA2.Lemmas.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Spec.Hash.Definitions.sha2_alg ->
b: Spec.SHA2.block_w a ->
t: Spec.SHA2.counter{t < Spec.SHA2.size_k_w a}
-> Spec.Hash.Definitions.word a | {
"end_col": 15,
"end_line": 32,
"start_col": 9,
"start_line": 32
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "Spec.Hash.Lemmas",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Spec.SHA2.Constants",
"short_module": "C"
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let shuffle_core = shuffle_core_ | let shuffle_core = | false | null | false | shuffle_core_ | {
"checked_file": "Spec.SHA2.Lemmas.fst.checked",
"dependencies": [
"Spec.SHA2.Constants.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.Loops.fst.checked",
"Spec.Hash.Lemmas.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Agile.Hash.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Spec.SHA2.Lemmas.fst"
} | [
"total"
] | [
"Spec.SHA2.Lemmas.shuffle_core_"
] | [] | module Spec.SHA2.Lemmas
open Lib.IntTypes
module C = Spec.SHA2.Constants
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.SHA2
open Spec.Hash.Lemmas
friend Spec.SHA2
friend Spec.Agile.Hash
#set-options "--z3rlimit 25 --fuel 0 --ifuel 0"
(* Scheduling function *)
(* Recursive Version *)
let rec ws_aux (a:sha2_alg) (b:block_w a) (t:counter{t < size_k_w a}): Tot (word a) =
if t < block_word_length a then b.[t]
else
let t16 = ws_aux a b (t - 16) in
let t15 = ws_aux a b (t - 15) in
let t7 = ws_aux a b (t - 7) in
let t2 = ws_aux a b (t - 2) in
let s1 = _sigma1 a t2 in
let s0 = _sigma0 a t15 in
(s1 +. t7 +. s0 +. t16)
[@"opaque_to_smt"]
let ws = ws_aux
(* Core shuffling function *)
let shuffle_core_ (a:sha2_alg) (block:block_w a) (hash:words_state a) (t:counter{t < size_k_w a}): Tot (words_state a) =
(**) assert(7 <= S.length hash);
let a0 = hash.[0] in
let b0 = hash.[1] in
let c0 = hash.[2] in
let d0 = hash.[3] in
let e0 = hash.[4] in
let f0 = hash.[5] in
let g0 = hash.[6] in
let h0 = hash.[7] in
(**) assert(S.length (k0 a) = size_k_w a);
let t1 = h0 +. (_Sigma1 a e0) +. (_Ch a e0 f0 g0) +. (k0 a).[t] +. (ws a block t) in
let t2 = (_Sigma0 a a0) +. (_Maj a a0 b0 c0) in
(**) assert(t < S.length (k0 a));
let l = [ t1 +. t2; a0; b0; c0; d0 +. t1; e0; f0; g0 ] in
assert_norm (List.Tot.length l = 8);
S.seq_of_list l | false | false | Spec.SHA2.Lemmas.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 25,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shuffle_core : a: Spec.Hash.Definitions.sha2_alg ->
block: Spec.SHA2.block_w a ->
hash: Spec.Hash.Definitions.words_state a ->
t: Spec.SHA2.counter{t < Spec.SHA2.size_k_w a}
-> Spec.Hash.Definitions.words_state a | [] | Spec.SHA2.Lemmas.shuffle_core | {
"file_name": "specs/lemmas/Spec.SHA2.Lemmas.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Spec.Hash.Definitions.sha2_alg ->
block: Spec.SHA2.block_w a ->
hash: Spec.Hash.Definitions.words_state a ->
t: Spec.SHA2.counter{t < Spec.SHA2.size_k_w a}
-> Spec.Hash.Definitions.words_state a | {
"end_col": 32,
"end_line": 56,
"start_col": 19,
"start_line": 56
} |
|
Prims.Tot | val shuffle_aux (a: sha2_alg) (hash: words_state a) (block: block_w a) : Tot (words_state a) | [
{
"abbrev": false,
"full_module": "Spec.Hash.Lemmas",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Spec.SHA2.Constants",
"short_module": "C"
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let shuffle_aux (a:sha2_alg) (hash:words_state a) (block:block_w a): Tot (words_state a) =
Spec.Loops.repeat_range 0 (size_k_w a) (shuffle_core a block) hash | val shuffle_aux (a: sha2_alg) (hash: words_state a) (block: block_w a) : Tot (words_state a)
let shuffle_aux (a: sha2_alg) (hash: words_state a) (block: block_w a) : Tot (words_state a) = | false | null | false | Spec.Loops.repeat_range 0 (size_k_w a) (shuffle_core a block) hash | {
"checked_file": "Spec.SHA2.Lemmas.fst.checked",
"dependencies": [
"Spec.SHA2.Constants.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.SHA2.fst.checked",
"Spec.Loops.fst.checked",
"Spec.Hash.Lemmas.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Agile.Hash.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Spec.SHA2.Lemmas.fst"
} | [
"total"
] | [
"Spec.Hash.Definitions.sha2_alg",
"Spec.Hash.Definitions.words_state",
"Spec.SHA2.block_w",
"Spec.Loops.repeat_range",
"Spec.SHA2.size_k_w",
"Spec.SHA2.Lemmas.shuffle_core"
] | [] | module Spec.SHA2.Lemmas
open Lib.IntTypes
module C = Spec.SHA2.Constants
module S = FStar.Seq
open Spec.Hash.Definitions
open Spec.SHA2
open Spec.Hash.Lemmas
friend Spec.SHA2
friend Spec.Agile.Hash
#set-options "--z3rlimit 25 --fuel 0 --ifuel 0"
(* Scheduling function *)
(* Recursive Version *)
let rec ws_aux (a:sha2_alg) (b:block_w a) (t:counter{t < size_k_w a}): Tot (word a) =
if t < block_word_length a then b.[t]
else
let t16 = ws_aux a b (t - 16) in
let t15 = ws_aux a b (t - 15) in
let t7 = ws_aux a b (t - 7) in
let t2 = ws_aux a b (t - 2) in
let s1 = _sigma1 a t2 in
let s0 = _sigma0 a t15 in
(s1 +. t7 +. s0 +. t16)
[@"opaque_to_smt"]
let ws = ws_aux
(* Core shuffling function *)
let shuffle_core_ (a:sha2_alg) (block:block_w a) (hash:words_state a) (t:counter{t < size_k_w a}): Tot (words_state a) =
(**) assert(7 <= S.length hash);
let a0 = hash.[0] in
let b0 = hash.[1] in
let c0 = hash.[2] in
let d0 = hash.[3] in
let e0 = hash.[4] in
let f0 = hash.[5] in
let g0 = hash.[6] in
let h0 = hash.[7] in
(**) assert(S.length (k0 a) = size_k_w a);
let t1 = h0 +. (_Sigma1 a e0) +. (_Ch a e0 f0 g0) +. (k0 a).[t] +. (ws a block t) in
let t2 = (_Sigma0 a a0) +. (_Maj a a0 b0 c0) in
(**) assert(t < S.length (k0 a));
let l = [ t1 +. t2; a0; b0; c0; d0 +. t1; e0; f0; g0 ] in
assert_norm (List.Tot.length l = 8);
S.seq_of_list l
[@"opaque_to_smt"]
let shuffle_core = shuffle_core_
(* Full shuffling function *) | false | false | Spec.SHA2.Lemmas.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 25,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shuffle_aux (a: sha2_alg) (hash: words_state a) (block: block_w a) : Tot (words_state a) | [] | Spec.SHA2.Lemmas.shuffle_aux | {
"file_name": "specs/lemmas/Spec.SHA2.Lemmas.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Spec.Hash.Definitions.sha2_alg ->
hash: Spec.Hash.Definitions.words_state a ->
block: Spec.SHA2.block_w a
-> Spec.Hash.Definitions.words_state a | {
"end_col": 68,
"end_line": 60,
"start_col": 2,
"start_line": 60
} |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.