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
#![allow(clippy::inconsistent_digit_grouping, clippy::unreadable_literal)]
pub use error::Error;
use pest::error::Error as PestError;
use pest::error::ErrorVariant as PestErrorVariant;
use pest::iterators::{Pair, Pairs};
use pest::Parser;
use pest_derive::Parser;
use std::io::{ErrorKind as IOErrorKind, Write};
use unescape::unescape;

use crate::util::parse_number_literal;
use symbol_table::SymbolTable;
use crate::symbol_table::table_to_string;

#[cfg(test)]
mod asm_tests;
pub(crate) mod error;
mod symbol_table;
mod util;

/// Parser struct.
#[derive(Parser)]
#[grammar = "asm.pest"]
pub struct AsmParser;

/// Reads code from input and produces [Vec] of parsed pairs.
pub fn parse(input: &str) -> Result<Pairs<Rule>, PestError<Rule>> {
    AsmParser::parse(Rule::file, input)
}

/// Reads code from input and produces object code output and symbol table.
pub fn assemble(input: impl AsRef<str>) -> Result<(Vec<u8>, Vec<u8>), Error> {
    let asm_parsed = parse(input.as_ref())?.collect();
    assemble_from_pairs(asm_parsed)
}

/// Reads code from slice of pairs and produces object code output.
pub fn assemble_from_pairs(asm_parsed: Vec<Pair<Rule>>) -> Result<(Vec<u8>, Vec<u8>), Error> {
    let (symbols, size, entry) = first_pass(&asm_parsed)?;
    let buf: Vec<u8> = Vec::with_capacity(size);
    let mut wr = util::BitVecWriter::new(buf);

    for pair in asm_parsed {
        second_pass(pair, &mut wr, &symbols)?;
        assert_eq!(
            wr.count_written().1,
            0,
            "Each assembly pass should write aligned bytes to buffer"
        );
    }

    let buf = wr.into_inner().into_writer();
    assert_eq!(buf.len(), size * 2 + 2);

    let table = table_to_string(symbols, entry)?;

    Ok((buf, table.into_bytes()))
}

fn first_pass<'i>(pairs: &[Pair<'i, Rule>]) -> Result<(SymbolTable<'i>, usize, usize), Error> {
    let mut symbols = SymbolTable::new();
    let mut offset = 0;
    let mut entry: Option<usize> = None;

    for pair in pairs {
        match pair.as_rule() {
            Rule::orig => {
                if entry.is_some() {
                    panic!("Cannot have multiple .ORIG pseudo-operation");
                }
                entry = Some(
                    parse_number_literal(pair.clone().into_inner().next().unwrap().as_str())? as usize,
                );
            }
            Rule::label_decl => {
                let name = pair.as_str();
                if let Some((_, prev_pair)) = symbols.get(name) {
                    return Err(pair_error_message!(
                        pair.clone(),
                        "Duplicate symbol definition\n{}",
                        pair_error_message!(
                            prev_pair.clone(),
                            "Note: First definition of the symbol was here",
                        )
                    )
                    .into());
                }
                symbols.insert(name.into(), (offset, pair.clone()));
            }
            Rule::instruction | Rule::trap_code | Rule::fill => offset += 1,
            Rule::blkw => {
                if let [content] = collect_inner!(pair.clone()) {
                    offset += util::parse_number_literal(content.as_str())? as usize;
                } else {
                    unreachable!(pair);
                }
            }
            Rule::stringz => {
                let string = pair
                    .clone()
                    .into_inner()
                    .next()
                    .unwrap()
                    .into_inner()
                    .next()
                    .unwrap()
                    .as_str();
                match unescape(string) {
                    Some(us) => {
                        offset += us.bytes().len() + 1;
                    }
                    None => {
                        return Err(
                            pair_error_message!(pair.clone(), "Invalid escape sequence",).into(),
                        );
                    }
                }
            }
            _ => (),
        }
    }

    Ok((
        symbols,
        offset,
        entry.expect("Expected a .ORIG pseudo-operation for file"),
    ))
}

fn second_pass<W: Write>(
    pair: Pair<Rule>,
    wr: &mut util::BitVecWriter<W>,
    symbols: &SymbolTable,
) -> Result<(), Error> {
    match pair.as_rule() {
        Rule::orig => {
            wr.write(16, util::parse_number_literal(pair.into_inner().as_str())?)?;
        }

        Rule::instruction => {
            for inner_pair in pair.into_inner() {
                second_pass(inner_pair, wr, symbols)?;
            }
        }

        rule @ Rule::add | rule @ Rule::and => {
            if let [dr, sr1, sr2] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 4, if rule == Rule::add { 0b0001 } else { 0b0101 }],
                    [register destination_register; dr],
                    [register source_register1; sr1],
                    [const; 3, 0b000],
                    [register source_register2; sr2],
                );
            } else {
                unreachable!();
            }
        }

        rule @ Rule::add_immd | rule @ Rule::and_immd => {
            if let [dr, sr1, immd5] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 4, if rule == Rule::add_immd { 0b0001 } else { 0b0101 }],
                    [register destination_register; dr],
                    [register source_register1; sr1],
                    [bool; true],
                    [number_signed immediate; 5, immd5],
                );
            } else {
                unreachable!();
            }
        }

        Rule::not => {
            if let [dr, sr] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 4, 0b1001],
                    [register destination_register; dr],
                    [register source_register; sr],
                    [const; 6, 0b111111]
                );
            } else {
                unreachable!();
            }
        }

        Rule::br => {
            if let [br_option, label] = collect_inner!(pair) {
                let br_option = br_option.as_str();
                let (n, z, p) = (
                    br_option.find('n').is_some(),
                    br_option.find('z').is_some(),
                    br_option.find('p').is_some(),
                );
                let implicit_unconditional_branch = (n, z, p) == (false, false, false);
                if implicit_unconditional_branch {
                    // TODO: Print position
                    eprintln!("Warning: Use BRnzp instead of BR for clarity");
                }
                write_fields!(
                    wr,
                    [const; 4, 0b0000],
                    [bool; n||implicit_unconditional_branch],
                    [bool; z||implicit_unconditional_branch],
                    [bool; p||implicit_unconditional_branch],
                    [pcoffset; 9, label, symbols],
                );
            } else {
                unreachable!();
            }
        }

        Rule::jmp => {
            if let [br] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 7, 0b1100_000],
                    [register base_register; br],
                    [const; 6, 0b000000]
                );
            } else {
                unreachable!();
            }
        }

        Rule::jsr => {
            if let [pcoffset] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 5, 0b0100_1],
                    [pcoffset; 11, pcoffset, symbols],
                );
            } else {
                unreachable!();
            }
        }

        Rule::jsrr => {
            if let [br] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 7, 0b0100_000],
                    [register base_register; br],
                    [const; 6, 0b000000],
                );
            } else {
                unreachable!();
            }
        }

        rule @ Rule::ld
        | rule @ Rule::ldi
        | rule @ Rule::lea
        | rule @ Rule::st
        | rule @ Rule::sti => {
            if let [dosr, label] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 4, match rule {
                        Rule::ld => 0b0010,
                        Rule::ldi => 0b1010,
                        Rule::lea => 0b1110,
                        Rule::st => 0b0011,
                        Rule::sti => 0b1011,
                        _ => unreachable!()
                    }],
                    [register destination_or_source_register; dosr],
                    [pcoffset; 9, label, symbols],
                );
            } else {
                unreachable!();
            }
        }

        rule @ Rule::ldr | rule @ Rule::str => {
            if let [dosr, br, offset_] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 4, if rule == Rule::ldr { 0b0110 } else { 0b0111 }],
                    [register destination_or_source_register; dosr],
                    [register base_register; br],
                    [number_signed offset; 6, offset_],
                );
            } else {
                unreachable!();
            }
        }

        Rule::ret => {
            write_fields!(wr, [const; 16, 0b1100_000_111_000000]);
        }

        Rule::rti => {
            write_fields!(wr, [const; 16, 0b1000_0000_0000_0000]);
        }

        Rule::trap => {
            if let [trap_vect] = collect_inner!(pair) {
                write_fields!(
                    wr,
                    [const; 8, 0b1111_0000],
                    [number_signed trap_vector; 8, trap_vect],
                );
            } else {
                unreachable!();
            }
        }

        Rule::trap_code => {
            write_fields!(
            wr,
            [const; 8, 0b1111_0000],
            [const; 8,
                match pair.as_str().to_lowercase().trim() {
                    "getc" => 0x20,
                    "out" => 0x21,
                    "puts" => 0x22,
                    "in" => 0x23,
                    "putsp" => 0x24,
                    "halt" => 0x25,
                    _ => unreachable!(),
                }
            ],
            );
        }

        Rule::stringz => {
            unescape(
                pair.into_inner()
                    .next()
                    .unwrap()
                    .into_inner()
                    .next()
                    .unwrap()
                    .as_str(),
            )
            .unwrap()
            .as_bytes()
            .iter()
            .chain(&[0])
            .cloned()
            .map(u16::from)
            .map(|x| wr.write(16, x))
            .collect::<Result<(), _>>()?;
        }

        Rule::nop => {
            write_fields!(
                wr,
                [const; 16, 0b0000_0000_0000_0000],
            );
        }

        Rule::blkw => {
            if let [blocks] = collect_inner!(pair) {
                for _ in 0..util::parse_number_literal(blocks.as_str()).unwrap() {
                    write_fields!(wr, [const; 16, 0u16]);
                }
            } else {
                unreachable!();
            }
        }

        Rule::fill => {
            if let [content] = collect_inner!(pair) {
                write_fields!(wr, [number_signed fill_content; 16, content]);
            } else {
                unreachable!();
            }
        }

        Rule::end => (),
        Rule::EOI => (),
        Rule::label_decl => (),
        _ => unreachable!("{:#?}", pair),
    }
    Ok(())
}