Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate memory region expressions #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::expressions::{BinaryOperator, Expression};

pub fn evaluate_expression(expr: Expression) -> Result<u64, String> {
Ok(match expr {
Expression::Number(n) => n,
Expression::BinaryOp {
left,
operator,
right,
} => {
let left = evaluate_expression(*left)?;
let right = evaluate_expression(*right)?;
match operator {
BinaryOperator::Plus => left.wrapping_add(right),
BinaryOperator::Minus => left.wrapping_sub(right),
BinaryOperator::Multiply => left.wrapping_mul(right),
BinaryOperator::Divide => left.wrapping_div(right),
_ => return Err(format!("Binary operator {:?} not supported", operator)),
}
}
_ => return Err(format!("Expression {:?} not supported", expr)),
})
}

#[cfg(test)]
mod tests {
use super::*;
use nom::combinator::map_res;
use BinaryOperator::*;

#[test]
fn test_evaluate_expression() {
assert_eq!(evaluate_expression(Expression::Number(42)), Ok(42));

assert_eq!(
evaluate_expression(Expression::BinaryOp {
left: Box::new(Expression::Number(42)),
operator: Plus,
right: Box::new(Expression::Number(42))
}),
Ok(84)
);
assert_eq!(
evaluate_expression(Expression::BinaryOp {
left: Box::new(Expression::Number(42)),
operator: Minus,
right: Box::new(Expression::Number(42))
}),
Ok(0)
);
assert_eq!(
evaluate_expression(Expression::BinaryOp {
left: Box::new(Expression::Number(42)),
operator: Multiply,
right: Box::new(Expression::Number(42))
}),
Ok(1764)
);
assert_eq!(
evaluate_expression(Expression::BinaryOp {
left: Box::new(Expression::Number(42)),
operator: Divide,
right: Box::new(Expression::Number(42))
}),
Ok(1)
);
}

fn expr_result(input: &str, expected: u64) {
assert_done!(
map_res(crate::expressions::expression, evaluate_expression)(input),
expected
);
}

#[test]
fn test_parsed_expressions() {
expr_result("42 - (20 + 21)", 1);
expr_result("42 - (4 * 8)", 10);
expr_result("42", 42);
expr_result("42 + 42", 84);
expr_result("42 - 42", 0);
expr_result("42 * 42", 1764);
expr_result("42 / 42", 1);
expr_result("0x2000000 + (4k * 4)", 0x2000000 + (4 * 1024 * 4));
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod utils;
#[macro_use]
mod whitespace;
mod commands;
mod eval;
mod expressions;
mod idents;
mod memory;
Expand Down
61 changes: 55 additions & 6 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use idents::symbol;
use nom::{
branch::alt,
bytes::complete::{tag, take_until},
combinator::opt,
combinator::{map_res, opt},
sequence::{delimited, tuple},
IResult,
};
use numbers::number;
use whitespace::opt_space;

use crate::{eval::evaluate_expression, expressions::expression};

#[derive(Debug, PartialEq)]
pub struct Region {
pub name: String,
Expand Down Expand Up @@ -37,15 +38,15 @@ pub fn region(input: &str) -> IResult<&str, Region> {
origin,
wsc!(tag("=")),
))(input)?;
let (input, org) = number(input)?;
let (input, origin) = map_res(expression, evaluate_expression)(input)?;
let (input, _) = tuple((wsc!(tag(",")), length, wsc!(tag("="))))(input)?;
let (input, len) = number(input)?;
let (input, length) = map_res(expression, evaluate_expression)(input)?;
Ok((
input,
Region {
name: name.into(),
origin: org,
length: len,
origin,
length,
},
))
}
Expand Down Expand Up @@ -73,4 +74,52 @@ mod tests {
}
);
}

#[test]
fn test_region_expr() {
assert_done!(
region("FLASH : ORIGIN = 0x08000000, LENGTH = 8K"),
Region {
name: "FLASH".into(),
origin: 0x08000000,
length: 8 * 1024,
}
);

assert_done!(
region("RAM : ORIGIN = 0x20000000 + 8K, LENGTH = 640K"),
Region {
name: "RAM".into(),
origin: 0x20000000 + 8 * 1024,
length: 640 * 1024,
}
);

assert_done!(
region("RAM : ORIGIN = 0x20000000, LENGTH = 640K - 8K"),
Region {
name: "RAM".into(),
origin: 0x20000000,
length: 640 * 1024 - 8 * 1024,
}
);

assert_done!(
region("RAM : ORIGIN = 0x20000000 + 8K - 4K, LENGTH = 640K - 8K + 4K"),
Region {
name: "RAM".into(),
origin: 0x20000000 + 4 * 1024,
length: 640 * 1024 - 4 * 1024,
}
);

assert_done!(
region("RAM: ORIGIN = 0x20000000 + 8K - 4K, LENGTH = 640K - 8K + 4K"),
Region {
name: "RAM".into(),
origin: 0x20000000 + 4 * 1024,
length: 640 * 1024 - 4 * 1024,
}
);
}
}
7 changes: 7 additions & 0 deletions src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ mod tests {
assert_done_vec!(parse(" /* hello */ "), 0);
}

#[test]
fn test_bootloader() {
let input = include_str!("../tests/bootloader.ld");
let res = parse(&input);
assert!(!res.unwrap().1.is_empty());
}

#[test]
fn test_parse() {
for entry in fs::read_dir("tests").unwrap() {
Expand Down
6 changes: 6 additions & 0 deletions tests/bootloader.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 8K

RAM : ORIGIN = 0x20000000 + 256K, LENGTH = 640K - 256K
}