Skip to content

Commit 547a8bc

Browse files
authored
[clang][x86] Support -masm=intel in cpuid.h (#127331)
Fixes #127271 Testing mostly done in Compiler Explorer https://godbolt.org/z/q1h3ohxr7
1 parent b36a18d commit 547a8bc

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

clang/lib/Headers/cpuid.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,18 @@
267267
: "0"(__leaf), "2"(__count))
268268
#else
269269
/* x86-64 uses %rbx as the base register, so preserve it. */
270-
#define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
271-
__asm(" xchgq %%rbx,%q1\n" \
272-
" cpuid\n" \
273-
" xchgq %%rbx,%q1" \
274-
: "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
270+
#define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
271+
__asm(" xchg{q|} {%%|}rbx,%q1\n" \
272+
" cpuid\n" \
273+
" xchg{q|} {%%|}rbx,%q1" \
274+
: "=a"(__eax), "=r"(__ebx), "=c"(__ecx), "=d"(__edx) \
275275
: "0"(__leaf))
276276

277-
#define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
278-
__asm(" xchgq %%rbx,%q1\n" \
279-
" cpuid\n" \
280-
" xchgq %%rbx,%q1" \
281-
: "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
277+
#define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
278+
__asm(" xchg{q|} {%%|}rbx,%q1\n" \
279+
" cpuid\n" \
280+
" xchg{q|} {%%|}rbx,%q1" \
281+
: "=a"(__eax), "=r"(__ebx), "=c"(__ecx), "=d"(__edx) \
282282
: "0"(__leaf), "2"(__count))
283283
#endif
284284

@@ -289,20 +289,22 @@ static __inline unsigned int __get_cpuid_max (unsigned int __leaf,
289289
#ifdef __i386__
290290
int __cpuid_supported;
291291

292-
__asm(" pushfl\n"
293-
" popl %%eax\n"
294-
" movl %%eax,%%ecx\n"
295-
" xorl $0x00200000,%%eax\n"
296-
" pushl %%eax\n"
297-
" popfl\n"
298-
" pushfl\n"
299-
" popl %%eax\n"
300-
" movl $0,%0\n"
301-
" cmpl %%eax,%%ecx\n"
292+
__asm(" pushf{l|d}\n"
293+
" pop{l|} {%%|}eax\n"
294+
" mov{l|} {%%eax,%%ecx|ecx,eax}\n"
295+
" xor{l|} {$0x00200000,%%eax|eax,0x00200000}\n"
296+
" push{l|} {%%|}eax\n"
297+
" popf{l|d}\n"
298+
" pushf{l|d}\n"
299+
" pop{l|} {%%|}eax\n"
300+
" mov{l|} {$0,%0|%0,0}\n"
301+
" cmp{l|} {%%eax,%%ecx|ecx,eax}\n"
302302
" je 1f\n"
303-
" movl $1,%0\n"
303+
" mov{l|} {$1,%0|%0,1}\n"
304304
"1:"
305-
: "=r" (__cpuid_supported) : : "eax", "ecx");
305+
: "=r"(__cpuid_supported)
306+
:
307+
: "eax", "ecx");
306308
if (!__cpuid_supported)
307309
return 0;
308310
#endif

clang/test/Headers/cpuid.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include <cpuid.h>
55
#include <cpuid.h> // Make sure multiple inclusion protection works.
66

7-
// CHECK-64: {{.*}} call { i32, i32, i32, i32 } asm " xchgq %rbx,${1:q}\0A cpuid\0A xchgq %rbx,${1:q}", "={ax},=r,={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}})
8-
// CHECK-64: {{.*}} call { i32, i32, i32, i32 } asm " xchgq %rbx,${1:q}\0A cpuid\0A xchgq %rbx,${1:q}", "={ax},=r,={cx},={dx},0,2,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}}, i32 %{{[a-z0-9]+}})
9-
// CHECK-64: {{.*}} call { i32, i32, i32, i32 } asm " xchgq %rbx,${1:q}\0A cpuid\0A xchgq %rbx,${1:q}", "={ax},=r,={cx},={dx},0,2,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}}, i32 %{{[a-z0-9]+}})
7+
// CHECK-64: {{.*}} call { i32, i32, i32, i32 } asm " xchg$(q$|$) $(%$|$)rbx,${1:q}\0A cpuid\0A xchg$(q$|$) $(%$|$)rbx,${1:q}", "={ax},=r,={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}})
8+
// CHECK-64: {{.*}} call { i32, i32, i32, i32 } asm " xchg$(q$|$) $(%$|$)rbx,${1:q}\0A cpuid\0A xchg$(q$|$) $(%$|$)rbx,${1:q}", "={ax},=r,={cx},={dx},0,2,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}}, i32 %{{[a-z0-9]+}})
9+
// CHECK-64: {{.*}} call { i32, i32, i32, i32 } asm " xchg$(q$|$) $(%$|$)rbx,${1:q}\0A cpuid\0A xchg$(q$|$) $(%$|$)rbx,${1:q}", "={ax},=r,={cx},={dx},0,2,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}}, i32 %{{[a-z0-9]+}})
1010

1111
// CHECK-32: {{.*}} call { i32, i32, i32, i32 } asm "cpuid", "={ax},={bx},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}})
1212
// CHECK-32: {{.*}} call { i32, i32, i32, i32 } asm "cpuid", "={ax},={bx},={cx},={dx},0,2,~{dirflag},~{fpsr},~{flags}"(i32 %{{[a-z0-9]+}}, i32 %{{[a-z0-9]+}})

0 commit comments

Comments
 (0)