From 6a567924cd611fcd8d26583d26841d0fb2709aa9 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Thu, 2 Sep 2021 22:24:40 -0700 Subject: [PATCH] WIP --- cranelift/isle/src/codegen.rs | 20 +++++++++++++++++++- cranelift/isle/src/compile.rs | 10 ++++++++++ cranelift/isle/src/main.rs | 5 +++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cranelift/isle/src/codegen.rs b/cranelift/isle/src/codegen.rs index e6fb4605b2..92c4d94188 100644 --- a/cranelift/isle/src/codegen.rs +++ b/cranelift/isle/src/codegen.rs @@ -1,5 +1,6 @@ //! Generate Rust code from a series of Sequences. +use crate::error::Error; use crate::ir::{lower_rule, ExprSequence, PatternInst, PatternSequence, Value}; use crate::sema::{RuleId, TermEnv, TermId, TypeEnv}; use peepmatic_automata::{Automaton, Builder as AutomatonBuilder}; @@ -70,6 +71,8 @@ struct TermFunctionsBuilder<'a> { impl<'a> TermFunctionsBuilder<'a> { fn new(typeenv: &'a TypeEnv, termenv: &'a TermEnv) -> Self { + log::trace!("typeenv: {:?}", typeenv); + log::trace!("termenv: {:?}", termenv); Self { builders_by_input: HashMap::new(), builders_by_output: HashMap::new(), @@ -82,6 +85,14 @@ impl<'a> TermFunctionsBuilder<'a> { for rule in 0..self.termenv.rules.len() { let rule = RuleId(rule); let (lhs_root, pattern, rhs_root, expr) = lower_rule(self.typeenv, self.termenv, rule); + log::trace!( + "build:\n- rule {:?}\n- lhs_root {:?} rhs_root {:?}\n- pattern {:?}\n- expr {:?}", + self.termenv.rules[rule.index()], + lhs_root, + rhs_root, + pattern, + expr + ); if let Some(input_root_term) = lhs_root { self.builders_by_input .entry(input_root_term) @@ -115,9 +126,16 @@ impl<'a> TermFunctionsBuilder<'a> { } } +#[derive(Clone, Debug)] pub struct Automata { pub automata_by_input: HashMap>, pub automata_by_output: HashMap>, } -impl Automata {} +impl Automata { + pub fn compile(typeenv: &TypeEnv, termenv: &TermEnv) -> Result { + let mut builder = TermFunctionsBuilder::new(typeenv, termenv); + builder.build(); + Ok(builder.create_automata()) + } +} diff --git a/cranelift/isle/src/compile.rs b/cranelift/isle/src/compile.rs index 1544b4c075..52e9c29483 100644 --- a/cranelift/isle/src/compile.rs +++ b/cranelift/isle/src/compile.rs @@ -1 +1,11 @@ //! Compilation process, from AST to Sema to Sequences of Insts. + +use crate::{ast, sema, ir, codegen}; +use crate::error::Error; + +pub fn compile(defs: &ast::Defs) -> Result { + let mut typeenv = sema::TypeEnv::from_ast(defs)?; + let termenv = sema::TermEnv::from_ast(&mut typeenv, defs)?; + let automata = codegen::Automata::compile(&typeenv, &termenv)?; + Ok(automata) +} diff --git a/cranelift/isle/src/main.rs b/cranelift/isle/src/main.rs index 7ba3cce903..922afbb57e 100644 --- a/cranelift/isle/src/main.rs +++ b/cranelift/isle/src/main.rs @@ -17,7 +17,8 @@ fn main() -> Result<(), error::Error> { let mut input = String::new(); stdin().read_to_string(&mut input)?; let mut parser = parser::Parser::new("", &input[..]); - let _defs = parser.parse_defs()?; - + let defs = parser.parse_defs()?; + let automata = compile::compile(&defs)?; + println!("automata: {:?}", automata); Ok(()) }