diff options
author | Xavier Leroy <xavierleroy@users.noreply.github.com> | 2017-02-01 12:59:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-01 12:59:55 +0100 |
commit | 31f86965bf172fb32f9cca99a292ebdf6cea57b9 (patch) | |
tree | ef78d97a02e2dad0e39ccd345288adf3545bb05e /cparser/Cutil.ml | |
parent | 71fa5147139f85cb0d14ded74b04b39dd52f776b (diff) | |
parent | ed55884ea9749f93ffd67f0734da0907fe338102 (diff) | |
download | compcert-31f86965bf172fb32f9cca99a292ebdf6cea57b9.tar.gz compcert-31f86965bf172fb32f9cca99a292ebdf6cea57b9.zip |
Merge pull request #159 from AbsInt/builtin_offsetof
Implement offsetof via builtin
Diffstat (limited to 'cparser/Cutil.ml')
-rw-r--r-- | cparser/Cutil.ml | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/cparser/Cutil.ml b/cparser/Cutil.ml index f5d5c425..20cdc038 100644 --- a/cparser/Cutil.ml +++ b/cparser/Cutil.ml @@ -533,6 +533,25 @@ let sizeof_struct env members = end in sizeof_rec 0 members +(* Compute the offset of a struct member *) +let offsetof env ty field = + let rec sub acc name = function + | [] -> List.rev acc + | m::rem -> if m.fld_name = name then + List.rev acc + else + sub (m::acc) name rem in + match unroll env ty with + | TStruct (id,_) -> + let str = Env.find_struct env id in + let pre = sub [] field.fld_name str.ci_members in + begin match sizeof_struct env pre, alignof env field.fld_typ with + | Some s, Some a -> + align s a + | _ -> assert false end + | TUnion _ -> 0 + | _ -> assert false + (* Simplified version to compute offsets on structs without bitfields *) let struct_layout env members = let rec struct_layout_rec mem ofs = function |