aboutsummaryrefslogtreecommitdiff
path: root/lib/xixanta/src/object.rs
blob: db7c51eb78f3a84f18b5e046953f7ae0fcb6943c (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use crate::mapping::Mapping;
use crate::node::{ControlType, NodeType, PNode, PString};
use crate::opcodes::CONTROL_FUNCTIONS;
use std::collections::HashMap;

/// The name of the global context as used internally.
const GLOBAL_CONTEXT: &str = "Global";

/// A Bundle represents a set of bytes that can be encoded as binary data.
#[derive(Debug, Default, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct Bundle {
    /// The bytes which make up any encodable element for the application. The
    /// capacity is of three bytes maximum, but the actual size is encoded in
    /// the `size` property.
    pub bytes: [u8; 3],

    /// The amount of bytes which have actually been set on this bundle.
    pub size: u8,

    /// The address where the given bytes are to be placed on the resulting
    /// binary file.
    pub address: usize,

    /// If this bundle encodes an instruction, the amount of cycles it takes for
    /// the CPU to actually execute it.
    pub cycles: u8,

    /// Whether the cost in cycles is affected when crossing a page boundary.
    pub affected_on_page: bool,

    /// Whether the bytes on `bytes` contain the final value or not. This is
    /// used for internal purposes only.
    pub resolved: bool,

    /// Whether we have to consider the bundle to contain a negative number.
    /// This comes from the fact that we just have a bunch of signednessless
    /// bytes, but using the negative unary operator will give us a hint on how
    /// to interpret it when needed.
    pub negative: bool,
}

impl Bundle {
    /// Create a default bundle but with the given `resolved` status.
    pub fn new(resolved: bool) -> Self {
        Self {
            resolved,
            ..Default::default()
        }
    }

    /// Create a bundle tailored for filling purposes.
    pub fn fill(value: u8) -> Self {
        Self {
            bytes: [value, 0, 0],
            size: 1,
            address: 0,
            cycles: 0,
            affected_on_page: false,
            resolved: true,
            negative: false,
        }
    }

    /// Returns the `bytes` field from the bundle as interpreted as bytes from a
    /// regular integer.
    pub fn value(&self) -> isize {
        if self.negative {
            isize::from_le_bytes([
                self.bytes[0],
                self.bytes[1],
                0xFF,
                0xFF,
                0xFF,
                0xFF,
                0xFF,
                0xFF,
            ])
        } else {
            isize::from_le_bytes([
                self.bytes[0],
                self.bytes[1],
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
            ])
        }
    }
}

/// The type of object being referenced, which is either a value as-is, or an
/// address that needs to be interpreted when fetching it.
#[derive(Debug, Clone)]
pub enum ObjectType {
    Address,
    Value,
}

/// Bundle and metadata which is stored on the context table for a given
/// variable or label.
#[derive(Debug, Clone)]
pub struct Object {
    /// Bundle representing the actual value.
    pub bundle: Bundle,

    /// The mapping index where the object was found. Note that this index
    /// doesn't mean much on the table, but it has to mean something by the
    /// caller.
    pub mapping: usize,

    /// The segment index within the referenced mapping where the object was
    /// found. Note that this index doesn't mean much on the table, but it has
    /// to mean something by the caller.
    pub segment: usize,

    /// The type for this object.
    pub object_type: ObjectType,
}

impl Object {
    /// Create a default bundle with the given metadata parameters.
    pub fn new(mapping: usize, segment: usize, object_type: ObjectType) -> Self {
        Self {
            bundle: Bundle::default(),
            mapping,
            segment,
            object_type,
        }
    }
}

/// Context holds information about the different scopes being defined, the
/// current scope, and has a map of all the variables defined for each scope.
#[derive(Debug)]
pub struct Context {
    /// Tracks the contexts that we have entered at any given point. The last
    /// element is the actual context, and it will be empty if we are in the
    /// global context.
    stack: Vec<String>,

    /// Map of objects for any given context. The key is the name of the
    /// context, and the value is another map. This inner map has the variable
    /// name as the key, and the Bundle as a value.
    pub map: HashMap<String, HashMap<String, Object>>,

    /// Map of labels for any given context. Note that this only keeps track of
    /// the amount of labels that have been defined, which might include
    /// anonymous positions. This is primarily used on relative addressing where
    /// the name of the label might not be provided (e.g. anonymous relative
    /// reference).
    pub labels: HashMap<String, Vec<Object>>,
}

impl Default for Context {
    fn default() -> Self {
        Self::new()
    }
}

impl Context {
    /// Returns a new empty context.
    pub fn new() -> Self {
        Context {
            stack: vec![],
            map: HashMap::from([(String::from(GLOBAL_CONTEXT), HashMap::new())]),
            labels: HashMap::from([(String::from(GLOBAL_CONTEXT), vec![])]),
        }
    }

    /// Returns the value of the object represented by the given `id`. Note that
    /// this `id` can be scoped or not, and this function will try to pick the
    /// variable from the right scope. The value itself will be resolved if the
    /// type is ObjectType::Address.
    pub fn get_variable(&self, id: &PString, mappings: &[Mapping]) -> Result<Object, String> {
        // First of all, figure out the name of the scope and the real name of
        // the variable. If this was not scoped at all (None case when trying to
        // rsplit by the "::" operator), then we assume on the current scope.
        let (scope_name, var_name) = match id.value.rsplit_once("::") {
            Some((scope, name)) => (scope, name),
            None => (self.name(), id.value.as_str()),
        };

        self.get_variable_in_scope(scope_name, var_name, mappings)
    }

    // Get the `var_name` variable on the `scope_name` scope (or parents). For
    // further context, take the `mappings` into consideration when resolving
    // labels, and `line` when producing context errors.
    fn get_variable_in_scope(
        &self,
        scope_name: &str,
        var_name: &str,
        mappings: &[Mapping],
    ) -> Result<Object, String> {
        // And with that, the only thing left is to find the scope and the
        // variable in it.
        match self.map.get(scope_name) {
            Some(scope) => match scope.get(var_name) {
                Some(var) => match var.object_type {
                    ObjectType::Value => Ok(var.clone()),
                    ObjectType::Address => Ok(self.resolve_label(mappings, var)?),
                },
                None => {
                    // If it cannot be found, then we have to move up through
                    // the scope hierarchy to see if we can fetch it there. For
                    // that, though, we first prepare an error so we return the
                    // original one, not the propagated one (see below).
                    let err = Err(format!(
                        "could not find variable '{}' in {}",
                        var_name,
                        self.to_human_with(scope_name)
                    ));

                    // If we are already in the global context and the object
                    // was not found, just leave with an error.
                    if scope_name == GLOBAL_CONTEXT {
                        err
                    } else {
                        // Recursive call to find the object on the parent
                        // scope. If this cannot be found, instead of using the
                        // error from the recursive call, preserve the original
                        // error so it better reflects the original scope where
                        // this was first attempted.
                        let parent = self.parent(scope_name);
                        if let Ok(object) = self.get_variable_in_scope(parent, var_name, mappings) {
                            Ok(object)
                        } else {
                            err
                        }
                    }
                }
            },
            None => Err(format!("could not find scope '{scope_name}'")),
        }
    }

    /// Given an `object` which is located via `mappings`, resolve the effective
    /// address.
    ///
    /// NOTE: this function asserts that the given `object` is of type
    /// ObjectType::Address, otherwise it doesn't make sense to call it.
    pub fn resolve_label(&self, mappings: &[Mapping], object: &Object) -> Result<Object, String> {
        assert!(matches!(object.object_type, ObjectType::Address));

        let mut ret = object.clone();

        let mapping = &mappings[ret.mapping];
        let internal_offset = u16::from_le_bytes([ret.bundle.bytes[0], ret.bundle.bytes[1]]);
        let segment_offset = crate::mapping::segment_offset(mapping, ret.segment);
        let addr = mapping.start as usize + segment_offset as usize + internal_offset as usize;

        // Avoid weird out of bound references for addresses.
        if addr > u16::MAX as usize {
            return Err(format!("address {addr:x} is out of bounds"));
        }

        let addr_bytes = (addr as u16).to_le_bytes();

        ret.bundle.bytes[0] = addr_bytes[0];
        ret.bundle.bytes[1] = addr_bytes[1];

        Ok(ret)
    }

    /// Sets a value for an object identified by `id`. If `overwrite` is set to
    /// true, then this value will be set even if the id already existed,
    /// otherwise it will return a ContextError
    pub fn set_variable(
        &mut self,
        id: &PString,
        object: &Object,
        overwrite: bool,
    ) -> Result<(), String> {
        let scope_name = self.name().to_string();
        let scope = self.map.get_mut(&scope_name).unwrap();

        match scope.get_mut(&id.value) {
            Some(sc) => {
                if !overwrite {
                    return Err(format!(
                        "'{}' already defined in {}: you cannot re-assign names",
                        id.value,
                        self.to_human()
                    ));
                }
                *sc = object.clone();
            }
            None => {
                scope.insert(id.value.clone(), object.to_owned());
            }
        }

        Ok(())
    }

    /// Add a new label to the list of known labels.
    pub fn add_label(&mut self, object: &Object) {
        let scope_name = self.name().to_string();
        let scope = self.labels.get_mut(&scope_name).unwrap();

        scope.push(object.clone());
    }

    /// Change the current context given a `node`. Returns true if the context
    /// has changed.
    pub fn change_context(&mut self, node: &PNode) -> Result<bool, String> {
        // The parser already guarantees that the control node is
        // from a function that we already know, so calling `unwrap`
        // is not dangerous.
        let control = CONTROL_FUNCTIONS
            .get(&node.value.value.to_lowercase())
            .unwrap();

        // If the control function does not touch the context, leave early.
        if !control.touches_context {
            return Ok(false);
        }

        // And push/pop the context depending on the control being used.
        match node.node_type {
            NodeType::Control(ControlType::StartMacro)
            | NodeType::Control(ControlType::StartProc)
            | NodeType::Control(ControlType::StartScope)
            | NodeType::Control(ControlType::StartRepeat) => {
                self.context_push(node.left.as_ref().unwrap());
                Ok(true)
            }
            NodeType::Control(ControlType::EndMacro)
            | NodeType::Control(ControlType::EndProc)
            | NodeType::Control(ControlType::EndScope)
            | NodeType::Control(ControlType::EndRepeat) => {
                self.context_pop(&node.value)?;
                Ok(true)
            }
            _ => Ok(false),
        }
    }

    /// Change the current context to the given one identified by `name`,
    /// disregarding any check. This is a destructive operation and will clear
    /// out any previous context. Hence, only call this when you are sure that
    /// there's no reliance on a specific context in the future.
    pub fn force_context_switch(&mut self, name: &str) {
        let mut ax = String::with_capacity(name.len());

        // Clear the context stack as we will set it manually.
        self.stack.truncate(0);

        // Forcing a context switch is not as simple as pushing the given name,
        // but we have to make sure that the whole scope hierarchy is laid out
        // so the code that fetches variables continues to work (e.g. if
        // "A::B::C", then the stack must look like ["A", "A::B", "A::B::C"]).
        for n in name.split("::") {
            if !ax.is_empty() {
                ax += "::";
            }
            ax += n;
            self.stack.push(ax.clone());
        }
    }

    /// Returns the amount of labels that have been submitted so far for the
    /// current scope.
    pub fn labels_seen(&self) -> usize {
        let scope_name = self.name().to_string();

        match self.labels.get(&scope_name) {
            Some(labels) => labels.len(),
            None => 0,
        }
    }

    /// Returns the bundle which is representative for the label being
    /// referenced in a relative way. The relation is given on the `rel`
    /// parameter, with a value between -4 or +4 where negative values represent
    /// previous labels and positive values next ones (e.g. -2 means "2 labels
    /// before"). This is in relation to `labels_seen`, which states how many
    /// labels have been seen by the caller at this point.
    pub fn get_relative_label(
        &self,
        rel: isize,
        labels_seen: usize,
        mappings: &[Mapping],
    ) -> Result<Object, String> {
        // Bound check: the given 'rel' parameter has a proper value.
        assert!(
            rel < 5 && rel > -5 && rel != 0,
            "bad parameter for relative label"
        );

        // Bound check: you cannot reference a past label that doesn't exist.
        // This is the programmer's to blame, not on us, so don't assert.
        if labels_seen == 0 && rel < 0 {
            return Err("cannot reference an unknown previous label".to_string());
        }

        // Get the labels as referenced in the current context, and also the
        // index that we will be using.
        let scope_name = self.name().to_string();
        let labels = self.labels.get(&scope_name).unwrap();
        let idx = if rel > 0 {
            labels_seen as isize + rel - 1
        } else {
            labels_seen as isize + rel
        };

        // Bound check: is the programmer referencing an "out of bounds" label?
        // If so then it's a mistake on their part.
        if idx < 0 || idx >= labels.len() as isize {
            return Err("cannot reference bogus label (out of bounds)".to_string());
        }

        // Everything should be fine from here on, simply return the bundle that
        // was being referenced.
        self.resolve_label(mappings, &labels[idx as usize])
    }

    // Pushes a new context given a `node`, which holds the identifier of the
    // new scope.
    fn context_push(&mut self, id: &PNode) {
        let name = match self.stack.last() {
            Some(n) => format!("{}::{}", n, id.value.value),
            None => id.value.value.clone(),
        };

        // Actually push the name to the stack and initialize it on the variable
        // map.
        self.stack.push(name.clone());
        self.map.entry(name.clone()).or_default();
        self.labels.entry(name).or_default();
    }

    // Pops out the latest context that was pushed.
    fn context_pop(&mut self, id: &PString) -> Result<(), String> {
        if self.stack.is_empty() {
            return Err(format!("missplaced '{}' statement", id.value));
        }

        self.stack.truncate(self.stack.len() - 1);
        Ok(())
    }

    // Returns the name context that is directly above the one named `name`.
    fn parent(&self, name: &str) -> &str {
        let index = self
            .stack
            .iter()
            .position(|n| n.as_str() == name)
            .unwrap_or(0);
        if index < 1 {
            GLOBAL_CONTEXT
        } else {
            self.stack.get(index - 1).unwrap()
        }
    }

    /// Returns the name of the current context.
    pub fn name(&self) -> &str {
        match self.stack.last() {
            Some(name) => name,
            None => GLOBAL_CONTEXT,
        }
    }

    /// Returns true if we are in the global scope.
    pub fn is_global(&self) -> bool {
        self.stack.is_empty()
    }

    // Returns a human-readable string representing the current context.
    fn to_human(&self) -> String {
        match self.stack.last() {
            Some(n) => format!("'{n}'"),
            None => "the global scope".to_string(),
        }
    }

    // Returns a human-readable string representing the given context.
    fn to_human_with(&self, name: &str) -> String {
        if name == GLOBAL_CONTEXT {
            "the global scope".to_string()
        } else if name.starts_with(".repeat-") {
            // The ".repeat-" generated scope is randomly generated and is not
            // clear to the human eye. Hence, just hide out the name for this
            // case.
            "the current scope".to_string()
        } else {
            format!("'{name}'")
        }
    }
}