[llvm-dev] Global Variable Initialization via Internal Function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] Global Variable Initialization via Internal Function

Chris Lattner via llvm-dev
Hello,

I'm trying to figure out how to initialize global variables via LLVM IR and I found that clang creates a internal function to initialize global variables if initialization values are not constants.

Do we really need to create an internal function for complex global variable initializations during variable declaration? Can't we create instructions in global scope?

Example Simple C++ code:

int func()
{
    return 5*2;
}

init globalVar = func();

int main(...)
...

Thanks for helping!

Aaron


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Global Variable Initialization via Internal Function

Chris Lattner via llvm-dev
On Mon, May 7, 2018 at 2:43 PM Aaron via llvm-dev <[hidden email]>
wrote:

> Hello,

> I'm trying to figure out how to initialize global variables via LLVM IR
and I found that clang creates a internal function to initialize global
variables if initialization values are not constants.

> Do we really need to create an internal function for complex global
variable initializations during variable declaration? Can't we create
instructions in global scope?


There might be other ways, but the C++ standard says something about the
order of initialisation of objects, and operating systems usually do this
by a convention. The convention seems to be that it will call functions
that are referred to from specially-named sections in a binary. So far, the
"cleanest" way of doing that is to define functions that get referred to
(by address) from these special sections.

> Example Simple C++ code:

> int func()
> {
>      return 5*2;
> }

> init globalVar = func();

> int main(...)
> ...

> Thanks for helping!

> Aaron

> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



--
Dean
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev