读取(简单)程序集以查找使用C++模板生成的编译时计算

Reading (simple) assembly to find compile-time calculation resulting from using C++ templates

本文关键字:计算 编译 C++ 简单 程序集 查找 读取      更新时间:2023-10-16

我正在比较Fortran 90与C++进行演示。我的一个比较依赖于g++gfortran为简单程序生成的程序集。

一个例子如下:

#include<cstdio> // quick and dirty number formatting
template<int N>
double dot(double x[], double y[]){
  return x[N-1] * y[N-1] + dot<N-1>(x, y);
}
template<>
double dot<1>(double x[], double y[]){
  return x[0] * y[0];
}
    
int main(){
  double x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  double y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  printf("x.y = %23.16En", dot<10>(x, y));
}

以下汇编程序由命令g++ -S -O3 myprogram.cpp在OS X 10.7.4 x86_64-apple-darwin11.4.2上使用g++ 4.7.2生成:

    .text
    .align 4,0x90
    .globl __Z3dotILi1EEdPdS0_
__Z3dotILi1EEdPdS0_:
LFB2:
    movsd   (%rdi), %xmm0
    mulsd   (%rsi), %xmm0
    ret
LFE2:
    .cstring
LC1:
    .ascii "x.y = %23.16E12"
    .section __TEXT,__text_startup,regular,pure_instructions
    .align 4
    .globl _main
_main:
LFB3:
    leaq    LC1(%rip), %rdi
    subq    $8, %rsp
LCFI0:
    movl    $1, %eax
    movsd   LC0(%rip), %xmm0
    call    _printf
    xorl    %eax, %eax
    addq    $8, %rsp
LCFI1:
    ret
LFE3:
    .literal8
    .align 3
LC0:
    .long   0
    .long   1081610240
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
    .set L$set$0,LECIE1-LSCIE1
    .long L$set$0
LSCIE1:
    .long   0
    .byte   0x1
    .ascii "zR"
    .byte   0x1
    .byte   0x78
    .byte   0x10
    .byte   0x1
    .byte   0x10
    .byte   0xc
    .byte   0x7
    .byte   0x8
    .byte   0x90
    .byte   0x1
    .align 3
LECIE1:
LSFDE1:
    .set L$set$1,LEFDE1-LASFDE1
    .long L$set$1
LASFDE1:
    .long   LASFDE1-EH_frame1
    .quad   LFB2-.
    .set L$set$2,LFE2-LFB2
    .quad L$set$2
    .byte   0
    .align 3
LEFDE1:
LSFDE3:
    .set L$set$3,LEFDE3-LASFDE3
    .long L$set$3
LASFDE3:
    .long   LASFDE3-EH_frame1
    .quad   LFB3-.
    .set L$set$4,LFE3-LFB3
    .quad L$set$4
    .byte   0
    .byte   0x4
    .set L$set$5,LCFI0-LFB3
    .long L$set$5
    .byte   0xe
    .byte   0x10
    .byte   0x4
    .set L$set$6,LCFI1-LCFI0
    .long L$set$6
    .byte   0xe
    .byte   0x8
    .align 3
LEFDE3:
    .constructor
    .destructor
    .align 1
    .subsections_via_symbols

点积是 385,似乎是在编译时计算的,但我似乎找不到确切的位置。我怀疑它位于以下汇编程序段的某个地方:

    movl    $1, %eax
    movsd   LC0(%rip), %xmm0
    call    _printf
    xorl    %eax, %eax
    addq    $8, %rsp
LCFI1:
    ret
LFE3:
    .literal8
    .align 3
LC0:
    .long   0
    .long   1081610240
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support

我对汇编的(非常非常有限)的理解会告诉我,点积是由编译器计算并放置在寄存器(LC0)中的。然后,指令movsd LC0(%rip), %xmm0将值放在字符串中,并对生成的格式化字符串调用printf

是这样吗?实际数字 385 是否包含在此输出中的某个位置,还是其他计算的?

谢谢!

编辑:

如果有人想知道gfortran生产的组件是什么样子的,我在下面附上它。请注意,尽管在编译时已知,并且我使用的是 Fortran 的内部dot_product运算符,但生成的程序集要大得多(130 行,而 C++ 版本中为 90 行),并且优化器似乎无法减少操作。

程序(请注意,我使用的是内置的内置dot_product运算符):

PROGRAM MAIN
  REAL(8), DIMENSION(10):: X = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)
  REAL(8), DIMENSION(10):: Y = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)
  PRINT "(A, E23.16)", "x.y = ", DOT_PRODUCT(X, Y)
ENDPROGRAM MAIN

Assembly (gfortran -S -O3 myprogram.cpp在OS X 10.7.4上使用gcc 4.7.2 x86_64-apple-darwin11.4.2)

    .cstring
LC0:
    .ascii "dotproduct-intrinsic.f90"
    .const
LC1:
    .ascii "(A, E23.16)"
LC2:
    .ascii "x.y = "
    .text
    .align 4,0x90
_MAIN__:
LFB0:
    leaq    LC0(%rip), %rax
    subq    $504, %rsp
LCFI0:
    movq    %rax, 24(%rsp)
    leaq    16(%rsp), %rdi
    leaq    LC1(%rip), %rax
    movl    $5, 32(%rsp)
    movq    %rax, 88(%rsp)
    movl    $11, 96(%rsp)
    movl    $4096, 16(%rsp)
    movl    $6, 20(%rsp)
    call    __gfortran_st_write
    leaq    16(%rsp), %rdi
    movl    $6, %edx
    leaq    LC2(%rip), %rsi
    call    __gfortran_transfer_character_write
    leaq    8(%rsp), %rsi
    movl    $8, %edx
    movabsq $4645480607818711040, %rax
    leaq    16(%rsp), %rdi
    movq    %rax, 8(%rsp)
    call    __gfortran_transfer_real_write
    leaq    16(%rsp), %rdi
    call    __gfortran_st_write_done
    addq    $504, %rsp
LCFI1:
    ret
LFE0:
    .section __TEXT,__text_startup,regular,pure_instructions
    .align 4
    .globl _main
_main:
LFB1:
    subq    $8, %rsp
LCFI2:
    call    __gfortran_set_args
    leaq    _options.3.1864(%rip), %rsi
    movl    $8, %edi
    call    __gfortran_set_options
    call    _MAIN__
    xorl    %eax, %eax
    addq    $8, %rsp
LCFI3:
    ret
LFE1:
    .const
    .align 5
_options.3.1864:
    .long   68
    .long   1023
    .long   0
    .long   0
    .long   1
    .long   1
    .long   0
    .long   1
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
    .set L$set$0,LECIE1-LSCIE1
    .long L$set$0
LSCIE1:
    .long   0
    .byte   0x1
    .ascii "zR"
    .byte   0x1
    .byte   0x78
    .byte   0x10
    .byte   0x1
    .byte   0x10
    .byte   0xc
    .byte   0x7
    .byte   0x8
    .byte   0x90
    .byte   0x1
    .align 3
LECIE1:
LSFDE1:
    .set L$set$1,LEFDE1-LASFDE1
    .long L$set$1
LASFDE1:
    .long   LASFDE1-EH_frame1
    .quad   LFB0-.
    .set L$set$2,LFE0-LFB0
    .quad L$set$2
    .byte   0
    .byte   0x4
    .set L$set$3,LCFI0-LFB0
    .long L$set$3
    .byte   0xe
    .byte   0x80,0x4
    .byte   0x4
    .set L$set$4,LCFI1-LCFI0
    .long L$set$4
    .byte   0xe
    .byte   0x8
    .align 3
LEFDE1:
LSFDE3:
    .set L$set$5,LEFDE3-LASFDE3
    .long L$set$5
LASFDE3:
    .long   LASFDE3-EH_frame1
    .quad   LFB1-.
    .set L$set$6,LFE1-LFB1
    .quad L$set$6
    .byte   0
    .byte   0x4
    .set L$set$7,LCFI2-LFB1
    .long L$set$7
    .byte   0xe
    .byte   0x10
    .byte   0x4
    .set L$set$8,LCFI3-LCFI2
    .long L$set$8
    .byte   0xe
    .byte   0x8
    .align 3
LEFDE3:
    .subsections_via_symbols

编辑 2

多亏了@JerryCoffin的回答,我能够验证位模式

LC0:
    .long   0
    .long   1081610240

在程序集输出中找到对应于数字 385。

我使用了@JerryCoffin提供的完全相同的程序,即:

#include <stdio.h>

    #pragma pack(1)
    struct x {
        long x, y;
    };    
    
    int main() { 
        x v = {0, 1081610240};
    
        printf("%fn", *(double *)&v);
        return 0;
    }

唯一需要注意的是,我必须使用 32 位目标编译它:g++ verification.cpp -m32 .

是的

-- 这个:

LC0:
    .long   0
    .long   1081610240

值为 3851 的双精度值的位模式。所以正在发生的事情是这样的:

LCFI0:
    movl    $1, %eax
    movsd   LC0(%rip), %xmm0
    call    _printf

。将该值加载到 XMM 寄存器中,然后调用 printf 将其打印出来。我不能肯定地说,但如果我不得不猜测,我会说1告诉它它正在传递一个要打印的参数。


1如果您想验证这一点,请尝试以下操作:

#include <stdio.h>
#pragma pack(1)
struct x {
    long x, y;
};    
int main() { 
    x v = {0, 1081610240};
    printf("%fn", *(double *)&v);
    return 0;
}

当然,官方认为这是不可移植的,等等,但是在同一台机器上使用相同的编译器,您获得与我相同的输出的可能性约为 99% - 385