|
|
@ -8,7 +8,6 @@ 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; |
|
|
|
use crate::asm::parse::sexp_expect::expect_string_atom; |
|
|
|
use crate::asm::parse::sexp_expect::expect_string_atom; |
|
|
|
use std::num::TryFromIntError; |
|
|
|
|
|
|
|
use crate::asm::patches::ErrWithPos; |
|
|
|
use crate::asm::patches::ErrWithPos; |
|
|
|
|
|
|
|
|
|
|
|
fn is_valid_identifier(name: &str) -> bool { |
|
|
|
fn is_valid_identifier(name: &str) -> bool { |
|
|
@ -46,14 +45,14 @@ pub fn parse_constant_name(name: Sexp) -> Result<(ConstantName, SourcePosition), |
|
|
|
pub fn parse_label(name: Sexp) -> Result<Label, CrsnError> { |
|
|
|
pub fn parse_label(name: Sexp) -> Result<Label, CrsnError> { |
|
|
|
// trace!("parse label: {:?}", name);
|
|
|
|
// trace!("parse label: {:?}", name);
|
|
|
|
let (name, namepos) = expect_string_atom(name)?; |
|
|
|
let (name, namepos) = expect_string_atom(name)?; |
|
|
|
Ok(parse_label_str(&name, namepos)?) |
|
|
|
Ok(parse_label_str(&name, &namepos)?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_label_str(name: &str, pos: SourcePosition) -> Result<Label, CrsnError> { |
|
|
|
pub fn parse_label_str(name: &str, pos: &SourcePosition) -> Result<Label, CrsnError> { |
|
|
|
let label = name.trim_start_matches(':'); |
|
|
|
let label = name.trim_start_matches(':'); |
|
|
|
|
|
|
|
|
|
|
|
Ok(if label.starts_with('#') { |
|
|
|
Ok(if label.starts_with('#') { |
|
|
|
let val = parse_u64(&label[1..], pos.clone())?; |
|
|
|
let val = parse_u64(&label[1..], pos)?; |
|
|
|
Label::Numbered(u32::try_from(val).err_pos(pos)?) |
|
|
|
Label::Numbered(u32::try_from(val).err_pos(pos)?) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Label::Named(label.to_string()) |
|
|
|
Label::Named(label.to_string()) |
|
|
@ -67,7 +66,7 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
// TODO implement masks
|
|
|
|
// 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) })) |
|
|
|
} |
|
|
|
} |
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
@ -92,7 +91,7 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
if let Some(val) = pstate.reg_aliases.get(reference) { |
|
|
|
if let Some(val) = pstate.reg_aliases.get(reference) { |
|
|
|
Ok(DataDisp::ObjectPtr(*val)) |
|
|
|
Ok(DataDisp::ObjectPtr(*val)) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
let reg = reg::parse_reg(reference, pos.clone())?; |
|
|
|
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)) |
|
|
@ -101,9 +100,9 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else if s.starts_with(|c: char| c.is_ascii_digit()) { |
|
|
|
} else if s.starts_with(|c: char| c.is_ascii_digit()) { |
|
|
|
Ok(DataDisp::Immediate(unsafe { std::mem::transmute(parse_i64(&s, pos)?) })) |
|
|
|
Ok(DataDisp::Immediate(unsafe { std::mem::transmute(parse_i64(&s, &pos)?) })) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
let reg = reg::parse_reg(&s, pos.clone())?; |
|
|
|
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() { |
|
|
@ -122,7 +121,7 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
/// Parse immediate value
|
|
|
|
/// Parse immediate value
|
|
|
|
pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<Value, CrsnError> { |
|
|
|
pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<Value, CrsnError> { |
|
|
|
match tok { |
|
|
|
match tok { |
|
|
|
Sexp::Atom(Atom::I(val), pos) => { |
|
|
|
Sexp::Atom(Atom::I(val), _pos) => { |
|
|
|
Ok(unsafe { std::mem::transmute(val) }) |
|
|
|
Ok(unsafe { std::mem::transmute(val) }) |
|
|
|
} |
|
|
|
} |
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
@ -131,7 +130,7 @@ pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<Value, CrsnError> { |
|
|
|
return Ok(*val); |
|
|
|
return Ok(*val); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Ok(unsafe { std::mem::transmute(parse_i64(&s, pos)?) }) |
|
|
|
Ok(unsafe { std::mem::transmute(parse_i64(&s, &pos)?) }) |
|
|
|
} |
|
|
|
} |
|
|
|
other => { |
|
|
|
other => { |
|
|
|
Err(CrsnError::Parse(format!("bad value format: {:?}", other).into(), other.pos().clone())) |
|
|
|
Err(CrsnError::Parse(format!("bad value format: {:?}", other).into(), other.pos().clone())) |
|
|
@ -140,7 +139,7 @@ pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<Value, CrsnError> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_u64(literal: &str, pos: SourcePosition) -> Result<u64, CrsnError> { |
|
|
|
pub fn parse_u64(literal: &str, pos: &SourcePosition) -> Result<u64, CrsnError> { |
|
|
|
// trace!("parse u64 from {}", literal);
|
|
|
|
// trace!("parse u64 from {}", literal);
|
|
|
|
let mut without_underscores = Cow::Borrowed(literal); |
|
|
|
let mut without_underscores = Cow::Borrowed(literal); |
|
|
|
if without_underscores.contains('_') { |
|
|
|
if without_underscores.contains('_') { |
|
|
@ -156,21 +155,21 @@ pub fn parse_u64(literal: &str, pos: SourcePosition) -> Result<u64, CrsnError> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_i64(literal: &str, pos: SourcePosition) -> Result<i64, CrsnError> { |
|
|
|
pub fn parse_i64(literal: &str, pos: &SourcePosition) -> Result<i64, CrsnError> { |
|
|
|
// trace!("parse i64 from {}", literal);
|
|
|
|
// trace!("parse i64 from {}", literal);
|
|
|
|
if let Some(_value) = literal.strip_prefix("-") { |
|
|
|
if let Some(_value) = literal.strip_prefix("-") { |
|
|
|
Ok(-1 * i64::try_from(parse_u64(literal, pos.clone())?).err_pos(pos)?) |
|
|
|
Ok(-1 * i64::try_from(parse_u64(literal, pos)?).err_pos(pos)?) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Ok(i64::try_from(parse_u64(literal, pos.clone())?).err_pos(pos)?) |
|
|
|
Ok(i64::try_from(parse_u64(literal, pos)?).err_pos(pos)?) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_rd(tok: Sexp, pcx: &ParserContext) -> Result<Rd, CrsnError> { |
|
|
|
pub fn parse_rd(tok: Sexp, pcx: &ParserContext) -> Result<Rd, CrsnError> { |
|
|
|
let pos = tok.pos().clone(); |
|
|
|
let pos = tok.pos().clone(); |
|
|
|
Ok(Rd::new(RdData::try_from(parse_data_disp(tok, pcx)?).err_pos(pos)?)) |
|
|
|
Ok(Rd::new(RdData::try_from(parse_data_disp(tok, pcx)?).err_pos(&pos)?)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_wr(tok: Sexp, pcx: &ParserContext) -> Result<Wr, CrsnError> { |
|
|
|
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)?)) |
|
|
|
} |
|
|
|
} |
|
|
|