Reputation: 51
I've been compiling some simple programs into assembly from C++ and Fortran to try and get more of an appreciation for computer architecture. I've written a very simple program in both C++ and Fortran to produce some output and then return. What I don't understand is why the Fortran compiler (gfortran) would produce assembly that creates such a large stack frame compared to the C++.
f_subroutines.f
PROGRAM F_SUBROUTINES
CALL SAY_HELLO
CALL SAY_GOODBYE
END
SUBROUTINE SAY_HELLO
PRINT '(A6)', "Hello."
END
INTEGER*4 FUNCTION GET_MY_NUMBER()
GET_MY_NUMBER = 3
END
SUBROUTINE SAY_GOODBYE
INTEGER :: GET_MY_NUMBER
PRINT '(A18, I1, A19)',
1 "Here's my number: ", GET_MY_NUMBER(), ". Call me. Goodbye!"
END
cpp_subroutines.cpp
#include <cstdio>
void say_hello()
{
std::printf("Hello.\n");
}
int get_my_number()
{
return 3;
}
void say_goodbye()
{
std::printf("Here's my number: %d. Call me. Goodbye!\n", get_my_number());
}
int main()
{
say_hello();
say_goodbye();
return 0;
}
Makefile
CPPFLAGS=-O0
all: cpp_subroutines cpp_subroutines.s f_subroutines f_subroutines.s
cpp_subroutines: cpp_subroutines.o
$(CXX) $(CPPFLAGS) -o $@ $^ $(LDFLAGS)
cpp_subroutines.s:
$(CXX) $(CPPFLAGS) -S cpp_subroutines.cpp
f_subroutines: f_subroutines.o
$(FC) -o $@ $^ $(LDFLAGS)
f_subroutines.s:
$(FC) -S f_subroutines.f
.PHONY: clean
clean:
$(RM) cpp_subroutines cpp_subroutines.o cpp_subroutines.s \
f_subroutines f_subroutines.o f_subroutines.s
cpp_subroutines.s: say_hello
_Z9say_hellov:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $.LC0, %edi
call puts
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
f_subroutines.s: say_hello
say_hello_:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $480, %rsp
movq $.LC0, -472(%rbp)
movl $8, -464(%rbp)
movq $.LC4, -408(%rbp)
movl $4, -400(%rbp)
movl $4096, -480(%rbp)
movl $6, -476(%rbp)
leaq -480(%rbp), %rax
movq %rax, %rdi
call _gfortran_st_write
leaq -480(%rbp), %rax
movl $6, %edx
movl $.LC5, %esi
movq %rax, %rdi
call _gfortran_transfer_character_write
leaq -480(%rbp), %rax
movq %rax, %rdi
call _gfortran_st_write_done
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
I am using gfortran and gcc version 5.4.0.
I'm not quite sure what the Fortran assembly is doing here:
subq $480, %rsp
movq $.LC0, -472(%rbp)
movl $8, -464(%rbp)
movq $.LC4, -408(%rbp)
movl $4, -400(%rbp)
movl $4096, -480(%rbp)
movl $6, -476(%rbp)
leaq -480(%rbp), %rax
It seems to be using 480 bytes for this stack frame and putting
I'm perfectly happy with the output of the C++ compilation but I would like to know why
Upvotes: 4
Views: 312
Reputation: 37228
This is a long-standing issue in GFortran, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48419 for details.
Upvotes: 4