Adding new data type

Hello Group,

I am trying to add a new data type to clang. Can someone tell me the important files and classes to look into and how to start in order to lex and parse and generate an AST node to this new data type.

Thanks

First you have to add the new type to the Type hierarchy. See
include/llvm/Type.h and its derived classes, find a suitable spot and
implement it.

Then you have to change the type handlers in clang to, based on the
source's type, add your custom type to the AST.

I'm not sure that LLVM's subsequent passes will recognize all
properties of your new type, but I guess if you build it well enough,
it might go on unnoticed.

Out of curiosity, what is this new type you need?

cheers,
--renato

http://systemcall.org/

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm

I think you mean include/clang/AST/Type.h. But changes to the type system can be very subtle; Kalyan, if you can be more specific about what type you're thinking of adding, we can give better advice. There isn't much internal documentation on adding a type.

John.

Hi guys,

Thanks for the replies. I am trying to add support to a new architecture called Line Associative Registers. It has vector registers in it. I would like to add a vector data type such as int[4] which could access a register with a width of 4 integers in it (max). Since it has to be mentioned in the source as

int[4] = {1, 2, 3, 4}

I am not sure about how to proceed. This form looks like an array with 4 elements but I want it to be a datatype.
I was looking into targetinfo.h and initpreprocessor.cpp. it looks like I can add a new type here or change the existing one. And about the type.h in ClangAST target, will it work if I added this type here.

Any advices about how to proceed would be really appreciated.

Thanks again.

Clang already has vector types (VectorType) and "extended" vector types (ExtVectorType). They're created using attributes, e.g.,

  typedef int v4si __attribute__ ((vector_size (16)));

You could probably just re-use these vector types, possibly tweaking their semantics a bit, for your architecture.

  - Doug

Thanks for the replies. I am trying to add support to a new architecture called Line Associative Registers. It has vector registers in it. I would like to add a vector data type such as int[4] which could access a register with a width of 4 integers in it (max).

Clang already has support for vector types based off the gcc vector_size and ext_vector attributes, as well as some support for altivec-style vectors; before you start hacking in a new type with associated syntax, I would suggesting investigating whether any of those would be sufficient or at least provide a better baseline to begin with.

Since it has to be mentioned in the source as

int[4] = {1, 2, 3, 4}

I am not sure about how to proceed. This form looks like an array with 4 elements but I want it to be a datatype.

Do you mean something like this?

  int myVector[4] = { 1, 2, 3, 4 };

Making that a vector will break existing C code.

Unless you *really* need new syntax, I would strongly suggest sticking with one of the existing vector types.

John.

hi,

Thanks for the replies. @john : I am looking into “typedef int int_4 attribute((ext_vector_type(4)));” way of adding the vector data type. I do not want to change the array type to vector type. Right now I am trying to add this as a header file into the clang architecture so that it won’t look bad and at the same time it would make the new data type “int_4” look like its a built in data type in clang when we write code at source level. I am still trying to figure it out. It looks like headers added to clang/lib/Headers are not recognized as I thought they would be. I am using visual studio 2008 on windows 7.

Thanks.

Hi,

kalyan ponnala schrieb:

Thanks for the replies. I am trying to add support to a new architecture called Line Associative Registers. It has vector registers in it. I would like to add a vector data type such as int[4] which could access a register with a width of 4 integers in it (max). Since it has to be mentioned in the source as

int[4] = {1, 2, 3, 4}

I am not sure about how to proceed. This form looks like an array with 4 elements but I want it to be a datatype.

I had a similiar problem when I wanted to add SSE data types. First I tried adding a new type to the type system too, but this was clearly the wrong way. I recommend adding just a typedef, which then can act like a built-in type. See my source code below (vecType should be int, float, double or the like).

QualType StmtCollector::getSimdType(const QualType& vecType, unsigned NumElts)
{
  // Ctx is a ASTContext
  BuiltinType *baseType = dyn_cast<BuiltinType>(Ctx.getCanonicalType(vecType).getTypePtr());
  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");

  TypedefDecl*& simdTypedef = m_SimdTypedefDecl[tSimdTypedefDeclKey(baseType, NumElts)];
  if (simdTypedef == 0)
  {
    const char* pTypeName;
    if (baseType == Ctx.FloatTy.getTypePtr() && NumElts == 4)
    {
      pTypeName = "__m128";
    }
    else if (baseType == Ctx.DoubleTy.getTypePtr() && NumElts == 2)
    {
      pTypeName = "__m128d";
    }
    else
    {
      assert(0 && "simd type unsupported"); }

    simdTypedef = TypedefDecl::Create(Ctx,
      Ctx.getTranslationUnitDecl(),
      SourceLocation(), &Ctx.Idents.get(pTypeName),
      Ctx.CreateTypeSourceInfo(Ctx.getVectorType(vecType, NumElts, false, false)));
  }

  return Ctx.getTypedefType(simdTypedef);
}

Hth
Olaf Krzikalla

Hi guys,

I was trying to add this header file named “NEW_DATATYPES.h” with the following information into clang.

typedef int int_2 attribute((ext_vector_type(2)));

I added it to clang\lib\Headers. And in the source program when I try to do:

code-1:

int main()
{
int_2 abc = { 1, 2 };
int_2 bbc = { 3, 4} ;
abc.x = bbc.y;
return 0;
}

clang gives me an error. saying that it does not identify int_2.
When I add the header file’s name “NEW_DATATYPES.h” to the source like below, clang compiles the code fine.

code2:

#include “NEW_DATATYPES.h”
int main()
{
int_2 abc = { 1, 2 };
int_2 bbc = { 3, 4} ;
abc.x = bbc.y;
return 0;
}

But what I want is that the clang should be able to compile the code-1 fine where I dont have to add the header file explicitly. Can someone please tell me how to do this without actually making any changes to the existing clang architecture. I am trying not to disturb the code.

Thanks a ton.

Kalyan

Hi guys,

I was trying to add this header file named "NEW_DATATYPES.h" with the
following information into clang.

typedef int int_2 __attribute__((ext_vector_type(2)));

I added it to clang\lib\Headers. And in the source program when I try to do:

code-1:

int main()
{
int_2 abc = { 1, 2 };
int_2 bbc = { 3, 4} ;
abc.x = bbc.y;
return 0;
}

clang gives me an error. saying that it does not identify int_2.
When I add the header file's name "NEW_DATATYPES.h" to the source like
below, clang compiles the code fine.

code2:

#include "NEW_DATATYPES.h"
int main()
{
int_2 abc = { 1, 2 };
int_2 bbc = { 3, 4} ;
abc.x = bbc.y;
return 0;
}

But what I want is that the clang should be able to compile the code-1 fine
where I dont have to add the header file explicitly. Can someone please tell
me how to do this without actually making any changes to the existing clang
architecture. I am trying not to disturb the code.

Pass "-include NEW_DATATYPES.h" on the command-line, or do the
equivalent in the code that calls clang::InitializePreprocessor, or
modify lib/Frontend/InitializePreprocessor.cpp to either add the
include or add the typedef directly.

-Eli