How to convert a genericValue to Value in LLVM? - llvm

I'm working in interpreter. I have the address of a variable in memory and I managed to get the real value form it and put it in a genericValue using LoadValueFromMemory function. Now I need to create a StoreInst and want to put this value got in a Value object to use it in the StoreInst. Any idea?

To answer the question in the title, GenericValue is just a type-agnostic container for some value. To create an LLVM Value from it, you need to create a Constant of the appropriate type, and use that value to initialize the constant.
I'm assuming you are referring to getting a value from one module and using it in another unrelated module, otherwise it's not clear to me why you pass through a GenericValue for this:
If the value came from the interpreter side, you don't need to store it in a GenericValue, you can just create a Constant from that value.
If the value came from the LLVM IR side in the same module, you also don't need to load that Value into a GenericValue, you can just use that Value directly as the argument when creating the store instruction.

Related

What is the difference between Lua registry with light userdata and references?

So with the Lua C API you can save a Lua value in the registry and retrieve it later. There are different ways to do it, you can create a variable and use it's pointer as the key in the registry as it's always unique. You would push the pointer as light userdata.
You can also create a reference using LuaL_ref(L, LUA_REGISTRYINDEX). What is the advantage of one over the other? When to use references and when to use pointers?
Also with references, as it is called a reference, if the Lua garbage collector collects the Lua value, will the value in the registry be nil? What if Lua updates the Lua value, will the value in the registry also change?
Lua registry is just another lua table, easily accessible via predefined "special" index. I guess you don't need explanations on how Lua table is different from light userdata.
It doesn't really matter how you will index registry table, as long as you can store that key on C/C++ side. For your convenience there's already functions (luaL_ref/luaL_unref) giving you integer key that is easy to store and move around.
About garbage collection - rules are always the same. As long as value is stored in table that wasn't marked as weak table (registry is not weak table), that value won't be cleared. You must explicitly remove value from the registry.
Changing value will obey normal Lua rules. Assigning new immutable value to some variable won't change value stored in registry, i.e. registry won't follow updates to some variable. But changing content of mutable value (table etc) is ok, since registry and variable will refer same value.
In addition to previous answer:
Differences between Lua lightuserdata and userdata
lightuserdata is a special Lua type (as well as nil, boolean, number, string, table, thread etc.) containing C pointer. Nothing more. You can't assign metatable to lightuserdata. On the contrary, you can assign metatable to userdata type. For example see Lua File operations, where file handle is userdata with methods. f:read("*all") f is userdata the command is equivalent to f.read(f, "*all")
Indexing LUA_REGISTRYINDEX with integer or C pointer
There are two methods that are widely used on registry table.
Create new reference to Lua value with luaL_ref and store the return integer value somewhere in your code. I.e. to access the Lua value you'll need to read C variable holding the reference and index registry table with lua_rawgeti(L, LUA_REGISTRYINDEX, i) where is that integer value. lua_rawseti(L, LUA_REGISTRYINDEX, i) is also possible, but don't try rewrite to a nil value with this method!
You creating a static C variable static int myvar; and then use lua_rawgetp(L, LUA_REGISTRYINDEX, &myvar) and lua_rawsetp(L, LUA_REGISTRYINDEX, &myvar) for manipulation of stored Lua value straight-forward.
Unfortunately I can't compare the performance of both methods. I think they are almost the same.

Lua get function from argument

A simple example of producing.
protocol.onConnect(function() end, function () end, ...)
Now in c, i want to get the functions which are in args #1, #2.
In strings, numbers,... we can get them using (lua_getstring,..), But I at-least didn't found how to get a function.
int luaProtocolOnConnect(lua_State* L)
{
int base_func // func #1
int call_func // func #2
....
}
You cannot really "get" a Lua function. Lua functions, like Lua tables, are pure-Lua objects. As such, they have no C or C++ analog. If you want to call a Lua function, that's done through lua_call, lua_pcall or similar functions. This is done in-situ on the Lua stack.
So you can't take a Lua function and turn it into a C++ value. What you can do is take a Lua function and manipulate it in the various ways that all Lua objects can be manipulated.
For example, let's say you want to store a Lua function in a C++ object, then later call whatever Lua function was stored there. Well obviously, you can't convert the Lua function directly into a C++ value. What you can do is store that Lua function in a place which C++ can access. You use some value which does have a C++ analog to reference that stored Lua function. The value must be unique for every object you want to store like this. The value you get when storing the object will be saved in your C++ object. When the time comes to retrieve the Lua function, you simply use the stored value to retrieve it.
Because this is an exceedingly common operation, Lua has ways to facilitate this. The first is the Lua registry, a table that C++ can access but Lua code cannot (not unless you give it access).
The second is the luaL_ref series of functions. luaL_ref takes whatever is at the top of the stack and sticks it in a table you provide, returning to you an integer key that can be used to retrieve it later. lua_rawgeti can be used to retrieve the function from the table by the key, and luaL_unref takes the table and the integer key, removing the referenced function from the table when you're done with it.
So if you want to store such functions, you simply need to create such a table, stick it in a known place in the registry (so that you can fetch it whenever you need to), and then use luaL_ref to store those functions. When it comes time to call them, retrieve them with lua_rawgeti. When you're finished using them, destroy them with luaL_unref.
You can use lua_isfunction to check if it's a function, use lua_pushvalue to push its value on top of the stack and then use luaL_ref (luaL_ref(L,LUA_REGISTRYINDEX);) to turn it into a unique key you can later reference to retrieve the value (lua_rawgeti(L,LUA_REGISTRYINDEX,ref)) and call the function.

Tracking global definitions in LLVM

I am trying to manually build a list of instructions where a particular variable is getting assigned a value in the LLVM IR.
For local variables in a function, i can easily get the right set of instructions by using the instruction iterator and checking the operands of a particular instruction. This approach doesn't seem to work for the global variables since there's no store instruction associated with them.
Is there some way to get keep track of where the global variable is being defined without looking at the metadata field? If not, is there some way to create a dummy instruction which can be treated as a special marker for initial definition of the global variables?
For local variables in a function, i can easily get the right set of instructions by using the instruction iterator and checking the operands of a particular instruction.
That's not entirely accurate. It's true as long as the variable is in memory (and assignment is done via store), but if it is promoted to registers you'll need to rely on llvm.dbg.value calls to track assignments into it.
This approach doesn't seem to work for the global variables since there's no store instruction associated with them.
Assignments to globals also appear as stores - except for the initial assignment.
Is there some way to get keep track of where the global variable is being defined without looking at the metadata field?
If by "where" you mean in which source line, you'll have to rely on the debug-info metadata.

class member is not attached c++

I read in data from an XML file, depending on the tags in the xml file, data gets attached to class member variables.
Is it possible if for example a value in the xml file contains "!", which in this case is not valid thus I can't accept that value.
So the member variable for that value is empty.
But the type of some member variables are other classes or integers or boolean. How can I check if those values are set? As there is no function emtpy() for those.
If they are not optional, you must cause your parsing mechanism to error when they are not present. Else, you could use something like boost::optional.
There is no way to detect at run time, whether a variable has been explicitly set. That's why some compilers give you a warning (not an error), if they suspect that a variable might be used uninitialized.
It is the programmer's responsibility, to keep track of what variables have been set. The low level way to do this, is to use pointers, initialize them to 0, change them when they should point to some initialized memory and change them back to 0 when the object they point to is deleted.
In C++, you can use Boost.Optional to spare you from messing around with pointers in this way.
you could during XML read, check the XML value and if it contains "!", assign a default value to whatever variable it is.
e.g. set ptr to nullptr, boolean to false and int to 0 or -1.
Use const default values whenever you can, that will make your code clearer and easier to maintain.

How to pass values in Qt?

I am doing the NFC project. In this I need to detect the tag and read the tag contents from the NFC tag and i need to send the tag id to the PHP server. From the server side i will get the response as {tagId&tagcontent}. After this i need to extract the tagId and tagcontents. Till this i have finished and working fine. Now my problem is to pass the value to another class. As soon as i get the SUCCESS response from server i need to pass the extracted values to another class. How can i do that. I am new to Qt programming. Please help me.
You can pass the parameters as key value pairs i.e. use a QHash or a QVector for this purpose.
Moreover, if you want the information ot be too specific, you can define a custom C++ class, containing values for the response fields and pass the reference from your 1st class to the other class.
Parameter passing in Qt is similar to C++.
Your QStringList, QVector or QHash all are just classes.
You can pass objects of these class as any other custom class.
For example:
In your target class (say T):
void getResponseHeader(QHash<QString,QString> x);
In your calling class(say C):
T *t = new T(); //or whatever instance you have
t->getResponseHeader(myParsedResponse);
More specifically, you can pass parameters inC++ either by value or by reference.
For passing objects, it is better to pass them via reference.
Passing by value means that a copy of the object is made on the callee’s stack and altering the object means altering a local copy so the caller’s object is unchanged when the function returns. Passing by reference means that the address of the object is send (a reference holds an address but behaves like an object) so that the callee can directly alter the original object.
Have a look at this thread for What's the difference between passing by reference vs. passing by value?
A nice explanation with examples is provided on codeforum at http://www.codeguru.com/forum/showthread.php?t=343473