1#! /bin/sh
2#
3# GMP config.guess wrapper.
4
5
6# Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2011 Free Software
7# Foundation, Inc.
8#
9# This file is part of the GNU MP Library.
10#
11# The GNU MP Library is free software; you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License as published
13# by the Free Software Foundation; either version 3 of the License, or (at
14# your option) any later version.
15#
16# The GNU MP Library is distributed in the hope that it will be useful, but
17# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19# License for more details.
20#
21# You should have received a copy of the GNU Lesser General Public License
22# along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.
23
24
25# Usage: config.guess
26#
27# Print the host system CPU-VENDOR-OS.
28#
29# configfsf.guess is run and its guess then sharpened up to take advantage
30# of the finer grained CPU types that GMP knows.
31
32
33# Expect to find configfsf.guess in the same directory as this config.guess
34configfsf_guess="`echo \"$0\" | sed 's/config.guess$/configfsf.guess/'`"
35if test "$configfsf_guess" = "$0"; then
36  echo "Cannot derive configfsf.guess from $0" 1>&2
37  exit 1
38fi
39if test -f "$configfsf_guess"; then
40  :
41else
42  echo "$configfsf_guess not found" 1>&2
43  exit 1
44fi
45
46# Setup a $SHELL with which to run configfsf.guess, using the same
47# $CONFIG_SHELL or /bin/sh as autoconf does when running config.guess
48SHELL=${CONFIG_SHELL-/bin/sh}
49
50# Identify ourselves on --version, --help or errors
51if test $# != 0; then
52  echo "(GNU MP wrapped config.guess)"
53  $SHELL $configfsf_guess "$@"
54  exit 1
55fi
56
57guess_full=`$SHELL $configfsf_guess`
58if test $? != 0; then
59  exit 1
60fi
61
62guess_cpu=`echo "$guess_full" | sed 's/-.*$//'`
63guess_rest=`echo "$guess_full" | sed 's/^[^-]*//'`
64exact_cpu=
65
66
67# -------------------------------------------------------------------------
68# The following should look at the current guess and probe the system to
69# establish a better guess in exact_cpu.  Leave exact_cpu empty if probes
70# can't be done, or don't work.
71#
72# When a number of probes are done, test -z "$exact_cpu" can be used instead
73# of putting each probe under an "else" of the preceeding.  That can stop
74# the code getting horribly nested and marching off the right side of the
75# screen.
76
77# Note that when a compile-and-link is done in one step we need to remove .o
78# files, since lame C compilers generate these even when not asked.
79#
80
81dummy=dummy-$$
82trap 'rm -f $dummy.c $dummy.o $dummy.core $dummy ${dummy}1.s ${dummy}2.c ; exit 1' 1 2 15
83
84# Use $HOST_CC if defined. $CC may point to a cross-compiler
85if test x"$CC_FOR_BUILD" = x; then
86  if test x"$HOST_CC" != x; then
87    CC_FOR_BUILD="$HOST_CC"
88  else
89    if test x"$CC" != x; then
90      CC_FOR_BUILD="$CC"
91    else
92      echo 'dummy(){}' >$dummy.c
93      for c in cc gcc c89 c99; do
94	  ($c $dummy.c -c) >/dev/null 2>&1
95	  if test $? = 0; then
96	      CC_FOR_BUILD="$c"; break
97	  fi
98      done
99      rm -f $dummy.c $dummy.o
100      if test x"$CC_FOR_BUILD" = x; then
101	CC_FOR_BUILD=no_compiler_found
102      fi
103    fi
104  fi
105fi
106
107
108case "$guess_full" in
109
110alpha-*-*)
111  # configfsf.guess detects exact alpha cpu types for OSF and GNU/Linux, but
112  # not for *BSD and other systems.  We try to get an exact type for any
113  # plain "alpha" it leaves.
114  #
115  # configfsf.guess used to have a block of code not unlike this, but these
116  # days does its thing with Linux kernel /proc/cpuinfo or OSF psrinfo.
117  #
118  cat <<EOF >$dummy.s
119	.data
120Lformat:
121	.byte 37,100,45,37,120,10,0	# "%d-%x\n"
122	.text
123	.globl main
124	.align 4
125	.ent main
126main:
127	.frame \$30,16,\$26,0
128	ldgp \$29,0(\$27)
129	.prologue 1
130	.long 0x47e03d91 # implver \$17
131	lda \$2,-1
132	.long 0x47e20c21 # amask \$2,\$1
133	lda \$16,Lformat
134	not \$1,\$18
135	jsr \$26,printf
136	ldgp \$29,0(\$26)
137	mov 0,\$16
138	jsr \$26,exit
139	.end main
140EOF
141  $CC_FOR_BUILD $dummy.s -o $dummy 2>/dev/null
142  if test "$?" = 0 ; then
143    case `./$dummy` in
144    0-0)	exact_cpu=alpha      ;;
145    1-0)	exact_cpu=alphaev5   ;;
146    1-1)	exact_cpu=alphaev56  ;;
147    1-101)	exact_cpu=alphapca56 ;;
148    2-303)	exact_cpu=alphaev6   ;;
149    2-307)	exact_cpu=alphaev67  ;;
150    2-1307)	exact_cpu=alphaev68  ;;
151    esac
152  fi
153  rm -f $dummy.s $dummy.o $dummy
154  ;;
155
156ia64*-*-*)
157  # CPUID[3] bits 24 to 31 is the processor family.  itanium2 is documented
158  # as 0x1f, plain itanium has been seen returning 0x07 on two systems, but
159  # haven't found any documentation on it as such.
160  #
161  # Defining both getcpuid and _getcpuid lets us ignore whether the system
162  # expects underscores or not.
163  #
164  # "unsigned long long" is always 64 bits, in fact on hpux in ilp32 mode
165  # (which is the default there), it's the only 64-bit type.
166  #
167  cat >${dummy}a.s <<EOF
168	.text
169	.global	_getcpuid
170	.proc	_getcpuid
171_getcpuid:
172	mov	r8 = CPUID[r32] ;;
173	br.ret.sptk.many rp ;;
174	.endp	_getcpuid
175	.global	getcpuid
176	.proc	getcpuid
177getcpuid:
178	mov	r8 = CPUID[r32] ;;
179	br.ret.sptk.many rp ;;
180	.endp	getcpuid
181EOF
182  cat >${dummy}b.c <<EOF
183#include <stdio.h>
184unsigned long long getcpuid ();
185int
186main ()
187{
188  if (getcpuid(0LL) == 0x49656E69756E6547LL && getcpuid(1LL) == 0x6C65746ELL)
189    {
190      /* "GenuineIntel" */
191      switch ((getcpuid(3LL) >> 24) & 0xFF) {
192      case 0x07: puts ("itanium");  break;
193      case 0x1F: puts ("itanium2"); break; /* McKinley, Madison */
194      case 0x20: puts ("itanium2"); break; /* Montecito */
195      }
196    }
197  return 0;
198}
199EOF
200  if $CC_FOR_BUILD ${dummy}a.s ${dummy}b.c -o $dummy >/dev/null 2>&1; then
201    exact_cpu=`./$dummy`
202  fi
203  rm -f ${dummy}a.s ${dummy}a.o ${dummy}b.c ${dummy}b.o $dummy $dummy.core core
204  ;;
205
206mips-*-irix[6789]*)
207  # IRIX 6 and up always has a 64-bit mips cpu
208  exact_cpu=mips64
209  ;;
210
211m68k-*-*)
212  # NetBSD (and presumably other *BSD) "sysctl hw.model" gives for example
213  #   hw.model = Apple Macintosh Quadra 610  (68040)
214  exact_cpu=`(sysctl hw.model) 2>/dev/null | sed -n 's/^.*\(680[012346]0\).*$/m\1/p'`
215  if test -z "$exact_cpu"; then
216    # Linux kernel 2.2 gives for example "CPU: 68020" (tabs in between).
217    exact_cpu=`sed -n 's/^CPU:.*\(680[012346]0\).*$/m\1/p' /proc/cpuinfo 2>/dev/null`
218  fi
219  if test -z "$exact_cpu"; then
220    # Try: movel #0,%d0; rts
221    # This is to check the compiler and our asm code works etc, before
222    # assuming failures below indicate cpu characteristics.
223    # .byte is used to avoid problems with assembler syntax variations.
224    # For testing, provoke failures by adding "illegal" possibly as
225    # ".byte 0x4A, 0xFC"
226    cat >$dummy.s <<EOF
227	.text
228	.globl main
229	.globl _main
230main:
231_main:
232	.byte	0x70, 0x00
233	.byte	0x4e, 0x75
234EOF
235    if ($CC_FOR_BUILD $dummy.s -o $dummy && ./$dummy) >/dev/null 2>&1; then
236
237      # $SHELL -c is used to execute ./$dummy below, since (./$dummy)
238      # 2>/dev/null still prints the SIGILL message on some shells.
239      #
240        # Try: movel #0,%d0
241        #      rtd #0
242        cat >$dummy.s <<EOF
243	.text
244	.globl main
245	.globl _main
246main:
247_main:
248	.byte	0x70, 0x00
249	.byte	0x4e, 0x74, 0x00, 0x00
250EOF
251        if $CC_FOR_BUILD $dummy.s -o $dummy >/dev/null 2>&1; then
252          $SHELL -c ./$dummy >/dev/null 2>&1
253	  if test $? != 0; then
254            exact_cpu=m68000    # because rtd didn't work
255          fi
256        fi
257      #
258
259      if test -z "$exact_cpu"; then
260        # Try: trapf
261        #      movel #0,%d0
262        #      rts
263        # Another possibility for identifying 68000 and 68010 is the
264        # different value stored by "movem a0,(a0)+"
265        cat >$dummy.s <<EOF
266	.text
267	.globl main
268	.globl _main
269main:
270_main:
271	.byte	0x51, 0xFC
272	.byte	0x70, 0x00
273	.byte	0x4e, 0x75
274EOF
275        if $CC_FOR_BUILD $dummy.s -o $dummy >/dev/null 2>&1; then
276          $SHELL -c ./$dummy >/dev/null 2>&1
277	  if test $? != 0; then
278            exact_cpu=m68010    # because trapf didn't work
279          fi
280        fi
281      fi
282
283      if test -z "$exact_cpu"; then
284        # Try: bfffo %d1{0:31},%d0
285        #      movel #0,%d0
286        #      rts
287        cat >$dummy.s <<EOF
288	.text
289	.globl main
290	.globl _main
291main:
292_main:
293	.byte	0xED, 0xC1, 0x00, 0x1F
294	.byte	0x70, 0x00
295	.byte	0x4e, 0x75
296EOF
297        if $CC_FOR_BUILD $dummy.s -o $dummy >/dev/null 2>&1; then
298          $SHELL -c ./$dummy >/dev/null 2>&1
299	  if test $? != 0; then
300            exact_cpu=m68360  # cpu32, because bfffo didn't work
301          fi
302        fi
303      fi
304
305      if test -z "$exact_cpu"; then
306        # FIXME: Now we know 68020 or up, but how to detect 030, 040 and 060?
307        exact_cpu=m68020
308      fi
309    fi
310    rm -f $dummy.s $dummy.o $dummy $dummy.core core
311  fi
312  if test -z "$exact_cpu"; then
313    case "$guess_full" in
314      *-*-next* | *-*-openstep*)  # NeXTs are 68020 or better
315        exact_cpu=m68020 ;;
316    esac
317  fi
318  ;;
319
320
321rs6000-*-* | powerpc*-*-*)
322  # Enhancement: On MacOS the "machine" command prints for instance
323  # "ppc750".  Interestingly on powerpc970-apple-darwin6.8.5 it prints
324  # "ppc970" where there's no actual #define for 970 from NXGetLocalArchInfo
325  # (as noted below).  But the man page says the command is still "under
326  # development", so it doesn't seem wise to use it just yet, not while
327  # there's an alternative.
328  #
329  # Try to read the PVR.  mfpvr is a protected instruction, NetBSD, MacOS
330  # and AIX don't allow it in user mode, but the Linux kernel does.
331  #
332  # Using explicit bytes for mfpvr avoids worrying about assembler syntax
333  # and underscores.  "char"s are used instead of "int"s to avoid worrying
334  # whether sizeof(int)==4 or if it's the right endianness.
335  #
336  # Note this is no good on AIX, since a C function there is the address of
337  # a function descriptor, not actual code.  But this doesn't matter since
338  # AIX doesn't allow mfpvr anyway.
339  #
340  cat >$dummy.c <<\EOF
341#include <stdio.h>
342struct {
343  int   n;  /* force 4-byte alignment */
344  char  a[8];
345} getpvr = {
346  0,
347  {
348    0x7c, 0x7f, 0x42, 0xa6,  /* mfpvr r3 */
349    0x4e, 0x80, 0x00, 0x20,  /* blr      */
350  }
351};
352int
353main ()
354{
355  unsigned  (*fun)();
356  unsigned  pvr;
357
358  /* a separate "fun" variable is necessary for gcc 2.95.2 on MacOS,
359     it gets a compiler error on a combined cast and call */
360  fun = (unsigned (*)()) getpvr.a;
361  pvr = (*fun) ();
362
363  switch (pvr >> 16) {
364  case 0x0001: puts ("powerpc601");  break;
365  case 0x0003: puts ("powerpc603");  break;
366  case 0x0004: puts ("powerpc604");  break;
367  case 0x0006: puts ("powerpc603e"); break;
368  case 0x0007: puts ("powerpc603e"); break;  /* 603ev */
369  case 0x0008: puts ("powerpc750");  break;
370  case 0x0009: puts ("powerpc604e"); break;
371  case 0x000a: puts ("powerpc604e"); break;  /* 604ev5 */
372  case 0x000c: puts ("powerpc7400"); break;
373  case 0x0041: puts ("powerpc630");  break;
374  case 0x0050: puts ("powerpc860");  break;
375  case 0x8000: puts ("powerpc7450"); break;
376  case 0x8001: puts ("powerpc7455"); break;
377  case 0x8002: puts ("powerpc7457"); break;
378  case 0x8003: puts ("powerpc7447"); break; /* really 7447A */
379  case 0x800c: puts ("powerpc7410"); break;
380  }
381  return 0;
382}
383EOF
384  if ($CC_FOR_BUILD $dummy.c -o $dummy) >/dev/null 2>&1; then
385    # This style construct is needed on AIX 4.3 to suppress the SIGILL error
386    # from (*fun)().  Using $SHELL -c ./$dummy 2>/dev/null doesn't work.
387    { x=`./$dummy`; } 2>/dev/null
388    if test -n "$x"; then
389      exact_cpu=$x
390    fi
391  fi
392  rm -f $dummy.c $dummy.o $dummy $dummy.core
393
394  # Grep the linux kernel /proc/cpuinfo pseudo-file.
395  # Anything unrecognised is ignored, since of course we mustn't spit out
396  # a cpu type config.sub doesn't know.
397  if test -z "$exact_cpu" && test -f /proc/cpuinfo; then
398    x=`grep "^cpu[ 	]" /proc/cpuinfo | head -n 1`
399    x=`echo $x | sed -n 's/^cpu[ 	]*:[ 	]*\([A-Za-z0-9]*\).*/\1/p'`
400    x=`echo $x | sed 's/PPC//'`
401    case $x in
402      601)     exact_cpu="power" ;;
403      603ev)   exact_cpu="powerpc603e" ;;
404      604ev5)  exact_cpu="powerpc604e" ;;
405      603 | 603e | 604 | 604e | 750 | 821 | 860 | 970)
406        exact_cpu="powerpc$x" ;;
407      POWER[4-9])
408        exact_cpu=`echo $x | sed "s;POWER;power;"` ;;
409    esac
410  fi
411
412  if test -z "$exact_cpu"; then
413    # On AIX, try looking at _system_configuration.  This is present in
414    # version 4 at least.
415    cat >$dummy.c <<EOF
416#include <stdio.h>
417#include <sys/systemcfg.h>
418int
419main ()
420{
421  switch (_system_configuration.implementation) {
422  /* Old versions of AIX don't have all these constants,
423     use ifdef for safety. */
424#ifdef POWER_RS2
425  case POWER_RS2:    puts ("power2");     break;
426#endif
427#ifdef POWER_601
428  case POWER_601:    puts ("power");      break;
429#endif
430#ifdef POWER_603
431  case POWER_603:    puts ("powerpc603"); break;
432#endif
433#ifdef POWER_604
434  case POWER_604:    puts ("powerpc604"); break;
435#endif
436#ifdef POWER_620
437  case POWER_620:    puts ("powerpc620"); break;
438#endif
439#ifdef POWER_630
440  case POWER_630:    puts ("powerpc630"); break;
441#endif
442  /* Dunno what this is, leave it out for now.
443  case POWER_A35:    puts ("powerpca35"); break;
444  */
445  /* This is waiting for a bit more info.
446  case POWER_RS64II: puts ("powerpcrs64ii"); break;
447  */
448#ifdef POWER_4
449  case POWER_4:    puts ("power4"); break;
450#endif
451#ifdef POWER_5
452  case POWER_5:    puts ("power5"); break;
453#endif
454#ifdef POWER_6
455  case POWER_6:    puts ("power6"); break;
456#endif
457#ifdef POWER_7
458  case POWER_7:    puts ("power7"); break;
459#endif
460  default:
461    if (_system_configuration.architecture == POWER_RS)
462      puts ("power");
463    else if (_system_configuration.width == 64)
464      puts ("powerpc64");
465  }
466  return 0;
467}
468EOF
469    if ($CC_FOR_BUILD $dummy.c -o $dummy) >/dev/null 2>&1; then
470      x=`./$dummy`
471      if test -n "$x"; then
472        exact_cpu=$x
473      fi
474    fi
475    rm -f $dummy.c $dummy.o $dummy
476  fi
477
478  if test -z "$exact_cpu"; then
479    # On MacOS X (or any Mach-O presumably), NXGetLocalArchInfo cpusubtype
480    # can tell us the exact cpu.
481    cat >$dummy.c <<EOF
482#include <stdio.h>
483#include <mach-o/arch.h>
484int
485main (void)
486{
487  const NXArchInfo *a = NXGetLocalArchInfo();
488  if (a->cputype == CPU_TYPE_POWERPC)
489    {
490      switch (a->cpusubtype) {
491      /* The following known to Darwin 1.3. */
492      case CPU_SUBTYPE_POWERPC_601:   puts ("powerpc601");  break;
493      case CPU_SUBTYPE_POWERPC_602:   puts ("powerpc602");  break;
494      case CPU_SUBTYPE_POWERPC_603:   puts ("powerpc603");  break;
495      case CPU_SUBTYPE_POWERPC_603e:  puts ("powerpc603e"); break;
496      case CPU_SUBTYPE_POWERPC_603ev: puts ("powerpc603e"); break;
497      case CPU_SUBTYPE_POWERPC_604:   puts ("powerpc604");  break;
498      case CPU_SUBTYPE_POWERPC_604e:  puts ("powerpc604e"); break;
499      case CPU_SUBTYPE_POWERPC_620:   puts ("powerpc620");  break;
500      case CPU_SUBTYPE_POWERPC_750:   puts ("powerpc750");  break;
501      case CPU_SUBTYPE_POWERPC_7400:  puts ("powerpc7400"); break;
502      case CPU_SUBTYPE_POWERPC_7450:  puts ("powerpc7450"); break;
503      /* Darwin 6.8.5 doesn't define the following */
504      case 0x8001:                    puts ("powerpc7455"); break;
505      case 0x8002:                    puts ("powerpc7457"); break;
506      case 0x8003:                    puts ("powerpc7447"); break;
507      case 100:                       puts ("powerpc970");  break;
508      }
509    }
510  return 0;
511}
512EOF
513    if ($CC_FOR_BUILD $dummy.c -o $dummy) >/dev/null 2>&1; then
514      x=`./$dummy`
515      if test -n "$x"; then
516        exact_cpu=$x
517      fi
518    fi
519    rm -f $dummy.c $dummy.o $dummy
520  fi
521  ;;
522
523sparc-*-* | sparc64-*-*)
524  # If we can recognise an actual v7 then $exact_cpu is set to "sparc" so as
525  # to short-circuit subsequent tests.
526
527  # Grep the linux kernel /proc/cpuinfo pseudo-file.
528  # A typical line is "cpu\t\t: TI UltraSparc II  (BlackBird)"
529  # See arch/sparc/kernel/cpu.c and arch/sparc64/kernel/cpu.c.
530  #
531  if test -f /proc/cpuinfo; then
532    if grep 'cpu.*Cypress' /proc/cpuinfo >/dev/null; then
533      exact_cpu="sparc"   # ie. v7
534    elif grep 'cpu.*Power-UP' /proc/cpuinfo >/dev/null; then
535      exact_cpu="sparc"   # ie. v7
536    elif grep 'cpu.*HyperSparc' /proc/cpuinfo >/dev/null; then
537      exact_cpu="sparcv8"
538    elif grep 'cpu.*SuperSparc' /proc/cpuinfo >/dev/null; then
539      exact_cpu="supersparc"
540    elif grep 'cpu.*MicroSparc' /proc/cpuinfo >/dev/null; then
541      exact_cpu="microsparc"
542    elif grep 'cpu.*MB86904' /proc/cpuinfo >/dev/null; then
543      # actually MicroSPARC-II
544      exact_cpu=microsparc
545    elif grep 'cpu.*UltraSparc T3' /proc/cpuinfo >/dev/null; then
546      exact_cpu="ultrasparct3"
547    elif grep 'cpu.*UltraSparc T2' /proc/cpuinfo >/dev/null; then
548      exact_cpu="ultrasparct2"
549    elif grep 'cpu.*UltraSparc T1' /proc/cpuinfo >/dev/null; then
550      exact_cpu="ultrasparct1"
551    elif grep 'cpu.*UltraSparc III' /proc/cpuinfo >/dev/null; then
552      exact_cpu="ultrasparc3"
553    elif grep 'cpu.*UltraSparc IIi' /proc/cpuinfo >/dev/null; then
554      exact_cpu="ultrasparc2i"
555    elif grep 'cpu.*UltraSparc II' /proc/cpuinfo >/dev/null; then
556      exact_cpu="ultrasparc2"
557    elif grep 'cpu.*UltraSparc' /proc/cpuinfo >/dev/null; then
558      exact_cpu="ultrasparc"
559    fi
560  fi
561
562  # Grep the output from sysinfo on SunOS.
563  # sysinfo has been seen living in /bin or in /usr/kvm
564  #	cpu0 is a "SuperSPARC Model 41 SPARCmodule" CPU
565  #	cpu0 is a "75 MHz TI,TMS390Z55" CPU
566  #
567  if test -z "$exact_cpu"; then
568    for i in sysinfo /usr/kvm/sysinfo; do
569      if $SHELL -c $i 2>/dev/null >conftest.dat; then
570        if grep 'cpu0 is a "SuperSPARC' conftest.dat >/dev/null; then
571          exact_cpu=supersparc
572          break
573        elif grep 'cpu0 is a .*TMS390Z5.' conftest.dat >/dev/null; then
574          # TMS390Z50 and TMS390Z55
575          exact_cpu=supersparc
576          break
577        fi
578      fi
579    done
580    rm -f conftest.dat
581  fi
582
583  # Grep the output from prtconf on Solaris.
584  # Use an explicit /usr/sbin, since that directory might not be in a normal
585  # user's path.
586  #
587  #     SUNW,UltraSPARC (driver not attached)
588  #     SUNW,UltraSPARC-II (driver not attached)
589  #     SUNW,UltraSPARC-IIi (driver not attached)
590  #     SUNW,UltraSPARC-III+ (driver not attached)
591  #     Ross,RT625 (driver not attached)
592  #     TI,TMS390Z50 (driver not attached)
593  #
594  # /usr/sbin/sysdef prints similar information, but includes all loadable
595  # cpu modules, not just the real cpu.
596  #
597  # We first try a plain prtconf, since that is known to work on older systems.
598  # But for newer T1 systems, that doesn't produce any useful output, we need
599  # "prtconf -vp" there.
600  #
601  for prtconfopt in "" "-vp"; do
602    if test -z "$exact_cpu"; then
603      if $SHELL -c "/usr/sbin/prtconf $prtconfopt" 2>/dev/null >conftest.dat; then
604	if grep 'SUNW,UltraSPARC-T3' conftest.dat >/dev/null; then
605	  exact_cpu=ultrasparct3
606	elif grep 'SUNW,UltraSPARC-T2' conftest.dat >/dev/null; then
607	  exact_cpu=ultrasparct2
608	elif grep 'SUNW,UltraSPARC-T1' conftest.dat >/dev/null; then
609	  exact_cpu=ultrasparct1
610	elif grep 'SUNW,UltraSPARC-III' conftest.dat >/dev/null; then
611	  exact_cpu=ultrasparc3
612	elif grep 'SUNW,UltraSPARC-IIi' conftest.dat >/dev/null; then
613	  exact_cpu=ultrasparc2i
614	elif grep 'SUNW,UltraSPARC-II' conftest.dat >/dev/null; then
615	  exact_cpu=ultrasparc2
616	elif grep 'SUNW,UltraSPARC' conftest.dat >/dev/null; then
617	  exact_cpu=ultrasparc
618	elif grep 'Ross,RT62.' conftest.dat >/dev/null; then
619	  # RT620, RT625, RT626 hypersparcs (v8).
620	  exact_cpu=sparcv8
621	elif grep 'TI,TMS390Z5.' conftest.dat >/dev/null; then
622	  # TMS390Z50 and TMS390Z55
623	  exact_cpu=supersparc
624	elif grep 'TI,TMS390S10' conftest.dat >/dev/null; then
625	  exact_cpu=microsparc
626	elif grep 'FMI,MB86904' conftest.dat >/dev/null; then
627	  # actually MicroSPARC-II
628	  exact_cpu=microsparc
629	fi
630      fi
631      rm -f conftest.dat
632    fi
633  done
634
635  # Grep the output from sysctl hw.model on sparc or sparc64 *BSD.
636  # Use an explicit /sbin, since that directory might not be in a normal
637  # user's path.  Example outputs,
638  #
639  #     hw.model: Sun Microsystems UltraSparc-IIi
640  #
641  if test -z "$exact_cpu"; then
642    if $SHELL -c "/sbin/sysctl hw.model" 2>/dev/null >conftest.dat; then
643      if grep -i 'UltraSparc-T3' conftest.dat >/dev/null; then
644        exact_cpu=ultrasparct3
645      elif grep -i 'UltraSparc-T2' conftest.dat >/dev/null; then
646        exact_cpu=ultrasparct2
647      elif grep -i 'UltraSparc-T1' conftest.dat >/dev/null; then
648        exact_cpu=ultrasparct1
649      elif grep -i 'UltraSparc-III' conftest.dat >/dev/null; then
650        exact_cpu=ultrasparc3
651      elif grep -i 'UltraSparc-IIi' conftest.dat >/dev/null; then
652        exact_cpu=ultrasparc2i
653      elif grep -i 'UltraSparc-II' conftest.dat >/dev/null; then
654        exact_cpu=ultrasparc2
655      elif grep -i 'UltraSparc' conftest.dat >/dev/null; then
656        exact_cpu=ultrasparc
657      elif grep 'TMS390Z5.' conftest.dat >/dev/null; then
658        # TMS390Z50 and TMS390Z55
659        exact_cpu=supersparc
660      elif grep 'TMS390S10' conftest.dat >/dev/null; then
661        exact_cpu=microsparc
662      elif grep 'MB86904' conftest.dat >/dev/null; then
663        # actually MicroSPARC-II
664        exact_cpu=microsparc
665      elif grep 'MB86907' conftest.dat >/dev/null; then
666        exact_cpu=turbosparc
667      fi
668    fi
669    rm -f conftest.dat
670  fi
671
672  # sun4m and sun4d are v8s of some sort, sun4u is a v9 of some sort
673  #
674  if test -z "$exact_cpu"; then
675    case `uname -m` in
676      sun4[md]) exact_cpu=sparcv8 ;;
677      sun4u)    exact_cpu=sparcv9 ;;
678    esac
679  fi
680  ;;
681
682
683# Recognise x86 processors using a tricky cpuid with 4 arguments, repeating
684# arguments; for x86-64 we effectively pass the 1st in rdx and the 2nd in rcx.
685# This allows the same asm to work for both standard and Windoze calling
686# conventions.
687
688i?86-*-* | amd64-*-* | x86_64-*-*)
689  cat <<EOF >${dummy}0.s
690	.globl cpuid
691	.globl _cpuid
692cpuid:
693_cpuid:
694	pushl %esi
695	pushl %ebx
696	movl 24(%esp),%eax
697	.byte 0x0f
698	.byte 0xa2
699	movl 20(%esp),%esi
700	movl %ebx,(%esi)
701	movl %edx,4(%esi)
702	movl %ecx,8(%esi)
703	popl %ebx
704	popl %esi
705	ret
706EOF
707  cat <<EOF >${dummy}1.s
708	.globl cpuid
709	.globl _cpuid
710cpuid:
711_cpuid:
712	push	%rbx
713	mov	%rdx, %r8
714	mov	%ecx, %eax
715	.byte	0x0f
716	.byte	0xa2
717	mov	%ebx, (%r8)
718	mov	%edx, 4(%r8)
719	mov	%ecx, 8(%r8)
720	pop	%rbx
721	ret
722EOF
723  cat <<EOF >${dummy}2.c
724#include <string.h>
725#include <stdio.h>
726#define CPUID(a,b) cpuid(b,a,a,b)
727#if __cplusplus
728extern "C"
729#endif
730unsigned int cpuid (int, char *, char *, int);
731int
732main ()
733{
734  char vendor_string[13];
735  char dummy_string[12];
736  long fms;
737  int family, model, stepping;
738  const char *modelstr;
739  int cpu_64bit = 0;
740
741  CPUID (vendor_string, 0);
742  vendor_string[12] = 0;
743
744  fms = CPUID (dummy_string, 1);
745
746  family = ((fms >> 8) & 0xf) + ((fms >> 20) & 0xff);
747  model = ((fms >> 4) & 0xf) + ((fms >> 12) & 0xf0);
748  stepping = fms & 0xf;
749
750  modelstr = "$guess_cpu";
751
752  /**************************************************/
753  /*** WARNING: keep this list in sync with fat.c ***/
754  /**************************************************/
755  if (strcmp (vendor_string, "GenuineIntel") == 0)
756    {
757      switch (family)
758	{
759	case 5:
760	  if (model <= 2)	modelstr = "pentium";
761	  else if (model >= 4)	modelstr = "pentiummmx";
762	  break;
763	case 6:
764	  if (model <= 1)		modelstr = "pentiumpro";
765	  else if (model <= 6)		modelstr = "pentium2";
766	  else if (model <= 8)		modelstr = "pentium3";
767	  else if (model <= 9)		modelstr = "pentiumm";
768	  else if (model <= 0x0c)	modelstr = "pentium3";
769	  else if (model <= 0x0e)	modelstr = "pentiumm";
770	  else if (model <= 0x19)	cpu_64bit = 1, modelstr = "core2";
771	  else if (model == 0x1a)	cpu_64bit = 1, modelstr = "coreinhm"; /* NHM Gainestown */
772	  else if (model == 0x1c)	cpu_64bit = 1, modelstr = "atom";  /* Silverthorne */
773	  else if (model == 0x1d)	cpu_64bit = 1, modelstr = "core2"; /* PNR Dunnington */
774	  else if (model == 0x1e)	cpu_64bit = 1, modelstr = "coreinhm"; /* NHM Lynnfield/Jasper */
775	  else if (model == 0x25)	cpu_64bit = 1, modelstr = "coreiwsm"; /* WSM Clarkdale/Arrandale */
776	  else if (model == 0x26)	cpu_64bit = 1, modelstr = "atom";  /* Lincroft */
777	  else if (model == 0x27)	cpu_64bit = 1, modelstr = "atom";  /* Saltwell */
778	  else if (model == 0x2a)	cpu_64bit = 1, modelstr = "coreisbr"; /* SB */
779	  else if (model == 0x2c)	cpu_64bit = 1, modelstr = "coreiwsm"; /* WSM Gulftown */
780	  else if (model == 0x2d)	cpu_64bit = 1, modelstr = "coreisbr"; /* SBC-EP */
781	  else if (model == 0x2e)	cpu_64bit = 1, modelstr = "coreinhm"; /* NHM Beckton */
782	  else if (model == 0x2f)	cpu_64bit = 1, modelstr = "coreiwsm"; /* WSM Eagleton */
783	  else cpu_64bit = 1, modelstr = "corei"; /* default */
784	  break;
785	case 15:
786	  cpu_64bit = 1, modelstr = "pentium4";
787	  break;
788	}
789    }
790  else if (strcmp (vendor_string, "AuthenticAMD") == 0)
791    {
792      switch (family)
793	{
794	case 5:
795	  if (model <= 3)	modelstr = "k5";
796	  else if (model <= 7)	modelstr = "k6";
797	  else if (model == 8)	modelstr = "k62";
798	  else if (model == 9)	modelstr = "k63";
799	  else if (model == 10) modelstr = "geode";
800	  else if (model == 13) modelstr = "k63";
801	  break;
802	case 6:
803	  modelstr = "athlon";
804	  break;
805	case 15:		/* K8, K9 */
806	  cpu_64bit = 1, modelstr = "k8";
807	  break;
808	case 16:		/* K10 */
809	  cpu_64bit = 1, modelstr = "k10";
810	  break;
811	case 17:		/* Hybrid k8/k10, claim k8 */
812	  cpu_64bit = 1, modelstr = "k8";
813	  break;
814	case 18:		/* Llano, uses K10 core */
815	  cpu_64bit = 1, modelstr = "k10";
816	  break;
817	case 19:		/* AMD Internal, assume future K10 */
818	  cpu_64bit = 1, modelstr = "k10";
819	  break;
820	case 20:		/* Bobcat */
821	  cpu_64bit = 1, modelstr = "bobcat";
822	  break;
823	case 21:		/* Bulldozer */
824	  cpu_64bit = 1, modelstr = "bulldozer";
825	  break;
826	case 22:		/* AMD Internal, assume future bulldozer */
827	  cpu_64bit = 1, modelstr = "bulldozer";
828	  break;
829	}
830    }
831  else if (strcmp (vendor_string, "CyrixInstead") == 0)
832    {
833      /* Should recognize Cyrix' processors too.  */
834    }
835  else if (strcmp (vendor_string, "CentaurHauls") == 0)
836    {
837      switch (family)
838	{
839	case 6:
840	  if (model < 9)	modelstr = "viac3";
841	  else if (model < 15)	modelstr = "viac32";
842	  else			cpu_64bit = 1, modelstr = "nano";
843	  break;
844	}
845    }
846
847  /* If our cpuid-based exact guess is more conservative than the previous
848     guess, revert.  This is of course wrong, but it can happen in an emulator,
849     so this workaround allows for successful 64-bit builds.  */
850  if (strcmp ("$guess_cpu", "x86_64") == 0 && ! cpu_64bit)
851    modelstr = "$guess_cpu";
852
853  printf ("%s", modelstr);
854  return 0;
855}
856EOF
857
858  if ($CC_FOR_BUILD ${dummy}1.s ${dummy}2.c -o $dummy) >/dev/null 2>&1; then
859    # On 80386 and early 80486 cpuid is not available and will result in a
860    # SIGILL message, hence 2>/dev/null.
861    #
862    # On i386-unknown-freebsd4.9, "/bin/sh -c ./dummy" seems to send an
863    # "Illegal instruction (core dumped)" message to stdout, so we test $?
864    # to check if the program run was successful.
865    #
866    x=`$SHELL -c ./$dummy 2>/dev/null`
867    if test $? = 0 && test -n "$x"; then
868      exact_cpu=$x
869    fi
870  fi
871
872  if test -z "$exact_cpu"; then
873  if ($CC_FOR_BUILD ${dummy}0.s ${dummy}2.c -o $dummy) >/dev/null 2>&1; then
874    # On 80386 and early 80486 cpuid is not available and will result in a
875    # SIGILL message, hence 2>/dev/null.
876    #
877    # On i386-unknown-freebsd4.9, "/bin/sh -c ./dummy" seems to send an
878    # "Illegal instruction (core dumped)" message to stdout, so we test $?
879    # to check if the program run was successful.
880    #
881    x=`$SHELL -c ./$dummy 2>/dev/null`
882    if test $? = 0 && test -n "$x"; then
883      exact_cpu=$x
884    fi
885  fi
886  fi
887
888  # We need to remove some .o files here since lame C compilers
889  # generate these even when not asked.
890  rm -f ${dummy}0.s ${dummy}0.o ${dummy}1.s ${dummy}1.o ${dummy}2.c ${dummy}2.o $dummy
891  ;;
892
893s390*-*-*)
894  model=`grep "^processor 0: version =" /proc/cpuinfo | sed -e 's/.*machine = //'`
895  case $model in
896    2064 | 2066) zcpu="z900" ;;
897    2084 | 2086) zcpu="z990" ;;
898    2094 | 2096) zcpu="z9"   ;;
899    2097 | 2098) zcpu="z10"  ;;
900    2817 | 2818 | *) zcpu="z196" ;;
901  esac
902  case "$guess_full" in
903    s390x-*-*) exact_cpu=${zcpu}    ;;
904    s390-*-*)  exact_cpu=${zcpu}esa ;;
905  esac
906  ;;
907
908esac
909
910
911
912# -------------------------------------------------------------------------
913# Use an exact cpu, if possible
914
915if test -n "$exact_cpu"; then
916  echo "$exact_cpu$guess_rest"
917else
918  echo "$guess_full"
919fi
920exit 0
921
922
923
924# Local variables:
925# fill-column: 76
926# End:
927