Create the empty structure with the LLVM C++ API - llvm

How can the empty structure be created using the LLVM C++ API?
In the LLVM IR I would just write {} {}.
It can be used for instance in a return statement: ret {} {}?

Related

Set LLVM assembly output syntax with C++ API

I'm trying to find a way to set the assembly syntax that LLVM outputs (for x86 in particular). Following the Compiling to Object Code section of the Kaleidoscope tutorial, I'm outputting assembly files with
TargetMachine->addPassesToEmitFile(pass, dest, nullptr, llvm::CGFT_AssemblyFile))
But there's no option to set the output syntax.
llc has the command line option --x86-asm-syntax with which you can set the output assembly syntax (for x86), so I'm sure there must be a way to do it.
So is there a way this can be done through LLVM's C++ API?
Here's a few things I found out by reading the LLVM source:
The output assembly syntax is controlled by the AssemblyDialect member in llvm::MCAsmInfo. For x86 the derived classes of llvm::MCAsmInfo can be found in X86MCAsmInfo.h and X86MCAsmInfo.cpp.
The member AssemblyDialect is initialized to the value of AsmWriterFlavor, which is set by the command line option x86-asm-syntax.
As far as I could tell AssemblyDialect isn't referenced anywhere else in the code base, so the only way to set it is by the x86-asm-syntax flag. For that you can use llvm::cl::ParseCommandLineOptions. Minimal example for setting the assembly syntax to intel:
#include <cassert>
#include <llvm/Support/CommandLine.h>
void set_asm_syntax_to_intel()
{
char const *args[] = { "some-exe-name", "--x86-asm-syntax=intel" };
auto const res = llvm::cl::ParseCommandLineOptions(std::size(args), args);
assert(res);
}

How to compile definitions in c++'s header file to IR in LLVM JIT Module?

I am writing my own language using LLVM's JIT, some class will implement by c++, such as List, Map, and so on.
I can compile these c++ files to object file and then add them to JIT for linking, but when i use them in my language, i need to know it's LLVM type, such as:
var data = new List<int>();
data.push(3);
when i implement var data = new List<int>(), i need to know List's struct type, and then alloca memory for it, but it's c++ code, i cannot know it.
So how to create StructType(LLVM IR) of List, in LLVM JIT? I don't what to write it by hand.

Get parent of register in llvm

In llvm used to be this great function (I don't know which version they are using here):
const unsigned* llvm::TargetRegisterInfo::getSuperRegisters(unsigned RegNo)
http://legup.eecg.utoronto.ca/doxygen/classllvm_1_1TargetRegisterInfo.html#90b85b889ff636c6bdd40b7543343473
Unfortunately I am using llvm 3.4, where this function does not exist. Is there something with similar functionality? Or is there an easy workaround to get all the parent registers of a given register?
Should have read the docu more carefully. Here is the answer:
http://llvm.org/docs/doxygen/html/classllvm_1_1MCSuperRegIterator.html
llvm::MCSuperRegIterator expects a physical register in its constructor and then iterates over all its parents.

Generating function pointers in LLVM

I am writing my first LLVM sample. I am trying to build a small LLVM module consisting of a function which takes in a name of a function and returns a pointer to it. The problem is I don't know how to generate function pointers in LLVM. I have got a Function object by calling getDeclaration(...). Is there some way I can get a pointer to it?
Function is a GlobalValue, so it is the pointer by itself. In the meantime, you can use LLVM's C++ backend to generate the C++ API calls which will recreate the IR you're feeding to llc.
For example, feed the following code into http://llvm.org/demo:
void foo(int (*bar)(int));
int factorial(int X);
int main(int argc, char **argv) {
foo(factorial);
}
Make sure you have the "Show LLVM C++ API code" checkbox selected and you'll see the corresponding LLVM IR and the C++ API calls which will recreate it.

How to invoke an Objective-C Block via the LLVM C++ API?

Say, for example, I have an Objective-C compiled Module that contains something like the following:
typedef bool (^BoolBlock)(void);
BoolBlock returnABlock(void)
{
return Block_copy(^bool(void){
printf("Block executing.\n");
return YES;
});
}
...then, using the LLVM C++ API, I load that Module and create a CallInst to call the returnABlock() function:
Function *returnABlockFunction = returnABlockModule->getFunction(std::string("returnABlock"));
CallInst *returnABlockCall = CallInst::Create(returnABlockFunction, "returnABlockCall", entryBlock);
How can I then invoke the Block returned via the returnABlockCall object?
Not an easy answer here, I'm afraid. Blocks are lowered by the front-end into calls into the blocks runtime. In the case of clang, the relevant code is at clang/lib/CodeGen/CGBlocks.[h|cpp].
It would be worth asking on the cfe-dev list if there's a way to factor this code out for reuse in other front-ends.
In C, I just act as if the var I assigned the block to was a function pointer. Using your code as an example, after you assign the result of the function to "returnABlockCall", you could just write:
returnABlockCall();
and it should work.
Warning, this is untested in C++, but I see no reason why it wouldn't work.