Sfoglia il codice sorgente

Do not require const-fn and asm features

Vadim Kaushan 6 anni fa
parent
commit
41378757c0
5 ha cambiato i file con 75 aggiunte e 49 eliminazioni
  1. 1 0
      Cargo.toml
  2. 1 1
      ci/script.sh
  3. 16 7
      src/asm.rs
  4. 1 2
      src/lib.rs
  5. 56 39
      src/register/macros.rs

+ 1 - 0
Cargo.toml

@@ -13,4 +13,5 @@ bare-metal = "0.2.0"
 bit_field = "0.9.0"
 
 [features]
+const-fn = ["bare-metal/const-fn"]
 inline-asm = []

+ 1 - 1
ci/script.sh

@@ -4,7 +4,7 @@ main() {
     cargo check --target $TARGET
 
     if [ $TRAVIS_RUST_VERSION = nightly ]; then
-        cargo check --target $TARGET --features inline-asm
+        cargo check --target $TARGET --features 'const-fn inline-asm'
     fi
 }
 

+ 16 - 7
src/asm.rs

@@ -5,8 +5,12 @@ macro_rules! instruction {
         #[inline]
         pub unsafe fn $fnname() {
             match () {
-                #[cfg(riscv)]
+                #[cfg(all(riscv, feature = "inline-asm"))]
                 () => asm!($asm :::: "volatile"),
+
+                #[cfg(all(riscv, not(feature = "inline-asm")))]
+                () => unimplemented!(),
+
                 #[cfg(not(riscv))]
                 () => unimplemented!(),
             }
@@ -22,11 +26,16 @@ instruction!(sfence_vma_all, "sfence.vma");
 
 
 #[inline]
-#[cfg(riscv)]
+#[allow(unused_variables)]
 pub unsafe fn sfence_vma(asid: usize, addr: usize) {
-    asm!("sfence.vma $0, $1" :: "r"(asid), "r"(addr) :: "volatile");
-}
+    match () {
+        #[cfg(all(riscv, feature = "inline-asm"))]
+        () => asm!("sfence.vma $0, $1" :: "r"(asid), "r"(addr) :: "volatile"),
 
-#[inline]
-#[cfg(not(riscv))]
-pub fn sfence_vma(_asid: usize, _addr: usize) {}
+        #[cfg(all(riscv, not(feature = "inline-asm")))]
+        () => unimplemented!(),
+
+        #[cfg(not(riscv))]
+        () => unimplemented!(),
+    }
+}

+ 1 - 2
src/lib.rs

@@ -8,8 +8,7 @@
 
 #![no_std]
 #![deny(warnings)]
-#![feature(asm)]
-#![feature(const_fn)]
+#![cfg_attr(feature = "inline-asm", feature(asm))]
 
 extern crate bare_metal;
 extern crate bit_field;

+ 56 - 39
src/register/macros.rs

@@ -2,17 +2,21 @@ macro_rules! read_csr {
     ($csr_number:expr) => {
         /// Reads the CSR
         #[inline]
-        #[cfg(riscv)]
         unsafe fn _read() -> usize {
-            let r: usize;
-            asm!("csrrs $0, $1, x0" : "=r"(r) : "i"($csr_number) :: "volatile");
-            r
-        }
+            match () {
+                #[cfg(all(riscv, feature = "inline-asm"))]
+                () => {
+                    let r: usize;
+                    asm!("csrrs $0, $1, x0" : "=r"(r) : "i"($csr_number) :: "volatile");
+                    r
+                }
 
-        #[inline]
-        #[cfg(not(riscv))]
-        unsafe fn _read() -> usize {
-            unimplemented!()
+                #[cfg(all(riscv, not(feature = "inline-asm")))]
+                () => unimplemented!(),
+
+                #[cfg(not(riscv))]
+                () => unimplemented!(),
+            }
         }
     };
 }
@@ -21,17 +25,21 @@ macro_rules! read_csr_rv32 {
     ($csr_number:expr) => {
         /// Reads the CSR
         #[inline]
-        #[cfg(riscv32)]
         unsafe fn _read() -> usize {
-            let r: usize;
-            asm!("csrrs $0, $1, x0" : "=r"(r) : "i"($csr_number) :: "volatile");
-            r
-        }
+            match () {
+                #[cfg(all(riscv32, feature = "inline-asm"))]
+                () => {
+                    let r: usize;
+                    asm!("csrrs $0, $1, x0" : "=r"(r) : "i"($csr_number) :: "volatile");
+                    r
+                }
 
-        #[inline]
-        #[cfg(not(riscv32))]
-        unsafe fn _read() -> usize {
-            unimplemented!()
+                #[cfg(all(riscv32, not(feature = "inline-asm")))]
+                () => unimplemented!(),
+
+                #[cfg(not(riscv32))]
+                () => unimplemented!(),
+            }
         }
     };
 }
@@ -76,15 +84,18 @@ macro_rules! write_csr {
     ($csr_number:expr) => {
         /// Writes the CSR
         #[inline]
-        #[cfg(riscv)]
+        #[allow(unused_variables)]
         unsafe fn _write(bits: usize) {
-            asm!("csrrw x0, $1, $0" :: "r"(bits), "i"($csr_number) :: "volatile");
-        }
+            match () {
+                #[cfg(all(riscv, feature = "inline-asm"))]
+                () => asm!("csrrw x0, $1, $0" :: "r"(bits), "i"($csr_number) :: "volatile"),
 
-        #[inline]
-        #[cfg(not(riscv))]
-        unsafe fn _write(_bits: usize) {
-            unimplemented!()
+                #[cfg(all(riscv, not(feature = "inline-asm")))]
+                () => unimplemented!(),
+
+                #[cfg(not(riscv))]
+                () => unimplemented!(),
+            }
         }
     };
 }
@@ -105,15 +116,18 @@ macro_rules! set {
     ($csr_number:expr) => {
         /// Set the CSR
         #[inline]
-        #[cfg(riscv)]
+        #[allow(unused_variables)]
         unsafe fn _set(bits: usize) {
-            asm!("csrrs x0, $1, $0" :: "r"(bits), "i"($csr_number) :: "volatile");
-        }
+            match () {
+                #[cfg(all(riscv, feature = "inline-asm"))]
+                () => asm!("csrrs x0, $1, $0" :: "r"(bits), "i"($csr_number) :: "volatile"),
 
-        #[inline]
-        #[cfg(not(riscv))]
-        unsafe fn _set(_bits: usize) {
-            unimplemented!()
+                #[cfg(all(riscv, not(feature = "inline-asm")))]
+                () => unimplemented!(),
+
+                #[cfg(not(riscv))]
+                () => unimplemented!(),
+            }
         }
     };
 }
@@ -122,15 +136,18 @@ macro_rules! clear {
     ($csr_number:expr) => {
         /// Clear the CSR
         #[inline]
-        #[cfg(riscv)]
+        #[allow(unused_variables)]
         unsafe fn _clear(bits: usize) {
-            asm!("csrrc x0, $1, $0" :: "r"(bits), "i"($csr_number) :: "volatile");
-        }
+            match () {
+                #[cfg(all(riscv, feature = "inline-asm"))]
+                () => asm!("csrrc x0, $1, $0" :: "r"(bits), "i"($csr_number) :: "volatile"),
 
-        #[inline]
-        #[cfg(not(riscv))]
-        unsafe fn _clear(_bits: usize) {
-            unimplemented!()
+                #[cfg(all(riscv, not(feature = "inline-asm")))]
+                () => unimplemented!(),
+
+                #[cfg(not(riscv))]
+                () => unimplemented!(),
+            }
         }
     };
 }