flatten the instruction dispatch loop

This commit is contained in:
Mark Thom
2021-12-19 18:57:57 -07:00
parent 3db86f1e25
commit 955e1799c8
52 changed files with 14247 additions and 10021 deletions

View File

@@ -1,39 +1,41 @@
use crate::parser::ast::*;
use crate::atom_table::*;
use crate::clause_types::*;
use crate::forms::*;
use crate::instructions::*;
use crate::iterators::*;
use crate::types::*;
pub(crate) struct FactInstruction;
pub(crate) struct QueryInstruction;
pub(crate) trait CompilationTarget<'a> {
type Iterator: Iterator<Item = TermRef<'a>>;
fn iter(term: &'a Term) -> Self::Iterator;
fn to_constant(lvl: Level, literal: Literal, r: RegType) -> Self;
fn to_list(lvl: Level, r: RegType) -> Self;
fn to_structure(ct: ClauseType, arity: usize, r: RegType) -> Self;
fn to_constant(lvl: Level, literal: Literal, r: RegType) -> Instruction;
fn to_list(lvl: Level, r: RegType) -> Instruction;
fn to_structure(name: Atom, arity: usize, r: RegType) -> Instruction;
fn to_void(num_subterms: usize) -> Self;
fn is_void_instr(&self) -> bool;
fn to_void(num_subterms: usize) -> Instruction;
fn is_void_instr(instr: &Instruction) -> bool;
fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Self;
fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Instruction;
fn incr_void_instr(&mut self);
fn incr_void_instr(instr: &mut Instruction);
fn constant_subterm(literal: Literal) -> Self;
fn constant_subterm(literal: Literal) -> Instruction;
fn argument_to_variable(r: RegType, r: usize) -> Self;
fn argument_to_value(r: RegType, val: usize) -> Self;
fn argument_to_variable(r: RegType, r: usize) -> Instruction;
fn argument_to_value(r: RegType, val: usize) -> Instruction;
fn move_to_register(r: RegType, val: usize) -> Self;
fn move_to_register(r: RegType, val: usize) -> Instruction;
fn subterm_to_variable(r: RegType) -> Self;
fn subterm_to_value(r: RegType) -> Self;
fn subterm_to_variable(r: RegType) -> Instruction;
fn subterm_to_value(r: RegType) -> Instruction;
fn clause_arg_to_instr(r: RegType) -> Self;
fn clause_arg_to_instr(r: RegType) -> Instruction;
}
impl<'a> CompilationTarget<'a> for FactInstruction {
@@ -43,66 +45,66 @@ impl<'a> CompilationTarget<'a> for FactInstruction {
breadth_first_iter(term, false) // do not iterate over the root clause if one exists.
}
fn to_constant(lvl: Level, constant: Literal, reg: RegType) -> Self {
FactInstruction::GetConstant(lvl, HeapCellValue::from(constant), reg)
fn to_constant(lvl: Level, constant: Literal, reg: RegType) -> Instruction {
Instruction::GetConstant(lvl, HeapCellValue::from(constant), reg)
}
fn to_structure(ct: ClauseType, arity: usize, reg: RegType) -> Self {
FactInstruction::GetStructure(ct, arity, reg)
fn to_structure(name: Atom, arity: usize, reg: RegType) -> Instruction {
Instruction::GetStructure(name, arity, reg)
}
fn to_list(lvl: Level, reg: RegType) -> Self {
FactInstruction::GetList(lvl, reg)
fn to_list(lvl: Level, reg: RegType) -> Instruction {
Instruction::GetList(lvl, reg)
}
fn to_void(num_subterms: usize) -> Self {
FactInstruction::UnifyVoid(num_subterms)
fn to_void(num_subterms: usize) -> Instruction {
Instruction::UnifyVoid(num_subterms)
}
fn is_void_instr(&self) -> bool {
match self {
&FactInstruction::UnifyVoid(_) => true,
fn is_void_instr(instr: &Instruction) -> bool {
match instr {
&Instruction::UnifyVoid(_) => true,
_ => false,
}
}
fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Self {
FactInstruction::GetPartialString(lvl, string, r, has_tail)
fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Instruction {
Instruction::GetPartialString(lvl, string, r, has_tail)
}
fn incr_void_instr(&mut self) {
match self {
&mut FactInstruction::UnifyVoid(ref mut incr) => *incr += 1,
fn incr_void_instr(instr: &mut Instruction) {
match instr {
&mut Instruction::UnifyVoid(ref mut incr) => *incr += 1,
_ => {}
}
}
fn constant_subterm(constant: Literal) -> Self {
FactInstruction::UnifyConstant(HeapCellValue::from(constant))
fn constant_subterm(constant: Literal) -> Instruction {
Instruction::UnifyConstant(HeapCellValue::from(constant))
}
fn argument_to_variable(arg: RegType, val: usize) -> Self {
FactInstruction::GetVariable(arg, val)
fn argument_to_variable(arg: RegType, val: usize) -> Instruction {
Instruction::GetVariable(arg, val)
}
fn move_to_register(arg: RegType, val: usize) -> Self {
FactInstruction::GetVariable(arg, val)
fn move_to_register(arg: RegType, val: usize) -> Instruction {
Instruction::GetVariable(arg, val)
}
fn argument_to_value(arg: RegType, val: usize) -> Self {
FactInstruction::GetValue(arg, val)
fn argument_to_value(arg: RegType, val: usize) -> Instruction {
Instruction::GetValue(arg, val)
}
fn subterm_to_variable(val: RegType) -> Self {
FactInstruction::UnifyVariable(val)
fn subterm_to_variable(val: RegType) -> Instruction {
Instruction::UnifyVariable(val)
}
fn subterm_to_value(val: RegType) -> Self {
FactInstruction::UnifyValue(val)
fn subterm_to_value(val: RegType) -> Instruction {
Instruction::UnifyValue(val)
}
fn clause_arg_to_instr(val: RegType) -> Self {
FactInstruction::UnifyVariable(val)
fn clause_arg_to_instr(val: RegType) -> Instruction {
Instruction::UnifyVariable(val)
}
}
@@ -113,65 +115,65 @@ impl<'a> CompilationTarget<'a> for QueryInstruction {
post_order_iter(term)
}
fn to_structure(ct: ClauseType, arity: usize, r: RegType) -> Self {
QueryInstruction::PutStructure(ct, arity, r)
fn to_structure(name: Atom, arity: usize, r: RegType) -> Instruction {
Instruction::PutStructure(name, arity, r)
}
fn to_constant(lvl: Level, constant: Literal, reg: RegType) -> Self {
QueryInstruction::PutConstant(lvl, HeapCellValue::from(constant), reg)
fn to_constant(lvl: Level, constant: Literal, reg: RegType) -> Instruction {
Instruction::PutConstant(lvl, HeapCellValue::from(constant), reg)
}
fn to_list(lvl: Level, reg: RegType) -> Self {
QueryInstruction::PutList(lvl, reg)
fn to_list(lvl: Level, reg: RegType) -> Instruction {
Instruction::PutList(lvl, reg)
}
fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Self {
QueryInstruction::PutPartialString(lvl, string, r, has_tail)
fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Instruction {
Instruction::PutPartialString(lvl, string, r, has_tail)
}
fn to_void(subterms: usize) -> Self {
QueryInstruction::SetVoid(subterms)
fn to_void(subterms: usize) -> Instruction {
Instruction::SetVoid(subterms)
}
fn is_void_instr(&self) -> bool {
match self {
&QueryInstruction::SetVoid(_) => true,
fn is_void_instr(instr: &Instruction) -> bool {
match instr {
&Instruction::SetVoid(_) => true,
_ => false,
}
}
fn incr_void_instr(&mut self) {
match self {
&mut QueryInstruction::SetVoid(ref mut incr) => *incr += 1,
fn incr_void_instr(instr: &mut Instruction) {
match instr {
&mut Instruction::SetVoid(ref mut incr) => *incr += 1,
_ => {}
}
}
fn constant_subterm(constant: Literal) -> Self {
QueryInstruction::SetConstant(HeapCellValue::from(constant))
fn constant_subterm(constant: Literal) -> Instruction {
Instruction::SetConstant(HeapCellValue::from(constant))
}
fn argument_to_variable(arg: RegType, val: usize) -> Self {
QueryInstruction::PutVariable(arg, val)
fn argument_to_variable(arg: RegType, val: usize) -> Instruction {
Instruction::PutVariable(arg, val)
}
fn move_to_register(arg: RegType, val: usize) -> Self {
QueryInstruction::GetVariable(arg, val)
fn move_to_register(arg: RegType, val: usize) -> Instruction {
Instruction::GetVariable(arg, val)
}
fn argument_to_value(arg: RegType, val: usize) -> Self {
QueryInstruction::PutValue(arg, val)
fn argument_to_value(arg: RegType, val: usize) -> Instruction {
Instruction::PutValue(arg, val)
}
fn subterm_to_variable(val: RegType) -> Self {
QueryInstruction::SetVariable(val)
fn subterm_to_variable(val: RegType) -> Instruction {
Instruction::SetVariable(val)
}
fn subterm_to_value(val: RegType) -> Self {
QueryInstruction::SetValue(val)
fn subterm_to_value(val: RegType) -> Instruction {
Instruction::SetValue(val)
}
fn clause_arg_to_instr(val: RegType) -> Self {
QueryInstruction::SetValue(val)
fn clause_arg_to_instr(val: RegType) -> Instruction {
Instruction::SetValue(val)
}
}