Skip to content

Commit ae239d8

Browse files
committed
use OffsetTableImpl without synchronization by default
1 parent 9ac2ce1 commit ae239d8

28 files changed

+975
-961
lines changed

build/instructions_template.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2698,7 +2698,7 @@ pub fn generate_instructions_rs() -> TokenStream {
26982698

26992699
if ident == "Named" {
27002700
clause_type_from_name_and_arity_arms.push(quote! {
2701-
(name, arity) => ClauseType::Named(arity, name, CodeIndex::default(arena))
2701+
(name, arity) => ClauseType::Named(arity, name, CodeIndex::default(&mut arena.code_index_tbl))
27022702
});
27032703

27042704
clause_type_to_instr_arms.push(quote! {

src/arena.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::ops::{Deref, DerefMut};
2121
use std::ptr;
2222
use std::ptr::addr_of_mut;
2323
use std::ptr::NonNull;
24-
use std::sync::Arc;
2524

2625
macro_rules! arena_alloc {
2726
($e:expr, $arena:expr) => {{
@@ -32,8 +31,7 @@ macro_rules! arena_alloc {
3231

3332
macro_rules! float_alloc {
3433
($e:expr, $arena:expr) => {{
35-
let result = $e;
36-
unsafe { $arena.f64_tbl.build_with(OrderedFloat(result)).as_ptr() }
34+
$arena.f64_tbl.build_with(OrderedFloat($e))
3735
}};
3836
}
3937

@@ -458,8 +456,8 @@ impl Drop for UntypedArenaSlab {
458456
#[derive(Debug)]
459457
pub struct Arena {
460458
base: Option<UntypedArenaSlab>,
461-
pub f64_tbl: Arc<F64Table>,
462-
pub code_index_tbl: Arc<CodeIndexTable>,
459+
pub f64_tbl: F64Table,
460+
pub code_index_tbl: CodeIndexTable,
463461
}
464462

465463
unsafe impl Send for Arena {}
@@ -584,23 +582,27 @@ mod tests {
584582

585583
#[test]
586584
fn float_ptr_cast() {
587-
let wam = MockWAM::new();
585+
let mut wam = MockWAM::new();
588586

589587
let f = 0f64;
590588
let fp = float_alloc!(f, wam.machine_st.arena);
591-
let mut cell = HeapCellValue::from(fp.clone());
589+
let mut cell = HeapCellValue::from(fp);
592590

593-
assert_eq!(cell.get_tag(), HeapCellValueTag::F64);
591+
assert_eq!(cell.get_tag(), HeapCellValueTag::F64Offset);
594592
assert!(!cell.get_mark_bit());
595-
assert_eq!(fp.deref(), &OrderedFloat(f));
593+
assert_eq!(
594+
wam.machine_st.arena.f64_tbl.lookup(fp).deref(),
595+
&OrderedFloat(f)
596+
);
596597

597598
cell.set_mark_bit(true);
598599

599600
assert!(cell.get_mark_bit());
600601

601602
read_heap_cell!(cell,
602-
(HeapCellValueTag::F64, ptr) => {
603-
assert_eq!(OrderedFloat(*ptr), OrderedFloat(f))
603+
(HeapCellValueTag::F64Offset, offset) => {
604+
let fp = wam.machine_st.arena.f64_tbl.lookup(offset.into());
605+
assert_eq!(*fp, OrderedFloat(0f64))
604606
}
605607
_ => { unreachable!() }
606608
);
@@ -825,7 +827,7 @@ mod tests {
825827
let float_ptr = float_alloc!(float, wam.machine_st.arena);
826828
let cell = HeapCellValue::from(float_ptr);
827829

828-
assert_eq!(cell.get_tag(), HeapCellValueTag::F64);
830+
assert_eq!(cell.get_tag(), HeapCellValueTag::F64Offset);
829831

830832
// char
831833

src/arithmetic.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
138138
#[derive(Debug)]
139139
pub(crate) struct ArithmeticEvaluator<'a> {
140140
marker: &'a mut DebrayAllocator,
141+
f64_tbl: &'a F64Table,
141142
interm: Vec<ArithmeticTerm>,
142143
interm_c: usize,
143144
}
@@ -156,11 +157,18 @@ impl<'a> ArithmeticTermIter<'a> for &'a Term {
156157
}
157158
}
158159

159-
fn push_literal(interm: &mut Vec<ArithmeticTerm>, c: &Literal) -> Result<(), ArithmeticError> {
160+
fn push_literal(
161+
f64_tbl: &F64Table,
162+
interm: &mut Vec<ArithmeticTerm>,
163+
c: &Literal,
164+
) -> Result<(), ArithmeticError> {
160165
match c {
161166
Literal::Fixnum(n) => interm.push(ArithmeticTerm::Number(Number::Fixnum(*n))),
162167
Literal::Integer(n) => interm.push(ArithmeticTerm::Number(Number::Integer(*n))),
163-
Literal::Float(n) => interm.push(ArithmeticTerm::Number(Number::Float(*n.as_ptr()))),
168+
&Literal::F64Offset(offset) => {
169+
let n = *f64_tbl.lookup(offset);
170+
interm.push(ArithmeticTerm::Number(Number::Float(n)));
171+
}
164172
Literal::Rational(n) => interm.push(ArithmeticTerm::Number(Number::Rational(*n))),
165173
Literal::Atom(name) if name == &atom!("e") => interm.push(ArithmeticTerm::Number(
166174
Number::Float(OrderedFloat(std::f64::consts::E)),
@@ -178,9 +186,14 @@ fn push_literal(interm: &mut Vec<ArithmeticTerm>, c: &Literal) -> Result<(), Ari
178186
}
179187

180188
impl<'a> ArithmeticEvaluator<'a> {
181-
pub(crate) fn new(marker: &'a mut DebrayAllocator, target_int: usize) -> Self {
189+
pub(crate) fn new(
190+
marker: &'a mut DebrayAllocator,
191+
f64_tbl: &'a F64Table,
192+
target_int: usize,
193+
) -> Self {
182194
ArithmeticEvaluator {
183195
marker,
196+
f64_tbl,
184197
interm: Vec::new(),
185198
interm_c: target_int,
186199
}
@@ -318,7 +331,7 @@ impl<'a> ArithmeticEvaluator<'a> {
318331

319332
for term_ref in src.iter()? {
320333
match term_ref? {
321-
ArithTermRef::Literal(c) => push_literal(&mut self.interm, &c)?,
334+
ArithTermRef::Literal(c) => push_literal(self.f64_tbl, &mut self.interm, &c)?,
322335
ArithTermRef::Var(lvl, cell, name) => {
323336
let var_num = name.to_var_num().unwrap();
324337

@@ -652,11 +665,11 @@ impl Ord for Number {
652665
}
653666
}
654667

655-
impl TryFrom<HeapCellValue> for Number {
668+
impl TryFrom<(HeapCellValue, &'_ F64Table)> for Number {
656669
type Error = ();
657670

658671
#[inline]
659-
fn try_from(value: HeapCellValue) -> Result<Number, Self::Error> {
672+
fn try_from((value, f64_tbl): (HeapCellValue, &F64Table)) -> Result<Number, Self::Error> {
660673
read_heap_cell!(value,
661674
(HeapCellValueTag::Cons, c) => {
662675
match_untyped_arena_ptr!(c,
@@ -671,8 +684,9 @@ impl TryFrom<HeapCellValue> for Number {
671684
}
672685
)
673686
}
674-
(HeapCellValueTag::F64, n) => {
675-
Ok(Number::Float(*n))
687+
(HeapCellValueTag::F64Offset, offset) => {
688+
let n = *f64_tbl.lookup(offset.into());
689+
Ok(Number::Float(n))
676690
}
677691
(HeapCellValueTag::Fixnum | HeapCellValueTag::CutPoint, n) => {
678692
Ok(Number::Fixnum(n))

src/codegen.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::forms::*;
66
use crate::indexing::*;
77
use crate::instructions::*;
88
use crate::iterators::*;
9+
use crate::offset_table::F64Table;
910
use crate::parser::ast::*;
1011
use crate::targets::*;
1112
use crate::types::*;
@@ -269,9 +270,10 @@ impl CodeGenSettings {
269270
}
270271

271272
#[derive(Debug)]
272-
pub(crate) struct CodeGenerator {
273+
pub(crate) struct CodeGenerator<'f64_tbl> {
273274
marker: DebrayAllocator,
274275
settings: CodeGenSettings,
276+
f64_tbl: &'f64_tbl F64Table,
275277
pub(crate) skeleton: PredicateSkeleton,
276278
}
277279

@@ -323,15 +325,15 @@ trait AddToFreeList<'a, Target: CompilationTarget<'a>> {
323325
fn add_subterm_to_free_list(&mut self, term: &Term);
324326
}
325327

326-
impl<'a> AddToFreeList<'a, FactInstruction> for CodeGenerator {
328+
impl<'a> AddToFreeList<'a, FactInstruction> for CodeGenerator<'_> {
327329
fn add_term_to_free_list(&mut self, r: RegType) {
328330
self.marker.add_reg_to_free_list(r);
329331
}
330332

331333
fn add_subterm_to_free_list(&mut self, _term: &Term) {}
332334
}
333335

334-
impl<'a> AddToFreeList<'a, QueryInstruction> for CodeGenerator {
336+
impl<'a> AddToFreeList<'a, QueryInstruction> for CodeGenerator<'_> {
335337
#[inline(always)]
336338
fn add_term_to_free_list(&mut self, _r: RegType) {}
337339

@@ -353,11 +355,12 @@ fn structure_cell(term: &Term) -> Option<&Cell<RegType>> {
353355
}
354356
}
355357

356-
impl CodeGenerator {
357-
pub(crate) fn new(settings: CodeGenSettings) -> Self {
358+
impl<'f64_tbl> CodeGenerator<'f64_tbl> {
359+
pub(crate) fn new(f64_tbl: &'f64_tbl F64Table, settings: CodeGenSettings) -> Self {
358360
CodeGenerator {
359361
marker: DebrayAllocator::new(),
360362
settings,
363+
f64_tbl,
361364
skeleton: PredicateSkeleton::new(),
362365
}
363366
}
@@ -427,7 +430,7 @@ impl CodeGenerator {
427430
where
428431
Target: crate::targets::CompilationTarget<'a>,
429432
Iter: Iterator<Item = TermRef<'a>>,
430-
CodeGenerator: AddToFreeList<'a, Target>,
433+
CodeGenerator<'f64_tbl>: AddToFreeList<'a, Target>,
431434
{
432435
let mut target = CodeDeque::new();
433436

@@ -443,7 +446,7 @@ impl CodeGenerator {
443446
}
444447
TermRef::Clause(lvl, cell, name, terms) => {
445448
let terms_range =
446-
if let Some(subterm @ Term::Literal(_, Literal::CodeIndex(_))) =
449+
if let Some(subterm @ Term::Literal(_, Literal::CodeIndexOffset(_))) =
447450
terms.last()
448451
{
449452
self.subterm_to_instr::<Target>(subterm, context, &mut target);
@@ -666,7 +669,7 @@ impl CodeGenerator {
666669
}
667670
},
668671
InlinedClauseType::IsFloat(..) => match terms[0] {
669-
Term::Literal(_, Literal::Float(_)) => {
672+
Term::Literal(_, Literal::F64Offset(_)) => {
670673
instr!("$succeed")
671674
}
672675
Term::Var(ref vr, ref name) => {
@@ -687,7 +690,7 @@ impl CodeGenerator {
687690
}
688691
},
689692
InlinedClauseType::IsNumber(..) => match terms[0] {
690-
Term::Literal(_, Literal::Float(_))
693+
Term::Literal(_, Literal::F64Offset(_))
691694
| Term::Literal(_, Literal::Rational(_))
692695
| Term::Literal(_, Literal::Integer(_))
693696
| Term::Literal(_, Literal::Fixnum(_)) => {
@@ -791,7 +794,7 @@ impl CodeGenerator {
791794
term_loc: GenContext,
792795
arg: usize,
793796
) -> Result<ArithCont, ArithmeticError> {
794-
let mut evaluator = ArithmeticEvaluator::new(&mut self.marker, target_int);
797+
let mut evaluator = ArithmeticEvaluator::new(&mut self.marker, self.f64_tbl, target_int);
795798
evaluator.compile_is(term, term_loc, arg)
796799
}
797800

@@ -861,7 +864,7 @@ impl CodeGenerator {
861864
Term::Literal(
862865
_,
863866
c @ Literal::Integer(_)
864-
| c @ Literal::Float(_)
867+
| c @ Literal::F64Offset(_)
865868
| c @ Literal::Rational(_)
866869
| c @ Literal::Fixnum(_),
867870
) => {

src/forms.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::machine::disjuncts::VarData;
66
use crate::machine::loader::PredicateQueue;
77
use crate::machine::machine_errors::*;
88
use crate::machine::machine_indices::*;
9+
use crate::offset_table::OffsetTable;
910
use crate::parser::ast::*;
1011
use crate::parser::dashu::{Integer, Rational};
1112
use crate::parser::parser::CompositeOpDesc;
@@ -742,7 +743,7 @@ impl ArenaFrom<Number> for HeapCellValue {
742743
match value {
743744
Number::Fixnum(n) => fixnum_as_cell!(n),
744745
Number::Integer(n) => typed_arena_ptr_as_cell!(n),
745-
Number::Float(OrderedFloat(n)) => HeapCellValue::from(float_alloc!(n, arena)),
746+
Number::Float(n) => HeapCellValue::from(arena.f64_tbl.build_with(n)),
746747
Number::Rational(n) => typed_arena_ptr_as_cell!(n),
747748
}
748749
}

0 commit comments

Comments
 (0)