1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#![deny(rust_2018_compatibility)]
#![deny(rust_2018_idioms)]
#![deny(warnings)]
#![recursion_limit = "128"]


use core::sync::atomic::{AtomicUsize, Ordering};
use proc_macro::TokenStream;

use proc_macro2::Span;
use quote::quote;
use syn::{parse, parse_macro_input, ItemStatic};

/// An attribute to place a static variable in shared memory
///
/// This static variable will refer to the same memory location on all cores
#[proc_macro_attribute]
pub fn shared(args: TokenStream, input: TokenStream) -> TokenStream {
    static COUNT: AtomicUsize = AtomicUsize::new(0);

    if !args.is_empty() {
        return parse::Error::new(Span::call_site(), "`#[shared]` takes no arguments")
            .to_compile_error()
            .into();
    }

    let item = parse_macro_input!(input as ItemStatic);

    let attrs = &item.attrs;
    let expr = &item.expr;
    let ident = &item.ident;
    let symbol = format!("{}.{}", ident, COUNT.fetch_add(1, Ordering::AcqRel));
    let ty = &item.ty;
    let vis = &item.vis;
    if item.mutability.is_some() {
        quote!(
            #[cfg(not(target_arch = "arm"))]
            compile_error!("Only the ARM architecture is supported at the moment");

            #(#attrs)*
            #[cfg(microamp)]
            #[link_section = ".shared"]
            #[export_name = #symbol]
            static mut #ident: #ty = {
                fn assert() {
                    microamp::export::is_data::<#ty>();
                }

                #expr
            };

            #[cfg(not(microamp))]
            extern "C" {
                #[link_name = #symbol]
                #vis static mut #ident: #ty;
            }
        )
        .into()
    } else {
        quote!(
            #[cfg(not(target_arch = "arm"))]
            compile_error!("Only the ARM architecture is supported at the moment");

            #(#attrs)*
            #[cfg(microamp)]
            #[link_section = ".shared"]
            #[no_mangle]
            static #ident: #ty = {
                fn assert() {
                    microamp::export::is_data::<#ty>();
                }

                #expr
            };

            #[cfg(not(microamp))]
            #vis struct #ident;

            #[cfg(not(microamp))]
            impl core::ops::Deref for #ident {
                type Target = #ty;

                fn deref(&self) -> &#ty {
                    #[inline(always)]
                    fn assert<T>() where T: Sync {}
                    assert::<#ty>();

                    extern "C" {
                        static #ident: #ty;
                    }

                    unsafe { &#ident }
                }
            }
        )
        .into()
    }
}