move several builtins to control.

This commit is contained in:
Mark Thom
2018-02-12 18:30:44 -07:00
parent 47055d189d
commit a1fb4f529b
4 changed files with 146 additions and 135 deletions

View File

@@ -820,28 +820,17 @@ pub enum ArithmeticInstruction {
} }
pub enum BuiltInInstruction { pub enum BuiltInInstruction {
CleanUpBlock, CleanUpBlock,
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
EraseBall, EraseBall,
Fail, Fail,
GetArgCall, GetArgCall,
GetArgExecute, GetArgExecute,
GetBall, GetBall,
GetCurrentBlock, GetCurrentBlock,
GetCutPoint(RegType), GetCutPoint(RegType),
DynamicCompareNumber(CompareNumberQT),
DynamicIs,
InstallCleaner, InstallCleaner,
InstallNewBlock, InstallNewBlock,
InternalCallN, InternalCallN,
IsAtomic(RegType),
IsCompound(RegType),
IsFloat(RegType),
IsInteger(RegType),
IsNonVar(RegType),
IsRational(RegType),
IsString(RegType),
IsVar(RegType),
ResetBlock, ResetBlock,
RestoreCutPolicy, RestoreCutPolicy,
SetBall, SetBall,
@@ -861,11 +850,14 @@ pub enum ControlInstruction {
CatchCall, CatchCall,
CatchExecute, CatchExecute,
CheckCpExecute, CheckCpExecute,
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
DisplayCall, DisplayCall,
DisplayExecute, DisplayExecute,
Deallocate, Deallocate,
DuplicateTermCall, DuplicateTermCall,
DuplicateTermExecute, DuplicateTermExecute,
DynamicCompareNumber(CompareNumberQT),
DynamicIs,
EqCall, EqCall,
EqExecute, EqExecute,
Execute(TabledRc<Atom>, usize), Execute(TabledRc<Atom>, usize),
@@ -876,10 +868,18 @@ pub enum ControlInstruction {
GotoCall(usize, usize), // p, arity. GotoCall(usize, usize), // p, arity.
GotoExecute(usize, usize), // p, arity. GotoExecute(usize, usize), // p, arity.
GroundCall, GroundCall,
GroundExecute, GroundExecute,
JmpByCall(usize, usize), // arity, global_offset. JmpByCall(usize, usize), // arity, global_offset.
JmpByExecute(usize, usize), JmpByExecute(usize, usize),
IsCall(RegType, ArithmeticTerm), IsCall(RegType, ArithmeticTerm),
IsAtomic(RegType),
IsCompound(RegType),
IsFloat(RegType),
IsInteger(RegType),
IsNonVar(RegType),
IsRational(RegType),
IsString(RegType),
IsVar(RegType),
IsExecute(RegType, ArithmeticTerm), IsExecute(RegType, ArithmeticTerm),
NotEqCall, NotEqCall,
NotEqExecute, NotEqExecute,
@@ -896,10 +896,13 @@ impl ControlInstruction {
&ControlInstruction::Call(_, _, _) => true, &ControlInstruction::Call(_, _, _) => true,
&ControlInstruction::CatchCall => true, &ControlInstruction::CatchCall => true,
&ControlInstruction::CatchExecute => true, &ControlInstruction::CatchExecute => true,
&ControlInstruction::CompareNumber(..) => true,
&ControlInstruction::DisplayCall => true, &ControlInstruction::DisplayCall => true,
&ControlInstruction::DisplayExecute => true, &ControlInstruction::DisplayExecute => true,
&ControlInstruction::DuplicateTermCall => true, &ControlInstruction::DuplicateTermCall => true,
&ControlInstruction::DuplicateTermExecute => true, &ControlInstruction::DuplicateTermExecute => true,
&ControlInstruction::DynamicCompareNumber(_) => true,
&ControlInstruction::DynamicIs => true,
&ControlInstruction::EqCall => true, &ControlInstruction::EqCall => true,
&ControlInstruction::EqExecute => true, &ControlInstruction::EqExecute => true,
&ControlInstruction::Execute(_, _) => true, &ControlInstruction::Execute(_, _) => true,
@@ -916,6 +919,14 @@ impl ControlInstruction {
&ControlInstruction::GotoExecute(..) => true, &ControlInstruction::GotoExecute(..) => true,
&ControlInstruction::GroundCall => true, &ControlInstruction::GroundCall => true,
&ControlInstruction::GroundExecute => true, &ControlInstruction::GroundExecute => true,
&ControlInstruction::IsAtomic(_) => true,
&ControlInstruction::IsCompound(_) => true,
&ControlInstruction::IsFloat(_) => true,
&ControlInstruction::IsInteger(_) => true,
&ControlInstruction::IsNonVar(_) => true,
&ControlInstruction::IsRational(_) => true,
&ControlInstruction::IsString(_) => true,
&ControlInstruction::IsVar(_) => true,
&ControlInstruction::IsCall(..) => true, &ControlInstruction::IsCall(..) => true,
&ControlInstruction::IsExecute(..) => true, &ControlInstruction::IsExecute(..) => true,
&ControlInstruction::JmpByCall(..) => true, &ControlInstruction::JmpByCall(..) => true,

View File

@@ -151,6 +151,24 @@ impl fmt::Display for ControlInstruction {
write!(f, "is_call {}, {}", r, at), write!(f, "is_call {}, {}", r, at),
&ControlInstruction::IsExecute(r, ref at) => &ControlInstruction::IsExecute(r, ref at) =>
write!(f, "is_execute {}, {}", r, at), write!(f, "is_execute {}, {}", r, at),
&ControlInstruction::IsAtomic(r) =>
write!(f, "is_atomic {}", r),
&ControlInstruction::IsCompound(r) =>
write!(f, "is_compound {}", r),
&ControlInstruction::IsFloat(r) =>
write!(f, "is_float {}", r),
&ControlInstruction::IsRational(r) =>
write!(f, "is_rational {}", r),
&ControlInstruction::IsNonVar(r) =>
write!(f, "is_non_var {}", r),
&ControlInstruction::IsString(r) =>
write!(f, "is_string {}", r),
&ControlInstruction::IsInteger(r) =>
write!(f, "is_integer {}", r),
&ControlInstruction::DynamicIs =>
write!(f, "call_is"),
&ControlInstruction::IsVar(r) =>
write!(f, "is_var {}", r),
&ControlInstruction::JmpByCall(arity, offset) => &ControlInstruction::JmpByCall(arity, offset) =>
write!(f, "jmp_by_call {}/{}", offset, arity), write!(f, "jmp_by_call {}/{}", offset, arity),
&ControlInstruction::JmpByExecute(arity, offset) => &ControlInstruction::JmpByExecute(arity, offset) =>
@@ -165,6 +183,10 @@ impl fmt::Display for ControlInstruction {
write!(f, "call_throw"), write!(f, "call_throw"),
&ControlInstruction::ThrowExecute => &ControlInstruction::ThrowExecute =>
write!(f, "execute_throw"), write!(f, "execute_throw"),
&ControlInstruction::CompareNumber(cmp, ref at_1, ref at_2) =>
write!(f, "number_test {}, {}, {} ", cmp, at_1, at_2),
&ControlInstruction::DynamicCompareNumber(cmp) =>
write!(f, "dynamic_number_test {}", cmp),
} }
} }
} }
@@ -186,11 +208,7 @@ 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::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::DynamicCompareNumber(cmp) =>
write!(f, "dynamic_number_test {}", cmp),
&BuiltInInstruction::EraseBall => &BuiltInInstruction::EraseBall =>
write!(f, "erase_ball"), write!(f, "erase_ball"),
&BuiltInInstruction::Fail => &BuiltInInstruction::Fail =>
@@ -210,25 +228,7 @@ impl fmt::Display for BuiltInInstruction {
&BuiltInInstruction::InstallNewBlock => &BuiltInInstruction::InstallNewBlock =>
write!(f, "install_new_block"), write!(f, "install_new_block"),
&BuiltInInstruction::InternalCallN => &BuiltInInstruction::InternalCallN =>
write!(f, "internal_call_N"), write!(f, "internal_call_N"),
&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::DynamicIs =>
write!(f, "call_is"),
&BuiltInInstruction::IsVar(r) =>
write!(f, "is_var {}", r),
&BuiltInInstruction::ResetBlock => &BuiltInInstruction::ResetBlock =>
write!(f, "reset_block"), write!(f, "reset_block"),
&BuiltInInstruction::RestoreCutPolicy => &BuiltInInstruction::RestoreCutPolicy =>

View File

@@ -1128,26 +1128,7 @@ impl MachineState {
cut_policy: &mut Box<CutPolicy>, cut_policy: &mut Box<CutPolicy>,
instr: &BuiltInInstruction) instr: &BuiltInInstruction)
{ {
match instr { match instr {
&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));
self.compare_numbers(cmp, n1, n2);
},
&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::DynamicIs => {
let a = self[temp_v!(1)].clone();
let result = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2)));
self.unify(a, Addr::Con(Constant::Number(result)));
self.p += 1;
},
&BuiltInInstruction::GetArgCall => &BuiltInInstruction::GetArgCall =>
try_or_fail!(self, { try_or_fail!(self, {
let val = self.try_get_arg(); let val = self.try_get_arg();
@@ -1292,71 +1273,7 @@ SetupCallCleanupCutPolicy.")
self.or_stack.truncate(self.b); self.or_stack.truncate(self.b);
self.fail = true; 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(_) => 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::InternalCallN => &BuiltInInstruction::InternalCallN =>
self.handle_internal_call_n(code_dir), self.handle_internal_call_n(code_dir),
&BuiltInInstruction::Fail => { &BuiltInInstruction::Fail => {
@@ -1615,6 +1532,89 @@ SetupCallCleanupCutPolicy.")
self.duplicate_term(); self.duplicate_term();
self.p = self.cp; self.p = self.cp;
}, },
&ControlInstruction::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));
self.compare_numbers(cmp, n1, n2);
},
&ControlInstruction::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);
},
&ControlInstruction::DynamicIs => {
let a = self[temp_v!(1)].clone();
let result = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2)));
self.unify(a, Addr::Con(Constant::Number(result)));
self.p += 1;
},
&ControlInstruction::IsAtomic(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(_) => self.p += 1,
_ => self.fail = true
};
},
&ControlInstruction::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
};
},
&ControlInstruction::IsCompound(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Str(_) => self.p += 1,
_ => self.fail = true
};
},
&ControlInstruction::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
};
},
&ControlInstruction::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
};
},
&ControlInstruction::IsString(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::Con(Constant::String(_)) => self.p += 1,
_ => self.fail = true
};
},
&ControlInstruction::IsNonVar(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::HeapCell(_) | Addr::StackCell(..) => self.fail = true,
_ => self.p += 1
};
},
&ControlInstruction::IsVar(r) => {
let d = self.store(self.deref(self[r].clone()));
match d {
Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1,
_ => self.fail = true
};
},
&ControlInstruction::EqCall => { &ControlInstruction::EqCall => {
self.fail = self.eq_test(); self.fail = self.eq_test();
self.p += 1; self.p += 1;

View File

@@ -30,7 +30,7 @@ macro_rules! deallocate {
macro_rules! compare_number_instr { macro_rules! compare_number_instr {
($cmp: expr, $at_1: expr, $at_2: expr) => ( ($cmp: expr, $at_1: expr, $at_2: expr) => (
Line::BuiltIn(BuiltInInstruction::CompareNumber($cmp, $at_1, $at_2)) Line::Control(ControlInstruction::CompareNumber($cmp, $at_1, $at_2))
) )
} }
@@ -158,50 +158,50 @@ macro_rules! retry_me_else {
macro_rules! is_atomic { macro_rules! is_atomic {
($reg:expr) => ( ($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsAtomic($reg)) Line::Control(ControlInstruction::IsAtomic($reg))
) )
} }
macro_rules! is_integer { macro_rules! is_integer {
($reg:expr) => ( ($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsInteger($reg)) Line::Control(ControlInstruction::IsInteger($reg))
) )
} }
macro_rules! is_compound { macro_rules! is_compound {
($r:expr) => ( ($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsCompound($r)) Line::Control(ControlInstruction::IsCompound($r))
) )
} }
macro_rules! is_float { macro_rules! is_float {
($r:expr) => ( ($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsFloat($r)) Line::Control(ControlInstruction::IsFloat($r))
) )
} }
macro_rules! is_rational { macro_rules! is_rational {
($r:expr) => ( ($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsRational($r)) Line::Control(ControlInstruction::IsRational($r))
) )
} }
macro_rules! is_nonvar { macro_rules! is_nonvar {
($r:expr) => ( ($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsNonVar($r)) Line::Control(ControlInstruction::IsNonVar($r))
) )
} }
macro_rules! is_string { macro_rules! is_string {
($r:expr) => ( ($r:expr) => (
Line::BuiltIn(BuiltInInstruction::IsString($r)) Line::Control(ControlInstruction::IsString($r))
) )
} }
macro_rules! is_var { macro_rules! is_var {
($reg:expr) => ( ($reg:expr) => (
Line::BuiltIn(BuiltInInstruction::IsVar($reg)) Line::Control(ControlInstruction::IsVar($reg))
) )
} }
@@ -469,13 +469,13 @@ macro_rules! display {
macro_rules! dynamic_is { macro_rules! dynamic_is {
() => ( () => (
Line::BuiltIn(BuiltInInstruction::DynamicIs) Line::Control(ControlInstruction::DynamicIs)
) )
} }
macro_rules! dynamic_num_test { macro_rules! dynamic_num_test {
($cmp:expr) => ( ($cmp:expr) => (
Line::BuiltIn(BuiltInInstruction::DynamicCompareNumber($cmp)) Line::Control(ControlInstruction::DynamicCompareNumber($cmp))
) )
} }