OSDN Git Service

fd065ec9469c4760508b8efc39caefd2eebd959e
[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     dump "-fdump-.*"
245 }
246
247 # Add -mfoo/-mno-foo options to mips_option_groups.
248 foreach option {
249     abicalls
250     branch-likely
251     dsp
252     dspr2
253     explicit-relocs
254     extern-sdata
255     fix-r4000
256     fix-r10000
257     fix-vr4130
258     gpopt
259     local-sdata
260     long-calls
261     paired-single
262     plt
263     shared
264     smartmips
265     sym32
266     synci
267     relax-pic-calls
268     mcount-ra-address
269 } {
270     lappend mips_option_groups $option "-m(no-|)$option"
271 }
272
273 # Add -mfoo= options to mips_option_groups.
274 foreach option {
275     branch-cost
276     code-readable
277     r10k-cache-barrier
278     tune
279 } {
280     lappend mips_option_groups $option "-m$option=.*"
281 }
282
283 # Add -ffoo/-fno-foo options to mips_option_groups.
284 foreach option {
285     delayed-branch
286     fast-math
287     finite-math-only
288     fixed-hi
289     fixed-lo
290     lax-vector-conversions
291     split-wide-types
292     tree-vectorize
293 } {
294     lappend mips_option_groups $option "-f(no-|)$option"
295 }
296
297 # A list of option groups that have an impact on the ABI.
298 set mips_abi_groups {
299     abi
300     abicalls
301     arch
302     endianness
303     float
304     fp
305     gp
306     gpopt
307     long
308     pic
309     small-data
310 }
311
312 # mips_option_tests(OPTION) is some assembly code that will run to completion
313 # on a target that supports OPTION.
314 set mips_option_tests(-mips16) {
315     move $2,$31
316     bal 1f
317     .set mips16
318     jr $31
319     .set nomips16
320     .align 2
321 1:
322     ori $3,$31,1
323     jalr $3
324     move $31,$2
325 }
326 set mips_option_tests(-mpaired-single) {
327     .set mips64
328     lui $2,0x3f80
329     mtc1 $2,$f0
330     cvt.ps.s $f2,$f0,$f0
331 }
332 set mips_option_tests(-mips3d) {
333     .set mips64
334     .set mips3d
335     lui $2,0x3f80
336     mtc1 $2,$f0
337     cvt.ps.s $f2,$f0,$f0
338     mulr.ps $f2,$f2,$f2
339     rsqrt1.s $f2,$f0
340     mul.s $f4,$f2,$f0
341     rsqrt2.s $f4,$f4,$f2
342     madd.s $f4,$f2,$f2,$f4
343 }
344 set mips_option_tests(-mdsp) {
345     .set mips64r2
346     .set dsp
347     addsc $2,$2,$2
348 }
349 set mips_option_tests(-mdspr2) {
350     .set mips64r2
351     .set dspr2
352     prepend $2,$3,11
353 }
354
355 # Canonicalize command-line option OPTION.
356 proc mips_canonicalize_option { option } {
357     regsub {^-mips([1-5]|32*|64*)$} $option {-march=mips\1} option
358
359     regsub {^-mel$} $option {-EL} option
360     regsub {^-meb$} $option {-EB} option
361
362     regsub {^-O$} $option {-O1} option
363
364     # MIPS doesn't use -fpic and -fPIC to distinguish between code models.
365     regsub {^-f(no-|)PIC} $option {-f\1pic} option
366
367     return $option
368 }
369
370 # Return true if OPTION1 and OPTION2 represent the same command-line option.
371 proc mips_same_option_p { option1 option2 } {
372     return [string equal \
373                 [mips_canonicalize_option $option1] \
374                 [mips_canonicalize_option $option2]]
375 }
376
377 # Preprocess CODE using target_compile options OPTIONS.  Return the
378 # compiler output.
379 proc mips_preprocess { options code } {
380     global tool
381
382     set src dummy[pid].c
383     set f [open $src "w"]
384     puts $f $code
385     close $f
386     set output [${tool}_target_compile $src "" preprocess $options]
387     file delete $src
388
389     return $output
390 }
391
392 # Set the target board's command-line options to NEW_OPTIONS, storing the
393 # old values in UPVAR.
394 proc mips_push_test_options { upvar new_options } {
395     upvar $upvar var
396     global board_info
397
398     array unset var
399     set var(name) board_info([target_info name],multilib_flags)
400     if { [info exists $var(name)] } {
401         set var(old_options) [set $var(name)]
402         set $var(name) [join $new_options " "]
403     }
404 }
405
406 # Undo the effects of [mips_push_test_options UPVAR ...]
407 proc mips_pop_test_options { upvar } {
408     upvar $upvar var
409     global board_info
410
411     if { [info exists var(old_options)] } {
412         set $var(name) $var(old_options)
413     }
414 }
415
416 # Return property PROP for architecture option ARCH (which belongs to
417 # the "arch" group in mips_option_groups).  See the comment at the
418 # top of the file for the valid property names.
419 #
420 # Cache the results in mips_arch_info (which can be reused between test
421 # variants).
422 proc mips_arch_info { arch prop } {
423     global mips_arch_info
424     global board_info
425
426     set arch [mips_canonicalize_option $arch]
427     if { ![info exists mips_arch_info($arch,$prop)] } {
428         mips_push_test_options saved_options {}
429         set output [mips_preprocess [list "additional_flags=$arch -mabi=32"] {
430             int isa = __mips;
431             #ifdef __mips_isa_rev
432             int isa_rev = __mips_isa_rev;
433             #else
434             int isa_rev = 0;
435             #endif
436         }]
437         foreach lhs { isa isa_rev } {
438             regsub ".*$lhs = (\[^;\]*).*" $output {\1} rhs
439             verbose -log "Architecture $arch has $lhs $rhs"
440             set mips_arch_info($arch,$lhs) $rhs
441         }
442         mips_pop_test_options saved_options
443     }
444     return $mips_arch_info($arch,$prop)
445 }
446
447 # Return the option group associated with OPTION, or "" if none.
448 proc mips_option_maybe_group { option } {
449     global mips_option_groups
450
451     foreach { group regexp } $mips_option_groups {
452         if { [regexp -- "^($regexp)\$" $option] } {
453             return $group
454         }
455     }
456     return ""
457 }
458
459 # Return the option group associated with OPTION.  Raise an error if
460 # there is none.
461 proc mips_option_group { option } {
462     set group [mips_option_maybe_group $option]
463     if { [string equal $group ""] } {
464         error "Unrecognised option: $option"
465     }
466     return $group
467 }
468
469 # Return the option for option group GROUP, or "" if no option in that
470 # group has been chosen.  UPSTATUS describes the option status.
471 proc mips_option { upstatus group } {
472     upvar $upstatus status
473
474     return $status(option,$group)
475 }
476
477 # If the base options for this test run include an option in group GROUP,
478 # return that option, otherwise return "".
479 proc mips_original_option { group } {
480     global mips_base_options
481
482     return [mips_option mips_base_options $group]
483 }
484
485 # Return true if the test described by UPSTATUS requires a specific
486 # option in group GROUP.  UPSTATUS describes the option status.
487 proc mips_test_option_p { upstatus group } {
488     upvar $upstatus status
489
490     return $status(test_option_p,$group)
491 }
492
493 # If the test described by UPSTATUS requires a particular option in group
494 # GROUP, return that option, otherwise return "".
495 proc mips_test_option { upstatus group } {
496     upvar $upstatus status
497
498     if { [mips_test_option_p status $group] } {
499         return [mips_option status $group]
500     } else {
501         return ""
502     }
503 }
504
505 # Return true if the options described by UPSTATUS include OPTION.
506 proc mips_have_option_p { upstatus option } {
507     upvar $upstatus status
508
509     return [mips_same_option_p \
510                 [mips_option status [mips_option_group $option]] \
511                 $option]
512 }
513
514 # Return true if the options described by UPSTATUS require MIPS16 support.
515 proc mips_using_mips16_p { upstatus } {
516     upvar $upstatus status
517
518     return [expr { [mips_have_option_p status "-mips16"]
519                    || [mips_have_option_p status "-mflip-mips16"] }]
520 }
521
522 # Return true if the test described by UPSTATUS requires option OPTION.
523 proc mips_have_test_option_p { upstatus option } {
524     upvar $upstatus status
525
526     set group [mips_option_group $option]
527     return [mips_same_option_p [mips_test_option status $group] $option]
528 }
529
530 # If the test described by UPSTATUS does not specify an option in
531 # OPTION's group, act as though it had specified OPTION.
532 #
533 # The first optional argument indicates whether the option should be
534 # treated as though it were wrapped in parentheses; see the comment at
535 # the top of the file for details about this convention.  The default is 0.
536 proc mips_make_test_option { upstatus option args } {
537     upvar $upstatus status
538
539     set group [mips_option_group $option]
540     if { ![mips_test_option_p status $group] } {
541         set status(option,$group) $option
542         set status(test_option_p,$group) 1
543         if { [llength $args] == 0 || ![lindex $args 0] } {
544             set status(explicit_p,$group) 1
545         }
546     }
547 }
548
549 # If the test described by UPSTATUS requires option FROM, assume that
550 # it implicitly requires option TO.
551 proc mips_option_dependency { upstatus from to } {
552     upvar $upstatus status
553
554     if { [mips_have_test_option_p status $from] } {
555         mips_make_test_option status $to
556     }
557 }
558
559 # Return true if the given arch-group option specifies a 32-bit ISA.
560 proc mips_32bit_arch_p { option } {
561     set isa [mips_arch_info $option isa]
562     return [expr { $isa < 3 || $isa == 32 }]
563 }
564
565 # Return true if the given arch-group option specifies a 64-bit ISA.
566 proc mips_64bit_arch_p { option } {
567     return [expr { ![mips_32bit_arch_p $option] }]
568 }
569
570 # Return true if the given abi-group option implicitly requires -mgp32.
571 proc mips_32bit_abi_p { option } {
572     switch -glob -- $option {
573         -mabi=32 {
574             return 1
575         }
576     }
577     return 0
578 }
579
580 # Return true if the given abi-group option implicitly requires -mgp64.
581 proc mips_64bit_abi_p { option } {
582     switch -glob -- $option {
583         -mabi=o64 -
584         -mabi=n32 -
585         -mabi=64 {
586             return 1
587         }
588     }
589     return 0
590 }
591
592 # Return true if the given abi-group option implicitly requires -mlong32.
593 # o64 requires this for -mabicalls, but not otherwise; pick the conservative
594 # case for simplicity.
595 proc mips_long32_abi_p { option } {
596     switch -glob -- $option {
597         -mabi=o64 -
598         -mabi=n32 -
599         -mabi=32 {
600             return 1
601         }
602     }
603     return 0
604 }
605
606 # Return true if the given abi-group option implicitly requires -mlong64.
607 proc mips_long64_abi_p { option } {
608     switch -glob -- $option {
609         -mabi=64 {
610             return 1
611         }
612     }
613     return 0
614 }
615
616 # Check whether the current target supports all the options that the
617 # current test requires.  Return "" if so, otherwise return one of
618 # the incompatible options.  UPSTATUS describes the option status.
619 proc mips_first_unsupported_option { upstatus } {
620     global mips_option_tests
621     upvar $upstatus status
622
623     foreach { option code } [array get mips_option_tests] {
624         if { [mips_have_test_option_p status $option] } {
625             regsub -all "\n" $code "\\n\\\n" asm
626             # Use check_runtime from target-supports.exp, which caches
627             # the result for us.
628             if { ![check_runtime mips_option_$option [subst {
629                 __attribute__((nomips16)) int
630                 main (void)
631                 {
632                     asm (".set push\
633                     $asm\
634                     .set pop");
635                     return 0;
636                 }
637             }]] } {
638                 return $option
639             }
640         }
641     }
642     return ""
643 }
644
645 # Initialize this testsuite for a new test variant.
646 proc mips-dg-init {} {
647     # Invariant information.
648     global mips_option_groups
649
650     # Internally-generated information about this run.
651     global mips_base_options
652     global mips_extra_options
653
654     # Override dg-options with our mips-dg-options routine.
655     rename dg-options mips-old-dg-options
656     rename mips-dg-options dg-options
657
658     # Start with a fresh option status.
659     array unset mips_base_options
660     foreach { group regexp } $mips_option_groups {
661         set mips_base_options(option,$group) ""
662         set mips_base_options(explicit_p,$group) 0
663         set mips_base_options(test_option_p,$group) 0
664     }
665
666     # Use preprocessor macros to work out as many implicit options as we can.
667     set output [mips_preprocess "" {
668         const char *options[] = {
669             #if !defined _MIPS_SIM
670             "-mabi=eabi",
671             #elif _MIPS_SIM==_ABIO32
672             "-mabi=32",
673             #elif _MIPS_SIM==_ABIO64
674             "-mabi=o64",
675             #elif _MIPS_SIM==_ABIN32
676             "-mabi=n32",
677             #else
678             "-mabi=64",
679             #endif
680
681             "-march=" _MIPS_ARCH,
682
683             #ifdef _MIPSEB
684             "-EB",
685             #else
686             "-EL",
687             #endif
688
689             #ifdef __mips_hard_float
690             "-mhard-float",
691             #else
692             "-msoft-float",
693             #endif
694
695             #if __mips_fpr == 64
696             "-mfp64",
697             #else
698             "-mfp32",
699             #endif
700
701             #ifdef __mips64
702             "-mgp64",
703             #else
704             "-mgp32",
705             #endif
706
707             #if _MIPS_SZLONG == 64
708             "-mlong64",
709             #else
710             "-mlong32",
711             #endif
712
713             #ifdef __mips16
714             "-mips16",
715             #else
716             "-mno-mips16",
717             #endif
718
719             #ifdef __mips3d
720             "-mips3d",
721             #else
722             "-mno-mips3d",
723             #endif
724
725             #ifdef __mips_paired_single_float
726             "-mpaired-single",
727             #else
728             "-mno-paired-single",
729             #endif
730
731             #if __mips_abicalls
732             "-mabicalls",
733             #else
734             "-mno-abicalls",
735             #endif
736
737             #if __mips_dsp_rev >= 2
738             "-mdspr2",
739             #else
740             "-mno-dspr2",
741             #endif
742
743             #if __mips_dsp_rev >= 1
744             "-mdsp",
745             #else
746             "-mno-dsp",
747             #endif
748
749             #ifndef __PIC__
750             "addressing=absolute",
751             #endif
752
753             #ifdef __mips_smartmips
754             "-msmartmips",
755             #else
756             "-mno-smartmips",
757             #endif
758
759             0
760         };
761     }]
762     foreach line [split $output "\r\n"] {
763         # Poor man's string concatenation.
764         regsub -all {" "} $line "" line
765         if { [regexp {"(.*)",} $line dummy option] } {
766             set group [mips_option_group $option]
767             set mips_base_options(option,$group) $option
768         }
769     }
770
771     # Process the target's multilib options, saving any unrecognized
772     # ones in mips_extra_options.
773     set mips_extra_options {}
774     foreach option [split [board_info target multilib_flags]] {
775         set group [mips_option_maybe_group $option]
776         if { ![string equal $group ""] } {
777             set mips_base_options(option,$group) $option
778             set mips_base_options(explicit_p,$group) 1
779         } else {
780             lappend mips_extra_options $option
781         }
782     }
783 }
784
785 # Finish a test run started by mips-dg-init.
786 proc mips-dg-finish {} {
787     rename dg-options mips-dg-options
788     rename mips-old-dg-options dg-options
789 }
790
791 # Override dg-options so that we can do some MIPS-specific processing.
792 # All options used in this testsuite must appear in mips_option_groups.
793 #
794 # Test options override multilib options.  Certain test options can
795 # also imply other test options, which also override multilib options.
796 # These dependencies are ordered as follows:
797 #
798 #          START                        END
799 #            |                           |
800 #         -mips16/-mflip-mips16       -mno-mips16
801 #            |                           |
802 #         -mips3d                     -mno-mips3d
803 #            |                           |
804 #         -mpaired-single             -mno-paired-single
805 #            |                           |
806 #         -mfp64                      -mfp32
807 #            |                           |
808 #         -mhard-float                -msoft-float
809 #            |                           |
810 #         -mno-sym32                  -msym32
811 #            |                           |
812 #         -mrelax-pic-calls           -mno-relax-pic-calls
813 #            |                           |
814 #         -fpic                       -fno-pic
815 #            |                           |
816 #         -mshared                    -mno-shared
817 #            |                           |
818 #         -mno-plt                    -mplt
819 #            |                           |
820 #         addressing=unknown          addressing=absolute
821 #            |                           |
822 #         -mabicalls                  -mno-abicalls
823 #            |                           |
824 #         -G0                         <other value>
825 #            |                           |
826 #         <other value>               -mr10k-cache-barrier=none
827 #            |                           |
828 #         -mfix-r10000                -mno-fix-r10000
829 #            |                           |
830 #         -mbranch-likely             -mno-branch-likely
831 #            |                           |
832 #         -msmartmips                 -mno-smartmips
833 #            |                           |
834 #         -mno-gpopt                  -mgpopt
835 #            |                           |
836 #         -mexplicit-relocs           -mno-explicit-relocs
837 #            |                           |
838 #         -mdspr2                     -mno-dspr2
839 #            |                           |
840 #         -mdsp                       -mno-dsp
841 #            |                           |
842 #            +-- gp, abi & arch ---------+
843 #
844 # For these purposes, the "gp", "abi" & "arch" option groups are treated
845 # as a single node.
846 proc mips-dg-options { args } {
847     # dg.exp variables.
848     upvar dg-extra-tool-flags extra_tool_flags
849     upvar dg-do-what do_what
850
851     # Invariant information.
852     global mips_option_groups
853     global mips_abi_groups
854
855     # Information about this run.
856     global mips_base_options
857
858     # Start out with the default option state.
859     array set options [array get mips_base_options]
860
861     # Record the options that this test explicitly needs.
862     foreach option [lindex $args 1] {
863         set all_but_p [regexp {^\((.*)\)$} $option dummy option]
864         set group [mips_option_group $option]
865         if { [mips_test_option_p options $group] } {
866             set old [mips_option options $group]
867             error "Inconsistent $group option: $old vs. $option"
868         } else {
869             mips_make_test_option options $option $all_but_p
870         }
871     }
872
873     # Handle dependencies between options on the left of the
874     # dependency diagram.
875     mips_option_dependency options "-mips3d" "-mpaired-single"
876     mips_option_dependency options "-mpaired-single" "-mfp64"
877     mips_option_dependency options "-mfp64" "-mhard-float"
878     mips_option_dependency options "-mrelax-pic-calls" "-mno-plt"
879     mips_option_dependency options "-mrelax-pic-calls" "-mabicalls"
880     mips_option_dependency options "-mrelax-pic-calls" "-mexplicit-relocs"
881     mips_option_dependency options "-fpic" "-mshared"
882     mips_option_dependency options "-mshared" "-mno-plt"
883     mips_option_dependency options "-mshared" "-mabicalls"
884     mips_option_dependency options "-mno-plt" "addressing=unknown"
885     mips_option_dependency options "-mabicalls" "-G0"
886     mips_option_dependency options "-mno-gpopt" "-mexplicit-relocs"
887
888     # Work out information about the current ABI.
889     set abi_test_option_p [mips_test_option_p options abi]
890     set abi [mips_option options abi]
891     set eabi_p [mips_same_option_p $abi "-mabi=eabi"]
892
893     # If the test forces a particular ABI, set the register size
894     # accordingly.
895     if { $abi_test_option_p } {
896         if { [mips_32bit_abi_p $abi] } {
897             mips_make_test_option options "-mgp32"
898         } elseif { [mips_64bit_abi_p $abi] } {
899             mips_make_test_option options "-mgp64"
900         }
901     }
902
903     # See whether forbid_cpu forces us to choose a new architecture.
904     set arch [mips_option mips_base_options arch]
905     set force_generic_isa_p [expr {
906         [regexp "forbid_cpu=(.*)" [mips_option options forbid_cpu] dummy spec]
907         && [regexp -- "^-march=$spec\$" $arch]
908     }]
909
910     # Interpret the special "isa" and "isa_rev" options.  If we have
911     # a choice of a 32-bit or a 64-bit architecture, prefer to keep
912     # the -mgp setting the same.
913     set spec [mips_option options arch]
914     if { [regexp {^[^-]} $spec] } {
915         if { [string equal $spec "isa=loongson"] } {
916             if { ![regexp {^-march=loongson} $arch] } {
917                 set arch "-march=loongson2f"
918             }
919         } else {
920             if { ![regexp {^(isa(?:|_rev))(=|<=|>=)([0-9]*)$} \
921                        $spec dummy prop relation value nocpus] } {
922                 error "Unrecognized isa specification: $spec"
923             }
924             set current [mips_arch_info $arch $prop]
925             if { $force_generic_isa_p
926                  || ($current < $value && ![string equal $relation "<="])
927                  || ($current > $value && ![string equal $relation ">="])
928                  || ([mips_have_test_option_p options "-mgp64"]
929                      && [mips_32bit_arch_p $arch]) } {
930                 # The current setting is out of range; it cannot
931                 # possibly be used.  Find a replacement that can.
932                 if { [string equal $prop "isa"] } {
933                     set arch "-mips$value"
934                 } elseif { $value == 0 } {
935                     set arch "-mips4"
936                 } else {
937                     if { [mips_have_option_p options "-mgp32"] } {
938                         set arch "-mips32"
939                     } else {
940                         set arch "-mips64"
941                     }
942                     if { $value > 1 } {
943                         append arch "r$value"
944                     }
945                 }
946             }
947         }
948         set options(option,arch) $arch
949     }
950
951     # Work out information about the current architecture.
952     set arch_test_option_p [mips_test_option_p options arch]
953     set arch [mips_option options arch]
954     set isa [mips_arch_info $arch isa]
955     set isa_rev [mips_arch_info $arch isa_rev]
956
957     # If the test forces a 32-bit architecture, force -mgp32.
958     # Force the current -mgp setting otherwise; if we don't,
959     # some configurations would make a 64-bit architecture
960     # imply -mgp64.
961     if { $arch_test_option_p } {
962         if { [mips_32bit_arch_p $arch] } {
963             mips_make_test_option options "-mgp32"
964         } else {
965             mips_make_test_option options [mips_option options gp]
966         }
967     }
968
969     # We've now fixed the GP register size.  Make it easily available.
970     set gp_size [expr { [mips_have_option_p options "-mgp32"] ? 32 : 64 }]
971
972     # Handle dependencies between the pre-arch options and the arch option.
973     # This should mirror the arch and post-arch code below.
974     if { !$arch_test_option_p } {
975         # We need a revision 2 or better ISA for:
976         #
977         #   - the combination of -mgp32 -mfp64
978         #   - the DSP ASE
979         if { $isa_rev < 2
980              && (($gp_size == 32 && [mips_have_test_option_p options "-mfp64"])
981                  || [mips_have_test_option_p options "-mdsp"]
982                  || [mips_have_test_option_p options "-mdspr2"]) } {
983             if { $gp_size == 32 } {
984                 mips_make_test_option options "-mips32r2"
985             } else {
986                 mips_make_test_option options "-mips64r2"
987             }
988         # We need a MIPS32 or MIPS64 ISA for:
989         #
990         #   - paired-single instructions(*)
991         #
992         # (*) Note that we don't support MIPS V at the moment.
993         } elseif { $isa_rev < 1
994                    && [mips_have_test_option_p options "-mpaired-single"] } {
995             if { $gp_size == 32 } {
996                 mips_make_test_option options "-mips32"
997             } else {
998                 mips_make_test_option options "-mips64"
999             }
1000         # We need MIPS III or higher for:
1001         #
1002         #   - the "cache" instruction
1003         } elseif { $isa < 3
1004                    && ([mips_have_test_option_p options \
1005                             "-mr10k-cache-barrier=load-store"]
1006                        || [mips_have_test_option_p options \
1007                                "-mr10k-cache-barrier=store"]) } {
1008             mips_make_test_option options "-mips3"
1009         # We need MIPS II or higher for:
1010         #
1011         #   - branch-likely instructions(*)
1012         #
1013         # (*) needed by both -mbranch-likely and -mfix-r10000
1014         } elseif { $isa < 2
1015                    && ([mips_have_test_option_p options "-mbranch-likely"]
1016                        || [mips_have_test_option_p options "-mfix-r10000"]) } {
1017             mips_make_test_option options "-mips2"
1018         # Check whether we need to switch from a 32-bit processor to the
1019         # "nearest" 64-bit processor.
1020         } elseif { $gp_size == 64 && [mips_32bit_arch_p $arch] } {
1021             if { $isa_rev == 0 } {
1022                 mips_make_test_option options "-mips3"
1023             } elseif { $isa_rev == 1 } {
1024                 mips_make_test_option options "-mips64"
1025             } else {
1026                 mips_make_test_option options "-mips64r$isa_rev"
1027             }
1028         # Otherwise, if the current choice of architecture is unacceptable,
1029         # choose the equivalent generic architecture.
1030         } elseif { $force_generic_isa_p } {
1031             set arch "-mips[mips_arch_info $arch isa]"
1032             if { $isa_rev > 1 } {
1033                 append arch "r$isa_rev"
1034             }
1035             mips_make_test_option options $arch
1036         }
1037         unset arch
1038         unset isa
1039         unset isa_rev
1040     }
1041
1042     # Set an appropriate ABI, handling dependencies between the pre-abi
1043     # options and the abi options.  This should mirror the abi and post-abi
1044     # code below.
1045     if { !$abi_test_option_p } {
1046         if { ($eabi_p
1047               && ([mips_have_option_p options "-mabicalls"]
1048                   || ($gp_size == 32
1049                       && [mips_have_option_p options "-mfp64"]))) } {
1050             # EABI doesn't support -mabicalls.
1051             # EABI doesn't support the combination -mgp32 -mfp64.
1052             set force_abi 1
1053         } elseif { [mips_using_mips16_p options]
1054                    && ![mips_same_option_p $abi "-mabi=32"]
1055                    && ![mips_same_option_p $abi "-mabi=o64"]
1056                    && (![mips_have_option_p options "addressing=absolute"]
1057                        || [mips_have_option_p options "-mhard-float"]) } {
1058             # -mips16 -mhard-float requires o32 or o64.
1059             # -mips16 PIC requires o32 or o64.
1060             set force_abi 1
1061         } elseif { [mips_have_test_option_p options "-mlong32"]
1062                    && [mips_long64_abi_p $abi] } {
1063             set force_abi 1
1064         } elseif { [mips_have_test_option_p options "-mlong64"]
1065                    && [mips_long32_abi_p $abi] } {
1066             set force_abi 1
1067         } else {
1068             set force_abi 0
1069         }
1070         if { $gp_size == 32 } {
1071             if { $force_abi || [mips_64bit_abi_p $abi] } {
1072                 if { [mips_have_test_option_p options "-mlong64"] } {
1073                     mips_make_test_option options "-mabi=eabi"
1074                     mips_make_test_option options "-mgp32"
1075                 } else {
1076                     mips_make_test_option options "-mabi=32"
1077                 }
1078             }
1079         } else {
1080             if { $force_abi || [mips_32bit_abi_p $abi] } {
1081                 if { [mips_have_test_option_p options "-mlong64"] } {
1082                     mips_make_test_option options "-mabi=eabi"
1083                     mips_make_test_option options "-mgp64"
1084                 } else {
1085                     # All configurations should have an assembler that
1086                     # supports o64, since it requires the same BFD target
1087                     # vector as o32.  In contrast, many assembler
1088                     # configurations do not have n32 or n64 support.
1089                     mips_make_test_option options "-mabi=o64"
1090                 }
1091             }
1092         }
1093         set abi_test_option_p [mips_test_option_p options abi]
1094         set abi [mips_option options abi]
1095         set eabi_p [mips_same_option_p $abi "-mabi=eabi"]
1096     }
1097
1098     # Handle dependencies between the abi options and the post-abi options.
1099     # This should mirror the abi and pre-abi code above.
1100     if { $abi_test_option_p } {
1101         if { $eabi_p } {
1102             mips_make_test_option options "-mno-abicalls"
1103             if { $gp_size == 32 } {
1104                 mips_make_test_option options "-mfp32"
1105             }
1106         }
1107         if { [mips_using_mips16_p options]
1108              && ![mips_same_option_p $abi "-mabi=32"]
1109              && ![mips_same_option_p $abi "-mabi=o64"]
1110              && (![mips_have_option_p options "addressing=absolute"]
1111                  || [mips_have_option_p options "-mhard-float"]) } {
1112             if { [mips_test_option_p options mips16] } {
1113                 mips_make_test_option options "addressing=absolute"
1114                 mips_make_test_option options "-msoft-float"
1115             } else {
1116                 mips_make_test_option options "-mno-mips16"
1117             }
1118         }
1119         if { [mips_long32_abi_p $abi] } {
1120             mips_make_test_option options "-mlong32"
1121         } elseif { [mips_long64_abi_p $abi] } {
1122             mips_make_test_option options "-mlong64"
1123         }
1124     }
1125
1126     # Handle dependencies between the arch option and the post-arch options.
1127     # This should mirror the arch and pre-arch code above.
1128     if { $arch_test_option_p } {
1129         if { $isa < 2 } {
1130             mips_make_test_option options "-mno-branch-likely"
1131             mips_make_test_option options "-mno-fix-r10000"
1132         }
1133         if { $isa < 3 } {
1134             mips_make_test_option options "-mr10k-cache-barrier=none"
1135         }
1136         if { $isa_rev < 1 } {
1137             mips_make_test_option options "-mno-paired-single"
1138         }
1139         if { $isa_rev < 2 } {
1140             if { $gp_size == 32 } {
1141                 mips_make_test_option options "-mfp32"
1142             }
1143             mips_make_test_option options "-mno-dsp"
1144         }
1145         unset arch
1146         unset isa
1147         unset isa_rev
1148     }
1149
1150     # Handle dependencies between options on the right of the diagram.
1151     mips_option_dependency options "-mno-dsp" "-mno-dspr2"
1152     mips_option_dependency options "-mno-explicit-relocs" "-mgpopt"
1153     switch -- [mips_test_option options small-data] {
1154         "" -
1155         -G0 {}
1156         default {
1157             mips_make_test_option options "-mno-abicalls"
1158         }
1159     }
1160     if { [mips_have_option_p options "-mabicalls"] } {
1161         mips_option_dependency options "addressing=absolute" "-mplt"
1162     }
1163     mips_option_dependency options "-mplt" "-msym32"
1164     mips_option_dependency options "-mplt" "-mno-shared"
1165     mips_option_dependency options "-mno-shared" "-fno-pic"
1166     mips_option_dependency options "-mfp32" "-mno-paired-single"
1167     mips_option_dependency options "-msoft-float" "-mno-paired-single"
1168     mips_option_dependency options "-mno-paired-single" "-mno-mips3d"
1169
1170     # If the test requires an unsupported option, change run tests
1171     # to link tests.
1172
1173     switch -- [lindex $do_what 0] {
1174         run {
1175             set option [mips_first_unsupported_option options]
1176             if { ![string equal $option ""] } {
1177                 set do_what [lreplace $do_what 0 0 link]
1178                 verbose -log "Downgraded to a 'link' test due to unsupported option '$option'"
1179             }
1180         }
1181     }
1182
1183     # If the test has overridden a option that changes the ABI,
1184     # downgrade a link or execution test to an assembler test.
1185     foreach group $mips_abi_groups {
1186         set old_option [mips_original_option $group]
1187         set new_option [mips_option options $group]
1188         if { ![mips_same_option_p $old_option $new_option] } {
1189             switch -- [lindex $do_what 0] {
1190                 link -
1191                 run {
1192                     set do_what [lreplace $do_what 0 0 assemble]
1193                     verbose -log "Downgraded to an 'assemble' test due to incompatible $group option ($old_option changed to $new_option)"
1194                 }
1195             }
1196             break
1197         }
1198     }
1199
1200     # Add all options to the dg variable.
1201     set options(explicit_p,addressing) 0
1202     set options(explicit_p,forbid_cpu) 0
1203     foreach { group regexp } $mips_option_groups {
1204         if { $options(explicit_p,$group) } {
1205             append extra_tool_flags " " $options(option,$group)
1206         }
1207     }
1208
1209     # If the test is MIPS16-compatible, provide a counterpart to the
1210     # NOMIPS16 convenience macro.
1211     if { [mips_have_test_option_p options "-mips16"] } {
1212         append extra_tool_flags " -DMIPS16=__attribute__((mips16))"
1213     }
1214
1215     # Use our version of gcc-dg-test for this test.
1216     if { ![string equal [info procs "mips-gcc-dg-test"] ""] } {
1217         rename gcc-dg-test mips-old-gcc-dg-test
1218         rename mips-gcc-dg-test gcc-dg-test
1219     }
1220 }
1221
1222 # A version of gcc-dg-test that is used by dg-options tests.
1223 proc mips-gcc-dg-test { prog do_what extra_tool_flags } {
1224     global board_info
1225     global mips_extra_options
1226
1227     # Override the user's chosen test options with the combined test/user
1228     # version.
1229     mips_push_test_options saved_options $mips_extra_options
1230     set result [gcc-dg-test-1 gcc_target_compile $prog \
1231                     $do_what $extra_tool_flags]
1232     mips_pop_test_options saved_options
1233
1234     # Restore the usual gcc-dg-test.
1235     rename gcc-dg-test mips-gcc-dg-test
1236     rename mips-old-gcc-dg-test gcc-dg-test
1237
1238     return $result
1239 }
1240
1241 dg-init
1242 mips-dg-init
1243 # MIPS16 is defined by "-mips16" or "(-mips16)" in dg-options.
1244 dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] "" \
1245     "-DNOMIPS16=__attribute__((nomips16))"
1246 mips-dg-finish
1247 dg-finish