aboutsummaryrefslogtreecommitdiffstats
path: root/exportclight
diff options
context:
space:
mode:
authorXavier Leroy <xavier.leroy@college-de-france.fr>2021-05-17 18:07:02 +0200
committerXavier Leroy <xavier.leroy@college-de-france.fr>2021-08-22 13:29:00 +0200
commit47fae389c800034e002c9f8a398e9adc79a14b81 (patch)
tree210933a5a526afe0469a66f59861c13d687c733e /exportclight
parenta94edc576ca2c288c66f710798ab2ada3c485a40 (diff)
downloadcompcert-kvx-47fae389c800034e002c9f8a398e9adc79a14b81.tar.gz
compcert-kvx-47fae389c800034e002c9f8a398e9adc79a14b81.zip
Native support for bit fields (#400)
This big PR adds support for bit fields in structs and unions to the verified part of CompCert, namely the CompCert C and Clight languages. The compilation of bit field accesses to normal integer accesses + shifts and masks is done and proved correct as part of the Cshmgen pass. The layout of bit fields in memory is done by the functions in module Ctypes. It follows the ELF ABI layout algorithm. As a bonus, basic soundness properties of the layout are shown, such as "two different bit fields do not overlap" or "a bit field and a regular field do not overlap". All this replaces the previous emulation of bit fields by source-to-source rewriting in the unverified front-end of CompCert (module cparse/Bitfield.ml). This emulation was prone to errors (see nonstandard layout instead. The core idea for the PR is that expressions in l-value position denote not just a block, a byte offset and a type, but also a bitfield designator saying whether all the bits of the type are accessed (designator Full) or only some of its bits (designator Bits). Designators of the Bits kind appear when the l-value is a bit field access; the bit width and bit offset in Bits are computed by the functions in Ctypes that implement the layout algorithm. Consequently, both in the semantics of CompCert C and Clight and in the SimplExpr, SimplLocals and Cshmgen compilation passes, pairs of a type and a bitfield designator are used in a number of places where a single type was used before. The introduction of bit fields has a big impact on static initialization (module cfrontend/Initializers.v), which had to be rewritten in large part, along with its soundness proof (cfrontend/Initializersproof.v). Both static initialization and run-time manipulation of bit fields are tested in test/abi using differential testing against GCC and randomly-generated structs. This work exposed subtle interactions between bit fields and the volatile modifier. Currently, the volatile modifier is ignored when accessing a bit field (and a warning is printed at compile-time), just like it is ignored when accessing a struct or union as a r-value. Currently, the natural alignment of bit fields and their storage units cannot be modified with the aligned attribute. _Alignas on bit fields is rejected as per C11, and the packed modifier cannot be applied to a struct containing bit fields.
Diffstat (limited to 'exportclight')
-rw-r--r--exportclight/ExportClight.ml31
1 files changed, 30 insertions, 1 deletions
diff --git a/exportclight/ExportClight.ml b/exportclight/ExportClight.ml
index 474a1bd8..ced07427 100644
--- a/exportclight/ExportClight.ml
+++ b/exportclight/ExportClight.ml
@@ -161,6 +161,22 @@ let attribute p a =
a.attr_volatile
(print_option coqN) a.attr_alignas
+(* Raw int size and signedness *)
+
+let intsize p sz =
+ fprintf p "%s"
+ (match sz with
+ | I8 -> "I8"
+ | I16 -> "I16"
+ | I32 -> "I32"
+ | IBool -> "IBool")
+
+let signedness p sg =
+ fprintf p "%s"
+ (match sg with
+ | Signed -> "Signed"
+ | Unsigned -> "Unsigned")
+
(* Types *)
let rec typ p t =
@@ -444,11 +460,24 @@ let print_ident_globdef p = function
(* Composite definitions *)
+let print_member p = function
+ | Member_plain (id, ty) ->
+ fprintf p "@[<hov 2>Member_plain@ %a@ %a@]"
+ ident id typ ty
+ | Member_bitfield (id, sz, sg, a, width, pad) ->
+ fprintf p "@[<hov 2>Member_bitfield@ %a@ %a@ %a@ %a@ %a@ %B@]"
+ ident id
+ intsize sz
+ signedness sg
+ attribute a
+ coqZ width
+ pad
+
let print_composite_definition p (Composite(id, su, m, a)) =
fprintf p "@[<hv 2>Composite %a %s@ %a@ %a@]"
ident id
(match su with Struct -> "Struct" | Union -> "Union")
- (print_list (print_pair ident typ)) m
+ (print_list print_member) m
attribute a
(* The prologue *)