summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/stages/compiler.rs5
-rw-r--r--src/stages/intermediate.rs16
-rw-r--r--src/stages/syntactic.rs20
-rw-r--r--src/stages/syntactic_tokens.rs3
-rw-r--r--src/types/word_template.rs34
5 files changed, 53 insertions, 25 deletions
diff --git a/src/stages/compiler.rs b/src/stages/compiler.rs
index 4777fb4..d4d28dd 100644
--- a/src/stages/compiler.rs
+++ b/src/stages/compiler.rs
@@ -221,10 +221,13 @@ impl SymbolParser {
}
BlockToken::WordTemplate(word_template) => {
for field in &word_template.fields {
+ // If a field is disjoint, the source span of the first range
+ // will identify the whole field.
+ let range = field.ranges.first().unwrap();
self.record_symbol(
&field.name.to_string(),
0,
- &field.source,
+ &range.source,
SymbolRoleDetailed::Invocation,
);
}
diff --git a/src/stages/intermediate.rs b/src/stages/intermediate.rs
index 3144b77..e48f31b 100644
--- a/src/stages/intermediate.rs
+++ b/src/stages/intermediate.rs
@@ -200,20 +200,24 @@ impl IntermediateParser {
let mut word_value = word_template.value;
for field in &word_template.fields {
let name = field.name.to_string();
- let field_source = &field.source;
+ let field_source = &field.ranges.first().unwrap().source;
let invocation = Invocation { name, arguments: Vec::new() };
if let Some(result) = self.parse_integer_invocation(&invocation, field_source) {
let field_value = **result;
// Inject field value into real value.
let value_width = width(field_value);
- if field.width < value_width {
- let error = IntermediateError::ValueTooWide(field.width, value_width);
+ if field.width() < value_width {
+ let error = IntermediateError::ValueTooWide(field.width(), value_width);
let new_source = field_source.wrap(result.source);
self.errors.push(Tracked::from(error, new_source));
} else {
- let mask = 2_usize.pow(field.width as u32) - 1;
- let clamped_value = (field_value as usize) & mask;
- word_value |= (clamped_value << field.shift) as usize;
+ let mut bits_mapped = 0;
+ for range in field.ranges.iter().rev() {
+ let range_mask = 2_usize.pow(range.width) - 1;
+ let range_bits = (field_value as usize >> bits_mapped) & range_mask;
+ word_value |= range_bits << range.shift;
+ bits_mapped += range.width;
+ }
}
}
}
diff --git a/src/stages/syntactic.rs b/src/stages/syntactic.rs
index a65faf9..9635c83 100644
--- a/src/stages/syntactic.rs
+++ b/src/stages/syntactic.rs
@@ -250,17 +250,17 @@ fn parse_word_template(mut t: Tokeniser) -> Result<WordTemplate, Vec<Tracked<Syn
let mut value_width = 0; // Bit width of the whole word template.
let mut field_width = 0; // Width of the current bit field.
let mut field_name = '\0'; // Name of the current bit field.
- let mut fields: Vec<Tracked<BitField>> = Vec::new();
+ let mut fields: Vec<BitField> = Vec::new();
let mut errors: Vec<Tracked<SyntacticError>> = Vec::new();
macro_rules! push_field {
() => {
- if fields.iter().any(|f| f.name == field_name) {
- let error = SyntacticError::DuplicateFieldNameInWord(field_name);
- errors.push(Tracked::from(error, t.get_source()));
+ let range = BitRange { width: field_width, shift: 0 };
+ let tracked = Tracked::from(range, t.get_source());
+ if let Some(field) = fields.iter_mut().find(|f| f.name == field_name) {
+ field.ranges.push(tracked);
} else {
- let field = BitField { name: field_name, width: field_width, shift: 0};
- fields.push(Tracked::from(field, t.get_source()));
+ fields.push(BitField { name: field_name, ranges: vec![tracked]});
}
};
}
@@ -276,7 +276,9 @@ fn parse_word_template(mut t: Tokeniser) -> Result<WordTemplate, Vec<Tracked<Syn
value <<= 1;
value_width += 1;
for field in &mut fields {
- field.shift += 1;
+ for range in &mut field.ranges {
+ range.shift += 1;
+ }
}
// Extend the current field.
@@ -316,7 +318,9 @@ fn parse_word_template(mut t: Tokeniser) -> Result<WordTemplate, Vec<Tracked<Syn
// Commit the final field.
for field in &mut fields {
- field.shift += 1;
+ for range in &mut field.ranges {
+ range.shift += 1;
+ }
}
if field_width > 0 {
t.mark_end();
diff --git a/src/stages/syntactic_tokens.rs b/src/stages/syntactic_tokens.rs
index 273dfe5..f8929b2 100644
--- a/src/stages/syntactic_tokens.rs
+++ b/src/stages/syntactic_tokens.rs
@@ -66,7 +66,6 @@ pub enum SyntacticError {
ExpectedSingleCharacter,
- DuplicateFieldNameInWord(char),
InvalidCharacterInWord(char),
InvalidDecimalLiteral(String),
@@ -112,8 +111,6 @@ fn report_syntactic_error(error: &Tracked<SyntacticError>, source_code: &str) {
SyntacticError::ExpectedSingleCharacter =>
"Character literal must contain exactly one character",
- SyntacticError::DuplicateFieldNameInWord(name) =>
- &format!("The field '{name}' has already been used in this word"),
SyntacticError::InvalidCharacterInWord(c) =>
&format!("The character '{c}' cannot be used in a word"),
diff --git a/src/types/word_template.rs b/src/types/word_template.rs
index 634e1d5..e319f05 100644
--- a/src/types/word_template.rs
+++ b/src/types/word_template.rs
@@ -6,18 +6,36 @@ pub struct WordTemplate {
pub value: usize,
/// Width of the word in bits.
pub width: u32,
- pub fields: Vec<Tracked<BitField>>,
+ pub fields: Vec<BitField>,
}
#[derive(Clone)]
pub struct BitField {
pub name: char,
- /// Width of the field in bits.
+ /// Guaranteed to contain at least one range. Field is disjoint if it
+ /// contains more than one range. Fields are ordered from most to least
+ /// significant.
+ pub ranges: Vec<Tracked<BitRange>>,
+}
+
+#[derive(Clone)]
+pub struct BitRange {
+ /// Width of the range in bits.
pub width: u32,
- /// Number of bits to the right of the field in the word.
+ /// Number of bits to the right of the range in the word.
pub shift: u32,
}
+impl BitField {
+ pub fn width(&self) -> u32 {
+ let mut width = 0;
+ for range in &self.ranges {
+ width += range.width;
+ }
+ return width;
+ }
+}
+
impl std::fmt::Display for WordTemplate {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
@@ -31,10 +49,12 @@ impl std::fmt::Display for WordTemplate {
write!(f, "_")?;
}
for field in &self.fields {
- let i = i as u32;
- if i <= field.width + field.shift - 1 && i >= field.shift {
- write!(f, "{}", field.name)?;
- continue 'bit;
+ for range in &field.ranges {
+ let i = i as u32;
+ if i <= range.width + range.shift - 1 && i >= range.shift {
+ write!(f, "{}", field.name)?;
+ continue 'bit;
+ }
}
}
match (self.value >> i) & 1 {