Skip to main content

cfd16_assembler/
text.rs

1use std::fmt;
2
3use cfd16_lib::prelude::*;
4
5#[derive(Debug, Clone, Copy)]
6pub(crate) enum SecKind {
7    Data,
8    Text,
9}
10
11#[derive(Debug, Clone)]
12/// Location where an error occured.
13pub struct Location {
14    pub(crate) file: String,
15    pub(crate) line: usize,
16}
17
18impl fmt::Display for Location {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        write!(f, "{}:{}", self.file, self.line)
21    }
22}
23
24impl Location {
25    pub(crate) fn new(file: String, line: usize) -> Self {
26        Self { file, line }
27    }
28}
29
30#[derive(Debug, Clone)]
31pub(crate) struct TextSection {
32    pub(crate) mode: Mode,
33    // TODO: Remove once ELF
34    #[allow(dead_code)]
35    pub(crate) kind: SecKind,
36    pub(crate) items: Vec<TextItem>,
37}
38
39impl TextSection {
40    pub(crate) fn new(mode: Mode, kind: SecKind) -> Self {
41        Self {
42            mode,
43            kind,
44            items: vec![],
45        }
46    }
47}
48
49impl fmt::Display for TextSection {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        let mut output = String::new();
52
53        for instr in &self.items {
54            output = format!("{output}\n{instr}")
55        }
56
57        write!(f, "{output}")
58    }
59}
60
61#[allow(missing_docs)]
62#[derive(Debug, Clone)]
63pub(crate) enum TextItemKind {
64    Dir(Directive),
65    Instr(InstructionBuilder),
66    String(String),
67}
68
69#[derive(Debug, Clone)]
70pub(crate) struct TextItem {
71    pub(crate) kind: TextItemKind,
72    pub(crate) address: usize,
73    pub(crate) loc: Location,
74}
75
76impl TextItem {
77    pub(crate) fn new(kind: TextItemKind, addr: &mut isize, loc: Location) -> Self {
78        let address = *addr;
79
80        use TextItemKind::*;
81        match kind {
82            Dir(Directive::Space(n)) => (*addr) += n as isize,
83            Dir(Directive::Word(_)) => (*addr) += 2 as isize,
84            Instr(_) => (*addr) += 2 as isize,
85            String(ref s) => (*addr) += (s.len() + 1) as isize,
86            _ => {}
87        }
88
89        TextItem {
90            kind,
91            address: address as usize,
92            loc,
93        }
94    }
95}
96
97impl fmt::Display for TextItem {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        let textitem = match &self.kind {
100            TextItemKind::Dir(d) => format!("{}", d),
101            TextItemKind::Instr(i) => format!("{}", i),
102            TextItemKind::String(s) => format!(".ascii '{}'", s),
103        };
104
105        write!(f, "{} ({:#08x}): {}", self.loc, self.address, textitem)
106    }
107}
108
109#[derive(Debug, Clone, PartialEq, Eq)]
110pub(crate) enum Directive {
111    Space(usize),
112    Word(u16),
113    Global(String),
114    Extern(String),
115}
116
117impl fmt::Display for Directive {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        use Directive::*;
120
121        match self {
122            Space(n) => write!(f, ".space {}", n),
123            Word(n) => write!(f, ".word {}", n),
124            Global(s) => write!(f, ".global {}", s),
125            Extern(s) => write!(f, ".extern {}", s),
126        }
127    }
128}