scanasm.exp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. # Copyright (C) 2000-2015 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with GCC; see the file COPYING3. If not see
  14. # <http://www.gnu.org/licenses/>.
  15. # Various utilities for scanning assembler output, used by gcc-dg.exp and
  16. # g++-dg.exp.
  17. # Utility for scanning compiler result, invoked via dg-final.
  18. # Transform newline and similar characters into their escaped form.
  19. proc make_pattern_printable { pattern } {
  20. return [string map {\t \\t \n \\n \r \\r \\ \\\\} $pattern]
  21. }
  22. # Scan the OUTPUT_FILE for a pattern. If it is present and POSITIVE
  23. # is non-zero, or it is not present and POSITIVE is zero, the test
  24. # passes. The ORIG_ARGS is the list of arguments provided by dg-final
  25. # to scan-assembler. The first element in ORIG_ARGS is the regular
  26. # expression to look for in the file. The second element, if present,
  27. # is a DejaGNU target selector.
  28. proc dg-scan { name positive testcase output_file orig_args } {
  29. if { [llength $orig_args] < 1 } {
  30. error "$name: too few arguments"
  31. return
  32. }
  33. if { [llength $orig_args] > 2 } {
  34. error "$name: too many arguments"
  35. return
  36. }
  37. if { [llength $orig_args] >= 2 } {
  38. switch [dg-process-target [lindex $orig_args 1]] {
  39. "S" { }
  40. "N" { return }
  41. "F" { setup_xfail "*-*-*" }
  42. "P" { }
  43. }
  44. }
  45. set pattern [lindex $orig_args 0]
  46. set printable_pattern [make_pattern_printable $pattern]
  47. if { [is_remote host] } {
  48. remote_upload host "$output_file"
  49. }
  50. set files [glob -nocomplain $output_file]
  51. if { $files == "" } {
  52. verbose -log "$testcase: output file does not exist"
  53. unresolved "$testcase $name $printable_pattern"
  54. return
  55. }
  56. set fd [open $output_file r]
  57. set text [read $fd]
  58. close $fd
  59. set match [regexp -- $pattern $text]
  60. if { $match == $positive } {
  61. pass "$testcase $name $printable_pattern"
  62. } else {
  63. fail "$testcase $name $printable_pattern"
  64. }
  65. }
  66. # Look for a pattern in the .s file produced by the compiler. See
  67. # dg-scan for details.
  68. proc scan-assembler { args } {
  69. set testcase [testname-for-summary]
  70. set output_file "[file rootname [file tail $testcase]].s"
  71. dg-scan "scan-assembler" 1 $testcase $output_file $args
  72. }
  73. force_conventional_output_for scan-assembler
  74. # Check that a pattern is not present in the .s file produced by the
  75. # compiler. See dg-scan for details.
  76. proc scan-assembler-not { args } {
  77. set testcase [testname-for-summary]
  78. set output_file "[file rootname [file tail $testcase]].s"
  79. dg-scan "scan-assembler-not" 0 $testcase $output_file $args
  80. }
  81. force_conventional_output_for scan-assembler-not
  82. # Return the scan for the assembly for hidden visibility.
  83. proc hidden-scan-for { symbol } {
  84. set objformat [gcc_target_object_format]
  85. switch $objformat {
  86. elf { return "hidden\[ \t_\]*$symbol" }
  87. mach-o { return "private_extern\[ \t_\]*_?$symbol" }
  88. default { return "" }
  89. }
  90. }
  91. # Check that a symbol is defined as a hidden symbol in the .s file
  92. # produced by the compiler.
  93. proc scan-hidden { args } {
  94. set testcase [testname-for-summary]
  95. set output_file "[file rootname [file tail $testcase]].s"
  96. set symbol [lindex $args 0]
  97. set hidden_scan [hidden-scan-for $symbol]
  98. set args [lreplace $args 0 0 "$hidden_scan"]
  99. dg-scan "scan-hidden" 1 $testcase $output_file $args
  100. }
  101. # Check that a symbol is not defined as a hidden symbol in the .s file
  102. # produced by the compiler.
  103. proc scan-not-hidden { args } {
  104. set testcase [testname-for-summary]
  105. set output_file "[file rootname [file tail $testcase]].s"
  106. set symbol [lindex $args 0]
  107. set hidden_scan [hidden-scan-for $symbol]
  108. set args [lreplace $args 0 0 "$hidden_scan"]
  109. dg-scan "scan-not-hidden" 0 $testcase $output_file $args
  110. }
  111. # Look for a pattern in OUTPUT_FILE. See dg-scan for details.
  112. proc scan-file { output_file args } {
  113. set testcase [testname-for-summary]
  114. dg-scan "scan-file" 1 $testcase $output_file $args
  115. }
  116. # Check that a pattern is not present in the OUTPUT_FILE. See dg-scan
  117. # for details.
  118. proc scan-file-not { output_file args } {
  119. set testcase [testname-for-summary]
  120. dg-scan "scan-file-not" 0 $testcase $output_file $args
  121. }
  122. # Look for a pattern in the .su file produced by the compiler. See
  123. # dg-scan for details.
  124. proc scan-stack-usage { args } {
  125. set testcase [testname-for-summary]
  126. set output_file "[file rootname [file tail $testcase]].su"
  127. dg-scan "scan-file" 1 $testcase $output_file $args
  128. }
  129. # Check that a pattern is not present in the .su file produced by the
  130. # compiler. See dg-scan for details.
  131. proc scan-stack-usage-not { args } {
  132. set testcase [testname-for-summary]
  133. set output_file "[file rootname [file tail $testcase]].su"
  134. dg-scan "scan-file-not" 0 $testcase $output_file $args
  135. }
  136. # Return the filename of the Ada spec corresponding to the argument.
  137. proc get_ada_spec_filename { testcase } {
  138. # The name might include a list of options; extract the file name.
  139. set filename [lindex $testcase 0]
  140. set tailname [file tail $filename]
  141. set extension [string trimleft [file extension $tailname] {.}]
  142. regsub -all {\-} [file rootname $tailname] {_} rootname
  143. return [string tolower "${rootname}_${extension}.ads"]
  144. }
  145. # Look for a pattern in the .ads file produced by the compiler. See
  146. # dg-scan for details.
  147. proc scan-ada-spec { args } {
  148. set testcase [testname-for-summary]
  149. set output_file "[get_ada_spec_filename $testcase]"
  150. dg-scan "scan-file" 1 $testcase $output_file $args
  151. }
  152. # Check that a pattern is not present in the .ads file produced by the
  153. # compiler. See dg-scan for details.
  154. proc scan-ada-spec-not { args } {
  155. set testcase [testname-for-summary]
  156. set output_file "[get_ada_spec_filename $testcase]"
  157. dg-scan "scan-file-not" 0 $testcase $output_file $args
  158. }
  159. # Call pass if pattern is present given number of times, otherwise fail.
  160. proc scan-assembler-times { args } {
  161. if { [llength $args] < 2 } {
  162. error "scan-assembler: too few arguments"
  163. return
  164. }
  165. if { [llength $args] > 3 } {
  166. error "scan-assembler: too many arguments"
  167. return
  168. }
  169. if { [llength $args] >= 3 } {
  170. switch [dg-process-target [lindex $args 2]] {
  171. "S" { }
  172. "N" { return }
  173. "F" { setup_xfail "*-*-*" }
  174. "P" { }
  175. }
  176. }
  177. set testcase [testname-for-summary]
  178. set pattern [lindex $args 0]
  179. set pp_pattern [make_pattern_printable $pattern]
  180. # This must match the rule in gcc-dg.exp.
  181. set output_file "[file rootname [file tail $testcase]].s"
  182. set files [glob -nocomplain $output_file]
  183. if { $files == "" } {
  184. verbose -log "$testcase: output file does not exist"
  185. unresolved "$testcase scan-assembler-times $pp_pattern [lindex $args 1]"
  186. return
  187. }
  188. set fd [open $output_file r]
  189. set text [read $fd]
  190. close $fd
  191. if { [llength [regexp -inline -all -- $pattern $text]] == [lindex $args 1]} {
  192. pass "$testcase scan-assembler-times $pp_pattern [lindex $args 1]"
  193. } else {
  194. fail "$testcase scan-assembler-times $pp_pattern [lindex $args 1]"
  195. }
  196. }
  197. force_conventional_output_for scan-assembler-times
  198. # Utility for scanning demangled compiler result, invoked via dg-final.
  199. # Call pass if pattern is present, otherwise fail.
  200. proc scan-assembler-dem { args } {
  201. global cxxfilt
  202. global base_dir
  203. if { [llength $args] < 1 } {
  204. error "scan-assembler-dem: too few arguments"
  205. return
  206. }
  207. if { [llength $args] > 2 } {
  208. error "scan-assembler-dem: too many arguments"
  209. return
  210. }
  211. if { [llength $args] >= 2 } {
  212. switch [dg-process-target [lindex $args 1]] {
  213. "S" { }
  214. "N" { return }
  215. "F" { setup_xfail "*-*-*" }
  216. "P" { }
  217. }
  218. }
  219. # Find c++filt like we find g++ in g++.exp.
  220. if ![info exists cxxfilt] {
  221. set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
  222. $base_dir/../../../binutils/cxxfilt \
  223. [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
  224. [findfile $base_dir/c++filt $base_dir/c++filt \
  225. [transform c++filt]]]]
  226. verbose -log "c++filt is $cxxfilt"
  227. }
  228. set testcase [testname-for-summary]
  229. set pattern [lindex $args 0]
  230. set pp_pattern [make_pattern_printable $pattern]
  231. set output_file "[file rootname [file tail $testcase]].s"
  232. set files [glob -nocomplain $output_file]
  233. if { $files == "" } {
  234. verbose -log "$testcase: output file does not exist"
  235. unresolved "$testcase scan-assembler-dem $pp_pattern"
  236. return
  237. }
  238. set output [remote_exec host "$cxxfilt" "" "$output_file"]
  239. set text [lindex $output 1]
  240. if [regexp -- $pattern $text] {
  241. pass "$testcase scan-assembler-dem $pp_pattern"
  242. } else {
  243. fail "$testcase scan-assembler-dem $pp_pattern"
  244. }
  245. }
  246. # Call pass if demangled pattern is not present, otherwise fail.
  247. proc scan-assembler-dem-not { args } {
  248. global cxxfilt
  249. global base_dir
  250. if { [llength $args] < 1 } {
  251. error "scan-assembler-dem-not: too few arguments"
  252. return
  253. }
  254. if { [llength $args] > 2 } {
  255. error "scan-assembler-dem-not: too many arguments"
  256. return
  257. }
  258. if { [llength $args] >= 2 } {
  259. switch [dg-process-target [lindex $args 1]] {
  260. "S" { }
  261. "N" { return }
  262. "F" { setup_xfail "*-*-*" }
  263. "P" { }
  264. }
  265. }
  266. # Find c++filt like we find g++ in g++.exp.
  267. if ![info exists cxxfilt] {
  268. set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
  269. $base_dir/../../../binutils/cxxfilt \
  270. [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
  271. [findfile $base_dir/c++filt $base_dir/c++filt \
  272. [transform c++filt]]]]
  273. verbose -log "c++filt is $cxxfilt"
  274. }
  275. set testcase [testname-for-summary]
  276. set pattern [lindex $args 0]
  277. set pp_pattern [make_pattern_printable $pattern]
  278. set output_file "[file rootname [file tail $testcase]].s"
  279. set files [glob -nocomplain $output_file]
  280. if { $files == "" } {
  281. verbose -log "$testcase: output file does not exist"
  282. unresolved "$testcase scan-assembler-dem-not $pp_pattern"
  283. return
  284. }
  285. set output [remote_exec host "$cxxfilt" "" "$output_file"]
  286. set text [lindex $output 1]
  287. if ![regexp -- $pattern $text] {
  288. pass "$testcase scan-assembler-dem-not $pp_pattern"
  289. } else {
  290. fail "$testcase scan-assembler-dem-not $pp_pattern"
  291. }
  292. }
  293. # Call pass if object size is ok, otherwise fail.
  294. # example: /* { dg-final { object-size text <= 54 } } */
  295. proc object-size { args } {
  296. global size
  297. global base_dir
  298. if { [llength $args] < 3 } {
  299. error "object-size: too few arguments"
  300. return
  301. }
  302. if { [llength $args] > 4 } {
  303. error "object-size: too many arguments"
  304. return
  305. }
  306. if { [llength $args] >= 4 } {
  307. switch [dg-process-target [lindex $args 3]] {
  308. "S" { }
  309. "N" { return }
  310. "F" { setup_xfail "*-*-*" }
  311. "P" { }
  312. }
  313. }
  314. # Find size like we find g++ in g++.exp.
  315. if ![info exists size] {
  316. set size [findfile $base_dir/../../../binutils/size \
  317. $base_dir/../../../binutils/size \
  318. [findfile $base_dir/../../size $base_dir/../../size \
  319. [findfile $base_dir/size $base_dir/size \
  320. [transform size]]]]
  321. verbose -log "size is $size"
  322. }
  323. set testcase [testname-for-summary]
  324. set what [lindex $args 0]
  325. set where [lsearch { text data bss total } $what]
  326. if { $where == -1 } {
  327. error "object-size: illegal argument: $what"
  328. return
  329. }
  330. set cmp [lindex $args 1]
  331. if { [lsearch { < > <= >= == != } $cmp] == -1 } {
  332. error "object-size: illegal argument: $cmp"
  333. return
  334. }
  335. set with [lindex $args 2]
  336. if ![string is integer $with ] {
  337. error "object-size: illegal argument: $with"
  338. return
  339. }
  340. set output_file "[file rootname [file tail $testcase]].o"
  341. if ![file_on_host exists $output_file] {
  342. verbose -log "$testcase: $output_file does not exist"
  343. unresolved "$testcase object-size $what $cmp $with"
  344. return
  345. }
  346. set output [remote_exec host "$size" "$output_file"]
  347. set status [lindex $output 0]
  348. if { $status != 0 } {
  349. verbose -log "$testcase object-size: $size failed"
  350. unresolved "$testcase object-size $what $cmp $with"
  351. return
  352. }
  353. set text [lindex $output 1]
  354. set lines [split $text "\n"]
  355. set line0 [lindex $lines 0]
  356. if ![regexp {^\s*text\s+data\s+bss\s+dec\s+hex\s+filename\s*$} $line0] {
  357. verbose -log "$testcase object-size: $size did not produce expected first line: $line0"
  358. unresolved "$testcase object-size $what $cmp $with"
  359. return
  360. }
  361. set line1 [lindex $lines 1]
  362. if ![regexp {^\s*\d+\s+\d+\s+\d+\s+\d+\s+[\da-fA-F]+\s+} $line1] {
  363. verbose -log "$testcase object-size: $size did not produce expected second line: $line1"
  364. unresolved "$testcase object-size $what $cmp $with"
  365. return
  366. }
  367. set actual [lindex $line1 $where]
  368. verbose -log "$what size is $actual"
  369. if [expr $actual $cmp $with] {
  370. pass "$testcase object-size $what $cmp $with"
  371. } else {
  372. fail "$testcase object-size $what $cmp $with"
  373. }
  374. }
  375. # Utility for testing that a function is defined on the current line.
  376. # Call pass if so, otherwise fail. Invoked directly; the file must
  377. # have been compiled with -g -dA.
  378. #
  379. # Argument 0 is the current line, passed implicitly by dejagnu
  380. # Argument 1 is the function to check
  381. # Argument 2 handles expected failures and the like
  382. # Argument 3 is "." to match the current line, or an integer to match
  383. # an explicit line.
  384. proc dg-function-on-line { args } {
  385. # Upvar from dg-final:
  386. upvar dg-final-code final-code
  387. set line [lindex $args 0]
  388. set symbol [lindex $args 1]
  389. set failures [lindex $args 2]
  390. if { [llength $args] >= 4 } {
  391. switch [lindex $args 3] {
  392. "." { }
  393. "default" { set line [lindex $args 3] }
  394. }
  395. }
  396. if { [istarget hppa*-*-*] } {
  397. set pattern [format {\t;[^:]+:%d\n(\t[^\t]+\n)+%s:\n\t.PROC} \
  398. $line $symbol]
  399. } elseif { [istarget mips*-*-*] } {
  400. 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} \
  401. $line $symbol $symbol $symbol]
  402. } elseif { [istarget microblaze*-*-*] } {
  403. set pattern [format {:%d\n\$.*:\n\t\.ent\t%s\n\t\.type\t%s, @function\n%s:\n} \
  404. $line $symbol $symbol $symbol]
  405. } else {
  406. set pattern [format {%s:[^\t]*(\t.(fnstart|frame|mask|file)[^\t]*)*\t[^:]+:%d\n} \
  407. $symbol $line]
  408. }
  409. # The lack of spaces around $pattern is important, since they'd
  410. # become part of the regex scan-assembler tries to match.
  411. set cmd "scan-assembler {$pattern}"
  412. if { [llength $args] >= 3 } {
  413. set cmd "$cmd {$failures}"
  414. }
  415. append final-code "$cmd\n"
  416. }
  417. # Look for a pattern in the .exe.ltrans0.s file produced by the
  418. # compiler. See dg-scan for details.
  419. proc scan-lto-assembler { args } {
  420. set testcase [testname-for-summary]
  421. set output_file "[file rootname [file tail $testcase]].exe.ltrans0.s"
  422. verbose "output_file: $output_file"
  423. dg-scan "scan-assembler" 1 $testcase $output_file $args
  424. }