summaryrefslogtreecommitdiff
path: root/src/stages/bytecode.rs
blob: 71f1ff01d0d682da9c7c9837019a2daebe2795ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use crate::*;

use std::collections::HashMap;


pub fn parse_bytecode(tokens: Vec<Tracked<IntermediateToken>>, width: Option<u32>) -> Result<Vec<Segment>, Vec<Tracked<BytecodeError>>> {
    BytecodeParser::new(width).parse(tokens)
}


pub struct BytecodeParser {
    width: Option<u32>,
    addresses: HashMap<String, Tracked<usize>>,
    address: usize,
    segment_address: usize,
    segment_source: Option<SourceSpan>,
    segments: Vec<Segment>,
    words: Vec<Tracked<Word>>,
    errors: Vec<Tracked<BytecodeError>>,
}

impl BytecodeParser {
    pub fn new(width: Option<u32>) -> Self {
        Self {
            width,
            addresses: HashMap::new(),
            address: 0,
            segment_address: 0,
            segment_source: None,
            segments: Vec::new(),
            words: Vec::new(),
            errors: Vec::new(),
        }
    }

    pub fn parse(mut self, tokens: Vec<Tracked<IntermediateToken>>) -> Result<Vec<Segment>, Vec<Tracked<BytecodeError>>> {
        // Register all labels with address 0.
        for token in &tokens {
            if let IntermediateToken::LabelDefinition(name) = &token.value {
                let tracked = Tracked::from(0, token.source.clone());
                if let Some(_) = self.addresses.insert(name.clone(), tracked) {
                    unreachable!("Uncaught duplicate label definition '{name}'");
                }
            }
        }
        // Attempt to calculate all label addresses naively ahead of time.
        // This will give false results if we pin an address calculated from a label address.
        let mut address = 0;
        for token in &tokens {
            let source = &token.source;
            match &token.value {
                IntermediateToken::LabelDefinition(name) => {
                    let tracked = Tracked::from(address, source.clone());
                    self.addresses.insert(name.clone(), tracked);
                }
                IntermediateToken::Word(_) => {
                    address += 1;
                }
                IntermediateToken::PinnedAddress(pinned) => {
                    // Attempt to calculate a sane initial value for a pinned address.
                    match &pinned.value {
                        IntermediateInteger::Integer(value) => {
                            address = (*value).try_into().unwrap_or(0);
                        }
                        IntermediateInteger::Expression(expression) => {
                            let result = self.evaluate_expression(&expression, &pinned.source);
                            address = result.try_into().unwrap_or(0);
                        }
                        IntermediateInteger::LabelReference(_) => {
                            let error = BytecodeError::PinnedLabel;
                            self.errors.push(Tracked::from(error, source.clone()));
                        }
                    }
                }
            }
        }
        // Return unrecoverable errors.
        if !self.errors.is_empty() {
            return Err(self.errors);
        }

       for i in 0..4 {
            info!("Attempting iteration {} of bytecode assembly stage", i+1);
            // Erase the previous parse attempt.
            self.segments.clear();
            self.errors.clear();
            // Attempt to parse the program.
            let previous_addresses = self.addresses.clone();
            self.parse_iteration(&tokens);
            // Return unrecoverable errors.
            if !self.errors.is_empty() {
                return Err(self.errors);
            }
            // Check label stability
            if self.check_for_instability(&previous_addresses) {
                continue;
            }
            // Check for backtrack
            if self.check_for_backtrack() {
                continue;
            };
            // Program is stable, return.
            info!("Stabilised in {} iteration of bytecode assembly stage", i+1);
            return Ok(self.segments);
        }

        return Err(self.errors);
    }

    /// Attempt to parse the full program using the current label values.
    fn parse_iteration(&mut self, tokens: &[Tracked<IntermediateToken>]) {
        for token in tokens {
            let source = &token.source;
            match &token.value {
                IntermediateToken::Word(word) => {
                    let word = self.evaluate_word(word, source);
                    // Check that the word width fits the provided width.
                    if let Some(width) = self.width {
                        if word.width != width {
                            let error = BytecodeError::IncorrectWidth(width, word.width);
                            self.errors.push(Tracked::from(error, source.clone()));
                        }
                    }
                    self.words.push(word);
                    self.address += 1;
                }
                IntermediateToken::PinnedAddress(integer) => {
                    // Calculate the address of the new segment.
                    let pinned = match &integer.value {
                        IntermediateInteger::Integer(value) => {
                            (*value).try_into().unwrap_or(0)
                        }
                        IntermediateInteger::Expression(expression) => {
                            let result = self.evaluate_expression(&expression, &integer.source);
                            result.try_into().unwrap_or(0)
                        }
                        IntermediateInteger::LabelReference(_) =>
                            // Already handled when registering initial label values.
                            unreachable!(),
                    };
                    // Start a new segment.
                    let words = std::mem::take(&mut self.words);
                    if !words.is_empty() {
                        let address = self.segment_address;
                        let source = std::mem::take(&mut self.segment_source);
                        let segment = Segment { address, source, words };
                        self.segments.push(segment);
                    }
                    self.segment_source = Some(integer.source.clone());
                    self.address = pinned;
                    self.segment_address = pinned;
                }
                IntermediateToken::LabelDefinition(name) => {
                    // Record the latest known address of this label.
                    let address = self.addresses.get_mut(name).unwrap();
                    address.value = self.address;
                }
            }
        }
        // Finish final segment.
        let words = std::mem::take(&mut self.words);
        if !words.is_empty() {
            let address = self.segment_address;
            let source = std::mem::take(&mut self.segment_source);
            let segment = Segment { address, source, words };
            self.segments.push(segment);
        }
    }

    fn evaluate_expression(&mut self, expression: &IntermediateExpression, source: &SourceSpan) -> isize {
        let mut stack = ExpressionStack::new();
        for token in &expression.tokens {
            let source = &token.source;
            match &token.value {
                IntermediateExpressionToken::Integer(integer) => match integer {
                    IntermediateInteger::Integer(value) => {
                        stack.push(*value);
                    }
                    IntermediateInteger::Expression(expression) => {
                        stack.push(self.evaluate_expression(expression, source));
                    }
                    IntermediateInteger::LabelReference(name) => {
                        stack.push(self.evaluate_label_reference(name));
                    }
                }
                IntermediateExpressionToken::Operator(operator) => {
                    if let Err(err) = stack.apply(*operator, source) {
                        let error = BytecodeError::StackError(err);
                        self.errors.push(Tracked::from(error, source.clone()))
                    }
                }
            }
        }
        match stack.pull_result() {
            Ok(value) => value,
            Err(err) => {
                let error = BytecodeError::StackError(Tracked::from(err, source.clone()));
                self.errors.push(Tracked::from(error, source.clone()));
                0
            }
        }
    }

    fn evaluate_label_reference(&mut self, name: &Tracked<String>) -> isize {
        if let Some(address) = self.addresses.get(&name.to_string()) {
            address.value as isize
        } else {
            unreachable!("Uncaught unresolved label reference '{name}'")
        }
    }

    fn evaluate_word(&mut self, word: &IntermediateWord, source: &SourceSpan) -> Tracked<Word> {
        let mut word_value = word.value;
        for field in &word.fields {
            let field_source = &field.value.value.source;
            let field_value = match &field.value.value.value {
                IntermediateInteger::Expression(expression) => {
                    self.evaluate_expression(expression, source)
                }
                IntermediateInteger::LabelReference(name) => {
                    self.evaluate_label_reference(name)
                }
                IntermediateInteger::Integer(value) => {
                    *value
                }
            };
            let value_width = width(field_value);
            if field.width < value_width {
                let error = BytecodeError::ValueTooWide(field.width, value_width);
                self.errors.push(Tracked::from(error, field_source.clone()));
            } 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 word = Word { width: word.width, value: word_value };
        return Tracked::from(word, source.clone());
    }

    fn check_for_instability(&mut self, previous_addresses: &HashMap<String, Tracked<usize>>) -> bool {
        let mut instability_occurred = false;
        for (name, previous_address) in previous_addresses.iter() {
            let current_address = &self.addresses[name];
            if current_address != previous_address {
                info!("Label '{name}' was unstable, moving from address 0x{:04x} to 0x{:04x}",
                    previous_address.value, current_address.value);
                let error = BytecodeError::UnstableLabel(name.to_string());
                self.errors.push(Tracked::from(error, previous_address.source.clone()));
                instability_occurred = true;
            }
        }
        return instability_occurred;
    }

    fn check_for_backtrack(&mut self) -> bool {
        let mut backtrack_occurred = false;
        let mut current_address = 0;
        for segment in &self.segments {
            if segment.address < current_address {
                let error = BytecodeError::PinnedAddressBacktrack(segment.address, current_address);
                if let Some(source) = &segment.source {
                    self.errors.push(Tracked::from(error, source.clone()));
                }
                info!("Backtrack occurred with segment at address 0x{:04x}", segment.address);
                backtrack_occurred = true;
            }
            current_address = segment.address + segment.words.len();
        }
        return backtrack_occurred;
    }
}