g ++中的哪个内部文件包含有关程序编译时计算的信息? [英] Which internal file in g++ contain the information about compile time calculation of program?

查看:134
本文介绍了g ++中的哪个内部文件包含有关程序编译时计算的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于使用递归方法找到斐波那契数字,我在先前的问题中询问了
。使用解决方案(已回答)中的一个,程序运行时间
几乎为0.我附加在GDB中的程序并且检查汇编指令
并且找到以下:

 #include< iostream> 
template< size_t N>
struct fibonacci:std :: integral_constant< size_t,fibonacci< N-1> {} + fibonacci< N-2> {}> {}
模板<>结构纤维蛋白1 :std :: integral_constant< size_t,1> {};
模板<> struct fibonacci< 0> :std :: integral_constant< size_t,0> {};
int main(){
int out = 0;
constexpr int number = 40;
out = fibonacci< number>();
std :: cout<<Fibonacci Series Of<<<<是<< out<< std :: endl;
}



我已经使用以下标志和汇编指令编译了我的程序
我的程式为:


$ g ++ -g -gdwarf-2 -Wall -fdump-tree-all -std = c ++ 11 fibonacci.cpp -o
fibcpp




  b函数main()的汇编代码转储:
0x0000000000400890< + 0> ;: push%rbp
0x0000000000400891+ mov:%rsp,%rbp
0x0000000000400894< 4> ;: sub $ 0x10,%rsp
0x0000000000400898 +:movl $ 0x0,-0x8(%rbp)
0x000000000040089f + 15:movl $ 0x28,-0x4(%rbp )
0x00000000004008a6< + 22> ;:lea -0x9(%rbp),%rax
0x00000000004008aa< + 26> ;: mov%rax,%rdi
= 0x00000000004008ad< + 29> ;: callq 0x400952< std :: integral_constant< unsigned long,102334155ul> :: operator unsigned long()const>
0x00000000004008b2< + 34> ;: mov%eax,-0x8(%rbp)
0x00000000004008b5 <+ 37>:mov $ 0x400a15,%esi
/ pre>

我们可以看到(在arrowed ==>)102334155是有fibonacci(40)。这表明确实所有的计算都发生在编译时间。



当我们编译程序并添加额外的标志( -fdump-tree-all )时,我们得到许多
的内部文件通常(fibonacci.gimple)文件是通常模板
实例化代码将去的那个文件。然而在这种情况下,我没有找到任何
相关的计算在fibonacci.gimple文件。



我的问题是在哪个文件g ++计算和存储这些信息?我的目的是在这里了解更多关于编译时间计算/操作发生在C ++程序。

解决方案

,调用方法运算符unsigned long(),而不是内联。当你看看它的反汇编,你应该看到实际返回的值。它是 integr_constant<> :: instance value_type()的实例化 = unsigned long as value_type



但你可能已经知道所有...你想实际看到它。 https://gcc.gnu.org/ml/gcc/2011消息-06 / msg00110.html 建议,其他人想到了一个 -ftrace-template-instantiation 选项,但还没有人实现它。



EDIT:在这个问题


This is about finding the Fibonacci number using recursive approach which I had asked in my previous question. Using one of the solution(answered), the run time taken by the program was almost 0. I attach the program in GDB and check the assembly instruction and found the following:

#include<iostream>
template<size_t N>
struct fibonacci:std::integral_constant<size_t,fibonacci<N-1>{}+fibonacci<N-2>{}>{};
template<> struct fibonacci<1> : std::integral_constant<size_t,1> {};
template<> struct fibonacci<0> : std::integral_constant<size_t,0> {};
int main() {
    int out = 0;
    constexpr int number = 40;
    out = fibonacci<number>();
    std::cout<<"Fibonacci Series Of "<<number<<" is "<<out<<std::endl;
}

I have compiled my program using following flags and assembly instruction of my program is as:

$g++ -g -gdwarf-2 -Wall -fdump-tree-all -std=c++11 fibonacci.cpp -o fibcpp

(gdb) disassemble main
   Dump of assembler code for function main():
   0x0000000000400890 <+0>: push   %rbp
   0x0000000000400891 <+1>: mov    %rsp,%rbp
   0x0000000000400894 <+4>: sub    $0x10,%rsp
  0x0000000000400898 <+8>:  movl   $0x0,-0x8(%rbp)
   0x000000000040089f <+15>:    movl   $0x28,-0x4(%rbp)
   0x00000000004008a6 <+22>:    lea    -0x9(%rbp),%rax
   0x00000000004008aa <+26>:    mov    %rax,%rdi
=>  0x00000000004008ad <+29>:   callq  0x400952 <std::integral_constant<unsigned long, 102334155ul>::operator unsigned long() const>
   0x00000000004008b2 <+34>:    mov    %eax,-0x8(%rbp)
   0x00000000004008b5 <+37>:    mov    $0x400a15,%esi

we can see that(on the arrowed==>) 102334155 is there which is fibonacci(40). This indicates that indeed all calculation has happened in the compile time.

When we compile our program and put extra flag(-fdump-tree-all), we get many internal files and normally(fibonacci.gimple) files are the one where normally template instantiated code would go. However in this case I did not find anything related to this calculation in fibonacci.gimple file.

My question is in which file g++ does calculate and store these information?. My aim over here is to understand more about compile time calculation/manipulation which happens in C++ program.

解决方案

From you disassembly it seems, that the "method" operator unsigned long() is called and not inlined. When you look at its disassembly, you should see the actual returned value. It is the instantiation of integral_constant<>::operator value_type() with size_t = unsigned long as value_type.

But you might already know all that... You want to actually see it. The message https://gcc.gnu.org/ml/gcc/2011-06/msg00110.html suggests, that others thought about an -ftrace-template-instantiation option, but no one implemented it, yet.

EDIT: There is lots of information about debugging and tracing templates in this question.

这篇关于g ++中的哪个内部文件包含有关程序编译时计算的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆