I have a LLVM Module object which contains a particular function that I would like to rename. Is there any way of simply changing the name of a Function?
Given a module, you can look up a specific function by name using the getFunction method, or you can iterate over all the functions in the module using begin() and end(). From there, Function inherits from Value, so you can just use the setName method change the name. This will also automatically update all the references and calls to it inside the same module.
Related
I'm writing a function pass in LLVM and need to call the method Module::getOrInsertFunction. I need to access the module of the current function. How do I get it?
You can use getParent() function: http://llvm.org/docs/doxygen/html/classllvm_1_1GlobalValue.html#a9e1fc23a17e97d2d1732e753ae9251ac
Please refer to: http://llvm.org/docs/WritingAnLLVMPass.html
According to the documentation here,
To be explicit, FunctionPass subclasses are not allowed to:
1. Inspect or modify a Function other than the one currently being processed.
2. Add or remove Functions from the current Module.
3. Add or remove global variables from the current Module.
4. Maintain state across invocations of runOnFunction (including global data).
So, you cannot call getOrInsertFunction from inside a FunctionPass. You will need a ModulePass
I am using SDL2_mixer library, but I believe that the question should hold for the general case also.
Currently, a function that I would like to use, Mix_HookMusicFinished(void (*music_finished)(void)) has a set callback to the global scope for a C style function. However, I would like to have that callback be set to a member function within my own class void CMusic::musicFinished() without having the need for a function in global scope.
Is there anyway to do this? Something like Mix_HookMusicFinished(musicFinished) would be great, but that directly has an error of argument of type "void (CMusic::*)()" is incompatible with parameter of type "void (*)()"
You need to make a "wrapper" function. However, the problem here is that you also need to be able to find the CMusic object that you want to "finish" - this is really what the crux of
argument of type ... is incompatible with ...
is all about. Since there is no way to pass a parameter to the musicFinished object, you will need some other way of "finding" the CMusic object.
If we assume there is a way to do that, then something like this would work:
class CMusic
{
...
public:
...
static void musicFinishedWrapper();
void musicFinished();
...
};
void CMusic::musicFinishedWrapper()
{
CMusic* music = getTheMusicSomehow(); // No idea how you do this - depends on your code.
music->musicFinished();
}
The reason you have to have a CMusic object is that your musicFinished expects a (hidden) this pointer argument - which is the value in music in my little function.
You could move musicFinished to your CMusic class and declare it as a static class method. static class methods aren't called on an object; they therefore don't have an implicit argument to specify the value of the this pointer, and they therefore can have the same signature as freestanding functions. You additionally can make it private to prevent anything but CMusic from using it.
However, since your musicFinished method currently works as a freestanding function and therefore probably doesn't need access to CMusic's protected or private members, and since your efforts to limit its scope presumably means that you don't want other things to call it, I personally would leave your musicFinished function as freestanding but declare it as static (or move it to an anonymous namespace, if you prefer) within the CMusic source (.cpp or .cc) file. Doing so would restrict its scope to the source file (the "compilation unit"). An advantage over a private, static class method is that it does not need to be exposed at all in a header file, so it is in some sense more private.
I have two projects under one solution in VS2010.Project Application is dependent on project Module.
I can't access application pointer in Module due to dependency.
But I know using a callback function I can get application pointer in Module.How to do it?
You can just extern a pointer from Module in one of Module's headers. Otherwise, you just call a function and return a pointer. Declare this function in a header and then call it from your application.
I am writing an LLVM pass, where I clone some functions by calling llvm::CloneFunction. Now I also want to insert those functions in the module. How can I do that?
Create a new function with Function::Create or by other means. A Function's constructors accept a module into which to insert the new function.
Clone a function into that new function with CloneFunctionInto, or just copy over the BBs you need.
CloneFunction will automatically insert the new Function into the old Function's module. From the doxygen:
Return a copy of the specified function and add it to that function's module.
You can use CloneFunction and insert it into the module afterwards like so
Function* duplicateFunction = CloneFunction(F, VMap,
/*ModuleLevelChanges=*/false);
F->getParent()->getFunctionList().push_back(duplicateFunction);
Example stolen from PartialInlining.cpp in the llvm source.
I have been working on a simple component based c++ and lua game engine. Currently all the subsystems like audio and physics can be called from lua with there own module using luaL_newlib.
The problem I am having is how can a lua script call functions to modify its entity. Like SetPosition() is obviously going to need to be called on the instance of an entity rather than static functions. So is there a way I can have these instanced functions.
One idea I had was each entity registering functions for itself with a module name that is unique to it. So it would look like entity1.Setposition and entity2.Setposition. Would it be possible to register, effectively duplicate functions like this?
Or is there another way to have instanced functions like this so components can modify the entity that they are a part of?
If I have explained myself badly please do say so I will gladly provide more information
Lua has syntactic sugar for "instance methods." If you call a function with a colon instead of a dot, like this:
entity1:SetPosition(...)
then Lua rewrites it to this:
entity1.SetPosition(entity, ...)
In other words, it adds an implicit first argument which is the object that the method is being called on. On the C side, you just see an extra argument. This is the preferred way to make instance methods.
Two other noteworthy things: the rewrite above isn't exactly what happens. If you do something like this:
SomeFunctionReturningEntity():SetPosition(...)
it doesn't rewrite it to
SomeFunctionReturningEntity().SetPosition(SomeFunctionReturningEntity(), ...)
It actually just calls the function once, as you'd expect.
Also, if you're writing an instance method from Lua itself, not from C, there's syntactic sugar for declaring one:
function entity1:SetPosition(...)
end
This is equivalent to:
function entity1.SetPosition(self, ...)
end
i.e. it adds the implicit first argument to the function, and calls it self.
(Incidentally, that's also equivalent to this:
entity1.SetPosition = function(self, ...)
end
Technically, even declaring a function inside of a table is syntactic sugar.)