codegen fixes
This commit is contained in:
@@ -24,7 +24,7 @@ impl fmt::Display for MachineInstruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum IntTerm<'a> {
|
enum IntTerm<'a> {
|
||||||
FinishedClause(usize, &'a Atom, &'a Vec<Box<Term>>),
|
FinishedClause(usize, usize, &'a Atom, &'a Vec<Box<Term>>),
|
||||||
UnfinishedClause(usize, &'a Atom, &'a Vec<Box<Term>>),
|
UnfinishedClause(usize, &'a Atom, &'a Vec<Box<Term>>),
|
||||||
FinishedAtom(usize, &'a Atom)
|
FinishedAtom(usize, &'a Atom)
|
||||||
}
|
}
|
||||||
@@ -50,28 +50,33 @@ pub fn compile_query<'a>(t: &'a Term) -> Program
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut max_reg_used : usize = 1;
|
||||||
|
|
||||||
while let Some(int_term) = stack.pop() {
|
while let Some(int_term) = stack.pop() {
|
||||||
match int_term {
|
match int_term {
|
||||||
IntTerm::UnfinishedClause(r, atom, terms) => {
|
IntTerm::UnfinishedClause(r, atom, terms) => {
|
||||||
stack.push(IntTerm::FinishedClause(r, atom, terms));
|
stack.push(IntTerm::FinishedClause(r, max_reg_used, atom, terms));
|
||||||
|
|
||||||
let mut counter : usize = r + 1;
|
let mut counter : usize = max_reg_used; // r + 1;
|
||||||
|
|
||||||
for t in terms {
|
for t in terms {
|
||||||
if t.is_variable() && !variable_allocs.contains_key(t.name()) {
|
if t.is_variable() && !variable_allocs.contains_key(t.name()) {
|
||||||
|
counter += 1;
|
||||||
variable_allocs.insert(t.name(), (counter, false));
|
variable_allocs.insert(t.name(), (counter, false));
|
||||||
}
|
} else if !t.is_variable() {
|
||||||
|
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
counter = r + terms.len();
|
max_reg_used = counter;
|
||||||
|
|
||||||
for t in terms.iter().rev() {
|
for t in terms.iter().rev() {
|
||||||
let r = if t.is_variable() {
|
let r = if t.is_variable() {
|
||||||
variable_allocs.get(t.name()).unwrap().0
|
variable_allocs.get(t.name()).unwrap().0
|
||||||
} else {
|
} else {
|
||||||
counter
|
let oc = counter;
|
||||||
|
counter -= 1;
|
||||||
|
oc
|
||||||
};
|
};
|
||||||
|
|
||||||
match t.as_ref() {
|
match t.as_ref() {
|
||||||
@@ -81,16 +86,14 @@ pub fn compile_query<'a>(t: &'a Term) -> Program
|
|||||||
stack.push(IntTerm::UnfinishedClause(r, atom, terms)),
|
stack.push(IntTerm::UnfinishedClause(r, atom, terms)),
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
counter -= 1;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
IntTerm::FinishedAtom(r, atom) =>
|
IntTerm::FinishedAtom(r, atom) =>
|
||||||
query.push(MachineInstruction::PutStructure(atom.clone(), 0, r)),
|
query.push(MachineInstruction::PutStructure(atom.clone(), 0, r)),
|
||||||
IntTerm::FinishedClause(r, atom, terms) => {
|
IntTerm::FinishedClause(r, mr, atom, terms) => {
|
||||||
query.push(MachineInstruction::PutStructure(atom.clone(), terms.len(), r));
|
query.push(MachineInstruction::PutStructure(atom.clone(), terms.len(), r));
|
||||||
|
|
||||||
let mut counter : usize = r + 1;
|
let mut counter : usize = mr + 1;
|
||||||
|
|
||||||
for t in terms {
|
for t in terms {
|
||||||
if let &Term::Var(ref var) = t.as_ref() {
|
if let &Term::Var(ref var) = t.as_ref() {
|
||||||
@@ -102,12 +105,17 @@ pub fn compile_query<'a>(t: &'a Term) -> Program
|
|||||||
} else {
|
} else {
|
||||||
query.push(MachineInstruction::SetValue(reg));
|
query.push(MachineInstruction::SetValue(reg));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
query.push(MachineInstruction::SetValue(counter));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if reg == counter {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
query.push(MachineInstruction::SetValue(counter));
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
max_reg_used = counter - 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,7 +163,6 @@ impl MachineState {
|
|||||||
self.h += 1;
|
self.h += 1;
|
||||||
},
|
},
|
||||||
MachineInstruction::UnifyVariable(reg) => {
|
MachineInstruction::UnifyVariable(reg) => {
|
||||||
if self.s < self.h {
|
|
||||||
match self.mode {
|
match self.mode {
|
||||||
MachineMode::Read => self.registers[reg] = self.heap[self.s].clone(),
|
MachineMode::Read => self.registers[reg] = self.heap[self.s].clone(),
|
||||||
MachineMode::Write => {
|
MachineMode::Write => {
|
||||||
@@ -172,14 +171,8 @@ impl MachineState {
|
|||||||
self.h += 1;
|
self.h += 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.s += 1;
|
|
||||||
} else {
|
|
||||||
self.fail = true;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
MachineInstruction::UnifyValue(reg) => {
|
MachineInstruction::UnifyValue(reg) => {
|
||||||
if self.s < self.h {
|
|
||||||
let s = self.s;
|
let s = self.s;
|
||||||
|
|
||||||
match self.mode {
|
match self.mode {
|
||||||
@@ -191,9 +184,6 @@ impl MachineState {
|
|||||||
};
|
};
|
||||||
|
|
||||||
self.s += 1;
|
self.s += 1;
|
||||||
} else {
|
|
||||||
self.fail = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use l0::machine::{MachineState};
|
|||||||
|
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
fn print_instructions(program : Program) {
|
fn print_instructions(program : &Program) {
|
||||||
for instruction in program {
|
for instruction in program {
|
||||||
println!("{:}", instruction);
|
println!("{:}", instruction);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user