Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,7 @@ let initGccBuiltins () : unit =
try
assert (sizeInBits mod 8 = 0);
let sizeInBytes = sizeInBits / 8 in
let sizedIntType = TInt (intKindForSize sizeInBytes false, []) in
let sizedIntType = TInt (intKindForSize sizeInBytes true, []) in
let name = Printf.sprintf "__builtin_bswap%d" sizeInBits in
H.add h name (sizedIntType, [ sizedIntType ], false)
with Not_found ->
Expand Down
18 changes: 18 additions & 0 deletions src/frontc/cabs2cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4752,6 +4752,24 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
prestype := intType
| _ -> ignore (warn "Invalid call to builtin_types_compatible_p");
end
else if fv.vname = "__builtin_bswap16" && asconst && isEmpty (!prechunk ()) then (* to support pure switch cases in Linux kernel *)
begin
match !pargs with
| [ arg ] -> begin
piscall := false;
prestype := TInt (IUShort, []); (* TODO: lookup via machdep? *)
let arg = CastE (IntegerPromotion, intType, arg) in (* bitwise operators will promote uint16 to int *) (* TODO: consider machdep? *)
pres :=
CastE (Internal, !prestype, (* force promoted int back to uint16 *)
BinOp (BOr,
BinOp (Shiftlt, arg, integer 8, intType),
BinOp (Shiftrt, arg, integer 8, intType),
intType)
);
end
| _ ->
ignore (warn "Invalid call to builtin_bswap16");
end
end
| _ -> ()
);
Expand Down
13 changes: 13 additions & 0 deletions test/small1/builtin_bswap_const.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "testharness.h"

int main (void)
{
switch (1) {
case (__builtin_bswap16(256)):
break;
default:
E(1);
}

SUCCESS;
}
1 change: 1 addition & 0 deletions test/testcil.pl
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ sub addToGroup {
addTest("testrun/builtin4 ");
addTest("test/builtin5 ");
addTest("test/builtin6 ");
addTest("testrun/builtin_bswap_const ");
addTest("test/sync-1 _GNUCC=1");
addTest("test/sync-2 _GNUCC=1");
addTest("test/sync-3 _GNUCC=1");
Expand Down