From 82f72146967e71693f2d4f6646f35c3c00780602 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 19 Aug 2016 00:18:18 +0000 Subject: [PATCH] Add source information to -fverbose-asm gcc/ChangeLog: * doc/invoke.texi (fverbose-asm): Note that source code lines are emitted, and provide an example. * final.c (asm_show_source): New function. (final_scan_insn): Call asm_show_source. From-SVN: r239604 --- gcc/ChangeLog | 7 +++ gcc/doc/invoke.texi | 83 ++++++++++++++++++++++++++++ gcc/final.c | 28 +++++++++- gcc/testsuite/ChangeLog | 4 ++ gcc/testsuite/gcc.dg/verbose-asm-2.c | 15 +++++ 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/verbose-asm-2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c268e0001ab..65efe1e5513 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2016-08-18 David Malcolm + + * doc/invoke.texi (fverbose-asm): Note that source code lines + are emitted, and provide an example. + * final.c (asm_show_source): New function. + (final_scan_insn): Call asm_show_source. + 2016-08-18 David Malcolm * diagnostic-show-locus.c (colorizer::colorizer): Replace diagnostic diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index d04be6fd4c8..1f045013549 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -11426,6 +11426,89 @@ debugging the compiler itself). extra information to be omitted and is useful when comparing two assembler files. +The added comments include: + +@itemize @bullet + +@item +information on the compiler version and command-line options, + +@item +the source code lines associated with the assembly instructions, +in the form FILENAME:LINENUMBER:CONTENT OF LINE, + +@item +hints on which high-level expressions correspond to +the various assembly instruction operands. + +@end itemize + +For example, given this C source file: + +@smallexample +int test (int n) +@{ + int i; + int total = 0; + + for (i = 0; i < n; i++) + total += i * i; + + return total; +@} +@end smallexample + +compiling to (x86_64) assembly via @option{-S} and emitting the result +direct to stdout via @option{-o} @option{-} + +@smallexample +gcc -S test.c -fverbose-asm -Os -o - +@end smallexample + +gives output similar to this: + +@smallexample + .file "test.c" +# GNU C11 (GCC) version 7.0.0 20160809 (experimental) (x86_64-pc-linux-gnu) + [...snip...] +# options passed: + [...snip...] + + .text + .globl test + .type test, @@function +test: +.LFB0: + .cfi_startproc +# test.c:4: int total = 0; + xorl %eax, %eax # +# test.c:6: for (i = 0; i < n; i++) + xorl %edx, %edx # i +.L2: +# test.c:6: for (i = 0; i < n; i++) + cmpl %edi, %edx # n, i + jge .L5 #, +# test.c:7: total += i * i; + movl %edx, %ecx # i, tmp92 + imull %edx, %ecx # i, tmp92 +# test.c:6: for (i = 0; i < n; i++) + incl %edx # i +# test.c:7: total += i * i; + addl %ecx, %eax # tmp92, + jmp .L2 # +.L5: +# test.c:10: @} + ret + .cfi_endproc +.LFE0: + .size test, .-test + .ident "GCC: (GNU) 7.0.0 20160809 (experimental)" + .section .note.GNU-stack,"",@@progbits +@end smallexample + +The comments are intended for humans rather than machines and hence the +precise format of the comments is subject to change. + @item -frecord-gcc-switches @opindex frecord-gcc-switches This switch causes the command line used to invoke the diff --git a/gcc/final.c b/gcc/final.c index 5b04311218e..eccc3d81710 100644 --- a/gcc/final.c +++ b/gcc/final.c @@ -2140,6 +2140,26 @@ call_from_call_insn (rtx_call_insn *insn) return x; } +/* Print a comment into the asm showing FILENAME, LINENUM, and the + corresponding source line, if available. */ + +static void +asm_show_source (const char *filename, int linenum) +{ + if (!filename) + return; + + int line_size; + const char *line = location_get_source_line (filename, linenum, &line_size); + if (!line) + return; + + fprintf (asm_out_file, "%s %s:%i: ", ASM_COMMENT_START, filename, linenum); + /* "line" is not 0-terminated, so we must use line_size. */ + fwrite (line, 1, line_size, asm_out_file); + fputc ('\n', asm_out_file); +} + /* The final scan for one insn, INSN. Args are same as in `final', except that INSN is the insn being scanned. @@ -2563,8 +2583,12 @@ final_scan_insn (rtx_insn *insn, FILE *file, int optimize_p ATTRIBUTE_UNUSED, note in a row. */ if (!DECL_IGNORED_P (current_function_decl) && notice_source_line (insn, &is_stmt)) - (*debug_hooks->source_line) (last_linenum, last_filename, - last_discriminator, is_stmt); + { + if (flag_verbose_asm) + asm_show_source (last_filename, last_linenum); + (*debug_hooks->source_line) (last_linenum, last_filename, + last_discriminator, is_stmt); + } if (GET_CODE (body) == PARALLEL && GET_CODE (XVECEXP (body, 0, 0)) == ASM_INPUT) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7da55951e9a..dd893ca414a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-08-18 David Malcolm + + * gcc.dg/verbose-asm-2.c: New test case. + 2016-08-18 David Malcolm * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c diff --git a/gcc/testsuite/gcc.dg/verbose-asm-2.c b/gcc/testsuite/gcc.dg/verbose-asm-2.c new file mode 100644 index 00000000000..747bff1875e --- /dev/null +++ b/gcc/testsuite/gcc.dg/verbose-asm-2.c @@ -0,0 +1,15 @@ +/* Ensure that the -fverbose-asm leads to source code information in the generated asm. */ +/* { dg-options "-fverbose-asm" } */ + +int test (int n) +{ + int i; + int total = 0; + + for (i = 0; i < n; i++) + total += i * i; + + return total; +} + +/* { dg-final { scan-assembler "total = 0" } } */