Restructure the project with frontends and backends

This commit is contained in:
Dzmitry Malyshau 2020-02-21 08:23:44 -05:00
parent 8a945c79c8
commit 0e38eef82d
6 changed files with 45 additions and 60 deletions

View File

@ -9,10 +9,10 @@ fn main() {
let args = env::args().collect::<Vec<_>>();
let input = fs::read(&args[1]).unwrap();
let module = javelin::parse_u8_slice(&input).unwrap();
let module = javelin::front::spirv::parse_u8_slice(&input).unwrap();
//println!("{:?}", module);
let options = javelin::msl::Options {};
let options = javelin::back::msl::Options {};
let msl = module.to_msl(&options).unwrap();
println!("{}", msl);
}

1
src/back/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod msl;

View File

@ -14,7 +14,7 @@ impl From<FmtError> for Error {
}
}
impl super::Module {
impl crate::Module {
pub fn to_msl(&self, _options: &Options) -> Result<String, Error> {
let mut out = String::new();

1
src/front/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod spirv;

View File

@ -1,8 +1,6 @@
use crate::storage::{Storage, Token};
use std::{
collections::HashMap,
hash::BuildHasherDefault,
use crate::{
storage::{Storage, Token},
FastHashMap,
};
const LAST_KNOWN_OPCODE: spirv::Op = spirv::Op::MemberDecorateStringGOOGLE;
@ -74,8 +72,6 @@ trait Lookup {
fn lookup(&self, key: spirv::Word) -> Result<&Self::Target, ParseError>;
}
type FastHashMap<K, T> = HashMap<K, T, BuildHasherDefault<fxhash::FxHasher>>;
impl<T> Lookup for FastHashMap<spirv::Word, T> {
type Target = T;
fn lookup(&self, key: spirv::Word) -> Result<&T, ParseError> {
@ -92,7 +88,7 @@ pub struct Parser<I> {
temp_bytes: Vec<u8>,
future_names: FastHashMap<spirv::Word, String>,
future_member_names: FastHashMap<(spirv::Word, MemberIndex), String>,
lookup_function: FastHashMap<spirv::Word, Token<super::Function>>,
lookup_function: FastHashMap<spirv::Word, Token<crate::Function>>,
}
impl<I: Iterator<Item = u32>> Parser<I> {
@ -101,9 +97,9 @@ impl<I: Iterator<Item = u32>> Parser<I> {
data,
state: ModuleState::Empty,
temp_bytes: Vec::new(),
future_names: HashMap::default(),
future_member_names: HashMap::default(),
lookup_function: HashMap::default(),
future_names: FastHashMap::default(),
future_member_names: FastHashMap::default(),
lookup_function: FastHashMap::default(),
}
}
@ -157,7 +153,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
}
}
pub fn parse(&mut self) -> Result<super::Module, ParseError> {
pub fn parse(&mut self) -> Result<crate::Module, ParseError> {
let header = {
if self.next()? != spirv::MAGIC_NUMBER {
return Err(ParseError::InvalidHeader);
@ -166,7 +162,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let generator = self.next()?;
let _bound = self.next()?;
let _schema = self.next()?;
super::Header {
crate::Header {
version: (version_raw[2], version_raw[1], version_raw[0]),
generator,
}
@ -274,14 +270,14 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let mut entry_points = Vec::with_capacity(raw_entry_points.len());
for (exec_model, name, fun_id) in raw_entry_points {
entry_points.push(super::EntryPoint {
entry_points.push(crate::EntryPoint {
exec_model,
name,
function: *self.lookup_function.lookup(fun_id)?,
});
}
Ok(super::Module {
Ok(crate::Module {
header,
struct_declarations: Storage::new(),
functions: Storage::new(),
@ -290,7 +286,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
}
}
pub fn parse_u8_slice(data: &[u8]) -> Result<super::Module, ParseError> {
pub fn parse_u8_slice(data: &[u8]) -> Result<crate::Module, ParseError> {
use std::convert::TryInto;
if data.len() % 4 != 0 {

View File

@ -1,16 +1,18 @@
extern crate spirv_headers as spirv;
pub mod msl;
mod parse;
pub mod back;
pub mod front;
mod storage;
pub use parse::{Parser, ParseError, parse_u8_slice};
use crate::storage::{Storage, Token};
use std::collections::HashMap;
use std::{
collections::HashMap,
hash::BuildHasherDefault,
};
use smallvec::SmallVec;
type FastHashMap<K, T> = HashMap<K, T, BuildHasherDefault<fxhash::FxHasher>>;
@ -37,47 +39,32 @@ pub enum Type {
}
#[derive(Debug)]
pub struct Jump {
pub target: Token<Block>,
pub arguments: SmallVec<[Token<Operation>; 1]>,
}
#[derive(Debug)]
pub enum Branch {
Jump(Jump),
JumpIf {
condition: Token<Operation>, //bool
accept: Jump,
reject: Jump,
},
Switch {
selector: Token<Operation>, //int
cases: HashMap<i32, Jump>,
default: Jump,
},
Return {
value: Option<Token<Operation>>,
},
}
#[derive(Debug)]
pub enum Operation {
pub enum Expression {
Arithmetic,
}
pub type Block = Vec<Statement>;
#[derive(Debug)]
pub enum Terminator {
Branch(Branch),
Kill,
Unreachable,
}
pub struct FallThrough;
#[derive(Debug)]
pub struct Block {
pub label: Option<String>,
pub argument_types: Vec<Type>,
pub operations: Storage<Operation>,
pub terminator: Terminator,
pub enum Statement {
Expression(Expression),
Block(Block),
If {
condition: Expression, //bool
accept: Block,
reject: Block,
},
Switch {
selector: Expression, //int
cases: FastHashMap<i32, (Block, Option<FallThrough>)>,
default: Block,
},
Return {
value: Option<Expression>,
},
Kill,
}
#[derive(Debug)]
@ -85,7 +72,7 @@ pub struct Function {
pub name: Option<String>,
pub parameter_types: Vec<Type>,
pub return_type: Type,
pub blocks: Storage<Block>,
pub body: Block,
}
#[derive(Debug)]