cambridge-asm 0.23.0

Run pseudoassembly from Cambridge International syllabus 9618
Documentation
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
// Copyright (c) 2021 Saadi Save
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#![allow(clippy::module_name_repetitions)]

use crate::exec::{ExecFunc, ExecInst};
use std::{fmt::Display, ops::Deref, str::FromStr};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Represents all possible types of pseudoassembly operands
#[derive(PartialEq, Debug, Clone, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Op {
    Fail(String),
    Acc,
    Ix,
    Cmp,
    Ar,
    Indirect(Box<Op>),
    Addr(usize),
    Literal(usize),
    Gpr(usize),
    MultiOp(Vec<Op>),
    Null,
}

impl Op {
    pub fn is_none(&self) -> bool {
        matches!(self, Op::Null)
    }

    pub fn is_register(&self) -> bool {
        matches!(self, Op::Acc | Op::Ix | Op::Ar | Op::Gpr(_))
    }

    pub fn is_read_write(&self) -> bool {
        self.is_register()
            || match self {
                Op::Indirect(op) if op.is_read_write() => true,
                _ => matches!(self, Op::Addr(_)),
            }
    }

    pub fn is_usizeable(&self) -> bool {
        self.is_read_write() || matches!(self, Op::Literal(_))
    }

    pub fn is_address(&self) -> bool {
        matches!(self, Op::Addr(_) | Op::Indirect(_)) && self.is_read_write()
    }
}

impl Display for Op {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        #[allow(clippy::enum_glob_use)]
        use Op::*;

        let s = match self {
            Null => String::new(),
            Acc => "ACC".into(),
            Ix => "IX".into(),
            Cmp => "CMP".into(),
            Ar => "AR".into(),
            Addr(x) => format!("{x}"),
            Literal(x) => format!("#{x}"),
            Indirect(op) => format!("({op})"),
            Fail(x) => x.clone(),
            Gpr(x) => format!("r{x}"),
            MultiOp(v) => v
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(","),
        };

        f.write_str(&s)
    }
}

fn get_literal(mut op: String) -> usize {
    if op.starts_with('#') {
        op.remove(0);

        match op.chars().next().unwrap() {
            'b' | 'B' => {
                op.remove(0);
                usize::from_str_radix(&op, 2).unwrap()
            }
            'x' | 'X' => {
                op.remove(0);
                usize::from_str_radix(&op, 16).unwrap()
            }
            'o' | 'O' => {
                op.remove(0);
                usize::from_str_radix(&op, 8).unwrap()
            }
            '0'..='9' => op.parse().unwrap(),
            _ => unreachable!(),
        }
    } else {
        panic!("Literal `{op}` is invalid")
    }
}

fn get_reg_no(mut op: String) -> usize {
    op = op.to_lowercase();
    op.remove(0);

    // Ensured by parser
    op.parse().unwrap()
}

impl<T: Deref<Target = str>> From<T> for Op {
    fn from(inp: T) -> Self {
        fn get_op(inp: &str) -> Op {
            #[allow(clippy::enum_glob_use)]
            use Op::*;

            if inp.is_empty() {
                Null
            } else if let Ok(x) = inp.parse() {
                Addr(x)
            } else if inp.contains('#') {
                Literal(get_literal(inp.into()))
            } else if inp.to_lowercase().starts_with('r')
                && inp.trim_start_matches('r').chars().all(char::is_numeric)
            {
                let x = get_reg_no(inp.into());

                if x > 29 {
                    panic!("Only registers from r0 to r29 are allowed")
                } else {
                    Gpr(x)
                }
            } else {
                match inp.to_lowercase().as_str() {
                    "acc" => Acc,
                    "cmp" => Cmp,
                    "ix" => Ix,
                    _ => Fail(inp.into()),
                }
            }
        }

        if inp.contains(',') {
            Op::MultiOp(inp.split(',').map(get_op).collect())
        } else {
            get_op(&inp)
        }
    }
}

/// Trait for instruction sets
///
/// Implement this for custom instruction sets. Manual implementation is tedious,
/// so use [`inst_set`] or [`extend`] macros if possible
pub trait InstSet: FromStr + Display
where
    <Self as FromStr>::Err: Display,
{
    fn as_func_ptr(&self) -> ExecFunc;
    fn id(&self) -> u64;
    fn from_id(_: u64) -> Result<Self, <Self as FromStr>::Err>;
}

/// Macro to generate an instruction set
///
/// For an example, go to this [file](https://github.com/SaadiSave/cambridge-asm/blob/main/cambridge-asm/tests/int_test.rs)
#[macro_export]
macro_rules! inst_set {
    ($(#[$outer:meta])* $vis:vis $name:ident { $( $inst:ident => $func:expr,)+ }) => {
        inst_set! { $(#[$outer])* $vis $name use std; { $( $inst => $func,)+ } }
    };
    ($(#[$outer:meta])* $vis:vis $name:ident $using:item { $( $inst:ident => $func:expr,)+ }) => {
        $(#[$outer])*
        #[repr(u64)]
        #[derive(Clone, Copy)]
        $vis enum $name {
            $($inst,)+
        }

        $(#[$outer])*
        impl std::str::FromStr for $name {
            type Err = String;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s.to_uppercase().as_str() {
                    $( stringify!($inst) => Ok(Self::$inst),)+
                    _ => Err(format!("{s} is not an instruction")),
                }
            }
        }

        $(#[$outer])*
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str(match self {
                    $(Self::$inst => stringify!($inst),)+
                })
            }
        }

        $(#[$outer])*
        impl $crate::inst::InstSet for $name {
            fn as_func_ptr(&self) -> $crate::exec::ExecFunc {
                $using
                match self {
                    $(Self::$inst => $func,)+
                }
            }

            fn id(&self) -> u64 {
                *self as u64
            }

            fn from_id(id: u64) -> Result<Self, String> {
                match id {
                    $(x if x == Self::$inst as u64 => Ok(Self::$inst),)+
                    _ => Err(format!("0x{:X} is not a valid instruction ID", id)),
                }
            }
        }
    };
}

/// Macro to extend an instruction set
///
/// For an example, go to this [file](https://github.com/SaadiSave/cambridge-asm/blob/main/cambridge-asm/tests/int_test.rs)
///
/// Due to language limitations (no `concat_ident!`), do not use this macro within the same file twice
#[macro_export]
macro_rules! extend {
    ($(#[$outer:meta])* $vis:vis $name:ident extends $parent:ident { $( $inst:ident => $func:expr,)+ }) => {
        extend! { $(#[$outer])* $vis $name extends $parent use std; { $( $inst => $func,)+ } }
    };
    ($(#[$outer:meta])* $vis:vis $name:ident extends $parent:ident $using:item { $( $inst:ident => $func:expr,)+ }) => {
        $(#[$outer])*
        $vis struct $name {
            __private: extend_priv::Combined,
        }

        $(#[$outer])*
        pub(crate) mod extend_priv {
            use $crate::inst::InstSet;
            use super::$parent;
            #[repr(u64)]
            #[derive(Clone, Copy)]
            pub enum $name {
                $($inst,)+
                #[allow(non_camel_case_types)]
                LAST_INST_MARKER,
            }

            impl std::str::FromStr for $name {
                type Err = String;

                fn from_str(s: &str) -> Result<Self, Self::Err> {
                    match s.to_uppercase().as_str() {
                        $( stringify!($inst) => Ok(Self::$inst),)+
                        _ => Err(String::new()),
                    }
                }
            }

            impl $name {
                fn id(self) -> u64 {
                    self as u64
                }

                fn as_func_ptr(&self) -> $crate::exec::ExecFunc {
                    $using
                    match self {
                        $(Self::$inst => $func,)+
                        Self::LAST_INST_MARKER => panic!("This should never happen, report this as a bug"),
                    }
                }

                fn from_id(id: u64) -> Result<Self, String> {
                    match id {
                        $(x if x == Self::$inst as u64 => Ok(Self::$inst),)+
                        _ => Err(format!("0x{id:X} is not a valid instruction ID")),
                    }
                }
            }

            impl std::fmt::Display for $name {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    match self {
                        $(Self::$inst => f.write_str(stringify!($inst)),)+
                        Self::LAST_INST_MARKER => panic!("This should never happen, report this as a bug"),
                    }
                }
            }

            pub enum Combined {
                Extension($name),
                Parent($parent),
            }

            impl Combined {
                const LAST_INST_MARKER: u64 = $name::LAST_INST_MARKER as u64;

                pub fn id(&self) -> u64 {
                    match self {
                        Self::Extension(ext) => ext.id(),
                        Self::Parent(parent) => Self::LAST_INST_MARKER + parent.id()
                    }
                }

                pub fn from_id(id: u64) -> Result<Self, String> {
                    if id >= $name::LAST_INST_MARKER as u64 {
                        Ok(Combined::Parent($parent::from_id(id - Self::LAST_INST_MARKER)?))
                    } else {
                        Ok(Combined::Extension($name::from_id(id)?))
                    }
                }

                pub fn as_func_ptr(&self) -> $crate::exec::ExecFunc {
                    match self {
                        Self::Extension(e) => e.as_func_ptr(),
                        Self::Parent(p) => p.as_func_ptr(),
                    }
                }
            }

            impl std::str::FromStr for Combined {
                type Err = String;

                fn from_str(s: &str) -> Result<Self, Self::Err> {
                    if let Ok(res) = s.parse::<$name>() {
                        Ok(Combined::Extension(res))
                    } else if let Ok(res) = s.parse::<$parent>() {
                        Ok(Combined::Parent(res))
                    } else {
                        Err(format!("{s} is not an instruction"))
                    }
                }
            }

            impl std::fmt::Display for Combined {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    match self {
                        Self::Extension(e) => write!(f, "{e}"),
                        Self::Parent(p) => write!(f, "{p}"),
                    }
                }
            }
        }

        $(#[$outer])*
        impl std::str::FromStr for $name {
            type Err = String;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok($name { __private: s.to_uppercase().as_str().parse::<extend_priv::Combined>()? })
            }
        }

        $(#[$outer])*
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.__private)
            }
        }

        $(#[$outer])*
        impl $crate::inst::InstSet for $name {
            fn as_func_ptr(&self) -> $crate::exec::ExecFunc {
                self.__private.as_func_ptr()
            }

            fn id(&self) -> u64 {
                self.__private.id()
            }

            fn from_id(id: u64) -> Result<Self, String> {
                Ok( Self { __private: extend_priv::Combined::from_id(id)? })
            }
        }
    };
}

/// Post-parsing representation of an instruction
pub struct Inst<T>
where
    T: InstSet,
    <T as FromStr>::Err: Display,
{
    pub id: u64,
    pub inst: T,
    pub op: Op,
}

impl<T> Inst<T>
where
    T: InstSet,
    <T as FromStr>::Err: Display,
{
    pub fn new(inst: T, op: Op) -> Self {
        Self {
            id: inst.id(),
            op,
            inst,
        }
    }

    pub fn to_exec_inst(self) -> ExecInst {
        ExecInst::new(self.id, self.inst.as_func_ptr(), self.op)
    }
}