Compare commits

..

No commits in common. 'master' and 'master' have entirely different histories.

@ -1,12 +1,11 @@
#[cfg(test)]
mod tests;
use crate::compiler::{
AddressingModel, BuiltinDecoration, Decoration, ExecutionMode, ExecutionModel, Instruction,
MemoryModel, Module, StorageClass,
};
use crate::compiler::{Instruction, Module};
use std::fmt;
use super::StorageClass;
use crate::parser::Ast;
#[derive(Debug, PartialEq, Clone)]
@ -44,43 +43,48 @@ pub fn parse_type(typ: &String) -> Type {
// So, *v4f32i is a pointer to a vector of 4 floats, with the storage class Input.
// *v4f32o is a pointer to a vector of 4 floats, with the storage class Output.
let mut chars = typ.chars();
match chars.next() {
Some('<') => {
let c = typ.chars().next().unwrap();
match c {
'<' => {
assert_eq!(typ.as_str(), "<>");
Type::Void
}
Some('*') => {
let mut typ = chars.collect::<String>();
let storage_class = match typ.pop() {
Some('i') => StorageClass::Input,
Some('o') => StorageClass::Output,
'*' => {
let mut chars = typ.chars();
chars.next();
let typ = chars.collect::<String>();
let storage_class = match typ.chars().last().unwrap() {
'i' => StorageClass::Input,
'o' => StorageClass::Output,
_ => panic!("Invalid storage class"),
};
let typ = typ.chars().take(typ.len() - 1).collect::<String>();
Type::Pointer(Box::new(parse_type(&typ)), storage_class)
}
Some('v') => {
let size = chars.next().map(|s| s.to_digit(10)).flatten().unwrap();
'v' => {
let mut chars = typ.chars();
chars.next();
let size = chars.next().unwrap().to_digit(10).unwrap();
let typ = chars.collect::<String>();
Type::Vector(Box::new(parse_type(&typ)), size)
}
Some('f') => {
let size = chars.collect::<String>().parse().unwrap();
'f' => {
let size = typ.chars().skip(1).collect::<String>().parse().unwrap();
Type::Float(size)
}
Some('s') => {
let size = chars.collect::<String>().parse().unwrap();
's' => {
let size = typ.chars().skip(1).collect::<String>().parse().unwrap();
Type::Int(size)
}
Some('u') => {
let size = chars.collect::<String>().parse().unwrap();
'u' => {
let size = typ.chars().skip(1).collect::<String>().parse().unwrap();
Type::Unsigned(size)
}
_ => panic!("Invalid type"),
}
}
fn emit_type(typ: &Type, ops: &mut Vec<(String, String)>) {
fn emit_type(typ: Type, ops: &mut Vec<(String, String)>) {
match &typ {
Type::Void => {
ops.push(("%void".to_string(), "OpTypeVoid".to_string()));
@ -104,14 +108,18 @@ fn emit_type(typ: &Type, ops: &mut Vec<(String, String)>) {
));
}
Type::Vector(in_typ, size) => {
emit_type(in_typ, ops);
emit_type(*in_typ.clone(), ops);
ops.push((
fix_name(&typ.to_string()),
format!("OpTypeVector {} {}", fix_name(&in_typ.to_string()), size),
format!(
"OpTypeVector {} {}",
fix_name(&in_typ.clone().to_string()),
size
),
))
}
Type::Pointer(in_typ, storage_class) => {
emit_type(in_typ, ops);
emit_type(*in_typ.clone(), ops);
let typ_id = fix_name(&typ.to_string());
let storage_class = match storage_class {
StorageClass::Input => "Input",
@ -130,19 +138,23 @@ fn emit_type(typ: &Type, ops: &mut Vec<(String, String)>) {
}
}
fn fix_name(name: &str) -> String {
fn fix_name(name: &String) -> String {
format!(
"%{}",
name.replace("-", "_")
name.clone()
.replace("-", "_")
.replace("*", "p")
.replace("<>", "void")
)
}
fn has_id<T>(name: &str, ops: &Vec<(Option<String>, T)>) -> bool {
ops.iter()
.find(|op| op.0.as_ref().map(|s| *s == name).unwrap_or(false))
.is_some()
fn has_id<T>(name: String, ops: &Vec<(Option<String>, T)>) -> bool {
for op in ops {
if op.0.is_some() && op.0.clone().unwrap() == name {
return true;
}
}
false
}
pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
@ -158,15 +170,15 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
}
let memory_model_address = match module.memory_model.addressing_model {
AddressingModel::Logical => "Logical",
AddressingModel::Physical32 => "Physical32",
AddressingModel::Physical64 => "Physical64",
AddressingModel::PhysicalStorageBuffer64 => "PhysicalStorageBuffer64",
crate::compiler::AddressingModel::Logical => "Logical",
crate::compiler::AddressingModel::Physical32 => "Physical32",
crate::compiler::AddressingModel::Physical64 => "Physical64",
crate::compiler::AddressingModel::PhysicalStorageBuffer64 => "PhysicalStorageBuffer64",
};
let memory_model_model = match module.memory_model.memory_model {
MemoryModel::Simple => "Simple",
MemoryModel::GLSL450 => "GLSL450",
MemoryModel::OpenCL => "OpenCL",
crate::compiler::MemoryModel::Simple => "Simple",
crate::compiler::MemoryModel::GLSL450 => "GLSL450",
crate::compiler::MemoryModel::OpenCL => "OpenCL",
_ => todo!(),
};
ops.push((
@ -180,8 +192,8 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
for entry in module.entry_points.clone() {
let exec_model = match entry.execution_model {
ExecutionModel::Fragment => "Fragment",
ExecutionModel::Vertex => "Vertex",
crate::compiler::ExecutionModel::Fragment => "Fragment",
crate::compiler::ExecutionModel::Vertex => "Vertex",
};
let name = entry.name;
let interface: Vec<String> = entry
@ -190,7 +202,7 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
.map(|i| fix_name(&i.to_string()))
.collect();
let exec_mode = match entry.execution_mode {
ExecutionMode::OriginUpperLeft => "OriginUpperLeft",
crate::compiler::ExecutionMode::OriginUpperLeft => "OriginUpperLeft",
};
ops.push((
None,
@ -214,16 +226,16 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
for global in module.globals.clone() {
let name = fix_name(&global.name);
let typ = global.typ;
let _typ = global.typ;
let storage_class = match global.storage_class {
StorageClass::Input => "Input",
StorageClass::Output => "Output",
StorageClass::Undefined => panic!("Bound a non-declared variable"),
crate::compiler::StorageClass::Input => "Input",
crate::compiler::StorageClass::Output => "Output",
crate::compiler::StorageClass::Undefined => panic!("Bound a non-declared variable"),
};
let mut type_ops = Vec::new();
emit_type(&parse_type(&typ), &mut type_ops);
emit_type(parse_type(&_typ), &mut type_ops);
for op in type_ops {
if has_id(&op.0, &ops) {
if has_id(op.0.clone(), &ops) {
continue;
}
ops.push((Some(op.0), vec![op.1]));
@ -232,17 +244,17 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
Some(name.clone()),
vec![
"OpVariable".to_string(),
fix_name(&typ),
fix_name(&_typ),
storage_class.to_string(),
],
));
for dec in global.decorations {
// Decorations have the format Location 0, or Builtin FragCoord
let dec = match dec {
Decoration::Location(loc) => format!("Location {}", loc),
Decoration::BuiltIn(builtin) => {
crate::compiler::Decoration::Location(loc) => format!("Location {}", loc),
crate::compiler::Decoration::BuiltIn(builtin) => {
let builtin = match builtin {
BuiltinDecoration::FragCoord => "FragCoord",
crate::compiler::BuiltinDecoration::FragCoord => "FragCoord",
};
format!("BuiltIn {}", builtin)
}
@ -255,15 +267,17 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
let name = fix_name(&format!("l_{}", fun.name));
let return_type = fix_name(&fun.return_type);
let mut type_ops = Vec::new();
emit_type(&parse_type(&fun.return_type), &mut type_ops);
emit_type(parse_type(&fun.return_type), &mut type_ops);
for op in type_ops {
if has_id(&op.0, &ops) {
if has_id(op.0.clone(), &ops) {
continue;
}
ops.push((Some(op.0), vec![op.1]));
}
// Push OpFunctionType
ops.push((Some(name), vec!["OpTypeFunction".to_string(), return_type]));
ops.push((
Some(name.clone()),
vec!["OpTypeFunction".to_string(), return_type.clone()],
));
}
// for op in ops {
@ -275,6 +289,7 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
// }
// writeln!(spirv_asm).unwrap();
// }
// spirv_asm
ops
}
@ -355,7 +370,8 @@ pub fn compile_ast_ssa(
out: &mut Vec<(Option<String>, String)>,
) {
match ast.clone().list() {
Some(mut lst) => {
Some(l) => {
let mut lst = l.clone();
assert!(!lst.is_empty());
let fun = lst.remove(0);
assert!(true); // no safe remove, thanks Rust
@ -405,7 +421,9 @@ pub fn compile_ast_ssa(
}
}
None => {
let sym = ast.symbol().unwrap();
let sym = ast.clone().symbol();
assert!(sym.is_some());
let sym = sym.unwrap();
match match_number(&sym) {
Number::Int(i) => {
let key = format!("i32_{}", i);
@ -482,14 +500,14 @@ pub fn compile_fun_ssa(module: &mut Module, ops: &Vec<(Option<String>, String)>)
let mut out_pre = vec![];
for t in &types {
let typ = parse_type(t);
if has_id(&fix_name(&typ.to_string()), ops) {
if has_id(fix_name(&typ.to_string()), ops) {
continue;
}
if has_id(&fix_name(&typ.to_string()), &out_pre) {
if has_id(fix_name(&typ.to_string()), &out_pre) {
continue;
}
let mut type_ops = vec![];
emit_type(&typ, &mut type_ops);
emit_type(typ, &mut type_ops);
for type_op in type_ops {
out_pre.push((Some(type_op.0), type_op.1));
}

@ -194,7 +194,7 @@ pub fn compile_fun<I: Iterator<Item = Ast>>(
};
let fun = Function {
name: name.to_string(),
return_type,
return_type: return_type,
arguments: vec![],
body: Some(vec![]),
ast: Some(Ast::List(Localised {

Loading…
Cancel
Save