How to get the inializers of global variables with LLVM API [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm writing an LLVM pass which works on the LLVM IR code. A part of the objective is to read the initial values of global variables, where global variables could be of any type, from basic types to structs and classes. How do I achieve that?

If you invoke getInitializer on a GlobalVariable instance you get the initializer, of type Constant (though make sure you call hasInitializer or one of its sister methods first, to verify there's an initializer at all). Also IIRC global variables and constant are implemented as the same thing, so use isConstant to filter out the constants if you don't want them.
Of course, a Constant is the abstract base class; the actual type will be one of its children, which you can see in the diagram presented on the documentation page. You can query and get the actual constant type in the usual way, via isa / cast / dyn_cast, or you can use getType on it (a constant is a Value after all) and work from there.
Finally, to get all the global variables from a Module use either the global_begin/global_end iterators, or just use getGlobalList on it (it has its own iterator).

Related

Why does C++ regards as Static Languages [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
From my recitation class -
In free variables (in function activation):
Static scoping: free variables are evaluated in the context of the defining occurrence of the function . include many you know: ML, Java,
C++.
Dynamic scoping: free variables in the function body are evaluated in the context of the function call
Static languages: Common
Include many you know: ML, Java, C++ Advantages modularity easier
variable access by the compiler
Can you give an example which describes why does C++ regards as Static languages ?
static language mean “statically typed language” . for example type of a variable can't be change and defined statically at compilation time.
But not because of any of reason you mentioned
int i = 10;
i in int can be char.
in opposite Python for example:
>>> x = "yourname" # x is pointing string
>>> x = 5 # x pointing number
So Python is an example of “dynamic typed language”
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. In C++, variables need to be defined before they are used so that compilers know what type they are, and hence is statically typed.
Please take a look at below articles
http://www.jvoegele.com/software/langcomp.html
http://existentialtype.wordpress.com/2011/03/19/dynamic-languages-are-static-languages/
http://en.wikipedia.org/wiki/Type_system
http://en.wikipedia.org/wiki/C%2B%2B

C/C++ The purpose of static const local variable [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C and C++, what is the advantage of making a local const variable static? Assuming the initialization does not use other variables, is there any difference between preserving the value between calls, and setting the same constant value each call?
Could a valid C compiler ignore the static?
In C++, it avoids the construction/destruction between calls, but could there be any other benefit?
It doesn't take up stack-space may be a benefit if you have something like:
static const double table[fairly_large_number] = { .... };
Obviously, cost of construction can also be substantial enough that if the function is called a lot, there's good value in only constructing the object once.
Yes, and it is huge: a semantic benefit.
When you put const, you don't just mean the compiler shouldn't let you modify the variable. You make a bolder statement to whoever reads the code later: this won't ever change. Not even by a side effect where you give this variable as a pointer to another function.
Also, the compiler can take advantage of that new information and optimize it away in some situations, depending on the specific type you are dealing with.
(to be clear, I'm speaking here about const vs. non-const, not static vs. non-static.)
Edit: This SO answer is very informative too.

Structs into classes - Noticeable function overhead? C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Taking this as an example:
I have 20 structs. I access all of the struct's fields directly, getting values off them, pointers to other sub-structs they might have, etc.
Now, I restructure the program:
Instead of acessing structs's fields DIRECTLY, I've encapsuled all of the struct into their own respective classes, and have functions for all the possible get(), and set(x).
The Question: Is there a performance impact for using methods/functions, instead of acessing structs directly?
Some sort of estimated % would be great, or some explanation.
In general, there shouldn't be a performance difference if the getters and setters are defined inline within the class. For virtually any contemporary compiler, the function call will be expanded inline, leaving no overhead. This will often be true for various small inline functions. (See, for example, http://www.gotw.ca/publications/mill18.htm, where Herb Sutter discusses why making most/all virtual functions nonpublic adds no overhead to the resulting code.)
Note that if you do define the function inline in the class, any client code will need to be recompiled if the definition ever changes. But that applies to most/all changes in header files.
I haven't heard of performance as a considerable difference between structs and classes, so it is your call.
I googled these pages below, and they say there shouldn't be a difference when it comes to performance.
http://www.gamedev.net/topic/115725-is-a-struct-faster-than-a-class/
http://forums.codeguru.com/showthread.php?332571-Struct-vs-Class

Is it possible to create variables during runtime in c++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it possible to create a new variable for a function everytime the function is called? I was considering making a huge array and just using a different block for each time a function is called but I would prefer to create variables on demand instead, is this possible in c++?
Example:
A user types asdf and clicks save, setting off the savetext function
void savetext(textvariable)
{
static int //(this variable name should somehow become asdf) = somedata;
return;
}
If you have something that, for example, adds things to an array, then vector is the right solution. The vector class will automatically grow as you need it, using the push_back function to add things to itself.
The vector class acts largely as an array that grows as you need it to, so it's very easy to use.
Every time you enter a function, all variables in it are new. There's no need to explicitly create variables unless you need objects that live longer than your function, or you don't know how many you need.

Using const on local variables [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Similar to this question, what are the pro/cons to using const local variables like in this answer?
Personally, I like to use const for any declared object (I'm not sure the word "variable" applies) unless I'm actually going to modify it. It documents to the reader that the value of the object is always going to be whatever it was initialized to, which can make the code easier to follow and analyze.
(It can also help the compiler in some cases, but most compilers, when invoked in optimizing mode, are smart enough to notice that the object is never modified. And if you invoke the compiler in non-optimizing mode, you're telling it that you don't care much about performance.)
In fact, if I were going to design my own language, all declared objects would be const (read-only) by default unless you explicitly mark them as modifiable.