2022-04-15 01:33:36 -05:00
|
|
|
use crate::ast::{Label, Scope, VASMLine};
|
|
|
|
|
use crate::parse_error::AssembleError;
|
|
|
|
|
use crate::vasm_evaluator::eval;
|
2022-04-17 23:35:48 -05:00
|
|
|
use crate::vasm_preprocessor::{Line, LineSource};
|
2022-03-27 23:10:08 -05:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
|
|
/// This will solve all the .equ directives and return a symbol table of them.
|
|
|
|
|
/// .equ directives must be able to be solved in order, that is, in terms of
|
|
|
|
|
/// only preceding .equ directives. Anything else is an error.
|
2022-04-05 18:05:24 -05:00
|
|
|
fn solve_equs(lines: &[VASMLine]) -> Result<Scope, AssembleError> {
|
2022-03-27 23:10:08 -05:00
|
|
|
let mut scope: Scope = Scope::new();
|
2022-04-05 18:05:24 -05:00
|
|
|
let line_nums: BTreeMap<usize, i32> = BTreeMap::new();
|
2022-03-27 23:10:08 -05:00
|
|
|
for (line_idx, line) in lines.iter().enumerate() {
|
2022-04-05 18:05:24 -05:00
|
|
|
let line_num = line_idx + 1;
|
2022-03-27 23:10:08 -05:00
|
|
|
if let VASMLine::Equ(Label(name), expr) = line {
|
2022-04-05 18:05:24 -05:00
|
|
|
let value = eval(expr, line_num, &line_nums, &scope)
|
|
|
|
|
.map_err(|e| AssembleError::EquResolveError(line_num, name.to_string(), e))?;
|
2022-03-27 23:10:08 -05:00
|
|
|
|
2022-04-05 18:05:24 -05:00
|
|
|
if let Some(_old_value) = scope.insert(name.clone(), value) {
|
|
|
|
|
return Err(AssembleError::EquDuplicateError(line_num, name.to_string()));
|
2022-03-27 23:10:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(scope)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 18:05:24 -05:00
|
|
|
type LineLengths = BTreeMap<usize, usize>;
|
|
|
|
|
type LineAddresses = BTreeMap<usize, i32>;
|
2022-03-27 23:10:08 -05:00
|
|
|
|
|
|
|
|
fn arg_length(val: i32) -> usize {
|
|
|
|
|
if val < 0 {
|
|
|
|
|
3
|
|
|
|
|
} else if val < 256 {
|
|
|
|
|
1
|
|
|
|
|
} else if val < 65536 {
|
|
|
|
|
2
|
|
|
|
|
} else {
|
|
|
|
|
3
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This figures out the instruction lengths. We'll do this naively; if we can't
|
|
|
|
|
/// immediately tell that an instruction needs only a 0/1/2 byte argument (because it's
|
|
|
|
|
/// a constant, or a .equ that we've solved, or something) then we'll assume it's a
|
|
|
|
|
/// full 24-bit argument.
|
|
|
|
|
///
|
|
|
|
|
/// - Lines that don't represent output (.equ, .org, etc) have length 0
|
|
|
|
|
/// - .db directives are either strings (set aside the length of the string), or
|
|
|
|
|
/// numbers (set aside three bytes. If it's shorter than that it still may be a variable,
|
|
|
|
|
/// which might grow to be larger).
|
|
|
|
|
/// - Opcodes with no argument are 1 byte long.
|
|
|
|
|
/// - Opcodes with an argument, if that argument is a constant or decidable solely with
|
|
|
|
|
/// what we know right now (.equs), are however long that argument is. If we don't
|
|
|
|
|
/// know right now (based on a label, say) then we'll set aside the full 3 bytes (so it's
|
|
|
|
|
/// 4 bytes long, with the instruction byte).
|
2022-04-05 18:05:24 -05:00
|
|
|
fn measure_instructions(lines: &[VASMLine], scope: &Scope) -> LineLengths {
|
|
|
|
|
let line_nums: BTreeMap<usize, i32> = BTreeMap::new();
|
2022-03-27 23:10:08 -05:00
|
|
|
let mut lengths = LineLengths::new();
|
|
|
|
|
for (line_idx, line) in lines.iter().enumerate() {
|
2022-04-05 18:05:24 -05:00
|
|
|
let line_num = line_idx + 1;
|
2022-03-27 23:10:08 -05:00
|
|
|
match line {
|
|
|
|
|
VASMLine::Instruction(_, _, None) => {
|
|
|
|
|
lengths.insert(line_num, 1);
|
|
|
|
|
}
|
|
|
|
|
VASMLine::Instruction(_, _, Some(node)) => {
|
2022-04-05 18:05:24 -05:00
|
|
|
let len = eval(node, line_num, &line_nums, scope).map_or(3, arg_length);
|
2022-03-27 23:10:08 -05:00
|
|
|
lengths.insert(line_num, len + 1);
|
|
|
|
|
}
|
|
|
|
|
VASMLine::Db(_, _) => {
|
|
|
|
|
lengths.insert(line_num, 3);
|
|
|
|
|
}
|
|
|
|
|
VASMLine::StringDb(_, value) => {
|
|
|
|
|
lengths.insert(line_num, value.len());
|
|
|
|
|
}
|
2022-04-17 23:35:48 -05:00
|
|
|
VASMLine::Org(_, _) | VASMLine::Equ(_, _) | VASMLine::LabelDef(_) | VASMLine::Blank => {
|
2022-03-27 23:10:08 -05:00
|
|
|
lengths.insert(line_num, 0);
|
|
|
|
|
}
|
2022-04-05 18:05:24 -05:00
|
|
|
VASMLine::Macro(_) => unreachable!(),
|
2022-03-27 23:10:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lengths
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Time to start placing labels. The tricky part here is the .org directives, which can have
|
|
|
|
|
/// expressions as their arguments. We'll compromise a little bit and say that a .org directive
|
|
|
|
|
/// can only refer to labels that precede it, so, you can use .orgs to generate (say) a jump table
|
|
|
|
|
/// but still make it easy for me to figure out what refers to what.
|
|
|
|
|
///
|
|
|
|
|
/// We'll go through the lines, adding each one's length (calculated in measure_instructions) to it.
|
|
|
|
|
/// If it has a label, we'll store that label's new value to the scope.
|
|
|
|
|
///
|
|
|
|
|
/// But, we'll skip labels that come before .equs: that would make every .equ set to its address,
|
|
|
|
|
/// rather than the argument.
|
2022-04-05 18:05:24 -05:00
|
|
|
fn place_labels(
|
|
|
|
|
lines: &[VASMLine],
|
|
|
|
|
scope: Scope,
|
2022-03-27 23:10:08 -05:00
|
|
|
lengths: &LineLengths,
|
2022-04-05 18:05:24 -05:00
|
|
|
) -> Result<(LineAddresses, Scope), AssembleError> {
|
2022-03-27 23:10:08 -05:00
|
|
|
let mut scope = scope;
|
|
|
|
|
let mut address = 0;
|
|
|
|
|
let mut addresses = LineAddresses::new();
|
|
|
|
|
for (line_idx, line) in lines.iter().enumerate() {
|
2022-04-05 18:05:24 -05:00
|
|
|
let line_num = line_idx + 1;
|
2022-03-27 23:10:08 -05:00
|
|
|
|
|
|
|
|
if let VASMLine::Org(_, expr) = line {
|
2022-04-05 18:05:24 -05:00
|
|
|
address = eval(expr, line_num, &addresses, &scope)
|
2022-03-27 23:10:08 -05:00
|
|
|
.map_err(|err| AssembleError::OrgResolveError(line_num, err))?;
|
|
|
|
|
addresses.insert(line_num, address);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(Label(label)) = line.label() {
|
|
|
|
|
if !scope.contains_key(label) {
|
2022-04-05 18:05:24 -05:00
|
|
|
scope.insert(label.clone(), address as i32);
|
2022-03-27 23:10:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match line {
|
|
|
|
|
VASMLine::Org(_, _) => {}
|
|
|
|
|
_ => {
|
|
|
|
|
addresses.insert(line_num, address);
|
|
|
|
|
address += *lengths.get(&line_num).unwrap_or(&0) as i32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok((addresses, scope))
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 22:00:18 -05:00
|
|
|
fn poke_word(code: &mut Vec<u8>, at: usize, word: i32) {
|
|
|
|
|
let [low, mid, high, _] = word.to_le_bytes();
|
|
|
|
|
code[at] = low;
|
|
|
|
|
code[at + 1] = mid;
|
|
|
|
|
code[at + 2] = high;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Find the lower and upper bounds where this program will place memory
|
2022-04-05 18:05:24 -05:00
|
|
|
fn code_bounds(
|
|
|
|
|
lines: &[VASMLine],
|
2022-04-01 22:00:18 -05:00
|
|
|
line_addresses: &LineAddresses,
|
|
|
|
|
line_lengths: &LineLengths,
|
2022-04-05 18:05:24 -05:00
|
|
|
) -> Result<(usize, usize), AssembleError> {
|
2022-04-01 22:00:18 -05:00
|
|
|
let mut actual_lines = lines
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.filter(|(_, line)| !line.zero_length());
|
|
|
|
|
let (first_idx, _) = actual_lines.next().ok_or(AssembleError::NoCode)?;
|
2022-04-05 18:05:24 -05:00
|
|
|
let start = line_addresses[&(first_idx + 1)] as usize;
|
2022-04-01 22:00:18 -05:00
|
|
|
|
|
|
|
|
let actual_lines = lines
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.filter(|(_, line)| !line.zero_length());
|
|
|
|
|
let (last_idx, _) = actual_lines.last().unwrap();
|
2022-04-05 18:05:24 -05:00
|
|
|
let end = line_addresses[&(last_idx + 1)] as usize;
|
|
|
|
|
let end_length = line_lengths[&(last_idx + 1)];
|
2022-04-01 22:00:18 -05:00
|
|
|
|
|
|
|
|
Ok((start, end + end_length - 1))
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 23:35:48 -05:00
|
|
|
/// Turn an iterable of strs into an assembled binary. This supports macros, but not
|
|
|
|
|
/// the `#include` macro. The resulting Vec is only as large as it needs to be; if your
|
|
|
|
|
/// code starts with `.org 0x400` and is five bytes long then the Vec will be five
|
|
|
|
|
/// bytes long and index 0 will represent 0x400.
|
|
|
|
|
/// ```
|
|
|
|
|
/// assert_eq!(
|
|
|
|
|
/// vasm::assemble_snippet(vec![".org 0x400", "push 5", "add 7"]),
|
|
|
|
|
/// Ok(vec![0x01, 0x05, 0x05, 0x07])
|
|
|
|
|
/// )
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn assemble_snippet<'a, T: IntoIterator<Item = &'a str>>(
|
|
|
|
|
lines: T,
|
|
|
|
|
) -> Result<Vec<u8>, AssembleError> {
|
|
|
|
|
let mut line_results: Vec<Result<Line, AssembleError>> =
|
|
|
|
|
LineSource::new("_snippet", lines, |_file| {
|
|
|
|
|
Err(AssembleError::IncludeError(
|
|
|
|
|
0,
|
|
|
|
|
"Including is not supported in assembling snippets".to_string(),
|
|
|
|
|
))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2022-04-02 00:15:33 -05:00
|
|
|
|
2022-04-17 23:35:48 -05:00
|
|
|
if let Some(Err(error)) = line_results.iter().find(|line| line.is_err()) {
|
|
|
|
|
Err(error.clone())
|
|
|
|
|
} else {
|
|
|
|
|
generate_code(
|
|
|
|
|
line_results
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.map(|line| line.clone().unwrap().line),
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-02 00:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 22:00:18 -05:00
|
|
|
/// At this point all lines have addresses and lengths, and all arguments are reduced to
|
|
|
|
|
/// numeric constants. It's time to generate code.
|
|
|
|
|
///
|
|
|
|
|
/// - Make an array of zeroes, length (end - start)
|
|
|
|
|
/// - Go through the list of instructions, generating code for them:
|
|
|
|
|
/// - .db instructions turn into byte values starting at `address - start`
|
|
|
|
|
/// - Opcodes turn into instruction bytes at `address - start` followed (maybe) by
|
|
|
|
|
/// arguments.
|
2022-04-02 00:15:33 -05:00
|
|
|
/// - .orgs cause us to skip ahead some in the output
|
2022-04-01 22:00:18 -05:00
|
|
|
///
|
|
|
|
|
/// The instruction bytes are formed of six bits defining the instruction followed by two
|
|
|
|
|
/// bits denoting how many bytes of argument follow it.
|
|
|
|
|
///
|
|
|
|
|
/// Vulcan is a little-endian architecture: multi-byte arguments / .dbs will store the
|
|
|
|
|
/// least-significant byte at the lowest address, then the more significant bytes following.
|
2022-04-17 23:35:48 -05:00
|
|
|
fn generate_code<T: IntoIterator<Item = VASMLine>>(lines: T) -> Result<Vec<u8>, AssembleError> {
|
2022-04-05 18:05:24 -05:00
|
|
|
let lines: Vec<VASMLine> = lines.into_iter().collect();
|
2022-04-02 00:15:33 -05:00
|
|
|
let scope = solve_equs(&lines)?;
|
|
|
|
|
let line_lengths = measure_instructions(&lines, &scope);
|
|
|
|
|
let (line_addresses, scope) = place_labels(&lines, scope, &line_lengths)?;
|
|
|
|
|
let (start, end) = code_bounds(&lines, &line_addresses, &line_lengths)?;
|
|
|
|
|
|
2022-04-01 22:00:18 -05:00
|
|
|
let mut code = vec![0u8; end - start + 1];
|
|
|
|
|
let mut current_addr = start;
|
|
|
|
|
|
|
|
|
|
for (line_idx, line) in lines.iter().enumerate() {
|
2022-04-05 18:05:24 -05:00
|
|
|
let line_num = line_idx + 1;
|
2022-04-01 22:00:18 -05:00
|
|
|
match line {
|
|
|
|
|
VASMLine::Instruction(_, opcode, None) => {
|
2022-04-17 23:35:48 -05:00
|
|
|
code[current_addr - start] = u8::from(*opcode) << 2;
|
2022-04-01 22:00:18 -05:00
|
|
|
current_addr += 1;
|
|
|
|
|
}
|
2022-04-02 00:15:33 -05:00
|
|
|
VASMLine::Instruction(_, opcode, Some(arg)) => {
|
|
|
|
|
let arg = eval(arg, line_num, &line_addresses, &scope)
|
|
|
|
|
.map_err(|err| AssembleError::ArgError(line_num, err))?;
|
2022-04-17 23:35:48 -05:00
|
|
|
let len = line_lengths[&line_num] - 1;
|
2022-04-01 22:00:18 -05:00
|
|
|
let instr = (u8::from(*opcode) << 2) + len as u8;
|
|
|
|
|
code[current_addr - start] = instr;
|
|
|
|
|
let [low, mid, high, _] = arg.to_le_bytes();
|
|
|
|
|
code[current_addr - start + 1] = low;
|
|
|
|
|
if len > 1 {
|
|
|
|
|
code[current_addr - start + 2] = mid
|
|
|
|
|
}
|
|
|
|
|
if len > 2 {
|
|
|
|
|
code[current_addr - start + 3] = high
|
|
|
|
|
}
|
|
|
|
|
current_addr += len + 1;
|
|
|
|
|
}
|
2022-04-02 00:15:33 -05:00
|
|
|
VASMLine::Db(_, arg) => {
|
|
|
|
|
let arg = eval(arg, line_num, &line_addresses, &scope)
|
|
|
|
|
.map_err(|err| AssembleError::ArgError(line_num, err))?;
|
2022-04-01 22:00:18 -05:00
|
|
|
poke_word(&mut code, current_addr - start, arg);
|
|
|
|
|
current_addr += 3;
|
|
|
|
|
}
|
|
|
|
|
VASMLine::StringDb(_, string) => {
|
|
|
|
|
for ch in string.as_bytes() {
|
|
|
|
|
code[current_addr - start] = *ch;
|
|
|
|
|
current_addr += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VASMLine::Org(_, _) => {
|
|
|
|
|
current_addr = line_addresses[&(line_num + 1)] as usize;
|
|
|
|
|
}
|
2022-04-17 23:35:48 -05:00
|
|
|
VASMLine::Equ(_, _) | VASMLine::LabelDef(_) | VASMLine::Blank => {}
|
2022-04-05 18:05:24 -05:00
|
|
|
VASMLine::Macro(_) => unreachable!(),
|
2022-04-01 22:00:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(code)
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-27 23:10:08 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::AssembleError::*;
|
|
|
|
|
use super::*;
|
2022-04-15 01:33:36 -05:00
|
|
|
use crate::ast::VASMLine;
|
2022-04-02 00:15:33 -05:00
|
|
|
use crate::parse_error;
|
2022-04-15 01:33:36 -05:00
|
|
|
use crate::parse_error::EvalError::*;
|
2022-03-27 23:10:08 -05:00
|
|
|
use crate::vasm_parser::parse_vasm_line;
|
|
|
|
|
|
2022-04-05 18:05:24 -05:00
|
|
|
fn parse<'a, T: IntoIterator<Item = &'a str>>(lines: T) -> Vec<VASMLine> {
|
2022-03-27 23:10:08 -05:00
|
|
|
lines
|
2022-04-02 00:15:33 -05:00
|
|
|
.into_iter()
|
|
|
|
|
.map(|line| parse_vasm_line(line).unwrap())
|
2022-03-27 23:10:08 -05:00
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-02 00:15:33 -05:00
|
|
|
fn place_labels_pass<'a, T: IntoIterator<Item = &'a str>>(
|
|
|
|
|
lines: T,
|
2022-04-05 18:05:24 -05:00
|
|
|
) -> Result<(LineAddresses, Scope), AssembleError> {
|
2022-04-02 00:15:33 -05:00
|
|
|
let parsed_lines = parse(lines);
|
|
|
|
|
let scope = solve_equs(&parsed_lines).unwrap();
|
|
|
|
|
let lengths = measure_instructions(&parsed_lines, &scope);
|
|
|
|
|
place_labels(&parsed_lines, scope, &lengths)
|
2022-04-01 22:00:18 -05:00
|
|
|
}
|
|
|
|
|
|
2022-04-02 00:15:33 -05:00
|
|
|
fn bounds<'a, T: IntoIterator<Item = &'a str>>(
|
|
|
|
|
lines: T,
|
2022-04-05 18:05:24 -05:00
|
|
|
) -> Result<(usize, usize), AssembleError> {
|
2022-04-02 00:15:33 -05:00
|
|
|
let parsed_lines = parse(lines);
|
|
|
|
|
let scope = solve_equs(&parsed_lines).unwrap();
|
|
|
|
|
let lengths = measure_instructions(&parsed_lines, &scope);
|
|
|
|
|
let (line_addresses, _scope) = place_labels(&parsed_lines, scope, &lengths)?;
|
|
|
|
|
code_bounds(&parsed_lines, &line_addresses, &lengths)
|
2022-04-01 18:38:46 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-27 23:10:08 -05:00
|
|
|
#[test]
|
|
|
|
|
fn test_equs() {
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["blah: .equ 5+3"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Ok([("blah".to_string(), 8)].into())
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["blah: .equ 5", "foo: .equ 3"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Ok([("blah".to_string(), 5), ("foo".to_string(), 3)].into())
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["blah: .equ 5", "foo: .equ blah + 7"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Ok([("blah".to_string(), 5), ("foo".to_string(), 12)].into())
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["add", "blah: .equ 5"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Ok([("blah".to_string(), 5)].into())
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unsolvable_equs() {
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["blah: .equ 5", "foo: .equ banana"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Err(EquResolveError(
|
|
|
|
|
2,
|
|
|
|
|
"foo".into(),
|
|
|
|
|
MissingLabel("banana".into())
|
|
|
|
|
))
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["blah: .equ foo+3", "foo: .equ 7"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Err(EquResolveError(
|
|
|
|
|
1,
|
|
|
|
|
"blah".into(),
|
|
|
|
|
MissingLabel("foo".into())
|
|
|
|
|
))
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
solve_equs(&parse(["blah: .equ 3", "blah: .equ 7"])),
|
2022-04-05 18:05:24 -05:00
|
|
|
Err(EquDuplicateError(2, "blah".into()))
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_lengths() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
measure_instructions(
|
2022-04-02 00:15:33 -05:00
|
|
|
&parse(["add", "add 1", "add 500", "add 70000", "add -7"]),
|
2022-03-27 23:10:08 -05:00
|
|
|
&[].into()
|
|
|
|
|
),
|
|
|
|
|
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 4)].into()
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
measure_instructions(&parse([".db 7", ".db \"hello\\0\""]), &[].into()),
|
2022-03-27 23:10:08 -05:00
|
|
|
[(1, 3), (2, 6)].into()
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
measure_instructions(&parse([".org 256", "blah:", "foo: .equ 7"]), &[].into()),
|
2022-03-27 23:10:08 -05:00
|
|
|
[(1, 0), (2, 0), (3, 0)].into()
|
|
|
|
|
);
|
2022-04-17 23:35:48 -05:00
|
|
|
assert_eq!(
|
|
|
|
|
measure_instructions(
|
|
|
|
|
&parse([".org 0x400", "push 3", "call blah", "hlt", "blah: mul 2"]),
|
|
|
|
|
&[].into()
|
|
|
|
|
),
|
|
|
|
|
[(1, 0), (2, 2), (3, 4), (4, 1), (5, 2)].into()
|
|
|
|
|
);
|
2022-03-27 23:10:08 -05:00
|
|
|
assert_eq!(
|
|
|
|
|
measure_instructions(
|
2022-04-02 00:15:33 -05:00
|
|
|
&parse(["add 2 + foo", "add 3 + blah", "jmpr @foo"]),
|
2022-04-05 18:05:24 -05:00
|
|
|
&[("blah".to_string(), 300)].into()
|
2022-03-27 23:10:08 -05:00
|
|
|
),
|
|
|
|
|
[(1, 4), (2, 3), (3, 4)].into()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_place_labels() {
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
place_labels_pass(["start: .org 256", "add", "dup"]),
|
2022-03-27 23:10:08 -05:00
|
|
|
Ok((
|
|
|
|
|
[(1, 256), (2, 256), (3, 257)].into(),
|
2022-04-05 18:05:24 -05:00
|
|
|
[("start".to_string(), 256)].into()
|
2022-03-27 23:10:08 -05:00
|
|
|
))
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
place_labels_pass(["push 70000", "dup"]),
|
2022-03-27 23:10:08 -05:00
|
|
|
Ok(([(1, 0), (2, 4)].into(), [].into()))
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
place_labels_pass(["start: .equ 256", "blah: .org start + 4", "add"]),
|
2022-03-27 23:10:08 -05:00
|
|
|
Ok((
|
|
|
|
|
[(1, 0), (2, 260), (3, 260)].into(),
|
2022-04-05 18:05:24 -05:00
|
|
|
[("blah".to_string(), 260), ("start".to_string(), 256)].into()
|
2022-03-27 23:10:08 -05:00
|
|
|
))
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
place_labels_pass(["start: .org 256", "blah: .org start + 10", "add"]),
|
2022-03-27 23:10:08 -05:00
|
|
|
Ok((
|
|
|
|
|
[(1, 256), (2, 266), (3, 266)].into(),
|
2022-04-05 18:05:24 -05:00
|
|
|
[("blah".to_string(), 266), ("start".to_string(), 256)].into()
|
2022-03-27 23:10:08 -05:00
|
|
|
))
|
|
|
|
|
);
|
2022-04-17 23:35:48 -05:00
|
|
|
assert_eq!(
|
|
|
|
|
place_labels_pass([
|
|
|
|
|
".org 1024",
|
|
|
|
|
"nop 3",
|
|
|
|
|
"call blah",
|
|
|
|
|
"hlt",
|
|
|
|
|
"blah: mul 2",
|
|
|
|
|
"ret"
|
|
|
|
|
]),
|
|
|
|
|
Ok((
|
|
|
|
|
[
|
|
|
|
|
(1, 1024),
|
|
|
|
|
(2, 1024),
|
|
|
|
|
(3, 1026),
|
|
|
|
|
(4, 1030),
|
|
|
|
|
(5, 1031),
|
|
|
|
|
(6, 1033)
|
|
|
|
|
]
|
|
|
|
|
.into(),
|
|
|
|
|
[("blah".to_string(), 1031)].into()
|
|
|
|
|
))
|
|
|
|
|
);
|
2022-03-27 23:10:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unresolvable_orgs() {
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
place_labels_pass([".org 0xffffff - blah"]),
|
2022-04-15 01:33:36 -05:00
|
|
|
Err(OrgResolveError(1, MissingLabel("blah".into())))
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
place_labels_pass(["blah: .org blah"]),
|
2022-04-15 01:33:36 -05:00
|
|
|
Err(OrgResolveError(1, MissingLabel("blah".into())))
|
2022-03-27 23:10:08 -05:00
|
|
|
);
|
|
|
|
|
}
|
2022-04-01 18:38:46 -05:00
|
|
|
|
2022-04-01 22:00:18 -05:00
|
|
|
#[test]
|
|
|
|
|
fn test_bounds() {
|
2022-04-02 00:15:33 -05:00
|
|
|
assert_eq!(bounds(["add"]), Ok((0, 0)));
|
|
|
|
|
assert_eq!(bounds(["add -4"]), Ok((0, 3)));
|
|
|
|
|
assert_eq!(bounds(["add 7"]), Ok((0, 1)));
|
|
|
|
|
assert_eq!(bounds([".org 0x400", "add"]), Ok((1024, 1024)));
|
2022-04-01 22:00:18 -05:00
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
bounds(["start: .equ 1024", ".org start", "add"]),
|
2022-04-01 22:00:18 -05:00
|
|
|
Ok((1024, 1024))
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
bounds([".org 0x400", "add", ".org 0x800"]),
|
2022-04-01 22:00:18 -05:00
|
|
|
Ok((1024, 1024))
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
bounds([".org 0x400", "add", ".org 0x800", ".db 5", "blah:"]),
|
2022-04-01 22:00:18 -05:00
|
|
|
Ok((1024, 2050))
|
|
|
|
|
);
|
2022-04-02 00:15:33 -05:00
|
|
|
assert_eq!(bounds([]), Err(AssembleError::NoCode));
|
|
|
|
|
assert_eq!(bounds([".org 0x400"]), Err(AssembleError::NoCode));
|
2022-04-01 22:00:18 -05:00
|
|
|
assert_eq!(
|
2022-04-02 00:15:33 -05:00
|
|
|
bounds(["foo: .equ 3", ".org 0x400"]),
|
2022-04-01 22:00:18 -05:00
|
|
|
Err(AssembleError::NoCode)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_generate_code() {
|
2022-04-02 00:15:33 -05:00
|
|
|
assert_eq!(generate_code(parse(["add"])), Ok(vec![4]));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
generate_code(parse([".org 0x400", "add 7"])),
|
|
|
|
|
Ok(vec![5, 7])
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(generate_code(parse([".db 57"])), Ok(vec![57, 0, 0]));
|
|
|
|
|
assert_eq!(generate_code(parse([".db \"AZ\0\""])), Ok(vec![65, 90, 0]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-04-17 23:35:48 -05:00
|
|
|
fn test_assemble_snippet() {
|
|
|
|
|
assert_eq!(assemble_snippet(["add"]), Ok(vec![4]));
|
2022-04-02 00:15:33 -05:00
|
|
|
assert_eq!(
|
2022-04-17 23:35:48 -05:00
|
|
|
assemble_snippet(["apple"]),
|
2022-04-02 00:15:33 -05:00
|
|
|
Err(ParseError(
|
|
|
|
|
1,
|
2022-04-05 18:05:24 -05:00
|
|
|
parse_error::ParseError::InvalidInstruction("apple".into())
|
2022-04-02 00:15:33 -05:00
|
|
|
))
|
|
|
|
|
);
|
2022-04-17 23:35:48 -05:00
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
assemble_snippet(
|
|
|
|
|
".org 0x400
|
|
|
|
|
nop 3
|
|
|
|
|
call blah
|
|
|
|
|
hlt
|
|
|
|
|
blah: mul 2
|
|
|
|
|
ret"
|
|
|
|
|
.lines()
|
|
|
|
|
),
|
|
|
|
|
Ok(vec![
|
|
|
|
|
0x01, 0x03, // nop 3
|
|
|
|
|
0x67, 0x07, 0x04, 0x00, // call blah (arg defaults to 3 bytes long)
|
|
|
|
|
0x74, // hlt
|
|
|
|
|
0x0d, 0x02, // mul 2
|
|
|
|
|
0x68
|
|
|
|
|
])
|
|
|
|
|
);
|
2022-04-01 22:00:18 -05:00
|
|
|
}
|
2022-03-27 23:10:08 -05:00
|
|
|
}
|