rucc-target 0.11.8

Target descriptions, register files and ABI data for the rucc C compiler.
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
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
//! Reading one line of AArch64 assembly in the syntax GNU as takes.
//!
//! This is the half of an assembler that turns `ldr x0, [sp, #16]` into a mnemonic and the
//! [`Value`]s [`encode`](crate::aarch64::encode) takes. It reads one instruction and nothing
//! around it: no labels, no directives, no comments and no expressions, since what writes those is
//! a line reader over this rather than this. What it is for now is the tests, which read every
//! line GNU as was given and check that the word is the same, and later the inline assembly and
//! the `.s` files the driver is handed for this machine.
//!
//! A name is a register, a condition or a barrier option before it is a symbol, the way GNU as
//! reads them, so a symbol spelled `lo` or `ish` has to be written some other way.

use std::fmt;

use crate::aarch64::encode::{
    Addr, Arrangement, Cond, Extend, Mode, Offset, Operator, Scalar, Shift, Value, Width,
};

/// One instruction, read.
#[derive(Debug, Clone, PartialEq)]
pub struct Line {
    /// Its mnemonic, in lower case.
    pub mnemonic: String,
    /// Its operands, in the order they were written.
    pub values: Vec<Value>,
    /// The symbol it names, when it names one. [`Value::Symbol`] and [`Offset::Symbol`] say where
    /// and which part of its address, and this says which symbol.
    pub symbol: Option<String>,
    /// What is added to the symbol's address, which is the `8` in `:lo12:table+8`.
    pub addend: i64,
}

/// Why a line could not be read.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    /// The operand that could not be read, or the whole line.
    pub text: String,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "cannot read `{}` as aarch64 assembly", self.text)
    }
}

impl std::error::Error for Error {}

fn error(text: &str) -> Error {
    Error { text: text.to_owned() }
}

/// Reads one instruction.
///
/// # Errors
///
/// When an operand is none of the things an instruction here takes. See [`Error`].
pub fn read(text: &str) -> Result<Line, Error> {
    let text = text.trim();
    let (mnemonic, rest) = match text.find(char::is_whitespace) {
        Some(at) => (&text[..at], text[at..].trim()),
        None => (text, ""),
    };
    if mnemonic.is_empty() {
        return Err(error(text));
    }
    let mut line = Line {
        mnemonic: mnemonic.to_ascii_lowercase(),
        values: Vec::new(),
        symbol: None,
        addend: 0,
    };
    let mut named = (None, 0);
    let pieces = split(rest);
    let mut at = 0;
    while at < pieces.len() {
        let piece = pieces[at];
        if piece.starts_with('[') {
            // A post-indexed address is written as two operands, the address and then the amount
            // it moves by, and it is one operand to the machine.
            let mut addr = address(piece, &mut named)?;
            if addr.mode == Mode::Offset && piece.ends_with(']') && at + 1 < pieces.len() {
                if let (Offset::Imm(0), Some(imm)) =
                    (addr.offset, pieces[at + 1].strip_prefix('#').and_then(number))
                {
                    addr.offset = Offset::Imm(imm);
                    addr.mode = Mode::Post;
                    at += 1;
                }
            }
            line.values.push(Value::Mem(addr));
        } else {
            line.values.push(operand(piece, &mut named)?);
        }
        at += 1;
    }
    (line.symbol, line.addend) = named;
    Ok(line)
}

/// The symbol a line names so far, and what is added to it.
type Named = (Option<String>, i64);

/// The operands, split at the commas that are not inside an address.
fn split(text: &str) -> Vec<&str> {
    let mut pieces = Vec::new();
    let (mut depth, mut start) = (0, 0);
    for (at, c) in text.char_indices() {
        match c {
            '[' => depth += 1,
            ']' => depth -= 1,
            ',' if depth == 0 => {
                pieces.push(text[start..at].trim());
                start = at + 1;
            }
            _ => {}
        }
    }
    if !text[start..].trim().is_empty() {
        pieces.push(text[start..].trim());
    }
    pieces
}

/// One operand that is not an address.
fn operand(piece: &str, symbol: &mut Named) -> Result<Value, Error> {
    let lower = piece.to_ascii_lowercase();
    if let Some(value) = register(&lower) {
        return Ok(value);
    }
    if let Some(imm) = lower.strip_prefix('#') {
        if let Some(value) = number(imm) {
            return Ok(Value::Imm(value));
        }
        if let Ok(value) = imm.parse::<f64>() {
            return Ok(Value::Float(value));
        }
        if imm.starts_with(':') {
            return reference(piece.trim_start_matches('#'), symbol).map(Value::Symbol);
        }
        return Err(error(piece));
    }
    if let Some(value) = modifier(&lower)? {
        return Ok(value);
    }
    if let Some(cond) = Cond::named(&lower) {
        return Ok(Value::Cond(cond));
    }
    if let Some(option) = barrier(&lower) {
        return Ok(Value::Barrier(option));
    }
    if let Some(field) = system(&lower) {
        return Ok(Value::System(field));
    }
    reference(piece, symbol).map(Value::Symbol)
}

/// A shift or an extension, with its amount.
fn modifier(lower: &str) -> Result<Option<Value>, Error> {
    let (name, amount) = match lower.split_once(char::is_whitespace) {
        Some((name, amount)) => (name, Some(amount.trim())),
        None => (lower, None),
    };
    let amount = match amount {
        Some(amount) => {
            let digits = amount.strip_prefix('#').unwrap_or(amount);
            let value = number(digits).and_then(|n| u8::try_from(n).ok());
            Some(value.ok_or_else(|| error(lower))?)
        }
        None => None,
    };
    let shift = match name {
        "lsl" => Some(Shift::Lsl),
        "lsr" => Some(Shift::Lsr),
        "asr" => Some(Shift::Asr),
        "ror" => Some(Shift::Ror),
        _ => None,
    };
    if let Some(shift) = shift {
        return amount.map(|amount| Some(Value::Shift(shift, amount))).ok_or_else(|| error(lower));
    }
    Ok(extension(name).map(|extend| Value::Extend(extend, amount)))
}

fn extension(name: &str) -> Option<Extend> {
    Some(match name {
        "uxtb" => Extend::Uxtb,
        "uxth" => Extend::Uxth,
        "uxtw" => Extend::Uxtw,
        "uxtx" => Extend::Uxtx,
        "sxtb" => Extend::Sxtb,
        "sxth" => Extend::Sxth,
        "sxtw" => Extend::Sxtw,
        "sxtx" => Extend::Sxtx,
        _ => return None,
    })
}

/// A register, by any of the names GNU as gives it.
fn register(name: &str) -> Option<Value> {
    match name {
        "sp" => return Some(Value::Sp(Width::X)),
        "wsp" => return Some(Value::Sp(Width::W)),
        "xzr" => return Some(Value::Gpr(Width::X, 31)),
        "wzr" => return Some(Value::Gpr(Width::W, 31)),
        "fp" => return Some(Value::Gpr(Width::X, 29)),
        "lr" => return Some(Value::Gpr(Width::X, 30)),
        _ => {}
    }
    if let Some((number, lanes)) = name.strip_prefix('v').and_then(|rest| rest.split_once('.')) {
        let arrangement = match lanes {
            "8b" => Arrangement::B8,
            "16b" => Arrangement::B16,
            "2d" => Arrangement::D2,
            _ => return None,
        };
        return Some(Value::Vector(arrangement, numbered(number, 32)?));
    }
    let (first, number) = name.split_at(name.char_indices().nth(1)?.0);
    Some(match first {
        "x" => Value::Gpr(Width::X, numbered(number, 31)?),
        "w" => Value::Gpr(Width::W, numbered(number, 31)?),
        "b" => Value::Fp(Scalar::B, numbered(number, 32)?),
        "h" => Value::Fp(Scalar::H, numbered(number, 32)?),
        "s" => Value::Fp(Scalar::S, numbered(number, 32)?),
        "d" => Value::Fp(Scalar::D, numbered(number, 32)?),
        "q" => Value::Fp(Scalar::Q, numbered(number, 32)?),
        _ => return None,
    })
}

/// A register number below a limit, written in decimal with no leading zero.
fn numbered(text: &str, below: u8) -> Option<u8> {
    if text.len() > 1 && text.starts_with('0') {
        return None;
    }
    text.parse::<u8>().ok().filter(|&number| number < below)
}

/// A whole number, in decimal or in hexadecimal after `0x`, with a sign or without.
fn number(text: &str) -> Option<i64> {
    let (negative, digits) = match text.strip_prefix('-') {
        Some(digits) => (true, digits),
        None => (false, text),
    };
    let magnitude = match digits.strip_prefix("0x").or_else(|| digits.strip_prefix("0X")) {
        Some(hex) => u64::from_str_radix(hex, 16).ok()?,
        None => digits.parse::<u64>().ok()?,
    };
    // A number past the largest signed one is its bits, the way GNU as reads
    // `#0xffffffffffffffff`.
    let value = magnitude as i64;
    Some(if negative { value.wrapping_neg() } else { value })
}

/// A symbol with or without an operator in front of it, saving its name and what is added to it.
///
/// A name is what GNU as takes as one, or a numbered local label with the `b` or `f` that says
/// which way to look for it. A number added or taken away after the name is the addend, which is
/// how a compiler reaches a field of a variable without a second instruction.
fn reference(text: &str, symbol: &mut Named) -> Result<Operator, Error> {
    let (operator, name) = match text.strip_prefix(':') {
        Some(rest) => {
            let (operator, name) = rest.split_once(':').ok_or_else(|| error(text))?;
            let operator = match operator.to_ascii_lowercase().as_str() {
                "lo12" => Operator::Lo12,
                "got" => Operator::Got,
                "got_lo12" => Operator::GotLo12,
                "gottprel" => Operator::GotTprel,
                "gottprel_lo12" => Operator::GotTprelLo12,
                "tprel_hi12" => Operator::TprelHi12,
                "tprel_lo12_nc" => Operator::TprelLo12Nc,
                _ => return Err(error(text)),
            };
            (operator, name)
        }
        None => (Operator::Plain, text),
    };
    let (name, addend) = match name.char_indices().skip(1).find(|&(_, c)| c == '+' || c == '-') {
        Some((at, sign)) => {
            let digits = name[at + 1..].trim();
            let magnitude = number(digits).filter(|_| !digits.starts_with('-'));
            let magnitude = magnitude.ok_or_else(|| error(text))?;
            (name[..at].trim(), if sign == '-' { magnitude.wrapping_neg() } else { magnitude })
        }
        None => (name, 0),
    };
    let symbolic =
        name.chars().next().is_some_and(|c| c.is_ascii_alphabetic() || "_.$".contains(c))
            && name.chars().all(|c| c.is_ascii_alphanumeric() || "_.$".contains(c));
    let numbered = name.len() > 1
        && name.ends_with(['b', 'f'])
        && name[..name.len() - 1].bytes().all(|b| b.is_ascii_digit());
    if !symbolic && !numbered {
        return Err(error(text));
    }
    *symbol = (Some(name.to_owned()), addend);
    Ok(operator)
}

/// The barrier options by name, as the number the encoding gives each.
pub(super) static BARRIERS: [(&str, u8); 12] = [
    ("oshld", 1),
    ("oshst", 2),
    ("osh", 3),
    ("nshld", 5),
    ("nshst", 6),
    ("nsh", 7),
    ("ishld", 9),
    ("ishst", 10),
    ("ish", 11),
    ("ld", 13),
    ("st", 14),
    ("sy", 15),
];

/// A barrier option, as the number the encoding gives it.
fn barrier(name: &str) -> Option<u8> {
    BARRIERS.iter().find(|(known, _)| *known == name).map(|&(_, option)| option)
}

/// The system registers a compiler reads by name, as the op0, op1, CRn, CRm and op2 fields.
pub(super) static SYSTEM: [(&str, [u16; 5]); 9] = [
    ("nzcv", [3, 3, 4, 2, 0]),
    ("fpcr", [3, 3, 4, 4, 0]),
    ("fpsr", [3, 3, 4, 4, 1]),
    ("tpidr_el0", [3, 3, 13, 0, 2]),
    ("tpidrro_el0", [3, 3, 13, 0, 3]),
    ("cntfrq_el0", [3, 3, 14, 0, 0]),
    ("cntvct_el0", [3, 3, 14, 0, 2]),
    ("dczid_el0", [3, 3, 0, 0, 7]),
    ("ctr_el0", [3, 3, 0, 0, 1]),
];

/// The fifteen bits `mrs` and `msr` carry for a system register with those five fields.
pub(super) const fn system_field([op0, op1, crn, crm, op2]: [u16; 5]) -> u16 {
    (op0 - 2) << 14 | op1 << 11 | crn << 7 | crm << 3 | op2
}

/// A system register, as the fifteen bits `mrs` and `msr` carry for it.
///
/// The ones in [`SYSTEM`], and any of them written the generic way, as `s3_3_c13_c0_2`.
fn system(name: &str) -> Option<u16> {
    if let Some(&(_, fields)) = SYSTEM.iter().find(|(known, _)| *known == name) {
        return Some(system_field(fields));
    }
    let mut parts = name.strip_prefix('s')?.split('_');
    let mut next = |prefix: &str, below: u16| {
        let part = parts.next()?;
        part.strip_prefix(prefix)?.parse::<u16>().ok().filter(|&n| n < below)
    };
    let fields = [next("", 4)?, next("", 8)?, next("c", 16)?, next("c", 16)?, next("", 8)?];
    if parts.next().is_some() || fields[0] < 2 {
        return None;
    }
    Some(system_field(fields))
}

/// An address, `[x0]`, `[x0, #8]`, `[x0, #8]!`, `[x0, x1, lsl #3]` or `[x0, :lo12:name]`.
fn address(piece: &str, symbol: &mut Named) -> Result<Addr, Error> {
    let (inner, mode) = if let Some(inner) = piece.strip_suffix("]!") {
        (inner, Mode::Pre)
    } else {
        (piece.strip_suffix(']').ok_or_else(|| error(piece))?, Mode::Offset)
    };
    let inner = inner.strip_prefix('[').ok_or_else(|| error(piece))?;
    let parts: Vec<&str> = inner.split(',').map(str::trim).collect();
    let base = match register(&parts[0].to_ascii_lowercase()) {
        Some(Value::Sp(Width::X)) => 31,
        Some(Value::Gpr(Width::X, number)) if number < 31 => number,
        _ => return Err(error(piece)),
    };
    let offset = match &parts[1..] {
        [] => Offset::Imm(0),
        [imm] if imm.starts_with("#:") || imm.starts_with(':') => {
            let text = imm.strip_prefix('#').unwrap_or(imm);
            Offset::Symbol(reference(text, symbol)?)
        }
        [imm] if imm.starts_with('#') => {
            Offset::Imm(imm.strip_prefix('#').and_then(number).ok_or_else(|| error(piece))?)
        }
        [index, rest @ ..] => {
            let (width, reg) = match register(&index.to_ascii_lowercase()) {
                Some(Value::Gpr(width, number)) if number < 31 => (width, number),
                _ => return Err(error(piece)),
            };
            let (extend, amount) = match rest {
                [] => (Extend::Uxtx, None),
                [modifier_text] => match modifier(&modifier_text.to_ascii_lowercase())? {
                    Some(Value::Shift(Shift::Lsl, amount)) => (Extend::Uxtx, Some(amount)),
                    Some(Value::Extend(extend, amount)) => (extend, amount),
                    _ => return Err(error(piece)),
                },
                _ => return Err(error(piece)),
            };
            // The register has to be the width the extension reads, which for a shift left or
            // for nothing is the whole of it.
            let reads_w = matches!(extend, Extend::Uxtw | Extend::Sxtw);
            if reads_w != (width == Width::W) {
                return Err(error(piece));
            }
            Offset::Reg { reg, extend, amount }
        }
    };
    if mode == Mode::Pre && !matches!(offset, Offset::Imm(_)) {
        return Err(error(piece));
    }
    Ok(Addr { base, offset, mode })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn an_address_is_one_operand_however_it_is_written() {
        let line = read("ldr x0, [x1], #16").unwrap();
        assert_eq!(
            line.values,
            [
                Value::Gpr(Width::X, 0),
                Value::Mem(Addr { base: 1, offset: Offset::Imm(16), mode: Mode::Post })
            ]
        );
        let line = read("str x30, [sp, #-16]!").unwrap();
        assert_eq!(
            line.values[1],
            Value::Mem(Addr { base: 31, offset: Offset::Imm(-16), mode: Mode::Pre })
        );
    }

    #[test]
    fn a_symbol_keeps_its_name() {
        let line = read("ldr x0, [x0, :got_lo12:environ]").unwrap();
        assert_eq!(line.symbol.as_deref(), Some("environ"));
        let line = read("bl printf").unwrap();
        assert_eq!(line.values, [Value::Symbol(Operator::Plain)]);
        assert_eq!(line.symbol.as_deref(), Some("printf"));
        let line = read("add x0, x0, :lo12:table+24").unwrap();
        assert_eq!((line.symbol.as_deref(), line.addend), (Some("table"), 24));
        let line = read("adrp x1, names-8").unwrap();
        assert_eq!((line.symbol.as_deref(), line.addend), (Some("names"), -8));
        let line = read("cbnz w3, 1b").unwrap();
        assert_eq!((line.symbol.as_deref(), line.addend), (Some("1b"), 0));
        assert!(read("b 1x").is_err());
        assert!(read("b table+").is_err());
    }

    #[test]
    fn what_is_not_a_register_is_refused() {
        assert!(read("ldr x0, [xzr]").is_err());
        assert!(read("ldr x0, [x1, w2]").is_err());
        assert!(read("add x0, x1, #zz").is_err());
        assert_eq!(system("s3_3_c13_c0_2"), system("tpidr_el0"));
    }
}