OSDN Git Service

55b26f9602d858790393002ec2fb168bd45c90a5
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.target / mips / mips.exp
1 # Copyright (C) 1997, 2007, 2008, 2009, 2010 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 # A MIPS version of the GCC dg.exp driver.
18 #
19 # There are many MIPS features that we want to test, and many of those
20 # features are specific to certain architectures, certain ABIs and so on.
21 # There are therefore many cases in which we want to test something that
22 # is incompatible with the user's chosen test options.
23 #
24 # In most dg testsuites, the options added by dg-options have a lower
25 # priority than the options chosen by the user.  For example, if a test
26 # specifies:
27 #
28 #    { dg-options "-mips1" }
29 #
30 # and the user passes the following option to runtest:
31 #
32 #    --target_board unix/-mips3
33 #
34 # the test would be compiled as MIPS III rather than MIPS I.  If the
35 # test really wouldn't work with -mips3, normal practice would be to
36 # have something like:
37 #
38 #    { dg-do compile { target can_force_mips1 } }
39 #
40 # so that the test is skipped when an option like -mips3 is passed.
41 #
42 # Sticking to the same approach here would cause us to skip many tests,
43 # even though the toolchain can generate the required code.  For example,
44 # there are 6 MIPS ABIs, plus variants.  Some configurations support
45 # more than one ABI, so it is natural to use something like:
46 #
47 #    --target_board unix{-mabi=n32,-mabi=32,-mabi=64}
48 #
49 # when testing them.  But these -mabi=* options would normally prevent any
50 # EABI and o64 tests from running.
51 #
52 # This testsuite therefore defines a local version of dg-options that
53 # overrides any user options that are incompatible with the test options.
54 # It tries to keep the other user options intact.
55 #
56 #
57 # Most of the tests in this testsuite are scan-assembler tests, but
58 # sometimes we need a link test instead.  In these cases, we must not
59 # try to link code with options that are incompatible with the current
60 # multilib, because xgcc is passed -L and -B options that are specific
61 # to that multilib.
62 #
63 # Normal GCC practice would be to skip incompatible link tests as
64 # unsupported, but in this particular case, it seems better to downgrade
65 # them to an assemble test instead.  At least that way we get some
66 # test-for-ICE and code-sanity coverage.
67 #
68 # The same problem applies to run tests.  If a test requires runtime
69 # support for a particular feature, and if the current target does not
70 # provide that support, normal practice would be to skip the test.
71 # But in this case it seems better to downgrade it to a link test instead.
72 # (We might then have to downgrade it to an assembler test according to
73 # the constraints just mentioned.)
74 #
75 # The local dg-options therefore checks whether the new options are
76 # link-compatiable with the user's options.  If not, it automatically
77 # downgrades link tests to assemble tests.  It does the same for run
78 # tests, but in addition, it downgrades run tests to link tests if the
79 # target does not provide runtime support for a required feature or ASE.
80 #
81 #
82 # Another problem is that many of the options we want to test require
83 # certain other features.  For example, -mips3d requires both 64-bit
84 # FPRs and a MIPS32 or MIPS64 target; -mfix-r10000 requires branch-
85 # likely instructions; and so on.  We could handle this by specifying
86 # a set of options that are guaranteed to give us what we want, such as:
87 #
88 #   dg-options "-mips3d -mpaired-single -mhard-float -mgp64 -mfp64 -mabi=n32 -march=mips64 -mips64"
89 #
90 # With the new dg-options semantics, this would override any troublesome
91 # user options like -mips3, -march=vr4100, -mfp32, -mgp32, -msoft-float,
92 # -mno-paired-single and so on.  But there are three major problems with
93 # this:
94 #
95 #   - It is easy to forget options.
96 #
97 #   - If a new option is added, all tests that are incompatible with that
98 #     option must be updated.
99 #
100 #   - We want to be able to test MIPS-3D with things like -march=mips32,
101 #     -march=mips64r2, -march=sb1, and so on.
102 #
103 # The local version of dg-options therefore works out the requirements
104 # of each test option.  As with the test options themselves, the local
105 # dg-options overrides any user options that incompatible with these
106 # requirements, but it keeps the other user options the same.
107 #
108 # For example, if the user passes -mips3, a MIPS-3D test will choose
109 # a different architecture like -mips64 instead.  But if the user
110 # passes -march=sb1, MIPS-3D tests will be run with that option.
111 #
112 #
113 # Sometimes it is useful to say "I want an environment that is compatible
114 # with option X, but I don't want to pass option X itself".  The main example
115 # of this is -mips16: we want to be able to test __attribute__((mips16))
116 # without requiring the test itself to be compiled as -mips16.  The local
117 # version of dg-options lets you do this by putting X in parentheses.
118 # For example:
119 #
120 #   { dg-options "(-mips16)" }
121 #
122 # selects a MIPS16-compatible target without passing -mips16 itself.
123 #
124 # It is also useful to say "any architecture within this ISA range is fine".
125 # This can be done using special pseudo-options of the form:
126 #
127 #   PROP=VALUE PROP<=VALUE PROP>=VALUE
128 #
129 # where PROP can be:
130 #
131 #   isa:
132 #       the value of the __mips macro.
133 #
134 #   isa_rev:
135 #       the value of the __mips_isa_rev macro, or 0 if it isn't defined.
136 #
137 # For example, "isa_rev>=1" selects a MIPS32 or MIPS64 processor,
138 # "isa=4" selects a MIPS IV processor, and so on.
139 #
140 # There are also the following special pseudo-options:
141 #
142 #   isa=loongson
143 #       select a Loongson processor
144 #
145 #   addressing=absolute
146 #       force absolute addresses to be used
147 #
148 #   forbid_cpu=REGEXP
149 #       forbid processors that match the given regexp; choose a
150 #       generic ISA instead.
151 #
152 #
153 # In summary:
154 #
155 #   (1) Try to avoid { target ... } requirements wherever possible.
156 #       Specify the requirements as dg-options instead.
157 #
158 #   (2) Don't worry about the consequences of (1) for link and run tests.
159 #       If the test uses { dg-do link } or { dg-do run }, and its
160 #       dg-options are incompatible with the current target, the
161 #       testsuite will downgrade them where necessary.
162 #
163 #   (3) Try to use the bare minimum of options and leave dg-options
164 #       to work out the dependencies.  For example, if you want
165 #       a MIPS-3D test, you should generally just specify -mips3d.
166 #       Don't specify an architecture option like -mips64 unless
167 #       the test really doesn't work with -mips32r2, -mips64r2,
168 #       -march=sb1, etc.
169 #
170 #   (4) If you want something compatible with a particular option,
171 #       but don't want to pass the option itself, wrap that option
172 #       in parentheses.  In particular, pass '(-mips16)' if you
173 #       want to use "mips16" attributes.
174 #
175 #   (5) When testing a feature of a generic ISA (as opposed to a
176 #       processor-specific extension), try to use the "isa" and
177 #       "isa_rev" pseudo-options instead of specific architecture
178 #       options.  For example, if the feature is present on revision 2
179 #       processors and above, try to use "isa_rev>=2" instead of
180 #       "-mips32r2" or "-mips64r2".
181 #
182 #   (6) If you need to disable processor-specific extensions use
183 #       forbid_cpu=REGEXP instead of forcing a generic ISA.
184 #
185 #
186 # Terminology
187 #
188 #   Option group or just group:
189 #      See comment before mips_option_groups.
190 #
191 #   Test options:
192 #      The options specified in dg-options.
193 #
194 #   Explicit options:
195 #      The options that were either passed to runtest as "multilib" options
196 #      (e.g. -mips4 in --target_board=mips-sim-idt/-mips4) or specified as
197 #      test options.  Note that options in parenthesis (i.e. (-mips16)) are
198 #      not explicit and can be omitted depending on the base options.
199 #
200 #   Base options:
201 #      Options that are on by default without being specified in dg-options,
202 #      e.g. -march=mips64r2 for mipsisa64r2-elf or because they've been
203 #      passed to runtest as "multilib" options.
204 #
205 #   Option array:
206 #      Many functions in this file work with option arrays.  These are
207 #      two-dimensional Tcl arrays where the first dimension can have three
208 #      values: option, explicit_p or test_option_p.  The second dimension is
209 #      the name of the option group.  "option" contains the name of the
210 #      option that is in effect from this group.  If no option is active it
211 #      contains the empty string.  The flags "explicit_p" and "test_option_p"
212 #      are set for explicit and test options.
213
214 # Exit immediately if this isn't a MIPS target.
215 if ![istarget mips*-*-*] {
216     return
217 }
218
219 # Load support procs.
220 load_lib gcc-dg.exp
221
222 # A list of GROUP REGEXP pairs.  Each GROUP represents a logical group of
223 # options from which only one option should be chosen.  REGEXP matches all
224 # the options in that group; it is implicitly wrapped in "^(...)$".
225 set mips_option_groups {
226     abi "-mabi=.*"
227     addressing "addressing=.*"
228     arch "-mips([1-5]|32.*|64.*)|-march=.*|isa(|_rev)(=|<=|>=).*"
229     debug "-g*"
230     dump_pattern "-dp"
231     endianness "-E(L|B)|-me(l|b)"
232     float "-m(hard|soft)-float"
233     forbid_cpu "forbid_cpu=.*"
234     fp "-mfp(32|64)"
235     gp "-mgp(32|64)"
236     long "-mlong(32|64)"
237     mips16 "-mips16|-mno-mips16|-mflip-mips16"
238     mips3d "-mips3d|-mno-mips3d"
239     optimization "-O(|[0-3s])"
240     pic "-f(no-|)(pic|PIC)"
241     profiling "-pg"
242     small-data "-G[0-9]+"
243     warnings "-w"
244 }
245
246 # Add -mfoo/-mno-foo options to mips_option_groups.
247 foreach option {
248     abicalls
249     branch-likely
250     dsp
251     dspr2
252     explicit-relocs
253     extern-sdata
254     fix-r4000
255     fix-r10000
256     fix-vr4130
257     gpopt
258     local-sdata
259     long-calls
260     paired-single
261     plt
262     shared
263     smartmips
264     sym32
265     synci
266     relax-pic-calls
267     mcount-ra-address
268 } {
269     lappend mips_option_groups $option "-m(no-|)$option"
270 }
271
272 # Add -mfoo= options to mips_option_groups.
273 foreach option {
274     branch-cost
275     code-readable
276     r10k-cache-barrier
277     tune
278 } {
279     lappend mips_option_groups $option "-m$option=.*"
280 }
281
282 # Add -ffoo/-fno-foo options to mips_option_groups.
283 foreach option {
284     delayed-branch
285     fast-math
286     finite-math-only
287     fixed-hi
288     fixed-lo
289     lax-vector-conversions
290     split-wide-types
291     tree-vectorize
292 } {
293     lappend mips_option_groups $option "-f(no-|)$option"
294 }
295
296 # A list of option groups that have an impact on the ABI.
297 set mips_abi_groups {
298     abi
299     abicalls
300     arch
301     endianness
302     float
303     fp
304     gp
305     gpopt
306     long
307     pic
308     small-data
309 }
310
311 # mips_option_tests(OPTION) is some assembly code that will run to completion
312 # on a target that supports OPTION.
313 set mips_option_tests(-mips16) {
314     move $2,$31
315     bal 1f
316     .set mips16
317     jr $31
318     .set nomips16
319     .align 2
320 1:
321     ori $3,$31,1
322     jalr $3
323     move $31,$2
324 }
325 set mips_option_tests(-mpaired-single) {
326     .set mips64
327     lui $2,0x3f80
328     mtc1 $2,$f0
329     cvt.ps.s $f2,$f0,$f0
330 }
331 set mips_option_tests(-mips3d) {
332     .set mips64
333     .set mips3d
334     lui $2,0x3f80
335     mtc1 $2,$f0
336     cvt.ps.s $f2,$f0,$f0
337     mulr.ps $f2,$f2,$f2
338     rsqrt1.s $f2,$f0
339     mul.s $f4,$f2,$f0
340     rsqrt2.s $f4,$f4,$f2
341     madd.s $f4,$f2,$f2,$f4
342 }
343 set mips_option_tests(-mdsp) {
344     .set mips64r2
345     .set dsp
346     addsc $2,$2,$2
347 }
348 set mips_option_tests(-mdspr2) {
349     .set mips64r2
350     .set dspr2
351     prepend $2,$3,11
352 }
353
354 # Canonicalize command-line option OPTION.
355 proc mips_canonicalize_option { option } {
356     regsub {^-mips([1-5]|32*|64*)$} $option {-march=mips\1} option
357
358     regsub {^-mel$} $option {-EL} option
359     regsub {^-meb$} $option {-EB} option
360
361     regsub {^-O$} $option {-O1} option
362
363     # MIPS doesn't use -fpic and -fPIC to distinguish between code models.
364     regsub {^-f(no-|)PIC} $option {-f\1pic} option
365
366     return $option
367 }
368
369 # Return true if OPTION1 and OPTION2 represent the same command-line option.
370 proc mips_same_option_p { option1 option2 } {
371     return [string equal \
372                 [mips_canonicalize_option $option1] \
373                 [mips_canonicalize_option $option2]]
374 }
375
376 # Preprocess CODE using target_compile options OPTIONS.  Return the
377 # compiler output.
378 proc mips_preprocess { options code } {
379     global tool
380
381     set src dummy[pid].c
382     set f [open $src "w"]
383     puts $f $code
384     close $f
385     set output [${tool}_target_compile $src "" preprocess $options]
386     file delete $src
387
388     return $output
389 }
390
391 # Set the target board's command-line options to NEW_OPTIONS, storing the
392 # old values in UPVAR.
393 proc mips_push_test_options { upvar new_options } {
394     upvar $upvar var
395     global board_info
396
397     array unset var
398     set var(name) board_info([target_info name],multilib_flags)
399     if { [info exists $var(name)] } {
400         set var(old_options) [set $var(name)]
401         set $var(name) [join $new_options " "]
402     }
403 }
404
405 # Undo the effects of [mips_push_test_options UPVAR ...]
406 proc mips_pop_test_options { upvar } {
407     upvar $upvar var
408     global board_info
409
410     if { [info exists var(old_options)] } {
411         set $var(name) $var(old_options)
412     }
413 }
414
415 # Return property PROP for architecture option ARCH (which belongs to
416 # the "arch" group in mips_option_groups).  See the comment at the
417 # top of the file for the valid property names.
418 #
419 # Cache the results in mips_arch_info (which can be reused between test
420 # variants).
421 proc mips_arch_info { arch prop } {
422     global mips_arch_info
423     global board_info
424
425     set arch [mips_canonicalize_option $arch]
426     if { ![info exists mips_arch_info($arch,$prop)] } {
427         mips_push_test_options saved_options {}
428         set output [mips_preprocess [list "additional_flags=$arch -mabi=32"] {
429             int isa = __mips;
430             #ifdef __mips_isa_rev
431             int isa_rev = __mips_isa_rev;
432             #else
433             int isa_rev = 0;
434             #endif
435         }]
436         foreach lhs { isa isa_rev } {
437             regsub ".*$lhs = (\[^;\]*).*" $output {\1} rhs
438             verbose -log "Architecture $arch has $lhs $rhs"
439             set mips_arch_info($arch,$lhs) $rhs
440         }
441         mips_pop_test_options saved_options
442     }
443     return $mips_arch_info($arch,$prop)
444 }
445
446 # Return the option group associated with OPTION, or "" if none.
447 proc mips_option_maybe_group { option } {
448     global mips_option_groups
449
450     foreach { group regexp } $mips_option_groups {
451         if { [regexp -- "^($regexp)\$" $option] } {
452             return $group
453         }
454     }
455     return ""
456 }
457
458 # Return the option group associated with OPTION.  Raise an error if
459 # there is none.
460 proc mips_option_group { option } {
461     set group [mips_option_maybe_group $option]
462     if { [string equal $group ""] } {
463         error "Unrecognised option: $option"
464     }
465     return $group
466 }
467
468 # Return the option for option group GROUP, or "" if no option in that
469 # group has been chosen.  UPSTATUS describes the option status.
470 proc mips_option { upstatus group } {
471     upvar $upstatus status
472
473     return $status(option,$group)
474 }
475
476 # If the base options for this test run include an option in group GROUP,
477 # return that option, otherwise return "".
478 proc mips_original_option { group } {
479     global mips_base_options
480
481     return [mips_option mips_base_options $group]
482 }
483
484 # Return true if the test described by UPSTATUS requires a specific
485 # option in group GROUP.  UPSTATUS describes the option status.
486 proc mips_test_option_p { upstatus group } {
487     upvar $upstatus status
488
489     return $status(test_option_p,$group)
490 }
491
492 # If the test described by UPSTATUS requires a particular option in group
493 # GROUP, return that option, otherwise return "".
494 proc mips_test_option { upstatus group } {
495     upvar $upstatus status
496
497     if { [mips_test_option_p status $group] } {
498         return [mips_option status $group]
499     } else {
500         return ""
501     }
502 }
503
504 # Return true if the options described by UPSTATUS include OPTION.
505 proc mips_have_option_p { upstatus option } {
506     upvar $upstatus status
507
508     return [mips_same_option_p \
509                 [mips_option status [mips_option_group $option]] \
510                 $option]
511 }
512
513 # Return true if the options described by UPSTATUS require MIPS16 support.
514 proc mips_using_mips16_p { upstatus } {
515     upvar $upstatus status
516
517     return [expr { [mips_have_option_p status "-mips16"]
518                    || [mips_have_option_p status "-mflip-mips16"] }]
519 }
520
521 # Return true if the test described by UPSTATUS requires option OPTION.
522 proc mips_have_test_option_p { upstatus option } {
523     upvar $upstatus status
524
525     set group [mips_option_group $option]
526     return [mips_same_option_p [mips_test_option status $group] $option]
527 }
528
529 # If the test described by UPSTATUS does not specify an option in
530 # OPTION's group, act as though it had specified OPTION.
531 #
532 # The first optional argument indicates whether the option should be
533 # treated as though it were wrapped in parentheses; see the comment at
534 # the top of the file for details about this convention.  The default is 0.
535 proc mips_make_test_option { upstatus option args } {
536     upvar $upstatus status
537
538     set group [mips_option_group $option]
539     if { ![mips_test_option_p status $group] } {
540         set status(option,$group) $option
541         set status(test_option_p,$group) 1
542         if { [llength $args] == 0 || ![lindex $args 0] } {
543             set status(explicit_p,$group) 1
544         }
545     }
546 }
547
548 # If the test described by UPSTATUS requires option FROM, assume that
549 # it implicitly requires option TO.
550 proc mips_option_dependency { upstatus from to } {
551     upvar $upstatus status
552
553     if { [mips_have_test_option_p status $from] } {
554         mips_make_test_option status $to
555     }
556 }
557
558 # Return true if the given arch-group option specifies a 32-bit ISA.
559 proc mips_32bit_arch_p { option } {
560     set isa [mips_arch_info $option isa]
561     return [expr { $isa < 3 || $isa == 32 }]
562 }
563
564 # Return true if the given arch-group option specifies a 64-bit ISA.
565 proc mips_64bit_arch_p { option } {
566     return [expr { ![mips_32bit_arch_p $option] }]
567 }
568
569 # Return true if the given abi-group option implicitly requires -mgp32.
570 proc mips_32bit_abi_p { option } {
571     switch -glob -- $option {
572         -mabi=32 {
573             return 1
574         }
575     }
576     return 0
577 }
578
579 # Return true if the given abi-group option implicitly requires -mgp64.
580 proc mips_64bit_abi_p { option } {
581     switch -glob -- $option {
582         -mabi=o64 -
583         -mabi=n32 -
584         -mabi=64 {
585             return 1
586         }
587     }
588     return 0
589 }
590
591 # Return true if the given abi-group option implicitly requires -mlong32.
592 # o64 requires this for -mabicalls, but not otherwise; pick the conservative
593 # case for simplicity.
594 proc mips_long32_abi_p { option } {
595     switch -glob -- $option {
596         -mabi=o64 -
597         -mabi=n32 -
598         -mabi=32 {
599             return 1
600         }
601     }
602     return 0
603 }
604
605 # Return true if the given abi-group option implicitly requires -mlong64.
606 proc mips_long64_abi_p { option } {
607     switch -glob -- $option {
608         -mabi=64 {
609             return 1
610         }
611     }
612     return 0
613 }
614
615 # Check whether the current target supports all the options that the
616 # current test requires.  Return "" if so, otherwise return one of
617 # the incompatible options.  UPSTATUS describes the option status.
618 proc mips_first_unsupported_option { upstatus } {
619     global mips_option_tests
620     upvar $upstatus status
621
622     foreach { option code } [array get mips_option_tests] {
623         if { [mips_have_test_option_p status $option] } {
624             regsub -all "\n" $code "\\n\\\n" asm
625             # Use check_runtime from target-supports.exp, which caches
626             # the result for us.
627             if { ![check_runtime mips_option_$option [subst {
628                 __attribute__((nomips16)) int
629                 main (void)
630                 {
631                     asm (".set push\
632                     $asm\
633                     .set pop");
634                     return 0;
635                 }
636             }]] } {
637                 return $option
638             }
639         }
640     }
641     return ""
642 }
643
644 # Initialize this testsuite for a new test variant.
645 proc mips-dg-init {} {
646     # Invariant information.
647     global mips_option_groups
648
649     # Internally-generated information about this run.
650     global mips_base_options
651     global mips_extra_options
652
653     # Override dg-options with our mips-dg-options routine.
654     rename dg-options mips-old-dg-options
655     rename mips-dg-options dg-options
656
657     # Start with a fresh option status.
658     array unset mips_base_options
659     foreach { group regexp } $mips_option_groups {
660         set mips_base_options(option,$group) ""
661         set mips_base_options(explicit_p,$group) 0
662         set mips_base_options(test_option_p,$group) 0
663     }
664
665     # Use preprocessor macros to work out as many implicit options as we can.
666     set output [mips_preprocess "" {
667         const char *options[] = {
668             #if !defined _MIPS_SIM
669             "-mabi=eabi",
670             #elif _MIPS_SIM==_ABIO32
671             "-mabi=32",
672             #elif _MIPS_SIM==_ABIO64
673             "-mabi=o64",
674             #elif _MIPS_SIM==_ABIN32
675             "-mabi=n32",
676             #else
677             "-mabi=64",
678             #endif
679
680             "-march=" _MIPS_ARCH,
681
682             #ifdef _MIPSEB
683             "-EB",
684             #else
685             "-EL",
686             #endif
687
688             #ifdef __mips_hard_float
689             "-mhard-float",
690             #else
691             "-msoft-float",
692             #endif
693
694             #if __mips_fpr == 64
695             "-mfp64",
696             #else
697             "-mfp32",
698             #endif
699
700             #ifdef __mips64
701             "-mgp64",
702             #else
703             "-mgp32",
704             #endif
705
706             #if _MIPS_SZLONG == 64
707             "-mlong64",
708             #else
709             "-mlong32",
710             #endif
711
712             #ifdef __mips16
713             "-mips16",
714             #else
715             "-mno-mips16",
716             #endif
717
718             #ifdef __mips3d
719             "-mips3d",
720             #else
721             "-mno-mips3d",
722             #endif
723
724             #ifdef __mips_paired_single_float
725             "-mpaired-single",
726             #else
727             "-mno-paired-single",
728             #endif
729
730             #if __mips_abicalls
731             "-mabicalls",
732             #else
733             "-mno-abicalls",
734             #endif
735
736             #if __mips_dsp_rev >= 2
737             "-mdspr2",
738             #else
739             "-mno-dspr2",
740             #endif
741
742             #if __mips_dsp_rev >= 1
743             "-mdsp",
744             #else
745             "-mno-dsp",
746             #endif
747
748             #ifndef __PIC__
749             "addressing=absolute",
750             #endif
751
752             #ifdef __mips_smartmips
753             "-msmartmips",
754             #else
755             "-mno-smartmips",
756             #endif
757
758             0
759         };
760     }]
761     foreach line [split $output "\r\n"] {
762         # Poor man's string concatenation.
763         regsub -all {" "} $line "" line
764         if { [regexp {"(.*)",} $line dummy option] } {
765             set group [mips_option_group $option]
766             set mips_base_options(option,$group) $option
767         }
768     }
769
770     # Process the target's multilib options, saving any unrecognized
771     # ones in mips_extra_options.
772     set mips_extra_options {}
773     foreach option [split [board_info target multilib_flags]] {
774         set group [mips_option_maybe_group $option]
775         if { ![string equal $group ""] } {
776             set mips_base_options(option,$group) $option
777             set mips_base_options(explicit_p,$group) 1
778         } else {
779             lappend mips_extra_options $option
780         }
781     }
782 }
783
784 # Finish a test run started by mips-dg-init.
785 proc mips-dg-finish {} {
786     rename dg-options mips-dg-options
787     rename mips-old-dg-options dg-options
788 }
789
790 # Override dg-options so that we can do some MIPS-specific processing.
791 # All options used in this testsuite must appear in mips_option_groups.
792 #
793 # Test options override multilib options.  Certain test options can
794 # also imply other test options, which also override multilib options.
795 # These dependencies are ordered as follows:
796 #
797 #          START                        END
798 #            |                           |
799 #         -mips16/-mflip-mips16       -mno-mips16
800 #            |                           |
801 #         -mips3d                     -mno-mips3d
802 #            |                           |
803 #         -mpaired-single             -mno-paired-single
804 #            |                           |
805 #         -mfp64                      -mfp32
806 #            |                           |
807 #         -mhard-float                -msoft-float
808 #            |                           |
809 #         -mno-sym32                  -msym32
810 #            |                           |
811 #         -mrelax-pic-calls           -mno-relax-pic-calls
812 #            |                           |
813 #         -fpic                       -fno-pic
814 #            |                           |
815 #         -mshared                    -mno-shared
816 #            |                           |
817 #         -mno-plt                    -mplt
818 #            |                           |
819 #         addressing=unknown          addressing=absolute
820 #            |                           |
821 #         -mabicalls                  -mno-abicalls
822 #            |                           |
823 #         -G0                         <other value>
824 #            |                           |
825 #         <other value>               -mr10k-cache-barrier=none
826 #            |                           |
827 #         -mfix-r10000                -mno-fix-r10000
828 #            |                           |
829 #         -mbranch-likely             -mno-branch-likely
830 #            |                           |
831 #         -msmartmips                 -mno-smartmips
832 #            |                           |
833 #         -mno-gpopt                  -mgpopt
834 #            |                           |
835 #         -mexplicit-relocs           -mno-explicit-relocs
836 #            |                           |
837 #         -mdspr2                     -mno-dspr2
838 #            |                           |
839 #         -mdsp                       -mno-dsp
840 #            |                           |
841 #            +-- gp, abi & arch ---------+
842 #
843 # For these purposes, the "gp", "abi" & "arch" option groups are treated
844 # as a single node.
845 proc mips-dg-options { args } {
846     # dg.exp variables.
847     upvar dg-extra-tool-flags extra_tool_flags
848     upvar dg-do-what do_what
849
850     # Invariant information.
851     global mips_option_groups
852     global mips_abi_groups
853
854     # Information about this run.
855     global mips_base_options
856
857     # Start out with the default option state.
858     array set options [array get mips_base_options]
859
860     # Record the options that this test explicitly needs.
861     foreach option [lindex $args 1] {
862         set all_but_p [regexp {^\((.*)\)$} $option dummy option]
863         set group [mips_option_group $option]
864         if { [mips_test_option_p options $group] } {
865             set old [mips_option options $group]
866             error "Inconsistent $group option: $old vs. $option"
867         } else {
868             mips_make_test_option options $option $all_but_p
869         }
870     }
871
872     # Handle dependencies between options on the left of the
873     # dependency diagram.
874     mips_option_dependency options "-mips3d" "-mpaired-single"
875     mips_option_dependency options "-mpaired-single" "-mfp64"
876     mips_option_dependency options "-mfp64" "-mhard-float"
877     mips_option_dependency options "-mrelax-pic-calls" "-mno-plt"
878     mips_option_dependency options "-mrelax-pic-calls" "-mabicalls"
879     mips_option_dependency options "-mrelax-pic-calls" "-mexplicit-relocs"
880     mips_option_dependency options "-fpic" "-mshared"
881     mips_option_dependency options "-mshared" "-mno-plt"
882     mips_option_dependency options "-mshared" "-mabicalls"
883     mips_option_dependency options "-mno-plt" "addressing=unknown"
884     mips_option_dependency options "-mabicalls" "-G0"
885     mips_option_dependency options "-mno-gpopt" "-mexplicit-relocs"
886
887     # Work out information about the current ABI.
888     set abi_test_option_p [mips_test_option_p options abi]
889     set abi [mips_option options abi]
890     set eabi_p [mips_same_option_p $abi "-mabi=eabi"]
891
892     # If the test forces a particular ABI, set the register size
893     # accordingly.
894     if { $abi_test_option_p } {
895         if { [mips_32bit_abi_p $abi] } {
896             mips_make_test_option options "-mgp32"
897         } elseif { [mips_64bit_abi_p $abi] } {
898             mips_make_test_option options "-mgp64"
899         }
900     }
901
902     # See whether forbid_cpu forces us to choose a new architecture.
903     set arch [mips_option mips_base_options arch]
904     set force_generic_isa_p [expr {
905         [regexp "forbid_cpu=(.*)" [mips_option options forbid_cpu] dummy spec]
906         && [regexp -- "^-march=$spec\$" $arch]
907     }]
908
909     # Interpret the special "isa" and "isa_rev" options.  If we have
910     # a choice of a 32-bit or a 64-bit architecture, prefer to keep
911     # the -mgp setting the same.
912     set spec [mips_option options arch]
913     if { [regexp {^[^-]} $spec] } {
914         if { [string equal $spec "isa=loongson"] } {
915             if { ![regexp {^-march=loongson} $arch] } {
916                 set arch "-march=loongson2f"
917             }
918         } else {
919             if { ![regexp {^(isa(?:|_rev))(=|<=|>=)([0-9]*)$} \
920                        $spec dummy prop relation value nocpus] } {
921                 error "Unrecognized isa specification: $spec"
922             }
923             set current [mips_arch_info $arch $prop]
924             if { $force_generic_isa_p
925                  || ($current < $value && ![string equal $relation "<="])
926                  || ($current > $value && ![string equal $relation ">="])
927                  || ([mips_have_test_option_p options "-mgp64"]
928                      && [mips_32bit_arch_p $arch]) } {
929                 # The current setting is out of range; it cannot
930                 # possibly be used.  Find a replacement that can.
931                 if { [string equal $prop "isa"] } {
932                     set arch "-mips$value"
933                 } elseif { $value == 0 } {
934                     set arch "-mips4"
935                 } else {
936                     if { [mips_have_option_p options "-mgp32"] } {
937                         set arch "-mips32"
938                     } else {
939                         set arch "-mips64"
940                     }
941                     if { $value > 1 } {
942                         append arch "r$value"
943                     }
944                 }
945             }
946         }
947         set options(option,arch) $arch
948     }
949
950     # Work out information about the current architecture.
951     set arch_test_option_p [mips_test_option_p options arch]
952     set arch [mips_option options arch]
953     set isa [mips_arch_info $arch isa]
954     set isa_rev [mips_arch_info $arch isa_rev]
955
956     # If the test forces a 32-bit architecture, force -mgp32.
957     # Force the current -mgp setting otherwise; if we don't,
958     # some configurations would make a 64-bit architecture
959     # imply -mgp64.
960     if { $arch_test_option_p } {
961         if { [mips_32bit_arch_p $arch] } {
962             mips_make_test_option options "-mgp32"
963         } else {
964             mips_make_test_option options [mips_option options gp]
965         }
966     }
967
968     # We've now fixed the GP register size.  Make it easily available.
969     set gp_size [expr { [mips_have_option_p options "-mgp32"] ? 32 : 64 }]
970
971     # Handle dependencies between the pre-arch options and the arch option.
972     # This should mirror the arch and post-arch code below.
973     if { !$arch_test_option_p } {
974         # We need a revision 2 or better ISA for:
975         #
976         #   - the combination of -mgp32 -mfp64
977         #   - the DSP ASE
978         if { $isa_rev < 2
979              && (($gp_size == 32 && [mips_have_test_option_p options "-mfp64"])
980                  || [mips_have_test_option_p options "-mdsp"]
981                  || [mips_have_test_option_p options "-mdspr2"]) } {
982             if { $gp_size == 32 } {
983                 mips_make_test_option options "-mips32r2"
984             } else {
985                 mips_make_test_option options "-mips64r2"
986             }
987         # We need a MIPS32 or MIPS64 ISA for:
988         #
989         #   - paired-single instructions(*)
990         #
991         # (*) Note that we don't support MIPS V at the moment.
992         } elseif { $isa_rev < 1
993                    && [mips_have_test_option_p options "-mpaired-single"] } {
994             if { $gp_size == 32 } {
995                 mips_make_test_option options "-mips32"
996             } else {
997                 mips_make_test_option options "-mips64"
998             }
999         # We need MIPS III or higher for:
1000         #
1001         #   - the "cache" instruction
1002         } elseif { $isa < 3
1003                    && ([mips_have_test_option_p options \
1004                             "-mr10k-cache-barrier=load-store"]
1005                        || [mips_have_test_option_p options \
1006                                "-mr10k-cache-barrier=store"]) } {
1007             mips_make_test_option options "-mips3"
1008         # We need MIPS II or higher for:
1009         #
1010         #   - branch-likely instructions(*)
1011         #
1012         # (*) needed by both -mbranch-likely and -mfix-r10000
1013         } elseif { $isa < 2
1014                    && ([mips_have_test_option_p options "-mbranch-likely"]
1015                        || [mips_have_test_option_p options "-mfix-r10000"]) } {
1016             mips_make_test_option options "-mips2"
1017         # Check whether we need to switch from a 32-bit processor to the
1018         # "nearest" 64-bit processor.
1019         } elseif { $gp_size == 64 && [mips_32bit_arch_p $arch] } {
1020             if { $isa_rev == 0 } {
1021                 mips_make_test_option options "-mips3"
1022             } elseif { $isa_rev == 1 } {
1023                 mips_make_test_option options "-mips64"
1024             } else {
1025                 mips_make_test_option options "-mips64r$isa_rev"
1026             }
1027         # Otherwise, if the current choice of architecture is unacceptable,
1028         # choose the equivalent generic architecture.
1029         } elseif { $force_generic_isa_p } {
1030             set arch "-mips[mips_arch_info $arch isa]"
1031             if { $isa_rev > 1 } {
1032                 append arch "r$isa_rev"
1033             }
1034             mips_make_test_option options $arch
1035         }
1036         unset arch
1037         unset isa
1038         unset isa_rev
1039     }
1040
1041     # Set an appropriate ABI, handling dependencies between the pre-abi
1042     # options and the abi options.  This should mirror the abi and post-abi
1043     # code below.
1044     if { !$abi_test_option_p } {
1045         if { ($eabi_p
1046               && ([mips_have_option_p options "-mabicalls"]
1047                   || ($gp_size == 32
1048                       && [mips_have_option_p options "-mfp64"]))) } {
1049             # EABI doesn't support -mabicalls.
1050             # EABI doesn't support the combination -mgp32 -mfp64.
1051             set force_abi 1
1052         } elseif { [mips_using_mips16_p options]
1053                    && ![mips_same_option_p $abi "-mabi=32"]
1054                    && ![mips_same_option_p $abi "-mabi=o64"]
1055                    && (![mips_have_option_p options "addressing=absolute"]
1056                        || [mips_have_option_p options "-mhard-float"]) } {
1057             # -mips16 -mhard-float requires o32 or o64.
1058             # -mips16 PIC requires o32 or o64.
1059             set force_abi 1
1060         } elseif { [mips_have_test_option_p options "-mlong32"]
1061                    && [mips_long64_abi_p $abi] } {
1062             set force_abi 1
1063         } elseif { [mips_have_test_option_p options "-mlong64"]
1064                    && [mips_long32_abi_p $abi] } {
1065             set force_abi 1
1066         } else {
1067             set force_abi 0
1068         }
1069         if { $gp_size == 32 } {
1070             if { $force_abi || [mips_64bit_abi_p $abi] } {
1071                 if { [mips_have_test_option_p options "-mlong64"] } {
1072                     mips_make_test_option options "-mabi=eabi"
1073                     mips_make_test_option options "-mgp32"
1074                 } else {
1075                     mips_make_test_option options "-mabi=32"
1076                 }
1077             }
1078         } else {
1079             if { $force_abi || [mips_32bit_abi_p $abi] } {
1080                 if { [mips_have_test_option_p options "-mlong64"] } {
1081                     mips_make_test_option options "-mabi=eabi"
1082                     mips_make_test_option options "-mgp64"
1083                 } else {
1084                     # All configurations should have an assembler that
1085                     # supports o64, since it requires the same BFD target
1086                     # vector as o32.  In contrast, many assembler
1087                     # configurations do not have n32 or n64 support.
1088                     mips_make_test_option options "-mabi=o64"
1089                 }
1090             }
1091         }
1092         set abi_test_option_p [mips_test_option_p options abi]
1093         set abi [mips_option options abi]
1094         set eabi_p [mips_same_option_p $abi "-mabi=eabi"]
1095     }
1096
1097     # Handle dependencies between the abi options and the post-abi options.
1098     # This should mirror the abi and pre-abi code above.
1099     if { $abi_test_option_p } {
1100         if { $eabi_p } {
1101             mips_make_test_option options "-mno-abicalls"
1102             if { $gp_size == 32 } {
1103                 mips_make_test_option options "-mfp32"
1104             }
1105         }
1106         if { [mips_using_mips16_p options]
1107              && ![mips_same_option_p $abi "-mabi=32"]
1108              && ![mips_same_option_p $abi "-mabi=o64"]
1109              && (![mips_have_option_p options "addressing=absolute"]
1110                  || [mips_have_option_p options "-mhard-float"]) } {
1111             if { [mips_test_option_p options mips16] } {
1112                 mips_make_test_option options "addressing=absolute"
1113                 mips_make_test_option options "-msoft-float"
1114             } else {
1115                 mips_make_test_option options "-mno-mips16"
1116             }
1117         }
1118         if { [mips_long32_abi_p $abi] } {
1119             mips_make_test_option options "-mlong32"
1120         } elseif { [mips_long64_abi_p $abi] } {
1121             mips_make_test_option options "-mlong64"
1122         }
1123     }
1124
1125     # Handle dependencies between the arch option and the post-arch options.
1126     # This should mirror the arch and pre-arch code above.
1127     if { $arch_test_option_p } {
1128         if { $isa < 2 } {
1129             mips_make_test_option options "-mno-branch-likely"
1130             mips_make_test_option options "-mno-fix-r10000"
1131         }
1132         if { $isa < 3 } {
1133             mips_make_test_option options "-mr10k-cache-barrier=none"
1134         }
1135         if { $isa_rev < 1 } {
1136             mips_make_test_option options "-mno-paired-single"
1137         }
1138         if { $isa_rev < 2 } {
1139             if { $gp_size == 32 } {
1140                 mips_make_test_option options "-mfp32"
1141             }
1142             mips_make_test_option options "-mno-dsp"
1143         }
1144         unset arch
1145         unset isa
1146         unset isa_rev
1147     }
1148
1149     # Handle dependencies between options on the right of the diagram.
1150     mips_option_dependency options "-mno-dsp" "-mno-dspr2"
1151     mips_option_dependency options "-mno-explicit-relocs" "-mgpopt"
1152     switch -- [mips_test_option options small-data] {
1153         "" -
1154         -G0 {}
1155         default {
1156             mips_make_test_option options "-mno-abicalls"
1157         }
1158     }
1159     if { [mips_have_option_p options "-mabicalls"] } {
1160         mips_option_dependency options "addressing=absolute" "-mplt"
1161     }
1162     mips_option_dependency options "-mplt" "-msym32"
1163     mips_option_dependency options "-mplt" "-mno-shared"
1164     mips_option_dependency options "-mno-shared" "-fno-pic"
1165     mips_option_dependency options "-mfp32" "-mno-paired-single"
1166     mips_option_dependency options "-msoft-float" "-mno-paired-single"
1167     mips_option_dependency options "-mno-paired-single" "-mno-mips3d"
1168
1169     # If the test requires an unsupported option, change run tests
1170     # to link tests.
1171
1172     switch -- [lindex $do_what 0] {
1173         run {
1174             set option [mips_first_unsupported_option options]
1175             if { ![string equal $option ""] } {
1176                 set do_what [lreplace $do_what 0 0 link]
1177                 verbose -log "Downgraded to a 'link' test due to unsupported option '$option'"
1178             }
1179         }
1180     }
1181
1182     # If the test has overridden a option that changes the ABI,
1183     # downgrade a link or execution test to an assembler test.
1184     foreach group $mips_abi_groups {
1185         set old_option [mips_original_option $group]
1186         set new_option [mips_option options $group]
1187         if { ![mips_same_option_p $old_option $new_option] } {
1188             switch -- [lindex $do_what 0] {
1189                 link -
1190                 run {
1191                     set do_what [lreplace $do_what 0 0 assemble]
1192                     verbose -log "Downgraded to an 'assemble' test due to incompatible $group option ($old_option changed to $new_option)"
1193                 }
1194             }
1195             break
1196         }
1197     }
1198
1199     # Add all options to the dg variable.
1200     set options(explicit_p,addressing) 0
1201     set options(explicit_p,forbid_cpu) 0
1202     foreach { group regexp } $mips_option_groups {
1203         if { $options(explicit_p,$group) } {
1204             append extra_tool_flags " " $options(option,$group)
1205         }
1206     }
1207
1208     # If the test is MIPS16-compatible, provide a counterpart to the
1209     # NOMIPS16 convenience macro.
1210     if { [mips_have_test_option_p options "-mips16"] } {
1211         append extra_tool_flags " -DMIPS16=__attribute__((mips16))"
1212     }
1213
1214     # Use our version of gcc-dg-test for this test.
1215     if { ![string equal [info procs "mips-gcc-dg-test"] ""] } {
1216         rename gcc-dg-test mips-old-gcc-dg-test
1217         rename mips-gcc-dg-test gcc-dg-test
1218     }
1219 }
1220
1221 # A version of gcc-dg-test that is used by dg-options tests.
1222 proc mips-gcc-dg-test { prog do_what extra_tool_flags } {
1223     global board_info
1224     global mips_extra_options
1225
1226     # Override the user's chosen test options with the combined test/user
1227     # version.
1228     mips_push_test_options saved_options $mips_extra_options
1229     set result [gcc-dg-test-1 gcc_target_compile $prog \
1230                     $do_what $extra_tool_flags]
1231     mips_pop_test_options saved_options
1232
1233     # Restore the usual gcc-dg-test.
1234     rename gcc-dg-test mips-gcc-dg-test
1235     rename mips-old-gcc-dg-test gcc-dg-test
1236
1237     return $result
1238 }
1239
1240 dg-init
1241 mips-dg-init
1242 # MIPS16 is defined by "-mips16" or "(-mips16)" in dg-options.
1243 dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] "" \
1244     "-DNOMIPS16=__attribute__((nomips16))"
1245 mips-dg-finish
1246 dg-finish