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

View File

@@ -153,8 +153,14 @@ impl fmt::Display for IndexedChoiceInstruction {
impl fmt::Display for BuiltInInstruction { impl fmt::Display for BuiltInInstruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { 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 => &BuiltInInstruction::CleanUpBlock =>
write!(f, "clean_up_block"), 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) => &BuiltInInstruction::DefaultSetCutPoint(r) =>
write!(f, "default_set_cp {}", r), write!(f, "default_set_cp {}", r),
&BuiltInInstruction::DefaultRetryMeElse(o) => &BuiltInInstruction::DefaultRetryMeElse(o) =>
@@ -167,9 +173,9 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "erase_ball"), write!(f, "erase_ball"),
&BuiltInInstruction::Fail => &BuiltInInstruction::Fail =>
write!(f, "false"), write!(f, "false"),
&BuiltInInstruction::GetArgCall => &BuiltInInstruction::GetArg(false) =>
write!(f, "get_arg_call X1, X2, X3"), write!(f, "get_arg_call X1, X2, X3"),
&BuiltInInstruction::GetArgExecute => &BuiltInInstruction::GetArg(true) =>
write!(f, "get_arg_execute X1, X2, X3"), write!(f, "get_arg_execute X1, X2, X3"),
&BuiltInInstruction::GetBall => &BuiltInInstruction::GetBall =>
write!(f, "get_ball X1"), write!(f, "get_ball X1"),
@@ -199,28 +205,6 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "unwind_stack"), write!(f, "unwind_stack"),
&BuiltInInstruction::Unify => &BuiltInInstruction::Unify =>
write!(f, "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 => &BuiltInInstruction::RemoveCallPolicyCheck =>
write!(f, "remove_call_policy_check"), write!(f, "remove_call_policy_check"),
&BuiltInInstruction::RemoveInferenceCounter(r1, r2) => &BuiltInInstruction::RemoveInferenceCounter(r1, r2) =>

View File

@@ -532,7 +532,10 @@ pub(crate) trait CallPolicy: Any {
Ok(()) 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>, pub(super) fn execute_built_in_instr<'a>(&mut self, code_dirs: CodeDirs<'a>,
call_policy: &mut Box<CallPolicy>, call_policy: &mut Box<CallPolicy>,
cut_policy: &mut Box<CutPolicy>, cut_policy: &mut Box<CutPolicy>,
instr: &BuiltInInstruction) instr: &BuiltInInstruction)
{ {
match instr { match instr {
&BuiltInInstruction::CallInlined(ref inlined, ref rs) =>
self.execute_inlined(inlined, rs),
&BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) => { &BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) => {
let n1 = try_or_fail!(self, self.get_number(at_1)); let n1 = try_or_fail!(self, self.get_number(at_1));
let n2 = try_or_fail!(self, self.get_number(at_2)); let n2 = try_or_fail!(self, self.get_number(at_2));
@@ -1233,27 +1323,21 @@ impl MachineState {
let mut call_policy = DefaultCallPolicy {}; let mut call_policy = DefaultCallPolicy {};
try_or_fail!(self, call_policy.trust_me(self)); 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 => { &BuiltInInstruction::EraseBall => {
self.ball.0 = 0; self.ball.0 = 0;
self.ball.1.truncate(0); self.ball.1.truncate(0);
self.p += 1; self.p += 1;
}, },
&BuiltInInstruction::GetArgCall => &BuiltInInstruction::GetArg(lco) =>
try_or_fail!(self, { try_or_fail!(self, {
let val = self.try_get_arg(); let val = self.try_get_arg();
self.p += 1;
val if lco {
}), self.p = self.cp.clone();
&BuiltInInstruction::GetArgExecute => } else {
try_or_fail!(self, { self.p += 1;
let val = self.try_get_arg(); }
self.p = self.cp.clone();
val val
}), }),
&BuiltInInstruction::GetCurrentBlock => { &BuiltInInstruction::GetCurrentBlock => {
@@ -1350,78 +1434,6 @@ impl MachineState {
_ => self.throw_exception(functor!("type_error", 1, [heap_atom!("integer_expected")])) _ => 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 => { &BuiltInInstruction::RemoveCallPolicyCheck => {
let restore_default = let restore_default =
match call_policy.downcast_mut::<CallWithInferenceLimitCallPolicy>().ok() { match call_policy.downcast_mut::<CallWithInferenceLimitCallPolicy>().ok() {

View File

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