Debian Codegraph @

gcc/testsuite/lib/scanasm.exp

raw
1 # Copyright (C) 2000-2025 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
16
17 # Various utilities for scanning assembler output, used by gcc-dg.exp and
18 # g++-dg.exp.
19
20 # Utility for scanning compiler result, invoked via dg-final.
21
22 # Transform newline and similar characters into their escaped form.
23 proc make_pattern_printable { pattern } {
24 return [string map {\t \\t \n \\n \r \\r \\ \\\\} $pattern]
25 }
26
27 # Append to ARGS to make it suitable for use by dg-scan to indicate
28 # that encoding ENC should be used when reading from the file.
29
30 proc append_encoding_arg { args enc } {
31 if { [llength $args] < 2 } {
32 # Add target selector.
33 lappend args { target "*-*-*" }
34 }
35 # Add encoding ENC.
36 lappend args $enc
37 return $args
38 }
39
40 # Scan the OUTPUT_FILE for a pattern. If it is present and POSITIVE
41 # is non-zero, or it is not present and POSITIVE is zero, the test
42 # passes. The ORIG_ARGS is the list of arguments provided by dg-final
43 # to scan-assembler. The first element in ORIG_ARGS is the regular
44 # expression to look for in the file. The second element, if present,
45 # is a DejaGNU target selector. The third element, if present, is the
46 # encoding to use when reading from the file.
47
48 proc dg-scan { name positive testcase output_file orig_args } {
49 if { [llength $orig_args] < 1 } {
50 error "$name: too few arguments"
51 return
52 }
53 if { [llength $orig_args] > 3 } {
54 error "$name: too many arguments"
55 return
56 }
57 if { [llength $orig_args] >= 2 } {
58 switch [dg-process-target [lindex $orig_args 1]] {
59 "S" { }
60 "N" { return }
61 "F" { setup_xfail "*-*-*" }
62 "P" { }
63 }
64 }
65
66 set pattern [lindex $orig_args 0]
67 set printable_pattern [make_pattern_printable $pattern]
68
69 set files [dg_glob_remote $output_file]
70 if { $files == "" } {
71 verbose -log "$testcase: output file does not exist"
72 unresolved "$testcase $name $printable_pattern"
73 return
74 }
75 set fd [open $output_file r]
76 if { [llength $orig_args] >= 3 } {
77 set file_encoding [lindex $orig_args 2]
78 fconfigure $fd -encoding $file_encoding
79 }
80 set text [read $fd]
81 close $fd
82 if { [string compare -length 14 $name scan-assembler] == 0 } {
83 # Remove LTO sections.
84 # ??? Somehow, .*? is still greedy.
85 # regsub -all {(^|\n)[[:space:]]*\.section[[:space:]]*"?\.gnu\.lto_.*?\n(?=[[:space:]]*\.text\n)} $text {\1} text
86 regsub -all {(^|\n)[[:space:]]*\.section[[:space:]]*"?\.gnu\.lto_(?:[^\n]*\n(?![[:space:]]*\.(section|text|data|bss)))*[^\n]*\n} $text {\1} text
87 }
88
89 set match [regexp -- $pattern $text]
90 if { $match == $positive } {
91 pass "$testcase $name $printable_pattern"
92 } else {
93 fail "$testcase $name $printable_pattern"
94 }
95 }
96
97 proc dg_glob_remote { file_pattern } {
98 if { [is_remote host] } {
99 remote_upload host $file_pattern
100 }
101 return [glob -nocomplain $file_pattern]
102 }
103
104 # Look for a pattern in the .s file produced by the compiler. See
105 # dg-scan for details.
106
107 proc scan-assembler { args } {
108 set testcase [testname-for-summary]
109 # The name might include a list of options; extract the file name.
110 set filename [lindex $testcase 0]
111 set output_file "[file rootname [file tail $filename]].s"
112 dg-scan "scan-assembler" 1 $testcase $output_file $args
113 }
114
115 set_required_options_for scan-assembler
116
117 proc scan-raw-assembler { args } {
118 set testcase [testname-for-summary]
119 # The name might include a list of options; extract the file name.
120 set filename [lindex $testcase 0]
121 set output_file "[file rootname [file tail $filename]].s"
122 dg-scan "scan-raw-assembler" 1 $testcase $output_file $args
123 }
124
125 set_required_options_for scan-raw-assembler
126
127 # Check that a pattern is not present in the .s file produced by the
128 # compiler. See dg-scan for details.
129
130 proc scan-assembler-not { args } {
131 set testcase [testname-for-summary]
132 # The name might include a list of options; extract the file name.
133 set filename [lindex $testcase 0]
134 set output_file "[file rootname [file tail $filename]].s"
135
136 dg-scan "scan-assembler-not" 0 $testcase $output_file $args
137 }
138
139 set_required_options_for scan-assembler-not
140
141 # Return the scan for the assembly for hidden visibility.
142
143 proc hidden-scan-for { symbol } {
144
145 set objformat [gcc_target_object_format]
146
147 switch $objformat {
148 coff { return "$symbol\[,\d\]*hidden" }
149 elf { return "hidden\[ \t_\]*$symbol" }
150 mach-o { return "private_extern\[ \t_\]*_?$symbol" }
151 default { return "" }
152 }
153
154 }
155
156
157 # Check that a symbol is defined as a hidden symbol in the .s file
158 # produced by the compiler.
159
160 proc scan-hidden { args } {
161 set testcase [testname-for-summary]
162 # The name might include a list of options; extract the file name.
163 set filename [lindex $testcase 0]
164 set output_file "[file rootname [file tail $filename]].s"
165
166 if { [llength $args] > 0 } {
167 set symbol [lindex $args 0]
168
169 set hidden_scan [hidden-scan-for $symbol]
170
171 set args [lreplace $args 0 0 "$hidden_scan"]
172 }
173
174 dg-scan "scan-hidden" 1 $testcase $output_file $args
175 }
176
177 # Check that a symbol is not defined as a hidden symbol in the .s file
178 # produced by the compiler.
179
180 proc scan-not-hidden { args } {
181 set testcase [testname-for-summary]
182 # The name might include a list of options; extract the file name.
183 set filename [lindex $testcase 0]
184 set output_file "[file rootname [file tail $filename]].s"
185
186 if { [llength $args] > 0 } {
187 set symbol [lindex $args 0]
188 set hidden_scan [hidden-scan-for $symbol]
189
190 set args [lreplace $args 0 0 "$hidden_scan"]
191 }
192
193 dg-scan "scan-not-hidden" 0 $testcase $output_file $args
194 }
195
196 # Check that symbols are emitted in the desired section.
197 # Like scan-symbol-section, but using the assembly output generated by
198 # the compiler.
199 #
200 # Example:
201 #
202 # // All numbered functions (func1, func2, etc.) must be in the .text section or
203 # // in a .text sub-section (like .text._func1).
204 # { dg-final { scan-assembler-symbol-section {^_func[1-5]$} {^\.text($|\.)} } }
205
206 proc scan-assembler-symbol-section { args } {
207 set testcase [testname-for-summary]
208 set filename [lindex $testcase 0]
209 set output_file "[file rootname [file tail $filename]].s"
210 dg-scan-symbol-section \
211 "scan-assembler-symbol-section" $testcase $output_file $args
212 }
213
214 # Check that symbols are emitted in the desired section.
215 #
216 # Symbols and sections are interpreted as regexp patterns.
217 #
218 # If no matching symbol is found, scan-symbol-section reports a FAILure.
219 #
220 # Usage:
221 #
222 # { dg-final { scan-symbol-section FILENAME SYMBOL SECTION } }
223 #
224 # Examples:
225 #
226 # // The my_var C symbol must be in the .data section (or in a .data._my_var section
227 # // if -ffunction-sections is in use).
228 # { dg-final { scan-symbol-section "my-test.s" {^_my_var$} {^\.data(\._my_var)?$} } }
229 #
230 # // All numbered functions (func1, func2, etc.) must be in the .text section
231 # // (and not in any other section like .text._func1).
232 # { dg-final { scan-symbol-section "my-test.s" {^_func[1-5]$} {^\.text$} } }
233 #
234 # Caveats:
235 #
236 # * Only ELF and Mach-O targets are supported. Other
237 # targets, like PE/COFF, might appear to work.
238 # * For Mach-O targets, the section name matched by scan-symbol-section has one
239 # of two forms:
240 # * The Mach-O segment name followed by a comma (',') followed by the Mach-O
241 # section name. For example, "__TEXT,__text". (There is no whitespace
242 # between the Mach-O segment name and the Mach-O section name.)
243 # * ".const", ".data", or ".text". For example, the .text assembler directive
244 # causes the section name to be ".text" (not "__TEXT,__text"). (However, a
245 # directive such as .section __TEXT,__text will cause the section name to be
246 # "__TEXT,__text".)
247 # * Because scan-symbol-section parses assembly code, scan-symbol-section is
248 # unaware of section rewriting performed by the linker. For example, the
249 # sections .text._f1 and .text._f2 would normally be merged by binutils'
250 # linker into one section called .text, but scan-symbol-section reports the
251 # sections as .text._f1 and .text._f2 (and not .text).
252 # * The symbol pattern matches any assembly label, including local labels which
253 # begin with `.L`.
254
255 proc scan-symbol-section { args } {
256 set testcase [testname-for-summary]
257 set output_file [lindex $args 0]
258 dg-scan-symbol-section \
259 "scan-symbol-section" $testcase $output_file $args
260 }
261
262 # Check that symbols are emitted in the desired section. The ORIG_ARGS is
263 # the list of arguments provided by dg-final to scan-symbol-section or
264 # scan-assembler-symbol-section. The first element in ORIG_ARGS is the
265 # regular expression to look for in the file. The second element, if
266 # present, is a DejaGNU target selector.
267 #
268 # Avoid calling this function directly. In tests, use scan-symbol-section,
269 # scan-assembler-symbol-section, or scan-lto-assembler-symbol-section instead.
270
271 proc dg-scan-symbol-section { name testcase output_file orig_args } {
272 if { [llength $orig_args] < 2 } {
273 error "$name: too few arguments"
274 return
275 }
276 if { [llength $orig_args] > 4 } {
277 error "$name: too many arguments"
278 return
279 }
280 switch $name {
281 "scan-symbol-section" { set arg_incr 1 }
282 default { set arg_incr 0 }
283 }
284 if { [llength $orig_args] >= 3 } {
285 switch [dg-process-target [lindex $orig_args [expr 2 + $arg_incr]]] {
286 "S" { }
287 "N" { return }
288 "F" { setup_xfail "*-*-*" }
289 "P" { }
290 }
291 }
292
293 set symbol_pattern [lindex $orig_args [expr 0 + $arg_incr]]
294 set expected_section_pattern [lindex $orig_args [expr 1 + $arg_incr]]
295
296 set printable_symbol_pattern [make_pattern_printable $symbol_pattern]
297 set printable_expected_section_pattern [make_pattern_printable $expected_section_pattern]
298
299 set files [dg_glob_remote $output_file]
300 if { $files == "" } {
301 verbose -log "$testcase: output file does not exist"
302 unresolved "$testcase $name symbol $printable_symbol_pattern has section $printable_expected_section_pattern"
303 return
304 }
305
306 parse_section_of_symbols $output_file section_by_symbol
307
308 set found_symbol 0
309 foreach symbol_name [lsort [array names section_by_symbol]] {
310 if { [regexp -- $symbol_pattern $symbol_name] } {
311 set section $section_by_symbol($symbol_name)
312 if { [istarget powerpc*-*-*] && $section == ".opd" } {
313 set section $section_by_symbol(.L.$symbol_name)
314 }
315 set description "$testcase $name symbol $printable_symbol_pattern (found $symbol_name) has section $printable_expected_section_pattern"
316 if { $section == "" } {
317 fail "$description (no section detected)"
318 } else {
319 set description "$description (found $section)"
320 if { [regexp -- $expected_section_pattern $section] } {
321 pass $description
322 } else {
323 fail $description
324 }
325 }
326 set found_symbol 1
327 }
328 }
329 if { ! $found_symbol } {
330 fail "$testcase $name symbol $printable_symbol_pattern (symbol not found) has section $printable_expected_section_pattern"
331 }
332 }
333
334 # Extract a symbol and section names from pre-processed assembly source code.
335 #
336 # This function adds entries in the RESULT array where the key is the symbol's
337 # name (including any leading underscores) and the value is the section's name
338 # (including any leading periods).
339 #
340 # For example, given the following assembly source code in file.s:
341 #
342 # .text
343 # .function _my_function
344 # _my_function:
345 # nop
346 # .data
347 # _my_data:
348 # .long 42
349 #
350 # Executing 'parse_section_of_symbols "file.s" symbols' would have the same
351 # effect as the following code:
352 #
353 # set $result(_my_function) .text
354 # set $result(_my_data) .data
355
356 proc parse_section_of_symbols { filename result } {
357 upvar $result up_result
358
359 set section_pattern {^\s*(?:(?:\.section|\.csect)\s+(.*)|(\.const|\.data|\.text)\s*)$}
360 set label_pattern {^(\S+):$}
361 set previous_pattern {^\s*\.previous\s*$}
362
363 set fd [open $filename r]
364 set current_section ""
365 set prev_section ""
366 while { [gets $fd line] >= 0 } {
367 if { [regexp -- $label_pattern $line dummy symbol_name] } {
368 set up_result($symbol_name) $current_section
369 } elseif { [regexp -- $section_pattern $line dummy section_directive_arguments full_section_directive] } {
370 set prev_section $current_section
371 if { $full_section_directive eq "" } {
372 # Example: .section .text,"ax",progbits
373 # Example: .section ".text",#alloc,#execinstr,#progbits
374 # Example: .section __TEXT,__text
375 set arguments [split $section_directive_arguments ","]
376 set current_section [string trim [lindex $arguments 0]]
377 set current_section [string trim $current_section {"}]
378 set arg_1 [string trim [lindex $arguments 1]]
379 if { [regexp {^_} $arg_1] } {
380 # The second argument looks like a Mach-O section name.
381 set current_section "$current_section,$arg_1"
382 }
383 } else {
384 # Example: .text
385 set current_section "$full_section_directive"
386 }
387 } elseif { [regexp -- $previous_pattern $line dummy] } {
388 set sect $prev_section
389 set prev_section $current_section
390 set current_section $sect
391 }
392 }
393 close $fd
394 }
395
396 # Look for a pattern in OUTPUT_FILE. See dg-scan for details.
397
398 proc scan-file { output_file args } {
399 set testcase [testname-for-summary]
400 dg-scan "scan-file" 1 $testcase $output_file $args
401 }
402
403 # Check that a pattern is not present in the OUTPUT_FILE. See dg-scan
404 # for details.
405
406 proc scan-file-not { output_file args } {
407 set testcase [testname-for-summary]
408 dg-scan "scan-file-not" 0 $testcase $output_file $args
409 }
410
411 # Look for a pattern in the .su file produced by the compiler. See
412 # dg-scan for details.
413
414 proc scan-stack-usage { args } {
415 set testcase [testname-for-summary]
416 # The name might include a list of options; extract the file name.
417 set filename [lindex $testcase 0]
418 set output_file "[file rootname [file tail $filename]].su"
419
420 dg-scan "scan-stack-usage" 1 $testcase $output_file $args
421 }
422
423 # Check that a pattern is not present in the .su file produced by the
424 # compiler. See dg-scan for details.
425
426 proc scan-stack-usage-not { args } {
427 set testcase [testname-for-summary]
428 # The name might include a list of options; extract the file name.
429 set filename [lindex $testcase 0]
430 set output_file "[file rootname [file tail $filename]].su"
431
432 dg-scan "scan-stack-usage-not" 0 $testcase $output_file $args
433 }
434
435 # Return the filename of the Ada spec corresponding to the argument.
436
437 proc get_ada_spec_filename { testcase } {
438 # The name might include a list of options; extract the file name.
439 set filename [lindex $testcase 0]
440 set tailname [file tail $filename]
441 set extension [string trimleft [file extension $tailname] {.}]
442 regsub -all {\-} [file rootname $tailname] {_} rootname
443
444 return [string tolower "${rootname}_${extension}.ads"]
445 }
446
447 # Look for a pattern in the .ads file produced by the compiler. See
448 # dg-scan for details.
449
450 proc scan-ada-spec { args } {
451 set testcase [testname-for-summary]
452 set output_file "[get_ada_spec_filename $testcase]"
453
454 dg-scan "scan-ada-spec" 1 $testcase $output_file $args
455 }
456
457 # Check that a pattern is not present in the .ads file produced by the
458 # compiler. See dg-scan for details.
459
460 proc scan-ada-spec-not { args } {
461 set testcase [testname-for-summary]
462 set output_file "[get_ada_spec_filename $testcase]"
463
464 dg-scan "scan-ada-spec-not" 0 $testcase $output_file $args
465 }
466
467 # Call pass if pattern is present given number of times, otherwise fail.
468 proc scan-assembler-times { args } {
469 if { [llength $args] < 2 } {
470 error "scan-assembler-times: too few arguments"
471 return
472 }
473 if { [llength $args] > 3 } {
474 error "scan-assembler-times: too many arguments"
475 return
476 }
477 if { [llength $args] >= 3 } {
478 switch [dg-process-target [lindex $args 2]] {
479 "S" { }
480 "N" { return }
481 "F" { setup_xfail "*-*-*" }
482 "P" { }
483 }
484 }
485
486 set testcase [testname-for-summary]
487 # The name might include a list of options; extract the file name.
488 set filename [lindex $testcase 0]
489 set pattern [lindex $args 0]
490 set times [lindex $args 1]
491 set pp_pattern [make_pattern_printable $pattern]
492
493 # This must match the rule in gcc-dg.exp.
494 set output_file "[file rootname [file tail $filename]].s"
495
496 set files [glob -nocomplain $output_file]
497 if { $files == "" } {
498 verbose -log "$testcase: output file does not exist"
499 unresolved "$testcase scan-assembler-times $pp_pattern $times"
500 return
501 }
502
503 set fd [open $output_file r]
504 set text [read $fd]
505 close $fd
506 regsub -all {(^|\n)[[:space:]]*\.section[[:space:]]*"?\.gnu\.lto_(?:[^\n]*\n(?![[:space:]]*\.(section|text|data|bss)))*[^\n]*\n} $text {\1} text
507
508 set result_count [regexp -all -- $pattern $text]
509 if {$result_count == $times} {
510 pass "$testcase scan-assembler-times $pp_pattern $times"
511 } else {
512 verbose -log "$testcase: $pp_pattern found $result_count times"
513 fail "$testcase scan-assembler-times $pp_pattern $times"
514 }
515 }
516
517 set_required_options_for scan-assembler-times
518
519 # Call pass if pattern is present within a lower or upper bound,
520 # otherwise fail.
521 # ex /* { dg-final { scan-assembler-bound {RE} > 3 } }
522 proc scan-assembler-bound { args } {
523 if { [llength $args] < 3 } {
524 error "scan-assembler-bound: too few arguments"
525 return
526 }
527 if { [llength $args] > 4 } {
528 error "scan-assembler-bound: too many arguments"
529 return
530 }
531 if { [llength $args] >= 4 } {
532 switch [dg-process-target [lindex $args 3]] {
533 "S" { }
534 "N" { return }
535 "F" { setup_xfail "*-*-*" }
536 "P" { }
537 }
538 }
539
540 set testcase [testname-for-summary]
541 # The name might include a list of options; extract the file name.
542 set filename [lindex $testcase 0]
543 set pattern [lindex $args 0]
544 set cmp [lindex $args 1]
545 set bound [lindex $args 2]
546 set pp_pattern [make_pattern_printable $pattern]
547
548 # This must match the rule in gcc-dg.exp.
549 set output_file "[file rootname [file tail $filename]].s"
550
551 set files [glob -nocomplain $output_file]
552 if { $files == "" } {
553 verbose -log "$testcase: output file does not exist"
554 unresolved "$testcase scan-assembler-bound $pp_pattern $min $max"
555 return
556 }
557
558 if { [lsearch { < > <= >= } $cmp] == -1 } {
559 error "scan-assembler-bound: illegal argument: $cmp"
560 return
561 }
562 if ![string is integer $bound ] {
563 error "scan-assembler-bound: illegal argument: $bound"
564 return
565 }
566
567 set fd [open $output_file r]
568 set text [read $fd]
569 close $fd
570 regsub -all {(^|\n)[[:space:]]*\.section[[:space:]]*"?\.gnu\.lto_(?:[^\n]*\n(?![[:space:]]*\.(section|text|data|bss)))*[^\n]*\n} $text {\1} text
571
572 set result_count [regexp -all -- $pattern $text]
573 if [expr $result_count $cmp $bound] {
574 pass "$testcase scan-assembler-bound $pp_pattern $cmp $bound"
575 } else {
576 verbose -log "$testcase: $pp_pattern found $result_count times"
577 fail "$testcase scan-assembler-bound $pp_pattern $cmp $bound"
578 }
579 }
580
581 set_required_options_for scan-assembler-bound
582
583 # Utility for scanning demangled compiler result, invoked via dg-final.
584 # Call pass if pattern is present, otherwise fail.
585 proc scan-assembler-dem { args } {
586 global cxxfilt
587 global base_dir
588
589 if { [llength $args] < 1 } {
590 error "scan-assembler-dem: too few arguments"
591 return
592 }
593 if { [llength $args] > 2 } {
594 error "scan-assembler-dem: too many arguments"
595 return
596 }
597 if { [llength $args] >= 2 } {
598 switch [dg-process-target [lindex $args 1]] {
599 "S" { }
600 "N" { return }
601 "F" { setup_xfail "*-*-*" }
602 "P" { }
603 }
604 }
605
606 # Find c++filt like we find g++ in g++.exp.
607 if ![info exists cxxfilt] {
608 set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
609 $base_dir/../../../binutils/cxxfilt \
610 [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
611 [findfile $base_dir/c++filt $base_dir/c++filt \
612 [transform c++filt]]]]
613 verbose -log "c++filt is $cxxfilt"
614 }
615
616 set testcase [testname-for-summary]
617 # The name might include a list of options; extract the file name.
618 set filename [lindex $testcase 0]
619 set pattern [lindex $args 0]
620 set pp_pattern [make_pattern_printable $pattern]
621 set output_file "[file rootname [file tail $filename]].s"
622
623 set files [glob -nocomplain $output_file]
624 if { $files == "" } {
625 verbose -log "$testcase: output file does not exist"
626 unresolved "$testcase scan-assembler-dem $pp_pattern"
627 return
628 }
629
630 set output [remote_exec host "$cxxfilt" "" "$output_file"]
631 set text [lindex $output 1]
632 regsub -all {(^|\n)[[:space:]]*\.section[[:space:]]*"?\.gnu\.lto_(?:[^\n]*\n(?![[:space:]]*\.(section|text|data|bss)))*[^\n]*\n} $text {\1} text
633
634 if [regexp -- $pattern $text] {
635 pass "$testcase scan-assembler-dem $pp_pattern"
636 } else {
637 fail "$testcase scan-assembler-dem $pp_pattern"
638 }
639 }
640
641 # Call pass if demangled pattern is not present, otherwise fail.
642 proc scan-assembler-dem-not { args } {
643 global cxxfilt
644 global base_dir
645
646 if { [llength $args] < 1 } {
647 error "scan-assembler-dem-not: too few arguments"
648 return
649 }
650 if { [llength $args] > 2 } {
651 error "scan-assembler-dem-not: too many arguments"
652 return
653 }
654 if { [llength $args] >= 2 } {
655 switch [dg-process-target [lindex $args 1]] {
656 "S" { }
657 "N" { return }
658 "F" { setup_xfail "*-*-*" }
659 "P" { }
660 }
661 }
662
663 # Find c++filt like we find g++ in g++.exp.
664 if ![info exists cxxfilt] {
665 set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
666 $base_dir/../../../binutils/cxxfilt \
667 [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
668 [findfile $base_dir/c++filt $base_dir/c++filt \
669 [transform c++filt]]]]
670 verbose -log "c++filt is $cxxfilt"
671 }
672
673 set testcase [testname-for-summary]
674 # The name might include a list of options; extract the file name.
675 set filename [lindex $testcase 0]
676 set pattern [lindex $args 0]
677 set pp_pattern [make_pattern_printable $pattern]
678 set output_file "[file rootname [file tail $filename]].s"
679
680 set files [glob -nocomplain $output_file]
681 if { $files == "" } {
682 verbose -log "$testcase: output file does not exist"
683 unresolved "$testcase scan-assembler-dem-not $pp_pattern"
684 return
685 }
686
687 set output [remote_exec host "$cxxfilt" "" "$output_file"]
688 set text [lindex $output 1]
689 regsub -all {(^|\n)[[:space:]]*\.section[[:space:]]*"?\.gnu\.lto_(?:[^\n]*\n(?![[:space:]]*\.(section|text|data|bss)))*[^\n]*\n} $text {\1} text
690
691 if ![regexp -- $pattern $text] {
692 pass "$testcase scan-assembler-dem-not $pp_pattern"
693 } else {
694 fail "$testcase scan-assembler-dem-not $pp_pattern"
695 }
696 }
697
698 # Call pass if object size is ok, otherwise fail.
699 # example: /* { dg-final { object-size text <= 54 } } */
700 proc object-size { args } {
701 global size
702 global base_dir
703
704 if { [llength $args] < 3 } {
705 error "object-size: too few arguments"
706 return
707 }
708 if { [llength $args] > 4 } {
709 error "object-size: too many arguments"
710 return
711 }
712 if { [llength $args] >= 4 } {
713 switch [dg-process-target [lindex $args 3]] {
714 "S" { }
715 "N" { return }
716 "F" { setup_xfail "*-*-*" }
717 "P" { }
718 }
719 }
720
721 # Find size like we find g++ in g++.exp.
722 if ![info exists size] {
723 set size [findfile $base_dir/../../../binutils/size \
724 $base_dir/../../../binutils/size \
725 [findfile $base_dir/../../size $base_dir/../../size \
726 [findfile $base_dir/size $base_dir/size \
727 [transform size]]]]
728 verbose -log "size is $size"
729 }
730
731 set testcase [testname-for-summary]
732 # The name might include a list of options; extract the file name.
733 set filename [lindex $testcase 0]
734 set what [lindex $args 0]
735 set where [lsearch { text data bss total } $what]
736 if { $where == -1 } {
737 error "object-size: illegal argument: $what"
738 return
739 }
740 set cmp [lindex $args 1]
741 if { [lsearch { < > <= >= == != } $cmp] == -1 } {
742 error "object-size: illegal argument: $cmp"
743 return
744 }
745 set with [lindex $args 2]
746 if ![string is integer $with ] {
747 error "object-size: illegal argument: $with"
748 return
749 }
750
751 set output_file "[file rootname [file tail $filename]].o"
752 if ![file_on_host exists $output_file] {
753 verbose -log "$testcase: $output_file does not exist"
754 unresolved "$testcase object-size $what $cmp $with"
755 return
756 }
757 set output [remote_exec host "$size" "$output_file"]
758 set status [lindex $output 0]
759 if { $status != 0 } {
760 verbose -log "$testcase object-size: $size failed"
761 unresolved "$testcase object-size $what $cmp $with"
762 return
763 }
764
765 set text [lindex $output 1]
766 set lines [split $text "\n"]
767
768 set l0match {^\s*text\s+data\s+bss\s+dec\s+hex\s+filename\s*$}
769 set l1match {^\s*\d+\s+\d+\s+\d+\s+\d+\s+[\da-fA-F]+\s+}
770
771 if { [istarget *-*-darwin*] } {
772 set l0match {^\s*__TEXT\s+__DATA\s+__OBJC\s+others\s+dec\s+hex\s*$}
773 set l1match {^\s*\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+[\da-fA-F]+\s+}
774 }
775
776 set line0 [lindex $lines 0]
777 if ![regexp $l0match $line0] {
778 verbose -log "$testcase object-size: $size did not produce expected first line: $line0"
779 unresolved "$testcase object-size $what $cmp $with"
780 return
781 }
782
783 set line1 [lindex $lines 1]
784 if ![regexp $l1match $line1] {
785 verbose -log "$testcase object-size: $size did not produce expected second line: $line1"
786 unresolved "$testcase object-size $what $cmp $with"
787 return
788 }
789
790 set actual [lindex $line1 $where]
791 verbose -log "$what size is $actual"
792
793 if [expr $actual $cmp $with] {
794 pass "$testcase object-size $what $cmp $with"
795 } else {
796 fail "$testcase object-size $what $cmp $with"
797 }
798 }
799
800 # Utility for testing that a function is defined on the current line.
801 # Call pass if so, otherwise fail. Invoked directly; the file must
802 # have been compiled with -g -dA.
803 #
804 # Argument 0 is the current line, passed implicitly by dejagnu
805 # Argument 1 is the function to check
806 # Argument 2 handles expected failures and the like
807 # Argument 3 is "." to match the current line, or an integer to match
808 # an explicit line.
809 proc dg-function-on-line { args } {
810 # Upvar from dg-final:
811 upvar dg-final-code final-code
812
813 set line [lindex $args 0]
814 set symbol [lindex $args 1]
815 set failures [lindex $args 2]
816
817 if { [llength $args] >= 4 } {
818 switch [lindex $args 3] {
819 "." { }
820 "default" { set line [lindex $args 3] }
821 }
822 }
823
824 if { [istarget hppa*-*-linux*] } {
825 set pattern [format {%s:\n\t.PROC\n\t.CALLINFO.*\n\t.ENTRY\n.L.*:\n(\t.file[^\t]*)*\t[^:]+:%d(:[0-9]+)?\n} \
826 $symbol $line]
827 } elseif { [istarget hppa*-*-*] } {
828 set pattern [format {\t;[^:]+:%d(:[0-9]+)?\n(\t[^\t]+\n)+%s:\n\t.PROC} \
829 $line $symbol]
830 } elseif { [istarget mips*-*-*] } {
831 set pattern [format {\t\.loc [0-9]+ %d [0-9]+( [^\n]*)?\n(\t.cfi_startproc[^\t]*\n)*\t\.set\t(no)?mips16\n\t(\.set\t(no)?micromips\n\t)?\.ent\t%s\n\t\.type\t%s, @function\n%s:\n} \
832 $line $symbol $symbol $symbol]
833 } elseif { [istarget microblaze*-*-*] } {
834 set pattern [format {:%d(:[0-9]+)?\n\$.*:\n\t\.ent\t%s\n\t\.type\t%s, @function\n%s:\n} \
835 $line $symbol $symbol $symbol]
836 } else {
837 set pattern [format {%s:[^\t]*(\t.(fnstart|frame|mask|file)[^\t]*)*\t[^:]+:%d(:[0-9]+)?\n} \
838 $symbol $line]
839 }
840
841 # The lack of spaces around $pattern is important, since they'd
842 # become part of the regex scan-assembler tries to match.
843 set cmd "scan-assembler {$pattern}"
844 if { [llength $args] >= 3 } {
845 set cmd "$cmd {$failures}"
846 }
847
848 append final-code "$cmd\n"
849 }
850
851 # Look for a pattern in the .exe.ltrans0.s file produced by the
852 # compiler. See dg-scan for details.
853
854 proc scan-lto-assembler { args } {
855 set testcase [testname-for-summary]
856 # The name might include a list of options; extract the file name.
857 set filename [lindex $testcase 0]
858 set output_file "[file rootname [file tail $filename]].ltrans0.ltrans.s"
859 verbose "output_file: $output_file"
860 dg-scan "scan-lto-assembler" 1 $testcase $output_file $args
861 }
862
863
864 # Set up CONFIG for check-function-bodies.
865
866 proc configure_check-function-bodies { config } {
867 upvar $config up_config
868
869 # Regexp for the start of a function definition (name in \1).
870 if { [istarget nvptx*-*-*] } {
871 set up_config(start) {
872 {^// BEGIN(?: GLOBAL|) FUNCTION DEF: ([a-zA-Z_]\S*)$}
873 }
874 } elseif { [istarget *-*-darwin*] } {
875 set up_config(start) {
876 {^_([a-zA-Z_]\S*):$}
877 {^LFB[0-9]+:}
878 }
879 } else {
880 set up_config(start) {{^([a-zA-Z_]\S*):$}}
881 }
882
883 # Regexp for the end of a function definition.
884 if { [istarget nvptx*-*-*] } {
885 set up_config(end) {^\}$}
886 } elseif { [istarget *-*-darwin*] } {
887 set up_config(end) {^LFE[0-9]+}
888 } else {
889 set up_config(end) {^\s*\.size}
890 }
891
892 # Regexp for lines that aren't interesting.
893 if { [istarget nvptx*-*-*] } {
894 # Skip lines beginning with '//' comments ('-fverbose-asm', for
895 # example).
896 set up_config(fluff) {^\s*(?://)}
897 } elseif { [istarget *-*-darwin*] } {
898 set up_config(fluff) {^\s*(?:\.|//|@)|^L[0-9ABCESV]}
899 } else {
900 # Skip lines beginning with labels ('.L[...]:') or other directives
901 # ('.align', '.cfi_startproc', '.quad [...]', '.text', etc.), '//' or
902 # '@' comments ('-fverbose-asm' or ARM-style, for example), or empty
903 # lines.
904 set up_config(fluff) {^\s*(?:\.|//|@|$)}
905 }
906
907 # Regexp for expected output lines prefix.
908 if { [istarget nvptx*-*-*] } {
909 # Certain instructions (such as predicable ones) are emitted with
910 # additional whitespace prefixed. On the other hand, labels don't get
911 # any whitespace prefixed, and we'd like to be able to match these,
912 # too. We thereare expect any amount of whitespace here.
913 set up_config(line_prefix) {\t*}
914 } else {
915 set up_config(line_prefix) {\t}
916 }
917 }
918
919 # Per CONFIG, read assembly file FILENAME and store a mapping from function
920 # names to function bodies in array RESULT. FILENAME has already been uploaded
921 # locally where necessary and is known to exist.
922
923 proc parse_function_bodies { config filename result } {
924 upvar $config up_config
925 upvar $result up_result
926
927 set fd [open $filename r]
928 set in_function 0
929 while { [gets $fd line] >= 0 } {
930 if { $in_function == 0 } {
931 if { [regexp [lindex $up_config(start) 0] \
932 $line dummy function_name] } {
933 set in_function 1
934 set function_body ""
935 }
936 } elseif { $in_function < [llength $up_config(start)] } {
937 if { [regexp [lindex $up_config(start) $in_function] $line] } {
938 incr in_function
939 } else {
940 verbose "parse_function_bodies: skipped $function_name"
941 set in_function 0
942 }
943 } elseif { $in_function } {
944 # We allow multiple function start labels, taking the last one seen
945 # as the function name.
946 if { [regexp [lindex $up_config(start) 0] \
947 $line dummy maybe_function_name] } {
948 verbose "parse_function_bodies: overriding $function_name with $maybe_function_name"
949 set function_name $maybe_function_name
950 set in_function 1
951 } elseif { [regexp $up_config(end) $line] } {
952 verbose "parse_function_bodies: $function_name:\n$function_body"
953 set up_result($function_name) $function_body
954 set in_function 0
955 } elseif { $up_config(matched) ne "" \
956 && [regexp $up_config(matched) $line] } {
957 append function_body $line "\n"
958 } elseif { [regexp $up_config(fluff) $line] } {
959 verbose "parse_function_bodies: $function_name: ignoring fluff line: $line"
960 } else {
961 append function_body $line "\n"
962 }
963 }
964 }
965 close $fd
966 }
967
968 # FUNCTIONS is an array that maps function names to function bodies.
969 # Return true if it contains a definition of function NAME and if
970 # that definition matches BODY_REGEXP.
971
972 proc check_function_body { functions name body_regexp } {
973 upvar $functions up_functions
974
975 if { ![info exists up_functions($name)] } {
976 return 0
977 }
978 set fn_res [regexp "^$body_regexp\$" $up_functions($name)]
979 if { !$fn_res } {
980 verbose -log "body: $body_regexp"
981 verbose -log "against: $up_functions($name)"
982 }
983 return $fn_res
984 }
985
986 # Check the implementations of functions against expected output. Used as:
987 #
988 # { dg-final { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR [MATCHED]]] } }
989 #
990 # See sourcebuild.texi for details.
991
992 proc check-function-bodies { args } {
993 if { [llength $args] < 2 } {
994 error "too few arguments to check-function-bodies"
995 }
996 if { [llength $args] > 5 } {
997 error "too many arguments to check-function-bodies"
998 }
999
1000 upvar 2 dg-extra-tool-flags extra_tool_flags
1001 set flags $extra_tool_flags
1002
1003 global torture_current_flags
1004 if { [info exists torture_current_flags] } {
1005 append flags " " $torture_current_flags
1006 }
1007
1008 if { [llength $args] >= 3 } {
1009 set required_flags [lindex $args 2]
1010
1011 foreach required_flag $required_flags {
1012 switch -- $required_flag {
1013 target -
1014 xfail {
1015 error "misplaced $required_flag in check-function-bodies"
1016 }
1017 }
1018 }
1019 foreach required_flag $required_flags {
1020 if { ![regexp " $required_flag " $flags] } {
1021 return
1022 }
1023 }
1024 }
1025
1026 set xfail_all 0
1027 if { [llength $args] >= 4 } {
1028 switch [dg-process-target [lindex $args 3]] {
1029 "S" { }
1030 "N" { return }
1031 "F" { set xfail_all 1 }
1032 "P" { }
1033 }
1034 }
1035
1036 set matched ""
1037 if { [llength $args] >= 5 } {
1038 set matched [lindex $args 4]
1039 }
1040
1041 set testcase [testname-for-summary]
1042 # The name might include a list of options; extract the file name.
1043 set filename [lindex $testcase 0]
1044
1045 global srcdir
1046 set input_filename "$srcdir/$filename"
1047 set output_filename "[file rootname [file tail $filename]]"
1048 if { [string match "* -flto *" " ${flags} "]
1049 && ![string match "* -fno-use-linker-plugin *" " ${flags} "]
1050 && ![string match "* -ffat-lto-objects *" " ${flags} "] } {
1051 append output_filename ".ltrans0.ltrans.s"
1052 } else {
1053 append output_filename ".s"
1054 }
1055
1056 set prefix [lindex $args 0]
1057 set prefix_len [string length $prefix]
1058 set terminator [lindex $args 1]
1059 if { [string equal $terminator ""] } {
1060 set terminator "*/"
1061 }
1062 set terminator_len [string length $terminator]
1063 # Regexp for the start of a function definition in expected output lines
1064 # (name in \1). This may be different from '$config(start)'.
1065 set start_expected {^(\S+):$}
1066
1067 set config(matched) $matched
1068 configure_check-function-bodies config
1069 set have_bodies 0
1070 if { [is_remote host] } {
1071 remote_upload host "$filename"
1072 }
1073 if { [file exists $output_filename] } {
1074 parse_function_bodies config $output_filename functions
1075 set have_bodies 1
1076 } else {
1077 verbose -log "$testcase: output file does not exist"
1078 }
1079
1080 set count 0
1081 set function_regexp ""
1082
1083 set lineno 1
1084 set fd [open $input_filename r]
1085 set in_function 0
1086 while { [gets $fd line] >= 0 } {
1087 if { [string equal -length $prefix_len $line $prefix] } {
1088 set line [string trim [string range $line $prefix_len end]]
1089 if { !$in_function } {
1090 if { [regexp "^(.*?\\S)\\s+{(.*)}\$" $line dummy \
1091 line selector] } {
1092 set selector [dg-process-target $selector]
1093 } else {
1094 set selector "P"
1095 }
1096 if { ![regexp $start_expected $line dummy function_name] } {
1097 close $fd
1098 error "check-function-bodies: line $lineno does not have a function label"
1099 }
1100 set in_function 1
1101 set function_regexp ""
1102 } elseif { [string equal $line "("] } {
1103 append function_regexp "(?:"
1104 } elseif { [string equal $line "|"] } {
1105 append function_regexp "|"
1106 } elseif { [string equal $line ")"] } {
1107 append function_regexp ")"
1108 } elseif { [string equal $line "..."] } {
1109 append function_regexp ".*"
1110 } elseif { [regexp {^\.L} $line] } {
1111 append function_regexp $line "\n"
1112 } else {
1113 append function_regexp $config(line_prefix) $line "\n"
1114 }
1115 } elseif { [string equal -length $terminator_len $line $terminator] } {
1116 if { ![string equal $selector "N"] } {
1117 if { $xfail_all || [string equal $selector "F"] } {
1118 setup_xfail "*-*-*"
1119 }
1120 set testname "$testcase check-function-bodies $function_name"
1121 if { !$have_bodies } {
1122 unresolved $testname
1123 } elseif { [check_function_body functions $function_name \
1124 $function_regexp] } {
1125 pass $testname
1126 } else {
1127 fail $testname
1128 }
1129 }
1130 set in_function 0
1131 incr count
1132 }
1133 incr lineno
1134 }
1135 close $fd
1136 if { $in_function } {
1137 error "check-function-bodies: missing \"$terminator\""
1138 }
1139 if { $count == 0 } {
1140 error "check-function-bodies: no matches found"
1141 }
1142 }