Using LLVM's C++ API, I'm calling constantInt->setName("name") but on constantInt->getName() it doesn't show up. I always get the empty string. Is ConstantInt not supposed to have a name?
You cannot assign names to constants (and neither can you assign names to void values). Unfortunately this is indeed poorly-documented, but you can see it in the source code of Value::setName. It also makes sense when you consider how constants look in the textual representation of the IR.
What you can do instead is create a global variable and mark it as constant - those can be named.
Related
Umm I've came across a problem in Fortran where I need to use Equivalence for a variable which is already declared in a module written by someone else(Probably is dead by now, otherwise I would have contacted him/her).
The variable in the module is of REAL type. And I want to store a INTEGER value in it.
If I do so directly, wrong data gets stored in the REAL type.
I have tried to use equivalence in module, But no luck.
Any Help ?
Well, EQUIVALENCE is an old keyword which should be avoided in modern Fortran because it makes the code misleading, but it has not been eliminated from Fortran standard yet, AFAIK.
However, if you have just the need to store the bit-by-bit representation of a integer in a real variable, I'd rather suggest to use the intrinsic function TRANSFER which literally transfers the binary contents of a variable to a variable of another type without any conversion and without raising errors. So, assuming that your real module variable is x and your integer value is i you can simply do:
x = TRANSFER(i,x)
The second argument could be any real variable, not necessarily x itself, it just gives a hint to the compiler that the result is of real type and it should not be an error to assign it to a real variable.
I was searching for a while on the net and unfortunately i didn't find an answer or a solution for my problem, in fact, let's say i have 2 functions named like this :
1) function1a(some_args)
2) function2b(some_args)
what i want to do is to write a macro that can recognize those functions when feeded with the correct parameter, just that the thing is, this parameter should be also a parameter of a C/C++ function, here is what i did so far.
#define FUNCTION_RECOGNIZER(TOKEN) function##TOKEN()
void function1a()
{
}
void function2a()
{
}
void anotherParentFunction(const char* type)
{
FUNCTION_RECOGNIZER(type);
}
clearly, the macro is recognizing "functiontype" and ignoring the argument of anotherParentFunction, i'm asking if there is/exist a trick or anything to perform this way of pasting.
thank you in advance :)
If you insist on using a macro: Skip the anotherParentFunction() function and use the macro directly instead. When called with constant strings, i.e.
FUNCTION_RECOGNIZER( "1a");
it should work.
A more C++ like solution would be to e.g use an enum, then implement anotherParentFunction() with the enum as parameter and a switch that calls the corresponding function. Of course you need to change the enum and the switch statement then every time you add a new function, but you would be more flexible in choosing the names of the functions.
There are many more solutions to achieve something similar, the question really is: What is your use case? What do want to achieve?
In 16.1.5 the standard says:
The implementation can process and skip sections of source files conditionally, include other source files, and replace macros. These capabilities are called preprocessing, because conceptually they occur before translation of the resulting translation unit.
[emphasis mine]
Originally pre-processing was done by a separate app, it is essentially an independent language.
Today, the pre-processor is often part of the compiler, but - for example - you can't see macros etc in the Clang AST tree.
The significance of this is that the pre-processor knows nothing about types or functions or arguments.
Your function definition
void anotherParentFunction(const char* type)
means nothing to the pre-processor and is completely ignored by it.
FUNCTION_RECOGNIZER(type);
this is recognized as a defined macro, but type is not a recognized pre-processor symbol so it is treated as a literal, the pre-processor does not consult the C++ parser or interact with it's AST tree.
It consults the macro definition:
#define FUNCTION_RECOGNIZER(TOKEN) function##TOKEN()
The argument, literal type, is tokenized as TOKEN. The word function is taken as a literal and copied to the result string, the ## tells the processor to copy the value of the token TOKEN literally, production functiontype in the result string. Because TOKEN isn't recognized as a macro, the ()s end the token and the () is appended as a literal to the result string.
Thus, the pre-processor substitutes
FUNCTION_RECOGNIZER(type);
with
functiontype();
So the bad news is, no there is no way to do what you were trying to do, but this may be an XY Problem and perhaps there's a solution to what you were trying to achieve instead.
For instance, it is possible to overload functions based on argument type, or to specialize template functions based on parameters, or you can create a lookup table based on parameter values.
How can I get all the variables (from bytecode file or IR file) with
const modifier or the variables that are not changed due execution?
I need to make list for further use.
I'm not sure you can get what you want directly because const is a C/C++ semantic that's useful for Clang but much less so for LLVM. Only some const promises are preserved (for example the readonly attribute on pointer function arguments - see the language reference for details).
LLVM IR level "constants" are something entirely different, and usually refer to actually constant (compile-time known) values that can be efficiently uniqued, etc. Read this documentation for the full scoop.
__declspec(naked) void printfive() {
int i = 5;
printf("%i\n", i);
}
for some reason this code works, but I do not understand where the i is stored? In the frame of the calling function? It becomes global variable? If it is stored in the caller's frame, then how compiler knows the displacement, because you can call printfive() from different functions with different frame size and local variables. If it is global, or something like static maybe, I have tried to go recursive and I can see that variable is not changed, it is not truly local indeed. But that's obvious, there is no entry code (prolog). Ok, I understand, no prolog, no frame, no register change, but this is values, but what happens to the scope? Is the behaviour of this specifier defined in any reference? Is this part of C++ standard anyway? This sort of functions are great if you mostly use asm {} inside them, (or use asm to call them and want to be sure the function is not over optimized), but you can mix with C++. But this is sort of brain twister.
I know this topic is more than several years old, Here are my own answers.
Since no references are made to Microsoft documentation regarding this topic, for those who care to learn more about what is needed or not as stated by Keltar, here is the Microsoft documentation that explains most of what Keltar didn't explain here.
According to Microsoft documentation, and should be avoided.
The following rules and limitations apply to naked functions:
The return statement is not permitted.
Structured Exception Handling and C++ Exception Handling constructs are not permitted because they must unwind across the stack frame.
For the same reason, any form of setjmp is prohibited
Use of the _alloca function is prohibited.
To ensure that no initialization code for local variables appears before the prolog sequence, initialized local variables are not
permitted at function scope. In particular, the declaration of C++
objects are not permitted at function scope. There may, however, be
initialized data in a nested scope.
Frame pointer optimization (the /Oy compiler option) is not recommended, but it is automatically suppressed for a naked function.
You cannot declare C++ class objects at the function lexical scope. You can, however, declare objects in a nested block.
From gcc manual:
Use this attribute ... to indicate that the specified function does
not need prologue/epilogue sequences generated by the compiler. It is
up to the programmer to provide these sequences. The only statements
that can be safely included in naked functions are asm statements that
do not have operands. All other statements, including declarations of
local variables, if statements, and so forth, should be avoided. Naked
functions should be used to implement the body of an assembly
function, while allowing the compiler to construct the requisite
function declaration for the assembler.
And it isn't standard (as well as any __declspec or __attribute__)
When entering or exiting a function, the compiler adds code to help with the passing or parameters. When a function is declared naked, non of the parameter variables assignment code is generated, if you want to get at any of the parameters, you will need to directly access the relevant registers or stack (depending on the ABI defined calling convention).
In your case, you are not passing parameters to the function, therefore your code works even though the function is declared naked. Take a look at the dis-assembler if you want to see the difference.
Easy question for you vets out there:
What is the accepted (assuming there is one...) prefix for a CString variable name? For clarification, I've seen the following for other data types:
int iIndex; //integer
int* pIndex; //pointer
bool fFlag; //bool flag
And there are countless others. Please feel free to let me know if this is really a non-question or something with a "Whatever you want" answer.
Prefixes such as those are an abuse of the concept of Hungarian Notation.
The idea of HN is that a variable is prefixed with a code describing its use. e.g., a variable holding the count of something would be prefixed cnt; a variable holding an index would be prefixed inx. A variable holding a flag would be prefixed f. A variable holding a number (that wasn't a count or an index or something else common) would be prefixed n.
However, soon people got lazy, (and largely due to that last example) and the prefixes started to be just an indication of the data type. This has some use in C, where the declaration of a variable had to be at the top of the function, potentially some distance from where it is used. (especially when code was written in a simple text editor)
But, eventually, we got more type safe languages, and better IDEs, so the faux-Hungarian Notation because unnecessary and scorned.
There is none. Never prefix a variable with its type. See Making Wrong Code Look Wrong for the correct use of prefixes.
There is no standard for notation in a variable name. In fact, with better development environments (with intellisense, etc.) it is highly unnecessary.
I've seen "s" used. Example: sIndexname.
I use "m" for a data member: mIndex, and "p" for a pointer to a data member: mpIndex. I only use them for class-scope variables. That's it.
By today’s "standards" even that is pushing it. AFAIK, except for a few Microsoft diehards Hungarian notation is dead. I am especially amused when I see it used for local variables that are used exactly once 2 lines later. Fun.
For some reason I am repulsed by the "m_" convention. It's just ugly, imo. :-)