From 2e3f99d89a5e67da82af3674f351f40a6aa53f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Santos?= Date: Tue, 17 Dec 2013 15:57:24 +0000 Subject: [PATCH 1/8] fix YAPOR_THREADS (prof. Vitor Costa) --- C/c_interface.c | 4 ++++ C/grow.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++--- C/threads.c | 2 +- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index c4c922dfa..4503d8dd0 100644 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -3172,7 +3172,11 @@ YAP_Init(YAP_init_args *yap_init) #endif /* YAPOR || TABLING */ #ifdef YAPOR Yap_init_yapor_workers(); +#if YAPOR_THREADS + if (Yap_thread_self() != 0) { +#else if (worker_id != 0) { +#endif #if defined(YAPOR_COPY) || defined(YAPOR_SBA) /* In the SBA we cannot just happily inherit registers diff --git a/C/grow.c b/C/grow.c index 30e828b64..4b39362f5 100755 --- a/C/grow.c +++ b/C/grow.c @@ -147,6 +147,16 @@ SetHeapRegs(int copying_threads USES_REGS) LOCAL_OpenArray = PtoGloAdjust(LOCAL_OpenArray); if (B) B = ChoicePtrAdjust(B); +#ifdef YAPOR_THREADS + { + choiceptr cpt; + cpt = Get_LOCAL_top_cp(); + if (cpt) { + // cpt = ChoicePtrAdjust( cpt ); + Set_LOCAL_top_cp( cpt ); + } + } +#endif #ifdef TABLING if (B_FZ) B_FZ = ChoicePtrAdjust(B_FZ); @@ -1899,11 +1909,59 @@ Yap_CopyThreadStacks(int worker_q, int worker_p, int incremental) /* make sure both stacks have same size */ Int p_size = REMOTE_ThreadHandle(worker_p).ssize+REMOTE_ThreadHandle(worker_p).tsize; Int q_size = REMOTE_ThreadHandle(worker_q).ssize+REMOTE_ThreadHandle(worker_q).tsize; - if (p_size != q_size) { - if (!(REMOTE_ThreadHandle(worker_q).stack_address = realloc(REMOTE_ThreadHandle(worker_q).stack_address,p_size*K1))) { - exit(1); + if (p_size != q_size) { + UInt start_growth_time, growth_time; + int gc_verbose; + size_t ssiz = REMOTE_ThreadHandle(worker_q).ssize*K1; + size_t tsiz = REMOTE_ThreadHandle(worker_q).tsize*K1; + size_t diff = (REMOTE_ThreadHandle(worker_p).ssize-REMOTE_ThreadHandle(worker_q).ssize)*K1; + char *oldq = (char *)REMOTE_ThreadHandle(worker_q).stack_address, *newq; + + if (!(newq = REMOTE_ThreadHandle(worker_q).stack_address = realloc(REMOTE_ThreadHandle(worker_q).stack_address,p_size*K1))) { + Yap_Error(OUT_OF_STACK_ERROR,TermNil,"cannot expand slave thread to match master thread"); + } + start_growth_time = Yap_cputime(); + gc_verbose = Yap_is_gc_verbose(); + LOCAL_stack_overflows++; + if (gc_verbose) { +#if defined(YAPOR) || defined(THREADS) + fprintf(GLOBAL_stderr, "%% Worker Id %d:\n", worker_id); +#endif + fprintf(GLOBAL_stderr, "%% Stack Overflow %d\n", LOCAL_stack_overflows); + fprintf(GLOBAL_stderr, "%% Stack: %8ld cells (%p-%p)\n", (unsigned long int)(LCL0-(CELL *)LOCAL_GlobalBase),LOCAL_GlobalBase,LCL0); + fprintf(GLOBAL_stderr, "%% Trail:%8ld cells (%p-%p)\n", + (unsigned long int)(TR-(tr_fr_ptr)LOCAL_TrailBase),LOCAL_TrailBase,TR); + fprintf(GLOBAL_stderr, "%% Growing the stacks %ld bytes\n", diff); + } + LOCAL_GDiff = LOCAL_GDiff0 = LOCAL_DelayDiff = LOCAL_BaseDiff = (newq-oldq); + LOCAL_TrDiff = LOCAL_LDiff = diff + LOCAL_GDiff; + LOCAL_XDiff = LOCAL_HDiff = 0; + LOCAL_GSplit = NULL; + YAPEnterCriticalSection(); + SetHeapRegs(FALSE PASS_REGS); + { + choiceptr imageB; + + LOCAL_OldLCL0 = LCL0; + LCL0 = REMOTE_ThreadHandle(0).current_yaam_regs->LCL0_; + imageB = Get_GLOBAL_root_cp(); + /* we know B */ + B->cp_tr = TR = + (tr_fr_ptr)((CELL)(imageB->cp_tr)+((CELL)LOCAL_OldLCL0-(CELL)LCL0)); + LCL0 = LOCAL_OldLCL0; + B->cp_h = H0; + B->cp_ap = GETWORK; + B->cp_or_fr = GLOBAL_root_or_fr; + } + YAPLeaveCriticalSection(); + growth_time = Yap_cputime()-start_growth_time; + LOCAL_total_stack_overflow_time += growth_time; + if (gc_verbose) { + fprintf(GLOBAL_stderr, "%% took %g sec\n", (double)growth_time/1000); + fprintf(GLOBAL_stderr, "%% Total of %g sec expanding stacks \n", (double)LOCAL_total_stack_overflow_time/1000); } } + REMOTE_ThreadHandle(worker_q).ssize = REMOTE_ThreadHandle(worker_p).ssize; REMOTE_ThreadHandle(worker_q).tsize = REMOTE_ThreadHandle(worker_p).tsize; /* compute offset indicators */ diff --git a/C/threads.c b/C/threads.c index 051e0ef6c..435c4501e 100755 --- a/C/threads.c +++ b/C/threads.c @@ -127,7 +127,7 @@ store_specs(int new_worker_id, UInt ssize, UInt tsize, UInt sysize, Term *tpgoal REMOTE_c_output_stream(new_worker_id) = REMOTE_c_output_stream(0); REMOTE_c_error_stream(new_worker_id) = REMOTE_c_error_stream(0); } - pm = (ssize + tsize)*1024; + pm = (ssize + tsize)*K1; if (!(REMOTE_ThreadHandle(new_worker_id).stack_address = malloc(pm))) { return FALSE; } From 35a332f23aff13cb31c68dc75cfafea1d3d20e44 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Tue, 17 Dec 2013 16:55:39 +0000 Subject: [PATCH 2/8] rational tries support for yap tabling --- OPTYap/opt.config.h | 4 + OPTYap/tab.rational.i | 77 +++++ OPTYap/tab.tries.c | 25 ++ OPTYap/tab.tries.i | 654 +++++++++++++++++++++++++++++------------- 4 files changed, 553 insertions(+), 207 deletions(-) create mode 100644 OPTYap/tab.rational.i diff --git a/OPTYap/opt.config.h b/OPTYap/opt.config.h index d1868dc7f..3605ad678 100644 --- a/OPTYap/opt.config.h +++ b/OPTYap/opt.config.h @@ -148,6 +148,10 @@ **************************************************/ /* #define OUTPUT_THREADS_TABLING 1 */ +/********************************************************* +** support rational terms ? (optional) ** +*********************************************************/ +#define TRIE_RATIONAL_TERMS 1 diff --git a/OPTYap/tab.rational.i b/OPTYap/tab.rational.i new file mode 100644 index 000000000..907137865 --- /dev/null +++ b/OPTYap/tab.rational.i @@ -0,0 +1,77 @@ +/************************************************************************ +** ** +** The YapTab/YapOr/OPTYap systems ** +** ** +** YapTab extends the Yap Prolog engine to support sequential tabling ** +** YapOr extends the Yap Prolog engine to support or-parallelism ** +** OPTYap extends the Yap Prolog engine to support or-parallel tabling ** +** ** +** ** +** Yap Prolog was developed at University of Porto, Portugal ** +** ** +************************************************************************/ + +#define RationalMark 7 //0m0...111 +#define IsRationalTerm(TERM) ((int) TERM == 7) + +typedef struct term_array { + void* *terms; + void* *nodes; + size_t length; + size_t capacity; +} term_array; + +void term_array_init(term_array *array, int capacity); +void term_array_free(term_array *array); +void term_array_push(term_array *array, void* t, void* n); +void* term_array_member(term_array array, void* t); + +void term_array_init(term_array *array, int capacity) { + array->length = 0; + array->terms = malloc(capacity * sizeof(void*)); + if (array->terms != NULL) { + array->capacity = capacity; + } else + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory + array->capacity = capacity; + array->nodes = malloc(capacity * sizeof(void*)); + if (array->nodes == NULL) + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory +} + +void term_array_free(term_array *array) { + free(array->terms); + free(array->nodes); + array->terms = NULL; + array->nodes = NULL; + array->length = 0; + array->capacity = 0; +} + +void term_array_push(term_array *array, void* t, void* n) { + if (array->length == array->capacity) { + int new_capacity = array->capacity * 2; + void *new_terms = realloc(array->terms, new_capacity * sizeof(void*)); + if (new_terms != NULL) { + array->terms = new_terms; + } else + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory + void *new_nodes = realloc(array->nodes, new_capacity * sizeof(void *)); + if (new_nodes != NULL) { + array->nodes = new_nodes; + } else + Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory + array->capacity = new_capacity; + } + array->terms[array->length] = t; + array->nodes[array->length] = n; + array->length++; +} + +void* term_array_member(term_array array, void* t) { + int i; + for (i = 0; i < array.length; i++) + if (array.terms[i] == t) return array.nodes[i]; + return NULL; +} + diff --git a/OPTYap/tab.tries.c b/OPTYap/tab.tries.c index d42a99b6f..82aecebc9 100644 --- a/OPTYap/tab.tries.c +++ b/OPTYap/tab.tries.c @@ -163,6 +163,12 @@ static struct trie_statistics{ free_global_trie_branch(NODE PASS_REGS) #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ +/****************************** +** Rational Terms Support ** +******************************/ +#ifdef TRIE_RATIONAL_TERMS +#include "tab.rational.i" +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ /****************************** @@ -200,7 +206,13 @@ static struct trie_statistics{ #define MODE_TERMS_LOOP #define INCLUDE_SUBGOAL_SEARCH_LOOP /* subgoal_search_terms_loop */ #define INCLUDE_ANSWER_SEARCH_LOOP /* answer_search_terms_loop */ +#ifdef TRIE_RATIONAL_TERMS +#undef TRIE_RATIONAL_TERMS #include "tab.tries.i" +#define TRIE_RATIONAL_TERMS +#else +#include "tab.tries.i" +#endif #undef INCLUDE_ANSWER_SEARCH_LOOP #undef INCLUDE_SUBGOAL_SEARCH_LOOP #undef MODE_TERMS_LOOP @@ -209,7 +221,13 @@ static struct trie_statistics{ #define INCLUDE_SUBGOAL_SEARCH_LOOP /* subgoal_search_global_trie_(terms)_loop */ #define INCLUDE_ANSWER_SEARCH_LOOP /* answer_search_global_trie_(terms)_loop */ #define INCLUDE_LOAD_ANSWER_LOOP /* load_substitution_loop */ +#ifdef TRIE_RATIONAL_TERMS +#undef TRIE_RATIONAL_TERMS #include "tab.tries.i" +#define TRIE_RATIONAL_TERMS +#else +#include "tab.tries.i" +#endif #undef INCLUDE_LOAD_ANSWER_LOOP #undef INCLUDE_ANSWER_SEARCH_LOOP #undef INCLUDE_SUBGOAL_SEARCH_LOOP @@ -899,6 +917,13 @@ static inline void traverse_trie_node(Term t, char *str, int *str_index_ptr, int } else if (mode == TRAVERSE_MODE_LONGINT_END) { mode = TRAVERSE_MODE_NORMAL; } else if (IsVarTerm(t)) { +#ifdef TRIE_RATIONAL_TERMS + if (t > VarIndexOfTableTerm(MAX_TABLE_VARS) && TrNode_child((gt_node_ptr) t) != 1) { //TODO: substitute the != 1 test to something more appropriate + /* Rational term */ + str_index += sprintf(& str[str_index], "**"); + traverse_update_arity(str, &str_index, arity); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { TrStat_gt_refs++; /* (type % 2 + 2): TRAVERSE_TYPE_ANSWER --> TRAVERSE_TYPE_GT_ANSWER */ diff --git a/OPTYap/tab.tries.i b/OPTYap/tab.tries.i index a02984888..0260e7e55 100644 --- a/OPTYap/tab.tries.i +++ b/OPTYap/tab.tries.i @@ -18,8 +18,8 @@ #ifdef MODE_GLOBAL_TRIE_ENTRY #define INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY) \ { register gt_node_ptr entry_node = (gt_node_ptr) (ENTRY); \ - TrNode_child(entry_node) = (gt_node_ptr) ((unsigned long int) TrNode_child(entry_node) + 1); \ - } + TrNode_child(entry_node) = (gt_node_ptr) ((unsigned long int) TrNode_child(entry_node) + 1); \ + } #define NEW_SUBGOAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) @@ -47,7 +47,7 @@ #else #define SUBGOAL_CHECK_INSERT_ENTRY(TAB_ENT, NODE, ENTRY) \ NODE = subgoal_trie_check_insert_entry(TAB_ENT, NODE, ENTRY PASS_REGS) -#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ +#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ NODE = answer_trie_check_insert_entry(SG_FR, NODE, ENTRY, INSTR PASS_REGS) #endif /* MODE_GLOBAL_TRIE_LOOP */ @@ -56,9 +56,9 @@ #define ANSWER_SAFE_INSERT_ENTRY(NODE, ENTRY, INSTR) \ { ans_node_ptr new_node; \ NEW_ANSWER_TRIE_NODE(new_node, INSTR, ENTRY, NULL, NODE, NULL); \ - TrNode_child(NODE) = new_node; \ + TrNode_child(NODE) = new_node; \ NODE = new_node; \ - } + } #ifdef THREADS #define INVALIDATE_ANSWER_TRIE_NODE(NODE, SG_FR) \ TrNode_next(NODE) = SgFr_invalid_chain(SG_FR); \ @@ -1048,19 +1048,26 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c goto subgoal_search_loop_non_atomic; #endif /* MODE_GLOBAL_TRIE_LOOP */ +#ifdef TRIE_RATIONAL_TERMS + /* Needed structures, variables to support rational terms */ + term_array Ts; + void* CyclicTerm; + term_array_init(&Ts, 10); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + do { if (IsVarTerm(t)) { if (IsTableVarTerm(t)) { - t = MakeTableVarTerm(VarIndexOfTerm(t)); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); + t = MakeTableVarTerm(VarIndexOfTerm(t)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); } else { - if (subs_arity == MAX_TABLE_VARS) - Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: MAX_TABLE_VARS exceeded"); - STACK_PUSH_UP(t, stack_vars); - *((CELL *)t) = GLOBAL_table_var_enumerator(subs_arity); - t = MakeTableVarTerm(subs_arity); - subs_arity = subs_arity + 1; - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); + if (subs_arity == MAX_TABLE_VARS) + Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: MAX_TABLE_VARS exceeded"); + STACK_PUSH_UP(t, stack_vars); + *((CELL *)t) = GLOBAL_table_var_enumerator(subs_arity); + t = MakeTableVarTerm(subs_arity); + subs_arity = subs_arity + 1; + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); } } else if (IsAtomOrIntTerm(t)) { SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); @@ -1075,48 +1082,103 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c current_node = subgoal_trie_check_insert_gt_entry(tab_ent, current_node, (Term) entry_node PASS_REGS); #else /* ! MODE_TERMS_LOOP */ } else +#ifdef TRIE_RATIONAL_TERMS + if (IsRationalTerm(t)) { + t = STACK_POP_DOWN(stack_terms); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ #if defined(MODE_GLOBAL_TRIE_LOOP) /* for the global trie, it is safe to start here in the first iteration */ subgoal_search_loop_non_atomic: #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_COMPACT_PAIRS if (IsPairTerm(t)) { +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ CELL *aux_pair = RepPair(t); if (aux_pair == PairTermMark) { - t = STACK_POP_DOWN(stack_terms); - if (IsPairTerm(t)) { - aux_pair = RepPair(t); - t = Deref(aux_pair[1]); - if (t == TermNil) { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); - } else { - /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ - /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** - ** up 3 terms has already initially checked for the CompactPairInit term */ - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); - } else { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndTerm); - STACK_PUSH_UP(t, stack_terms); - } + t = STACK_POP_DOWN(stack_terms); +#ifdef TRIE_RATIONAL_TERMS + if (IsPairTerm(t) && ! IsRationalTerm(t)) { + term_array_push(&Ts, (void *) t, (void *) current_node); +#else + if (IsPairTerm(t)) { +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + aux_pair = RepPair(t); + t = Deref(aux_pair[1]); +#ifdef TRIE_RATIONAL_TERMS + if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { + CyclicTerm = term_array_member(Ts, (void *) t); + } + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + if (t == TermNil) { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); + } else { + /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ + /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** + ** up 3 terms has already initially checked for the CompactPairInit term */ + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + } +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; + if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) + CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + } else { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndTerm); + STACK_PUSH_UP(t, stack_terms); + } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ } else { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairInit); - t = Deref(aux_pair[1]); - if (t == TermNil) { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); - } else { - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); +#ifdef TRIE_RATIONAL_TERMS + term_array_push(&Ts, (void *) t, (void *) current_node); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairInit); + t = Deref(aux_pair[1]); +#ifdef TRIE_RATIONAL_TERMS + if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { + CyclicTerm = term_array_member(Ts, (void *) t); + } + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + if (t == TermNil) { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); + } else { + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + } +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; + if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) + CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { @@ -1140,37 +1202,50 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c } else if (IsApplTerm(t)) { Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { - union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; - } u; - u.dbl = FloatOfTerm(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + union { + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; + } u; + u.dbl = FloatOfTerm(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[1]); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[1]); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[0]); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[0]); #ifdef MODE_GLOBAL_TRIE_LOOP - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #endif /* MODE_GLOBAL_TRIE_LOOP */ } else if (f == FunctorLongInt) { - Int li = LongIntOfTerm(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, li); + Int li = LongIntOfTerm(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, li); #ifdef MODE_GLOBAL_TRIE_LOOP - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #endif /* MODE_GLOBAL_TRIE_LOOP */ } else if (f == FunctorDBRef) { - Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorDBRef"); + Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorDBRef"); } else if (f == FunctorBigInt) { - Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorBigInt"); + Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorBigInt"); } else { - int i; - CELL *aux_appl = RepAppl(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); - for (i = ArityOfFunctor(f); i >= 1; i--) - STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); +#ifdef TRIE_RATIONAL_TERMS + term_array_push(&Ts, (void *) t, (void *) current_node); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + int i; + CELL *aux_appl = RepAppl(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); + for (i = ArityOfFunctor(f); i >= 1; i--) { +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; + if (IsVarTerm(aux_appl[i]) || IsApplTerm(aux_appl[i])) + CyclicTerm = term_array_member(Ts, (void *) Deref(aux_appl[i])); + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); + } } } else { Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unknown type tag"); @@ -1178,7 +1253,9 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c } t = STACK_POP_DOWN(stack_terms); } while (t); - +#ifdef TRIE_RATIONAL_TERMS + term_array_free(&Ts); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ *subs_arity_ptr = subs_arity; *stack_vars_ptr = stack_vars; return current_node; @@ -1256,20 +1333,26 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr goto answer_search_loop_non_atomic; #endif /* MODE_GLOBAL_TRIE_LOOP */ +#ifdef TRIE_RATIONAL_TERMS + term_array Ts; + void* CyclicTerm; + term_array_init(&Ts, 10); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + do { if (IsVarTerm(t)) { t = Deref(t); if (IsTableVarTerm(t)) { - t = MakeTableVarTerm(VarIndexOfTerm(t)); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_val + in_pair); + t = MakeTableVarTerm(VarIndexOfTerm(t)); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_val + in_pair); } else { - if (vars_arity == MAX_TABLE_VARS) - Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: MAX_TABLE_VARS exceeded"); - stack_vars_base[vars_arity] = t; - *((CELL *)t) = GLOBAL_table_var_enumerator(vars_arity); - t = MakeTableVarTerm(vars_arity); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); - vars_arity = vars_arity + 1; + if (vars_arity == MAX_TABLE_VARS) + Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: MAX_TABLE_VARS exceeded"); + stack_vars_base[vars_arity] = t; + *((CELL *)t) = GLOBAL_table_var_enumerator(vars_arity); + t = MakeTableVarTerm(vars_arity); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); + vars_arity = vars_arity + 1; } #ifdef TRIE_COMPACT_PAIRS in_pair = 0; @@ -1289,52 +1372,109 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ current_node = answer_trie_check_insert_gt_entry(sg_fr, current_node, (Term) entry_node, _trie_retry_gterm + in_pair PASS_REGS); #else /* ! MODE_TERMS_LOOP */ - } else + } else +#ifdef TRIE_RATIONAL_TERMS + if (IsRationalTerm(t)) { + t = STACK_POP_DOWN(stack_terms); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); //TODO create _trie_.._rational + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ #if defined(MODE_GLOBAL_TRIE_LOOP) /* for the global trie, it is safe to start here in the first iteration */ answer_search_loop_non_atomic: #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_COMPACT_PAIRS if (IsPairTerm(t)) { +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ CELL *aux_pair = RepPair(t); if (aux_pair == PairTermMark) { - t = STACK_POP_DOWN(stack_terms); - if (IsPairTerm(t)) { - aux_pair = RepPair(t); - t = Deref(aux_pair[1]); - if (t == TermNil) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); - } else { - /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ - /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** - ** up 3 terms has already initially checked for the CompactPairInit term */ - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - in_pair = 4; - } - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); - } else { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndTerm, _trie_retry_null); - STACK_PUSH_UP(t, stack_terms); - } + t = STACK_POP_DOWN(stack_terms); +#ifdef TRIE_RATIONAL_TERMS + if (IsPairTerm(t) && ! IsRationalTerm(t)) { + term_array_push(&Ts, (void *) t, (void *) current_node); +#else + if (IsPairTerm(t)) { +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + aux_pair = RepPair(t); + t = Deref(aux_pair[1]); +#ifdef TRIE_RATIONAL_TERMS + if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { + CyclicTerm = term_array_member(Ts, (void *) t); + } + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); // CyclicTerm + STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + in_pair = 4; + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + if (t == TermNil) { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); + } else { + /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ + /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** + ** up 3 terms has already initially checked for the CompactPairInit term */ + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + in_pair = 4; + } +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; + if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) + CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + } else { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndTerm, _trie_retry_null); + STACK_PUSH_UP(t, stack_terms); + } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ } else { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairInit, _trie_retry_null + in_pair); - t = Deref(aux_pair[1]); - if (t == TermNil) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); - in_pair = 0; - } else { - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - in_pair = 4; - } - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); +#ifdef TRIE_RATIONAL_TERMS + term_array_push(&Ts, (void *) t, (void *) current_node); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairInit, _trie_retry_null + in_pair); + t = Deref(aux_pair[1]); +#ifdef TRIE_RATIONAL_TERMS + if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { + CyclicTerm = term_array_member(Ts, (void *) t); + } + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + in_pair = 4; + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + if (t == TermNil) { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); + in_pair = 0; + } else { + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + in_pair = 4; + } +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; + if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) + CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { @@ -1358,33 +1498,46 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr } else if (IsApplTerm(t)) { Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { - union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; - } u; - u.dbl = FloatOfTerm(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); + union { + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; + } u; + u.dbl = FloatOfTerm(t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[1], _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[1], _trie_retry_extension); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[0], _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_double); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[0], _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_double); } else if (f == FunctorLongInt) { - Int li = LongIntOfTerm (t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, li, _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_longint); + Int li = LongIntOfTerm (t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, li, _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_longint); } else if (f == FunctorDBRef) { - Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorDBRef"); + Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorDBRef"); } else if (f == FunctorBigInt) { - Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorBigInt"); + Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorBigInt"); } else { - int i; - CELL *aux_appl = RepAppl(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_appl + in_pair); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); - for (i = ArityOfFunctor(f); i >= 1; i--) - STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); +#ifdef TRIE_RATIONAL_TERMS + term_array_push(&Ts, (void *) t, (void *) current_node); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + int i; + CELL *aux_appl = RepAppl(t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_appl + in_pair); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); + for (i = ArityOfFunctor(f); i >= 1; i--) { +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = NULL; + if (IsVarTerm(aux_appl[i]) || IsApplTerm(aux_appl[i])) + CyclicTerm = term_array_member(Ts, (void *) Deref(aux_appl[i])); + if (CyclicTerm != NULL) { + STACK_PUSH_UP((Term) CyclicTerm, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); + } } #ifdef TRIE_COMPACT_PAIRS in_pair = 0; @@ -1395,7 +1548,9 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr } t = STACK_POP_DOWN(stack_terms); } while (t); - +#ifdef TRIE_RATIONAL_TERMS + term_array_free(&Ts); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ *vars_arity_ptr = vars_arity; return current_node; @@ -1430,8 +1585,8 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c trie_value = (Float) TrNode_entry(child_node); } else if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; } u; u.t_dbl[0] = TrNode_entry(child_node); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P @@ -1470,8 +1625,8 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; } u; u.dbl = FloatOfTerm(t); ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); @@ -1516,8 +1671,8 @@ static inline ans_node_ptr answer_search_sum(sg_fr_ptr sg_fr, ans_node_ptr curre trie_value = (Float) TrNode_entry(child_node); } else if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; } u; u.t_dbl[0] = TrNode_entry(child_node); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P @@ -1580,18 +1735,18 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i do { current_node = *bucket; if (current_node) { - ans_node_ptr next_node = TrNode_next(current_node); - if (IS_ANSWER_LEAF_NODE(current_node)) { - INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); - } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); - INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); - } - while (next_node) { - current_node = next_node; - next_node = TrNode_next(current_node); - invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); - } + ans_node_ptr next_node = TrNode_next(current_node); + if (IS_ANSWER_LEAF_NODE(current_node)) { + INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); + } else { + invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); + } + while (next_node) { + current_node = next_node; + next_node = TrNode_next(current_node); + invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); + } } } while (++bucket != last_bucket); if (Hash_next(hash)) @@ -1606,22 +1761,22 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i if (position == TRAVERSE_POSITION_FIRST) { ans_node_ptr next_node = TrNode_next(current_node); if (IS_ANSWER_LEAF_NODE(current_node)) { - INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); + INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); - INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } while (next_node) { - current_node = next_node; - next_node = TrNode_next(current_node); - invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); + current_node = next_node; + next_node = TrNode_next(current_node); + invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); } } else { if (IS_ANSWER_LEAF_NODE(current_node)) { - INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); + INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); - INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } } } @@ -1687,47 +1842,115 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { current_node = (ans_node_ptr) UNTAG_ANSWER_NODE(TrNode_parent(current_node)); #endif /* MODE_GLOBAL_TRIE_LOOP */ +#ifdef TRIE_RATIONAL_TERMS + term_array Ts; + void* CyclicTerm; + term_array_init(&Ts, 10); + Term RationalTermTMP; // a temporary temp to be used from the rational code +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + do { +#ifdef TRIE_RATIONAL_TERMS + CyclicTerm = term_array_member(Ts, (void *) current_node); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ if (IsVarTerm(t)) { +#ifdef TRIE_RATIONAL_TERMS + if (t > VarIndexOfTableTerm(MAX_TABLE_VARS) && TrNode_child((gt_node_ptr) t) != 1) { //TODO: substitute the != 1 test to something more appropriate + /* Rational term */ + RationalTermTMP = (Term) term_array_member(Ts, (void *) t); + if (RationalTermTMP) { + /* rational term is assigned a variable already */ + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); + STACK_PUSH_UP(RationalTermTMP, stack_terms); + } else { + RationalTermTMP = MkVarTerm(); + STACK_PUSH_UP(RationalTermTMP, stack_terms); + /* memorize the rational term and assign it a variable */ + term_array_push(&Ts, (void *) t, (void *) RationalTermTMP); + } + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + { #if ! defined(MODE_GLOBAL_TRIE_LOOP) || defined(GLOBAL_TRIE_FOR_SUBTERMS) - if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { - stack_terms = load_substitution_loop((gt_node_ptr) t, &vars_arity, stack_terms PASS_REGS); - } else + if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { + stack_terms = load_substitution_loop((gt_node_ptr) t, &vars_arity, stack_terms PASS_REGS); + } else #endif /* ! MODE_GLOBAL_TRIE_LOOP || GLOBAL_TRIE_FOR_SUBTERMS */ - { int var_index = VarIndexOfTableTerm(t); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit - vars_arity + var_index + 1); - if (var_index >= vars_arity) { - while (vars_arity < var_index) - stack_vars_base[vars_arity++] = 0; - stack_vars_base[vars_arity++] = MkVarTerm(); - } else if (stack_vars_base[var_index] == 0) - stack_vars_base[var_index] = MkVarTerm(); - STACK_PUSH_UP(stack_vars_base[var_index], stack_terms); + { int var_index = VarIndexOfTableTerm(t); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit - vars_arity + var_index + 1); + if (var_index >= vars_arity) { + while (vars_arity < var_index) + stack_vars_base[vars_arity++] = 0; + stack_vars_base[vars_arity++] = MkVarTerm(); + } else if (stack_vars_base[var_index] == 0) + stack_vars_base[var_index] = MkVarTerm(); + STACK_PUSH_UP(stack_vars_base[var_index], stack_terms); + } } } else if (IsAtomOrIntTerm(t)) { AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); +#ifdef TRIE_RATIONAL_TERMS + if (CyclicTerm) { + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 4); + STACK_PUSH_UP((Term) RationalMark, stack_terms); // Add a rational term marker necessary as we read both ways the stack // + STACK_PUSH_UP(t, stack_terms); // Add the term // + STACK_PUSH_UP(CyclicTerm, stack_terms); // Add the variable that the term will unify with // + STACK_PUSH_UP((Term) RationalMark, stack_terms); // Add a rational term marker necessary as we read both ways the stack // + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ STACK_PUSH_UP(t, stack_terms); } else if (IsPairTerm(t)) { #ifdef TRIE_COMPACT_PAIRS if (t == CompactPairInit) { - Term *stack_aux = stack_terms_base - stack_terms_pair_offset; - Term head, tail = STACK_POP_UP(stack_aux); - while (STACK_NOT_EMPTY(stack_aux, stack_terms)) { - head = STACK_POP_UP(stack_aux); - tail = MkPairTerm(head, tail); - } - stack_terms = stack_terms_base - stack_terms_pair_offset; - stack_terms_pair_offset = (int) STACK_POP_DOWN(stack_terms); - STACK_PUSH_UP(tail, stack_terms); + Term *stack_aux = stack_terms_base - stack_terms_pair_offset; + Term head, tail = STACK_POP_UP(stack_aux); +#ifdef TRIE_RATIONAL_TERMS + if (IsRationalTerm(tail)) { + Yap_Error(INTERNAL_ERROR, tail, "Rational element of a Rational Term appears as the first Tail of a list"); + } +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + while (STACK_NOT_EMPTY(stack_aux, stack_terms)) { + head = STACK_POP_UP(stack_aux); +#ifdef TRIE_RATIONAL_TERMS + if (IsRationalTerm(head)) { + head = STACK_POP_UP(stack_aux); // thats the rational term + RationalTermTMP = STACK_POP_UP(stack_aux); // that is the variable to unify with + (void) STACK_POP_UP(stack_aux); // eat the second rational mark + tail = MkPairTerm(head, tail); + Yap_unify(RationalTermTMP, tail); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + tail = MkPairTerm(head, tail); + } + stack_terms = stack_terms_base - stack_terms_pair_offset; + stack_terms_pair_offset = (int) STACK_POP_DOWN(stack_terms); + STACK_PUSH_UP(tail, stack_terms); } else { /* CompactPairEndList / CompactPairEndTerm */ - Term last; - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); - last = STACK_POP_DOWN(stack_terms); - STACK_PUSH_UP(stack_terms_pair_offset, stack_terms); - stack_terms_pair_offset = (int) (stack_terms_base - stack_terms); - if (t == CompactPairEndList) - STACK_PUSH_UP(TermNil, stack_terms); - STACK_PUSH_UP(last, stack_terms); + Term last; + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); + last = STACK_POP_DOWN(stack_terms); +#ifdef TRIE_RATIONAL_TERMS + RationalTermTMP = TermNil; + if (IsRationalTerm(last)) { // rather unlikely case the rational term is the last of a list + RationalTermTMP = STACK_POP_DOWN(stack_terms); // in this case we need to invert the term with the end of list + last = STACK_POP_DOWN(stack_terms); // variable to unify with + (void) STACK_POP_DOWN(stack_terms); // eat the second rational mark + } +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(stack_terms_pair_offset, stack_terms); + stack_terms_pair_offset = (int) (stack_terms_base - stack_terms); + if (t == CompactPairEndList) + STACK_PUSH_UP(TermNil, stack_terms); +#ifdef TRIE_RATIONAL_TERMS + if (RationalTermTMP && RationalTermTMP != TermNil) { + /* most probably this never occurs */ + STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP(last, stack_terms); + STACK_PUSH_UP(RationalTermTMP, stack_terms); + STACK_PUSH_UP((Term) RationalMark, stack_terms); + } else +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + STACK_PUSH_UP(last, stack_terms); } #else /* ! TRIE_COMPACT_PAIRS */ Term head = STACK_POP_DOWN(stack_terms); @@ -1738,37 +1961,54 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { } else if (IsApplTerm(t)) { Functor f = (Functor) RepAppl(t); if (f == FunctorDouble) { - union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; - } u; - t = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - u.t_dbl[0] = t; + union { + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; + } u; + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + u.t_dbl[0] = t; #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - t = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - u.t_dbl[1] = t; + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + u.t_dbl[1] = t; #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - current_node = TrNode_parent(current_node); - t = MkFloatTerm(u.dbl); + current_node = TrNode_parent(current_node); + t = MkFloatTerm(u.dbl); } else if (f == FunctorLongInt) { - Int li = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - current_node = TrNode_parent(current_node); - t = MkLongIntTerm(li); + Int li = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + current_node = TrNode_parent(current_node); + t = MkLongIntTerm(li); } else { - int f_arity = ArityOfFunctor(f); - t = Yap_MkApplTerm(f, f_arity, stack_terms); - stack_terms += f_arity; + int f_arity = ArityOfFunctor(f); + t = Yap_MkApplTerm(f, f_arity, stack_terms); + stack_terms += f_arity; } AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); STACK_PUSH_UP(t, stack_terms); } +#ifdef TRIE_RATIONAL_TERMS + if (CyclicTerm) { + RationalTermTMP = STACK_POP_DOWN(stack_terms); + if IsRationalTerm(RationalTermTMP) { + //printf("Special Case\n"); + } else if (IsPairTerm(RationalTermTMP)) { + Yap_unify((Term) CyclicTerm, RationalTermTMP); + } else if (IsApplTerm(RationalTermTMP)) { + Yap_unify((Term) CyclicTerm, RationalTermTMP); + } + STACK_PUSH_UP(RationalTermTMP, stack_terms); + } + RationalTermTMP = TermNil; + CyclicTerm = NULL; +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ t = TrNode_entry(current_node); current_node = TrNode_parent(current_node); } while (current_node); - +#ifdef TRIE_RATIONAL_TERMS + term_array_free(&Ts); +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ #ifdef MODE_GLOBAL_TRIE_LOOP *vars_arity_ptr = vars_arity; #endif /* MODE_GLOBAL_TRIE_LOOP */ From 5a8017156ad02b23ae71806a8e21008f1a5b8444 Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Tue, 17 Dec 2013 18:03:46 +0000 Subject: [PATCH 3/8] undid the last changes... --- OPTYap/tab.rational.i | 77 ------------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 OPTYap/tab.rational.i diff --git a/OPTYap/tab.rational.i b/OPTYap/tab.rational.i deleted file mode 100644 index 907137865..000000000 --- a/OPTYap/tab.rational.i +++ /dev/null @@ -1,77 +0,0 @@ -/************************************************************************ -** ** -** The YapTab/YapOr/OPTYap systems ** -** ** -** YapTab extends the Yap Prolog engine to support sequential tabling ** -** YapOr extends the Yap Prolog engine to support or-parallelism ** -** OPTYap extends the Yap Prolog engine to support or-parallel tabling ** -** ** -** ** -** Yap Prolog was developed at University of Porto, Portugal ** -** ** -************************************************************************/ - -#define RationalMark 7 //0m0...111 -#define IsRationalTerm(TERM) ((int) TERM == 7) - -typedef struct term_array { - void* *terms; - void* *nodes; - size_t length; - size_t capacity; -} term_array; - -void term_array_init(term_array *array, int capacity); -void term_array_free(term_array *array); -void term_array_push(term_array *array, void* t, void* n); -void* term_array_member(term_array array, void* t); - -void term_array_init(term_array *array, int capacity) { - array->length = 0; - array->terms = malloc(capacity * sizeof(void*)); - if (array->terms != NULL) { - array->capacity = capacity; - } else - Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory - array->capacity = capacity; - array->nodes = malloc(capacity * sizeof(void*)); - if (array->nodes == NULL) - Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory -} - -void term_array_free(term_array *array) { - free(array->terms); - free(array->nodes); - array->terms = NULL; - array->nodes = NULL; - array->length = 0; - array->capacity = 0; -} - -void term_array_push(term_array *array, void* t, void* n) { - if (array->length == array->capacity) { - int new_capacity = array->capacity * 2; - void *new_terms = realloc(array->terms, new_capacity * sizeof(void*)); - if (new_terms != NULL) { - array->terms = new_terms; - } else - Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory - void *new_nodes = realloc(array->nodes, new_capacity * sizeof(void *)); - if (new_nodes != NULL) { - array->nodes = new_nodes; - } else - Yap_Error(RESOURCE_ERROR_MEMORY, TermNil, "Out of memory."); // Handle out-of-memory - array->capacity = new_capacity; - } - array->terms[array->length] = t; - array->nodes[array->length] = n; - array->length++; -} - -void* term_array_member(term_array array, void* t) { - int i; - for (i = 0; i < array.length; i++) - if (array.terms[i] == t) return array.nodes[i]; - return NULL; -} - From 65eb3b8e4d5d5c292aca9d4ccf9ef832c597fe3d Mon Sep 17 00:00:00 2001 From: Theo Date: Tue, 17 Dec 2013 18:15:34 +0000 Subject: [PATCH 4/8] Still undoing... sorry --- OPTYap/opt.config.h | 4 - OPTYap/tab.tries.c | 25 -- OPTYap/tab.tries.i | 654 ++++++++++++++------------------------------ 3 files changed, 207 insertions(+), 476 deletions(-) diff --git a/OPTYap/opt.config.h b/OPTYap/opt.config.h index 3605ad678..d1868dc7f 100644 --- a/OPTYap/opt.config.h +++ b/OPTYap/opt.config.h @@ -148,10 +148,6 @@ **************************************************/ /* #define OUTPUT_THREADS_TABLING 1 */ -/********************************************************* -** support rational terms ? (optional) ** -*********************************************************/ -#define TRIE_RATIONAL_TERMS 1 diff --git a/OPTYap/tab.tries.c b/OPTYap/tab.tries.c index 82aecebc9..d42a99b6f 100644 --- a/OPTYap/tab.tries.c +++ b/OPTYap/tab.tries.c @@ -163,12 +163,6 @@ static struct trie_statistics{ free_global_trie_branch(NODE PASS_REGS) #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ -/****************************** -** Rational Terms Support ** -******************************/ -#ifdef TRIE_RATIONAL_TERMS -#include "tab.rational.i" -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ /****************************** @@ -206,13 +200,7 @@ static struct trie_statistics{ #define MODE_TERMS_LOOP #define INCLUDE_SUBGOAL_SEARCH_LOOP /* subgoal_search_terms_loop */ #define INCLUDE_ANSWER_SEARCH_LOOP /* answer_search_terms_loop */ -#ifdef TRIE_RATIONAL_TERMS -#undef TRIE_RATIONAL_TERMS #include "tab.tries.i" -#define TRIE_RATIONAL_TERMS -#else -#include "tab.tries.i" -#endif #undef INCLUDE_ANSWER_SEARCH_LOOP #undef INCLUDE_SUBGOAL_SEARCH_LOOP #undef MODE_TERMS_LOOP @@ -221,13 +209,7 @@ static struct trie_statistics{ #define INCLUDE_SUBGOAL_SEARCH_LOOP /* subgoal_search_global_trie_(terms)_loop */ #define INCLUDE_ANSWER_SEARCH_LOOP /* answer_search_global_trie_(terms)_loop */ #define INCLUDE_LOAD_ANSWER_LOOP /* load_substitution_loop */ -#ifdef TRIE_RATIONAL_TERMS -#undef TRIE_RATIONAL_TERMS #include "tab.tries.i" -#define TRIE_RATIONAL_TERMS -#else -#include "tab.tries.i" -#endif #undef INCLUDE_LOAD_ANSWER_LOOP #undef INCLUDE_ANSWER_SEARCH_LOOP #undef INCLUDE_SUBGOAL_SEARCH_LOOP @@ -917,13 +899,6 @@ static inline void traverse_trie_node(Term t, char *str, int *str_index_ptr, int } else if (mode == TRAVERSE_MODE_LONGINT_END) { mode = TRAVERSE_MODE_NORMAL; } else if (IsVarTerm(t)) { -#ifdef TRIE_RATIONAL_TERMS - if (t > VarIndexOfTableTerm(MAX_TABLE_VARS) && TrNode_child((gt_node_ptr) t) != 1) { //TODO: substitute the != 1 test to something more appropriate - /* Rational term */ - str_index += sprintf(& str[str_index], "**"); - traverse_update_arity(str, &str_index, arity); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { TrStat_gt_refs++; /* (type % 2 + 2): TRAVERSE_TYPE_ANSWER --> TRAVERSE_TYPE_GT_ANSWER */ diff --git a/OPTYap/tab.tries.i b/OPTYap/tab.tries.i index 0260e7e55..a02984888 100644 --- a/OPTYap/tab.tries.i +++ b/OPTYap/tab.tries.i @@ -18,8 +18,8 @@ #ifdef MODE_GLOBAL_TRIE_ENTRY #define INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY) \ { register gt_node_ptr entry_node = (gt_node_ptr) (ENTRY); \ - TrNode_child(entry_node) = (gt_node_ptr) ((unsigned long int) TrNode_child(entry_node) + 1); \ - } + TrNode_child(entry_node) = (gt_node_ptr) ((unsigned long int) TrNode_child(entry_node) + 1); \ + } #define NEW_SUBGOAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) @@ -47,7 +47,7 @@ #else #define SUBGOAL_CHECK_INSERT_ENTRY(TAB_ENT, NODE, ENTRY) \ NODE = subgoal_trie_check_insert_entry(TAB_ENT, NODE, ENTRY PASS_REGS) -#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ +#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ NODE = answer_trie_check_insert_entry(SG_FR, NODE, ENTRY, INSTR PASS_REGS) #endif /* MODE_GLOBAL_TRIE_LOOP */ @@ -56,9 +56,9 @@ #define ANSWER_SAFE_INSERT_ENTRY(NODE, ENTRY, INSTR) \ { ans_node_ptr new_node; \ NEW_ANSWER_TRIE_NODE(new_node, INSTR, ENTRY, NULL, NODE, NULL); \ - TrNode_child(NODE) = new_node; \ + TrNode_child(NODE) = new_node; \ NODE = new_node; \ - } + } #ifdef THREADS #define INVALIDATE_ANSWER_TRIE_NODE(NODE, SG_FR) \ TrNode_next(NODE) = SgFr_invalid_chain(SG_FR); \ @@ -1048,26 +1048,19 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c goto subgoal_search_loop_non_atomic; #endif /* MODE_GLOBAL_TRIE_LOOP */ -#ifdef TRIE_RATIONAL_TERMS - /* Needed structures, variables to support rational terms */ - term_array Ts; - void* CyclicTerm; - term_array_init(&Ts, 10); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - do { if (IsVarTerm(t)) { if (IsTableVarTerm(t)) { - t = MakeTableVarTerm(VarIndexOfTerm(t)); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); + t = MakeTableVarTerm(VarIndexOfTerm(t)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); } else { - if (subs_arity == MAX_TABLE_VARS) - Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: MAX_TABLE_VARS exceeded"); - STACK_PUSH_UP(t, stack_vars); - *((CELL *)t) = GLOBAL_table_var_enumerator(subs_arity); - t = MakeTableVarTerm(subs_arity); - subs_arity = subs_arity + 1; - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); + if (subs_arity == MAX_TABLE_VARS) + Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: MAX_TABLE_VARS exceeded"); + STACK_PUSH_UP(t, stack_vars); + *((CELL *)t) = GLOBAL_table_var_enumerator(subs_arity); + t = MakeTableVarTerm(subs_arity); + subs_arity = subs_arity + 1; + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); } } else if (IsAtomOrIntTerm(t)) { SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); @@ -1082,103 +1075,48 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c current_node = subgoal_trie_check_insert_gt_entry(tab_ent, current_node, (Term) entry_node PASS_REGS); #else /* ! MODE_TERMS_LOOP */ } else -#ifdef TRIE_RATIONAL_TERMS - if (IsRationalTerm(t)) { - t = STACK_POP_DOWN(stack_terms); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ #if defined(MODE_GLOBAL_TRIE_LOOP) /* for the global trie, it is safe to start here in the first iteration */ subgoal_search_loop_non_atomic: #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_COMPACT_PAIRS if (IsPairTerm(t)) { -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ CELL *aux_pair = RepPair(t); if (aux_pair == PairTermMark) { - t = STACK_POP_DOWN(stack_terms); -#ifdef TRIE_RATIONAL_TERMS - if (IsPairTerm(t) && ! IsRationalTerm(t)) { - term_array_push(&Ts, (void *) t, (void *) current_node); -#else - if (IsPairTerm(t)) { -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - aux_pair = RepPair(t); - t = Deref(aux_pair[1]); -#ifdef TRIE_RATIONAL_TERMS - if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); - } - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); - } else { - /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ - /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** - ** up 3 terms has already initially checked for the CompactPairInit term */ - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; - if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); - } else { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndTerm); - STACK_PUSH_UP(t, stack_terms); - } + t = STACK_POP_DOWN(stack_terms); + if (IsPairTerm(t)) { + aux_pair = RepPair(t); + t = Deref(aux_pair[1]); + if (t == TermNil) { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); + } else { + /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ + /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** + ** up 3 terms has already initially checked for the CompactPairInit term */ + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + } + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + } else { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndTerm); + STACK_PUSH_UP(t, stack_terms); + } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ } else { -#ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairInit); - t = Deref(aux_pair[1]); -#ifdef TRIE_RATIONAL_TERMS - if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); - } - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); - } else { - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; - if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairInit); + t = Deref(aux_pair[1]); + if (t == TermNil) { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); + } else { + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + } + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { @@ -1202,50 +1140,37 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c } else if (IsApplTerm(t)) { Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { - union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; - } u; - u.dbl = FloatOfTerm(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + union { + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; + } u; + u.dbl = FloatOfTerm(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[1]); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[1]); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[0]); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, u.t_dbl[0]); #ifdef MODE_GLOBAL_TRIE_LOOP - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #endif /* MODE_GLOBAL_TRIE_LOOP */ } else if (f == FunctorLongInt) { - Int li = LongIntOfTerm(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, li); + Int li = LongIntOfTerm(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, li); #ifdef MODE_GLOBAL_TRIE_LOOP - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #endif /* MODE_GLOBAL_TRIE_LOOP */ } else if (f == FunctorDBRef) { - Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorDBRef"); + Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorDBRef"); } else if (f == FunctorBigInt) { - Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorBigInt"); + Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unsupported type tag FunctorBigInt"); } else { -#ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - int i; - CELL *aux_appl = RepAppl(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); - for (i = ArityOfFunctor(f); i >= 1; i--) { -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; - if (IsVarTerm(aux_appl[i]) || IsApplTerm(aux_appl[i])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_appl[i])); - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); - } + int i; + CELL *aux_appl = RepAppl(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); + for (i = ArityOfFunctor(f); i >= 1; i--) + STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); } } else { Yap_Error(INTERNAL_ERROR, TermNil, "subgoal_search_loop: unknown type tag"); @@ -1253,9 +1178,7 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c } t = STACK_POP_DOWN(stack_terms); } while (t); -#ifdef TRIE_RATIONAL_TERMS - term_array_free(&Ts); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + *subs_arity_ptr = subs_arity; *stack_vars_ptr = stack_vars; return current_node; @@ -1333,26 +1256,20 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr goto answer_search_loop_non_atomic; #endif /* MODE_GLOBAL_TRIE_LOOP */ -#ifdef TRIE_RATIONAL_TERMS - term_array Ts; - void* CyclicTerm; - term_array_init(&Ts, 10); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - do { if (IsVarTerm(t)) { t = Deref(t); if (IsTableVarTerm(t)) { - t = MakeTableVarTerm(VarIndexOfTerm(t)); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_val + in_pair); + t = MakeTableVarTerm(VarIndexOfTerm(t)); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_val + in_pair); } else { - if (vars_arity == MAX_TABLE_VARS) - Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: MAX_TABLE_VARS exceeded"); - stack_vars_base[vars_arity] = t; - *((CELL *)t) = GLOBAL_table_var_enumerator(vars_arity); - t = MakeTableVarTerm(vars_arity); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); - vars_arity = vars_arity + 1; + if (vars_arity == MAX_TABLE_VARS) + Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: MAX_TABLE_VARS exceeded"); + stack_vars_base[vars_arity] = t; + *((CELL *)t) = GLOBAL_table_var_enumerator(vars_arity); + t = MakeTableVarTerm(vars_arity); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); + vars_arity = vars_arity + 1; } #ifdef TRIE_COMPACT_PAIRS in_pair = 0; @@ -1372,109 +1289,52 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ current_node = answer_trie_check_insert_gt_entry(sg_fr, current_node, (Term) entry_node, _trie_retry_gterm + in_pair PASS_REGS); #else /* ! MODE_TERMS_LOOP */ - } else -#ifdef TRIE_RATIONAL_TERMS - if (IsRationalTerm(t)) { - t = STACK_POP_DOWN(stack_terms); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); //TODO create _trie_.._rational - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + } else #if defined(MODE_GLOBAL_TRIE_LOOP) /* for the global trie, it is safe to start here in the first iteration */ answer_search_loop_non_atomic: #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_COMPACT_PAIRS if (IsPairTerm(t)) { -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ CELL *aux_pair = RepPair(t); if (aux_pair == PairTermMark) { - t = STACK_POP_DOWN(stack_terms); -#ifdef TRIE_RATIONAL_TERMS - if (IsPairTerm(t) && ! IsRationalTerm(t)) { - term_array_push(&Ts, (void *) t, (void *) current_node); -#else - if (IsPairTerm(t)) { -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - aux_pair = RepPair(t); - t = Deref(aux_pair[1]); -#ifdef TRIE_RATIONAL_TERMS - if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); - } - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); // CyclicTerm - STACK_PUSH_UP((Term) RationalMark, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - in_pair = 4; - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); - } else { - /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ - /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** - ** up 3 terms has already initially checked for the CompactPairInit term */ - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - in_pair = 4; - } -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; - if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); - } else { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndTerm, _trie_retry_null); - STACK_PUSH_UP(t, stack_terms); - } + t = STACK_POP_DOWN(stack_terms); + if (IsPairTerm(t)) { + aux_pair = RepPair(t); + t = Deref(aux_pair[1]); + if (t == TermNil) { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); + } else { + /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ + /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** + ** up 3 terms has already initially checked for the CompactPairInit term */ + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + in_pair = 4; + } + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + } else { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndTerm, _trie_retry_null); + STACK_PUSH_UP(t, stack_terms); + } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ } else { -#ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairInit, _trie_retry_null + in_pair); - t = Deref(aux_pair[1]); -#ifdef TRIE_RATIONAL_TERMS - if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); - } - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - in_pair = 4; - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); - in_pair = 0; - } else { - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); - STACK_PUSH_UP(t, stack_terms); - STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - in_pair = 4; - } -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; - if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairInit, _trie_retry_null + in_pair); + t = Deref(aux_pair[1]); + if (t == TermNil) { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); + in_pair = 0; + } else { + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); + STACK_PUSH_UP(t, stack_terms); + STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); + in_pair = 4; + } + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { @@ -1498,46 +1358,33 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr } else if (IsApplTerm(t)) { Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { - union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; - } u; - u.dbl = FloatOfTerm(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); + union { + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; + } u; + u.dbl = FloatOfTerm(t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[1], _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[1], _trie_retry_extension); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[0], _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_double); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[0], _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_double); } else if (f == FunctorLongInt) { - Int li = LongIntOfTerm (t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, li, _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_longint); + Int li = LongIntOfTerm (t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, li, _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_longint); } else if (f == FunctorDBRef) { - Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorDBRef"); + Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorDBRef"); } else if (f == FunctorBigInt) { - Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorBigInt"); + Yap_Error(INTERNAL_ERROR, TermNil, "answer_search_loop: unsupported type tag FunctorBigInt"); } else { -#ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - int i; - CELL *aux_appl = RepAppl(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_appl + in_pair); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); - for (i = ArityOfFunctor(f); i >= 1; i--) { -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; - if (IsVarTerm(aux_appl[i]) || IsApplTerm(aux_appl[i])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_appl[i])); - if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); - } + int i; + CELL *aux_appl = RepAppl(t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_appl + in_pair); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); + for (i = ArityOfFunctor(f); i >= 1; i--) + STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); } #ifdef TRIE_COMPACT_PAIRS in_pair = 0; @@ -1548,9 +1395,7 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr } t = STACK_POP_DOWN(stack_terms); } while (t); -#ifdef TRIE_RATIONAL_TERMS - term_array_free(&Ts); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + *vars_arity_ptr = vars_arity; return current_node; @@ -1585,8 +1430,8 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c trie_value = (Float) TrNode_entry(child_node); } else if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; } u; u.t_dbl[0] = TrNode_entry(child_node); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P @@ -1625,8 +1470,8 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; } u; u.dbl = FloatOfTerm(t); ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); @@ -1671,8 +1516,8 @@ static inline ans_node_ptr answer_search_sum(sg_fr_ptr sg_fr, ans_node_ptr curre trie_value = (Float) TrNode_entry(child_node); } else if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; } u; u.t_dbl[0] = TrNode_entry(child_node); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P @@ -1735,18 +1580,18 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i do { current_node = *bucket; if (current_node) { - ans_node_ptr next_node = TrNode_next(current_node); - if (IS_ANSWER_LEAF_NODE(current_node)) { - INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); - } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); - INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); - } - while (next_node) { - current_node = next_node; - next_node = TrNode_next(current_node); - invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); - } + ans_node_ptr next_node = TrNode_next(current_node); + if (IS_ANSWER_LEAF_NODE(current_node)) { + INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); + } else { + invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); + } + while (next_node) { + current_node = next_node; + next_node = TrNode_next(current_node); + invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); + } } } while (++bucket != last_bucket); if (Hash_next(hash)) @@ -1761,22 +1606,22 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i if (position == TRAVERSE_POSITION_FIRST) { ans_node_ptr next_node = TrNode_next(current_node); if (IS_ANSWER_LEAF_NODE(current_node)) { - INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); + INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); - INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } while (next_node) { - current_node = next_node; - next_node = TrNode_next(current_node); - invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); + current_node = next_node; + next_node = TrNode_next(current_node); + invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); } } else { if (IS_ANSWER_LEAF_NODE(current_node)) { - INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); + INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); - INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } } } @@ -1842,115 +1687,47 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { current_node = (ans_node_ptr) UNTAG_ANSWER_NODE(TrNode_parent(current_node)); #endif /* MODE_GLOBAL_TRIE_LOOP */ -#ifdef TRIE_RATIONAL_TERMS - term_array Ts; - void* CyclicTerm; - term_array_init(&Ts, 10); - Term RationalTermTMP; // a temporary temp to be used from the rational code -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - do { -#ifdef TRIE_RATIONAL_TERMS - CyclicTerm = term_array_member(Ts, (void *) current_node); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ if (IsVarTerm(t)) { -#ifdef TRIE_RATIONAL_TERMS - if (t > VarIndexOfTableTerm(MAX_TABLE_VARS) && TrNode_child((gt_node_ptr) t) != 1) { //TODO: substitute the != 1 test to something more appropriate - /* Rational term */ - RationalTermTMP = (Term) term_array_member(Ts, (void *) t); - if (RationalTermTMP) { - /* rational term is assigned a variable already */ - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); - STACK_PUSH_UP(RationalTermTMP, stack_terms); - } else { - RationalTermTMP = MkVarTerm(); - STACK_PUSH_UP(RationalTermTMP, stack_terms); - /* memorize the rational term and assign it a variable */ - term_array_push(&Ts, (void *) t, (void *) RationalTermTMP); - } - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - { #if ! defined(MODE_GLOBAL_TRIE_LOOP) || defined(GLOBAL_TRIE_FOR_SUBTERMS) - if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { - stack_terms = load_substitution_loop((gt_node_ptr) t, &vars_arity, stack_terms PASS_REGS); - } else + if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { + stack_terms = load_substitution_loop((gt_node_ptr) t, &vars_arity, stack_terms PASS_REGS); + } else #endif /* ! MODE_GLOBAL_TRIE_LOOP || GLOBAL_TRIE_FOR_SUBTERMS */ - { int var_index = VarIndexOfTableTerm(t); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit - vars_arity + var_index + 1); - if (var_index >= vars_arity) { - while (vars_arity < var_index) - stack_vars_base[vars_arity++] = 0; - stack_vars_base[vars_arity++] = MkVarTerm(); - } else if (stack_vars_base[var_index] == 0) - stack_vars_base[var_index] = MkVarTerm(); - STACK_PUSH_UP(stack_vars_base[var_index], stack_terms); - } + { int var_index = VarIndexOfTableTerm(t); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit - vars_arity + var_index + 1); + if (var_index >= vars_arity) { + while (vars_arity < var_index) + stack_vars_base[vars_arity++] = 0; + stack_vars_base[vars_arity++] = MkVarTerm(); + } else if (stack_vars_base[var_index] == 0) + stack_vars_base[var_index] = MkVarTerm(); + STACK_PUSH_UP(stack_vars_base[var_index], stack_terms); } } else if (IsAtomOrIntTerm(t)) { AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); -#ifdef TRIE_RATIONAL_TERMS - if (CyclicTerm) { - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 4); - STACK_PUSH_UP((Term) RationalMark, stack_terms); // Add a rational term marker necessary as we read both ways the stack // - STACK_PUSH_UP(t, stack_terms); // Add the term // - STACK_PUSH_UP(CyclicTerm, stack_terms); // Add the variable that the term will unify with // - STACK_PUSH_UP((Term) RationalMark, stack_terms); // Add a rational term marker necessary as we read both ways the stack // - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ STACK_PUSH_UP(t, stack_terms); } else if (IsPairTerm(t)) { #ifdef TRIE_COMPACT_PAIRS if (t == CompactPairInit) { - Term *stack_aux = stack_terms_base - stack_terms_pair_offset; - Term head, tail = STACK_POP_UP(stack_aux); -#ifdef TRIE_RATIONAL_TERMS - if (IsRationalTerm(tail)) { - Yap_Error(INTERNAL_ERROR, tail, "Rational element of a Rational Term appears as the first Tail of a list"); - } -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - while (STACK_NOT_EMPTY(stack_aux, stack_terms)) { - head = STACK_POP_UP(stack_aux); -#ifdef TRIE_RATIONAL_TERMS - if (IsRationalTerm(head)) { - head = STACK_POP_UP(stack_aux); // thats the rational term - RationalTermTMP = STACK_POP_UP(stack_aux); // that is the variable to unify with - (void) STACK_POP_UP(stack_aux); // eat the second rational mark - tail = MkPairTerm(head, tail); - Yap_unify(RationalTermTMP, tail); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - tail = MkPairTerm(head, tail); - } - stack_terms = stack_terms_base - stack_terms_pair_offset; - stack_terms_pair_offset = (int) STACK_POP_DOWN(stack_terms); - STACK_PUSH_UP(tail, stack_terms); + Term *stack_aux = stack_terms_base - stack_terms_pair_offset; + Term head, tail = STACK_POP_UP(stack_aux); + while (STACK_NOT_EMPTY(stack_aux, stack_terms)) { + head = STACK_POP_UP(stack_aux); + tail = MkPairTerm(head, tail); + } + stack_terms = stack_terms_base - stack_terms_pair_offset; + stack_terms_pair_offset = (int) STACK_POP_DOWN(stack_terms); + STACK_PUSH_UP(tail, stack_terms); } else { /* CompactPairEndList / CompactPairEndTerm */ - Term last; - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); - last = STACK_POP_DOWN(stack_terms); -#ifdef TRIE_RATIONAL_TERMS - RationalTermTMP = TermNil; - if (IsRationalTerm(last)) { // rather unlikely case the rational term is the last of a list - RationalTermTMP = STACK_POP_DOWN(stack_terms); // in this case we need to invert the term with the end of list - last = STACK_POP_DOWN(stack_terms); // variable to unify with - (void) STACK_POP_DOWN(stack_terms); // eat the second rational mark - } -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(stack_terms_pair_offset, stack_terms); - stack_terms_pair_offset = (int) (stack_terms_base - stack_terms); - if (t == CompactPairEndList) - STACK_PUSH_UP(TermNil, stack_terms); -#ifdef TRIE_RATIONAL_TERMS - if (RationalTermTMP && RationalTermTMP != TermNil) { - /* most probably this never occurs */ - STACK_PUSH_UP((Term) RationalMark, stack_terms); - STACK_PUSH_UP(last, stack_terms); - STACK_PUSH_UP(RationalTermTMP, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); - } else -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(last, stack_terms); + Term last; + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); + last = STACK_POP_DOWN(stack_terms); + STACK_PUSH_UP(stack_terms_pair_offset, stack_terms); + stack_terms_pair_offset = (int) (stack_terms_base - stack_terms); + if (t == CompactPairEndList) + STACK_PUSH_UP(TermNil, stack_terms); + STACK_PUSH_UP(last, stack_terms); } #else /* ! TRIE_COMPACT_PAIRS */ Term head = STACK_POP_DOWN(stack_terms); @@ -1961,54 +1738,37 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { } else if (IsApplTerm(t)) { Functor f = (Functor) RepAppl(t); if (f == FunctorDouble) { - union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; - } u; - t = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - u.t_dbl[0] = t; + union { + Term t_dbl[sizeof(Float)/sizeof(Term)]; + Float dbl; + } u; + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + u.t_dbl[0] = t; #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - t = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - u.t_dbl[1] = t; + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + u.t_dbl[1] = t; #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - current_node = TrNode_parent(current_node); - t = MkFloatTerm(u.dbl); + current_node = TrNode_parent(current_node); + t = MkFloatTerm(u.dbl); } else if (f == FunctorLongInt) { - Int li = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - current_node = TrNode_parent(current_node); - t = MkLongIntTerm(li); + Int li = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + current_node = TrNode_parent(current_node); + t = MkLongIntTerm(li); } else { - int f_arity = ArityOfFunctor(f); - t = Yap_MkApplTerm(f, f_arity, stack_terms); - stack_terms += f_arity; + int f_arity = ArityOfFunctor(f); + t = Yap_MkApplTerm(f, f_arity, stack_terms); + stack_terms += f_arity; } AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); STACK_PUSH_UP(t, stack_terms); } -#ifdef TRIE_RATIONAL_TERMS - if (CyclicTerm) { - RationalTermTMP = STACK_POP_DOWN(stack_terms); - if IsRationalTerm(RationalTermTMP) { - //printf("Special Case\n"); - } else if (IsPairTerm(RationalTermTMP)) { - Yap_unify((Term) CyclicTerm, RationalTermTMP); - } else if (IsApplTerm(RationalTermTMP)) { - Yap_unify((Term) CyclicTerm, RationalTermTMP); - } - STACK_PUSH_UP(RationalTermTMP, stack_terms); - } - RationalTermTMP = TermNil; - CyclicTerm = NULL; -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ t = TrNode_entry(current_node); current_node = TrNode_parent(current_node); } while (current_node); -#ifdef TRIE_RATIONAL_TERMS - term_array_free(&Ts); -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ + #ifdef MODE_GLOBAL_TRIE_LOOP *vars_arity_ptr = vars_arity; #endif /* MODE_GLOBAL_TRIE_LOOP */ From b0fdf9263cc9f005f4dba6c81d5688cc5ec0a5da Mon Sep 17 00:00:00 2001 From: Theofrastos Mantadelis Date: Wed, 18 Dec 2013 15:01:02 +0000 Subject: [PATCH 5/8] Restored the [] as the default value for tabling_mode yap flag --- C/stdpreds.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/C/stdpreds.c b/C/stdpreds.c index a150cc445..c5f6ca20d 100644 --- a/C/stdpreds.c +++ b/C/stdpreds.c @@ -1666,15 +1666,15 @@ p_access_yap_flags( USES_REGS1 ) tout = TermNil; if (IsMode_LocalTrie(yap_flags[flag])) tout = MkPairTerm(MkAtomTerm(AtomLocalTrie), tout); - else // if (IsMode_GlobalTrie(yap_flags[flag])) + else if (IsMode_GlobalTrie(yap_flags[flag])) tout = MkPairTerm(MkAtomTerm(AtomGlobalTrie), tout); if (IsMode_LoadAnswers(yap_flags[flag])) tout = MkPairTerm(MkAtomTerm(AtomLoadAnswers), tout); - else // if (IsMode_ExecAnswers(yap_flags[flag])) + else if (IsMode_ExecAnswers(yap_flags[flag])) tout = MkPairTerm(MkAtomTerm(AtomExecAnswers), tout); if (IsMode_Local(yap_flags[flag])) tout = MkPairTerm(MkAtomTerm(AtomLocal), tout); - else // if (IsMode_Batched(yap_flags[flag])) + else if (IsMode_Batched(yap_flags[flag])) tout = MkPairTerm(MkAtomTerm(AtomBatched), tout); #else tout = MkAtomTerm(AtomFalse); From 30828eba989d7c645b191889f01c13705855564d Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 18 Dec 2013 16:42:05 +0000 Subject: [PATCH 6/8] fix input sz: should only matter with NCHARS --- C/text.c | 4 +-- H/YapText.h | 69 +++++++++++++++++++++++++--------------------- os/pl-prologflag.c | 3 +- packages/real | 2 +- pl/absf.yap | 2 +- 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/C/text.c b/C/text.c index 2f570c5bd..a71e3f7c3 100644 --- a/C/text.c +++ b/C/text.c @@ -463,14 +463,14 @@ read_Text( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *leng #endif case YAP_STRING_CHARS: *enc = YAP_CHAR; - if (inp->sz) + if (inp->type & YAP_STRING_NCHARS) *lengp = inp->sz; else *lengp = strlen(inp->val.c); return (void *)inp->val.c; case YAP_STRING_WCHARS: *enc = YAP_WCHAR; - if (inp->sz) + if (inp->type & YAP_STRING_NCHARS) *lengp = inp->sz; else *lengp = wcslen(inp->val.w); diff --git a/H/YapText.h b/H/YapText.h index 24768605a..10ff66475 100644 --- a/H/YapText.h +++ b/H/YapText.h @@ -294,6 +294,7 @@ Yap_CharsToAtom( const char *s USES_REGS ) seq_tv_t inp, out; inp.val.c = s; + inp.sz = 0; inp.type = YAP_STRING_CHARS; out.type = YAP_STRING_ATOM; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -307,6 +308,7 @@ Yap_CharsToListOfAtoms( const char *s USES_REGS ) seq_tv_t inp, out; inp.val.c = s; + inp.sz = 0; inp.type = YAP_STRING_CHARS; out.type = YAP_STRING_ATOMS; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -320,6 +322,7 @@ Yap_CharsToListOfCodes( const char *s USES_REGS ) seq_tv_t inp, out; inp.val.c = s; + inp.sz = 0; inp.type = YAP_STRING_CHARS; out.type = YAP_STRING_CODES; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -333,6 +336,7 @@ Yap_CharsToString( const char *s USES_REGS ) seq_tv_t inp, out; inp.val.c = s; + inp.sz = 0; inp.type = YAP_STRING_CHARS; out.type = YAP_STRING_STRING; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -346,6 +350,7 @@ Yap_CharsToTDQ( const char *s, Term mod USES_REGS ) seq_tv_t inp, out; inp.val.c = s; + inp.sz = 0; inp.type = YAP_STRING_CHARS; inp.mod = mod; out.type = mod_to_type(mod PASS_REGS); @@ -689,7 +694,7 @@ static inline Atom Yap_StringToAtom(Term t0 USES_REGS) { seq_tv_t inp, out; - inp.sz = -1; + inp.sz = 0; inp.val.t = t0; inp.type = YAP_STRING_STRING; out.type = YAP_STRING_ATOM; @@ -702,7 +707,7 @@ static inline Atom Yap_StringSWIToAtom(Term t0 USES_REGS) { seq_tv_t inp, out; - inp.sz = -1; + inp.sz = 0; inp.val.t = t0; inp.type = YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_ATOMS_CODES|YAP_STRING_TERM; out.type = YAP_STRING_ATOM; @@ -715,7 +720,7 @@ static inline size_t Yap_StringToAtomic(Term t0 USES_REGS) { seq_tv_t inp, out; - inp.sz = -1; + inp.sz = 0; inp.val.t = t0; inp.type = YAP_STRING_STRING; out.type = YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_TERM; @@ -728,7 +733,7 @@ static inline size_t Yap_StringToLength(Term t0 USES_REGS) { seq_tv_t inp, out; - inp.sz = -1; + inp.sz = 0; inp.val.t = t0; inp.type = YAP_STRING_STRING; out.type = YAP_STRING_LENGTH; @@ -741,7 +746,7 @@ static inline size_t Yap_StringToListOfAtoms(Term t0 USES_REGS) { seq_tv_t inp, out; - inp.sz = -1; + inp.sz = 0; inp.val.t = t0; inp.type = YAP_STRING_STRING; out.type = YAP_STRING_ATOMS; @@ -754,7 +759,7 @@ static inline size_t Yap_StringSWIToListOfAtoms(Term t0 USES_REGS) { seq_tv_t inp, out; - inp.sz = -1; + inp.sz = 0; inp.val.t = t0; inp.type = YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_ATOMS_CODES|YAP_STRING_TERM; out.type = YAP_STRING_ATOMS; @@ -768,7 +773,7 @@ Yap_StringToListOfCodes(Term t0 USES_REGS) { seq_tv_t inp, out; inp.val.t = t0; - inp.sz = -1; + inp.sz = 0; inp.type = YAP_STRING_STRING; out.type = YAP_STRING_CODES; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -793,7 +798,7 @@ Yap_StringToNumber(Term t0 USES_REGS) { seq_tv_t inp, out; inp.val.t = t0; - inp.sz = -1; + inp.sz = 0; inp.type = YAP_STRING_STRING; out.type = YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_TERM; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -806,7 +811,7 @@ Yap_WCharsToListOfCodes(const wchar_t *s USES_REGS) { seq_tv_t inp, out; inp.val.w = s; - inp.sz = -1; + inp.sz = 0; inp.type = YAP_STRING_WCHARS; out.type = YAP_STRING_CODES; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -821,7 +826,7 @@ Yap_WCharsToTDQ( wchar_t *s, Term mod USES_REGS ) inp.val.w = s; inp.type = YAP_STRING_WCHARS; - inp.sz = -1; + inp.sz = 0; inp.mod = mod; out.type = mod_to_type(mod PASS_REGS); if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -834,7 +839,7 @@ Yap_WCharsToString(const wchar_t *s USES_REGS) { seq_tv_t inp, out; inp.val.w = s; - inp.sz = -1; + inp.sz = 0; inp.type = YAP_STRING_WCHARS; out.type = YAP_STRING_STRING; if (!Yap_CVT_Text(&inp, &out PASS_REGS)) @@ -848,10 +853,10 @@ Yap_ConcatAtoms(Term t1, Term t2 USES_REGS) seq_tv_t inpv[2], out; inpv[0].val.t = t1; inpv[0].type = YAP_STRING_ATOM; - inpv[0].sz = -1; + inpv[0].sz = 0; inpv[1].val.t = t2; inpv[1].type = YAP_STRING_ATOM; - inpv[1].sz = -1; + inpv[1].sz = 0; out.type = YAP_STRING_ATOM; if (!Yap_Concat_Text(2, inpv, &out PASS_REGS)) return NULL; @@ -864,10 +869,10 @@ Yap_ConcatAtomics(Term t1, Term t2 USES_REGS) seq_tv_t inpv[2], out; inpv[0].val.t = t1; inpv[0].type = YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_TERM; - inpv[0].sz = -1; + inpv[0].sz = 0; inpv[1].val.t = t2; inpv[1].type = YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_TERM; - inpv[1].sz = -1; + inpv[1].sz = 0; out.type = YAP_STRING_ATOM; if (!Yap_Concat_Text(2, inpv, &out PASS_REGS)) return NULL; @@ -880,10 +885,10 @@ Yap_ConcatStrings(Term t1, Term t2 USES_REGS) seq_tv_t inpv[2], out; inpv[0].val.t = t1; inpv[0].type = YAP_STRING_STRING; - inpv[0].sz = -1; + inpv[0].sz = 0; inpv[1].val.t = t2; inpv[1].type = YAP_STRING_STRING; - inpv[1].sz = -1; + inpv[1].sz = 0; out.type = YAP_STRING_STRING; if (!Yap_Concat_Text(2, inpv, &out PASS_REGS)) return 0L; @@ -900,11 +905,11 @@ Yap_SpliceAtom(Term t1, Atom ats[], size_t cut, size_t max USES_REGS) cuts[1] = max; inp.type = YAP_STRING_ATOM; inp.val.t = t1; - inp.sz = -1; + inp.sz = 0; outv[0].type = YAP_STRING_ATOM; - outv[0].sz = -1; + outv[0].sz = 0; outv[1].type = YAP_STRING_ATOM; - outv[1].sz = -1; + outv[1].sz = 0; if (!Yap_Splice_Text(2, cuts, &inp, NULL, outv PASS_REGS)) return NULL; ats[0] = outv[0].val.a; @@ -918,13 +923,13 @@ Yap_SubtractHeadAtom(Term t1, Term th USES_REGS) seq_tv_t outv[2], inp; inp.type = YAP_STRING_ATOM; inp.val.t = t1; - inp.sz = -1; + inp.sz = 0; outv[0].type = YAP_STRING_ATOM; outv[0].val.t = th; - outv[0].sz = -1; + outv[0].sz = 0; outv[1].type = YAP_STRING_ATOM; outv[1].val.t = 0; - outv[1].sz = -1; + outv[1].sz = 0; if (!Yap_Splice_Text(2, NULL, &inp, NULL, outv PASS_REGS)) return NULL; return outv[1].val.a; @@ -937,10 +942,10 @@ Yap_SubtractTailAtom(Term t1, Term th USES_REGS) seq_tv_t outv[2], inp; inp.type = YAP_STRING_ATOM; inp.val.t = t1; - inp.sz = -1; + inp.sz = 0; outv[0].type = YAP_STRING_ATOM; outv[0].val.t = 0; - outv[0].sz = -1; + outv[0].sz = 0; outv[1].type = YAP_STRING_ATOM; outv[1].val.t = th; if (!Yap_Splice_Text(2, NULL, &inp, NULL, outv PASS_REGS)) @@ -955,10 +960,10 @@ Yap_SpliceString(Term t1, Term ts[], size_t cut, size_t max USES_REGS) size_t cuts[2]; inp.type = YAP_STRING_STRING; inp.val.t = t1; - inp.sz = -1; + inp.sz = 0; outv[0].type = YAP_STRING_STRING; outv[1].type = YAP_STRING_STRING; - outv[1].sz = -1; + outv[1].sz = 0; cuts[0] = cut; cuts[1] = max; if (!Yap_Splice_Text(2, cuts, &inp, NULL, outv PASS_REGS)) @@ -974,13 +979,13 @@ Yap_SubtractHeadString(Term t1, Term th USES_REGS) seq_tv_t outv[2], inp; inp.type = YAP_STRING_STRING; inp.val.t = t1; - inp.sz = -1; + inp.sz = 0; outv[0].type = YAP_STRING_STRING; outv[0].val.t = th; - outv[0].sz = -1; + outv[0].sz = 0; outv[1].type = YAP_STRING_STRING; outv[1].val.t = 0; - outv[1].sz = -1; + outv[1].sz = 0; if (!Yap_Splice_Text(2, NULL, &inp, NULL, outv PASS_REGS)) return 0L; return outv[1].val.t; @@ -992,10 +997,10 @@ Yap_SubtractTailString(Term t1, Term th USES_REGS) seq_tv_t outv[2], inp; inp.type = YAP_STRING_STRING; inp.val.t = t1; - inp.sz = -1; + inp.sz = 0; outv[0].type = YAP_STRING_STRING; outv[0].val.t = 0; - outv[0].sz = -1; + outv[0].sz = 0; outv[1].type = YAP_STRING_STRING; outv[1].val.t = th; if (!Yap_Splice_Text(2, NULL, &inp, NULL, outv PASS_REGS)) diff --git a/os/pl-prologflag.c b/os/pl-prologflag.c index b1a744fce..29f593808 100644 --- a/os/pl-prologflag.c +++ b/os/pl-prologflag.c @@ -118,10 +118,11 @@ indexOfBoolMask(unsigned int mask) return i; } - void setPrologFlag(const char *name, int flags, ...) { GET_LD + if (!strcmp("file_name_variables",name)) + printf("hello\n"); atom_t an = PL_new_atom(name); prolog_flag *f; Symbol s; diff --git a/packages/real b/packages/real index a383bfd51..5a72fe49e 160000 --- a/packages/real +++ b/packages/real @@ -1 +1 @@ -Subproject commit a383bfd51144172fdea8463bd56e9308742e5de2 +Subproject commit 5a72fe49e5a5c651a890a388eb967b83da8e2c52 diff --git a/pl/absf.yap b/pl/absf.yap index b7f4809c1..3b4845a7b 100644 --- a/pl/absf.yap +++ b/pl/absf.yap @@ -224,7 +224,7 @@ absolute_file_name(File,Opts,TrueFileName) :- (Type == directory -> exists_directory(F0) ; - \+ exists_directory(F0) % if it has a type cannot be a directory. + \+ exists_directory(F0) % if it has a type cannot be a directory. ). '$add_extensions'([Ext|_],File,F) :- From c6351abe25888c0f158e1757f55b69b237083bfc Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 18 Dec 2013 19:09:21 +0000 Subject: [PATCH 7/8] remove debugging msg --- os/pl-prologflag.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/os/pl-prologflag.c b/os/pl-prologflag.c index 29f593808..b1a744fce 100644 --- a/os/pl-prologflag.c +++ b/os/pl-prologflag.c @@ -118,11 +118,10 @@ indexOfBoolMask(unsigned int mask) return i; } + void setPrologFlag(const char *name, int flags, ...) { GET_LD - if (!strcmp("file_name_variables",name)) - printf("hello\n"); atom_t an = PL_new_atom(name); prolog_flag *f; Symbol s; From 493b591b669567fa269a6de2ea65585842411f4d Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 18 Dec 2013 19:34:15 +0000 Subject: [PATCH 8/8] slots again: make sure they're bound after resetting the stack and when calling InitForeign --- C/c_interface.c | 3 +++ C/load_foreign.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/C/c_interface.c b/C/c_interface.c index 4503d8dd0..dabd21bd8 100644 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -3394,6 +3394,9 @@ YAP_Reset(void) /* the first real choice-point will also have AP=FAIL */ /* always have an empty slots for people to use */ P = CP = YESCODE; + // ensure that we have slots where we need them + LOCAL_CurSlot = 0; + Yap_StartSlots( PASS_REGS1 ); RECOVER_MACHINE_REGS(); return res; } diff --git a/C/load_foreign.c b/C/load_foreign.c index 937c89f26..5a5f07b5f 100755 --- a/C/load_foreign.c +++ b/C/load_foreign.c @@ -83,7 +83,9 @@ p_load_foreign( USES_REGS1 ) /* call the OS specific function for dynamic loading */ if(Yap_LoadForeign(ofiles,libs,InitProcName,&InitProc)==LOAD_SUCCEEDED) { + Int CurSlot = Yap_StartSlots( PASS_REGS1 ); (*InitProc)(); + LOCAL_CurSlot = CurSlot; returncode = TRUE; }