Merge pull request #3185 from mthom/install_verify_attr_opt

Optimize `verify_attr` by removing the need to scan instructions
This commit is contained in:
Mark Thom
2025-12-03 19:10:46 -07:00
committed by GitHub
12 changed files with 2272 additions and 1947 deletions

View File

@@ -834,12 +834,9 @@ enum InstructionTemplate {
// break from loop instruction. // break from loop instruction.
#[strum_discriminants(strum(props(Arity = "0", Name = "break_from_dispatch")))] #[strum_discriminants(strum(props(Arity = "0", Name = "break_from_dispatch")))]
BreakFromDispatchLoop, BreakFromDispatchLoop,
// swap the verify attr interrupt instruction with the next control instruction. // run verify_attr, eventually.
#[strum_discriminants(strum(props(Arity = "0", Name = "install_verify_attr")))] #[strum_discriminants(strum(props(Arity = "0", Name = "run_verify_attr")))]
InstallVerifyAttr, RunVerifyAttr,
// call verify_attrs.
#[strum_discriminants(strum(props(Arity = "1", Name = "verify_attr_interrupt")))]
VerifyAttrInterrupt(usize),
// procedures // procedures
CallClause(ClauseType, usize, usize, bool, bool), // ClauseType, CallClause(ClauseType, usize, usize, bool, bool), // ClauseType,
// arity, // arity,
@@ -951,8 +948,8 @@ fn generate_instruction_preface() -> TokenStream {
fn into_functor(self, arena: &mut Arena) -> MachineStub { fn into_functor(self, arena: &mut Arena) -> MachineStub {
match self { match self {
ArithmeticTerm::Reg(r) => reg_type_into_functor(r), ArithmeticTerm::Reg(r) => reg_type_into_functor(r),
ArithmeticTerm::Interm(i) => { ArithmeticTerm::IntermReg(i) => {
functor!(atom!("intermediate"), [fixnum(i)]) functor!(atom!("x"), [fixnum(i)])
} }
ArithmeticTerm::Number(n) => { ArithmeticTerm::Number(n) => {
functor!(atom!("number"), [number(n, arena)]) functor!(atom!("number"), [number(n, arena)])
@@ -1168,33 +1165,6 @@ fn generate_instruction_preface() -> TokenStream {
pub type CodeDeque = VecDeque<Instruction>; pub type CodeDeque = VecDeque<Instruction>;
impl Instruction { impl Instruction {
#[inline]
pub fn registers(&self) -> Vec<RegType> {
match *self {
Instruction::GetConstant(_, _, r) => vec![r],
Instruction::GetList(_, r) => vec![r],
Instruction::GetPartialString(_, _, r) => vec![r],
Instruction::GetStructure(_, _, _, r) => vec![r],
Instruction::GetVariable(r, t) => vec![r, temp_v!(t)],
Instruction::GetValue(r, t) => vec![r, temp_v!(t)],
Instruction::UnifyLocalValue(r) => vec![r],
Instruction::UnifyVariable(r) => vec![r],
Instruction::PutConstant(_, _, r) => vec![r],
Instruction::PutList(_, r) => vec![r],
Instruction::PutPartialString(_, _, r) => vec![r],
Instruction::PutStructure(_, _, r) => vec![r],
Instruction::PutValue(r, t) => vec![r, temp_v!(t)],
Instruction::PutVariable(r, t) => vec![r, temp_v!(t)],
Instruction::SetLocalValue(r) => vec![r],
Instruction::SetVariable(r) => vec![r],
Instruction::SetValue(r) => vec![r],
Instruction::GetLevel(r) => vec![r],
Instruction::GetPrevLevel(r) => vec![r],
Instruction::GetCutPoint(r) => vec![r],
_ => vec![],
}
}
#[inline] #[inline]
pub fn to_indexing_line_mut(&mut self) -> Option<&mut Vec<IndexingLine>> { pub fn to_indexing_line_mut(&mut self) -> Option<&mut Vec<IndexingLine>> {
match self { match self {
@@ -1211,38 +1181,6 @@ fn generate_instruction_preface() -> TokenStream {
} }
} }
#[inline]
pub fn is_head_instr(&self) -> bool {
matches!(self,
Instruction::Deallocate |
Instruction::GetConstant(..) |
Instruction::GetList(..) |
Instruction::GetPartialString(..) |
Instruction::GetStructure(..) |
Instruction::GetValue(..) |
Instruction::UnifyConstant(..) |
Instruction::UnifyLocalValue(..) |
Instruction::UnifyVariable(..) |
Instruction::UnifyValue(..) |
Instruction::UnifyVoid(..) |
Instruction::GetVariable(..) |
Instruction::PutConstant(..) |
Instruction::PutList(..) |
Instruction::PutPartialString(..) |
Instruction::PutStructure(..) |
Instruction::PutUnsafeValue(..) |
Instruction::PutValue(..) |
Instruction::PutVariable(..) |
Instruction::SetConstant(..) |
Instruction::SetLocalValue(..) |
Instruction::SetVariable(..) |
Instruction::SetValue(..) |
Instruction::SetVoid(..) |
Instruction::GetLevel(..) |
Instruction::GetPrevLevel(..) |
Instruction::GetCutPoint(..))
}
pub fn enqueue_functors( pub fn enqueue_functors(
&self, &self,
arena: &mut Arena, arena: &mut Arena,
@@ -1278,11 +1216,8 @@ fn generate_instruction_preface() -> TokenStream {
fn to_functor(&self, arena: &mut Arena) -> MachineStub { fn to_functor(&self, arena: &mut Arena) -> MachineStub {
match self { match self {
&Instruction::InstallVerifyAttr => { &Instruction::RunVerifyAttr => {
functor!(atom!("install_verify_attr")) functor!(atom!("run_verify_attr"))
}
&Instruction::VerifyAttrInterrupt(arity) => {
functor!(atom!("verify_attr_interrupt"), [fixnum(arity)])
} }
&Instruction::DynamicElse(birth, death, next_or_fail) => { &Instruction::DynamicElse(birth, death, next_or_fail) => {
match (death, next_or_fail) { match (death, next_or_fail) {

View File

@@ -22,7 +22,7 @@ use num_order::NumOrd;
use ordered_float::{Float, OrderedFloat}; use ordered_float::{Float, OrderedFloat};
use std::cell::Cell; use std::cell::Cell;
use std::cmp::{max, min, Ordering}; use std::cmp::Ordering;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::f64; use std::f64;
use std::num::FpCategory; use std::num::FpCategory;
@@ -31,21 +31,11 @@ use std::vec::Vec;
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ArithmeticTerm { pub enum ArithmeticTerm {
IntermReg(usize),
Reg(RegType), Reg(RegType),
Interm(usize),
Number(Number), Number(Number),
} }
impl ArithmeticTerm {
pub(crate) fn interm_or(&self, interm: usize) -> usize {
if let &ArithmeticTerm::Interm(interm) = self {
interm
} else {
interm
}
}
}
impl Default for ArithmeticTerm { impl Default for ArithmeticTerm {
fn default() -> Self { fn default() -> Self {
ArithmeticTerm::Number(Number::default()) ArithmeticTerm::Number(Number::default())
@@ -57,7 +47,7 @@ pub(crate) struct ArithInstructionIterator<'a> {
state_stack: Vec<TermIterState<'a>>, state_stack: Vec<TermIterState<'a>>,
} }
pub(crate) type ArithCont = (CodeDeque, Option<ArithmeticTerm>); pub(crate) type ArithCont = (CodeDeque, ArithmeticTerm);
impl<'a> ArithInstructionIterator<'a> { impl<'a> ArithInstructionIterator<'a> {
fn push_subterm(&mut self, lvl: Level, term: &'a Term) { fn push_subterm(&mut self, lvl: Level, term: &'a Term) {
@@ -90,7 +80,7 @@ impl<'a> ArithInstructionIterator<'a> {
#[derive(Debug)] #[derive(Debug)]
pub(crate) enum ArithTermRef<'a> { pub(crate) enum ArithTermRef<'a> {
Literal(Literal), Literal(Literal),
Op(Atom, usize), // name, arity. Op(Level, &'a Cell<RegType>, Atom, usize), // name, arity.
Var(Level, &'a Cell<VarReg>, VarPtr), Var(Level, &'a Cell<VarReg>, VarPtr),
} }
@@ -105,7 +95,7 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
let arity = subterms.len(); let arity = subterms.len();
if child_num == arity { if child_num == arity {
return Some(Ok(ArithTermRef::Op(name, arity))); return Some(Ok(ArithTermRef::Op(lvl, cell, name, arity)));
} else { } else {
self.state_stack.push(TermIterState::Clause( self.state_stack.push(TermIterState::Clause(
lvl, lvl,
@@ -139,7 +129,6 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
pub(crate) struct ArithmeticEvaluator<'a> { pub(crate) struct ArithmeticEvaluator<'a> {
marker: &'a mut DebrayAllocator, marker: &'a mut DebrayAllocator,
interm: Vec<ArithmeticTerm>, interm: Vec<ArithmeticTerm>,
interm_c: usize,
} }
pub(crate) trait ArithmeticTermIter<'a> { pub(crate) trait ArithmeticTermIter<'a> {
@@ -180,11 +169,10 @@ fn push_literal(interm: &mut Vec<ArithmeticTerm>, c: &Literal) -> Result<(), Ari
} }
impl<'a> ArithmeticEvaluator<'a> { impl<'a> ArithmeticEvaluator<'a> {
pub(crate) fn new(marker: &'a mut DebrayAllocator, target_int: usize) -> Self { pub(crate) fn new(marker: &'a mut DebrayAllocator) -> Self {
ArithmeticEvaluator { ArithmeticEvaluator {
marker, marker,
interm: Vec::new(), interm: Vec::new(),
interm_c: target_int,
} }
} }
@@ -252,56 +240,37 @@ impl<'a> ArithmeticEvaluator<'a> {
} }
} }
fn incr_interm(&mut self) -> usize { fn try_add_to_free_list(&mut self, a1: ArithmeticTerm) {
let temp = self.interm_c; if let ArithmeticTerm::IntermReg(t) = a1 {
self.marker.add_reg_to_free_list(RegType::Temp(t));
self.interm.push(ArithmeticTerm::Interm(temp)); }
self.interm_c += 1;
temp
} }
fn instr_from_clause( fn instr_from_clause(
&mut self, &mut self,
name: Atom, name: Atom,
arity: usize, arity: usize,
arg: usize,
) -> Result<Instruction, ArithmeticError> { ) -> Result<Instruction, ArithmeticError> {
match arity { match arity {
1 => { 1 => {
let a1 = self.interm.pop().unwrap(); let a1 = self.interm.pop().unwrap();
let ninterm = if a1.interm_or(0) == 0 { self.interm.push(ArithmeticTerm::IntermReg(arg));
self.incr_interm() self.try_add_to_free_list(a1);
} else {
self.interm.push(a1);
a1.interm_or(0)
};
self.get_unary_instr(name, a1, ninterm) self.get_unary_instr(name, a1, arg)
} }
2 => { 2 => {
let a2 = self.interm.pop().unwrap(); let a2 = self.interm.pop().unwrap();
let a1 = self.interm.pop().unwrap(); let a1 = self.interm.pop().unwrap();
let min_interm = min(a1.interm_or(0), a2.interm_or(0)); self.interm.push(ArithmeticTerm::IntermReg(arg));
let ninterm = if min_interm == 0 { self.try_add_to_free_list(a1);
let max_interm = max(a1.interm_or(0), a2.interm_or(0)); self.try_add_to_free_list(a2);
if max_interm == 0 { self.get_binary_instr(name, a1, a2, arg)
self.incr_interm()
} else {
self.interm.push(ArithmeticTerm::Interm(max_interm));
self.interm_c = max_interm + 1;
max_interm
}
} else {
self.interm.push(ArithmeticTerm::Interm(min_interm));
self.interm_c = min_interm + 1;
min_interm
};
self.get_binary_instr(name, a1, a2, ninterm)
} }
_ => Err(ArithmeticError::NonEvaluableFunctor( _ => Err(ArithmeticError::NonEvaluableFunctor(
Literal::Atom(name), Literal::Atom(name),
@@ -346,13 +315,20 @@ impl<'a> ArithmeticEvaluator<'a> {
self.interm.push(ArithmeticTerm::Reg(r)); self.interm.push(ArithmeticTerm::Reg(r));
} }
ArithTermRef::Op(name, arity) => { ArithTermRef::Op(lvl, cell, name, arity) => {
code.push_back(self.instr_from_clause(name, arity)?); self.marker
.mark_non_var::<QueryInstruction>(lvl, term_loc, cell, &mut code);
if let RegType::Temp(t) = cell.get() {
code.push_back(self.instr_from_clause(name, arity, t)?);
} else {
unreachable!()
}
} }
} }
} }
Ok((code, self.interm.pop())) Ok((code, self.interm.pop().unwrap()))
} }
} }

View File

@@ -560,20 +560,12 @@ impl CodeGenerator {
&InlinedClauseType::CompareNumber(mut cmp) => { &InlinedClauseType::CompareNumber(mut cmp) => {
self.marker.reset_arg(2); self.marker.reset_arg(2);
let (mut lcode, at_1) = self.compile_arith_expr(&terms[0], 1, term_loc, 1)?; let (mut lcode, at_1) = self.compile_arith_expr(&terms[0], term_loc, 1)?;
let (mut rcode, at_2) = self.compile_arith_expr(&terms[1], term_loc, 2)?;
if !matches!(terms[0], Term::Var(..)) {
self.marker.advance_arg();
}
let (mut rcode, at_2) = self.compile_arith_expr(&terms[1], 2, term_loc, 2)?;
code.append(&mut lcode); code.append(&mut lcode);
code.append(&mut rcode); code.append(&mut rcode);
let at_1 = at_1.unwrap_or(interm!(1));
let at_2 = at_2.unwrap_or(interm!(2));
compare_number_instr!(cmp, at_1, at_2) compare_number_instr!(cmp, at_1, at_2)
} }
InlinedClauseType::IsAtom(..) => match &terms[0] { InlinedClauseType::IsAtom(..) => match &terms[0] {
@@ -787,11 +779,10 @@ impl CodeGenerator {
fn compile_arith_expr( fn compile_arith_expr(
&mut self, &mut self,
term: &Term, term: &Term,
target_int: usize,
term_loc: GenContext, term_loc: GenContext,
arg: usize, arg: usize,
) -> Result<ArithCont, ArithmeticError> { ) -> Result<ArithCont, ArithmeticError> {
let mut evaluator = ArithmeticEvaluator::new(&mut self.marker, target_int); let mut evaluator = ArithmeticEvaluator::new(&mut self.marker);
evaluator.compile_is(term, term_loc, arg) evaluator.compile_is(term, term_loc, arg)
} }
@@ -804,7 +795,7 @@ impl CodeGenerator {
) -> Result<(), CompilationError> { ) -> Result<(), CompilationError> {
macro_rules! compile_expr { macro_rules! compile_expr {
($self:expr, $terms:expr, $term_loc:expr, $code:expr) => {{ ($self:expr, $terms:expr, $term_loc:expr, $code:expr) => {{
let (acode, at) = $self.compile_arith_expr($terms, 1, $term_loc, 2)?; let (acode, at) = $self.compile_arith_expr($terms, $term_loc, 2)?;
$code.extend(acode.into_iter()); $code.extend(acode.into_iter());
at at
}}; }};
@@ -859,16 +850,18 @@ impl CodeGenerator {
} }
} }
Term::Literal( Term::Literal(
_, ref cell,
c @ Literal::Integer(_) c @ Literal::Integer(_)
| c @ Literal::F64(..) | c @ Literal::F64(..)
| c @ Literal::Rational(_) | c @ Literal::Rational(_)
| c @ Literal::Fixnum(_), | c @ Literal::Fixnum(_),
) => { ) => {
let v = HeapCellValue::from(c); let v = HeapCellValue::from(c);
code.push_back(instr!("put_constant", Level::Shallow, v, temp_v!(1)));
self.marker.advance_arg(); self.marker
.mark_non_var::<QueryInstruction>(Level::Shallow, term_loc, &cell, code);
code.push_back(instr!("put_constant", Level::Shallow, v, temp_v!(1)));
compile_expr!(self, &terms[1], term_loc, code) compile_expr!(self, &terms[1], term_loc, code)
} }
_ => { _ => {
@@ -877,7 +870,6 @@ impl CodeGenerator {
} }
}; };
let at = at.unwrap_or(interm!(1));
self.add_call(code, instr!("is", temp_v!(1), at), call_policy); self.add_call(code, instr!("is", temp_v!(1), at), call_policy);
Ok(()) Ok(())
} }

View File

@@ -59,7 +59,7 @@ get_attrs_var_check(Module) -->
!, !,
'$get_attr_list'(Var, Ls), '$get_attr_list'(Var, Ls),
nonvar(Ls), nonvar(Ls),
atts:'$copy_attr_list'(Ls, Module, Attr))]. atts:'$copy_attr_list'(Ls, Module, Attr))].
put_attrs(Name/Arity, Module) --> put_attrs(Name/Arity, Module) -->
put_attr(Name, Arity, Module), put_attr(Name, Arity, Module),

View File

@@ -21,7 +21,6 @@ use ordered_float::{Float, OrderedFloat};
use std::cmp; use std::cmp;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::f64; use std::f64;
use std::mem;
macro_rules! try_numeric_result { macro_rules! try_numeric_result {
($e: expr, $stub_gen: expr) => { ($e: expr, $stub_gen: expr) => {
@@ -1120,23 +1119,18 @@ pub(crate) fn bitwise_complement(n1: Number, arena: &mut Arena) -> Result<Number
impl MachineState { impl MachineState {
#[inline] #[inline]
pub fn get_number(&mut self, at: &ArithmeticTerm) -> Result<Number, MachineStub> { pub fn get_number(&mut self, at: &ArithmeticTerm) -> Result<Number, MachineStub> {
match at { let value = match at {
&ArithmeticTerm::Reg(r) => { &ArithmeticTerm::Reg(r) => self.store(self.deref(self[r])),
let value = self.store(self.deref(self[r])); &ArithmeticTerm::IntermReg(i) => self.registers[i],
ArithmeticTerm::Number(n) => return Ok(*n),
};
match Number::try_from((value, &self.arena.f64_tbl)) { match Number::try_from((value, &self.arena.f64_tbl)) {
Ok(n) => Ok(n), Ok(n) => Ok(n),
Err(_) => { Err(_) => {
self.heap[0] = value; self.heap[0] = value;
self.arith_eval_by_metacall(0) self.arith_eval_by_metacall(0)
}
}
} }
&ArithmeticTerm::Interm(i) => Ok(mem::replace(
&mut self.interms[i - 1],
Number::Fixnum(Fixnum::build_with(0)),
)),
ArithmeticTerm::Number(n) => Ok(*n),
} }
} }
@@ -1158,6 +1152,7 @@ impl MachineState {
term_loc: usize, term_loc: usize,
) -> Result<Number, MachineStub> { ) -> Result<Number, MachineStub> {
let stub_gen = || functor_stub(atom!("is"), 2); let stub_gen = || functor_stub(atom!("is"), 2);
let mut interms = vec![];
let mut iter = let mut iter =
stackful_post_order_iter::<NonListElider>(&mut self.heap, &mut self.stack, term_loc); stackful_post_order_iter::<NonListElider>(&mut self.heap, &mut self.stack, term_loc);
@@ -1194,38 +1189,38 @@ impl MachineState {
read_heap_cell!(value, read_heap_cell!(value,
(HeapCellValueTag::Atom, (name, arity)) => { (HeapCellValueTag::Atom, (name, arity)) => {
if arity == 2 { if arity == 2 {
let a2 = self.interms.pop().unwrap(); let a2 = interms.pop().unwrap();
let a1 = self.interms.pop().unwrap(); let a1 = interms.pop().unwrap();
match name { match name {
atom!("+") => self.interms.push(drop_iter_on_err!( atom!("+") => interms.push(drop_iter_on_err!(
self, self,
iter, iter,
try_numeric_result!(add(a1, a2, &mut self.arena), stub_gen) try_numeric_result!(add(a1, a2, &mut self.arena), stub_gen)
)), )),
atom!("-") => self.interms.push(drop_iter_on_err!( atom!("-") => interms.push(drop_iter_on_err!(
self, self,
iter, iter,
try_numeric_result!(sub(a1, a2, &mut self.arena), stub_gen) try_numeric_result!(sub(a1, a2, &mut self.arena), stub_gen)
)), )),
atom!("*") => self.interms.push(drop_iter_on_err!( atom!("*") => interms.push(drop_iter_on_err!(
self, self,
iter, iter,
try_numeric_result!(mul(a1, a2, &mut self.arena), stub_gen) try_numeric_result!(mul(a1, a2, &mut self.arena), stub_gen)
)), )),
atom!("/") => self.interms.push( atom!("/") => interms.push(
drop_iter_on_err!(self, iter, div(a1, a2)) drop_iter_on_err!(self, iter, div(a1, a2))
), ),
atom!("**") => self.interms.push( atom!("**") => interms.push(
drop_iter_on_err!(self, iter, pow(a1, a2, atom!("is"))) drop_iter_on_err!(self, iter, pow(a1, a2, atom!("is")))
), ),
atom!("^") => self.interms.push( atom!("^") => interms.push(
drop_iter_on_err!(self, iter, int_pow(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, int_pow(a1, a2, &mut self.arena))
), ),
atom!("max") => self.interms.push( atom!("max") => interms.push(
drop_iter_on_err!(self, iter, max(a1, a2)) drop_iter_on_err!(self, iter, max(a1, a2))
), ),
atom!("min") => self.interms.push( atom!("min") => interms.push(
drop_iter_on_err!(self, iter, min(a1, a2)) drop_iter_on_err!(self, iter, min(a1, a2))
), ),
atom!("rdiv") => { atom!("rdiv") => {
@@ -1246,39 +1241,39 @@ impl MachineState {
&mut self.arena &mut self.arena
); );
self.interms.push(Number::Rational(result)); interms.push(Number::Rational(result));
} }
atom!("//") => self.interms.push( atom!("//") => interms.push(
drop_iter_on_err!(self, iter, idiv(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, idiv(a1, a2, &mut self.arena))
), ),
atom!("div") => self.interms.push( atom!("div") => interms.push(
drop_iter_on_err!(self, iter, int_floor_div(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, int_floor_div(a1, a2, &mut self.arena))
), ),
atom!(">>") => self.interms.push( atom!(">>") => interms.push(
drop_iter_on_err!(self, iter, shr(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, shr(a1, a2, &mut self.arena))
), ),
atom!("<<") => self.interms.push( atom!("<<") => interms.push(
drop_iter_on_err!(self, iter, shl(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, shl(a1, a2, &mut self.arena))
), ),
atom!("/\\") => self.interms.push( atom!("/\\") => interms.push(
drop_iter_on_err!(self, iter, and(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, and(a1, a2, &mut self.arena))
), ),
atom!("\\/") => self.interms.push( atom!("\\/") => interms.push(
drop_iter_on_err!(self, iter, or(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, or(a1, a2, &mut self.arena))
), ),
atom!("xor") => self.interms.push( atom!("xor") => interms.push(
drop_iter_on_err!(self, iter, xor(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, xor(a1, a2, &mut self.arena))
), ),
atom!("mod") => self.interms.push( atom!("mod") => interms.push(
drop_iter_on_err!(self, iter, modulus(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, modulus(a1, a2, &mut self.arena))
), ),
atom!("rem") => self.interms.push( atom!("rem") => interms.push(
drop_iter_on_err!(self, iter, remainder(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, remainder(a1, a2, &mut self.arena))
), ),
atom!("atan2") => self.interms.push(Number::Float(OrderedFloat( atom!("atan2") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, atan2(a1, a2)) drop_iter_on_err!(self, iter, atan2(a1, a2))
))), ))),
atom!("gcd") => self.interms.push( atom!("gcd") => interms.push(
drop_iter_on_err!(self, iter, gcd(a1, a2, &mut self.arena)) drop_iter_on_err!(self, iter, gcd(a1, a2, &mut self.arena))
), ),
_ => { _ => {
@@ -1294,56 +1289,56 @@ impl MachineState {
continue; continue;
} else if arity == 1 { } else if arity == 1 {
let a1 = self.interms.pop().unwrap(); let a1 = interms.pop().unwrap();
match name { match name {
atom!("-") => self.interms.push(neg(a1, &mut self.arena)), atom!("-") => interms.push(neg(a1, &mut self.arena)),
atom!("+") => self.interms.push(a1), atom!("+") => interms.push(a1),
atom!("cos") => self.interms.push(Number::Float(OrderedFloat( atom!("cos") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, cos(a1)) drop_iter_on_err!(self, iter, cos(a1))
))), ))),
atom!("sin") => self.interms.push(Number::Float(OrderedFloat( atom!("sin") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, sin(a1)) drop_iter_on_err!(self, iter, sin(a1))
))), ))),
atom!("tan") => self.interms.push(Number::Float(OrderedFloat( atom!("tan") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, tan(a1)) drop_iter_on_err!(self, iter, tan(a1))
))), ))),
atom!("float_fractional_part") => self.interms.push(Number::Float(OrderedFloat( atom!("float_fractional_part") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, float_fractional_part(a1)) drop_iter_on_err!(self, iter, float_fractional_part(a1))
))), ))),
atom!("float_integer_part") => self.interms.push(Number::Float(OrderedFloat( atom!("float_integer_part") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, float_integer_part(a1)) drop_iter_on_err!(self, iter, float_integer_part(a1))
))), ))),
atom!("sqrt") => self.interms.push(Number::Float(OrderedFloat( atom!("sqrt") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, sqrt(a1)) drop_iter_on_err!(self, iter, sqrt(a1))
))), ))),
atom!("log") => self.interms.push(Number::Float(OrderedFloat( atom!("log") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, log(a1)) drop_iter_on_err!(self, iter, log(a1))
))), ))),
atom!("exp") => self.interms.push(Number::Float(OrderedFloat( atom!("exp") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, exp(a1)) drop_iter_on_err!(self, iter, exp(a1))
))), ))),
atom!("acos") => self.interms.push(Number::Float(OrderedFloat( atom!("acos") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, acos(a1)) drop_iter_on_err!(self, iter, acos(a1))
))), ))),
atom!("asin") => self.interms.push(Number::Float(OrderedFloat( atom!("asin") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, asin(a1)) drop_iter_on_err!(self, iter, asin(a1))
))), ))),
atom!("atan") => self.interms.push(Number::Float(OrderedFloat( atom!("atan") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, atan(a1)) drop_iter_on_err!(self, iter, atan(a1))
))), ))),
atom!("abs") => self.interms.push(abs(a1, &mut self.arena)), atom!("abs") => interms.push(abs(a1, &mut self.arena)),
atom!("float") => self.interms.push(Number::Float(OrderedFloat( atom!("float") => interms.push(Number::Float(OrderedFloat(
drop_iter_on_err!(self, iter, float(a1)) drop_iter_on_err!(self, iter, float(a1))
))), ))),
atom!("truncate") => self.interms.push(truncate(a1, &mut self.arena)), atom!("truncate") => interms.push(truncate(a1, &mut self.arena)),
atom!("round") => self.interms.push(drop_iter_on_err!(self, iter, round(a1, &mut self.arena))), atom!("round") => interms.push(drop_iter_on_err!(self, iter, round(a1, &mut self.arena))),
atom!("ceiling") => self.interms.push(ceiling(a1, &mut self.arena)), atom!("ceiling") => interms.push(ceiling(a1, &mut self.arena)),
atom!("floor") => self.interms.push(floor(a1, &mut self.arena)), atom!("floor") => interms.push(floor(a1, &mut self.arena)),
atom!("\\") => self.interms.push( atom!("\\") => interms.push(
drop_iter_on_err!(self, iter, bitwise_complement(a1, &mut self.arena)) drop_iter_on_err!(self, iter, bitwise_complement(a1, &mut self.arena))
), ),
atom!("sign") => self.interms.push(a1.sign()), atom!("sign") => interms.push(a1.sign()),
_ => { _ => {
let evaluable_stub = functor_stub(name, 1); let evaluable_stub = functor_stub(name, 1);
std::mem::drop(iter); std::mem::drop(iter);
@@ -1362,15 +1357,15 @@ impl MachineState {
} else if arity == 0 { } else if arity == 0 {
match name { match name {
atom!("pi") => { atom!("pi") => {
self.interms.push(Number::Float(OrderedFloat(f64::consts::PI))); interms.push(Number::Float(OrderedFloat(f64::consts::PI)));
continue; continue;
} }
atom!("e") => { atom!("e") => {
self.interms.push(Number::Float(OrderedFloat(f64::consts::E))); interms.push(Number::Float(OrderedFloat(f64::consts::E)));
continue; continue;
} }
atom!("epsilon") => { atom!("epsilon") => {
self.interms.push(Number::Float(OrderedFloat(f64::EPSILON))); interms.push(Number::Float(OrderedFloat(f64::EPSILON)));
continue; continue;
} }
_ => { _ => {
@@ -1386,19 +1381,19 @@ impl MachineState {
return Err(self.error_form(evaluable_error, stub)); return Err(self.error_form(evaluable_error, stub));
} }
(HeapCellValueTag::Fixnum, n) => { (HeapCellValueTag::Fixnum, n) => {
self.interms.push(Number::Fixnum(n)); interms.push(Number::Fixnum(n));
} }
(HeapCellValueTag::F64Offset, offset) => { (HeapCellValueTag::F64Offset, offset) => {
let fl = self.arena.f64_tbl.get_entry(offset); let fl = self.arena.f64_tbl.get_entry(offset);
self.interms.push(Number::Float(fl)); interms.push(Number::Float(fl));
} }
(HeapCellValueTag::Cons, ptr) => { (HeapCellValueTag::Cons, ptr) => {
match_untyped_arena_ptr!(ptr, match_untyped_arena_ptr!(ptr,
(ArenaHeaderTag::Integer, n) => { (ArenaHeaderTag::Integer, n) => {
self.interms.push(Number::Integer(n)); interms.push(Number::Integer(n));
} }
(ArenaHeaderTag::Rational, r) => { (ArenaHeaderTag::Rational, r) => {
self.interms.push(Number::Rational(r)); interms.push(Number::Rational(r));
} }
_ => { _ => {
std::mem::drop(iter); std::mem::drop(iter);
@@ -1429,7 +1424,7 @@ impl MachineState {
) )
} }
Ok(self.interms.pop().unwrap()) Ok(interms.pop().unwrap())
} }
} }

View File

@@ -15,7 +15,6 @@ pub(super) struct AttrVarInitializer {
pub(super) bindings: Bindings, pub(super) bindings: Bindings,
pub(super) p: usize, pub(super) p: usize,
pub(super) cp: usize, pub(super) cp: usize,
// pub(super) instigating_p: usize,
pub(super) verify_attrs_loc: usize, pub(super) verify_attrs_loc: usize,
} }
@@ -41,13 +40,13 @@ impl MachineState {
pub(super) fn push_attr_var_binding(&mut self, h: usize, addr: HeapCellValue) { pub(super) fn push_attr_var_binding(&mut self, h: usize, addr: HeapCellValue) {
if self.attr_var_init.bindings.is_empty() { if self.attr_var_init.bindings.is_empty() {
// save self.p and self.cp and ensure that the next // save self.p and self.cp and ensure that the next
// instruction is InstallVerifyAttrInterrupt. // instruction is RunVerifyAttrInterrupt.
self.attr_var_init.p = self.p; self.attr_var_init.p = self.p;
self.attr_var_init.cp = self.cp; self.attr_var_init.cp = self.cp;
self.p = INSTALL_VERIFY_ATTR_INTERRUPT - 1; self.p = VERIFY_ATTR_INTERRUPT_LOC - 1;
self.cp = INSTALL_VERIFY_ATTR_INTERRUPT; self.cp = VERIFY_ATTR_INTERRUPT_LOC;
} }
debug_assert_eq!(self.heap[h].get_tag(), HeapCellValueTag::AttrVar); debug_assert_eq!(self.heap[h].get_tag(), HeapCellValueTag::AttrVar);
@@ -122,7 +121,7 @@ impl MachineState {
let e = self.e; let e = self.e;
let and_frame = self.stack.index_and_frame_mut(e); let and_frame = self.stack.index_and_frame_mut(e);
for i in 1..arity + 1 { for i in 1..=arity {
and_frame[i] = self.registers[i]; and_frame[i] = self.registers[i];
} }

File diff suppressed because it is too large Load Diff

View File

@@ -86,7 +86,6 @@ pub struct MachineState {
pub(super) ball: Ball, pub(super) ball: Ball,
pub(super) ball_stack: Vec<Ball>, // save current ball before jumping via, e.g., verify_attr interrupt. pub(super) ball_stack: Vec<Ball>, // save current ball before jumping via, e.g., verify_attr interrupt.
pub(super) lifted_heap: Heap, pub(super) lifted_heap: Heap,
pub(super) interms: Vec<Number>, // intermediate numbers.
// locations of cleaners, cut points, the previous scc_block. for setup_call_cleanup/3. // locations of cleaners, cut points, the previous scc_block. for setup_call_cleanup/3.
pub(super) cont_pts: Vec<(HeapCellValue, usize, usize)>, pub(super) cont_pts: Vec<(HeapCellValue, usize, usize)>,
pub(super) cwil: CWIL, pub(super) cwil: CWIL,
@@ -125,7 +124,6 @@ impl fmt::Debug for MachineState {
.field("ball", &self.ball) .field("ball", &self.ball)
.field("ball_stack", &self.ball_stack) .field("ball_stack", &self.ball_stack)
.field("lifted_heap", &self.lifted_heap) .field("lifted_heap", &self.lifted_heap)
.field("interms", &self.interms)
.field("flags", &self.flags) .field("flags", &self.flags)
.field("cc", &self.cc) .field("cc", &self.cc)
.field("global_clock", &self.global_clock) .field("global_clock", &self.global_clock)

View File

@@ -58,7 +58,6 @@ impl MachineState {
ball: Ball::new(), ball: Ball::new(),
ball_stack: vec![], ball_stack: vec![],
lifted_heap: Heap::new(), lifted_heap: Heap::new(),
interms: vec![Number::default(); 256],
cont_pts: Vec::with_capacity(256), cont_pts: Vec::with_capacity(256),
cwil: CWIL::new(), cwil: CWIL::new(),
flags: MachineFlags::default(), flags: MachineFlags::default(),

View File

@@ -143,9 +143,8 @@ mod libraries {
} }
pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0; pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0;
pub static INSTALL_VERIFY_ATTR_INTERRUPT: usize = 1; pub static VERIFY_ATTR_INTERRUPT_LOC: usize = 1;
pub static VERIFY_ATTR_INTERRUPT_LOC: usize = 2; pub static LIB_QUERY_SUCCESS: usize = 2;
pub static LIB_QUERY_SUCCESS: usize = 3;
pub struct MachinePreludeView<'a> { pub struct MachinePreludeView<'a> {
pub indices: &'a mut IndexStore, pub indices: &'a mut IndexStore,
@@ -418,12 +417,11 @@ impl Machine {
} }
pub(crate) fn add_impls_to_indices(&mut self) { pub(crate) fn add_impls_to_indices(&mut self) {
let impls_offset = self.code.len() + 4; let impls_offset = self.code.len() + 3;
self.code.extend(vec![ self.code.extend(vec![
Instruction::BreakFromDispatchLoop, Instruction::BreakFromDispatchLoop,
Instruction::InstallVerifyAttr, Instruction::RunVerifyAttr,
Instruction::VerifyAttrInterrupt(0),
Instruction::BreakFromDispatchLoop, // the location of LIB_QUERY_SUCCESS Instruction::BreakFromDispatchLoop, // the location of LIB_QUERY_SUCCESS
Instruction::ExecuteTermGreaterThan, Instruction::ExecuteTermGreaterThan,
Instruction::ExecuteTermLessThan, Instruction::ExecuteTermLessThan,

View File

@@ -25,7 +25,7 @@ use crate::machine::machine_state::*;
use crate::machine::partial_string::*; use crate::machine::partial_string::*;
use crate::machine::stack::*; use crate::machine::stack::*;
use crate::machine::streams::*; use crate::machine::streams::*;
use crate::machine::{get_structure_index, Machine, VERIFY_ATTR_INTERRUPT_LOC}; use crate::machine::{get_structure_index, Machine};
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::char_reader::*; use crate::parser::char_reader::*;
use crate::parser::dashu::Integer; use crate::parser::dashu::Integer;
@@ -6267,6 +6267,7 @@ impl Machine {
self.machine_st.heap[var.get_value() as usize] = value; self.machine_st.heap[var.get_value() as usize] = value;
} }
/*
#[inline(always)] #[inline(always)]
pub(super) fn restore_instr_at_verify_attr_interrupt(&mut self) { pub(super) fn restore_instr_at_verify_attr_interrupt(&mut self) {
match &self.code[VERIFY_ATTR_INTERRUPT_LOC] { match &self.code[VERIFY_ATTR_INTERRUPT_LOC] {
@@ -6281,10 +6282,11 @@ impl Machine {
} }
} }
} }
*/
#[inline(always)] #[inline(always)]
pub(crate) fn reset_attr_var_state(&mut self, queue_len: usize) { pub(crate) fn reset_attr_var_state(&mut self, queue_len: usize) {
self.restore_instr_at_verify_attr_interrupt(); // self.restore_instr_at_verify_attr_interrupt();
self.machine_st.attr_var_init.reset(queue_len); self.machine_st.attr_var_init.reset(queue_len);
} }
@@ -6315,7 +6317,7 @@ impl Machine {
#[inline(always)] #[inline(always)]
pub(crate) fn return_from_verify_attr(&mut self) { pub(crate) fn return_from_verify_attr(&mut self) {
self.restore_instr_at_verify_attr_interrupt(); // self.restore_instr_at_verify_attr_interrupt();
let e = self.machine_st.e; let e = self.machine_st.e;
let frame_len = self.machine_st.stack.index_and_frame(e).prelude.num_cells; let frame_len = self.machine_st.stack.index_and_frame(e).prelude.num_cells;

View File

@@ -369,12 +369,6 @@ macro_rules! compare_number_instr {
}}; }};
} }
macro_rules! interm {
($n: expr) => {
ArithmeticTerm::Interm($n)
};
}
macro_rules! ar_reg { macro_rules! ar_reg {
($r: expr) => { ($r: expr) => {
ArithmeticTerm::Reg($r) ArithmeticTerm::Reg($r)