Commit 771dadc5e0

Jacob Young <jacobly0@users.noreply.github.com>
2022-11-02 00:57:14
x86: cleanup inline asm
Multiple outputs work now, so use that instead of deleting clobbers.
1 parent 9b2db56
Changed files (1)
lib
std
zig
system
lib/std/zig/system/x86.zig
@@ -528,26 +528,20 @@ const CpuidLeaf = packed struct {
 };
 
 fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf {
-    // Workaround for https://github.com/ziglang/zig/issues/215
-    // Inline assembly in zig only supports one output,
-    // so we pass a pointer to the struct.
-    var cpuid_leaf: CpuidLeaf = undefined;
-
     // valid for both x86 and x86_64
-    asm volatile (
-        \\ cpuid
-        \\ movl %%eax, 0(%[leaf_ptr])
-        \\ movl %%ebx, 4(%[leaf_ptr])
-        \\ movl %%ecx, 8(%[leaf_ptr])
-        \\ movl %%edx, 12(%[leaf_ptr])
-        :
-        : [leaf_id] "{eax}" (leaf_id),
-          [subid] "{ecx}" (subid),
-          [leaf_ptr] "r" (&cpuid_leaf),
-        : "ebx", "edx" // "eax" and "ecx" are already inputs
+    var eax: u32 = undefined;
+    var ebx: u32 = undefined;
+    var ecx: u32 = undefined;
+    var edx: u32 = undefined;
+    asm volatile ("cpuid"
+        : [_] "={eax}" (eax),
+          [_] "={ebx}" (ebx),
+          [_] "={ecx}" (ecx),
+          [_] "={edx}" (edx),
+        : [_] "{eax}" (leaf_id),
+          [_] "{ecx}" (subid),
     );
-
-    return cpuid_leaf;
+    return .{ .eax = eax, .ebx = ebx, .ecx = ecx, .edx = edx };
 }
 
 // Read control register 0 (XCR0). Used to detect features such as AVX.
@@ -555,8 +549,8 @@ fn getXCR0() u32 {
     return asm volatile (
         \\ xor %%ecx, %%ecx
         \\ xgetbv
-        : [ret] "={eax}" (-> u32),
+        : [_] "={eax}" (-> u32),
         :
-        : "edx", "ecx" // "eax" is already an output
+        : "edx", "ecx"
     );
 }