1# Copyright (C) 2000-2018 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.
23proc make_pattern_printable { pattern } {
24    return [string map {\t \\t \n \\n \r \\r \\ \\\\} $pattern]
25}
26
27# Scan the OUTPUT_FILE for a pattern.  If it is present and POSITIVE
28# is non-zero, or it is not present and POSITIVE is zero, the test
29# passes.  The ORIG_ARGS is the list of arguments provided by dg-final
30# to scan-assembler.  The first element in ORIG_ARGS is the regular
31# expression to look for in the file.  The second element, if present,
32# is a DejaGNU target selector.
33
34proc dg-scan { name positive testcase output_file orig_args } {
35    if { [llength $orig_args] < 1 } {
36	error "$name: too few arguments"
37        return
38    }
39    if { [llength $orig_args] > 2 } {
40	error "$name: too many arguments"
41	return
42    }
43    if { [llength $orig_args] >= 2 } {
44	switch [dg-process-target [lindex $orig_args 1]] {
45	    "S" { }
46	    "N" { return }
47	    "F" { setup_xfail "*-*-*" }
48	    "P" { }
49	}
50    }
51
52    set pattern [lindex $orig_args 0]
53    set printable_pattern [make_pattern_printable $pattern]
54
55    if { [is_remote host] } {
56	remote_upload host "$output_file"
57    }
58    set files [glob -nocomplain $output_file]
59    if { $files == "" } {
60	verbose -log "$testcase: output file does not exist"
61	unresolved "$testcase $name $printable_pattern"
62	return
63    }
64    set fd [open $output_file r]
65    set text [read $fd]
66    close $fd
67
68    set match [regexp -- $pattern $text]
69    if { $match == $positive } {
70	pass "$testcase $name $printable_pattern"
71    } else {
72	fail "$testcase $name $printable_pattern"
73    }
74}
75
76# Look for a pattern in the .s file produced by the compiler.  See
77# dg-scan for details.
78
79proc scan-assembler { args } {
80    set testcase [testname-for-summary]
81    # The name might include a list of options; extract the file name.
82    set filename [lindex $testcase 0]
83    set output_file "[file rootname [file tail $filename]].s"
84    dg-scan "scan-assembler" 1 $testcase $output_file $args
85}
86
87force_conventional_output_for scan-assembler
88
89# Check that a pattern is not present in the .s file produced by the
90# compiler.  See dg-scan for details.
91
92proc scan-assembler-not { args } {
93    set testcase [testname-for-summary]
94    # The name might include a list of options; extract the file name.
95    set filename [lindex $testcase 0]
96    set output_file "[file rootname [file tail $filename]].s"
97
98    dg-scan "scan-assembler-not" 0 $testcase $output_file $args
99}
100
101force_conventional_output_for scan-assembler-not
102
103# Return the scan for the assembly for hidden visibility.
104
105proc hidden-scan-for { symbol } {
106
107    set objformat [gcc_target_object_format]
108
109    switch $objformat {
110        coff     { return "$symbol\[,\d\]*hidden" }
111        elf      { return "hidden\[ \t_\]*$symbol" }
112        mach-o   { return "private_extern\[ \t_\]*_?$symbol" }
113        default  { return "" }
114    }
115
116}
117
118
119# Check that a symbol is defined as a hidden symbol in the .s file
120# produced by the compiler.
121
122proc scan-hidden { args } {
123    set testcase [testname-for-summary]
124    # The name might include a list of options; extract the file name.
125    set filename [lindex $testcase 0]
126    set output_file "[file rootname [file tail $filename]].s"
127
128    set symbol [lindex $args 0]
129
130    set hidden_scan [hidden-scan-for $symbol]
131
132    set args [lreplace $args 0 0 "$hidden_scan"]
133
134    dg-scan "scan-hidden" 1 $testcase $output_file $args
135}
136
137# Check that a symbol is not defined as a hidden symbol in the .s file
138# produced by the compiler.
139
140proc scan-not-hidden { args } {
141    set testcase [testname-for-summary]
142    # The name might include a list of options; extract the file name.
143    set filename [lindex $testcase 0]
144    set output_file "[file rootname [file tail $filename]].s"
145
146    set symbol [lindex $args 0]
147    set hidden_scan [hidden-scan-for $symbol]
148
149    set args [lreplace $args 0 0 "$hidden_scan"]
150
151    dg-scan "scan-not-hidden" 0 $testcase $output_file $args
152}
153
154# Look for a pattern in OUTPUT_FILE.  See dg-scan for details.
155
156proc scan-file { output_file args } {
157    set testcase [testname-for-summary]
158    dg-scan "scan-file" 1 $testcase $output_file $args
159}
160
161# Check that a pattern is not present in the OUTPUT_FILE.  See dg-scan
162# for details.
163
164proc scan-file-not { output_file args } {
165    set testcase [testname-for-summary]
166    dg-scan "scan-file-not" 0 $testcase $output_file $args
167}
168
169# Look for a pattern in the .su file produced by the compiler.  See
170# dg-scan for details.
171
172proc scan-stack-usage { args } {
173    set testcase [testname-for-summary]
174    # The name might include a list of options; extract the file name.
175    set filename [lindex $testcase 0]
176    set output_file "[file rootname [file tail $filename]].su"
177
178    dg-scan "scan-file" 1 $testcase $output_file $args
179}
180
181# Check that a pattern is not present in the .su file produced by the
182# compiler.  See dg-scan for details.
183
184proc scan-stack-usage-not { args } {
185    set testcase [testname-for-summary]
186    # The name might include a list of options; extract the file name.
187    set filename [lindex $testcase 0]
188    set output_file "[file rootname [file tail $filename]].su"
189
190    dg-scan "scan-file-not" 0 $testcase $output_file $args
191}
192
193# Return the filename of the Ada spec corresponding to the argument.
194
195proc get_ada_spec_filename { testcase } {
196    # The name might include a list of options; extract the file name.
197    set filename  [lindex $testcase 0]
198    set tailname  [file tail $filename]
199    set extension [string trimleft [file extension $tailname] {.}]
200    regsub -all {\-} [file rootname $tailname] {_} rootname
201
202    return [string tolower "${rootname}_${extension}.ads"]
203}
204
205# Look for a pattern in the .ads file produced by the compiler.  See
206# dg-scan for details.
207
208proc scan-ada-spec { args } {
209    set testcase  [testname-for-summary]
210    set output_file "[get_ada_spec_filename $testcase]"
211
212    dg-scan "scan-file" 1 $testcase $output_file $args
213}
214
215# Check that a pattern is not present in the .ads file produced by the
216# compiler.  See dg-scan for details.
217
218proc scan-ada-spec-not { args } {
219    set testcase  [testname-for-summary]
220    set output_file "[get_ada_spec_filename $testcase]"
221
222    dg-scan "scan-file-not" 0 $testcase $output_file $args
223}
224
225# Call pass if pattern is present given number of times, otherwise fail.
226proc scan-assembler-times { args } {
227    if { [llength $args] < 2 } {
228	error "scan-assembler: too few arguments"
229        return
230    }
231    if { [llength $args] > 3 } {
232	error "scan-assembler: too many arguments"
233	return
234    }
235    if { [llength $args] >= 3 } {
236	switch [dg-process-target [lindex $args 2]] {
237	    "S" { }
238	    "N" { return }
239	    "F" { setup_xfail "*-*-*" }
240	    "P" { }
241	}
242    }
243
244    set testcase [testname-for-summary]
245    # The name might include a list of options; extract the file name.
246    set filename [lindex $testcase 0]
247    set pattern [lindex $args 0]
248    set times [lindex $args 1]
249    set pp_pattern [make_pattern_printable $pattern]
250
251    # This must match the rule in gcc-dg.exp.
252    set output_file "[file rootname [file tail $filename]].s"
253
254    set files [glob -nocomplain $output_file]
255    if { $files == "" } {
256	verbose -log "$testcase: output file does not exist"
257	unresolved "$testcase scan-assembler-times $pp_pattern $times"
258	return
259    }
260
261    set fd [open $output_file r]
262    set text [read $fd]
263    close $fd
264
265    set result_count [llength [regexp -inline -all -- $pattern $text]]
266    if {$result_count == $times} {
267	pass "$testcase scan-assembler-times $pp_pattern $times"
268    } else {
269	verbose -log "$testcase: $pp_pattern found $result_count times"
270	fail "$testcase scan-assembler-times $pp_pattern $times"
271    }
272}
273
274force_conventional_output_for scan-assembler-times
275
276# Utility for scanning demangled compiler result, invoked via dg-final.
277# Call pass if pattern is present, otherwise fail.
278proc scan-assembler-dem { args } {
279    global cxxfilt
280    global base_dir
281
282    if { [llength $args] < 1 } {
283	error "scan-assembler-dem: too few arguments"
284        return
285    }
286    if { [llength $args] > 2 } {
287	error "scan-assembler-dem: too many arguments"
288	return
289    }
290    if { [llength $args] >= 2 } {
291	switch [dg-process-target [lindex $args 1]] {
292	    "S" { }
293	    "N" { return }
294	    "F" { setup_xfail "*-*-*" }
295	    "P" { }
296	}
297    }
298
299    # Find c++filt like we find g++ in g++.exp.
300    if ![info exists cxxfilt]  {
301	set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
302		     $base_dir/../../../binutils/cxxfilt \
303	             [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
304		      [findfile $base_dir/c++filt $base_dir/c++filt \
305		       [transform c++filt]]]]
306	verbose -log "c++filt is $cxxfilt"
307    }
308
309    set testcase [testname-for-summary]
310    # The name might include a list of options; extract the file name.
311    set filename [lindex $testcase 0]
312    set pattern [lindex $args 0]
313    set pp_pattern [make_pattern_printable $pattern]
314    set output_file "[file rootname [file tail $filename]].s"
315
316    set files [glob -nocomplain $output_file]
317    if { $files == "" } {
318	verbose -log "$testcase: output file does not exist"
319	unresolved "$testcase scan-assembler-dem $pp_pattern"
320	return
321    }
322
323    set output [remote_exec host "$cxxfilt" "" "$output_file"]
324    set text [lindex $output 1]
325
326    if [regexp -- $pattern $text] {
327	pass "$testcase scan-assembler-dem $pp_pattern"
328    } else {
329	fail "$testcase scan-assembler-dem $pp_pattern"
330    }
331}
332
333# Call pass if demangled pattern is not present, otherwise fail.
334proc scan-assembler-dem-not { args } {
335    global cxxfilt
336    global base_dir
337
338    if { [llength $args] < 1 } {
339	error "scan-assembler-dem-not: too few arguments"
340        return
341    }
342    if { [llength $args] > 2 } {
343	error "scan-assembler-dem-not: too many arguments"
344	return
345    }
346    if { [llength $args] >= 2 } {
347	switch [dg-process-target [lindex $args 1]] {
348	    "S" { }
349	    "N" { return }
350	    "F" { setup_xfail "*-*-*" }
351	    "P" { }
352	}
353    }
354
355    # Find c++filt like we find g++ in g++.exp.
356    if ![info exists cxxfilt]  {
357	set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
358		     $base_dir/../../../binutils/cxxfilt \
359		     [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
360		      [findfile $base_dir/c++filt $base_dir/c++filt \
361		       [transform c++filt]]]]
362	verbose -log "c++filt is $cxxfilt"
363    }
364
365    set testcase [testname-for-summary]
366    # The name might include a list of options; extract the file name.
367    set filename [lindex $testcase 0]
368    set pattern [lindex $args 0]
369    set pp_pattern [make_pattern_printable $pattern]
370    set output_file "[file rootname [file tail $filename]].s"
371
372    set files [glob -nocomplain $output_file]
373    if { $files == "" } {
374	verbose -log "$testcase: output file does not exist"
375	unresolved "$testcase scan-assembler-dem-not $pp_pattern"
376	return
377    }
378
379    set output [remote_exec host "$cxxfilt" "" "$output_file"]
380    set text [lindex $output 1]
381
382    if ![regexp -- $pattern $text] {
383	pass "$testcase scan-assembler-dem-not $pp_pattern"
384    } else {
385	fail "$testcase scan-assembler-dem-not $pp_pattern"
386    }
387}
388
389# Call pass if object size is ok, otherwise fail.
390# example: /* { dg-final { object-size text <= 54 } } */
391proc object-size { args } {
392    global size
393    global base_dir
394
395    if { [llength $args] < 3 } {
396	error "object-size: too few arguments"
397        return
398    }
399    if { [llength $args] > 4 } {
400	error "object-size: too many arguments"
401	return
402    }
403    if { [llength $args] >= 4 } {
404	switch [dg-process-target [lindex $args 3]] {
405	    "S" { }
406	    "N" { return }
407	    "F" { setup_xfail "*-*-*" }
408	    "P" { }
409	}
410    }
411
412    # Find size like we find g++ in g++.exp.
413    if ![info exists size]  {
414	set size [findfile $base_dir/../../../binutils/size \
415		  $base_dir/../../../binutils/size \
416		  [findfile $base_dir/../../size $base_dir/../../size \
417		   [findfile $base_dir/size $base_dir/size \
418		    [transform size]]]]
419	verbose -log "size is $size"
420    }
421
422    set testcase [testname-for-summary]
423    # The name might include a list of options; extract the file name.
424    set filename [lindex $testcase 0]
425    set what [lindex $args 0]
426    set where [lsearch { text data bss total } $what]
427    if { $where == -1 } {
428        error "object-size: illegal argument: $what"
429        return
430    }
431    set cmp [lindex $args 1]
432    if { [lsearch { < > <= >= == != } $cmp] == -1 } {
433        error "object-size: illegal argument: $cmp"
434        return
435    }
436    set with [lindex $args 2]
437    if ![string is integer $with ] {
438        error "object-size: illegal argument: $with"
439        return
440    }
441
442    set output_file "[file rootname [file tail $filename]].o"
443    if ![file_on_host exists $output_file] {
444	verbose -log "$testcase: $output_file does not exist"
445	unresolved "$testcase object-size $what $cmp $with"
446	return
447    }
448    set output [remote_exec host "$size" "$output_file"]
449    set status [lindex $output 0]
450    if { $status != 0 } {
451        verbose -log "$testcase object-size: $size failed"
452        unresolved "$testcase object-size $what $cmp $with"
453        return
454    }
455
456    set text [lindex $output 1]
457    set lines [split $text "\n"]
458
459    set line0 [lindex $lines 0]
460    if ![regexp {^\s*text\s+data\s+bss\s+dec\s+hex\s+filename\s*$} $line0] {
461        verbose -log "$testcase object-size: $size did not produce expected first line: $line0"
462        unresolved "$testcase object-size $what $cmp $with"
463        return
464    }
465
466    set line1 [lindex $lines 1]
467    if ![regexp {^\s*\d+\s+\d+\s+\d+\s+\d+\s+[\da-fA-F]+\s+} $line1] {
468        verbose -log "$testcase object-size: $size did not produce expected second line: $line1"
469        unresolved "$testcase object-size $what $cmp $with"
470        return
471    }
472
473    set actual [lindex $line1 $where]
474    verbose -log "$what size is $actual"
475
476    if [expr $actual $cmp $with] {
477	pass "$testcase object-size $what $cmp $with"
478    } else {
479	fail "$testcase object-size $what $cmp $with"
480    }
481}
482
483# Utility for testing that a function is defined on the current line.
484# Call pass if so, otherwise fail.  Invoked directly; the file must
485# have been compiled with -g -dA.
486#
487# Argument 0 is the current line, passed implicitly by dejagnu
488# Argument 1 is the function to check
489# Argument 2 handles expected failures and the like
490# Argument 3 is "." to match the current line, or an integer to match
491# an explicit line.
492proc dg-function-on-line { args } {
493    # Upvar from dg-final:
494    upvar dg-final-code final-code
495
496    set line [lindex $args 0]
497    set symbol [lindex $args 1]
498    set failures [lindex $args 2]
499
500    if { [llength $args] >= 4 } {
501	switch [lindex $args 3] {
502	    "." { }
503	    "default" { set line [lindex $args 3] }
504	}
505    }
506
507    if { [istarget hppa*-*-linux*] } {
508	set pattern [format {%s:\n\t.PROC\n\t.CALLINFO.*\n\t.ENTRY\n.L.*:\n(\t.file[^\t]*)*\t[^:]+:%d(:[0-9]+)?\n} \
509                     $symbol $line]
510    } elseif { [istarget hppa*-*-*] } {
511	set pattern [format {\t;[^:]+:%d(:[0-9]+)?\n(\t[^\t]+\n)+%s:\n\t.PROC} \
512                     $line $symbol]
513    } elseif { [istarget mips*-*-*] } {
514	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} \
515		     $line $symbol $symbol $symbol]
516    } elseif { [istarget microblaze*-*-*] } {
517        set pattern [format {:%d(:[0-9]+)?\n\$.*:\n\t\.ent\t%s\n\t\.type\t%s, @function\n%s:\n} \
518                     $line $symbol $symbol $symbol]
519    } else {
520	set pattern [format {%s:[^\t]*(\t.(fnstart|frame|mask|file)[^\t]*)*\t[^:]+:%d(:[0-9]+)?\n} \
521                     $symbol $line]
522    }
523
524    # The lack of spaces around $pattern is important, since they'd
525    # become part of the regex scan-assembler tries to match.
526    set cmd "scan-assembler {$pattern}"
527    if { [llength $args] >= 3 } {
528        set cmd "$cmd {$failures}"
529    }
530
531    append final-code "$cmd\n"
532}
533
534# Look for a pattern in the .exe.ltrans0.s file produced by the
535# compiler.  See dg-scan for details.
536
537proc scan-lto-assembler { args } {
538    set testcase [testname-for-summary]
539    # The name might include a list of options; extract the file name.
540    set filename [lindex $testcase 0]
541    set output_file "[file rootname [file tail $filename]].exe.ltrans0.s"
542    verbose "output_file: $output_file"
543    dg-scan "scan-assembler" 1 $testcase $output_file $args
544}
545