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
51 changes: 46 additions & 5 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,25 @@ impl<'p, 'a> Ff<'p, 'a> {
Ok(Expr::Ident(ret_name))
}

/// The value of `expr` now, in a temporary of its type.
fn keep_argument(&mut self, expr: &sv::ir::Expr) -> Result<sv::ir::Expr, sv::AnalyzerError> {
let signed = self.expr_signed(expr);
let float = floating::precision(expr, self.m.variables, self.m.name_to_id);
let handle = reads_string_variable(expr, self.m.variables, self.m.name_to_id);
let value = self.eval(expr, None)?;
let register = self.b.register(&value).clone();
let width = register.width();
let four_state = matches!(register, celox_sir::RegisterType::Logic { .. });
let (temp, name) = self.m.temp("task_argument", width, signed, four_state);
let variable = self.m.variables.get_mut(&temp).unwrap();
variable.float = float;
if handle {
variable.value_kind = celox_design::ValueKind::StrRef;
}
self.store(temp, SIROffset::Static(0), width, value);
Ok(sv::ir::Expr::Ident(name))
}

fn plusargs(
&mut self,
function: sv::ir::SystemFunction,
Expand Down Expand Up @@ -3211,6 +3230,32 @@ impl<'p, 'a> Ff<'p, 'a> {
};
let (template, mut values) = system_task_template(self.m, &kind, args);
prepare_time_operands(self.m, template.as_deref(), &mut values)?;
// Arguments are evaluated left to right. A later argument's call can
// change what an earlier one reads, or suspend the process (a host
// request) so that a value read before it would not survive the
// resume. So every argument before the last one with a call is
// evaluated whole into a temporary at its place; that one only has
// its calls run before its own reads, as any expression does.
let last_call = values.iter().rposition(
|value| matches!(value, sv::ir::SystemTaskArg::Expr(expr) if self.m.calls(expr)),
);
if let Some(last_call) = last_call {
for (index, value) in values[..=last_call].iter_mut().enumerate() {
let sv::ir::SystemTaskArg::Expr(expr) = value else {
continue;
};
let hoisted = if self.m.calls(expr) {
self.hoist(expr)?
} else {
expr.clone()
};
*expr = if index == last_call || matches!(hoisted, sv::ir::Expr::Literal(_)) {
hoisted
} else {
self.keep_argument(&hoisted)?
};
}
}
let mut regs = Vec::with_capacity(values.len());
let mut arg_widths = Vec::with_capacity(values.len());
let mut arg_signed = Vec::with_capacity(values.len());
Expand All @@ -3220,11 +3265,7 @@ impl<'p, 'a> Ff<'p, 'a> {
for value in &values {
match value {
sv::ir::SystemTaskArg::Expr(expr) => {
let expr = if self.m.calls(expr) {
self.hoist(expr)?
} else {
expr.clone()
};
let expr = expr.clone();
let signed = self.expr_signed(&expr);
arg_is_handle.push(reads_string_variable(
&expr,
Expand Down
76 changes: 76 additions & 0 deletions crates/celox/tests/sv_plusargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,79 @@ packed_string_literals_backend!(packed_string_literals_cranelift, build);
#[cfg(target_arch = "x86_64")]
packed_string_literals_backend!(packed_string_literals_native, build_native);
packed_string_literals_backend!(packed_string_literals_wasm, build_wasm);

/// A plusargs query among the arguments of a `$display` suspends the process
/// for the host; the arguments read before and after it keep their values.
const DISPLAYED: &str = r#"
module Top;
int i;
logic [7:0] v;
initial begin
i = 3;
v = 0;
$display("%0d %0d %0d %0d", i, $test$plusargs("verbose"), $value$plusargs("v=%d", v), v);
end
endmodule
"#;

/// Arguments are evaluated left to right: a later argument's call does not
/// change what an earlier argument read (Icarus prints `3 1 5 7`).
const ORDERED: &str = r#"
module Top;
int i;
function automatic int f();
return 0;
endfunction
function automatic int g();
i = 7;
return 5;
endfunction
initial begin
i = 3;
$display("%0d %0d %0d %0d", i + f(), $test$plusargs("verbose"), g(), i);
end
endmodule
"#;

macro_rules! display_plusargs {
($name:ident, $backend:ident) => {
display_plusargs!($name, $backend, DISPLAYED, "3 1 1 7");
};
($name:ident, $backend:ident, $source:expr, $expected:literal) => {
#[test]
fn $name() {
let mut sim = celox::Simulation::from_sv_sources(
vec![($source, Path::new("displayed.sv"))],
"Top",
)
.plusargs(["+verbose", "+v=7"])
.$backend()
.unwrap();
sim.run_until(0).unwrap();
let lines: Vec<String> = sim
.drain_runtime_events()
.into_iter()
.filter_map(|event| match event {
celox::RuntimeEvent::Display { message } => {
Some(String::from_utf8_lossy(&message).into_owned())
}
_ => None,
})
.collect();
assert_eq!(lines, [$expected]);
}
};
}
display_plusargs!(display_of_plusargs_interpreter, build_interpreter);
display_plusargs!(display_of_plusargs_compiled, build);
#[cfg(target_arch = "x86_64")]
display_plusargs!(display_of_plusargs_native, build_native);
display_plusargs!(
ordered_arguments_interpreter,
build_interpreter,
ORDERED,
"3 1 5 7"
);
display_plusargs!(ordered_arguments_compiled, build, ORDERED, "3 1 5 7");
#[cfg(target_arch = "x86_64")]
display_plusargs!(ordered_arguments_native, build_native, ORDERED, "3 1 5 7");
Loading