diff options
| author | Ben Bridle <ben@derelict.engineering> | 2026-11-09 16:21:17 +1300 |
|---|---|---|
| committer | Ben Bridle <ben@derelict.engineering> | 2026-11-09 16:21:17 +1300 |
| commit | b469d0595321c44723075632fa5414d2339cc629 (patch) | |
| tree | 87ba834df68c282e9502b6beca09601f7407276c /src/types | |
| parent | aecc8e439f7360be65d37aa88c0c522434e00881 (diff) | |
| download | torque-asm-b469d0595321c44723075632fa5414d2339cc629.zip | |
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.
Diffstat (limited to 'src/types')
| -rw-r--r-- | src/types/word_template.rs | 34 |
1 files changed, 27 insertions, 7 deletions
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 { |
