diff options
| author | Ben Bridle <ben@derelict.engineering> | 2025-03-06 20:33:27 +1300 |
|---|---|---|
| committer | Ben Bridle <ben@derelict.engineering> | 2025-03-11 16:59:26 +1300 |
| commit | 1ecee352f5844b0809d7ae66df52e34f42b44c8e (patch) | |
| tree | 472b6fd57ff7f64ac3f8cd676cbe7a113ba01f05 /src/types/word_template.rs | |
| parent | f2ed89083f5326a7a6f0a1720033d3388aa431fb (diff) | |
| download | torque-asm-1ecee352f5844b0809d7ae66df52e34f42b44c8e.zip | |
Rewrite entire assembler
The language is now more general, the code is better structured, error
reporting is more detailed, and many new language features have
been implemented:
- conditional blocks
- first-class strings
- more expression operators
- binary literals
- negative values
- invocations in constant expressions
Diffstat (limited to 'src/types/word_template.rs')
| -rw-r--r-- | src/types/word_template.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/types/word_template.rs b/src/types/word_template.rs new file mode 100644 index 0000000..33d5933 --- /dev/null +++ b/src/types/word_template.rs @@ -0,0 +1,46 @@ +use crate::*; + + +pub struct WordTemplate { + pub value: usize, + /// Width of the word in bits. + pub width: u32, + pub fields: Vec<Tracked<BitField>>, +} + +pub struct BitField { + pub name: char, + /// Width of the field in bits. + pub width: u32, + /// Number of bits to the right of the field in the word. + pub shift: u32, +} + + +impl std::fmt::Display for WordTemplate { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + if self.value == 0 { + write!(f, "0")?; + } else { + let bitcount = (self.value.ilog2() + 1) as usize; + 'bit: for i in (0..bitcount).rev() { + let is_first_bit = i+1 == bitcount; + if !is_first_bit && (i+1) % 4 == 0 { + 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; + } + } + match (self.value >> i) & 1 { + 0 => write!(f, "0")?, + _ => write!(f, "1")?, + } + } + } + return Ok(()); + } +} |
