Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ pub enum HostRequestKind {
Plusargs,
FileIo,
Vcd,
Scan,
}

impl HostRequestKind {
Expand All @@ -311,6 +312,7 @@ impl HostRequestKind {
Self::Plusargs => 1u64,
Self::FileIo => 2,
Self::Vcd => 3,
Self::Scan => 4,
};
(family << 32) | site as u64
}
Expand All @@ -321,6 +323,7 @@ impl HostRequestKind {
1 => Self::Plusargs,
2 => Self::FileIo,
3 => Self::Vcd,
4 => Self::Scan,
_ => return None,
};
Some((family, request as u32))
Expand Down Expand Up @@ -1452,6 +1455,7 @@ mod tests {
HostRequestKind::Plusargs,
HostRequestKind::FileIo,
HostRequestKind::Vcd,
HostRequestKind::Scan,
] {
for site in [0, 1, u32::MAX] {
assert_eq!(
Expand All @@ -1460,7 +1464,7 @@ mod tests {
);
}
}
for invalid in [0, u32::MAX as u64, 4u64 << 32, 1u64 << 63] {
for invalid in [0, u32::MAX as u64, 5u64 << 32, 1u64 << 63] {
assert_eq!(HostRequestKind::decode(invalid), None);
}
assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5988,6 +5988,8 @@ fn lower_bit_vector_function_slt<A: std::hash::Hash + Eq + Clone>(
| SystemFunction::Fgetc
| SystemFunction::Ungetc
| SystemFunction::Feof
| SystemFunction::Sscanf
| SystemFunction::Fscanf
| SystemFunction::Timeunit
| SystemFunction::Timeprecision
| SystemFunction::Isunbounded
Expand Down
10 changes: 10 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

mod file_io;
mod fread;
mod scan;

use super::procedural::*;
use super::*;
Expand Down Expand Up @@ -854,6 +855,15 @@ impl<'p, 'a> Ff<'p, 'a> {
args,
..
} => self.file_function(*function, args)?,
Expr::Call {
callee:
sv::ir::Callee::System(
function
@ (sv::ir::SystemFunction::Sscanf | sv::ir::SystemFunction::Fscanf),
),
args,
..
} => self.scan_function(*function, args)?,
Expr::Call {
callee: sv::ir::Callee::System(sv::ir::SystemFunction::Fopen),
args,
Expand Down
112 changes: 112 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff/scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use super::*;
use celox_design::{HostRequestKind, HostRequestSite, HostRequestValue};

impl Ff<'_, '_> {
pub(super) fn scan_function(
&mut self,
function: sv::ir::SystemFunction,
args: &[Option<sv::ir::Expr>],
) -> Result<sv::ir::Expr, sv::AnalyzerError> {
use sv::ir::Expr;
if self.kernel.is_none() {
return Err(unsupported(format!(
"{} requires Simulation scheduled execution",
function.name()
)));
}
let mut inputs = Vec::new();
for (position, argument) in args.iter().take(2).enumerate() {
let argument = argument
.as_ref()
.ok_or_else(|| unsupported("omitted scan source/format"))?;
let argument = self.hoist(argument)?;
let width = self
.m
.comparison_width(&argument, &argument)
.ok_or_else(|| {
unsupported("scan source/format must be a packed integral or literal string")
})?;
// File descriptors are 32-bit packed values (21.3.1), even when
// the actual expression has a wider integral type.
let width = if function == sv::ir::SystemFunction::Fscanf && position == 0 {
32
} else {
width
};
let (id, _) = self.host_hidden("scan_input", &argument, width, false, true)?;
inputs.push(HostRequestValue {
float_precision: None,
id,
width,
signed: false,
is_string: function != sv::ir::SystemFunction::Fscanf || position != 0,
});
}
let (result_id, result_name) = self.m.temp("scan_count", 32, true, false);
let mut results = vec![HostRequestValue {
float_precision: None,
id: result_id,
width: 32,
signed: true,
is_string: false,
}];
let mut destinations = Vec::new();
for argument in args.iter().skip(2) {
let argument = argument
.as_ref()
.ok_or_else(|| unsupported("omitted scan destination"))?;
let (lvalues, _) = self.capture_system_actual(argument)?;
let width=self.m.comparison_width(argument,argument).ok_or_else(||unsupported("scan destination must be an integral variable; string/real outputs are unsupported"))?;
let (id, name) = self.m.temp("scan_value", width, false, true);
results.push(HostRequestValue {
float_precision: None,
id,
width,
signed: false,
is_string: false,
});
destinations.push((lvalues, Expr::Ident(name)));
}
let site = self
.host_request_sites
.iter()
.filter(|site| site.kind == HostRequestKind::Scan)
.count() as u32;
self.host_request_sites.push(HostRequestSite {
process: 0,
kind: HostRequestKind::Scan,
requires_timed_simulation: false,
operation: u32::from(function == sv::ir::SystemFunction::Fscanf),
args: inputs,
results,
strings: Vec::new(),
});
self.with_kernel(|kernel| kernel.host_request(HostRequestKind::Scan, site))
.map_err(kernel_error)?;
// A failed or ignored output must not overwrite a previously assigned
// alias. Scan assignments are contiguous even with suppressed fields.
for (index, (lvalues, value)) in destinations.into_iter().enumerate() {
let condition = Expr::Binary {
left: Box::new(Expr::Ident(result_name.clone())),
op: sv::ir::BinaryOp::Gt,
right: Box::new(Expr::Literal(format!("32'sd{index}"))),
};
let (condition, _) = self.eval_truth(&condition)?;
let write = self.b.new_block();
let join = self.b.new_block();
self.b.seal_block(SIRTerminator::Branch {
cond: condition,
true_block: (write, Vec::new()),
false_block: (join, Vec::new()),
});
self.b.switch_to_block(write);
match lvalues.as_slice() {
[one] => self.assign(one, &value)?,
parts => self.assign_concat(parts, &value)?,
};
self.jump(join);
self.b.switch_to_block(join);
}
Ok(Expr::Ident(result_name))
}
}
1 change: 1 addition & 0 deletions crates/celox-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod file_io;
pub mod parallel;
pub mod plusargs;
mod reflection;
pub mod scan;
mod schedule;
pub mod scheduler;
mod simulation;
Expand Down
Loading
Loading