Debian Codegraph @

gcc/testsuite/jit.dg/jit.exp

raw
1 # Test code for libgccjit.so
2 #
3 # We will compile each of jit.dg/test-*.c into an executable
4 # dynamically linked against libgccjit.so, and then run each
5 # such executable.
6 #
7 # These executables call into the libgccjit.so API to create
8 # code, compile it, and run it, verifying that the results
9 # are as expected. See harness.h for shared code used by all
10 # such executables.
11 #
12 # The executables call into DejaGnu's unit testing C API to
13 # report PASS/FAIL results, which this script gathers back
14 # up into the Tcl world, reporting a summary of all results
15 # across all of the executables.
16
17 # Kludge alert:
18 # We need g++_init so that it can find the stdlib include path.
19 #
20 # g++_init (in lib/g++.exp) uses g++_maybe_build_wrapper,
21 # which normally comes from the definition of
22 # ${tool}_maybe_build_wrapper within lib/wrapper.exp.
23 #
24 # However, for us, ${tool} is "jit".
25 # Hence we load wrapper.exp with tool == "g++", so that
26 # g++_maybe_build_wrapper is defined.
27 set tool g++
28 load_lib wrapper.exp
29 set tool jit
30
31 load_lib dg.exp
32 load_lib prune.exp
33 load_lib target-supports.exp
34 load_lib gcc-defs.exp
35 load_lib timeout.exp
36 load_lib target-libpath.exp
37 load_lib gcc.exp
38 load_lib g++.exp
39 load_lib dejagnu.exp
40 load_lib target-supports-dg.exp
41 load_lib valgrind.exp
42
43 # Skip these tests for targets that don't support -lgccjit
44 if { ![check_effective_target_lgccjit] } {
45 return
46 }
47
48 # The default do-what keyword.
49 set dg-do-what-default compile
50
51 # Given WRES, the result from "wait", issue a PASS
52 # if the spawnee exited cleanly, or a FAIL for various kinds of
53 # unexpected exits.
54
55 proc verify_exit_status { executable wres } {
56 set extra [lassign $wres pid spawnid os_error_flag value]
57 verbose "pid: $pid" 3
58 verbose "spawnid: $spawnid" 3
59 verbose "os_error_flag: $os_error_flag" 3
60 verbose "value: $value" 3
61
62 # Detect segfaults etc:
63 set len [llength $extra]
64 if { $len >= 1 } {
65 if { [lindex $extra 0] == "CHILDKILLED" } {
66 set reason "Unknown Reason"
67 set detail "No Details"
68 if { $len >= 2 } {
69 set reason [lindex $extra 1]
70 if { $len >= 3 } {
71 set detail [lindex $extra 2]
72 }
73 }
74 fail "$executable killed: $reason $detail"
75 return
76 }
77 }
78 if { $os_error_flag != 0 } {
79 fail "$executable: OS error: $os_error_flag $extra"
80 return
81 }
82 if { $value != 0 } {
83 fail "$executable: non-zero exit code: $value $extra"
84 return
85 }
86 pass "$executable exited cleanly"
87 }
88
89 # This is host_execute from dejagnu.exp commit
90 # 126a089777158a7891ff975473939f08c0e31a1c
91 # with the following patch applied, and renaming to "fixed_host_execute".
92 # See the discussion at
93 # http://lists.gnu.org/archive/html/dejagnu/2014-10/msg00000.html
94 #
95 # --- /usr/share/dejagnu/dejagnu.exp.old 2014-10-08 13:38:57.274068541 -0400
96 # +++ /usr/share/dejagnu/dejagnu.exp 2014-10-10 12:27:51.113813659 -0400
97 # @@ -113,8 +113,6 @@ proc host_execute {args} {
98 # set timetol 0
99 # set arguments ""
100 #
101 # - expect_before buffer_full { perror "Buffer full" }
102 # -
103 # if { [llength $args] == 0} {
104 # set executable $args
105 # } else {
106
107
108 # Execute the executable file, and anaylyse the output for the
109 # test state keywords.
110 # Returns:
111 # A "" (empty) string if everything worked, or an error message
112 # if there was a problem.
113 #
114 proc fixed_host_execute {args} {
115 global env
116 global text
117 global spawn_id
118
119 verbose "fixed_host_execute: $args"
120
121 set timeoutmsg "Timed out: Never got started, "
122 set timeout 100
123 set file all
124 set timetol 0
125 set arguments ""
126
127 if { [llength $args] == 0} {
128 set executable $args
129 } else {
130 set executable [lindex $args 0]
131 set params [lindex $args 1]
132 }
133
134 verbose "The executable is $executable" 2
135 if {![file exists ${executable}]} {
136 perror "The executable, \"$executable\" is missing" 0
137 return "No source file found"
138 } elseif {![file executable ${executable}]} {
139 perror "The executable, \"$executable\" is not usable" 0
140 return "Bad executable found"
141 }
142
143 verbose "params: $params" 2
144
145 # spawn the executable and look for the DejaGnu output messages from the
146 # test case.
147 # spawn -noecho -open [open "|./${executable}" "r"]
148
149 # Run under valgrind if RUN_UNDER_VALGRIND is present in the environment.
150 # Note that it's best to configure gcc with --enable-valgrind-annotations
151 # when testing under valgrind.
152 set run_under_valgrind [info exists env(RUN_UNDER_VALGRIND)]
153 if $run_under_valgrind {
154 set valgrind_logfile "${executable}.valgrind.txt"
155 set valgrind_params {"valgrind"}
156 lappend valgrind_params "--leak-check=full"
157 lappend valgrind_params "--log-file=${valgrind_logfile}"
158 } else {
159 set valgrind_params {}
160 }
161 verbose "valgrind_params: $valgrind_params" 2
162
163 set args ${valgrind_params}
164 lappend args "./${executable}"
165 set args [concat $args ${params}]
166 verbose "args: $args" 2
167
168 # We checked that the executable exists above, and can be executed, but
169 # that does not cover other reasons that the launch could fail (e.g.
170 # missing or malformed params); catch such cases here and report them.
171 set err [catch "spawn -noecho $args" pid]
172 set sub_proc_id $spawn_id
173 if { $pid <= 0 || $err != 0 || $sub_proc_id < 0 } {
174 warning "failed to spawn : $args : err = $err"
175 }
176
177 # Increase the buffer size, if needed to avoid spurious buffer-full
178 # events; GCC uses 10000; chose a power of two here.
179 set current_max_match [match_max -i $sub_proc_id]
180 if { $current_max_match < 8192 } {
181 match_max -i $sub_proc_id 8192
182 set used [match_max -i $sub_proc_id]
183 }
184
185 # If we get a buffer-full error, that seems to be unrecoverable so try to
186 # exit in a reasonable manner to avoid wedged processes.
187 expect_after full_buffer {
188 verbose -log "fixed_host_execute: $args FULL BUFFER"
189 # FIXME: this all assumes that closing the connection will cause the
190 # sub-process to terminate (but that is not going to be the case if,
191 # for example, there is something started with -nohup somewhere).
192 # We should explicitly kill it here.
193 # Set the process to be a nowait exit.
194 wait -nowait -i $sub_proc_id
195 catch close
196 perror "${executable} got full_buffer"
197 return "${executable} got full_buffer"
198 }
199
200 set prefix "\[^\r\n\]*"
201 # Work around a Darwin tcl or termios bug that sometimes inserts extra
202 # CR characters into the cooked tty stream
203 set endline "\r\n"
204 if { [istarget *-*-darwin*] } {
205 set endline "\r(\r)*\n"
206 }
207
208 # Note that the logic here assumes that we cannot (validly) get single
209 # carriage return or line feed characters in the stream. If those occur,
210 # it will stop any further matching. We arange for the matching to be
211 # at the start of the buffer - so that if there is any spurious output
212 # to be discarded, it must be done explicitly - not by matching part-way
213 # through the buffer.
214 expect {
215 -re "^$prefix\[0-9\]\[0-9\]:..:..:${text}*$endline" {
216 regsub "\[\n\r\t\]*NOTE: $text\r\n" $expect_out(0,string) "" output
217 verbose "$output" 3
218 set timetol 0
219 exp_continue
220 }
221 -re "^\tNOTE: (\[^\r\n\]+)$endline" {
222 # discard notes.
223 verbose "Ignored note: $expect_out(1,string)" 2
224 set timetol 0
225 exp_continue
226 }
227 -re "^\tPASSED: (\[^\r\n\]+)$endline" {
228 pass "$expect_out(1,string)"
229 set timetol 0
230 exp_continue
231 }
232 -re "^\tFAILED: (\[^\r\n\]+)$endline" {
233 fail "$expect_out(1,string)"
234 set timetol 0
235 exp_continue
236 }
237 -re "^\tUNTESTED: (\[^\r\n\]+)$endline" {
238 untested "$expect_out(1,string)"
239 set timetol 0
240 exp_continue
241 }
242 -re "^\tUNRESOLVED: (\[^\r\n\]+)$endline" {
243 unresolved "$expect_out(1,string)"
244 set timetol 0
245 exp_continue
246 }
247 -re "^$prefix$endline" {
248 # This matches and discards any other lines (including blank ones).
249 if { [string length $expect_out(buffer)] <= 2 } {
250 set output "blank line"
251 } else {
252 set output [string range $expect_out(buffer) 0 end-2]
253 }
254 verbose -log "DISCARDED $expect_out(spawn_id) : $output"
255 exp_continue
256 }
257 eof {
258 # This seems to be the only way that we can reliably know that the
259 # output is finished since there are cases where further output
260 # follows the dejagnu test harness totals.
261 verbose "saw eof" 2
262 }
263 timeout {
264 if { $timetol <= 2 } {
265 verbose -log "Timed out with retry (timeout = $timeout)"
266 incr timetol
267 exp_continue
268 } else {
269 warning "Timed out executing testcase (timeout = $timeout)"
270 catch close
271 return "Timed out executing test case"
272 }
273 }
274 }
275
276 # Use "wait" to pick up the sub-process exit state. If the sub-process is
277 # writing to a file (perhaps under valgrind) then that also needs to be
278 # complete; only attempt this on a valid spawn.
279 if { $sub_proc_id > 0 } {
280 verbose "waiting for $sub_proc_id" 1
281 # Be explicit about what we are waiting for.
282 catch "wait -i $sub_proc_id" wres
283 verbose "wres: $wres" 2
284 verify_exit_status $executable $wres
285 }
286
287 if $run_under_valgrind {
288 upvar 2 name name
289 # Use xfail to report leaks, as libgccjit isn't yet clean of
290 # memory leaks (PR jit/63854)
291 parse_valgrind_logfile $name $valgrind_logfile xfail
292 }
293
294 # force a close of the executable to be safe.
295 catch close
296
297 return ""
298 }
299
300 # (end of code from dejagnu.exp)
301
302 # GCC_UNDER_TEST is needed by gcc_target_compile
303 global GCC_UNDER_TEST
304 if ![info exists GCC_UNDER_TEST] {
305 set GCC_UNDER_TEST "[find_gcc]"
306 }
307
308 g++_init
309
310 # Initialize dg.
311 dg-init
312
313 # Gather a list of all tests.
314
315 # C tests within the testsuite: gcc/testsuite/jit.dg/test-*.c
316 set tests [find $srcdir/$subdir test-*.c]
317
318 # C++ tests within the testsuite: gcc/testsuite/jit.dg/test-*.cc
319 set tests [concat $tests [find $srcdir/$subdir test-*.cc]]
320
321 # We also test the examples within the documentation, to ensure that
322 # they compile:
323 set tests [concat $tests [find $srcdir/../jit/docs/examples *.c]]
324 set tests [concat $tests [find $srcdir/../jit/docs/examples *.cc]]
325
326 set tests [lsort $tests]
327
328 verbose "tests: $tests"
329
330 # Is testcase NAME meant to generate a reproducer?
331 proc is_testcase_meant_to_generate_a_reproducer {name} {
332 # We expect most testcases to generate a reproducer.
333 # The exceptions are the tutorials (which don't have a "test-"
334 # prefix), and test-threads.c and test-benchmark.c (which are each
335 # unique).
336 verbose "is_testcase_meant_to_generate_a_reproducer: $name"
337 if { [string match "*test-*" $name] } {
338 if { [string match "*test-threads.c" $name] } {
339 return 0
340 }
341 if { [string match "*test-benchmark.c" $name] } {
342 return 0
343 }
344 return 1
345 }
346 return 0
347 }
348
349 # libgloss has found the driver (as "xgcc" or "gcc) and stored
350 # its full path as GCC_UNDER_TEST.
351 proc get_path_of_driver {} {
352 global GCC_UNDER_TEST
353
354 verbose "GCC_UNDER_TEST: $GCC_UNDER_TEST"
355 set binary [lindex $GCC_UNDER_TEST 0]
356 verbose "binary: $binary"
357
358 return [file dirname $binary]
359 }
360
361 # Expand "SRCDIR" within ARG to the location of the top-level
362 # src directory
363
364 proc jit-expand-vars {arg} {
365 verbose "jit-expand-vars: $arg"
366 global srcdir
367 verbose " srcdir: $srcdir"
368 # "srcdir" is that of the gcc/testsuite directory, so
369 # we need to go up two levels.
370 set arg [string map [list "SRCDIR" $srcdir/../..] $arg]
371 verbose " new arg: $arg"
372 return $arg
373 }
374
375 # Parameters used when invoking the executables built from the test cases.
376
377 global jit-exe-params
378 set jit-exe-params {}
379
380 # Set "jit-exe-params", expanding "SRCDIR" in each arg to the location of
381 # the top-level srcdir.
382
383 proc dg-jit-set-exe-params { args } {
384 verbose "dg-jit-set-exe-params: $args"
385
386 global jit-exe-params
387 set jit-exe-params {}
388 # Skip initial arg (line number)
389 foreach arg [lrange $args 1 [llength $args] ] {
390 lappend jit-exe-params [jit-expand-vars $arg]
391 }
392 }
393
394 # For test-debuginfo.c. Starts gdb, does cmds and checks the output against match
395 proc jit-check-debug-info { obj_file cmds match } {
396 verbose "Checking debug info for $obj_file with match: $match"
397
398 if { [catch {exec gdb -v} fid] } {
399 verbose "No gdb seems to be in path. Can't check debug info. Reporting 'unsupported'."
400 unsupported "No gdb seems to be in path. Can't check debug info"
401 return
402 }
403
404 spawn gdb $obj_file
405
406 verbose "Disable color styling in GDB newer then 8.3 (errors on older)"
407 send "set style enabled off\n"
408
409 foreach cmd $cmds {
410 send $cmd
411 }
412 expect {
413 -re "Dwarf Error: wrong version in compilation unit header" {
414 set testcase [testname-for-summary]
415 unsupported "$testcase: gdb does not support dwarf version"
416 }
417 -re $match { pass OK }
418 default { fail FAIL }
419 }
420
421 # Quit gdb
422 send "set confirm off\n"
423 send "q\n"
424 }
425
426 proc jit-dg-test { prog do_what extra_tool_flags } {
427 verbose "within jit-dg-test..."
428 verbose " prog: $prog"
429 verbose " do_what: $do_what"
430 verbose " extra_tool_flags: $extra_tool_flags"
431
432 global dg-do-what-default
433 set dg-do-what [list ${dg-do-what-default} "" P]
434
435 set tmp [dg-get-options $prog]
436 foreach op $tmp {
437 verbose "Processing option: $op" 3
438 set status [catch "$op" errmsg]
439 if { $status != 0 } {
440 if { 0 && [info exists errorInfo] } {
441 # This also prints a backtrace which will just confuse
442 # testcase writers, so it's disabled.
443 perror "$name: $errorInfo\n"
444 } else {
445 perror "$name: $errmsg for \"$op\"\n"
446 }
447 perror "$name: $errmsg for \"$op\"" 0
448 return
449 }
450 }
451
452 # If we're not supposed to try this test on this target, we're done.
453 if { [lindex ${dg-do-what} 1] == "N" } {
454 unsupported "$name"
455 verbose "$name not supported on this target, skipping it" 3
456 return
457 }
458
459 # test-threads.c needs to be linked against pthreads
460 if {[string match "*test-threads.c" $prog]} {
461 append extra_tool_flags " -lpthread"
462 }
463
464 # test-add-driver-options.c needs a shared library built from
465 # add-driver-options-testlib.c
466 if {[string match "*test-add-driver-options.c" $prog]} {
467 global srcdir
468 global subdir
469
470 set comp_output [gcc_target_compile \
471 $srcdir/$subdir/add-driver-options-testlib.c \
472 "libadd-driver-options-testlib.so" \
473 "executable" \
474 "additional_flags=-fPIC additional_flags=-shared"]
475 }
476
477 # Any test case that uses jit-verify-output-file-was-created
478 # needs to call jit-setup-compile-to-file here.
479 # (is there a better way to handle setup/finish pairs in dg?)
480 set tmp [grep $prog "jit-verify-output-file-was-created"]
481 if {![string match "" $tmp]} {
482 jit-setup-compile-to-file $prog
483 }
484
485 # Determine what to name the built executable.
486 #
487 # We simply append .exe to the filename, e.g.
488 # "test-foo.c.exe"
489 # since some testcases exist in both
490 # "test-foo.c" and
491 # "test-foo.cc"
492 # variants, and we don't want them to clobber each other's
493 # executables.
494 #
495 # This also ensures that the source name makes it into the
496 # pass/fail output, so that we can distinguish e.g. which test-foo
497 # is failing.
498 set output_file "[file tail $prog].exe"
499 verbose "output_file: $output_file"
500
501 # Create the test executable:
502 set extension [file extension $prog]
503 if {$extension == ".cc"} {
504 set compilation_function "g++_target_compile"
505 set options "{additional_flags=$extra_tool_flags}"
506 } else {
507 set compilation_function "gcc_target_compile"
508 # Until recently, <dejagnu.h> assumed -fgnu89-inline
509 # Ideally we should fixincludes it (PR other/63613), but
510 # for now add -fgnu89-inline when compiling C JIT testcases.
511 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63613
512 # and http://lists.gnu.org/archive/html/dejagnu/2014-10/msg00011.html
513 set options "{additional_flags=$extra_tool_flags -fgnu89-inline}"
514 }
515 verbose "compilation_function=$compilation_function"
516 verbose "options=$options"
517
518 set comp_output [$compilation_function $prog $output_file \
519 "executable" $options]
520 upvar 1 name name
521 if ![jit_check_compile "$name" "initial compilation" \
522 $output_file $comp_output] then {
523 return
524 }
525
526 # Most of the test cases use gcc_jit_context_dump_reproducer_to_file
527 # as they run to write out a .c file that reproduces their behavior,
528 # exercising that API.
529 set generated_reproducer "${output_file}.reproducer.c"
530
531 # Delete any such generated .c file from a previous run.
532 catch "exec rm -f $generated_reproducer"
533
534 # Run the test executable, capturing the PASS/FAIL textual output
535 # from the C API, converting it into the Tcl API.
536
537 # We need to set LD_LIBRARY_PATH so that the test files can find
538 # libgccjit.so
539 # Do this using set_ld_library_path_env_vars from target-libpath.exp
540 # We will restore the old value later using
541 # restore_ld_library_path_env_vars.
542
543 # Unfortunately this API only supports a single saved value, rather
544 # than a stack, and g++_init has already called into this API,
545 # injecting the appropriate value for LD_LIBRARY_PATH for finding
546 # the built copy of libstdc++.
547 # Hence the call to restore_ld_library_path_env_vars would restore
548 # the *initial* value of LD_LIBRARY_PATH, and attempts to run
549 # a C++ testcase after running any prior testcases would thus look
550 # in the wrong place for libstdc++. This led to failures at startup
551 # of the form:
552 # ./tut01-hello-world.cc.exe: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./tut01-hello-world.cc.exe)
553 # when the built libstdc++ is more recent that the system libstdc++.
554 #
555 # As a workaround, reset the variable "orig_environment_saved" within
556 # target-libpath.exp, so that the {set|restore}_ld_library_path_env_vars
557 # API saves/restores the current value of LD_LIBRARY_PATH (as set up
558 # by g++_init).
559 global orig_environment_saved
560 set orig_environment_saved 0
561
562 global ld_library_path
563 global base_dir
564 set ld_library_path "$base_dir/../../"
565 set_ld_library_path_env_vars
566
567 # libgccjit uses the driver to convert .s files to .so libraries
568 # via its *installed* name, FULL_DRIVER_NAME
569 # ${target_noncanonical}-gcc-${gcc_BASEVER}${exeext}
570 # e.g. "x86_64-unknown-linux-gnu-gcc-5.0.0"
571 # looking for it on PATH. Hence we need to prepend the location of
572 # that executable to PATH when running the tests
573 set dir_containing_driver [get_path_of_driver ]
574 verbose "dir_containing_driver: $dir_containing_driver"
575 global env
576 set old_path $env(PATH)
577 setenv "PATH" $dir_containing_driver:$old_path
578 verbose -log "PATH=[getenv PATH]"
579
580 # We have:
581 # test-executables
582 # linked to -> libgccjit.so
583 # -> invokes driver:
584 # -> invokes the assembler
585 # -> invokes the linker
586 # We want to be able to run this from the builddir without installing
587 # but the linker needs to be able to locate various libraries, or we
588 # get:
589 # ld: cannot find crtbeginS.o: No such file or directory
590 # ld: cannot find -lgcc
591 # ld: cannot find -lgcc_s
592 # These can be found in the "gcc" subdir of the build.
593 # Hence to be able to run the testsuite without installing, we need
594 # to set or prepend the "gcc" subdir of the build to LIBRARY_PATH:
595 if { [info exists env(LIBRARY_PATH) ] } {
596 set old_library_path $env(LIBRARY_PATH)
597 setenv "LIBRARY_PATH" $dir_containing_driver:$old_library_path
598 } else {
599 setenv "LIBRARY_PATH" $dir_containing_driver
600 }
601 verbose -log "LIBRARY_PATH=[getenv LIBRARY_PATH]"
602
603 # dejagnu.exp's host_execute has code to scrape out test results
604 # from the DejaGnu C API and bring back into the tcl world, so we
605 # use that to invoke the built code.
606 # However, it appears to be buggy; see:
607 # http://lists.gnu.org/archive/html/dejagnu/2014-10/msg00000.html
608 # We instead call a patched local copy, "fixed_host_execute", defined
609 # above.
610
611 global jit-exe-params
612 set args ${jit-exe-params}
613 set jit-exe-params {}
614
615 set result [fixed_host_execute $output_file $args ]
616 verbose "result: $result"
617
618 # Restore PATH
619 setenv "PATH" $old_path
620
621 # Restore LIBRARY_PATH
622 if { [info exists old_library_path] } {
623 setenv "LIBRARY_PATH" $old_library_path
624 } else {
625 unsetenv "LIBRARY_PATH"
626 }
627
628 restore_ld_library_path_env_vars
629
630 # Most of the test cases use gcc_jit_context_dump_reproducer_to_file
631 # as they run to write out a .c file that reproduces their behavior,
632 # exercising that API.
633
634 if { [is_testcase_meant_to_generate_a_reproducer $name] } {
635 verbose "$name is meant to generate a reproducer"
636 # Verify that a reproducer was generated
637 if { [file exists $generated_reproducer] == 1} {
638 pass "found generated reproducer: $generated_reproducer"
639 set output_file "${generated_reproducer}.exe"
640 # (this overwrites output_file)
641
642 # Try to compile the generated reproducer
643 verbose "compilation_function=$compilation_function"
644
645 # The .c file written by gcc_jit_context_dump_reproducer_to_file
646 # assigns the result of each API call to a unique variable, and not
647 # all are necessarily used, so we need -Wno-unused-variable.
648 set options \
649 "{additional_flags=$extra_tool_flags -Wno-unused-variable}"
650 verbose "options=$options"
651
652 set comp_output2 [$compilation_function $generated_reproducer \
653 $output_file "executable" $options]
654 if ![jit_check_compile "generated reproducer from $name" "initial compilation" \
655 $output_file $comp_output2] then {
656 return
657 }
658
659 # The caller, dg-test, will verify comp_output, which contains
660 # the output from compiling the testcase and will issue a fail
661 # if it's non-empty (e.g. containing warnings, the
662 # "test for excess errors").
663 #
664 # Append the output from compiling the reproducer, so that this is also
665 # verified:
666 append comp_output $comp_output2
667
668 # TODO: we should try to run the built executable
669 # It's not quite a quine, since it embeds ptrs which could change
670 # from run to run.
671 } else {
672 fail "did not find a generated reproducer: $generated_reproducer"
673 }
674 } else {
675 verbose "$name is not meant to generate a reproducer"
676 }
677
678 # Normally we would return $comp_output and $output_file to the
679 # caller, which would delete $output_file, the generated executable.
680 # If we need to debug, it's handy to be able to suppress this behavior,
681 # keeping the executable around.
682 set preserve_executables [info exists env(PRESERVE_EXECUTABLES)]
683 if $preserve_executables {
684 set output_file ""
685 }
686
687 return [list $comp_output $output_file]
688 }
689
690 # Given source file PROG, scrape out the value of
691 # #define OUTPUT_FILENAME
692 # failing if it's not found.
693
694 proc jit-get-output-filename {prog} {
695 set tmp [grep $prog "#define OUTPUT_FILENAME (.*)"]
696 if {![string match "" $tmp]} {
697 foreach i $tmp {
698 verbose "i: $i"
699 if {[regexp "^\#define OUTPUT_FILENAME\[ \t\]\+\"(.*)\"$" $i i group] } {
700 verbose "group: '$group'"
701 return $group
702 } else {
703 fail "Unable to parse line: $i"
704 }
705 }
706 }
707 fail "Unable to locate OUTPUT_FILENAME"
708 return ""
709 }
710
711 # For testcases that use jit-verify-output-file-was-created
712 # delete OUTPUT_FILENAME beforehand, to ensure that the
713 # testcase is indeed creating it.
714
715 proc jit-setup-compile-to-file { prog } {
716 verbose "jit-setup-compile-to-file: $prog"
717 set output_filename [jit-get-output-filename $prog]
718 verbose " output_filename: $output_filename"
719 if {![string match "" $output_filename]} {
720 verbose " deleting any $output_filename"
721 catch "exec rm -f $output_filename"
722 }
723 }
724
725 proc jit-verify-output-file-was-created { args } {
726 verbose "jit-verify-output-file-was-created: $args"
727
728 upvar 2 prog prog
729 verbose "prog: $prog"
730 set output_filename [jit-get-output-filename $prog]
731 verbose " output_filename: $output_filename"
732
733 # Verify that the expected file was written out
734 if { [file exists $output_filename] == 1} {
735 pass "$output_filename exists"
736 } else {
737 fail "$output_filename does not exist"
738 }
739 }
740
741 # Verify that the given file exists, and is executable.
742 # Attempt to execute it, and verify that its stdout matches
743 # the given regex.
744
745 proc jit-run-executable { args } {
746 verbose "jit-run-executable: $args"
747
748 set executable-name [lindex $args 0]
749 verbose "executable-name: ${executable-name}"
750
751 set dg-output-text [lindex $args 1]
752 verbose "dg-output-text: ${dg-output-text}"
753
754 if { [file executable ${executable-name}] } {
755 pass "${executable-name} has executable bit set"
756 } else {
757 fail "${executable-name} does not have executable bit set"
758 }
759
760 # Attempt to run the executable; adapted from dg.exp's dg-test
761 set status -1
762 set result [jit_load ./${executable-name}]
763 set status [lindex $result 0]
764 set output [lindex $result 1]
765 verbose " status: $status"
766 verbose " output: $output"
767 # send_user "After exec, status: $status\n"
768 if { "$status" == "pass" } {
769 pass "${executable-name} execution test"
770 verbose "Exec succeeded." 3
771 set texttmp ${dg-output-text}
772 if { ![regexp $texttmp ${output}] } {
773 fail "${executable-name} output pattern test, is ${output}, should match $texttmp"
774 verbose "Failed test for output pattern $texttmp" 3
775 } else {
776 pass "${executable-name} output pattern test, $texttmp"
777 verbose "Passed test for output pattern $texttmp" 3
778 }
779 unset texttmp
780 } elseif { "$status" == "fail" } {
781 # It would be nice to get some info out of errorCode.
782 if {[info exists errorCode]} {
783 verbose "Exec failed, errorCode: $errorCode" 3
784 } else {
785 verbose "Exec failed, errorCode not defined!" 3
786 }
787 fail "${executable-name} execution test"
788 } else {
789 $status "${executable-name} execution test"
790 }
791 }
792
793 # Assuming that a .s file has been written out named
794 # OUTPUT_FILENAME, invoke the driver to try to turn it into
795 # an executable, and try to run the result.
796 # For use by the test-compile-to-assembler.c testcase.
797 proc jit-verify-assembler { args } {
798 verbose "jit-verify-assembler: $args"
799
800 set dg-output-text [lindex $args 0]
801 verbose "dg-output-text: ${dg-output-text}"
802
803 upvar 2 name name
804 verbose "name: $name"
805
806 upvar 2 prog prog
807 verbose "prog: $prog"
808 set asm_filename [jit-get-output-filename $prog]
809 verbose " asm_filename: ${asm_filename}"
810
811 # Name the built executable as OUTPUT_FILENAME with
812 # ".exe" appended.
813 set executable_from_asm ${asm_filename}.exe
814 verbose " executable_from_asm: ${executable_from_asm}"
815
816 # Invoke the driver to assemble/link the .s file to the .exe
817 set comp_output [gcc_target_compile \
818 ${asm_filename} \
819 ${executable_from_asm} \
820 "executable" \
821 "{}"]
822 if ![jit_check_compile \
823 "$name" \
824 "assemble/link of ${asm_filename}" \
825 ${executable_from_asm} \
826 $comp_output] then {
827 return
828 }
829
830 # Verify that the executable was created.
831 if { [file exists $executable_from_asm] == 1} {
832 pass "$executable_from_asm exists"
833 } else {
834 fail "$executable_from_asm does not exist"
835 }
836
837 # Run it and verify that the output matches the regex.
838 jit-run-executable ${executable_from_asm} ${dg-output-text}
839 }
840
841 # Assuming that a .s file has been written out named
842 # OUTPUT_FILENAME, check that the argument matches the
843 # output file.
844 # For use by the test-link-section-assembler.c testcase.
845 proc jit-verify-assembler-output { args } {
846 verbose "jit-verify-assembler: $args"
847
848 if { [llength $args] > 3 } {
849 error "jit-verify-assembler-output: too many arguments"
850 return
851 }
852
853 set dg-output-text [lindex $args 0]
854 verbose "dg-output-text: ${dg-output-text}"
855
856 if { [llength $args] >= 2 } {
857 switch [dg-process-target [lindex $args 1]] {
858 "S" { }
859 "N" { return }
860 "F" { setup_xfail "*-*-*" }
861 "P" { }
862 }
863 }
864
865 upvar 2 name name
866 verbose "name: $name"
867
868 upvar 2 prog prog
869 verbose "prog: $prog"
870 set asm_filename [jit-get-output-filename $prog]
871 verbose " asm_filename: ${asm_filename}"
872
873 # Read the assembly file.
874 set f [open $asm_filename r]
875 set content [read $f]
876 close $f
877
878 # Verify that the assembly matches the regex.
879 if { ![regexp ${dg-output-text} $content] } {
880 fail "${asm_filename} output pattern test, is ${content}, should match ${dg-output-text}"
881 verbose "Failed test for output pattern ${dg-output-text}" 3
882 } else {
883 pass "${asm_filename} output pattern test, ${dg-output-text}"
884 verbose "Passed test for output pattern ${dg-output-text}" 3
885 }
886 }
887
888 # Assuming that a .s file has been written out named
889 # OUTPUT_FILENAME, check that the argument doesn't match
890 # the output file.
891 proc jit-verify-assembler-output-not { args } {
892 verbose "jit-verify-assembler: $args"
893
894 if { [llength $args] > 3 } {
895 error "jit-verify-assembler-output-not: too many arguments"
896 return
897 }
898
899 set dg-output-text [lindex $args 0]
900 verbose "dg-output-text: ${dg-output-text}"
901
902 if { [llength $args] >= 2 } {
903 switch [dg-process-target [lindex $args 1]] {
904 "S" { }
905 "N" { return }
906 "F" { setup_xfail "*-*-*" }
907 "P" { }
908 }
909 }
910
911 upvar 2 name name
912 verbose "name: $name"
913
914 upvar 2 prog prog
915 verbose "prog: $prog"
916 set asm_filename [jit-get-output-filename $prog]
917 verbose " asm_filename: ${asm_filename}"
918
919 # Read the assembly file.
920 set f [open $asm_filename r]
921 set content [read $f]
922 close $f
923
924 # Verify that the assembly matches the regex.
925 if { [regexp ${dg-output-text} $content] } {
926 fail "${asm_filename} output pattern test, is ${content}, should not match ${dg-output-text}"
927 verbose "Failed test for output pattern ${dg-output-text}" 3
928 } else {
929 pass "${asm_filename} output pattern test, ${dg-output-text}"
930 verbose "Passed test for output pattern ${dg-output-text}" 3
931 }
932 }
933
934
935 # Assuming that a .o file has been written out named
936 # OUTPUT_FILENAME, invoke the driver to try to turn it into
937 # an executable, and try to run the result.
938 # For use by the test-compile-to-object.c testcase.
939 proc jit-verify-object { args } {
940 verbose "jit-verify-object: $args"
941
942 set dg-output-text [lindex $args 0]
943 verbose "dg-output-text: ${dg-output-text}"
944
945 upvar 2 name name
946 verbose "name: $name"
947
948 upvar 2 prog prog
949 verbose "prog: $prog"
950 set obj_filename [jit-get-output-filename $prog]
951 verbose " obj_filename: ${obj_filename}"
952
953 # Name the linked executable as OUTPUT_FILENAME with
954 # ".exe" appended.
955 set executable_from_obj ${obj_filename}.exe
956 verbose " executable_from_obj: ${executable_from_obj}"
957
958 # Invoke the driver to link the .o file to the .exe
959 set comp_output [gcc_target_compile \
960 ${obj_filename} \
961 ${executable_from_obj} \
962 "executable" \
963 "{}"]
964 if ![jit_check_compile \
965 "$name" \
966 "link of ${obj_filename}" \
967 ${executable_from_obj} \
968 $comp_output] then {
969 return
970 }
971
972 # Verify that the executable was created.
973 if { [file exists $executable_from_obj] == 1} {
974 pass "$executable_from_obj exists"
975 } else {
976 fail "$executable_from_obj does not exist"
977 }
978
979 # Run it and verify that the output matches the regex.
980 jit-run-executable ${executable_from_obj} ${dg-output-text}
981 }
982
983 # Assuming that a .so file has been written out named
984 # OUTPUT_FILENAME, build a test executable to use it,
985 # and try to run the result.
986 # For use by the test-compile-to-dynamic-library.c testcase.
987 proc jit-verify-dynamic-library { args } {
988 verbose "jit-verify-object: $args"
989
990 global srcdir
991 global subdir
992
993 set dg-output-text [lindex $args 0]
994 verbose "dg-output-text: ${dg-output-text}"
995
996 upvar 2 name name
997 verbose "name: $name"
998
999 upvar 2 prog prog
1000 verbose "prog: $prog"
1001 set obj_filename [jit-get-output-filename $prog]
1002 verbose " obj_filename: ${obj_filename}"
1003
1004 # Build a test executable from
1005 # verify-dynamic-library.c
1006 set test_src "verify-dynamic-library.c"
1007 set test_executable ${test_src}.exe
1008 verbose " test_executable: ${test_executable}"
1009
1010 # Invoke the driver to build the test executable
1011 set comp_output [gcc_target_compile \
1012 $srcdir/$subdir/${test_src} \
1013 ${test_executable} \
1014 "executable" \
1015 "{additional_flags=-ldl}"]
1016 if ![jit_check_compile \
1017 "$name" \
1018 "build of ${test_executable}" \
1019 ${test_executable} \
1020 $comp_output] then {
1021 return
1022 }
1023
1024 # Verify that the test executable was created.
1025 if { [file exists $test_executable] == 1} {
1026 pass "$test_executable exists"
1027 } else {
1028 fail "$test_executable does not exist"
1029 }
1030
1031 # Run it and verify that the output matches the regex.
1032 jit-run-executable ${test_executable} ${dg-output-text}
1033 }
1034
1035 # A way to invoke "jit-run-executable" with the given regex,
1036 # using OUTPUT_FILENAME within the testcase to determine
1037 # the name of the executable to run.
1038 # For use by the test-compile-to-executable.c testcase.
1039
1040 proc jit-verify-executable { args } {
1041 verbose "jit-verify-executable: $args"
1042
1043 set dg-output-text [lindex $args 0]
1044 verbose "dg-output-text: ${dg-output-text}"
1045
1046 upvar 2 name name
1047 verbose "name: $name"
1048
1049 upvar 2 prog prog
1050 verbose "prog: $prog"
1051 set output_filename [jit-get-output-filename $prog]
1052 verbose " output_filename: $output_filename"
1053
1054 jit-run-executable $output_filename ${dg-output-text}
1055 }
1056
1057 set DEFAULT_CFLAGS "-I$srcdir/../jit -lgccjit -g -Wall -Werror"
1058
1059 # We need to link with --export-dynamic for test-calling-external-function.c
1060 # so that the JIT-built code can call into functions from the main program.
1061
1062 if { [check_effective_target_rdynamic] } {
1063 set DEFAULT_CFLAGS "$DEFAULT_CFLAGS -rdynamic"
1064 }
1065
1066 # Main loop. This will invoke jig-dg-test on each test-*.c file.
1067 dg-runtest $tests "" $DEFAULT_CFLAGS
1068
1069 # All done.
1070 dg-finish