2022-07-18 16:53:44 +00:00
|
|
|
//! part of the vcd parser that handles parsing the signal tree and
|
|
|
|
//! building the resulting signal tree
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub(super) fn parse_var<'a>(
|
2022-08-11 21:35:40 +00:00
|
|
|
word_reader: &mut WordReader,
|
|
|
|
parent_scope_idx: ScopeIdx,
|
|
|
|
vcd: &'a mut VCD,
|
|
|
|
signal_map: &mut HashMap<String, SignalIdx>,
|
2022-07-18 16:53:44 +00:00
|
|
|
) -> Result<(), String> {
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = next_word!(word_reader)?;
|
|
|
|
let expected_types = [
|
|
|
|
"integer",
|
|
|
|
"parameter",
|
|
|
|
"real",
|
|
|
|
"reg",
|
|
|
|
"string",
|
|
|
|
"wire",
|
|
|
|
"tri1",
|
|
|
|
"time",
|
|
|
|
];
|
2022-07-18 16:53:44 +00:00
|
|
|
|
|
|
|
// $var parameter 3 a IDLE $end
|
|
|
|
// ^^^^^^^^^ - var_type
|
|
|
|
let var_type = match word {
|
2022-08-11 21:35:40 +00:00
|
|
|
"integer" => Ok(SigType::Integer),
|
|
|
|
"parameter" => Ok(SigType::Parameter),
|
|
|
|
"real" => Ok(SigType::Real),
|
|
|
|
"reg" => Ok(SigType::Reg),
|
|
|
|
"string" => Ok(SigType::Str),
|
|
|
|
"wire" => Ok(SigType::Wire),
|
|
|
|
"tri1" => Ok(SigType::Tri1),
|
|
|
|
"time" => Ok(SigType::Time),
|
2022-07-18 16:53:44 +00:00
|
|
|
_ => {
|
2022-08-11 21:35:40 +00:00
|
|
|
let err = format!(
|
|
|
|
"Error near {}:{} \
|
2022-08-04 17:19:52 +00:00
|
|
|
found keyword `{word}` but expected one of \
|
2022-08-11 21:35:40 +00:00
|
|
|
{expected_types:?} on {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
2022-07-18 16:53:44 +00:00
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}?;
|
|
|
|
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = next_word!(word_reader)?;
|
|
|
|
|
2022-07-18 16:53:44 +00:00
|
|
|
let parse_err = format!("failed to parse as usize on {cursor:?}");
|
|
|
|
|
|
|
|
// $var parameter 3 a IDLE $end
|
2022-08-22 14:08:27 +00:00
|
|
|
// ^ - num_bits
|
|
|
|
let num_bits = match var_type {
|
2022-08-11 21:35:40 +00:00
|
|
|
SigType::Integer
|
|
|
|
| SigType::Parameter
|
|
|
|
| SigType::Real
|
|
|
|
| SigType::Reg
|
|
|
|
| SigType::Wire
|
|
|
|
| SigType::Tri1
|
|
|
|
| SigType::Time => {
|
2022-08-22 14:08:27 +00:00
|
|
|
let num_bits = word.parse::<usize>().expect(parse_err.as_str());
|
|
|
|
let num_bits = u16::try_from(num_bits).map_err(|_| {
|
2022-08-20 01:13:29 +00:00
|
|
|
format!(
|
|
|
|
"Error near {}:{} while parsing vcd file at {cursor:?}. \
|
2022-08-22 14:08:27 +00:00
|
|
|
This signal has {num_bits} > 2^16 - 1 bits.",
|
2022-08-20 01:13:29 +00:00
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
)
|
|
|
|
})?;
|
2022-08-22 14:08:27 +00:00
|
|
|
Some(num_bits)
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
// for strings, we don't really care what the number of bits is
|
2022-08-11 21:35:40 +00:00
|
|
|
_ => None,
|
2022-07-18 16:53:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// $var parameter 3 a IDLE $end
|
|
|
|
// ^ - signal_alias
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, _) = next_word!(word_reader)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
let signal_alias = word.to_string();
|
|
|
|
|
|
|
|
// $var parameter 3 a IDLE $end
|
|
|
|
// ^^^^ - full_signal_name(can extend until $end)
|
|
|
|
let mut full_signal_name = Vec::<String>::new();
|
|
|
|
loop {
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, _) = next_word!(word_reader)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
match word {
|
2022-08-11 21:35:40 +00:00
|
|
|
"$end" => break,
|
|
|
|
_ => full_signal_name.push(word.to_string()),
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let full_signal_name = full_signal_name.join(" ");
|
|
|
|
|
2022-08-22 14:08:27 +00:00
|
|
|
let num_bytes = if num_bits.is_some() {
|
|
|
|
let bytes_required = Signal::bytes_required(num_bits.unwrap(), &full_signal_name)?;
|
2022-08-22 13:48:56 +00:00
|
|
|
Some(bytes_required)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2022-07-18 16:53:44 +00:00
|
|
|
// Is the current variable an alias to a signal already encountered?
|
|
|
|
// if so, handle ref_signal_idx accordingly, if not, add signal to hash
|
|
|
|
// map
|
|
|
|
let (signal, signal_idx) = match signal_map.get(&signal_alias) {
|
|
|
|
Some(ref_signal_idx) => {
|
2022-08-02 23:31:35 +00:00
|
|
|
let signal_idx = SignalIdx(vcd.all_signals.len());
|
2022-08-11 21:35:40 +00:00
|
|
|
let signal = Signal::Alias {
|
2022-07-18 16:53:44 +00:00
|
|
|
name: full_signal_name,
|
2022-08-11 21:35:40 +00:00
|
|
|
signal_alias: *ref_signal_idx,
|
|
|
|
};
|
2022-07-18 16:53:44 +00:00
|
|
|
(signal, signal_idx)
|
|
|
|
}
|
|
|
|
None => {
|
2022-08-02 23:31:35 +00:00
|
|
|
let signal_idx = SignalIdx(vcd.all_signals.len());
|
2022-07-18 16:53:44 +00:00
|
|
|
signal_map.insert(signal_alias.to_string(), signal_idx);
|
2022-08-11 21:35:40 +00:00
|
|
|
let signal = Signal::Data {
|
2022-07-18 16:53:44 +00:00
|
|
|
name: full_signal_name,
|
|
|
|
sig_type: var_type,
|
2022-07-30 23:58:54 +00:00
|
|
|
signal_error: None,
|
2022-08-22 14:08:27 +00:00
|
|
|
num_bits: num_bits,
|
2022-08-22 13:48:56 +00:00
|
|
|
num_bytes: num_bytes,
|
2022-07-18 16:53:44 +00:00
|
|
|
self_idx: signal_idx,
|
2022-08-20 00:13:46 +00:00
|
|
|
nums_encoded_as_fixed_width_le_u8: vec![],
|
|
|
|
string_vals: vec![],
|
|
|
|
lsb_indxs_of_num_tmstmp_vals_on_tmln: vec![],
|
|
|
|
byte_len_of_num_tmstmp_vals_on_tmln: vec![],
|
|
|
|
lsb_indxs_of_string_tmstmp_vals_on_tmln: vec![],
|
|
|
|
byte_len_of_string_tmstmp_vals_on_tmln: vec![],
|
2022-08-11 21:35:40 +00:00
|
|
|
scope_parent: parent_scope_idx,
|
|
|
|
};
|
2022-07-18 16:53:44 +00:00
|
|
|
(signal, signal_idx)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vcd.all_signals.push(signal);
|
2022-08-02 23:31:35 +00:00
|
|
|
let ScopeIdx(parent_scope_idx_usize) = parent_scope_idx;
|
2022-07-18 16:53:44 +00:00
|
|
|
let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx_usize).unwrap();
|
|
|
|
parent_scope.child_signals.push(signal_idx);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:40:40 +00:00
|
|
|
/// Sometimes, variables can be listed outside of scopes.
|
|
|
|
/// We call these orphaned vars.
|
|
|
|
fn parse_orphaned_vars<'a>(
|
2022-08-11 21:35:40 +00:00
|
|
|
word_reader: &mut WordReader,
|
|
|
|
vcd: &'a mut VCD,
|
|
|
|
signal_map: &mut HashMap<String, SignalIdx>,
|
2022-08-02 20:40:40 +00:00
|
|
|
) -> Result<(), String> {
|
|
|
|
// create scope for unscoped signals if such a scope does not
|
|
|
|
// yet exist
|
|
|
|
let scope_name = "Orphaned Signals";
|
|
|
|
|
|
|
|
// set default scope_idx to the count of existing scope as we
|
|
|
|
// generally set scope.self_idx to the number of existing scopes
|
|
|
|
// when that particular scope was inserted
|
2022-08-02 23:31:35 +00:00
|
|
|
let mut scope_idx = ScopeIdx(vcd.all_scopes.len());
|
2022-08-02 20:40:40 +00:00
|
|
|
|
|
|
|
// Override scope_idx if we find a scope named "Orphaned Signals"
|
|
|
|
// already exists
|
|
|
|
let mut scope_already_exists = false;
|
|
|
|
for scope in &vcd.all_scopes {
|
|
|
|
if scope.name == scope_name {
|
|
|
|
scope_idx = scope.self_idx;
|
|
|
|
scope_already_exists = true;
|
2022-08-11 21:35:40 +00:00
|
|
|
break;
|
2022-08-02 20:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !scope_already_exists {
|
2022-08-11 21:35:40 +00:00
|
|
|
vcd.all_scopes.push(Scope {
|
|
|
|
name: scope_name.to_string(),
|
|
|
|
parent_idx: None,
|
|
|
|
self_idx: scope_idx,
|
|
|
|
child_signals: vec![],
|
|
|
|
child_scopes: vec![],
|
|
|
|
});
|
2022-08-20 00:13:46 +00:00
|
|
|
vcd.root_scopes.push(scope_idx);
|
2022-08-02 20:40:40 +00:00
|
|
|
}
|
2022-08-11 21:35:40 +00:00
|
|
|
|
2022-08-02 20:40:40 +00:00
|
|
|
// we can go ahead and parse the current var as we've already encountered
|
|
|
|
// "$var" before now.
|
|
|
|
parse_var(word_reader, scope_idx, vcd, signal_map)?;
|
|
|
|
|
|
|
|
loop {
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = next_word!(word_reader)?;
|
2022-08-02 20:40:40 +00:00
|
|
|
|
|
|
|
match word {
|
|
|
|
"$var" => {
|
|
|
|
parse_var(word_reader, scope_idx, vcd, signal_map)?;
|
|
|
|
}
|
2022-08-11 21:35:40 +00:00
|
|
|
"$scope" => break,
|
2022-08-02 20:40:40 +00:00
|
|
|
_ => {
|
2022-08-11 21:35:40 +00:00
|
|
|
let msg = format!(
|
|
|
|
"Error near {}:{}.\
|
2022-08-04 17:19:52 +00:00
|
|
|
Expected $scope or $var, found \
|
2022-08-11 21:35:40 +00:00
|
|
|
{word} at {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
2022-08-02 20:40:40 +00:00
|
|
|
Err(msg)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:35:40 +00:00
|
|
|
fn parse_scopes_inner<'a>(
|
|
|
|
word_reader: &mut WordReader,
|
|
|
|
parent_scope_idx: Option<ScopeIdx>,
|
|
|
|
vcd: &'a mut VCD,
|
|
|
|
signal_map: &mut HashMap<String, SignalIdx>,
|
2022-07-18 16:53:44 +00:00
|
|
|
) -> Result<(), String> {
|
|
|
|
// $scope module reg_mag_i $end
|
|
|
|
// ^^^^^^ - module keyword
|
2022-08-11 21:35:40 +00:00
|
|
|
let (keyword, cursor) = next_word!(word_reader)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
|
|
|
|
let expected = ["module", "begin", "task", "function"];
|
|
|
|
if expected.contains(&keyword) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2022-08-11 21:35:40 +00:00
|
|
|
let err = format!(
|
|
|
|
"Error near {}:{}. \
|
2022-08-04 17:19:52 +00:00
|
|
|
found keyword `{keyword}` but expected one of \
|
2022-08-11 21:35:40 +00:00
|
|
|
{expected:?} on {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
2022-07-18 16:53:44 +00:00
|
|
|
Err(err)
|
|
|
|
}?;
|
|
|
|
|
|
|
|
// $scope module reg_mag_i $end
|
|
|
|
// ^^^^^^^^^ - scope name
|
2022-08-11 21:35:40 +00:00
|
|
|
let (scope_name, _) = next_word!(word_reader)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
|
2022-08-02 23:31:35 +00:00
|
|
|
let curr_scope_idx = ScopeIdx(vcd.all_scopes.len());
|
2022-08-11 21:35:40 +00:00
|
|
|
|
2022-07-18 16:53:44 +00:00
|
|
|
// register this scope as a child of the current parent scope
|
|
|
|
// if there is a parent scope, or else we register this scope as
|
|
|
|
// root scope
|
|
|
|
match parent_scope_idx {
|
2022-08-02 23:31:35 +00:00
|
|
|
Some(ScopeIdx(parent_scope_idx)) => {
|
2022-07-18 16:53:44 +00:00
|
|
|
let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx).unwrap();
|
|
|
|
parent_scope.child_scopes.push(curr_scope_idx);
|
|
|
|
}
|
2022-08-20 00:13:46 +00:00
|
|
|
None => vcd.root_scopes.push(curr_scope_idx),
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add this scope to list of existing scopes
|
2022-08-11 21:35:40 +00:00
|
|
|
vcd.all_scopes.push(Scope {
|
|
|
|
name: scope_name.to_string(),
|
|
|
|
parent_idx: parent_scope_idx,
|
|
|
|
self_idx: curr_scope_idx,
|
|
|
|
child_signals: vec![],
|
|
|
|
child_scopes: vec![],
|
|
|
|
});
|
2022-07-18 16:53:44 +00:00
|
|
|
|
|
|
|
// $scope module reg_mag_i $end
|
|
|
|
// ^^^^ - end keyword
|
|
|
|
ident(word_reader, "$end")?;
|
|
|
|
|
|
|
|
loop {
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = next_word!(word_reader)?;
|
|
|
|
let ParseResult { matched, residual } = tag(word, "$");
|
2022-07-18 16:53:44 +00:00
|
|
|
match matched {
|
2022-08-01 14:11:05 +00:00
|
|
|
// we hope that this word starts with a `$`
|
2022-08-11 21:35:40 +00:00
|
|
|
"$" => {
|
2022-07-18 16:53:44 +00:00
|
|
|
match residual {
|
|
|
|
"scope" => {
|
|
|
|
// recursive - parse inside of current scope tree
|
2022-08-11 21:35:40 +00:00
|
|
|
parse_scopes_inner(word_reader, Some(curr_scope_idx), vcd, signal_map)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
"var" => {
|
|
|
|
parse_var(word_reader, curr_scope_idx, vcd, signal_map)?;
|
|
|
|
}
|
|
|
|
"upscope" => {
|
|
|
|
ident(word_reader, "$end")?;
|
2022-08-11 21:35:40 +00:00
|
|
|
break;
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
// we ignore comments
|
2022-08-11 21:35:40 +00:00
|
|
|
"comment" => loop {
|
|
|
|
if ident(word_reader, "$end").is_ok() {
|
|
|
|
break;
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
2022-08-11 21:35:40 +00:00
|
|
|
},
|
2022-07-18 16:53:44 +00:00
|
|
|
_ => {
|
2022-08-11 21:35:40 +00:00
|
|
|
let err = format!(
|
|
|
|
"Error near {}:{}. \
|
2022-08-04 17:19:52 +00:00
|
|
|
found keyword `{residual}` but expected \
|
|
|
|
`$scope`, `$var`, `$comment`, or `$upscope` \
|
2022-08-11 21:35:40 +00:00
|
|
|
on {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
|
|
|
return Err(err);
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2022-08-11 21:35:40 +00:00
|
|
|
let err = format!(
|
|
|
|
"Error near {}:{}. \
|
2022-08-04 17:19:52 +00:00
|
|
|
found keyword `{matched}` but \
|
2022-08-11 21:35:40 +00:00
|
|
|
expected `$` on {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
|
|
|
return Err(err);
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn parse_scopes<'a>(
|
2022-08-11 21:35:40 +00:00
|
|
|
word_reader: &mut WordReader,
|
|
|
|
vcd: &'a mut VCD,
|
|
|
|
signal_map: &mut HashMap<String, SignalIdx>,
|
2022-07-18 16:53:44 +00:00
|
|
|
) -> Result<(), String> {
|
2022-08-02 20:40:40 +00:00
|
|
|
// get the current word
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = curr_word!(word_reader)?;
|
2022-08-02 20:40:40 +00:00
|
|
|
|
|
|
|
// we may have orphaned vars that occur before the first scope
|
|
|
|
if word == "$var" {
|
|
|
|
parse_orphaned_vars(word_reader, vcd, signal_map)?;
|
2022-08-11 21:35:40 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:40:40 +00:00
|
|
|
// get the current word
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = curr_word!(word_reader)?;
|
2022-08-02 20:40:40 +00:00
|
|
|
|
|
|
|
// the current word should be "scope", as `parse_orphaned_vars`(if it
|
|
|
|
// was called), should have terminated upon encountering "$scope".
|
|
|
|
// If `parse_orphaned_vars` was not called, `parse_scopes` should still
|
|
|
|
// have only been called if the caller encountered the word "$scope"
|
|
|
|
if word != "$scope" {
|
2022-08-11 21:35:40 +00:00
|
|
|
let msg = format!(
|
|
|
|
"Error near {}:{}.\
|
2022-08-04 17:19:52 +00:00
|
|
|
Expected $scope or $var, found \
|
2022-08-11 21:35:40 +00:00
|
|
|
{word} at {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
|
|
|
return Err(msg);
|
2022-08-02 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now for the interesting part
|
2022-08-11 21:35:40 +00:00
|
|
|
parse_scopes_inner(word_reader, None, vcd, signal_map)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
|
2022-08-04 15:36:26 +00:00
|
|
|
// let err = format!("reached end of file without parser leaving {}", function_name!());
|
2022-07-18 16:53:44 +00:00
|
|
|
let expected_keywords = ["$scope", "$enddefinitions"];
|
|
|
|
|
2022-08-02 20:40:40 +00:00
|
|
|
// there could be multiple signal trees, and unfortunately, we
|
2022-08-11 21:35:40 +00:00
|
|
|
// can't merge the earlier call to `parse_scopes_inner` into this loop
|
|
|
|
// because this loop gets a word from `next_word` instead of
|
2022-08-02 20:40:40 +00:00
|
|
|
// `curr_word()`.
|
2022-07-18 16:53:44 +00:00
|
|
|
loop {
|
2022-08-11 21:35:40 +00:00
|
|
|
let (word, cursor) = next_word!(word_reader)?;
|
|
|
|
|
2022-07-18 16:53:44 +00:00
|
|
|
match word {
|
|
|
|
"$scope" => {
|
2022-08-11 21:35:40 +00:00
|
|
|
parse_scopes_inner(word_reader, None, vcd, signal_map)?;
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
"$enddefinitions" => {
|
|
|
|
ident(word_reader, "$end")?;
|
2022-08-11 21:35:40 +00:00
|
|
|
break;
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
"comment" => {
|
2022-08-02 20:40:40 +00:00
|
|
|
// although we don't store comments, we still need to advance the
|
|
|
|
// word_reader cursor to the end of the comment
|
2022-07-18 16:53:44 +00:00
|
|
|
loop {
|
2022-08-11 21:35:40 +00:00
|
|
|
if ident(word_reader, "$end").is_ok() {
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2022-08-11 21:35:40 +00:00
|
|
|
let err = format!(
|
|
|
|
"Error near {}:{} \
|
2022-08-04 17:19:52 +00:00
|
|
|
found keyword `{word}` but expected one of \
|
2022-08-11 21:35:40 +00:00
|
|
|
{expected_keywords:?} on {cursor:?}",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
);
|
|
|
|
return Err(err);
|
2022-07-18 16:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-08-11 21:35:40 +00:00
|
|
|
}
|