Skip to content
Snippets Groups Projects
Commit 9b867ab9 authored by Per's avatar Per
Browse files

POC for cortex-m-rt like code gen

parent 52fdbbb0
No related branches found
No related tags found
No related merge requests found
/target
**/*.rs.bk
...@@ -4,13 +4,16 @@ ...@@ -4,13 +4,16 @@
"version": "2.0.0", "version": "2.0.0",
"tasks": [ "tasks": [
{ {
"label": "cargo run --example main", "label": "cargo expand --example trust > expanded.rs",
"type": "shell", "type": "shell",
"command": "cargo run --example main", "command": "cargo expand --example trust > expanded.rs",
"group": { "group": {
"kind": "build", "kind": "build",
"isDefault": true "isDefault": true
} },
"problemMatcher": [
"$rustc"
]
}, },
{ {
"label": "cargo run --example trust", "label": "cargo run --example trust",
......
...@@ -3,22 +3,32 @@ use proc_collect::*; ...@@ -3,22 +3,32 @@ use proc_collect::*;
#[trust] #[trust]
mod kalleanka { mod kalleanka {
fn hello() { fn hello() {
println!("in trust hello");
ext_hello(); ext_hello();
} }
#[entry] #[entry]
fn entry() {} #[inline(always)]
fn entry() {
hello();
}
#[interrupt] #[interrupt]
fn interrupt() {} fn i1() {
println!("i1");
}
#[interrupt]
fn i2() {
println!("i2");
}
#[exception] #[exception]
fn exception() {} fn exception() {
println!("exc");
}
} }
fn ext_hello() { fn ext_hello() {
println!("hello"); println!("ext_hello");
}
fn main() {
hello();
} }
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use proc_collect::*;
fn hello() {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["in trust hello\n"],
&match () {
() => [],
},
));
};
ext_hello();
}
#[inline(always)]
fn entry() {
hello();
}
fn i1() {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["i1\n"],
&match () {
() => [],
},
));
};
}
fn i2() {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["i2\n"],
&match () {
() => [],
},
));
};
}
fn exception() {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["exc\n"],
&match () {
() => [],
},
));
};
}
fn main() {
let _ = entry();
let _ = i1();
let _ = i2();
let _ = exception();
}
fn ext_hello() {
{
::std::io::_print(::core::fmt::Arguments::new_v1(
&["ext_hello\n"],
&match () {
() => [],
},
));
};
}
...@@ -6,36 +6,75 @@ use std::fs::{File, OpenOptions}; ...@@ -6,36 +6,75 @@ use std::fs::{File, OpenOptions};
use std::io::prelude::*; use std::io::prelude::*;
use std::iter::FromIterator; use std::iter::FromIterator;
use syn::parse::{Parse, ParseStream}; use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, Attribute, Ident, Item, ItemMod, Result, Token}; use syn::{parse_macro_input, Attribute, Ident, Item, ItemFn, ItemMod, Result, Token};
// use syn::{parse_macro_input, Attribute, Ident, ItemMod, Result, Token}; // use syn::{parse_macro_input, Attribute, Ident, ItemMod, Result, Token};
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn entry(_args: TokenStream, input: TokenStream) -> TokenStream { pub fn entry(_args: TokenStream, input: TokenStream) -> TokenStream {
let items = parse_macro_input!(input as FnItem).items; let fn_item = parse_macro_input!(input as ItemFn);
(quote! { #(#items)* }).into() (quote! { #fn_item }).into()
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn interrupt(_args: TokenStream, input: TokenStream) -> TokenStream { pub fn interrupt(_args: TokenStream, input: TokenStream) -> TokenStream {
input let fn_item = parse_macro_input!(input as ItemFn);
(quote! { #fn_item }).into()
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn exception(_args: TokenStream, input: TokenStream) -> TokenStream { pub fn exception(_args: TokenStream, input: TokenStream) -> TokenStream {
input let fn_item = parse_macro_input!(input as ItemFn);
(quote! { #fn_item }).into()
} }
#[derive(Debug)] #[derive(Debug)]
struct TrustMod { struct TrustMod {
items: Vec<Item>, items: Vec<Item>,
entries: Vec<Entry>,
}
#[derive(Debug)]
enum Entry {
Entry(Ident),
Interrupt(Ident),
Exception(Ident),
} }
impl Parse for TrustMod { impl Parse for TrustMod {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
let module: ItemMod = input.parse()?; let module: ItemMod = input.parse()?;
match module.content { match module.content {
Some((_, items)) => Ok(TrustMod { items }), Some((_, items)) => {
let mut v = vec![];
let mut e = vec![];
for i in items {
match i {
Item::Fn(ref item_fn) => {
for a in &item_fn.attrs {
for s in &a.path.segments {
match s.ident.to_string().as_str() {
"entry" => e.push(Entry::Entry(item_fn.sig.ident.clone())),
"interrupt" => {
e.push(Entry::Interrupt(item_fn.sig.ident.clone()))
}
"exception" => {
e.push(Entry::Exception(item_fn.sig.ident.clone()))
}
_ => (),
}
}
}
}
_ => (),
}
v.push(i);
}
Ok(TrustMod {
items: v,
entries: e,
})
}
_ => panic!("illegal trust module"), _ => panic!("illegal trust module"),
} }
} }
...@@ -43,6 +82,24 @@ impl Parse for TrustMod { ...@@ -43,6 +82,24 @@ impl Parse for TrustMod {
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn trust(_args: TokenStream, input: TokenStream) -> TokenStream { pub fn trust(_args: TokenStream, input: TokenStream) -> TokenStream {
let items = parse_macro_input!(input as TrustMod).items; let trust_mod = parse_macro_input!(input as TrustMod);
(quote! { #(#items)* }).into() let items = trust_mod.items;
let mut calls = vec![];
for e in trust_mod.entries {
let id = match e {
Entry::Entry(id) => id,
Entry::Interrupt(id) => id,
Entry::Exception(id) => id,
};
calls.push(quote! { let _ = #id(); });
}
(quote! {
#(#items)*
fn main() {
#(#calls)*
}
})
.into()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment