summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.target/mips/mips.exp
blob: 1f0d0d6223f218de9c6ed5ed028f67c6bf53c4da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# Copyright (C) 1997-2013 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

# A MIPS version of the GCC dg.exp driver.
#
# There are many MIPS features that we want to test, and many of those
# features are specific to certain architectures, certain ABIs and so on.
# There are therefore many cases in which we want to test something that
# is incompatible with the user's chosen test options.
#
# In most dg testsuites, the options added by dg-options have a lower
# priority than the options chosen by the user.  For example, if a test
# specifies:
#
#    { dg-options "-mips1" }
#
# and the user passes the following option to runtest:
#
#    --target_board unix/-mips3
#
# the test would be compiled as MIPS III rather than MIPS I.  If the
# test really wouldn't work with -mips3, normal practice would be to
# have something like:
#
#    { dg-do compile { target can_force_mips1 } }
#
# so that the test is skipped when an option like -mips3 is passed.
#
# Sticking to the same approach here would cause us to skip many tests,
# even though the toolchain can generate the required code.  For example,
# there are 6 MIPS ABIs, plus variants.  Some configurations support
# more than one ABI, so it is natural to use something like:
#
#    --target_board unix{-mabi=n32,-mabi=32,-mabi=64}
#
# when testing them.  But these -mabi=* options would normally prevent any
# EABI and o64 tests from running.
#
# This testsuite therefore defines a local version of dg-options that
# overrides any user options that are incompatible with the test options.
# It tries to keep the other user options intact.
#
#
# Most of the tests in this testsuite are scan-assembler tests, but
# sometimes we need a link test instead.  In these cases, we must not
# try to link code with options that are incompatible with the current
# multilib, because xgcc is passed -L and -B options that are specific
# to that multilib.
#
# Normal GCC practice would be to skip incompatible link tests as
# unsupported, but in this particular case, it seems better to downgrade
# them to an assemble test instead.  At least that way we get some
# test-for-ICE and code-sanity coverage.
#
# The same problem applies to run tests.  If a test requires runtime
# support for a particular feature, and if the current target does not
# provide that support, normal practice would be to skip the test.
# But in this case it seems better to downgrade it to a link test instead.
# (We might then have to downgrade it to an assembler test according to
# the constraints just mentioned.)
#
# The local dg-options therefore checks whether the new options are
# link-compatiable with the user's options.  If not, it automatically
# downgrades link tests to assemble tests.  It does the same for run
# tests, but in addition, it downgrades run tests to link tests if the
# target does not provide runtime support for a required feature or ASE.
#
#
# Another problem is that many of the options we want to test require
# certain other features.  For example, -mips3d requires both 64-bit
# FPRs and a MIPS32 or MIPS64 target; -mfix-r10000 requires branch-
# likely instructions; and so on.  We could handle this by specifying
# a set of options that are guaranteed to give us what we want, such as:
#
#   dg-options "-mips3d -mpaired-single -mhard-float -mgp64 -mfp64 -mabi=n32 -march=mips64 -mips64"
#
# With the new dg-options semantics, this would override any troublesome
# user options like -mips3, -march=vr4100, -mfp32, -mgp32, -msoft-float,
# -mno-paired-single and so on.  But there are three major problems with
# this:
#
#   - It is easy to forget options.
#
#   - If a new option is added, all tests that are incompatible with that
#     option must be updated.
#
#   - We want to be able to test MIPS-3D with things like -march=mips32,
#     -march=mips64r2, -march=sb1, and so on.
#
# The local version of dg-options therefore works out the requirements
# of each test option.  As with the test options themselves, the local
# dg-options overrides any user options that incompatible with these
# requirements, but it keeps the other user options the same.
#
# For example, if the user passes -mips3, a MIPS-3D test will choose
# a different architecture like -mips64 instead.  But if the user
# passes -march=sb1, MIPS-3D tests will be run with that option.
#
#
# Sometimes it is useful to say "I want an environment that is compatible
# with option X, but I don't want to pass option X itself".  The main example
# of this is -mips16: we want to be able to test __attribute__((mips16))
# without requiring the test itself to be compiled as -mips16.  The local
# version of dg-options lets you do this by putting X in parentheses.
# For example:
#
#   { dg-options "(-mips16)" }
#
# selects a MIPS16-compatible target without passing -mips16 itself.
#
# It is also useful to say "any architecture within this ISA range is fine".
# This can be done using special pseudo-options of the form:
#
#   PROP=VALUE PROP<=VALUE PROP>=VALUE
#
# where PROP can be:
#
#   isa:
#	the value of the __mips macro.
#
#   isa_rev:
#	the value of the __mips_isa_rev macro, or 0 if it isn't defined.
#
# For example, "isa_rev>=1" selects a MIPS32 or MIPS64 processor,
# "isa=4" selects a MIPS IV processor, and so on.
#
# There are also the following special pseudo-options:
#
#   isa=loongson
#	select a Loongson processor
#
#   addressing=absolute
#	force absolute addresses to be used
#
#   forbid_cpu=REGEXP
#	forbid processors that match the given regexp; choose a
#	generic ISA instead.
#
#
# In summary:
#
#   (1) Try to avoid { target ... } requirements wherever possible.
#       Specify the requirements as dg-options instead.
#
#   (2) Don't worry about the consequences of (1) for link and run tests.
#       If the test uses { dg-do link } or { dg-do run }, and its
#       dg-options are incompatible with the current target, the
#       testsuite will downgrade them where necessary.
#
#   (3) Try to use the bare minimum of options and leave dg-options
#       to work out the dependencies.  For example, if you want
#       a MIPS-3D test, you should generally just specify -mips3d.
#       Don't specify an architecture option like -mips64 unless
#       the test really doesn't work with -mips32r2, -mips64r2,
#       -march=sb1, etc.
#
#   (4) If you want something compatible with a particular option,
#       but don't want to pass the option itself, wrap that option
#       in parentheses.  In particular, pass '(-mips16)' if you
#       want to use "mips16" attributes.
#
#   (5) When testing a feature of a generic ISA (as opposed to a
#	processor-specific extension), try to use the "isa" and
#	"isa_rev" pseudo-options instead of specific architecture
#	options.  For example, if the feature is present on revision 2
#	processors and above, try to use "isa_rev>=2" instead of
#	"-mips32r2" or "-mips64r2".
#
#   (6) If you need to disable processor-specific extensions use
#	forbid_cpu=REGEXP instead of forcing a generic ISA.
#
#
# Terminology
#
#   Option group or just group:
#      See comment before mips_option_groups.
#
#   Test options:
#      The options specified in dg-options.
#
#   Explicit options:
#      The options that were either passed to runtest as "multilib" options
#      (e.g. -mips4 in --target_board=mips-sim-idt/-mips4) or specified as
#      test options.  Note that options in parenthesis (i.e. (-mips16)) are
#      not explicit and can be omitted depending on the base options.
#
#   Base options:
#      Options that are on by default without being specified in dg-options,
#      e.g. -march=mips64r2 for mipsisa64r2-elf or because they've been
#      passed to runtest as "multilib" options.
#
#   Option array:
#      Many functions in this file work with option arrays.  These are
#      two-dimensional Tcl arrays where the first dimension can have three
#      values: option, explicit_p or test_option_p.  The second dimension is
#      the name of the option group.  "option" contains the name of the
#      option that is in effect from this group.  If no option is active it
#      contains the empty string.  The flags "explicit_p" and "test_option_p"
#      are set for explicit and test options.

# Exit immediately if this isn't a MIPS target.
if ![istarget mips*-*-*] {
    return
}

# Load support procs.
load_lib gcc-dg.exp

# A list of GROUP REGEXP pairs.  Each GROUP represents a logical group of
# options from which only one option should be chosen.  REGEXP matches all
# the options in that group; it is implicitly wrapped in "^(...)$".
#
# Note that -O* is deliberately omitted from this list.  Tests in this
# directory are run at various optimisation levels and should use
# dg-skip-if to skip any incompatible levels.
set mips_option_groups {
    abi "-mabi=.*"
    addressing "addressing=.*"
    arch "-mips([1-5]|32.*|64.*)|-march=.*|isa(|_rev)(=|<=|>=).*"
    debug "-g.*"
    dump_pattern "-dp"
    endianness "-E(L|B)|-me(l|b)"
    float "-m(hard|soft)-float"
    forbid_cpu "forbid_cpu=.*"
    fp "-mfp(32|64)"
    gp "-mgp(32|64)"
    long "-mlong(32|64)"
    micromips "-mmicromips|-mno-micromips"
    mips16 "-mips16|-mno-mips16|-mflip-mips16"
    mips3d "-mips3d|-mno-mips3d"
    pic "-f(no-|)(pic|PIC)"
    profiling "-pg"
    small-data "-G[0-9]+"
    warnings "-w"
    dump "-fdump-.*"
}

# Add -mfoo/-mno-foo options to mips_option_groups.
foreach option {
    abicalls
    branch-likely
    dsp
    dspr2
    explicit-relocs
    extern-sdata
    fix-r4000
    fix-r10000
    fix-vr4130
    gpopt
    local-sdata
    long-calls
    paired-single
    plt
    shared
    smartmips
    sym32
    synci
    relax-pic-calls
    mcount-ra-address
} {
    lappend mips_option_groups $option "-m(no-|)$option"
}

# Add -mfoo= options to mips_option_groups.
foreach option {
    abs
    branch-cost
    code-readable
    nan
    r10k-cache-barrier
    tune
} {
    lappend mips_option_groups $option "-m$option=.*"
}

# Add -ffoo/-fno-foo options to mips_option_groups.
foreach option {
    common
    delayed-branch
    expensive-optimizations
    fast-math
    fat-lto-objects
    finite-math-only
    fixed-hi
    fixed-lo
    lax-vector-conversions
    omit-frame-pointer
    optimize-sibling-calls
    peephole2
    schedule-insns2
    split-wide-types
    tree-vectorize
    unroll-all-loops
    unroll-loops
} {
    lappend mips_option_groups $option "-f(no-|)$option"
}

# A list of option groups that have an impact on the ABI.
set mips_abi_groups {
    abi
    abicalls
    arch
    endianness
    float
    fp
    gp
    gpopt
    long
    pic
    small-data
}

# mips_option_tests(OPTION) is some assembly code that will run to completion
# on a target that supports OPTION.
set mips_option_tests(-mips16) {
    move $2,$31
    bal 1f
    .set mips16
    jr $31
    .set nomips16
    .align 2
1:
    ori $3,$31,1
    jalr $3
    move $31,$2
}
set mips_option_tests(-mpaired-single) {
    .set mips64
    lui $2,0x3f80
    mtc1 $2,$f0
    cvt.ps.s $f2,$f0,$f0
}
set mips_option_tests(-mips3d) {
    .set mips64
    .set mips3d
    lui $2,0x3f80
    mtc1 $2,$f0
    cvt.ps.s $f2,$f0,$f0
    mulr.ps $f2,$f2,$f2
    rsqrt1.s $f2,$f0
    mul.s $f4,$f2,$f0
    rsqrt2.s $f4,$f4,$f2
    madd.s $f4,$f2,$f2,$f4
}
set mips_option_tests(-mdsp) {
    .set mips64r2
    .set dsp
    addsc $2,$2,$2
}
set mips_option_tests(-mdspr2) {
    .set mips64r2
    .set dspr2
    prepend $2,$3,11
}

# Canonicalize command-line option OPTION.
proc mips_canonicalize_option { option } {
    regsub {^-mips([1-5]|32*|64*)$} $option {-march=mips\1} option

    regsub {^-mel$} $option {-EL} option
    regsub {^-meb$} $option {-EB} option

    regsub {^-O$} $option {-O1} option

    # MIPS doesn't use -fpic and -fPIC to distinguish between code models.
    regsub {^-f(no-|)PIC} $option {-f\1pic} option

    return $option
}

# Return true if OPTION1 and OPTION2 represent the same command-line option.
proc mips_same_option_p { option1 option2 } {
    return [string equal \
		[mips_canonicalize_option $option1] \
		[mips_canonicalize_option $option2]]
}

# Preprocess CODE using target_compile options OPTIONS.  Return the
# compiler output.
proc mips_preprocess { options code } {
    global tool

    set src dummy[pid].c
    set f [open $src "w"]
    puts $f $code
    close $f
    set output [${tool}_target_compile $src "" preprocess $options]
    file delete $src

    return $output
}

# Set the target board's command-line options to NEW_OPTIONS, storing the
# old values in UPVAR.
proc mips_push_test_options { upvar new_options } {
    upvar $upvar var
    global board_info

    array unset var
    set var(name) board_info([target_info name],multilib_flags)
    if { [info exists $var(name)] } {
	set var(old_options) [set $var(name)]
	set $var(name) [join $new_options " "]
    }
}

# Undo the effects of [mips_push_test_options UPVAR ...]
proc mips_pop_test_options { upvar } {
    upvar $upvar var
    global board_info

    if { [info exists var(old_options)] } {
	set $var(name) $var(old_options)
    }
}

# Return property PROP for architecture option ARCH (which belongs to
# the "arch" group in mips_option_groups).  See the comment at the
# top of the file for the valid property names.
#
# Cache the results in mips_arch_info (which can be reused between test
# variants).
proc mips_arch_info { arch prop } {
    global mips_arch_info
    global board_info

    set arch [mips_canonicalize_option $arch]
    if { ![info exists mips_arch_info($arch,$prop)] } {
	mips_push_test_options saved_options {}
	set output [mips_preprocess [list "additional_flags=$arch -mabi=32"] {
	    int isa = __mips;
	    #ifdef __mips_isa_rev
	    int isa_rev = __mips_isa_rev;
	    #else
	    int isa_rev = 0;
	    #endif
	}]
	foreach lhs { isa isa_rev } {
	    regsub ".*$lhs = (\[^;\]*).*" $output {\1} rhs
	    verbose -log "Architecture $arch has $lhs $rhs"
	    set mips_arch_info($arch,$lhs) $rhs
	}
	mips_pop_test_options saved_options
    }
    return $mips_arch_info($arch,$prop)
}

# Return the option group associated with OPTION, or "" if none.
proc mips_option_maybe_group { option } {
    global mips_option_groups

    foreach { group regexp } $mips_option_groups {
	if { [regexp -- "^($regexp)\$" $option] } {
	    return $group
	}
    }
    return ""
}

# Return the option group associated with OPTION.  Raise an error if
# there is none.
proc mips_option_group { option } {
    set group [mips_option_maybe_group $option]
    if { [string equal $group ""] } {
	error "Unrecognised option: $option"
    }
    return $group
}

# Return the option for option group GROUP, or "" if no option in that
# group has been chosen.  UPSTATUS describes the option status.
proc mips_option { upstatus group } {
    upvar $upstatus status

    return $status(option,$group)
}

# If the base options for this test run include an option in group GROUP,
# return that option, otherwise return "".
proc mips_original_option { group } {
    global mips_base_options

    return [mips_option mips_base_options $group]
}

# Return true if the test described by UPSTATUS requires a specific
# option in group GROUP.  UPSTATUS describes the option status.
proc mips_test_option_p { upstatus group } {
    upvar $upstatus status

    return $status(test_option_p,$group)
}

# If the test described by UPSTATUS requires a particular option in group
# GROUP, return that option, otherwise return "".
proc mips_test_option { upstatus group } {
    upvar $upstatus status

    if { [mips_test_option_p status $group] } {
	return [mips_option status $group]
    } else {
	return ""
    }
}

# Return true if the options described by UPSTATUS include OPTION.
proc mips_have_option_p { upstatus option } {
    upvar $upstatus status

    return [mips_same_option_p \
		[mips_option status [mips_option_group $option]] \
		$option]
}

# Return true if the options described by UPSTATUS require MIPS16 support.
proc mips_using_mips16_p { upstatus } {
    upvar $upstatus status

    return [expr { [mips_have_option_p status "-mips16"]
		   || [mips_have_option_p status "-mflip-mips16"] }]
}

# Return true if the test described by UPSTATUS requires option OPTION.
proc mips_have_test_option_p { upstatus option } {
    upvar $upstatus status

    set group [mips_option_group $option]
    return [mips_same_option_p [mips_test_option status $group] $option]
}

# If the test described by UPSTATUS does not specify an option in
# OPTION's group, act as though it had specified OPTION.
#
# The first optional argument indicates whether the option should be
# treated as though it were wrapped in parentheses; see the comment at
# the top of the file for details about this convention.  The default is 0.
proc mips_make_test_option { upstatus option args } {
    upvar $upstatus status

    set group [mips_option_group $option]
    if { ![mips_test_option_p status $group] } {
	set status(option,$group) $option
	set status(test_option_p,$group) 1
	if { [llength $args] == 0 || ![lindex $args 0] } {
	    set status(explicit_p,$group) 1
	}
    }
}

# If the test described by UPSTATUS requires option FROM, assume that
# it implicitly requires option TO.
proc mips_option_dependency { upstatus from to } {
    upvar $upstatus status

    if { [mips_have_test_option_p status $from] } {
	mips_make_test_option status $to
    }
}

# Return true if the given arch-group option specifies a 32-bit ISA.
proc mips_32bit_arch_p { option } {
    set isa [mips_arch_info $option isa]
    return [expr { $isa < 3 || $isa == 32 }]
}

# Return true if the given arch-group option specifies a 64-bit ISA.
proc mips_64bit_arch_p { option } {
    return [expr { ![mips_32bit_arch_p $option] }]
}

# Return true if the given abi-group option implicitly requires -mgp32.
proc mips_32bit_abi_p { option } {
    switch -glob -- $option {
	-mabi=32 {
	    return 1
	}
    }
    return 0
}

# Return true if the given abi-group option implicitly requires -mgp64.
proc mips_64bit_abi_p { option } {
    switch -glob -- $option {
	-mabi=o64 -
	-mabi=n32 -
	-mabi=64 {
	    return 1
	}
    }
    return 0
}

# Return true if the given abi-group option implicitly requires -mlong32.
# o64 requires this for -mabicalls, but not otherwise; pick the conservative
# case for simplicity.
proc mips_long32_abi_p { option } {
    switch -glob -- $option {
	-mabi=o64 -
	-mabi=n32 -
	-mabi=32 {
	    return 1
	}
    }
    return 0
}

# Return true if the given abi-group option implicitly requires -mlong64.
proc mips_long64_abi_p { option } {
    switch -glob -- $option {
	-mabi=64 {
	    return 1
	}
    }
    return 0
}

# Check whether the current target supports all the options that the
# current test requires.  Return "" if so, otherwise return one of
# the incompatible options.  UPSTATUS describes the option status.
proc mips_first_unsupported_option { upstatus } {
    global mips_option_tests
    upvar $upstatus status

    foreach { option code } [array get mips_option_tests] {
	if { [mips_have_test_option_p status $option] } {
	    regsub -all "\n" $code "\\n\\\n" asm
	    # Use check_runtime from target-supports.exp, which caches
	    # the result for us.
	    if { ![check_runtime mips_option_$option [subst {
		__attribute__((nomips16)) int
		main (void)
		{
		    asm (".set push\
		    $asm\
		    .set pop");
		    return 0;
		}
	    }]] } {
		return $option
	    }
	}
    }
    return ""
}

# Initialize this testsuite for a new test variant.
proc mips-dg-init {} {
    # Invariant information.
    global mips_option_groups

    # Internally-generated information about this run.
    global mips_base_options
    global mips_extra_options

    # Override dg-options with our mips-dg-options routine.
    rename dg-options mips-old-dg-options
    rename mips-dg-options dg-options

    # Start with a fresh option status.
    array unset mips_base_options
    foreach { group regexp } $mips_option_groups {
	set mips_base_options(option,$group) ""
	set mips_base_options(explicit_p,$group) 0
	set mips_base_options(test_option_p,$group) 0
    }

    # Use preprocessor macros to work out as many implicit options as we can.
    set output [mips_preprocess "" {
	const char *options[] = {
	    #if !defined _MIPS_SIM
	    "-mabi=eabi",
	    #elif _MIPS_SIM==_ABIO32
	    "-mabi=32",
	    #elif _MIPS_SIM==_ABIO64
	    "-mabi=o64",
	    #elif _MIPS_SIM==_ABIN32
	    "-mabi=n32",
	    #else
	    "-mabi=64",
	    #endif

	    "-march=" _MIPS_ARCH,

	    #ifdef _MIPSEB
	    "-EB",
	    #else
	    "-EL",
	    #endif

	    #ifdef __mips_hard_float
	    "-mhard-float",
	    #else
	    "-msoft-float",
	    #endif

	    #ifdef __mips_abs2008
	    "-mabs=2008",
	    #else
	    "-mabs=legacy",
	    #endif

	    #ifdef __mips_nan2008
	    "-mnan=2008",
	    #else
	    "-mnan=legacy",
	    #endif

	    #if __mips_fpr == 64
	    "-mfp64",
	    #else
	    "-mfp32",
	    #endif

	    #ifdef __mips64
	    "-mgp64",
	    #else
	    "-mgp32",
	    #endif

	    #if _MIPS_SZLONG == 64
	    "-mlong64",
	    #else
	    "-mlong32",
	    #endif

	    #ifdef __mips16
	    "-mips16",
	    #else
	    "-mno-mips16",
	    #endif

	    #ifdef __mips3d
	    "-mips3d",
	    #else
	    "-mno-mips3d",
	    #endif

	    #ifdef __mips_paired_single_float
	    "-mpaired-single",
	    #else
	    "-mno-paired-single",
	    #endif

	    #if __mips_abicalls
	    "-mabicalls",
	    #else
	    "-mno-abicalls",
	    #endif

	    #if __mips_dsp_rev >= 2
	    "-mdspr2",
	    #else
	    "-mno-dspr2",
	    #endif

	    #if __mips_dsp_rev >= 1
	    "-mdsp",
	    #else
	    "-mno-dsp",
	    #endif

	    #ifndef __PIC__
	    "addressing=absolute",
	    #endif

	    #ifdef __mips_smartmips
	    "-msmartmips",
	    #else
	    "-mno-smartmips",
	    #endif

	    #ifdef __mips_synci
	    "-msynci",
	    #else
	    "-mno-synci",
	    #endif

	    0
	};
    }]
    foreach line [split $output "\r\n"] {
	# Poor man's string concatenation.
	regsub -all {" "} $line "" line
	if { [regexp {"(.*)",} $line dummy option] } {
	    set group [mips_option_group $option]
	    set mips_base_options(option,$group) $option
	}
    }

    # Process the target's multilib options, saving any unrecognized
    # ones in mips_extra_options.
    set mips_extra_options {}
    foreach option [split [board_info target multilib_flags]] {
	set group [mips_option_maybe_group $option]
	if { ![string equal $group ""] } {
	    set mips_base_options(option,$group) $option
	    set mips_base_options(explicit_p,$group) 1
	} else {
	    lappend mips_extra_options $option
	}
    }
}

# Finish a test run started by mips-dg-init.
proc mips-dg-finish {} {
    rename dg-options mips-dg-options
    rename mips-old-dg-options dg-options
}

# Override dg-options so that we can do some MIPS-specific processing.
# All options used in this testsuite must appear in mips_option_groups.
#
# Test options override multilib options.  Certain test options can
# also imply other test options, which also override multilib options.
# These dependencies are ordered as follows:
#
#          START                        END
#            |                           |
#         -mips16/-mflip-mips16       -mno-mips16
#            |                           |
#         -micromips                  -mno-micromips
#            |                           |
#         -mips3d                     -mno-mips3d
#            |                           |
#         -mpaired-single             -mno-paired-single
#            |                           |
#         -mfp64                      -mfp32
#            |                           |
#         -mabs=2008/-mabs=legacy     <no option>
#            |                           |
#         -mhard-float                -msoft-float
#            |                           |
#         -mno-sym32                  -msym32
#            |                           |
#         -mrelax-pic-calls           -mno-relax-pic-calls
#            |                           |
#         -fpic                       -fno-pic
#            |                           |
#         -mshared                    -mno-shared
#            |                           |
#         -mno-plt                    -mplt
#            |                           |
#         addressing=unknown          addressing=absolute
#            |                           |
#         -mabicalls                  -mno-abicalls
#            |                           |
#         -G0                         <other value>
#            |                           |
#         <other value>               -mr10k-cache-barrier=none
#            |                           |
#         -mfix-r10000                -mno-fix-r10000
#            |                           |
#         -mbranch-likely             -mno-branch-likely
#            |                           |
#         -msmartmips                 -mno-smartmips
#            |                           |
#         -mno-gpopt                  -mgpopt
#            |                           |
#         -mexplicit-relocs           -mno-explicit-relocs
#            |                           |
#         -mdspr2                     -mno-dspr2
#            |                           |
#         -mdsp                       -mno-dsp
#            |                           |
#         -msynci                     -mno-synci
#            |                           |
#            +-- gp, abi & arch ---------+
#
# For these purposes, the "gp", "abi" & "arch" option groups are treated
# as a single node.
proc mips-dg-options { args } {
    # dg.exp variables.
    upvar dg-extra-tool-flags extra_tool_flags
    upvar dg-do-what do_what

    # Invariant information.
    global mips_option_groups
    global mips_abi_groups

    # Information about this run.
    global mips_base_options

    if { [llength $args] >= 3 } {
        switch { [dg-process-target [lindex $args 2]] } {
            "S" { }
            "N" { return }
            "F" { error "[lindex $args 0]: `xfail' not allowed here" }
            "P" { error "[lindex $args 0]: `xfail' not allowed here" }
        }
    }

    # Start out with the default option state.
    array set options [array get mips_base_options]

    # Record the options that this test explicitly needs.
    foreach option [lindex $args 1] {
	set all_but_p [regexp {^\((.*)\)$} $option dummy option]
	set group [mips_option_group $option]
	if { [mips_test_option_p options $group] } {
	    set old [mips_option options $group]
	    error "Inconsistent $group option: $old vs. $option"
	} else {
	    mips_make_test_option options $option $all_but_p
	}
    }

    # Handle dependencies between the test options and the optimization ones.
    mips_option_dependency options "-fno-unroll-loops" "-fno-unroll-all-loops"
    mips_option_dependency options "-pg" "-fno-omit-frame-pointer"

    # Handle dependencies between options on the left of the
    # dependency diagram.
    mips_option_dependency options "-mips16" "-mno-micromips"
    mips_option_dependency options "-mmicromips" "-mno-mips16"
    mips_option_dependency options "-mips3d" "-mpaired-single"
    mips_option_dependency options "-mpaired-single" "-mfp64"
    mips_option_dependency options "-mfp64" "-mhard-float"
    mips_option_dependency options "-mabs=2008" "-mhard-float"
    mips_option_dependency options "-mabs=legacy" "-mhard-float"
    mips_option_dependency options "-mrelax-pic-calls" "-mno-plt"
    mips_option_dependency options "-mrelax-pic-calls" "-mabicalls"
    mips_option_dependency options "-mrelax-pic-calls" "-mexplicit-relocs"
    mips_option_dependency options "-fpic" "-mshared"
    mips_option_dependency options "-mshared" "-mno-plt"
    mips_option_dependency options "-mshared" "-mabicalls"
    mips_option_dependency options "-mno-plt" "addressing=unknown"
    mips_option_dependency options "-mabicalls" "-G0"
    mips_option_dependency options "-mno-gpopt" "-mexplicit-relocs"

    # Work out information about the current ABI.
    set abi_test_option_p [mips_test_option_p options abi]
    set abi [mips_option options abi]
    set eabi_p [mips_same_option_p $abi "-mabi=eabi"]

    # If the test forces a particular ABI, set the register size
    # accordingly.
    if { $abi_test_option_p } {
	if { [mips_32bit_abi_p $abi] } {
	    mips_make_test_option options "-mgp32"
	} elseif { [mips_64bit_abi_p $abi] } {
	    mips_make_test_option options "-mgp64"
	}
    }

    # See whether forbid_cpu forces us to choose a new architecture.
    set arch [mips_option mips_base_options arch]
    set force_generic_isa_p [expr {
	[regexp "forbid_cpu=(.*)" [mips_option options forbid_cpu] dummy spec]
	&& [regexp -- "^-march=$spec\$" $arch]
    }]

    # Interpret the special "isa" and "isa_rev" options.  If we have
    # a choice of a 32-bit or a 64-bit architecture, prefer to keep
    # the -mgp setting the same.
    set spec [mips_option options arch]
    if { [regexp {^[^-]} $spec] } {
	if { [string equal $spec "isa=loongson"] } {
	    if { ![regexp {^-march=loongson} $arch] } {
		set arch "-march=loongson2f"
	    }
	} else {
	    if { ![regexp {^(isa(?:|_rev))(=|<=|>=)([0-9]*)$} \
		       $spec dummy prop relation value nocpus] } {
		error "Unrecognized isa specification: $spec"
	    }
	    set current [mips_arch_info $arch $prop]
	    if { $force_generic_isa_p
		 || ($current < $value && ![string equal $relation "<="])
		 || ($current > $value && ![string equal $relation ">="])
		 || ([mips_have_test_option_p options "-mgp64"]
		     && [mips_32bit_arch_p $arch]) } {
		# The current setting is out of range; it cannot
		# possibly be used.  Find a replacement that can.
		if { [string equal $prop "isa"] } {
		    set arch "-mips$value"
		} elseif { $value == 0 } {
		    set arch "-mips4"
		} else {
		    if { [mips_have_option_p options "-mgp32"] } {
			set arch "-mips32"
		    } else {
			set arch "-mips64"
		    }
		    if { $value > 1 } {
			append arch "r$value"
		    }
		}
	    }
	}
	set options(option,arch) $arch
    }

    # Work out information about the current architecture.
    set arch_test_option_p [mips_test_option_p options arch]
    set arch [mips_option options arch]
    set isa [mips_arch_info $arch isa]
    set isa_rev [mips_arch_info $arch isa_rev]

    # If the test forces a 32-bit architecture, force -mgp32.
    # Force the current -mgp setting otherwise; if we don't,
    # some configurations would make a 64-bit architecture
    # imply -mgp64.
    if { $arch_test_option_p } {
	if { [mips_32bit_arch_p $arch] } {
	    mips_make_test_option options "-mgp32"
	} else {
	    mips_make_test_option options [mips_option options gp]
	}
    }

    # We've now fixed the GP register size.  Make it easily available.
    set gp_size [expr { [mips_have_option_p options "-mgp32"] ? 32 : 64 }]

    # Handle dependencies between the pre-arch options and the arch option.
    # This should mirror the arch and post-arch code below.
    if { !$arch_test_option_p } {
	# We need a revision 2 or better ISA for:
	#
	#   - the combination of -mgp32 -mfp64
	#   - the DSP ASE
	if { $isa_rev < 2
	     && (($gp_size == 32 && [mips_have_test_option_p options "-mfp64"])
		 || [mips_have_test_option_p options "-msynci"]
		 || [mips_have_test_option_p options "-mdsp"]
		 || [mips_have_test_option_p options "-mdspr2"]) } {
	    if { $gp_size == 32 } {
		mips_make_test_option options "-mips32r2"
	    } else {
		mips_make_test_option options "-mips64r2"
	    }
        # We need a MIPS32 or MIPS64 ISA for:
	#
        #   - paired-single instructions(*)
        #
	# (*) Note that we don't support MIPS V at the moment.
	} elseif { $isa_rev < 1
		   && [mips_have_test_option_p options "-mpaired-single"] } {
	    if { $gp_size == 32 } {
		mips_make_test_option options "-mips32"
	    } else {
		mips_make_test_option options "-mips64"
	    }
        # We need MIPS III or higher for:
	#
	#   - the "cache" instruction
	} elseif { $isa < 3
		   && ([mips_have_test_option_p options \
			    "-mr10k-cache-barrier=load-store"]
		       || [mips_have_test_option_p options \
			       "-mr10k-cache-barrier=store"]) } {
	    mips_make_test_option options "-mips3"
        # We need MIPS II or higher for:
	#
	#   - branch-likely instructions(*)
        #
	# (*) needed by both -mbranch-likely and -mfix-r10000
	} elseif { $isa < 2
		   && ([mips_have_test_option_p options "-mbranch-likely"]
		       || [mips_have_test_option_p options "-mfix-r10000"]) } {
	    mips_make_test_option options "-mips2"
	# Check whether we need to switch from a 32-bit processor to the
	# "nearest" 64-bit processor.
	} elseif { $gp_size == 64 && [mips_32bit_arch_p $arch] } {
	    if { $isa_rev == 0 } {
		mips_make_test_option options "-mips3"
	    } elseif { $isa_rev == 1 } {
		mips_make_test_option options "-mips64"
	    } else {
		mips_make_test_option options "-mips64r$isa_rev"
	    }
	# Otherwise, if the current choice of architecture is unacceptable,
	# choose the equivalent generic architecture.
	} elseif { $force_generic_isa_p } {
	    set arch "-mips[mips_arch_info $arch isa]"
	    if { $isa_rev > 1 } {
		append arch "r$isa_rev"
	    }
	    mips_make_test_option options $arch
	}
	unset arch
	unset isa
	unset isa_rev
    }

    # Set an appropriate ABI, handling dependencies between the pre-abi
    # options and the abi options.  This should mirror the abi and post-abi
    # code below.
    if { !$abi_test_option_p } {
	if { ($eabi_p
	      && ([mips_have_option_p options "-mabicalls"]
		  || ($gp_size == 32
		      && [mips_have_option_p options "-mfp64"]))) } {
	    # EABI doesn't support -mabicalls.
	    # EABI doesn't support the combination -mgp32 -mfp64.
	    set force_abi 1
	} elseif { [mips_using_mips16_p options]
		   && ![mips_same_option_p $abi "-mabi=32"]
		   && ![mips_same_option_p $abi "-mabi=o64"]
		   && (![mips_have_option_p options "addressing=absolute"]
		       || [mips_have_option_p options "-mhard-float"]) } {
	    # -mips16 -mhard-float requires o32 or o64.
	    # -mips16 PIC requires o32 or o64.
	    set force_abi 1
	} elseif { [mips_have_test_option_p options "-mlong32"]
		   && [mips_long64_abi_p $abi] } {
	    set force_abi 1
	} elseif { [mips_have_test_option_p options "-mlong64"]
		   && [mips_long32_abi_p $abi] } {
	    set force_abi 1
	} else {
	    set force_abi 0
	}
	if { $gp_size == 32 } {
	    if { $force_abi || [mips_64bit_abi_p $abi] } {
		if { [mips_have_test_option_p options "-mlong64"] } {
		    mips_make_test_option options "-mabi=eabi"
		    mips_make_test_option options "-mgp32"
		} else {
		    mips_make_test_option options "-mabi=32"
		}
	    }
	} else {
	    if { $force_abi || [mips_32bit_abi_p $abi] } {
		if { [mips_have_test_option_p options "-mlong64"] } {
		    mips_make_test_option options "-mabi=eabi"
		    mips_make_test_option options "-mgp64"
		} else {
		    # All configurations should have an assembler that
		    # supports o64, since it requires the same BFD target
		    # vector as o32.  In contrast, many assembler
		    # configurations do not have n32 or n64 support.
		    mips_make_test_option options "-mabi=o64"
		}
	    }
	}
	set abi_test_option_p [mips_test_option_p options abi]
	set abi [mips_option options abi]
	set eabi_p [mips_same_option_p $abi "-mabi=eabi"]
    }

    # Handle dependencies between the abi options and the post-abi options.
    # This should mirror the abi and pre-abi code above.
    if { $abi_test_option_p } {
	if { $eabi_p } {
	    mips_make_test_option options "-mno-abicalls"
	    if { $gp_size == 32 } {
		mips_make_test_option options "-mfp32"
	    }
	}
	if { [mips_using_mips16_p options]
	     && ![mips_same_option_p $abi "-mabi=32"]
	     && ![mips_same_option_p $abi "-mabi=o64"]
	     && ([mips_have_option_p options "-mabicalls"]
		 || [mips_have_option_p options "-mhard-float"]) } {
	    if { [mips_test_option_p options mips16] } {
		mips_make_test_option options "addressing=absolute"
		mips_make_test_option options "-msoft-float"
	    } else {
		mips_make_test_option options "-mno-mips16"
	    }
	}
	if { [mips_long32_abi_p $abi] } {
	    mips_make_test_option options "-mlong32"
	} elseif { [mips_long64_abi_p $abi] } {
	    mips_make_test_option options "-mlong64"
	}
    }

    # Handle dependencies between the arch option and the post-arch options.
    # This should mirror the arch and pre-arch code above.
    if { $arch_test_option_p } {
	if { $isa < 2 } {
	    mips_make_test_option options "-mno-branch-likely"
	    mips_make_test_option options "-mno-fix-r10000"
	}
	if { $isa < 3 } {
	    mips_make_test_option options "-mr10k-cache-barrier=none"
	}
	if { $isa_rev < 1 } {
	    mips_make_test_option options "-mno-paired-single"
	}
	if { $isa_rev < 2 } {
	    if { $gp_size == 32 } {
		mips_make_test_option options "-mfp32"
	    }
	    mips_make_test_option options "-mno-dsp"
	    mips_make_test_option options "-mno-synci"
	}
	unset arch
	unset isa
	unset isa_rev
    }

    # Handle dependencies between options on the right of the diagram.
    mips_option_dependency options "-mno-dsp" "-mno-dspr2"
    mips_option_dependency options "-mno-explicit-relocs" "-mgpopt"
    switch -- [mips_test_option options small-data] {
	"" -
	-G0 {}
	default {
	    mips_make_test_option options "-mno-abicalls"
	}
    }
    if { [mips_have_option_p options "-mabicalls"] } {
	mips_option_dependency options "addressing=absolute" "-mplt"
    }
    mips_option_dependency options "-mplt" "-msym32"
    mips_option_dependency options "-mplt" "-mno-shared"
    mips_option_dependency options "-mno-shared" "-fno-pic"
    mips_option_dependency options "-mfp32" "-mno-paired-single"
    mips_option_dependency options "-msoft-float" "-mno-paired-single"
    mips_option_dependency options "-mno-paired-single" "-mno-mips3d"

    # If the test requires an unsupported option, change run tests
    # to link tests.

    switch -- [lindex $do_what 0] {
	run {
	    set option [mips_first_unsupported_option options]
	    if { ![string equal $option ""] } {
		set do_what [lreplace $do_what 0 0 link]
		verbose -log "Downgraded to a 'link' test due to unsupported option '$option'"
	    }
	}
    }

    # If the test has overridden a option that changes the ABI,
    # downgrade a link or execution test to an assembler test.
    foreach group $mips_abi_groups {
	set old_option [mips_original_option $group]
	set new_option [mips_option options $group]
	if { ![mips_same_option_p $old_option $new_option] } {
	    switch -- [lindex $do_what 0] {
		link -
		run {
		    set do_what [lreplace $do_what 0 0 assemble]
		    verbose -log "Downgraded to an 'assemble' test due to incompatible $group option ($old_option changed to $new_option)"
		}
	    }
	    break
	}
    }

    # Add all options to the dg variable.
    set options(explicit_p,addressing) 0
    set options(explicit_p,forbid_cpu) 0
    foreach { group regexp } $mips_option_groups {
	if { $options(explicit_p,$group) } {
	    append extra_tool_flags " " $options(option,$group)
	}
    }

    # If the test is MIPS16-compatible, provide a counterpart to the
    # NOMIPS16 convenience macro.
    if { [mips_have_test_option_p options "-mips16"] } {
	append extra_tool_flags " -DMIPS16=__attribute__((mips16))"
    }

    if { [mips_have_test_option_p options "-mmicromips"] } {
	append extra_tool_flags " -DMICROMIPS=__attribute__((micromips))"
    }

    # Use our version of gcc-dg-test for this test.
    if { ![string equal [info procs "mips-gcc-dg-test"] ""] } {
	rename gcc-dg-test mips-old-gcc-dg-test
	rename mips-gcc-dg-test gcc-dg-test
    }
}

# A version of gcc-dg-test that is used by dg-options tests.
proc mips-gcc-dg-test { prog do_what extra_tool_flags } {
    global board_info
    global mips_extra_options

    # Override the user's chosen test options with the combined test/user
    # version.
    mips_push_test_options saved_options $mips_extra_options
    set result [gcc-dg-test-1 gcc_target_compile $prog \
		    $do_what $extra_tool_flags]
    mips_pop_test_options saved_options

    # Restore the usual gcc-dg-test.
    rename gcc-dg-test mips-gcc-dg-test
    rename mips-old-gcc-dg-test gcc-dg-test

    return $result
}

dg-init
mips-dg-init
gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.c]] \
    "-DNOMIPS16=__attribute__((nomips16)) -DNOMICROMIPS=__attribute__((nomicromips)) -DNOCOMPRESSION=__attribute__((nocompression))"
mips-dg-finish
dg-finish