|
|
@ -3,7 +3,7 @@ use std::convert::TryFrom; |
|
|
|
|
|
|
|
|
|
|
|
use sexp::{Atom, Sexp, SourcePosition}; |
|
|
|
use sexp::{Atom, Sexp, SourcePosition}; |
|
|
|
|
|
|
|
|
|
|
|
use crate::asm::data::{DataDisp, Rd, RdData, reg, Wr, WrData}; |
|
|
|
use crate::asm::data::{DataDisp, Rd, RdData, reg, Wr, WrData, RdWr}; |
|
|
|
use crate::asm::data::literal::{ConstantName, Label, RegisterAlias, Value}; |
|
|
|
use crate::asm::data::literal::{ConstantName, Label, RegisterAlias, Value}; |
|
|
|
use crate::asm::error::CrsnError; |
|
|
|
use crate::asm::error::CrsnError; |
|
|
|
use crate::asm::parse::ParserContext; |
|
|
|
use crate::asm::parse::ParserContext; |
|
|
@ -71,9 +71,6 @@ pub fn parse_label_str(name: &str, pos: &SourcePosition) -> Result<Label, CrsnEr |
|
|
|
/// Parse data disposition (address/value, without the read/write restriction)
|
|
|
|
/// Parse data disposition (address/value, without the read/write restriction)
|
|
|
|
pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnError> { |
|
|
|
pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnError> { |
|
|
|
// trace!("parse data: {:?}", tok);
|
|
|
|
// trace!("parse data: {:?}", tok);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO implement masks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match tok { |
|
|
|
match tok { |
|
|
|
Sexp::Atom(Atom::I(val), _pos) => { |
|
|
|
Sexp::Atom(Atom::I(val), _pos) => { |
|
|
|
Ok(DataDisp::Immediate(unsafe { std::mem::transmute(val) })) |
|
|
|
Ok(DataDisp::Immediate(unsafe { std::mem::transmute(val) })) |
|
|
@ -94,6 +91,13 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
Err(CrsnError::Parse("List not expected here".into(), pos)) |
|
|
|
Err(CrsnError::Parse("List not expected here".into(), pos)) |
|
|
|
} |
|
|
|
} |
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
|
|
|
|
parse_data_disp_from_str(&s, &pos, pcx) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Parse data disp from a string token (a &str is used so the string can be preprocessed)
|
|
|
|
|
|
|
|
pub fn parse_data_disp_from_str(s: &str, pos: &SourcePosition, pcx: &ParserContext) -> Result<DataDisp, CrsnError> { |
|
|
|
if s == "_" { |
|
|
|
if s == "_" { |
|
|
|
return Ok(DataDisp::Discard); |
|
|
|
return Ok(DataDisp::Discard); |
|
|
|
} |
|
|
|
} |
|
|
@ -114,12 +118,12 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
return Ok(DataDisp::RegObject(*reg)) |
|
|
|
return Ok(DataDisp::RegObject(*reg)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let reg = reg::parse_reg(reference, &pos)?; |
|
|
|
let reg = reg::parse_reg(reference, pos)?; |
|
|
|
|
|
|
|
|
|
|
|
if pstate.reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
if pstate.reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
Err(CrsnError::Parse(format!("Sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos)) |
|
|
|
Err(CrsnError::Parse(format!("Sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos.clone())) |
|
|
|
} else if pstate.global_reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
} else if pstate.global_reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
Err(CrsnError::Parse(format!("Global sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos)) |
|
|
|
Err(CrsnError::Parse(format!("Global sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos.clone())) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Ok(DataDisp::RegObject(reg)) |
|
|
|
Ok(DataDisp::RegObject(reg)) |
|
|
|
} |
|
|
|
} |
|
|
@ -133,32 +137,50 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
|
|
|
|
|
|
|
|
/* program constants */ |
|
|
|
/* program constants */ |
|
|
|
let pstate = pcx.state.borrow(); |
|
|
|
let pstate = pcx.state.borrow(); |
|
|
|
if let Some(val) = pstate.constants.get(&s) { |
|
|
|
if let Some(val) = pstate.constants.get(s) { |
|
|
|
return Ok(DataDisp::Immediate(*val)); |
|
|
|
return Ok(DataDisp::Immediate(*val)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* register aliases */ |
|
|
|
/* register aliases */ |
|
|
|
if let Some(val) = pstate.reg_aliases.get(&s) { |
|
|
|
if let Some(val) = pstate.reg_aliases.get(s) { |
|
|
|
return Ok(DataDisp::Register(*val)); |
|
|
|
return Ok(DataDisp::Register(*val)); |
|
|
|
} else if let Some(val) = pstate.global_reg_aliases.get(&s) { |
|
|
|
} else if let Some(val) = pstate.global_reg_aliases.get(s) { |
|
|
|
return Ok(DataDisp::Register(*val)); |
|
|
|
return Ok(DataDisp::Register(*val)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* It can also be a plain old number */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if s.starts_with('#') |
|
|
|
|
|
|
|
|| s.starts_with('-') |
|
|
|
|
|
|
|
|| s.starts_with('.') |
|
|
|
|
|
|
|
|| s.starts_with("0x") |
|
|
|
|
|
|
|
|| s.starts_with("0b") |
|
|
|
|
|
|
|
|| s.starts_with(|c : char| c.is_ascii_digit()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
match sexp::atom_of_string(s.to_string()) { |
|
|
|
|
|
|
|
Atom::C(v) => return Ok(DataDisp::Immediate(v as Value)), |
|
|
|
|
|
|
|
Atom::I(v) => return Ok(DataDisp::Immediate(unsafe { std::mem::transmute::<_, u64>(v) } as Value)), |
|
|
|
|
|
|
|
Atom::U(v) => return Ok(DataDisp::Immediate(v as Value)), |
|
|
|
|
|
|
|
Atom::F(v) => return Ok(DataDisp::Immediate(unsafe { std::mem::transmute::<_, u64>(v) } as Value)), |
|
|
|
|
|
|
|
Atom::S(_) | Atom::QS(_) => { |
|
|
|
|
|
|
|
// this will probably fail validation down the line, but it is definitely not a number
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* register */ |
|
|
|
/* register */ |
|
|
|
let reg = reg::parse_reg(&s, &pos)?; |
|
|
|
let reg = reg::parse_reg(&s, &pos)?; |
|
|
|
|
|
|
|
|
|
|
|
let pstate = pcx.state.borrow(); |
|
|
|
let pstate = pcx.state.borrow(); |
|
|
|
if pstate.reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
if pstate.reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
Err(CrsnError::Parse(format!("Sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos)) |
|
|
|
Err(CrsnError::Parse(format!("Sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos.clone())) |
|
|
|
} else if pstate.global_reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
} else if pstate.global_reg_aliases.values().find(|v| **v == reg).is_some() { |
|
|
|
Err(CrsnError::Parse(format!("Global sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos)) |
|
|
|
Err(CrsnError::Parse(format!("Global sym exists for register {}, direct access denied. Unsym it if needed.", reg).into(), pos.clone())) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Ok(DataDisp::Register(reg)) |
|
|
|
Ok(DataDisp::Register(reg)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Parse immediate value
|
|
|
|
/// Parse immediate value
|
|
|
|
pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<(Value, SourcePosition), CrsnError> { |
|
|
|
pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<(Value, SourcePosition), CrsnError> { |
|
|
@ -225,3 +247,59 @@ pub fn parse_wr(tok: Sexp, pcx: &ParserContext) -> Result<Wr, CrsnError> { |
|
|
|
let pos = tok.pos().clone(); |
|
|
|
let pos = tok.pos().clone(); |
|
|
|
Ok(Wr::new(WrData::try_from(parse_data_disp(tok, pcx)?).err_pos(&pos)?)) |
|
|
|
Ok(Wr::new(WrData::try_from(parse_data_disp(tok, pcx)?).err_pos(&pos)?)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_rdwr(tok: Sexp, pcx: &ParserContext) -> Result<RdWr, CrsnError> { |
|
|
|
|
|
|
|
let pos = tok.pos().clone(); |
|
|
|
|
|
|
|
let wr = parse_wr(tok, pcx)?; |
|
|
|
|
|
|
|
if !wr.is_readable() { |
|
|
|
|
|
|
|
return Err(CrsnError::Parse("Argument is not readable!".into(), pos)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(RdWr::new(wr.0)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_wr_offset(tok: Sexp, pcx: &ParserContext) -> Result<(Wr, u32), CrsnError> { |
|
|
|
|
|
|
|
match tok { |
|
|
|
|
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
|
|
|
|
let (s, offset) = parse_disp_offs(&s, &pos)?; |
|
|
|
|
|
|
|
Ok((Wr::new(WrData::try_from(parse_data_disp_from_str(s, &pos, pcx)?).err_pos(&pos)?), offset)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => Ok((parse_wr(tok, pcx)?, 0)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_rd_offset(tok: Sexp, pcx: &ParserContext) -> Result<(Rd, u32), CrsnError> { |
|
|
|
|
|
|
|
match tok { |
|
|
|
|
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
|
|
|
|
let (s, offset) = parse_disp_offs(&s, &pos)?; |
|
|
|
|
|
|
|
Ok((Rd::new(RdData::try_from(parse_data_disp_from_str(s, &pos, pcx)?).err_pos(&pos)?), offset)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => Ok((parse_rd(tok, pcx)?, 0)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_rdwr_offset(tok: Sexp, pcx: &ParserContext) -> Result<(RdWr, u32), CrsnError> { |
|
|
|
|
|
|
|
match tok { |
|
|
|
|
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
|
|
|
|
let (s, offset) = parse_disp_offs(&s, &pos)?; |
|
|
|
|
|
|
|
let wr = WrData::try_from(parse_data_disp_from_str(s, &pos, pcx)?).err_pos(&pos)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !wr.is_readable() { |
|
|
|
|
|
|
|
return Err(CrsnError::Parse("Argument is not readable!".into(), pos)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok((RdWr::new(wr), offset)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => Ok((parse_rdwr(tok, pcx)?, 0)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_disp_offs<'a>(s : &'a str, pos: &SourcePosition) -> Result<(&'a str, u32), CrsnError> { |
|
|
|
|
|
|
|
if s.contains(':') { |
|
|
|
|
|
|
|
let mut split = s.split(':'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let disp = split.next().unwrap(); |
|
|
|
|
|
|
|
let num : u32 = split.next().unwrap().parse().err_pos(pos)?; |
|
|
|
|
|
|
|
Ok((disp, num)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Ok((&s, 0)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|