yonghong-song created this revision. yonghong-song added reviewers: ast, anakryiko. Herald added a project: clang. Herald added a subscriber: cfe-commits.
The patch is still work in progress. The signature: u64 __builtin_bitfield_info(bitfield_member_access) return value: ret >> 16 : bitfield offset from the start of the struct (ret >> 15) & 0x1 : bitfield signness ret & (1 << 15 - 1) : bitfield size The information is extracted from: https://clang.llvm.org/doxygen/structclang_1_1CodeGen_1_1CGBitFieldInfo.html An example: -bash-4.4$ cat bit.c struct s { int a; long long p1; int p2; int b1:2; int b2:10; int b3:2; int b4:10; int b5:3; int d; }; struct s *a; int main () { unsigned long long x = __builtin_bitfield_info(a->b3); printf("offset = %lld, sign = %lld, size = %lld\n", x >> 16, (x >> 15) & 1, x & ((1 << 15) - 1)); return 0; } -bash-4.4$ clang -O2 -g bit.c -bash-4.4$ ./a.out offset = 172, sign = 1, size = 2 -bash-4.4$ This builtin returns necessary information to user space to extract values from the bitfields. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D67980 Files: clang/include/clang/Basic/Builtins.def clang/lib/CodeGen/CGBuiltin.cpp clang/lib/Sema/SemaChecking.cpp Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -1429,6 +1429,19 @@ if (SemaBuiltinPreserveAI(*this, TheCall)) return ExprError(); break; + case Builtin::BI__builtin_bitfield_info: { + if (checkArgCount(*this, TheCall, 1)) + return ExprError(); + + Expr *Arg = TheCall->getArg(0); + if (Arg->getType()->getAsPlaceholderType()) + return ExprError(); + if (Arg->IgnoreParens()->getObjectKind() != OK_BitField) + return ExprError(); + + TheCall->setType(Context.UnsignedLongLongTy); + break; + } case Builtin::BI__builtin_call_with_static_chain: if (SemaBuiltinCallWithStaticChain(*this, TheCall)) return ExprError(); Index: clang/lib/CodeGen/CGBuiltin.cpp =================================================================== --- clang/lib/CodeGen/CGBuiltin.cpp +++ clang/lib/CodeGen/CGBuiltin.cpp @@ -1882,6 +1882,13 @@ IsInPreservedAIRegion = false; return RValue::get(Res); } + case Builtin::BI__builtin_bitfield_info: { + const CGBitFieldInfo &Info = EmitLValue(E->getArg(0)).getBitFieldInfo(); + uint64_t Val = ((Info.StorageOffset.getQuantity() * 8 + Info.Offset) << 16) | + (Info.IsSigned << 15) | + Info.Size; + return RValue::get(ConstantInt::get(Int64Ty, Val)); + } case Builtin::BI__builtin_cimag: case Builtin::BI__builtin_cimagf: Index: clang/include/clang/Basic/Builtins.def =================================================================== --- clang/include/clang/Basic/Builtins.def +++ clang/include/clang/Basic/Builtins.def @@ -1469,6 +1469,7 @@ BUILTIN(__builtin_char_memchr, "c*cC*iz", "n") BUILTIN(__builtin_dump_struct, "ivC*v*", "tn") BUILTIN(__builtin_preserve_access_index, "v.", "t") +BUILTIN(__builtin_bitfield_info, "LLUi.", "t") // Safestack builtins BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn")
Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -1429,6 +1429,19 @@ if (SemaBuiltinPreserveAI(*this, TheCall)) return ExprError(); break; + case Builtin::BI__builtin_bitfield_info: { + if (checkArgCount(*this, TheCall, 1)) + return ExprError(); + + Expr *Arg = TheCall->getArg(0); + if (Arg->getType()->getAsPlaceholderType()) + return ExprError(); + if (Arg->IgnoreParens()->getObjectKind() != OK_BitField) + return ExprError(); + + TheCall->setType(Context.UnsignedLongLongTy); + break; + } case Builtin::BI__builtin_call_with_static_chain: if (SemaBuiltinCallWithStaticChain(*this, TheCall)) return ExprError(); Index: clang/lib/CodeGen/CGBuiltin.cpp =================================================================== --- clang/lib/CodeGen/CGBuiltin.cpp +++ clang/lib/CodeGen/CGBuiltin.cpp @@ -1882,6 +1882,13 @@ IsInPreservedAIRegion = false; return RValue::get(Res); } + case Builtin::BI__builtin_bitfield_info: { + const CGBitFieldInfo &Info = EmitLValue(E->getArg(0)).getBitFieldInfo(); + uint64_t Val = ((Info.StorageOffset.getQuantity() * 8 + Info.Offset) << 16) | + (Info.IsSigned << 15) | + Info.Size; + return RValue::get(ConstantInt::get(Int64Ty, Val)); + } case Builtin::BI__builtin_cimag: case Builtin::BI__builtin_cimagf: Index: clang/include/clang/Basic/Builtins.def =================================================================== --- clang/include/clang/Basic/Builtins.def +++ clang/include/clang/Basic/Builtins.def @@ -1469,6 +1469,7 @@ BUILTIN(__builtin_char_memchr, "c*cC*iz", "n") BUILTIN(__builtin_dump_struct, "ivC*v*", "tn") BUILTIN(__builtin_preserve_access_index, "v.", "t") +BUILTIN(__builtin_bitfield_info, "LLUi.", "t") // Safestack builtins BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn")
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits