Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cfrontend/C2C.ml
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,9 @@ let rec convertExpr env e =
ewrap (Ctyping.evalof l)

| C.EConst(C.CInt(i, k, _)) ->
let sg = if Cutil.is_signed_ikind k then Signed else Unsigned in
if Cutil.sizeof_ikind k = 8
let sz = Cutil.sizeof_ikind k in
let sg = if sz < 4 || Cutil.is_signed_ikind k then Signed else Unsigned in
if sz = 8
then Ctyping.econst_long (convertInt64 i) sg
else Ctyping.econst_int (convertInt32 i) sg
| C.EConst(C.CFloat(f, k)) ->
Expand Down
32 changes: 18 additions & 14 deletions cfrontend/Ctypes.v
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Definition attr_of_type (ty: type) :=
| Tunion id a => a
end.

(** Change the top-level attributes of a type *)
(** Change the top-level attributes of a type. *)

Definition change_attributes (f: attr -> attr) (ty: type) : type :=
match ty with
Expand All @@ -143,25 +143,29 @@ Definition change_attributes (f: attr -> attr) (ty: type) : type :=
| Tunion id a => Tunion id (f a)
end.

(** Erase the top-level attributes of a type *)
(** Erase the top-level attributes of a type. *)

Definition remove_attributes (ty: type) : type :=
change_attributes (fun _ => noattr) ty.

(** Add extra attributes to the top-level attributes of a type *)
(** Set the volatile attribute on a type.
For array types, the volatile attribute is also applied to the element type. *)

Definition attr_union (a1 a2: attr) : attr :=
{| attr_volatile := a1.(attr_volatile) || a2.(attr_volatile);
attr_alignas :=
match a1.(attr_alignas), a2.(attr_alignas) with
| None, al => al
| al, None => al
| Some n1, Some n2 => Some (N.max n1 n2)
end
|}.
Definition make_attr_volatile (a: attr) : attr :=
{| attr_volatile := true; attr_alignas := a.(attr_alignas) |}.

Definition merge_attributes (ty: type) (a: attr) : type :=
change_attributes (attr_union a) ty.
Fixpoint make_volatile (ty: type) : type :=
match ty with
| Tvoid => ty
| Tint sz si a => Tint sz si (make_attr_volatile a)
| Tlong si a => Tlong si (make_attr_volatile a)
| Tfloat sz a => Tfloat sz (make_attr_volatile a)
| Tpointer elt a => Tpointer elt (make_attr_volatile a)
| Tarray elt sz a => Tarray (make_volatile elt) sz (make_attr_volatile a)
| Tfunction args res cc => ty
| Tstruct id a => Tstruct id (make_attr_volatile a)
| Tunion id a => Tunion id (make_attr_volatile a)
end.

(** Maximal size in bits of a bitfield of type [sz]. *)

Expand Down
8 changes: 2 additions & 6 deletions cfrontend/Ctyping.v
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ Definition ptrdiff_t : type :=

(** The type of a member of a composite (struct or union).
The "volatile" attribute carried by the composite propagates
to the type of the member, but not the "alignas" attribute. *)

Definition attr_add_volatile (vol: bool) (a: attr) :=
{| attr_volatile := a.(attr_volatile) || vol;
attr_alignas := a.(attr_alignas) |}.
to the type of the member, but not the other attributes. *)

Definition type_of_member (a: attr) (f: ident) (m: members) : res type :=
do ty <- field_type f m;
OK (change_attributes (attr_add_volatile a.(attr_volatile)) ty).
OK (if a.(attr_volatile) then make_volatile ty else ty).

(** Type-checking of arithmetic and logical operators *)

Expand Down
16 changes: 8 additions & 8 deletions cparser/Ceval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,6 @@ let binop env op tyop tyres ty1 v1 ty2 v2 =
comparison env (<=) None tyop v1 v2
| Oge ->
comparison env (>=) None tyop v1 v2
| Ologand ->
if boolean_value v1
then if boolean_value v2 then I 1L else I 0L
else I 0L
| Ologor ->
if boolean_value v1
then I 1L
else if boolean_value v2 then I 1L else I 0L
| _ -> raise Notconst
(* force normalization of result, e.g. of double to float *)
in cast env tyres res
Expand All @@ -259,6 +251,14 @@ let rec expr env e =
raise Notconst
| EUnop(op, e1) ->
unop env op e.etyp e1.etyp (expr env e1)
| EBinop(Ologand, e1, e2, ty) ->
if boolean_value (expr env e1)
then if boolean_value (expr env e2) then I 1L else I 0L
else I 0L
| EBinop(Ologor, e1, e2, ty) ->
if boolean_value (expr env e1)
then I 1L
else if boolean_value (expr env e2) then I 1L else I 0L
| EBinop(op, e1, e2, ty) ->
binop env op ty e.etyp e1.etyp (expr env e1) e2.etyp (expr env e2)
| EConditional(e1, e2, e3) ->
Expand Down
4 changes: 1 addition & 3 deletions cparser/Lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,7 @@ and singleline_comment = parse
| Pre_parser.STRING_LITERAL (enc', str', loc') ->
ignore (Queue.pop tokens);
let (enc'', str'') = doConcat enc' str' in
if str'' <> []
then (combine_encodings loc enc enc'', str @ str'')
else (enc, str)
(combine_encodings loc enc enc'', str @ str'')
| _ -> (enc, str)
| exception Queue.Empty -> (enc, str)
in
Expand Down
68 changes: 42 additions & 26 deletions driver/Interp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -295,33 +295,49 @@ external format_int64: string -> int64 -> string
= "caml_int64_format"

let format_value m flags length conv arg =
match conv.[0], length, arg with
| ('d'|'i'|'u'|'o'|'x'|'X'|'c'), (""|"h"|"hh"|"l"|"z"|"t"), Vint i ->
format_int32 (flags ^ conv) (camlint_of_coqint i)
| ('d'|'i'|'u'|'o'|'x'|'X'|'c'), (""|"h"|"hh"|"l"|"z"|"t"), _ ->
"<int argument expected>"
| ('d'|'i'|'u'|'o'|'x'|'X'), ("ll"|"j"), Vlong i ->
format_int64 (flags ^ conv) (camlint64_of_coqint i)
| ('d'|'i'|'u'|'o'|'x'|'X'), ("ll"|"j"), _ ->
"<long long argument expected"
| ('f'|'e'|'E'|'g'|'G'|'a'), (""|"l"), Vfloat f ->
format_float (flags ^ conv) (camlfloat_of_coqfloat f)
| ('f'|'e'|'E'|'g'|'G'|'a'), "", _ ->
"<float argument expected"
| 's', "", Vptr(blk, ofs) ->
begin match extract_string m blk ofs with
| Some s -> s
| None -> "<bad string>"
match conv.[0], length with
| ('d'|'i'|'u'|'o'|'x'|'X'|'c'), (""|"h"|"hh"|"l") ->
begin match arg with
| Vint i -> format_int32 (flags ^ conv) (camlint_of_coqint i)
| _ -> "<int argument expected>"
end
| 's', "", _ ->
"<pointer argument expected>"
| 'p', "", Vptr(blk, ofs) ->
Printf.sprintf "<%ld%+ld>" (P.to_int32 blk) (camlint_of_coqint ofs)
| 'p', "", Vint i ->
format_int32 (flags ^ "x") (camlint_of_coqint i)
| 'p', "", _ ->
"<int or pointer argument expected>"
| _, _, _ ->
| ('d'|'i'|'u'|'o'|'x'|'X'), ("ll"|"j") ->
begin match arg with
| Vlong i -> format_int64 (flags ^ conv) (camlint64_of_coqint i)
| _ -> "<long long argument expected"
end
| ('d'|'i'|'u'|'o'|'x'|'X'), ("z"|"t") ->
begin match arg with
| Vint i -> format_int32 (flags ^ conv) (camlint_of_coqint i)
| Vlong i -> format_int64 (flags ^ conv) (camlint64_of_coqint i)
| _ -> "<size argument expected>"
end
| ('f'|'e'|'E'|'g'|'G'|'a'), (""|"l") ->
begin match arg with
| Vfloat f -> format_float (flags ^ conv) (camlfloat_of_coqfloat f)
| _ -> "<float argument expected"
end
| 's', "" ->
begin match arg with
| Vptr(blk, ofs) ->
begin match extract_string m blk ofs with
| Some s -> s
| None -> "<bad string>"
end
| _ -> "<pointer argument expected>"
end
| 'p', "" ->
begin match arg with
| Vptr(blk, ofs) ->
Printf.sprintf "<%ld%+ld>" (P.to_int32 blk) (camlint_of_coqint ofs)
| Vint i ->
format_int32 (flags ^ "x") (camlint_of_coqint i)
| Vlong i ->
format_int64 (flags ^ "x") (camlint64_of_coqint i)
| _ ->
"<int or pointer argument expected>"
end
| _, _ ->
"<unrecognized format>"

let do_printf m fmt args =
Expand Down
1 change: 0 additions & 1 deletion extraction/extraction.v
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ Cd "extraction".
Separate Extraction
Compiler.transf_c_program Compiler.transf_cminor_program
Cexec.do_initial_state Cexec.do_step Cexec.at_final_state
Ctypes.merge_attributes Ctypes.remove_attributes
Ctypes.build_composite_env Ctypes.layout_struct
Initializers.transl_init Initializers.constval
Csyntax.Eindex Csyntax.Epreincr Csyntax.Eselection
Expand Down
Loading