Add codegen for new rust yunq parser.

This commit is contained in:
Drew Galbraith 2024-07-27 20:23:03 -07:00
parent 8f35d38e93
commit 2cbf871d09
8 changed files with 261 additions and 96 deletions

View File

@ -1,7 +1,5 @@
#![no_std] #![no_std]
extern crate alloc; extern crate alloc;
use alloc::string::String; use alloc::string::String;
use alloc::string::ToString; use alloc::string::ToString;
use mammoth::syscall::z_cap_t; use mammoth::syscall::z_cap_t;
@ -9,22 +7,54 @@ use mammoth::syscall::ZError;
use yunq::ByteBuffer; use yunq::ByteBuffer;
use yunq::YunqMessage; use yunq::YunqMessage;
use yunq_derive::YunqMessage; use yunq_derive::YunqMessage;
#[derive(YunqMessage)]
pub struct RegisterEndpointRequest {
pub endpoint_name: String,
pub endpoint_capability: z_cap_t,
}
#[derive(YunqMessage)] #[derive(YunqMessage)]
pub struct GetEndpointRequest { pub struct GetEndpointRequest {
pub endpoint_name: String, pub endpoint_name: String,
} }
#[derive(YunqMessage)] #[derive(YunqMessage)]
pub struct Endpoint { pub struct Endpoint {
pub endpoint: z_cap_t, 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 { pub struct YellowstoneClient {
endpoint_cap: z_cap_t, endpoint_cap: z_cap_t,
byte_buffer: ByteBuffer<0x1000>, byte_buffer: ByteBuffer<0x1000>,
} }
impl YellowstoneClient { impl YellowstoneClient {
pub fn new(endpoint_cap: z_cap_t) -> Self { pub fn new(endpoint_cap: z_cap_t) -> Self {
Self { Self {
@ -32,7 +62,44 @@ impl YellowstoneClient {
byte_buffer: ByteBuffer::new(), byte_buffer: ByteBuffer::new(),
} }
} }
pub fn get_endpoint(&mut self, req: &GetEndpointRequest) -> Result<Endpoint, ZError> { 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) 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,
)
}
} }

View File

@ -26,6 +26,12 @@ fn serialize_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenS
buf.write_at(yunq::message::field_offset(offset, #ind), cap_ind as u64)?; 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 { } else {
panic!( panic!(
"Serialization not implemented for: {}", "Serialization not implemented for: {}",
@ -51,6 +57,10 @@ fn parse_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStrea
caps[cap_ind as usize] caps[cap_ind as usize]
}; };
} }
} else if path.is_ident("u64") {
quote! {
let #name = buf.at::<u64>(yunq::message::field_offset(offset, #ind))?;
}
} else { } else {
panic!("Parsing not implemented for: {}", path.into_token_stream()); panic!("Parsing not implemented for: {}", path.into_token_stream());
} }

View File

@ -26,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!()
}
}

59
yunq/rust/Cargo.lock generated
View File

@ -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",
] ]

View File

@ -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"

View File

@ -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 fmt = fmt::Config::from_lang::<Rust>() let name = ident(&method.name.to_case(Case::Snake));
.with_indentation(fmt::Indentation::Space(4)) let maybe_req = method.request.clone().map(|r| ident(&r));
.with_newline("\n"); let maybe_resp = method.response.clone().map(|r| ident(&r));
match (maybe_req, maybe_resp) {
let config = rust::Config::default() (Some(req), Some(resp)) => quote! {
// Prettier imports and use. pub fn #name (&mut self, req: & #req) -> Result<#resp, ZError> {
.with_default_import(rust::ImportMode::Qualified); yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
}
tokens.format_file(&mut w.as_formatter(&fmt), &config)?; },
(Some(req), None) => quote! {
Ok(w.into_inner()) 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())
} }

View File

@ -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(())
} }

View File

@ -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 {