Skip to content

Commit 6f6104d

Browse files
committed
CHC: introduce concept of scalar struct
1 parent b3246d8 commit 6f6104d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CodeHawk/CHC/cchlib/cCHTypesUtil.ml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,46 @@ let is_volatile_type (t:typ) =
831831
(get_typ_attributes t)
832832

833833

834+
let rec is_scalar_struct_type (t: typ): bool =
835+
match fenv#get_type_unrolled t with
836+
| TComp (ckey, _) ->
837+
let cinfo = fenv#get_comp ckey in
838+
List.for_all (fun finfo ->
839+
match finfo.ftype with
840+
| TInt _
841+
| TFloat _
842+
| TPtr _ -> true
843+
| TComp _ -> is_scalar_struct_type finfo.ftype
844+
| _ -> false) cinfo.cfields
845+
| _ -> false
846+
847+
848+
let rec get_scalar_struct_offsets (t: typ): offset list =
849+
if is_scalar_struct_type t then
850+
match fenv#get_type_unrolled t with
851+
| TComp (ckey, _) ->
852+
let cinfo = fenv#get_comp ckey in
853+
List.fold_left (fun acc finfo ->
854+
match finfo.ftype with
855+
| TInt _
856+
| TFloat _
857+
| TPtr _ -> (Field ((finfo.fname, finfo.fckey), NoOffset)) :: acc
858+
| TComp _ ->
859+
let suboffsets = get_scalar_struct_offsets finfo.ftype in
860+
let suboffsets =
861+
List.map (fun suboffset ->
862+
Field ((finfo.fname, finfo.fckey), suboffset))
863+
(List.rev suboffsets) in
864+
suboffsets @ acc
865+
| _ -> acc) [] cinfo.cfields
866+
| _ -> []
867+
else
868+
raise
869+
(CCHFailure
870+
(LBLOCK [
871+
STR "Type is not a scalar struct: "; typ_to_pretty t]))
872+
873+
834874
let constant_value (c:constant) =
835875
match c with
836876
| CInt (i64, _, _) ->

CodeHawk/CHC/cchlib/cCHTypesUtil.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ val is_void_type: typ -> bool
110110

111111
val is_struct_type: typ -> bool
112112

113+
val is_scalar_struct_type: typ -> bool
114+
113115
val is_void_ptr_type: typ -> bool
114116

115117
val is_function_type: typ -> bool
@@ -126,6 +128,8 @@ val is_enum_constant: string -> exp -> bool
126128

127129
val get_field_offset: offset -> string * int
128130

131+
val get_scalar_struct_offsets: typ -> offset list
132+
129133
val add_offset: offset -> offset -> offset
130134

131135
val get_field_lval_exp: exp -> string * int

0 commit comments

Comments
 (0)