futilehdl/src/frontend.rs

139 lines
3.6 KiB
Rust
Raw Normal View History

2022-01-05 01:08:25 +00:00
use std::collections::BTreeMap;
2022-01-05 01:09:08 +00:00
use crate::builtin_cells::get_builtins;
2022-01-04 22:05:25 +00:00
use crate::parser;
use crate::rtlil;
fn make_pubid(id: &str) -> String {
"\\".to_owned() + id
}
#[derive(Debug)]
pub enum CompileErrorKind {
2022-01-05 01:09:08 +00:00
UndefinedReference(String),
}
#[derive(Debug)]
pub struct CompileError {
kind: CompileErrorKind,
}
impl CompileError {
fn new(kind: CompileErrorKind) -> Self {
2022-01-05 01:09:08 +00:00
Self { kind }
}
}
pub enum GenericParam<T> {
Unsolved,
Solved(T),
}
pub enum Type {
/// a wire of some width
2022-01-05 01:09:08 +00:00
Wire(GenericParam<u32>),
}
2022-01-05 01:08:25 +00:00
impl Type {
pub fn wire() -> Self {
Self::Wire(GenericParam::Unsolved)
}
}
pub struct CallArgument {
pub name: String,
pub atype: Type,
}
// module that can be instantiated like a function
pub struct Callable {
2022-01-05 01:08:25 +00:00
pub name: String,
pub args: Vec<CallArgument>,
pub ret: Type,
2022-01-05 01:09:08 +00:00
pub instantiate: Box<dyn Fn(&str, &[String], &str) -> rtlil::Cell>,
2022-01-05 01:08:25 +00:00
}
struct Context {
2022-01-05 01:09:08 +00:00
callables: BTreeMap<String, Callable>,
}
2022-01-04 22:05:25 +00:00
2022-01-05 01:09:08 +00:00
fn lower_expression(
ctx: &Context,
module: &mut rtlil::Module,
expr: &parser::Expression,
) -> Result<String, CompileError> {
2022-01-04 22:05:25 +00:00
match expr {
2022-01-05 01:11:13 +00:00
parser::Expression::Ident(ident) => Ok(make_pubid(ident)),
2022-01-04 22:05:25 +00:00
parser::Expression::Call(call) => {
let output_gen_id = module.make_genid("cell");
module.add_wire(rtlil::Wire::new(&output_gen_id, 1, None));
2022-01-05 01:09:08 +00:00
let args_resolved = call
.args
.iter()
.map(|expr| lower_expression(ctx, module, expr))
.collect::<Result<Vec<_>, _>>()?;
let callable = ctx
.callables
.get(call.name.fragment() as &str)
.ok_or_else(|| {
CompileError::new(CompileErrorKind::UndefinedReference(
call.name.fragment().to_string(),
))
})?;
2022-01-05 01:08:25 +00:00
let cell_id = module.make_genid(&callable.name);
let cell = (*callable.instantiate)(&cell_id, args_resolved.as_slice(), &output_gen_id);
2022-01-04 22:05:25 +00:00
module.add_cell(cell);
Ok(output_gen_id)
}
parser::Expression::Operation(_op) => todo!(),
2022-01-04 22:05:25 +00:00
}
}
2022-01-05 01:09:08 +00:00
fn lower_assignment(
ctx: &Context,
module: &mut rtlil::Module,
assignment: parser::Assign,
) -> Result<(), CompileError> {
2022-01-05 01:11:13 +00:00
let target_id = make_pubid(assignment.lhs);
2022-01-05 01:08:25 +00:00
let return_wire = lower_expression(ctx, module, &assignment.expr)?;
2022-01-04 22:05:25 +00:00
module.add_connection(target_id, return_wire);
Ok(())
}
pub fn lower_module(pa_module: parser::Module) -> Result<String, CompileError> {
let mut writer = rtlil::ILWriter::new();
2022-01-05 01:11:13 +00:00
let mut ir_module = rtlil::Module::new(make_pubid(pa_module.name));
let context = Context {
2022-01-05 01:09:08 +00:00
callables: get_builtins()
.into_iter()
.map(|clb| (clb.name.to_owned(), clb))
.collect(),
};
2022-01-04 22:05:25 +00:00
writer.write_line("autoidx 1");
for (idx, port) in pa_module.ports.iter().enumerate() {
let dir_option = match port.direction {
parser::PortDirection::Input => rtlil::PortOption::Input(idx as i32 + 1),
parser::PortDirection::Output => rtlil::PortOption::Output(idx as i32 + 1),
};
let wire = rtlil::Wire::new(
2022-01-05 01:11:13 +00:00
make_pubid(port.net.name),
2022-01-04 22:05:25 +00:00
port.net.width.unwrap_or(1) as u32,
2022-01-05 01:09:08 +00:00
Some(dir_option),
2022-01-04 22:05:25 +00:00
);
ir_module.add_wire(wire);
}
for stmt in pa_module.statements {
match stmt {
2022-01-05 01:09:08 +00:00
parser::Statement::Assign(assignment) => {
lower_assignment(&context, &mut ir_module, assignment)?
}
2022-01-04 22:05:25 +00:00
}
}
ir_module.write_rtlil(&mut writer);
Ok(writer.finish())
}