AArch64: Add MTE ptrace requests
[binutils-gdb.git] / sim / testsuite / lib / sim-defs.exp
index 8bc0bf3cc2dd8c59cef542cfefb2a59020d02f16..0157f9bb2834576b0866a5601261bbea653d2b7d 100644 (file)
@@ -1,4 +1,5 @@
 # Simulator dejagnu utilities.
+# TODO: Switch to using dg-xxx helpers rather than parsing the files directly.
 
 # Communicate simulator path from sim_init to sim_version.
 # For some reason [board_info target sim] doesn't work in sim_version.
@@ -12,6 +13,16 @@ proc sim_init { args } {
     global sim_path
     set sim_path [board_info target sim]
     # Need to return an empty string (copied from GAS).
+
+    # As gross as it is, we unset the linker script specified by the target
+    # board.  The simulator board file mips-sim.exp, sets ldscript to the
+    # MIPS libgloss linker scripts which include libgcc (and possibly other
+    # libraries), which the linker (used to link these tests rather than the
+    # compiler) can't necessarily find.  Similarly iq2000-sim.exp and
+    # m68hc11-sim.exp.  So, we make it a common rule to clear the slate for
+    # all simulators.
+    unset_currtarget_info ldscript
+
     return ""
 }
 
@@ -51,7 +62,7 @@ proc sim_compile { source dest type options } {
 #      timeout=val     - set the timeout to val for this run
 #
 # The result is a list of two elements.
-# The first is one of pass/fail/etc.
+# The first is the program's exit status (0/1/etc...).
 # The second is the program's output.
 #
 # This is different than the sim_load routine provided by
@@ -82,19 +93,55 @@ proc sim_run { prog sim_opts prog_opts redir options } {
        } elseif [regexp {^timeout=(.*)} $o full val] {
            set testcase_timeout $val
        }
+       
     }
+       
+    verbose "testcase timeout is set to $testcase_timeout" 1
 
     set sim [board_info target sim]
+    if [string equal "" $sim] {
+       # Special case the simulator.  These tests are designed to
+       # be run inside of the simulator, not on the native host.
+       # So if the sim target isn't set, default to the target run.
+       # These global variables come from generated site.exp.
+       global objdir
+       global arch
+       set sim "$objdir/$arch/run"
+    }
+
+    if [is_remote host] {
+       set prog [remote_download host $prog]
+       if { $prog == "" } {
+           error "download failed"
+           return -1
+       }
+    }
+
+    set board [target_info name]
+    if [board_info $board exists sim,options] {
+       set always_opts [board_info $board sim,options]
+    } else {
+       set always_opts ""
+    }
 
     # FIXME: this works for UNIX only
     if { "$testcase_env" != "" } {
        set sim "env $testcase_env $sim"
     }
 
+    if { [board_info target sim,protocol] == "sid" } {
+       set cmd ""
+       set sim_opts "$sim_opts -e \"set cpu-loader file [list ${prog}]\""
+    } else {
+       set cmd "$prog"
+    }
+
+    send_log "$sim $always_opts $SIMFLAGS $sim_opts $cmd $prog_opts\n"
+
     if { "$redir" == "" } {
-       remote_spawn host "$sim $SIMFLAGS $sim_opts $prog $prog_opts"
+       remote_spawn host "$sim $always_opts $SIMFLAGS $sim_opts $cmd $prog_opts"
     } else {
-       remote_spawn host "$sim $SIMFLAGS $sim_opts $prog $prog_opts $redir" writeonly
+       remote_spawn host "$sim $always_opts $SIMFLAGS $sim_opts $cmd $prog_opts $redir" writeonly
     }
     set result [remote_wait host $testcase_timeout]
 
@@ -104,33 +151,52 @@ proc sim_run { prog sim_opts prog_opts redir options } {
     # we want to match.
     regsub -all -- "\r" $output "" output
 
-    # ??? Not sure the test for pass/fail is right.
-    # We just care that the simulator ran correctly, not whether the simulated
-    # program return 0 or non-zero from `main'.
-    set status fail
-    if { $return_code == 0 } {
-       set status pass
+    if [is_remote host] {
+       # clean up after ourselves.
+       remote_file host delete $prog
     }
 
-    return [list $status $output]
+    return [list $return_code $output]
 }
 
 # Run testcase NAME.
 # NAME is either a fully specified file name, or just the file name in which
 # case $srcdir/$subdir will be prepended.
-# The file can contain options in the form "# option(mach list): value"
+# REQUESTED_MACHS is a list of machines to run the testcase on.  If NAME isn't
+# for the specified machine(s), it is ignored.
+# Typically REQUESTED_MACHS contains just one element, it is up to the caller
+# to iterate over the desired machine variants.
+#
+# The file can contain options in the form "# option(mach list): value".
 # Possibilities:
-# mach(): machine names
-# as(mach): <assembler options>
-# ld(mach): <linker options>
-# sim(mach): <simulator options>
-# output(): program output pattern to match with string-match
-# If `output' is not specified, the program must output "pass".
-
-proc run_sim_test { name } {
+# mach: [all | machine names]
+# as[(mach-list)]: <assembler options>
+# ld[(mach-list)]: <linker options>
+# cc[(mach-list)]: <compiler options>
+# sim[(mach-list)]: <simulator options>
+# progopts: <arguments to the program being simulated>
+# status: program exit status to treat as "pass"
+# output: program output pattern to match with string-match
+# xerror: program is expected to return with a "failure" exit code
+# xfail: <PRMS-opt> <target-triplets-where-test-fails>
+# kfail: <PRMS> <target-triplets-where-test-fails>
+# If `output' is not specified, the program must output "pass" if !xerror or
+# "fail" if xerror.
+# The parens in "optname()" are optional if the specification is for all machs.
+# Multiple "output", "xfail" and "kfail" options concatenate.
+# The xfail and kfail arguments are space-separated target triplets and PRIDs.
+# There must be a PRMS (bug report ID) specified for kfail, while it's
+# optional for xfail.
+
+proc run_sim_test { name requested_machs } {
     global subdir srcdir
-    global AS ASFLAGS LD LDFLAGS SIMFLAGS
+    global SIMFLAGS
     global opts
+    global cpu_option
+    global global_as_options
+    global global_ld_options
+    global global_cc_options
+    global global_sim_options
 
     if [string match "*/*" $name] {
        set file $name
@@ -144,12 +210,49 @@ proc run_sim_test { name } {
        unresolved $subdir/$name
        return
     }
-    set opts(as) {}
-    set opts(ld) {}
-    set opts(sim) {}
-    set opts(output) {}
-    set opts(mach) {}
-    set opts(timeout) {}
+    # Clear default options
+    set opts(as) ""
+    set opts(ld) ""
+    set opts(cc) ""
+    set opts(progopts) ""
+    set opts(sim) ""
+    set opts(status) "0"
+    set opts(output) ""
+    set opts(mach) ""
+    set opts(timeout) ""
+    set opts(xerror) "no"
+    set opts(xfail) ""
+    set opts(kfail) ""
+    set seen_output 0
+
+    if ![info exists global_as_options] {
+        set global_as_options ""
+    }
+    if ![info exists global_ld_options] {
+        set global_ld_options ""
+    }
+    if ![info exists global_cc_options] {
+        set global_cc_options ""
+    }
+    if ![info exists global_sim_options] {
+        set global_sim_options ""
+    }
+
+    # Clear any machine specific options specified in a previous test case
+    foreach m $requested_machs {
+       if [info exists opts(as,$m)] {
+           unset opts(as,$m)
+       }
+       if [info exists opts(ld,$m)] {
+           unset opts(ld,$m)
+       }
+       if [info exists opts(cc,$m)] {
+           unset opts(cc,$m)
+       }
+       if [info exists opts(sim,$m)] {
+           unset opts(sim,$m)
+       }
+    }
 
     foreach i $opt_array {
        set opt_name [lindex $i 0]
@@ -160,6 +263,16 @@ proc run_sim_test { name } {
            unresolved $subdir/$name
            return
        }
+       # Multiple "output" specifications concatenate, they don't override.
+       if { $opt_name == "output" } {
+           set opt_val "$opts(output)$opt_val"
+           set seen_output 1
+       }
+       # Similar with "xfail" and "kfail", but arguments are space-separated.
+       if { $opt_name == "xfail" || $opt_name == "kfail" } {
+           set opt_val "$opts($opt_name) $opt_val"
+       }
+
        foreach m $opt_machs {
            set opts($opt_name,$m) $opt_val
        }
@@ -170,34 +283,87 @@ proc run_sim_test { name } {
 
     set testname $name
     set sourcefile $file
-    if { $opts(output) == "" } {
-       set opts(output) "pass\n"
+    if { $seen_output == 0 } {
+       if { "$opts(xerror)" == "no" } {
+           set opts(output) "pass\n"
+       } else {
+           set opts(output) "fail\n"
+       }
+    }
+    # Change \n sequences to newline chars.
+    regsub -all "\\\\n" $opts(output) "\n" opts(output)
+
+    set testcase_machs $opts(mach)
+    if { "$testcase_machs" == "all" } {
+       set testcase_machs $requested_machs
     }
 
-    foreach mach $opts(mach) {
-       verbose "Testing $name on $mach."
+    foreach mach $testcase_machs {
+       if { [lsearch $requested_machs $mach] < 0 } {
+           verbose -log "Skipping $mach version of $name, not requested."
+           continue
+       }
+
+       verbose -log "Testing $name on machine $mach."
+
+       # Time to setup xfailures and kfailures.
+       if { "$opts(xfail)" != "" } {
+           verbose -log "xfail: $opts(xfail)"
+           # Using eval to make $opts(xfail) appear as individual
+           # arguments.
+           eval setup_xfail $opts(xfail)
+       }
+       if { "$opts(kfail)" != "" } {
+           verbose -log "kfail: $opts(kfail)"
+           eval setup_kfail $opts(kfail)
+       }
 
        if ![info exists opts(as,$mach)] {
            set opts(as,$mach) $opts(as)
        }
-       send_log "$AS $ASFLAGS $opts(as,$mach) -I$srcdir/$subdir -o ${name}.o $sourcefile\n"
-       catch "exec $AS $ASFLAGS $opts(as,$mach) -I$srcdir/$subdir -o ${name}.o $sourcefile" comp_output
 
-       if ![string match "" $comp_output] {
-           verbose -log "$comp_output" 3
-           fail "$mach $testname"
-           continue
+       set as_options "$opts(as,$mach) -I$srcdir/$subdir"
+       if [info exists cpu_option] {
+           set as_options "$as_options $cpu_option=$mach"
        }
+       regsub {(^ *| +)([^ ]+)} "$as_options $global_as_options" { -Wa,\2} c_as_options
 
        if ![info exists opts(ld,$mach)] {
            set opts(ld,$mach) $opts(ld)
        }
-       send_log "$LD $LDFLAGS $opts(ld,$mach) -o ${name}.x ${name}.o\n"
-       catch "exec $LD $LDFLAGS $opts(ld,$mach) -o ${name}.x ${name}.o" comp_output
+       regsub {(^ *| +)([^ ]+)} "$opts(ld,$mach) $global_ld_options" { -Wl,\2} c_ld_options
+
+       if ![info exists opts(cc,$mach)] {
+           set opts(cc,$mach) $opts(cc)
+       }
+
+       if [string match "*.c" $sourcefile] {
+           set comp_output [target_compile $sourcefile ${name}.x "executable" \
+               [list "incdir=$srcdir/$subdir" "additional_flags=$c_as_options $c_ld_options $opts(cc,$mach) $global_cc_options"]]
+           set method "compiling/linking"
+       } else {
+           if [string match "*.S" $sourcefile] {
+               set comp_output [target_compile $sourcefile ${name}.o "object" \
+                   [list "incdir=$srcdir/$subdir" "additional_flags=$c_as_options"]]
+               set method "compiling"
+           } else {
+               set comp_output [target_assemble $sourcefile ${name}.o "$as_options $global_as_options"]
+               set method "assembling"
+           }
+
+           if ![string match "" $comp_output] {
+               verbose -log "$comp_output" 3
+               fail "$mach $testname (${method})"
+               continue
+           }
+
+           set comp_output [target_link ${name}.o ${name}.x "$opts(ld,$mach) $global_ld_options"]
+           set method "linking"
+       }
 
        if ![string match "" $comp_output] {
            verbose -log "$comp_output" 3
-           fail "$mach $testname"
+           fail "$mach $testname (${method})"
            continue
        }
 
@@ -212,24 +378,54 @@ proc run_sim_test { name } {
            set options "$options timeout=$opts(timeout)"
        }
 
-       set result [sim_run ${name}.x "$opts(sim,$mach)" "" "" "$options"]
-       set status [lindex $result 0]
+       set result [sim_run ${name}.x "$opts(sim,$mach) $global_sim_options" "$opts(progopts)" "" "$options"]
+       set return_code [lindex $result 0]
        set output [lindex $result 1]
 
+       set status fail
+       if { $return_code == $opts(status) } {
+           set status pass
+       }
+
        if { "$status" == "pass" } {
-           if ![string match $opts(output) $output] {
-               verbose -log "output:  $output" 3
-               verbose -log "pattern: $opts(output)" 3
+           if { "$opts(xerror)" == "no" } {
+               if [string match $opts(output) $output] {
+                   pass "$mach $testname"
+                   file delete ${name}.o ${name}.x
+               } else {
+                   verbose -log "status:  $return_code" 3
+                   verbose -log "output:  $output" 3
+                   verbose -log "pattern: $opts(output)" 3
+                   fail "$mach $testname (execution)"
+               }
+           } else {
+               verbose -log "`pass' return code when expecting failure" 3
+               fail "$mach $testname (execution)"
+           }
+       } elseif { "$status" == "fail" } {
+           if { "$opts(xerror)" == "no" } {
+               fail "$mach $testname (execution)"
+           } else {
+               if [string match $opts(output) $output] {
+                   pass "$mach $testname"
+                   file delete ${name}.o ${name}.x
+               } else {
+                   verbose -log "status:  $return_code" 3
+                   verbose -log "output:  $output" 3
+                   verbose -log "pattern: $opts(output)" 3
+                   fail "$mach $testname (execution)"
+               }
            }
+       } else {
+           $status "$mach $testname"
        }
-
-       $status "$mach $testname"
     }
 }
 
 # Subroutine of run_sim_test to process options in FILE.
 
 proc slurp_options { file } {
+    global subdir srcdir
     if [catch { set f [open $file r] } x] {
        #perror "couldn't open `$file': $x"
        perror "$x"
@@ -241,21 +437,25 @@ proc slurp_options { file } {
     set nws {[^        ]*}
     # whitespace is ignored anywhere except within the options list;
     # option names are alphabetic only
-    set pat "^#${ws}(\[a-zA-Z\]*)\\((.*)\\)$ws:${ws}(.*)$ws\$"
-    # Allow comment as first line of file.
-    set firstline 1
+    set pat "^#${ws}(\[a-zA-Z\]*)\\(?(\[^):\]*)\\)?$ws:${ws}(.*)$ws\$"
+    # Allow arbitrary lines until the first option is seen.
+    set seen_opt 0
     while { [gets $f line] != -1 } {
        set line [string trim $line]
        # Whitespace here is space-tab.
        if [regexp $pat $line xxx opt_name opt_machs opt_val] {
            # match!
+           set opt_val [string map [list \
+               {$srcdir} "$srcdir" \
+               {$subdir} "$subdir" \
+           ] "$opt_val"]
            lappend opt_array [list $opt_name $opt_machs $opt_val]
+           set seen_opt 1
        } else {
-           if { ! $firstline } {
+           if { $seen_opt } {
                break
            }
        }
-       set firstline 0
     }
     close $f
     return $opt_array