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
49 changes: 49 additions & 0 deletions crates/celox-frontend-veryl/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,53 @@ pub fn schedule_symbolic_rtl(
trace,
)?;
let lookup = &output.scheduled.frontend_lookup;
let mut constant_strings = crate::HashMap::default();
for (&instance, module_id) in &lookup.instance_module {
let Some(module) = module_ir.get(module_id) else {
continue;
};
let Some(variables) = source_id_maps.get(module_id) else {
continue;
};
for declaration in &module.declarations {
let Declaration::Comb(comb) = declaration else {
continue;
};
for statement in &comb.statements {
let veryl_analyzer::ir::Statement::Assign(assign) = statement else {
continue;
};
if !assign.expr.comptime().r#type.is_string()
|| assign.hier_dst.is_some()
|| assign.dst.len() != 1
{
continue;
}
let dst = &assign.dst[0];
let Some(variable) = module.variables.get(&dst.id) else {
continue;
};
if !matches!(variable.kind, veryl_analyzer::ir::VarKind::Let) {
continue;
}
if !assign.expr.comptime().is_const
|| assign.expr.comptime().get_value().is_err()
|| variable.r#type.is_array()
{
return Err(ParserError::illegal_context(
"string let initializer",
"Celox currently supports only constant scalar string let initializers",
Some(&variable.token),
));
}
if let Some(id) = variables.get(&dst.id)
&& let Some((address, _)) = lookup.instance_variable(instance, *id)
{
constant_strings.insert(address, assign.expr.clone());
}
}
}
}
let mut child_sources = Vec::new();
let mut components = Vec::new();
let mut component_bindings = Vec::new();
Expand All @@ -97,6 +144,7 @@ pub fn schedule_symbolic_rtl(
.collect();
if blocks.iter().any(|block| !block.is_empty()) {
child_sources.push(VerylTestbenchSource {
constant_strings: constant_strings.clone(),
id_map: VerylIdMap {
module_variables: source_id_maps
.get(module_id)
Expand Down Expand Up @@ -132,6 +180,7 @@ pub fn schedule_symbolic_rtl(
component_bindings.append(&mut instance_bindings);
}
let testbench_source = VerylTestbenchSource {
constant_strings,
id_map: VerylIdMap {
module_variables: source_id_maps,
},
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-frontend-veryl/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl VerylIdMap {
#[derive(Clone, Default)]
pub struct VerylTestbenchSource {
pub id_map: VerylIdMap,
/// Constant scalar string lets are continuous assignments, outside initial blocks.
pub constant_strings: HashMap<celox_design::StateAddr, Expression>,
pub initial_statements: Option<Vec<Statement>>,
/// Length of each initial block in `initial_statements`; empty means one block.
pub initial_block_lengths: Vec<usize>,
Expand Down
186 changes: 177 additions & 9 deletions crates/celox-frontend-veryl/src/testbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ struct ExprCompiler<'a> {
lookup: &'a FrontendLookup,
id_map: &'a VerylIdMap,
functions: &'a HashMap<VarId, Function>,
constant_strings: &'a HashMap<StateAddr, Expression>,
base_instance: InstanceId,
}

Expand Down Expand Up @@ -1167,6 +1168,13 @@ impl ExprCompiler<'_> {
ops: &mut Vec<UnboundTbOpcode>,
context: Option<ValueContext>,
) -> ValueContext {
// A string's declared width is nominal; its runtime length must never
// pass through integer context sizing or assignment truncation.
let context = if expr.comptime().r#type.is_string() {
None
} else {
context
};
match expr {
Expression::Term(f) => {
self.emit_factor(f, ops);
Expand Down Expand Up @@ -1242,6 +1250,19 @@ impl ExprCompiler<'_> {
return self.resize_result(ops, casted, context);
}

if lhs.comptime().r#type.is_string() && rhs.comptime().r#type.is_string() {
self.emit_in_context(lhs, ops, None);
self.emit_in_context(rhs, ops, None);
ops.push(TbOpcode::BinOp(lower_testbench_operator(*op)));
return self.resize_result(
ops,
ValueContext {
width: 1,
signed: false,
},
context,
);
}
let lhs_width = self.natural_width(lhs);
let rhs_width = self.natural_width(rhs);
let semantics = binary_semantics(
Expand Down Expand Up @@ -1351,6 +1372,40 @@ impl ExprCompiler<'_> {
}

fn emit_factor(&self, factor: &Factor, ops: &mut Vec<UnboundTbOpcode>) {
if factor.comptime().r#type.is_string() {
match factor {
Factor::Variable(id, index, select, comptime) if !comptime.is_const => {
let (address, info) =
self.local_variable(*id).expect("string variable metadata");
self.emit_string_access(address, info, index, select, ops);
return;
}
Factor::HierVariable(reference) => {
let (address, info) = self
.hierarchical_variable(reference)
.expect("string variable metadata");
self.emit_string_access(
address,
info,
&reference.index,
&reference.select,
ops,
);
return;
}
_ => {
if let Ok(value) = factor.comptime().get_value() {
let len = value.width().div_ceil(8);
let bytes = value.payload().to_bytes_be();
let mut result = vec![0; len];
let copied = len.min(bytes.len());
result[len - copied..].copy_from_slice(&bytes[bytes.len() - copied..]);
ops.push(TbOpcode::ConstString(result));
return;
}
}
}
}
match factor {
Factor::Variable(var_id, index, select, comptime) => {
if comptime.is_const
Expand Down Expand Up @@ -1402,6 +1457,41 @@ impl ExprCompiler<'_> {
}
}

fn emit_string_access(
&self,
address: StateAddr,
info: &VariableInfo,
index: &VarIndex,
select: &VarSelect,
ops: &mut Vec<UnboundTbOpcode>,
) {
if let Some(expression) = self.constant_strings.get(&address) {
self.emit(expression, ops);
return;
}

let target = self
.resolve_target_parts(
SemanticSignal {
address,
width: info.width,
},
info,
index,
select,
)
.expect("validated string access");
if let Some(selection) = target.selection {
ops.extend_from_slice(selection.offset.ops());
} else {
ops.push(TbOpcode::ConstU64(0));
}
ops.push(TbOpcode::LoadString {
location: Self::state_location_at(address, 0),
elements: info.width,
});
}

fn emit_constant_value(
&self,
value: &veryl_analyzer::value::Value,
Expand Down Expand Up @@ -1452,10 +1542,16 @@ impl ExprCompiler<'_> {
signed: expression_signed(arg_expr),
}),
);
ops.push(TbOpcode::StoreU64 {
location: self.state_location(arg_var_id, 0),
byte_size: get_byte_size(sig.width),
});
if arg_expr.comptime().r#type.is_string() {
ops.push(TbOpcode::StoreString {
location: self.state_location(arg_var_id, 0),
});
} else {
ops.push(TbOpcode::StoreU64 {
location: self.state_location(arg_var_id, 0),
byte_size: get_byte_size(sig.width),
});
}
}
}
}
Expand All @@ -1473,10 +1569,16 @@ impl ExprCompiler<'_> {
signed: expression_signed(&a.expr),
}),
);
ops.push(TbOpcode::StoreU64 {
location: self.state_location(first_dst.id, 0),
byte_size: get_byte_size(dst_sig.width),
});
if a.expr.comptime().r#type.is_string() {
ops.push(TbOpcode::StoreString {
location: self.state_location(first_dst.id, 0),
});
} else {
ops.push(TbOpcode::StoreU64 {
location: self.state_location(first_dst.id, 0),
byte_size: get_byte_size(dst_sig.width),
});
}
}
}
}
Expand All @@ -1487,7 +1589,15 @@ impl ExprCompiler<'_> {
// 3. Load return value
if let Some(ret_var_id) = &func_body.ret {
if let Some(sig) = self.resolve_var(ret_var_id) {
self.emit_load(*ret_var_id, 0, sig.width, ops);
if fc.comptime.r#type.is_string() {
ops.push(TbOpcode::ConstU64(0));
ops.push(TbOpcode::LoadString {
location: self.state_location(*ret_var_id, 0),
elements: 1,
});
} else {
self.emit_load(*ret_var_id, 0, sig.width, ops);
}
} else {
ops.push(TbOpcode::ConstU64(0));
}
Expand Down Expand Up @@ -2520,6 +2630,7 @@ impl<'a> SemanticTestbenchBuilder<'a> {
let ec = ExprCompiler {
lookup: self.lookup,
id_map: &self.testbench_source.id_map,
constant_strings: &self.testbench_source.constant_strings,
functions: &self.testbench_source.functions,
base_instance,
};
Expand Down Expand Up @@ -2549,6 +2660,7 @@ impl<'a> SemanticTestbenchBuilder<'a> {
let ec = ExprCompiler {
lookup: self.lookup,
id_map: &self.testbench_source.id_map,
constant_strings: &self.testbench_source.constant_strings,
functions: &component.functions,
base_instance: component.parent_instance,
};
Expand Down Expand Up @@ -3149,6 +3261,37 @@ fn validate_testbench_expression(
source: &VerylTestbenchSource,
active_functions: &mut FxHashSet<(VarId, Option<Vec<usize>>)>,
) -> Result<(), ParserError> {
if let Expression::Term(factor) = expression
&& factor.comptime().r#type.is_string()
{
let access = match factor.as_ref() {
Factor::Variable(id, index, select, _) => source
.id_map
.instance_var(lookup, source.base_instance(lookup), *id)
.and_then(|id| lookup.instance_variable(source.base_instance(lookup), id))
.map(|(_, info)| (info, index, select)),
Factor::HierVariable(reference) => Some((
resolve_hierarchical_reference_from(
lookup,
source.base_instance(lookup),
reference,
)?
.1,
&reference.index,
&reference.select,
)),
_ => None,
};
if let Some((info, index, select)) = access
&& (index.0.len() > info.array_dims.len() || !select.0.is_empty() || select.1.is_some())
{
return Err(ParserError::illegal_context(
"string character selection",
"character indexing and slicing of strings are not supported in Celox testbenches",
Some(&factor.comptime().token),
));
}
}
match expression {
Expression::Term(factor) => match factor.as_ref() {
Factor::HierVariable(reference) => {
Expand Down Expand Up @@ -3205,9 +3348,23 @@ fn validate_testbench_expression(
Factor::Value(_) | Factor::Anonymous(_) | Factor::Unknown(_) => Ok(()),
},
Expression::Unary(_, inner, _) => {
if inner.comptime().r#type.is_string() {
return Err(ParserError::illegal_context(
"unary string operation",
"unary operations on strings are not supported in Celox testbenches",
Some(&inner.comptime().token),
));
}
validate_testbench_expression(inner, lookup, source, active_functions)
}
Expression::Binary(lhs, _, rhs, _) => {
if lhs.comptime().r#type.is_string() != rhs.comptime().r#type.is_string() {
return Err(ParserError::illegal_context(
"mixed string operation",
"operations mixing string and non-string operands are not supported in Celox testbenches",
Some(&expression.comptime().token),
));
}
validate_testbench_expression(lhs, lookup, source, active_functions)?;
validate_testbench_expression(rhs, lookup, source, active_functions)
}
Expand All @@ -3218,6 +3375,13 @@ fn validate_testbench_expression(
}
Expression::Concatenation(items, _) => {
for (expression, repeat) in items {
if expression.comptime().r#type.is_string() {
return Err(ParserError::illegal_context(
"string concatenation or replication",
"string concatenation and replication are not supported in Celox testbenches",
Some(&expression.comptime().token),
));
}
validate_testbench_expression(expression, lookup, source, active_functions)?;
if let Some(repeat) = repeat {
validate_testbench_expression(repeat, lookup, source, active_functions)?;
Expand Down Expand Up @@ -3515,6 +3679,7 @@ fn validate_testbench_hierarchical_destination(
let compiler = ExprCompiler {
lookup,
id_map: &source.id_map,
constant_strings: &source.constant_strings,
functions: &source.functions,
base_instance: source.base_instance(lookup),
};
Expand Down Expand Up @@ -3546,6 +3711,7 @@ fn validate_testbench_destination(
ExprCompiler {
lookup,
id_map: &source.id_map,
constant_strings: &source.constant_strings,
functions: &source.functions,
base_instance: source.base_instance(lookup),
}
Expand Down Expand Up @@ -3737,6 +3903,7 @@ mod tests {
let compiler = ExprCompiler {
lookup: &lookup,
id_map: &source.id_map,
constant_strings: &source.constant_strings,
functions: &source.functions,
base_instance: lookup.root_instance_and_module().unwrap().0,
};
Expand All @@ -3756,6 +3923,7 @@ mod tests {
let compiler = ExprCompiler {
lookup: &lookup,
id_map: &source.id_map,
constant_strings: &source.constant_strings,
functions: &source.functions,
base_instance: lookup.root_instance_and_module().unwrap().0,
};
Expand Down
Loading
Loading