From fa49d7abfb3c19cab7df33b9668fc7ba1b3ada57 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 4 Jul 2023 21:58:15 +0200 Subject: [PATCH] Read in map file --- src/Tools/readmap/src/lib.rs | 64 +++++++++++++++++++++++++----- src/Tools/readmap/src/types/mod.rs | 10 ++--- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index f66a5dba..83c7c243 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -5,16 +5,20 @@ use std::io::BufRead; use types::{Content, Section}; pub fn scan(input: Input) -> Result, Error> { - process(input.lines().map(|l| l.unwrap()).into_iter())?; + let sections = process(input.lines().map(|l| l.unwrap()).into_iter())?; - Err(Error::not_implemented("readmap::scan")) + Ok(sections) } -fn process>(mut line_iter:F) -> Result<(), Error> { +fn process>(mut line_iter:F) -> Result, Error> { + let mut sections = Vec::new(); while let Some(mut line) = line_iter.next() { loop { if line.starts_with(".") { - line = process_section(line, &mut line_iter)?; + let (new_line, new_section) = process_section(line, &mut line_iter)?; + + sections.push(new_section); + line = new_line; } else { @@ -23,17 +27,16 @@ fn process>(mut line_iter:F) -> Result<(), E } } - Ok(()) + Ok(sections) } -fn process_section>(line: String, line_iter:&mut F) -> Result { +fn process_section>(line: String, line_iter:&mut F) -> Result<(String, Section), Error> { let mut next_line_closure = || { line_iter.next().ok_or(Error::from_str("Unexpected end of file")) }; let (mut section, line) = read_section(line, &mut next_line_closure)?; - println!("Section: {} @{:?} {:?} \"{:?}\"", section.name, section.adr, section.size, section.file); - process_subsection(&mut section, line, &mut next_line_closure) + Ok((process_subsection(&mut section, line, &mut next_line_closure)?, section)) } fn process_subsection Result>(section: &mut Section, line: Option, next_line: &mut F) -> Result { @@ -42,6 +45,18 @@ fn process_subsection Result>(section: &mut Section, section.content.push(Content::Section(sub_section)); } } + fn add_element(sub_section: Option
, section: &mut Section, value: T, callback: fn(&mut Section, T)) -> Option
{ + if let Some(mut sub_section) = sub_section { + callback(&mut sub_section, value); + Some(sub_section) + } + + else { + callback(section, value); + None + } + } + let mut line = { if let Some(line) = line { line @@ -69,8 +84,7 @@ fn process_subsection Result>(section: &mut Section, if line.starts_with(" .") { push_sub_section(section, sub_section); let (section, new_line) = read_section(line, &mut next_line_closure)?; - - println!("\t>>>>> {:?} {:?} {:?} {:?}", section.name, section.adr, section.size, section.file); + sub_section = Some(section); if let Some(new_line) = new_line { @@ -79,6 +93,20 @@ fn process_subsection Result>(section: &mut Section, } } + else if line.starts_with(" *fill*") { + let fill = read_fill(line)?; + sub_section = add_element(sub_section, section, fill, |section, fill| { + section.content.push(Content::Fill(fill)); + }); + } + + else if line.starts_with(" 0x") { + let symbol = read_symbol(line)?; + sub_section = add_element(sub_section, section, symbol, |section, symbol| { + section.content.push(Content::Symbol(symbol)); + }); + } + else if line.is_empty() { push_sub_section(section, sub_section); return Ok(line); @@ -124,6 +152,22 @@ fn read_section Result>(mut line: String, next_line: Ok((Section::new(name, adr, size, file), None)) } +fn read_fill(line: String) -> Result { + let mut split_line = line.split_whitespace().skip(1); + let adr = read_as::(split_line.next()).ok_or_else(|| {return Error::from_str("*fill* statement requires an address")})?; + let size = read_as::(split_line.next()).ok_or_else(|| {return Error::from_str("*fill* statement requires a size")})?; + + Ok(types::Fill::new(adr, size)) +} + +fn read_symbol(line: String) -> Result { + let mut split_line = line.split_whitespace(); + let adr = read_as::(split_line.next()).ok_or_else(|| {return Error::from_str("Symbol statement requires an address")})?; + let name = split_line.next().ok_or_else(|| {return Error::from_str("Symbol statement requires a symbol name")})?.to_string(); + + Ok(types::Symbol::new(name, adr)) +} + fn read_as(str: Option<&str>) -> Option { if let Some(str) = str { F::from_str_radix(str.trim_start_matches("0x"), 16).ok() diff --git a/src/Tools/readmap/src/types/mod.rs b/src/Tools/readmap/src/types/mod.rs index 03b06a44..8d4b1493 100644 --- a/src/Tools/readmap/src/types/mod.rs +++ b/src/Tools/readmap/src/types/mod.rs @@ -11,18 +11,18 @@ pub struct Section { #[derive(Default)] pub struct Symbol { - name: String, - adr: u64, + pub name: String, + pub adr: u64, } #[derive(Default)] pub struct Fill { - adr: u64, - size: usize, + pub adr: u64, + pub size: usize, } pub enum Content { - Fill, + Fill(Fill), Section(Section), Symbol(Symbol), }