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
This commit is contained in:
David Malcolm 2016-08-19 00:18:18 +00:00 committed by David Malcolm
parent f4e46e34a2
commit 82f7214696
5 changed files with 135 additions and 2 deletions

View File

@ -1,3 +1,10 @@
2016-08-18 David Malcolm <dmalcolm@redhat.com>
* 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 <dmalcolm@redhat.com>
* diagnostic-show-locus.c (colorizer::colorizer): Replace diagnostic

View File

@ -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 # <retval>
# 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, <retval>
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

View File

@ -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)

View File

@ -1,3 +1,7 @@
2016-08-18 David Malcolm <dmalcolm@redhat.com>
* gcc.dg/verbose-asm-2.c: New test case.
2016-08-18 David Malcolm <dmalcolm@redhat.com>
* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c

View File

@ -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" } } */