Debian Codegraph @

rpython/jit/backend/arm/detect.py

raw symbols SCIP
1 import os
2
3 from rpython.translator.tool.cbuild import ExternalCompilationInfo
4 from rpython.rtyper.tool import rffi_platform
5 from rpython.rlib.clibffi import FFI_DEFAULT_ABI, FFI_SYSV, FFI_VFP
6 from rpython.translator.platform import CompilationError
7 from rpython.rlib.debug import debug_print, debug_start, debug_stop
8
9 eci = ExternalCompilationInfo(
10 post_include_bits=["""
11 // we need to disable optimizations so the compiler does not remove this
12 // function when checking if the file compiles
13 static void __attribute__((optimize("O0"))) pypy__arm_has_vfp()
14 {
15 asm volatile("VMOV s0, s1");
16 }
17 """])
18
19 def detect_hardfloat():
20 return FFI_DEFAULT_ABI == FFI_VFP
21
22 def detect_float():
23 """Check for hardware float support
24 we try to compile a function containing a VFP instruction, and if the
25 compiler accepts it we assume we are fine
26 """
27 try:
28 rffi_platform.verify_eci(eci)
29 return True
30 except CompilationError:
31 return False
32
33
34 def detect_arch_version(filename="/proc/cpuinfo"):
35 fd = os.open(filename, os.O_RDONLY, 0644)
36 n = 0
37 debug_start("jit-backend-arch")
38 try:
39 buf = os.read(fd, 2048)
40 if not buf:
41 n = 6 # we assume ARMv6 as base case
42 debug_print("Could not detect ARM architecture "
43 "version, assuming", "ARMv%d" % n)
44 finally:
45 os.close(fd)
46 # "Processor : ARMv%d-compatible processor rev 7 (v6l)"
47 i = buf.find('ARMv')
48 if i == -1:
49 n = 6
50 debug_print("Could not detect architecture version, "
51 "falling back to", "ARMv%d" % n)
52 else:
53 n = int(buf[i + 4])
54
55 if n < 6:
56 raise ValueError("Unsupported ARM architecture version")
57
58 debug_print("Detected", "ARMv%d" % n)
59
60 if n > 7:
61 n = 7
62 debug_print("Architecture version not explicitly supported, "
63 "falling back to", "ARMv%d" % n)
64 debug_stop("jit-backend-arch")
65 return n
66
67
68 # Once we can rely on the availability of glibc >= 2.16, replace this with:
69 # from rpython.rtyper.lltypesystem import lltype, rffi
70 # getauxval = rffi.llexternal("getauxval", [lltype.Unsigned], lltype.Unsigned)
71 def getauxval(type_, filename='/proc/self/auxv'):
72 fd = os.open(filename, os.O_RDONLY, 0644)
73
74 buf_size = 2048
75 struct_size = 8 # 2x uint32
76 try:
77 buf = os.read(fd, buf_size)
78 finally:
79 os.close(fd)
80
81 # decode chunks of 8 bytes (a_type, a_val), and
82 # return the a_val whose a_type corresponds to type_,
83 # or zero if not found.
84 i = 0
85 while i <= buf_size - struct_size:
86 # We only support little-endian ARM
87 a_type = (ord(buf[i]) |
88 (ord(buf[i+1]) << 8) |
89 (ord(buf[i+2]) << 16) |
90 (ord(buf[i+3]) << 24))
91 a_val = (ord(buf[i+4]) |
92 (ord(buf[i+5]) << 8) |
93 (ord(buf[i+6]) << 16) |
94 (ord(buf[i+7]) << 24))
95 i += struct_size
96 if a_type == type_:
97 return a_val
98
99 return 0
100
101
102 def detect_neon():
103 AT_HWCAP = 16
104 HWCAP_NEON = 1 << 12
105 hwcap = getauxval(AT_HWCAP)
106 return bool(hwcap & HWCAP_NEON)