refactor BuiltInInstruction

This commit is contained in:
Mark Thom
2018-03-17 15:45:17 -06:00
parent 64ab9a0337
commit a66049a321
5 changed files with 130 additions and 140 deletions

View File

@@ -1192,16 +1192,15 @@ pub enum ArithmeticInstruction {
}
pub enum BuiltInInstruction {
CallInlined(InlinedClauseType, Vec<RegType>),
CleanUpBlock,
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
DefaultRetryMeElse(usize),
DefaultTrustMe,
DefaultSetCutPoint(RegType),
DynamicCompareNumber(CompareNumberQT),
EraseBall,
Fail,
GetArgCall,
GetArgExecute,
GetArg(bool), // last call.
GetBall,
GetCurrentBlock,
GetCutPoint(RegType),
@@ -1209,16 +1208,7 @@ pub enum BuiltInInstruction {
InstallCleaner,
InstallInferenceCounter(RegType, RegType, RegType),
InstallNewBlock,
InternalCallN,
IsAtom(RegType),
IsAtomic(RegType),
IsCompound(RegType),
IsFloat(RegType),
IsInteger(RegType),
IsNonVar(RegType),
IsRational(RegType),
IsString(RegType),
IsVar(RegType),
InternalCallN,
RemoveCallPolicyCheck,
RemoveInferenceCounter(RegType, RegType),
ResetBlock,

View File

@@ -153,8 +153,14 @@ impl fmt::Display for IndexedChoiceInstruction {
impl fmt::Display for BuiltInInstruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&BuiltInInstruction::CallInlined(InlinedClauseType::CompareNumber(cmp), ref rs) =>
write!(f, "number_test {}, {}, {}", cmp, &rs[0], &rs[1]),
&BuiltInInstruction::CallInlined(ict, ref rs) =>
write!(f, "call_inlined_{}, {}", ict.name(), &rs[0]),
&BuiltInInstruction::CleanUpBlock =>
write!(f, "clean_up_block"),
&BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) =>
write!(f, "number_test {}, {}, {} ", cmp, at_1, at_2),
&BuiltInInstruction::DefaultSetCutPoint(r) =>
write!(f, "default_set_cp {}", r),
&BuiltInInstruction::DefaultRetryMeElse(o) =>
@@ -167,9 +173,9 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "erase_ball"),
&BuiltInInstruction::Fail =>
write!(f, "false"),
&BuiltInInstruction::GetArgCall =>
&BuiltInInstruction::GetArg(false) =>
write!(f, "get_arg_call X1, X2, X3"),
&BuiltInInstruction::GetArgExecute =>
&BuiltInInstruction::GetArg(true) =>
write!(f, "get_arg_execute X1, X2, X3"),
&BuiltInInstruction::GetBall =>
write!(f, "get_ball X1"),
@@ -199,28 +205,6 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "unwind_stack"),
&BuiltInInstruction::Unify =>
write!(f, "unify"),
&BuiltInInstruction::IsAtom(r) =>
write!(f, "is_atom {}", r),
&BuiltInInstruction::IsAtomic(r) =>
write!(f, "is_atomic {}", r),
&BuiltInInstruction::IsCompound(r) =>
write!(f, "is_compound {}", r),
&BuiltInInstruction::IsFloat(r) =>
write!(f, "is_float {}", r),
&BuiltInInstruction::IsRational(r) =>
write!(f, "is_rational {}", r),
&BuiltInInstruction::IsNonVar(r) =>
write!(f, "is_non_var {}", r),
&BuiltInInstruction::IsString(r) =>
write!(f, "is_string {}", r),
&BuiltInInstruction::IsInteger(r) =>
write!(f, "is_integer {}", r),
&BuiltInInstruction::IsVar(r) =>
write!(f, "is_var {}", r),
&BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) =>
write!(f, "number_test {}, {}, {} ", cmp, at_1, at_2),
&BuiltInInstruction::DynamicCompareNumber(cmp) =>
write!(f, "dynamic_number_test {}", cmp),
&BuiltInInstruction::RemoveCallPolicyCheck =>
write!(f, "remove_call_policy_check"),
&BuiltInInstruction::RemoveInferenceCounter(r1, r2) =>

View File

@@ -532,7 +532,10 @@ pub(crate) trait CallPolicy: Any {
Ok(())
},
_ => panic!("inlined command: should have been superseded by previous clause.")
&ClauseType::Inlined(ref inlined) => {
machine_st.execute_inlined(inlined, &vec![temp_v!(1), temp_v!(2)]);
Ok(())
}
}
}
}

View File

@@ -1209,12 +1209,102 @@ impl MachineState {
};
}
pub(super) fn execute_inlined(&mut self, inlined: &InlinedClauseType, rs: &Vec<RegType>)
{
let r1 = rs[0].clone();
match inlined {
&InlinedClauseType::CompareNumber(cmp) => {
let r2 = rs[1].clone();
let n1 = try_or_fail!(self, self.arith_eval_by_metacall(r1));
let n2 = try_or_fail!(self, self.arith_eval_by_metacall(r2));
self.compare_numbers(cmp, n1, n2);
},
&InlinedClauseType::IsAtom => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Con(Constant::Atom(_)) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsAtomic => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Con(_) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsInteger => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Con(Constant::Number(Number::Integer(_))) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsCompound => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Str(_) | Addr::Lis(_) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsFloat => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Con(Constant::Number(Number::Float(_))) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsRational => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Con(Constant::Number(Number::Rational(_))) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsString => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::Con(Constant::String(_)) => self.p += 1,
_ => self.fail = true
};
},
&InlinedClauseType::IsNonVar => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::HeapCell(_) | Addr::StackCell(..) => self.fail = true,
_ => self.p += 1
};
},
&InlinedClauseType::IsVar => {
let d = self.store(self.deref(self[r1].clone()));
match d {
Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1,
_ => self.fail = true
};
},
}
}
pub(super) fn execute_built_in_instr<'a>(&mut self, code_dirs: CodeDirs<'a>,
call_policy: &mut Box<CallPolicy>,
cut_policy: &mut Box<CutPolicy>,
instr: &BuiltInInstruction)
{
match instr {
&BuiltInInstruction::CallInlined(ref inlined, ref rs) =>
self.execute_inlined(inlined, rs),
&BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) => {
let n1 = try_or_fail!(self, self.get_number(at_1));
let n2 = try_or_fail!(self, self.get_number(at_2));
@@ -1233,27 +1323,21 @@ impl MachineState {
let mut call_policy = DefaultCallPolicy {};
try_or_fail!(self, call_policy.trust_me(self));
},
&BuiltInInstruction::DynamicCompareNumber(cmp) => {
let n1 = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(1)));
let n2 = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2)));
self.compare_numbers(cmp, n1, n2);
},
&BuiltInInstruction::EraseBall => {
self.ball.0 = 0;
self.ball.1.truncate(0);
self.p += 1;
},
&BuiltInInstruction::GetArgCall =>
&BuiltInInstruction::GetArg(lco) =>
try_or_fail!(self, {
let val = self.try_get_arg();
self.p += 1;
val
}),
&BuiltInInstruction::GetArgExecute =>
try_or_fail!(self, {
let val = self.try_get_arg();
self.p = self.cp.clone();
if lco {
self.p = self.cp.clone();
} else {
self.p += 1;
}
val
}),
&BuiltInInstruction::GetCurrentBlock => {
@@ -1350,78 +1434,6 @@ impl MachineState {
_ => self.throw_exception(functor!("type_error", 1, [heap_atom!("integer_expected")]))
};
},
&BuiltInInstruction::IsAtom(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(Constant::Atom(_)) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsAtomic(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(_) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsInteger(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(Constant::Number(Number::Integer(_))) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsCompound(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Str(_) | Addr::Lis(_) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsFloat(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(Constant::Number(Number::Float(_))) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsRational(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(Constant::Number(Number::Rational(_))) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsString(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(Constant::String(_)) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::IsNonVar(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::HeapCell(_) | Addr::StackCell(..) => self.fail = true,
_ => self.p += 1
};
},
&BuiltInInstruction::IsVar(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1,
_ => self.fail = true
};
},
&BuiltInInstruction::RemoveCallPolicyCheck => {
let restore_default =
match call_policy.downcast_mut::<CallWithInferenceLimitCallPolicy>().ok() {

View File

@@ -168,57 +168,57 @@ macro_rules! retry_me_else {
}
macro_rules! is_atom {
($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsAtom($reg))
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsAtom, vec![$r]))
)
}
macro_rules! is_atomic {
($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsAtomic($reg))
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsAtomic, vec![$r]))
)
}
macro_rules! is_integer {
($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsInteger($reg))
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsInteger, vec![$r]))
)
}
macro_rules! is_compound {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsCompound($r))
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsCompound, vec![$r]))
)
}
macro_rules! is_float {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsFloat($r))
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsFloat, vec![$r]))
)
}
macro_rules! is_rational {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsRational($r))
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsRational, vec![$r]))
)
}
macro_rules! is_nonvar {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsNonVar($r))
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsNonVar, vec![$r]))
)
}
macro_rules! is_string {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsString($r))
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsString, vec![$r]))
)
}
macro_rules! is_var {
($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsVar($reg))
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::IsVar, vec![$r]))
)
}
@@ -462,13 +462,13 @@ macro_rules! sub {
macro_rules! get_arg_call {
() => (
Line::BuiltIn(BuiltInInstruction::GetArgCall)
Line::BuiltIn(BuiltInInstruction::GetArg(false))
)
}
macro_rules! get_arg_execute {
() => (
Line::BuiltIn(BuiltInInstruction::GetArgExecute)
Line::BuiltIn(BuiltInInstruction::GetArg(true))
)
}
@@ -504,7 +504,8 @@ macro_rules! dynamic_is {
macro_rules! dynamic_num_test {
($cmp:expr) => (
Line::BuiltIn(BuiltInInstruction::DynamicCompareNumber($cmp))
Line::BuiltIn(BuiltInInstruction::CallInlined(InlinedClauseType::CompareNumber($cmp),
vec![temp_v!(1), temp_v!(2)]))
)
}