[cfe-dev] Extending C to support non power of two bit variables

Eli Friedman eli.friedman at gmail.com
Sat Jun 14 15:20:26 PDT 2008

On Sat, Jun 14, 2008 at 1:43 PM, Nadav Rotem <nadav256 at gmail.com> wrote:
> Hello,
> I would like to extend the C language, using clang, to include variable
> types which are not of power of two bit size.  For example, I would like
> to add the variable type "int5" which will be an integer of 5 bits.
> Can you recommend the best course of action to implement such a
> feature ?

The first bit is actually figuring out how to represent these types in
C code. I'd suggest you do something similar to the existing mode
attribute for types.  (See
for the gcc docs; it shouldn't be too hard to find the implementation
in clang.)  This way, you can do something like "typedef int int5
__attribute__((bitsized_integer(5)));" to get arbitrary integers.
(For the implementation of a new attribute, see

To support these new types for the AST and semantic analysis, you have
to add an additional type to the AST (see Type.h and ASTContext for
how types are implemented), and you need to make sure all the existing
code handles this new integer type appropriately for
promotions/compatibility/etc.  You probably want to look at the C99
standard's rules for integer types and promotion to get an idea of
what is needed for a correct implementation.

For the codegen bit, you can just map the types to the LLVM
arbitrary-width integer types, which should have the right behavior.
The LLVM codegen support for non-power-of-two integer types still
isn't quite mature; however, the situation is steadily improving, so
hopefully it will be stable in a few months.

That's just a rough outline, but I think I included everything
important; if you have any more specific questions, please ask.


More information about the cfe-dev mailing list