Skip to content

Commit

Permalink
Merge pull request #11 from rameloni/fix-idx-outofbounds-builder-10
Browse files Browse the repository at this point in the history
[Bug fix] Index out of bounds in builder.rs
  • Loading branch information
rameloni authored Jun 10, 2024
2 parents 6a6896d + 67b92c7 commit 7482091
Show file tree
Hide file tree
Showing 5 changed files with 1,344 additions and 11 deletions.
44 changes: 34 additions & 10 deletions src/tyvcd/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ impl TyVcdBuilder<hgldd::Hgldd> {

// Build the fields of the struct
let mut fields: Vec<Variable> = Vec::with_capacity(obj.port_vars.len());

// If the struct has an unpacked range, get the sub-sub-expressions
let expressions = if hgldd_var.unpacked_range.is_some() {
helper::get_sub_expressions(expressions.first())
} else {
expressions
};

#[allow(clippy::needless_range_loop)]
for i in 0..obj.port_vars.len() {
let mut var = obj.port_vars[i].clone();
Expand Down Expand Up @@ -409,6 +417,29 @@ mod helper {
}
}

/// Extract the sub-expressions from an hgldd expression.
///
/// # Example
/// ```json
/// "value": {
// "opcode": "'{",
// "operands": [
// { "sig_name": "io_a_0" },
// {
// "opcode": "'{",
// "operands": [
// {
// "opcode": "'{",
// "operands": [ { "sig_name": "io_b_b_vec_0_0" }, { "sig_name": "io_b_b_vec_1_0" }]
// }
// ]
// }
// ]
// }
/// ```
/// Its call in sequence will return:
/// 1. [io_a_0, {io_b_b_vec_0_0, io_b_b_vec_1_0}]
/// 2. [io_b_b_vec_0_0, io_b_b_vec_1_0]
#[inline]
pub(in crate::tyvcd) fn get_sub_expressions(
expression: Option<&hgldd::Expression>,
Expand All @@ -420,16 +451,9 @@ mod helper {
| hgldd::Expression::BitVector(_)
| hgldd::Expression::IntegerNum(_) => std::slice::from_ref(expression),
// This variable contains an operator, this means it contains the "values" of all its child variables (to be added in kind)
hgldd::Expression::Operator { opcode, operands } => match opcode {
&hgldd::Opcode::Struct => {
// if operands.len() == 1 {
// get_sub_expressions(&operands.first().cloned())
// } else {
operands.as_slice()
// }
}
_ => operands.as_slice(),
},
hgldd::Expression::Operator { opcode, operands } => {
operands.as_slice() // TODO: check if the opcode is needed here
}
}
} else {
&[]
Expand Down
1 change: 1 addition & 0 deletions tests/expected_tyvcd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bar;
pub mod foo;
pub mod with_bundles_and_vecs;
pub mod vec_test;
Loading

0 comments on commit 7482091

Please sign in to comment.