|
|
|
@ -3,12 +3,15 @@ use std::convert::TryFrom; |
|
|
|
|
|
|
|
|
|
use sexp::{Atom, Sexp, SourcePosition}; |
|
|
|
|
|
|
|
|
|
use crate::asm::data::{DataDisp, Rd, RdData, reg, Wr, WrData, RdWr}; |
|
|
|
|
use crate::asm::data::{DataDisp, Rd, RdData, reg, Wr, WrData, RdWr, Register}; |
|
|
|
|
use crate::asm::data::literal::{ConstantName, Label, RegisterAlias, Value}; |
|
|
|
|
use crate::asm::error::CrsnError; |
|
|
|
|
use crate::asm::parse::ParserContext; |
|
|
|
|
use crate::asm::parse::{ParserContext, parse_instructions}; |
|
|
|
|
use crate::asm::parse::sexp_expect::expect_string_atom; |
|
|
|
|
use crate::asm::patches::ErrWithPos; |
|
|
|
|
use std::sync::atomic::AtomicU32; |
|
|
|
|
use crate::module::OpTrait; |
|
|
|
|
use crate::asm::instr::Cond; |
|
|
|
|
|
|
|
|
|
fn is_valid_identifier(name: &str) -> bool { |
|
|
|
|
// ascii symbols "!\"#$_&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
|
|
|
@ -72,6 +75,7 @@ pub fn parse_label_str(name: &str, pos: &SourcePosition) -> Result<Label, CrsnEr |
|
|
|
|
pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnError> { |
|
|
|
|
// trace!("parse data: {:?}", tok);
|
|
|
|
|
match tok { |
|
|
|
|
// immediates are okay in const eval - no tests needed
|
|
|
|
|
Sexp::Atom(Atom::I(val), _pos) => { |
|
|
|
|
Ok(DataDisp::Immediate(unsafe { std::mem::transmute(val) })) |
|
|
|
|
} |
|
|
|
@ -87,7 +91,57 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
|
Sexp::Atom(Atom::QS(_s), pos) => { |
|
|
|
|
Err(CrsnError::Parse("Quoted string not expected here".into(), pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::List(_list, pos) => { |
|
|
|
|
Sexp::List(list, pos) => { |
|
|
|
|
if let Some(Sexp::Atom(Atom::S(s), s_pos)) = list.first() { |
|
|
|
|
// Const eval
|
|
|
|
|
|
|
|
|
|
let parsing_expr = pcx.state.borrow().parsing_expr; |
|
|
|
|
|
|
|
|
|
if !s.starts_with('=') && !parsing_expr { |
|
|
|
|
return Err(CrsnError::Parse(format!("Invalid syntax, did you mean \"={}\" (a compile-time expression)?", s).into(), s_pos.clone())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// start expr parsing mode
|
|
|
|
|
let orig_parsing_expr = pcx.state.borrow().parsing_expr; |
|
|
|
|
pcx.state.borrow_mut().parsing_expr = true; |
|
|
|
|
|
|
|
|
|
let lmut = AtomicU32::new(0); |
|
|
|
|
|
|
|
|
|
let mut expr_ops = parse_instructions( |
|
|
|
|
// TODO avoid this madness
|
|
|
|
|
vec![Sexp::List(list, pos.clone())].into_iter(), |
|
|
|
|
&pos, pcx)? |
|
|
|
|
.flatten(&lmut)?; |
|
|
|
|
|
|
|
|
|
if expr_ops.len() != 1 { |
|
|
|
|
return Err(CrsnError::Parse(format!("Expected one top level operation in an expression, got: {:?}", expr_ops).into(), pos.clone())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let op = expr_ops.remove(0); |
|
|
|
|
|
|
|
|
|
if !op.is_compile_time_evaluable() { |
|
|
|
|
return Err(CrsnError::Parse(format!("Instruction {} cannot be used in an expression!", op.to_sexp()).into(), pos.clone())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let mut state_mut = pcx.state.borrow_mut(); |
|
|
|
|
state_mut.const_eval.clear_all(); |
|
|
|
|
|
|
|
|
|
let ticl = state_mut.const_eval.thread_info.clone(); |
|
|
|
|
op.execute(&ticl, &mut state_mut.const_eval) |
|
|
|
|
.map_err(|f| CrsnError::ParseOther(Box::new(f), pos.clone()))?; |
|
|
|
|
|
|
|
|
|
if state_mut.const_eval.test_cond(Cond::Invalid) { |
|
|
|
|
return Err(CrsnError::Parse("Expression evaluation failed - invalid flag set!".into(), pos.clone())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !orig_parsing_expr { |
|
|
|
|
// exit expr parsing mode
|
|
|
|
|
state_mut.parsing_expr = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Ok(DataDisp::Immediate(state_mut.const_eval.frame.gen[0])); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Err(CrsnError::Parse("List not expected here".into(), pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
@ -97,12 +151,16 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// 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> { |
|
|
|
|
fn parse_data_disp_from_str(s: &str, pos: &SourcePosition, pcx: &ParserContext) -> Result<DataDisp, CrsnError> { |
|
|
|
|
if s == "_" { |
|
|
|
|
return Ok(DataDisp::Discard); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let Some(reference) = s.strip_prefix('@') { |
|
|
|
|
if pcx.state.borrow().parsing_expr { |
|
|
|
|
return Err(CrsnError::Parse("Object handles cannot be used in expressions!".into(), pos.clone())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* extension constants (pre-defined handles) */ |
|
|
|
|
for p in pcx.parsers { |
|
|
|
|
if let Some(val) = p.get_constant_value(reference) { |
|
|
|
@ -141,7 +199,7 @@ pub fn parse_data_disp_from_str(s: &str, pos: &SourcePosition, pcx: &ParserConte |
|
|
|
|
return Ok(DataDisp::Immediate(*val)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* register aliases */ |
|
|
|
|
/* register aliases - no need to check for const_eval, the aliases could never be defined */ |
|
|
|
|
if let Some(val) = pstate.reg_aliases.get(s) { |
|
|
|
|
return Ok(DataDisp::Register(*val)); |
|
|
|
|
} else if let Some(val) = pstate.global_reg_aliases.get(s) { |
|
|
|
@ -168,6 +226,14 @@ pub fn parse_data_disp_from_str(s: &str, pos: &SourcePosition, pcx: &ParserConte |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if pcx.state.borrow().parsing_expr { |
|
|
|
|
if s == "=result" { |
|
|
|
|
return Ok(DataDisp::Register(Register::Gen(0))); |
|
|
|
|
} else { |
|
|
|
|
return Err(CrsnError::Parse("Registers cannot be used in expressions!".into(), pos.clone())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* register */ |
|
|
|
|
let reg = reg::parse_reg(&s, &pos)?; |
|
|
|
|
|
|
|
|
@ -184,39 +250,13 @@ pub fn parse_data_disp_from_str(s: &str, pos: &SourcePosition, pcx: &ParserConte |
|
|
|
|
|
|
|
|
|
/// Parse immediate value
|
|
|
|
|
pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result<(Value, SourcePosition), CrsnError> { |
|
|
|
|
match tok { |
|
|
|
|
Sexp::Atom(Atom::I(val), pos) => { |
|
|
|
|
Ok((unsafe { std::mem::transmute(val) }, pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::Atom(Atom::U(val), pos) => { |
|
|
|
|
Ok((val, pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::Atom(Atom::C(val), pos) => { |
|
|
|
|
Ok((val as u64, pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::Atom(Atom::QS(_), pos) => { |
|
|
|
|
Err(CrsnError::Parse("quoted string not expected here".into(), pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::Atom(Atom::F(val), pos) => { |
|
|
|
|
Ok((unsafe { std::mem::transmute(val) }, pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::Atom(Atom::S(s), pos) => { |
|
|
|
|
let pstate = pcx.state.borrow(); |
|
|
|
|
if let Some(val) = pstate.constants.get(&s) { |
|
|
|
|
return Ok((*val, pos)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* extension constants */ |
|
|
|
|
for p in pcx.parsers { |
|
|
|
|
if let Some(val) = p.get_constant_value(&s) { |
|
|
|
|
return Ok((val, pos)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
let pos = tok.pos().clone(); |
|
|
|
|
let parsed = parse_rd(tok, pcx)?; |
|
|
|
|
|
|
|
|
|
Err(CrsnError::Parse(format!("unknown constant: {}", s).into(), pos)) |
|
|
|
|
} |
|
|
|
|
Sexp::List(_, pos) => { |
|
|
|
|
Err(CrsnError::Parse("expected a value".into(), pos)) |
|
|
|
|
match parsed { |
|
|
|
|
Rd(RdData::Immediate(value)) => Ok((value, pos)), |
|
|
|
|
_ => { |
|
|
|
|
Err(CrsnError::Parse("expected an immediate value".into(), pos)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|