Compare commits
3 Commits
3eea4d811a
...
2cbf871d09
Author | SHA1 | Date |
---|---|---|
|
2cbf871d09 | |
|
8f35d38e93 | |
|
ccd13fecf1 |
|
@ -34,6 +34,24 @@ dependencies = [
|
||||||
"linked_list_allocator",
|
"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]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
|
@ -49,12 +67,38 @@ dependencies = [
|
||||||
"lock_api",
|
"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]]
|
[[package]]
|
||||||
name = "testbed"
|
name = "testbed"
|
||||||
version = "0.1.0"
|
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 = [
|
dependencies = [
|
||||||
"mammoth",
|
"mammoth",
|
||||||
"yunq",
|
"yunq",
|
||||||
|
"yunq-derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -62,4 +106,14 @@ name = "yunq"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mammoth",
|
"mammoth",
|
||||||
|
"yunq-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "yunq-derive"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
||||||
members = [
|
members = [
|
||||||
"lib/mammoth", "lib/yunq", "usr/testbed",
|
"lib/mammoth", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "usr/testbed",
|
||||||
]
|
]
|
||||||
|
|
||||||
# the profile used for `cargo build`
|
# the profile used for `cargo build`
|
||||||
|
|
|
@ -146,9 +146,7 @@ macro_rules! debug {
|
||||||
($fmt:literal, $($val:expr),+) => {{
|
($fmt:literal, $($val:expr),+) => {{
|
||||||
use core::fmt::Write as _;
|
use core::fmt::Write as _;
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
// TODO: Find a way to do this so we don't have to import writer.
|
let mut w = $crate::syscall::Writer::new();
|
||||||
// We can't fully qualify this if we want to use it in this crate.
|
|
||||||
let mut w = Writer::new();
|
|
||||||
write!(&mut w, $fmt, $($val),*).expect("Failed to format");
|
write!(&mut w, $fmt, $($val),*).expect("Failed to format");
|
||||||
let s: String = w.into();
|
let s: String = w.into();
|
||||||
debug(&s);
|
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]
|
[dependencies]
|
||||||
mammoth = {path = "../mammoth"}
|
mammoth = {path = "../mammoth"}
|
||||||
|
yunq-derive = {path = "../yunq-derive"}
|
||||||
|
|
|
@ -4,110 +4,8 @@
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
mod buffer;
|
mod buffer;
|
||||||
mod client;
|
pub mod client;
|
||||||
mod message;
|
pub mod message;
|
||||||
|
|
||||||
use alloc::string::String;
|
|
||||||
use alloc::vec::Vec;
|
|
||||||
pub use buffer::ByteBuffer;
|
pub use buffer::ByteBuffer;
|
||||||
use mammoth::syscall::z_cap_t;
|
|
||||||
use mammoth::syscall::ZError;
|
|
||||||
pub use message::YunqMessage;
|
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;
|
use mammoth::syscall::ZError;
|
||||||
|
|
||||||
pub const MESSAGE_IDENT: u32 = 0x33441122;
|
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 {
|
pub trait YunqMessage {
|
||||||
fn parse<const N: usize>(
|
fn parse<const N: usize>(
|
||||||
|
@ -21,3 +26,27 @@ pub trait YunqMessage {
|
||||||
caps: &mut Vec<z_cap_t>,
|
caps: &mut Vec<z_cap_t>,
|
||||||
) -> Result<usize, ZError>;
|
) -> 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]
|
[dependencies]
|
||||||
mammoth = { path = "../../lib/mammoth" }
|
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::define_entry;
|
||||||
use mammoth::syscall::debug;
|
use mammoth::syscall::debug;
|
||||||
use mammoth::syscall::z_err_t;
|
use mammoth::syscall::z_err_t;
|
||||||
use mammoth::syscall::Writer;
|
use yellowstone::GetEndpointRequest;
|
||||||
|
use yellowstone::YellowstoneClient;
|
||||||
|
|
||||||
define_entry!();
|
define_entry!();
|
||||||
|
|
||||||
|
@ -29,11 +30,11 @@ pub extern "C" fn main() -> z_err_t {
|
||||||
|
|
||||||
let mut yellowstone;
|
let mut yellowstone;
|
||||||
unsafe {
|
unsafe {
|
||||||
yellowstone = yunq::YellowstoneClient::new(mammoth::INIT_ENDPOINT);
|
yellowstone = YellowstoneClient::new(mammoth::INIT_ENDPOINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
let endpoint = yellowstone
|
let endpoint = yellowstone
|
||||||
.get_endpoint(&yunq::GetEndpointRequest {
|
.get_endpoint(&GetEndpointRequest {
|
||||||
endpoint_name: "denali".to_string(),
|
endpoint_name: "denali".to_string(),
|
||||||
})
|
})
|
||||||
.expect("Failed to get endpoint");
|
.expect("Failed to get endpoint");
|
||||||
|
|
|
@ -98,25 +98,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
|
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "genco"
|
name = "convert_case"
|
||||||
version = "0.17.9"
|
version = "0.6.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "afac3cbb14db69ac9fef9cdb60d8a87e39a7a527f85a81a923436efa40ad42c6"
|
checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"genco-macros",
|
"unicode-segmentation",
|
||||||
"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",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -131,6 +118,16 @@ version = "1.70.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
|
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]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.85"
|
version = "1.0.85"
|
||||||
|
@ -149,18 +146,6 @@ dependencies = [
|
||||||
"proc-macro2",
|
"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]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
|
@ -169,9 +154,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.66"
|
version = "2.0.72"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
|
checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -184,6 +169,12 @@ version = "1.0.12"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-segmentation"
|
||||||
|
version = "1.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf8parse"
|
name = "utf8parse"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
@ -268,5 +259,9 @@ name = "yunq"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"genco",
|
"convert_case",
|
||||||
|
"prettyplease",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
|
@ -5,4 +5,8 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.7", features = ["derive"] }
|
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 crate::parser::{Decl, Interface, Message, Method};
|
||||||
use genco::fmt;
|
use convert_case::{Case, Casing};
|
||||||
use genco::fmt::FmtWriter;
|
use proc_macro2::Ident;
|
||||||
use genco::prelude::quote_fn;
|
use proc_macro2::Span;
|
||||||
use genco::prelude::quote_in;
|
use proc_macro2::TokenStream;
|
||||||
use genco::prelude::rust;
|
use quote::quote;
|
||||||
use genco::prelude::FormatInto;
|
|
||||||
use genco::prelude::Rust;
|
|
||||||
|
|
||||||
pub fn generate_field_def<'a>(field: &'a Field) -> impl FormatInto<Rust> + 'a {
|
fn ident(s: &str) -> Ident {
|
||||||
quote_fn!(
|
Ident::new(s, Span::call_site())
|
||||||
$(field.name.clone()): $(field.field_type.rust_type()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_message_code<'a>(message: &'a Message) -> impl FormatInto<Rust> + 'a {
|
fn generate_message(message: &Message) -> TokenStream {
|
||||||
quote_fn!(
|
let name = ident(&message.name);
|
||||||
struct $(&message.name) {$['\r']
|
let field_names = message.fields.iter().map(|f| ident(&f.name));
|
||||||
$(for field in &message.fields =>
|
let field_types = message
|
||||||
$[' ']$(generate_field_def(field))$['\r']
|
.fields
|
||||||
)}$['\n']
|
.iter()
|
||||||
|
.map(|f| Ident::new(f.field_type.rust_type(), Span::call_site()));
|
||||||
impl $(&message.name) {$['\r']
|
quote! {
|
||||||
jjj
|
#[derive(YunqMessage)]
|
||||||
|
pub struct #name {
|
||||||
}$['\n']
|
#(pub #field_names: #field_types),*
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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))),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut w = FmtWriter::new(String::new());
|
fn generate_method(method: &Method) -> TokenStream {
|
||||||
|
let name = ident(&method.name.to_case(Case::Snake));
|
||||||
let fmt = fmt::Config::from_lang::<Rust>()
|
let maybe_req = method.request.clone().map(|r| ident(&r));
|
||||||
.with_indentation(fmt::Indentation::Space(4))
|
let maybe_resp = method.response.clone().map(|r| ident(&r));
|
||||||
.with_newline("\n");
|
match (maybe_req, maybe_resp) {
|
||||||
|
(Some(req), Some(resp)) => quote! {
|
||||||
let config = rust::Config::default()
|
pub fn #name (&mut self, req: & #req) -> Result<#resp, ZError> {
|
||||||
// Prettier imports and use.
|
yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||||
.with_default_import(rust::ImportMode::Qualified);
|
}
|
||||||
|
},
|
||||||
tokens.format_file(&mut w.as_formatter(&fmt), &config)?;
|
(Some(req), None) => quote! {
|
||||||
|
pub fn #name (&mut self, req: & #req) -> Result<yunq::message::Empty, ZError> {
|
||||||
Ok(w.into_inner())
|
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;
|
mod parser;
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::read_to_string;
|
use std::fs;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
@ -13,22 +13,24 @@ struct Args {
|
||||||
// The .yunq file to parse
|
// The .yunq file to parse
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
input_path: String,
|
input_path: String,
|
||||||
|
|
||||||
|
// The .rs file to generate
|
||||||
|
#[arg(short, long)]
|
||||||
|
output_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let args = Args::parse();
|
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 tokens = lexer::lex_input(&input)?;
|
||||||
|
|
||||||
let mut ast_parser = parser::Parser::new(&tokens);
|
let mut ast_parser = parser::Parser::new(&tokens);
|
||||||
ast_parser.parse_ast()?;
|
ast_parser.parse_ast()?;
|
||||||
ast_parser.type_check()?;
|
ast_parser.type_check()?;
|
||||||
|
|
||||||
for decl in ast_parser.ast() {
|
let code = codegen::generate_code(ast_parser.ast());
|
||||||
println!("{:?}", decl);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{}", codegen::generate_code(ast_parser.ast())?);
|
fs::write(args.output_path, code)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ impl Type {
|
||||||
Type::I64 => "i64",
|
Type::I64 => "i64",
|
||||||
Type::String => "String",
|
Type::String => "String",
|
||||||
Type::Bytes => "Vec<u8>",
|
Type::Bytes => "Vec<u8>",
|
||||||
Type::Capability => "u64",
|
Type::Capability => "z_cap_t",
|
||||||
Type::Message(s) => s,
|
Type::Message(s) => s,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,8 +102,8 @@ impl Debug for Method {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Interface {
|
pub struct Interface {
|
||||||
name: String,
|
pub name: String,
|
||||||
methods: Vec<Method>,
|
pub methods: Vec<Method>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Decl {
|
pub enum Decl {
|
||||||
|
|
Loading…
Reference in New Issue