Compiler extension for evaluating a "complex" function (with known input values) at compile time, regardless of `constexpr`

Please see this example . An attempt to use constexprto obtain an estimate of compilation time would be extremely difficult, if not impossible.

However, when a function is called, all arguments are known at compile time. Theoretically (if there is some optimization attribute?), The Compiler can pause parsing, create a small program using a function, compile it, run it and get the result to create a char constant that will be used mainly to compile the program.

I understand that one of the problems can be, for example, cross-compilation: you can run the program only from the compiler, if the compiler can create a program that can run on a machine that builds. But this does not seem impossible to overcome.

There is no doubt: I am not the first to think about it. But I tried to search, and I could find things constexprand template. Is there any plan for implementing such a function in any compiler in the future? This function can also be rephrased as: Give the opportunity to write a C ++ program in your C ++ source, which the compiler can compile and run on demand to create a constant.

+4
source share
2

constexpr template - ++ - . . , `` '', . / .

, std::string, char*, - , , .

++ 14, constexpr. , . .

https://isocpp.org/wiki/faq/cpp14-language#extended-constexpr


, , . Staging - , . , :

, ++.

+3

- , ++. , , , , .

CyghusX1 .

POSIX (, Linux), ++ "runtime", (.. dlopen) ""; :

  • AST ++,
  • -rdynamic -ldl
  • , , . , extern "C" int foo(const char*).
  • , AST
  • , , AST ++ - , ++ ( int foo(const char*), name mangling)
  • , ++ /tmp/temp1234.cc
  • , - g++ -O -Wall -fPIC /tmp/temp1234.cc -shared -o /tmp/temp1234.so, . (3) -
  • dlopen (3) /tmp/temp1234.so RTLD_NOW|RTLD_GLOBAL; ( dlerror())
  • dlsym , dlopen, foo. , .

Linux (, , . manydl.c). GCC MELT .

, GCC MELT GCC, , , , GCC; , , MELT, ( GCC, Gimple, , __COMPACT_PRETTY_FUNCTION__ GCC, MELT).

-

#define __COMPACT_PRETTY_FUNCTION__ _builtin_compact_pretty_function()

MELT, _builtin_compact_pretty_function.

+2

Source: https://habr.com/ru/post/1584573/


All Articles