Compare commits
3 Commits
3eea4d811a
...
2cbf871d09
Author | SHA1 | Date |
---|---|---|
Drew Galbraith | 2cbf871d09 | |
Drew Galbraith | 8f35d38e93 | |
Drew Galbraith | ccd13fecf1 |
|
@ -34,6 +34,24 @@ dependencies = [
|
|||
"linked_list_allocator",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
|
@ -49,12 +67,38 @@ dependencies = [
|
|||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "testbed"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mammoth",
|
||||
"yellowstone",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "yellowstone"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mammoth",
|
||||
"yunq",
|
||||
"yunq-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -62,4 +106,14 @@ name = "yunq"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mammoth",
|
||||
"yunq-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yunq-derive"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[workspace]
|
||||
|
||||
members = [
|
||||
"lib/mammoth", "lib/yunq", "usr/testbed",
|
||||
"lib/mammoth", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "usr/testbed",
|
||||
]
|
||||
|
||||
# the profile used for `cargo build`
|
||||
|
|
|
@ -146,9 +146,7 @@ macro_rules! debug {
|
|||
($fmt:literal, $($val:expr),+) => {{
|
||||
use core::fmt::Write as _;
|
||||
use alloc::string::String;
|
||||
// TODO: Find a way to do this so we don't have to import writer.
|
||||
// We can't fully qualify this if we want to use it in this crate.
|
||||
let mut w = Writer::new();
|
||||
let mut w = $crate::syscall::Writer::new();
|
||||
write!(&mut w, $fmt, $($val),*).expect("Failed to format");
|
||||
let s: String = w.into();
|
||||
debug(&s);
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "yellowstone"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
mammoth = { path = "../mammoth" }
|
||||
yunq = {path = "../yunq"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
#![no_std]
|
||||
extern crate alloc;
|
||||
use alloc::string::String;
|
||||
use alloc::string::ToString;
|
||||
use mammoth::syscall::z_cap_t;
|
||||
use mammoth::syscall::ZError;
|
||||
use yunq::ByteBuffer;
|
||||
use yunq::YunqMessage;
|
||||
use yunq_derive::YunqMessage;
|
||||
#[derive(YunqMessage)]
|
||||
pub struct RegisterEndpointRequest {
|
||||
pub endpoint_name: String,
|
||||
pub endpoint_capability: z_cap_t,
|
||||
}
|
||||
#[derive(YunqMessage)]
|
||||
pub struct GetEndpointRequest {
|
||||
pub endpoint_name: String,
|
||||
}
|
||||
#[derive(YunqMessage)]
|
||||
pub struct Endpoint {
|
||||
pub endpoint: z_cap_t,
|
||||
}
|
||||
#[derive(YunqMessage)]
|
||||
pub struct AhciInfo {
|
||||
pub ahci_region: z_cap_t,
|
||||
pub region_length: u64,
|
||||
}
|
||||
#[derive(YunqMessage)]
|
||||
pub struct XhciInfo {
|
||||
pub xhci_region: z_cap_t,
|
||||
pub region_length: u64,
|
||||
}
|
||||
#[derive(YunqMessage)]
|
||||
pub struct FramebufferInfo {
|
||||
pub address_phys: u64,
|
||||
pub width: u64,
|
||||
pub height: u64,
|
||||
pub pitch: u64,
|
||||
pub bpp: u64,
|
||||
pub memory_model: u64,
|
||||
pub red_mask_size: u64,
|
||||
pub red_mask_shift: u64,
|
||||
pub green_mask_size: u64,
|
||||
pub green_mask_shift: u64,
|
||||
pub blue_mask_size: u64,
|
||||
pub blue_mask_shift: u64,
|
||||
}
|
||||
#[derive(YunqMessage)]
|
||||
pub struct DenaliInfo {
|
||||
pub denali_endpoint: z_cap_t,
|
||||
pub device_id: u64,
|
||||
pub lba_offset: u64,
|
||||
}
|
||||
pub struct YellowstoneClient {
|
||||
endpoint_cap: z_cap_t,
|
||||
byte_buffer: ByteBuffer<0x1000>,
|
||||
}
|
||||
impl YellowstoneClient {
|
||||
pub fn new(endpoint_cap: z_cap_t) -> Self {
|
||||
Self {
|
||||
endpoint_cap,
|
||||
byte_buffer: ByteBuffer::new(),
|
||||
}
|
||||
}
|
||||
pub fn register_endpoint(
|
||||
&mut self,
|
||||
req: &RegisterEndpointRequest,
|
||||
) -> Result<yunq::message::Empty, ZError> {
|
||||
yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
pub fn get_endpoint(
|
||||
&mut self,
|
||||
req: &GetEndpointRequest,
|
||||
) -> Result<Endpoint, ZError> {
|
||||
yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
pub fn get_ahci_info(&mut self) -> Result<AhciInfo, ZError> {
|
||||
yunq::client::call_endpoint(
|
||||
&yunq::message::Empty {},
|
||||
&mut self.byte_buffer,
|
||||
self.endpoint_cap,
|
||||
)
|
||||
}
|
||||
pub fn get_xhci_info(&mut self) -> Result<XhciInfo, ZError> {
|
||||
yunq::client::call_endpoint(
|
||||
&yunq::message::Empty {},
|
||||
&mut self.byte_buffer,
|
||||
self.endpoint_cap,
|
||||
)
|
||||
}
|
||||
pub fn get_framebuffer_info(&mut self) -> Result<FramebufferInfo, ZError> {
|
||||
yunq::client::call_endpoint(
|
||||
&yunq::message::Empty {},
|
||||
&mut self.byte_buffer,
|
||||
self.endpoint_cap,
|
||||
)
|
||||
}
|
||||
pub fn get_denali(&mut self) -> Result<DenaliInfo, ZError> {
|
||||
yunq::client::call_endpoint(
|
||||
&yunq::message::Empty {},
|
||||
&mut self.byte_buffer,
|
||||
self.endpoint_cap,
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "yunq-derive"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "1.0", features = ["full", "extra-traits", "printing"] }
|
|
@ -0,0 +1,166 @@
|
|||
use proc_macro::{self, TokenStream};
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Ident, Path, Type, TypePath};
|
||||
|
||||
fn serialize_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStream {
|
||||
if path.is_ident("String") {
|
||||
quote! {
|
||||
{
|
||||
let str_offset = next_extension;
|
||||
let str_length = self.#name.len() as u32;
|
||||
|
||||
buf.write_str_at(offset + next_extension as usize, &self.#name)?;
|
||||
next_extension += str_length;
|
||||
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), str_offset)?;
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind) + 4, str_length)?;
|
||||
}
|
||||
|
||||
}
|
||||
} else if path.is_ident("z_cap_t") {
|
||||
quote! {
|
||||
{
|
||||
let cap_ind = caps.len();
|
||||
caps.push(self.#name);
|
||||
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), cap_ind as u64)?;
|
||||
}
|
||||
}
|
||||
} else if path.is_ident("u64") {
|
||||
quote! {
|
||||
{
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), self.#name as u64)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!(
|
||||
"Serialization not implemented for: {}",
|
||||
path.to_token_stream()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStream {
|
||||
if path.is_ident("String") {
|
||||
quote! {
|
||||
let #name = {
|
||||
let str_offset = buf.at::<u32>(yunq::message::field_offset(offset, #ind))?;
|
||||
let str_length = buf.at::<u32>(yunq::message::field_offset(offset, #ind) + 4)?;
|
||||
|
||||
buf.str_at(offset + str_offset as usize, str_length as usize)?.to_string()
|
||||
};
|
||||
}
|
||||
} else if path.is_ident("z_cap_t") {
|
||||
quote! {
|
||||
let #name = {
|
||||
let cap_ind = buf.at::<u64>(yunq::message::field_offset(offset, #ind))?;
|
||||
caps[cap_ind as usize]
|
||||
};
|
||||
}
|
||||
} else if path.is_ident("u64") {
|
||||
quote! {
|
||||
let #name = buf.at::<u64>(yunq::message::field_offset(offset, #ind))?;
|
||||
}
|
||||
} else {
|
||||
panic!("Parsing not implemented for: {}", path.into_token_stream());
|
||||
}
|
||||
}
|
||||
|
||||
fn field_names(name: &Ident, _ind: usize, _path: &Path) -> proc_macro2::TokenStream {
|
||||
quote! { #name }
|
||||
}
|
||||
|
||||
fn apply_to_struct_fields<T>(input: &DeriveInput, func: fn(&Ident, usize, &Path) -> T) -> Vec<T> {
|
||||
match &input.data {
|
||||
Data::Struct(DataStruct {
|
||||
fields: Fields::Named(fields),
|
||||
..
|
||||
}) => fields
|
||||
.named
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(ind, field)| match &field.ty {
|
||||
Type::Path(TypePath { path, .. }) => {
|
||||
func(&field.ident.as_ref().unwrap(), ind, path)
|
||||
}
|
||||
_ => {
|
||||
panic!("Unrecognized type: {:?}", field)
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
_ => {
|
||||
panic!("Unrecognized input (Expected Struct): {:?}", input.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_derive(YunqMessage)]
|
||||
pub fn derive_client(input_tokens: TokenStream) -> TokenStream {
|
||||
let input: DeriveInput = parse_macro_input!(input_tokens);
|
||||
let ident = input.ident.clone();
|
||||
|
||||
let prelude = quote! {
|
||||
impl YunqMessage for #ident
|
||||
};
|
||||
|
||||
let num_fields = apply_to_struct_fields(&input, |_, _, _| ()).len();
|
||||
|
||||
let serializers = apply_to_struct_fields(&input, serialize_field);
|
||||
let serialize = quote! {
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
buf: &mut yunq::ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &mut alloc::vec::Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
let num_fields = #num_fields;
|
||||
let core_size: u32 = (yunq::message::MESSAGE_HEADER_SIZE + 8 * num_fields) as u32;
|
||||
let mut next_extension = core_size;
|
||||
|
||||
#(#serializers)*
|
||||
|
||||
buf.write_at(offset + 0, yunq::message::MESSAGE_IDENT)?;
|
||||
buf.write_at(offset + 4, core_size)?;
|
||||
buf.write_at(offset + 8, next_extension)?;
|
||||
buf.write_at(offset + 12, 0 as u32)?;
|
||||
Ok(next_extension as usize)
|
||||
}
|
||||
};
|
||||
|
||||
let field_names = apply_to_struct_fields(&input, field_names);
|
||||
let parsers = apply_to_struct_fields(&input, parse_field);
|
||||
let parse = quote! {
|
||||
fn parse<const N: usize>(
|
||||
buf: &yunq::ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &alloc::vec::Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if buf.at::<u32>(offset + 0)? != yunq::message::MESSAGE_IDENT {
|
||||
return Err(ZError::INVALID_ARGUMENT);
|
||||
}
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
|
||||
#(#parsers)*
|
||||
|
||||
Ok(Self {
|
||||
#(#field_names),*
|
||||
})
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
let output = quote! {
|
||||
#prelude {
|
||||
#serialize
|
||||
|
||||
#parse
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
}
|
|
@ -5,3 +5,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
mammoth = {path = "../mammoth"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
|
|
@ -4,110 +4,8 @@
|
|||
extern crate alloc;
|
||||
|
||||
mod buffer;
|
||||
mod client;
|
||||
mod message;
|
||||
pub mod client;
|
||||
pub mod message;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
pub use buffer::ByteBuffer;
|
||||
use mammoth::syscall::z_cap_t;
|
||||
use mammoth::syscall::ZError;
|
||||
pub use message::YunqMessage;
|
||||
|
||||
const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
|
||||
|
||||
pub struct GetEndpointRequest {
|
||||
pub endpoint_name: String,
|
||||
}
|
||||
|
||||
impl YunqMessage for GetEndpointRequest {
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
buf: &mut buffer::ByteBuffer<N>,
|
||||
offset: usize,
|
||||
_caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
let core_size: u32 = (MESSAGE_HEADER_SIZE + 8 * 1) as u32;
|
||||
let mut next_extension = core_size;
|
||||
|
||||
let endpoint_name_offset = next_extension;
|
||||
let endpoint_name_length = self.endpoint_name.len() as u32;
|
||||
|
||||
buf.write_str_at(offset + next_extension as usize, &self.endpoint_name)?;
|
||||
next_extension += endpoint_name_length;
|
||||
|
||||
buf.write_at(field_offset(offset, 0), endpoint_name_offset)?;
|
||||
buf.write_at(field_offset(offset, 0) + 4, endpoint_name_length)?;
|
||||
|
||||
buf.write_at(offset + 0, message::MESSAGE_IDENT)?;
|
||||
buf.write_at(offset + 4, core_size)?;
|
||||
buf.write_at(offset + 8, next_extension)?;
|
||||
buf.write_at(offset + 12, 0 as u32)?;
|
||||
Ok(next_extension as usize)
|
||||
}
|
||||
|
||||
fn parse<const N: usize>(
|
||||
_buf: &ByteBuffer<N>,
|
||||
_offset: usize,
|
||||
_caps: &Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Endpoint {
|
||||
pub endpoint: z_cap_t,
|
||||
}
|
||||
|
||||
fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
||||
}
|
||||
|
||||
impl YunqMessage for Endpoint {
|
||||
fn parse<const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError> {
|
||||
if buf.at::<u32>(offset + 0)? != message::MESSAGE_IDENT {
|
||||
return Err(ZError::INVALID_ARGUMENT);
|
||||
}
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
|
||||
let endpoint_ptr = buf.at::<u64>(field_offset(offset, 0))?;
|
||||
let endpoint = caps[endpoint_ptr as usize];
|
||||
Ok(Endpoint { endpoint })
|
||||
}
|
||||
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
_buf: &mut buffer::ByteBuffer<N>,
|
||||
_offset: usize,
|
||||
_caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct YellowstoneClient {
|
||||
endpoint_cap: z_cap_t,
|
||||
byte_buffer: ByteBuffer<0x1000>,
|
||||
}
|
||||
|
||||
impl YellowstoneClient {
|
||||
pub fn new(endpoint_cap: z_cap_t) -> Self {
|
||||
Self {
|
||||
endpoint_cap,
|
||||
byte_buffer: ByteBuffer::new(),
|
||||
}
|
||||
}
|
||||
pub fn get_endpoint(&mut self, req: &GetEndpointRequest) -> Result<Endpoint, ZError> {
|
||||
client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,11 @@ use mammoth::syscall::z_cap_t;
|
|||
use mammoth::syscall::ZError;
|
||||
|
||||
pub const MESSAGE_IDENT: u32 = 0x33441122;
|
||||
pub const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
|
||||
|
||||
pub fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
||||
}
|
||||
|
||||
pub trait YunqMessage {
|
||||
fn parse<const N: usize>(
|
||||
|
@ -21,3 +26,27 @@ pub trait YunqMessage {
|
|||
caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError>;
|
||||
}
|
||||
|
||||
pub struct Empty {}
|
||||
|
||||
impl YunqMessage for Empty {
|
||||
fn parse<const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
buf: &mut ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,4 +5,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
mammoth = { path = "../../lib/mammoth" }
|
||||
yunq = { path = "../../lib/yunq" }
|
||||
yellowstone = { path = "../../lib/yellowstone" }
|
||||
|
|
|
@ -9,7 +9,8 @@ use mammoth::debug;
|
|||
use mammoth::define_entry;
|
||||
use mammoth::syscall::debug;
|
||||
use mammoth::syscall::z_err_t;
|
||||
use mammoth::syscall::Writer;
|
||||
use yellowstone::GetEndpointRequest;
|
||||
use yellowstone::YellowstoneClient;
|
||||
|
||||
define_entry!();
|
||||
|
||||
|
@ -29,11 +30,11 @@ pub extern "C" fn main() -> z_err_t {
|
|||
|
||||
let mut yellowstone;
|
||||
unsafe {
|
||||
yellowstone = yunq::YellowstoneClient::new(mammoth::INIT_ENDPOINT);
|
||||
yellowstone = YellowstoneClient::new(mammoth::INIT_ENDPOINT);
|
||||
}
|
||||
|
||||
let endpoint = yellowstone
|
||||
.get_endpoint(&yunq::GetEndpointRequest {
|
||||
.get_endpoint(&GetEndpointRequest {
|
||||
endpoint_name: "denali".to_string(),
|
||||
})
|
||||
.expect("Failed to get endpoint");
|
||||
|
|
|
@ -98,25 +98,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
|
||||
|
||||
[[package]]
|
||||
name = "genco"
|
||||
version = "0.17.9"
|
||||
name = "convert_case"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afac3cbb14db69ac9fef9cdb60d8a87e39a7a527f85a81a923436efa40ad42c6"
|
||||
checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
|
||||
dependencies = [
|
||||
"genco-macros",
|
||||
"relative-path",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "genco-macros"
|
||||
version = "0.17.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "553630feadf7b76442b0849fd25fdf89b860d933623aec9693fed19af0400c78"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -131,6 +118,16 @@ version = "1.70.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
|
||||
|
||||
[[package]]
|
||||
name = "prettyplease"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.85"
|
||||
|
@ -149,18 +146,6 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "relative-path"
|
||||
version = "1.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
|
@ -169,9 +154,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.66"
|
||||
version = "2.0.72"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
|
||||
checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -184,6 +169,12 @@ version = "1.0.12"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
|
@ -268,5 +259,9 @@ name = "yunq"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"genco",
|
||||
"convert_case",
|
||||
"prettyplease",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
|
@ -5,4 +5,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.7", features = ["derive"] }
|
||||
genco = { version = "0.17.9" }
|
||||
convert_case = "0.6.0"
|
||||
prettyplease = "0.2.20"
|
||||
proc-macro2 = { version = "1.0" }
|
||||
quote = { version = "1.0" }
|
||||
syn = "2.0.72"
|
||||
|
|
|
@ -1,53 +1,116 @@
|
|||
use crate::parser::{Decl, Field, Interface, Message};
|
||||
use genco::fmt;
|
||||
use genco::fmt::FmtWriter;
|
||||
use genco::prelude::quote_fn;
|
||||
use genco::prelude::quote_in;
|
||||
use genco::prelude::rust;
|
||||
use genco::prelude::FormatInto;
|
||||
use genco::prelude::Rust;
|
||||
use crate::parser::{Decl, Interface, Message, Method};
|
||||
use convert_case::{Case, Casing};
|
||||
use proc_macro2::Ident;
|
||||
use proc_macro2::Span;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
pub fn generate_field_def<'a>(field: &'a Field) -> impl FormatInto<Rust> + 'a {
|
||||
quote_fn!(
|
||||
$(field.name.clone()): $(field.field_type.rust_type()),
|
||||
)
|
||||
fn ident(s: &str) -> Ident {
|
||||
Ident::new(s, Span::call_site())
|
||||
}
|
||||
|
||||
pub fn generate_message_code<'a>(message: &'a Message) -> impl FormatInto<Rust> + 'a {
|
||||
quote_fn!(
|
||||
struct $(&message.name) {$['\r']
|
||||
$(for field in &message.fields =>
|
||||
$[' ']$(generate_field_def(field))$['\r']
|
||||
)}$['\n']
|
||||
|
||||
impl $(&message.name) {$['\r']
|
||||
jjj
|
||||
|
||||
}$['\n']
|
||||
)
|
||||
}
|
||||
|
||||
pub fn generate_code(ast: &Vec<Decl>) -> Result<String, std::fmt::Error> {
|
||||
let mut tokens = rust::Tokens::new();
|
||||
|
||||
for decl in ast {
|
||||
match decl {
|
||||
Decl::Message(message) => quote_in!(tokens => $(generate_message_code(message))),
|
||||
_ => {}
|
||||
fn generate_message(message: &Message) -> TokenStream {
|
||||
let name = ident(&message.name);
|
||||
let field_names = message.fields.iter().map(|f| ident(&f.name));
|
||||
let field_types = message
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| Ident::new(f.field_type.rust_type(), Span::call_site()));
|
||||
quote! {
|
||||
#[derive(YunqMessage)]
|
||||
pub struct #name {
|
||||
#(pub #field_names: #field_types),*
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let mut w = FmtWriter::new(String::new());
|
||||
|
||||
let fmt = fmt::Config::from_lang::<Rust>()
|
||||
.with_indentation(fmt::Indentation::Space(4))
|
||||
.with_newline("\n");
|
||||
|
||||
let config = rust::Config::default()
|
||||
// Prettier imports and use.
|
||||
.with_default_import(rust::ImportMode::Qualified);
|
||||
|
||||
tokens.format_file(&mut w.as_formatter(&fmt), &config)?;
|
||||
|
||||
Ok(w.into_inner())
|
||||
}
|
||||
|
||||
fn generate_method(method: &Method) -> TokenStream {
|
||||
let name = ident(&method.name.to_case(Case::Snake));
|
||||
let maybe_req = method.request.clone().map(|r| ident(&r));
|
||||
let maybe_resp = method.response.clone().map(|r| ident(&r));
|
||||
match (maybe_req, maybe_resp) {
|
||||
(Some(req), Some(resp)) => quote! {
|
||||
pub fn #name (&mut self, req: & #req) -> Result<#resp, ZError> {
|
||||
yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
},
|
||||
(Some(req), None) => quote! {
|
||||
pub fn #name (&mut self, req: & #req) -> Result<yunq::message::Empty, ZError> {
|
||||
yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
},
|
||||
(None, Some(resp)) => quote! {
|
||||
pub fn #name (&mut self) -> Result<#resp, ZError> {
|
||||
yunq::client::call_endpoint(&yunq::message::Empty{}, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_interface(interface: &Interface) -> TokenStream {
|
||||
let client_name = interface.name.clone() + "Client";
|
||||
let name = ident(&client_name);
|
||||
let methods = interface.methods.iter().map(|m| generate_method(&m));
|
||||
quote! {
|
||||
pub struct #name {
|
||||
endpoint_cap: z_cap_t,
|
||||
byte_buffer: ByteBuffer<0x1000>,
|
||||
}
|
||||
|
||||
impl #name {
|
||||
pub fn new(endpoint_cap: z_cap_t) -> Self {
|
||||
Self {
|
||||
endpoint_cap,
|
||||
byte_buffer: ByteBuffer::new(),
|
||||
}
|
||||
}
|
||||
#(#methods)*
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_code(ast: &Vec<Decl>) -> String {
|
||||
let prelude = quote! {
|
||||
#![no_std]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::string::ToString;
|
||||
use mammoth::syscall::z_cap_t;
|
||||
use mammoth::syscall::ZError;
|
||||
use yunq::ByteBuffer;
|
||||
use yunq::YunqMessage;
|
||||
use yunq_derive::YunqMessage;
|
||||
};
|
||||
|
||||
let message_decls = ast
|
||||
.iter()
|
||||
.filter_map(|decl| match decl {
|
||||
Decl::Message(m) => Some(m),
|
||||
_ => None,
|
||||
})
|
||||
.map(|message| generate_message(&message));
|
||||
|
||||
let interface_decls = ast
|
||||
.iter()
|
||||
.filter_map(|decl| match decl {
|
||||
Decl::Interface(i) => Some(i),
|
||||
_ => None,
|
||||
})
|
||||
.map(|interface| generate_interface(&interface));
|
||||
|
||||
let output = quote! {
|
||||
#prelude
|
||||
|
||||
#(#message_decls)*
|
||||
|
||||
#(#interface_decls)*
|
||||
}
|
||||
.to_string();
|
||||
|
||||
prettyplease::unparse(&syn::parse_file(&output).unwrap())
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ mod lexer;
|
|||
mod parser;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fs::read_to_string;
|
||||
use std::fs;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
|
@ -13,22 +13,24 @@ struct Args {
|
|||
// The .yunq file to parse
|
||||
#[arg(short, long)]
|
||||
input_path: String,
|
||||
|
||||
// The .rs file to generate
|
||||
#[arg(short, long)]
|
||||
output_path: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let args = Args::parse();
|
||||
let input = read_to_string(args.input_path)?;
|
||||
let input = fs::read_to_string(args.input_path)?;
|
||||
let tokens = lexer::lex_input(&input)?;
|
||||
|
||||
let mut ast_parser = parser::Parser::new(&tokens);
|
||||
ast_parser.parse_ast()?;
|
||||
ast_parser.type_check()?;
|
||||
|
||||
for decl in ast_parser.ast() {
|
||||
println!("{:?}", decl);
|
||||
}
|
||||
let code = codegen::generate_code(ast_parser.ast());
|
||||
|
||||
println!("{}", codegen::generate_code(ast_parser.ast())?);
|
||||
fs::write(args.output_path, code)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ impl Type {
|
|||
Type::I64 => "i64",
|
||||
Type::String => "String",
|
||||
Type::Bytes => "Vec<u8>",
|
||||
Type::Capability => "u64",
|
||||
Type::Capability => "z_cap_t",
|
||||
Type::Message(s) => s,
|
||||
}
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ impl Debug for Method {
|
|||
}
|
||||
|
||||
pub struct Interface {
|
||||
name: String,
|
||||
methods: Vec<Method>,
|
||||
pub name: String,
|
||||
pub methods: Vec<Method>,
|
||||
}
|
||||
|
||||
pub enum Decl {
|
||||
|
|
Loading…
Reference in New Issue