Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new types in LLVM (in particular a pointer to function type)

Tags:

llvm

I'd like to create a following Type,

  void (i8*)*

I tried using Type class to create above type, But I did't find any direct method to do the same.
Someone Please suggest me a method to create the above type.
Thanks in advance.

like image 943
shashikiran Avatar asked Feb 24 '12 16:02

shashikiran


1 Answers

If you mean i8** (pointer to pointer to i8), then:

// This creates the i8* type
PointerType* PointerTy = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
// This creates the i8** type
PointerType* PointerPtrTy = PointerType::get(PointerTy, 0);

If you need a pointer to a function returning nothing and taking i8*, then:

// This creates the i8* type
PointerType* PointerTy = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);

// Create a function type. Its argument types are passed as a vector
std::vector<Type*>FuncTy_args;
FuncTy_args.push_back(PointerTy);                 // one argument: char*
FunctionType* FuncTy = FunctionType::get(
  /*Result=*/Type::getVoidTy(mod->getContext()),  // returning void
  /*Params=*/FuncTy_args,                         // taking those args
  /*isVarArg=*/false);

// Finally this is the pointer to the function type described above
PointerType* PtrToFuncTy = PointerType::get(FuncTy, 0);

A more general answer would be: you can use the LLVM C++ API backend to generate the C++ code needed to create any kind of IR. This can be conveniently done with the online LLVM demo - http://llvm.org/demo/ - this is how I generated the code for this answer.

like image 74
Eli Bendersky Avatar answered Sep 30 '22 11:09

Eli Bendersky