Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fixed_cols_in_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Jul 25, 2024
2 parents dd3b250 + 204b9e4 commit 0e6af5c
Show file tree
Hide file tree
Showing 80 changed files with 4,364 additions and 1,423 deletions.
7 changes: 4 additions & 3 deletions airgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ const MAIN_FUNCTION: &str = "main";
pub fn compile(input: AnalysisASMFile) -> PILGraph {
let main_location = Location::main();

let non_std_machines = input
let non_std_non_rom_machines = input
.machines()
.filter(|(k, _)| k.parts().next() != Some("std"))
.filter(|(k, _)| !k.parts().last().unwrap().ends_with("ROM"))
.collect::<BTreeMap<_, _>>();

// we start from the main machine
let main_ty = match non_std_machines.len() {
let main_ty = match non_std_non_rom_machines.len() {
0 => {
// There is no machine. Create an empty main machine but retain
// all PIL utility definitions.
Expand All @@ -50,7 +51,7 @@ pub fn compile(input: AnalysisASMFile) -> PILGraph {
};
}
// if there is a single machine, treat it as main
1 => (*non_std_machines.keys().next().unwrap()).clone(),
1 => (*non_std_non_rom_machines.keys().next().unwrap()).clone(),
// otherwise, use the machine called `MAIN`
_ => {
let p = parse_absolute_path(MAIN_MACHINE);
Expand Down
14 changes: 2 additions & 12 deletions analysis/src/machine_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ impl TypeChecker {
"Machine {ctx} should not have call_selectors as it has a pc"
));
}
for _ in &links {
errors.push(format!(
"Machine {ctx} has a pc, links cannot be used outside of instructions."
));
}
for o in callable.operation_definitions() {
errors.push(format!(
"Machine {ctx} should not have operations as it has a pc, found `{}`",
Expand Down Expand Up @@ -446,7 +441,7 @@ machine Main with latch: latch, operation_id: id {
}

#[test]
fn virtual_machine_has_no_links() {
fn virtual_machine_with_links() {
let src = r#"
machine Main {
reg pc[@pc];
Expand All @@ -456,12 +451,7 @@ machine Main {
link => B = submachine.foo(A);
}
"#;
expect_check_str(
src,
Err(vec![
"Machine ::Main has a pc, links cannot be used outside of instructions.",
]),
);
expect_check_str(src, Ok(()));
}

#[test]
Expand Down
50 changes: 37 additions & 13 deletions asm-to-pil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
#![deny(clippy::print_stdout)]

use powdr_ast::asm_analysis::{AnalysisASMFile, Item};
use powdr_ast::asm_analysis::{AnalysisASMFile, Item, SubmachineDeclaration};
use powdr_number::FieldElement;
use romgen::generate_machine_rom;
use vm_to_constrained::ROM_SUBMACHINE_NAME;
mod common;
mod romgen;
mod vm_to_constrained;

pub const ROM_SUFFIX: &str = "ROM";

/// Remove all ASM from the machine tree. Takes a tree of virtual or constrained machines and returns a tree of constrained machines
pub fn compile<T: FieldElement>(file: AnalysisASMFile) -> AnalysisASMFile {
AnalysisASMFile {
items: file
.items
.into_iter()
.map(|(name, m)| {
(
name,
match m {
Item::Machine(m) => {
let (m, rom) = generate_machine_rom::<T>(m);
Item::Machine(vm_to_constrained::convert_machine::<T>(m, rom))
.flat_map(|(name, m)| match m {
Item::Machine(m) => {
let (m, rom) = generate_machine_rom::<T>(m);
let (mut m, rom_machine) = vm_to_constrained::convert_machine::<T>(m, rom);

match rom_machine {
// in the absence of ROM, simply return the machine
None => vec![(name, Item::Machine(m))],
Some(rom_machine) => {
// introduce a new name for the ROM machine, based on the original name
let mut rom_name = name.clone();
let machine_name = rom_name.pop().unwrap();
rom_name.push(format!("{machine_name}{ROM_SUFFIX}"));

// add the ROM as a submachine
m.submachines.push(SubmachineDeclaration {
name: ROM_SUBMACHINE_NAME.into(),
ty: rom_name.clone(),
args: vec![],
});

// return both the machine and the rom
vec![
(name, Item::Machine(m)),
(rom_name, Item::Machine(rom_machine)),
]
}
Item::Expression(e) => Item::Expression(e),
Item::TypeDeclaration(enum_decl) => Item::TypeDeclaration(enum_decl),
Item::TraitDeclaration(trait_decl) => Item::TraitDeclaration(trait_decl),
},
)
}
}
Item::Expression(e) => vec![(name, Item::Expression(e))],
Item::TypeDeclaration(enum_decl) => vec![(name, Item::TypeDeclaration(enum_decl))],
Item::TraitDeclaration(trait_decl) => {
vec![(name, Item::TraitDeclaration(trait_decl))]
}
})
.collect(),
}
Expand Down
Loading

0 comments on commit 0e6af5c

Please sign in to comment.