|
|
|
@ -1,12 +1,11 @@
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
|
|
|
|
use crate::compiler::{Instruction, Module};
|
|
|
|
|
use crate::compiler::{
|
|
|
|
|
AddressingModel, ExecutionMode, ExecutionModel, MemoryModel, Module, StorageClass,
|
|
|
|
|
};
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
|
|
use super::StorageClass;
|
|
|
|
|
|
|
|
|
|
use crate::parser::Ast;
|
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
|
pub enum Type {
|
|
|
|
@ -148,7 +147,7 @@ fn fix_name(name: &String) -> String {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn has_id<T>(name: String, ops: &Vec<(Option<String>, T)>) -> bool {
|
|
|
|
|
fn has_id(name: String, ops: &Vec<(Option<String>, Vec<String>)>) -> bool {
|
|
|
|
|
for op in ops {
|
|
|
|
|
if op.0.is_some() && op.0.clone().unwrap() == name {
|
|
|
|
|
return true;
|
|
|
|
@ -157,7 +156,8 @@ fn has_id<T>(name: String, ops: &Vec<(Option<String>, T)>) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
|
|
|
|
|
pub fn spirv_meta(module: Module) -> String {
|
|
|
|
|
let mut spirv_asm = String::new();
|
|
|
|
|
let mut ops: Vec<(Option<String>, Vec<String>)> = Vec::new();
|
|
|
|
|
|
|
|
|
|
let capabilities: Vec<String> = module
|
|
|
|
@ -170,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 {
|
|
|
|
|
crate::compiler::AddressingModel::Logical => "Logical",
|
|
|
|
|
crate::compiler::AddressingModel::Physical32 => "Physical32",
|
|
|
|
|
crate::compiler::AddressingModel::Physical64 => "Physical64",
|
|
|
|
|
crate::compiler::AddressingModel::PhysicalStorageBuffer64 => "PhysicalStorageBuffer64",
|
|
|
|
|
AddressingModel::Logical => "Logical",
|
|
|
|
|
AddressingModel::Physical32 => "Physical32",
|
|
|
|
|
AddressingModel::Physical64 => "Physical64",
|
|
|
|
|
AddressingModel::PhysicalStorageBuffer64 => "PhysicalStorageBuffer64",
|
|
|
|
|
};
|
|
|
|
|
let memory_model_model = match module.memory_model.memory_model {
|
|
|
|
|
crate::compiler::MemoryModel::Simple => "Simple",
|
|
|
|
|
crate::compiler::MemoryModel::GLSL450 => "GLSL450",
|
|
|
|
|
crate::compiler::MemoryModel::OpenCL => "OpenCL",
|
|
|
|
|
MemoryModel::Simple => "Simple",
|
|
|
|
|
MemoryModel::GLSL450 => "GLSL450",
|
|
|
|
|
MemoryModel::OpenCL => "OpenCL",
|
|
|
|
|
_ => todo!(),
|
|
|
|
|
};
|
|
|
|
|
ops.push((
|
|
|
|
@ -190,10 +190,10 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
|
|
|
|
|
],
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
for entry in module.entry_points.clone() {
|
|
|
|
|
for entry in module.entry_points {
|
|
|
|
|
let exec_model = match entry.execution_model {
|
|
|
|
|
crate::compiler::ExecutionModel::Fragment => "Fragment",
|
|
|
|
|
crate::compiler::ExecutionModel::Vertex => "Vertex",
|
|
|
|
|
ExecutionModel::Fragment => "Fragment",
|
|
|
|
|
ExecutionModel::Vertex => "Vertex",
|
|
|
|
|
};
|
|
|
|
|
let name = entry.name;
|
|
|
|
|
let interface: Vec<String> = entry
|
|
|
|
@ -202,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 {
|
|
|
|
|
crate::compiler::ExecutionMode::OriginUpperLeft => "OriginUpperLeft",
|
|
|
|
|
ExecutionMode::OriginUpperLeft => "OriginUpperLeft",
|
|
|
|
|
};
|
|
|
|
|
ops.push((
|
|
|
|
|
None,
|
|
|
|
@ -224,16 +224,16 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for global in module.globals.clone() {
|
|
|
|
|
for global in module.globals {
|
|
|
|
|
let name = fix_name(&global.name);
|
|
|
|
|
let _typ = global.typ;
|
|
|
|
|
let typ = global.typ;
|
|
|
|
|
let storage_class = match global.storage_class {
|
|
|
|
|
crate::compiler::StorageClass::Input => "Input",
|
|
|
|
|
crate::compiler::StorageClass::Output => "Output",
|
|
|
|
|
crate::compiler::StorageClass::Undefined => panic!("Bound a non-declared variable"),
|
|
|
|
|
StorageClass::Input => "Input",
|
|
|
|
|
StorageClass::Output => "Output",
|
|
|
|
|
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.clone(), &ops) {
|
|
|
|
|
continue;
|
|
|
|
@ -244,7 +244,7 @@ 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(),
|
|
|
|
|
],
|
|
|
|
|
));
|
|
|
|
@ -263,8 +263,8 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for fun in module.functions.clone() {
|
|
|
|
|
let name = fix_name(&format!("l_{}", fun.name));
|
|
|
|
|
for fun in module.functions {
|
|
|
|
|
let name = fix_name(&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);
|
|
|
|
@ -274,277 +274,21 @@ pub fn spirv_meta(module: &mut Module) -> Vec<(Option<String>, Vec<String>)> {
|
|
|
|
|
}
|
|
|
|
|
ops.push((Some(op.0), vec![op.1]));
|
|
|
|
|
}
|
|
|
|
|
// Push OpFunctionType
|
|
|
|
|
ops.push((
|
|
|
|
|
Some(name.clone()),
|
|
|
|
|
vec!["OpTypeFunction".to_string(), return_type.clone()],
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for op in ops {
|
|
|
|
|
// if op.0.is_some() {
|
|
|
|
|
// write!(spirv_asm, "{} = ", op.0.unwrap()).unwrap();
|
|
|
|
|
// }
|
|
|
|
|
// for arg in op.1 {
|
|
|
|
|
// write!(spirv_asm, "{} ", arg).unwrap();
|
|
|
|
|
// }
|
|
|
|
|
// writeln!(spirv_asm).unwrap();
|
|
|
|
|
// }
|
|
|
|
|
// spirv_asm
|
|
|
|
|
ops
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum Number {
|
|
|
|
|
Int(i32),
|
|
|
|
|
Float(f32),
|
|
|
|
|
NotANumber,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn match_number(s: &str) -> Number {
|
|
|
|
|
// floats have to be in the format of [0-9]+\.[0-9]*
|
|
|
|
|
// integers have to be in the format of [0-9]+
|
|
|
|
|
let mut chars = s.chars();
|
|
|
|
|
let mut has_dot = false;
|
|
|
|
|
while let Some(c) = chars.next() {
|
|
|
|
|
if c == '.' {
|
|
|
|
|
if has_dot {
|
|
|
|
|
// only one dot allowed.
|
|
|
|
|
return Number::NotANumber;
|
|
|
|
|
}
|
|
|
|
|
has_dot = true;
|
|
|
|
|
} else if !c.is_digit(10) {
|
|
|
|
|
// not a number;
|
|
|
|
|
// has a character that is not a digit or a dot.
|
|
|
|
|
return Number::NotANumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if has_dot {
|
|
|
|
|
// we have checked that the whole thing is numbers and dots
|
|
|
|
|
// and that there is precisely one dot
|
|
|
|
|
// that matches the regex.
|
|
|
|
|
Number::Float(s.parse().unwrap())
|
|
|
|
|
} else {
|
|
|
|
|
// this is an integer, since no dots.
|
|
|
|
|
Number::Int(s.parse().unwrap())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compile_biop(
|
|
|
|
|
op: &str,
|
|
|
|
|
lst: &mut Vec<Ast>,
|
|
|
|
|
vars: &mut Vec<(String, String)>,
|
|
|
|
|
constants: &mut Vec<(String, String)>,
|
|
|
|
|
types: &mut Vec<String>,
|
|
|
|
|
counter: &mut i32,
|
|
|
|
|
stack: &mut Vec<String>,
|
|
|
|
|
out: &mut Vec<(Option<String>, String)>,
|
|
|
|
|
) {
|
|
|
|
|
assert!(lst.len() == 2);
|
|
|
|
|
let rhs = lst.pop().unwrap();
|
|
|
|
|
let lhs = lst.pop().unwrap();
|
|
|
|
|
compile_ast_ssa(lhs, vars, constants, types, counter, stack, out);
|
|
|
|
|
compile_ast_ssa(rhs, vars, constants, types, counter, stack, out);
|
|
|
|
|
let rhs_id = stack.pop().unwrap();
|
|
|
|
|
let lhs_id = stack.pop().unwrap();
|
|
|
|
|
let id = String::from(counter.to_string());
|
|
|
|
|
*counter += 1;
|
|
|
|
|
out.push((
|
|
|
|
|
Some(id.clone()),
|
|
|
|
|
format!(
|
|
|
|
|
"{} {} {} {}",
|
|
|
|
|
op,
|
|
|
|
|
fix_name(&String::from("f32")),
|
|
|
|
|
fix_name(&lhs_id),
|
|
|
|
|
fix_name(&rhs_id),
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
stack.push(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn compile_ast_ssa(
|
|
|
|
|
ast: Ast,
|
|
|
|
|
vars: &mut Vec<(String, String)>,
|
|
|
|
|
constants: &mut Vec<(String, String)>,
|
|
|
|
|
types: &mut Vec<String>,
|
|
|
|
|
counter: &mut i32,
|
|
|
|
|
stack: &mut Vec<String>,
|
|
|
|
|
out: &mut Vec<(Option<String>, String)>,
|
|
|
|
|
) {
|
|
|
|
|
match ast.clone().list() {
|
|
|
|
|
Some(l) => {
|
|
|
|
|
let mut lst = l.clone();
|
|
|
|
|
assert!(!lst.is_empty());
|
|
|
|
|
let fun = lst.remove(0);
|
|
|
|
|
assert!(true); // no safe remove, thanks Rust
|
|
|
|
|
let fun_name = fun.symbol();
|
|
|
|
|
assert!(fun_name.is_some());
|
|
|
|
|
let fun_name = fun_name.unwrap();
|
|
|
|
|
match fun_name.as_str() {
|
|
|
|
|
"store-ptr" => {
|
|
|
|
|
assert!(lst.len() == 2);
|
|
|
|
|
let ptr = lst.pop().unwrap();
|
|
|
|
|
let val = lst.pop().unwrap();
|
|
|
|
|
compile_ast_ssa(ptr, vars, constants, types, counter, stack, out);
|
|
|
|
|
compile_ast_ssa(val, vars, constants, types, counter, stack, out);
|
|
|
|
|
let val_id = stack.pop().unwrap();
|
|
|
|
|
let ptr_id = stack.pop().unwrap();
|
|
|
|
|
out.push((
|
|
|
|
|
None,
|
|
|
|
|
format!("OpStore {} {}", fix_name(&val_id), fix_name(&ptr_id)),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
"/" => {
|
|
|
|
|
compile_biop(
|
|
|
|
|
"OpFDiv", &mut lst, vars, constants, types, counter, stack, out,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
"*" => {
|
|
|
|
|
compile_biop(
|
|
|
|
|
"OpFMul", &mut lst, vars, constants, types, counter, stack, out,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
"+" => {
|
|
|
|
|
compile_biop(
|
|
|
|
|
"OpFAdd", &mut lst, vars, constants, types, counter, stack, out,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
"-" => {
|
|
|
|
|
compile_biop(
|
|
|
|
|
"OpFSub", &mut lst, vars, constants, types, counter, stack, out,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
s => {
|
|
|
|
|
panic!(
|
|
|
|
|
"Unknown function: {} with params {:#?} in context:\n{:#?}",
|
|
|
|
|
s, lst, ast
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
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);
|
|
|
|
|
let mut contains = false;
|
|
|
|
|
for c in constants.iter() {
|
|
|
|
|
if c.0 == key {
|
|
|
|
|
contains = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !contains {
|
|
|
|
|
constants.push((key.clone(), format!("OpConstant %i32 {}", i.to_string())));
|
|
|
|
|
}
|
|
|
|
|
stack.push(key);
|
|
|
|
|
}
|
|
|
|
|
Number::Float(f) => {
|
|
|
|
|
let key = format!("f32_{}", f);
|
|
|
|
|
let mut contains = false;
|
|
|
|
|
for c in constants.iter() {
|
|
|
|
|
if c.0 == key {
|
|
|
|
|
contains = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !contains {
|
|
|
|
|
constants.push((key.clone(), format!("OpConstant %f32 {}", f.to_string())));
|
|
|
|
|
}
|
|
|
|
|
for t in types.iter() {
|
|
|
|
|
if t == "f32" {
|
|
|
|
|
contains = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !contains {
|
|
|
|
|
types.push("f32".to_string());
|
|
|
|
|
}
|
|
|
|
|
stack.push(key);
|
|
|
|
|
}
|
|
|
|
|
Number::NotANumber => {
|
|
|
|
|
for v in vars.iter() {
|
|
|
|
|
if v.0 == sym {
|
|
|
|
|
stack.push(v.0.clone());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
panic!("Unknown variable or constant: {}", sym);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn compile_fun_ssa(module: &mut Module, ops: &Vec<(Option<String>, String)>) {
|
|
|
|
|
let mut label_counter = Box::new(0);
|
|
|
|
|
for fun in module.functions.iter_mut() {
|
|
|
|
|
assert!(fun.ast.is_some());
|
|
|
|
|
let ast = fun.ast.as_mut().unwrap();
|
|
|
|
|
let block = ast.clone().list().unwrap().get(0).unwrap().clone();
|
|
|
|
|
let mut vars = vec![];
|
|
|
|
|
let mut constants = vec![];
|
|
|
|
|
let mut types = vec![];
|
|
|
|
|
let mut counter = Box::new(0);
|
|
|
|
|
let mut stack = vec![];
|
|
|
|
|
let mut out_op = vec![];
|
|
|
|
|
for v in &module.globals {
|
|
|
|
|
vars.push((v.name.clone(), v.typ.clone()));
|
|
|
|
|
}
|
|
|
|
|
compile_ast_ssa(
|
|
|
|
|
block,
|
|
|
|
|
&mut vars,
|
|
|
|
|
&mut constants,
|
|
|
|
|
&mut types,
|
|
|
|
|
&mut counter,
|
|
|
|
|
&mut stack,
|
|
|
|
|
&mut out_op,
|
|
|
|
|
);
|
|
|
|
|
let mut out_pre = vec![];
|
|
|
|
|
for t in &types {
|
|
|
|
|
let typ = parse_type(t);
|
|
|
|
|
if has_id(fix_name(&typ.to_string()), ops) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if has_id(fix_name(&typ.to_string()), &out_pre) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let mut type_ops = vec![];
|
|
|
|
|
emit_type(typ, &mut type_ops);
|
|
|
|
|
for type_op in type_ops {
|
|
|
|
|
out_pre.push((Some(type_op.0), type_op.1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for c in &constants {
|
|
|
|
|
out_pre.push((Some(fix_name(&c.0.clone())), c.1.clone()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO non-void type
|
|
|
|
|
out_pre.push((
|
|
|
|
|
Some(fix_name(&fun.name)),
|
|
|
|
|
format!("OpFunction %void None %l_{}", fun.name),
|
|
|
|
|
));
|
|
|
|
|
out_pre.push((
|
|
|
|
|
Some(format!("%n_{}", label_counter.to_string())),
|
|
|
|
|
"OpLabel".to_string(),
|
|
|
|
|
));
|
|
|
|
|
*label_counter += 1;
|
|
|
|
|
let mut out_ops = out_pre.clone();
|
|
|
|
|
for op in out_op {
|
|
|
|
|
if op.0.is_some() {
|
|
|
|
|
out_ops.push((Some(fix_name(&op.0.unwrap())), op.1));
|
|
|
|
|
} else {
|
|
|
|
|
out_ops.push((None, op.1));
|
|
|
|
|
}
|
|
|
|
|
for op in ops {
|
|
|
|
|
if op.0.is_some() {
|
|
|
|
|
write!(spirv_asm, "{} = ", op.0.unwrap()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
for op in out_ops {
|
|
|
|
|
let split: Vec<String> = op.1.split(" ").map(|s| s.to_string()).collect();
|
|
|
|
|
let op_name: String = (&split[0]).clone();
|
|
|
|
|
let op_args: Vec<String> = split[1..].iter().map(|s| s.clone()).collect();
|
|
|
|
|
let op_id = op.0.clone();
|
|
|
|
|
let ins: Instruction = Instruction {
|
|
|
|
|
result_id: op_id,
|
|
|
|
|
op: op_name,
|
|
|
|
|
operands: op_args,
|
|
|
|
|
};
|
|
|
|
|
fun.body.as_mut().unwrap().push(ins);
|
|
|
|
|
for arg in op.1 {
|
|
|
|
|
write!(spirv_asm, "{} ", arg).unwrap();
|
|
|
|
|
}
|
|
|
|
|
writeln!(spirv_asm).unwrap();
|
|
|
|
|
}
|
|
|
|
|
spirv_asm
|
|
|
|
|
}
|
|
|
|
|