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