From b469d0595321c44723075632fa5414d2339cc629 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Mon, 9 Nov 2026 16:21:17 +1300 Subject: Add support for disjoint fields in word templates The AVR instruction set requires that fields in some instructions are split across multiple ranges, requiring words that look like: #0001_11rd_dddd_rrrr This commit makes it possible to define fields with this syntax. The syntactic error DuplicateFieldNameInWord has been removed. --- src/stages/compiler.rs | 5 ++++- src/stages/intermediate.rs | 16 ++++++++++------ src/stages/syntactic.rs | 20 ++++++++++++-------- src/stages/syntactic_tokens.rs | 3 --- 4 files changed, 26 insertions(+), 18 deletions(-) (limited to 'src/stages') 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> = Vec::new(); + let mut fields: Vec = Vec::new(); let mut errors: Vec> = 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 Result 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, 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"), -- cgit v1.3.1