From 7d841065e95b51fe3c08ea401752fd4233576235 Mon Sep 17 00:00:00 2001 From: Ricardo Rocha Date: Sun, 27 Sep 2009 02:31:31 +0100 Subject: [PATCH] support for the common global trie (version GT-T: global trie for terms) --- OPTYap/opt.config.h | 129 ++-- OPTYap/opt.init.c | 15 +- OPTYap/opt.macros.h | 66 +- OPTYap/opt.preds.c | 245 ++++--- OPTYap/opt.proto.h | 15 +- OPTYap/opt.structs.h | 28 +- OPTYap/tab.insts.i | 30 +- OPTYap/tab.macros.h | 172 +++-- OPTYap/tab.structs.h | 58 +- OPTYap/tab.tries.c | 1387 +++++++++++++++++++++++--------------- OPTYap/tab.tries.insts.i | 1327 ++++++++++++++++++++---------------- 11 files changed, 2091 insertions(+), 1381 deletions(-) diff --git a/OPTYap/opt.config.h b/OPTYap/opt.config.h index 79aad1ebf..41b25cf38 100644 --- a/OPTYap/opt.config.h +++ b/OPTYap/opt.config.h @@ -13,9 +13,9 @@ ** General Configuration Parameters ** ** ----------------------------------------------------------------- */ -/* ------------------------------------------ ** -** memory alloc scheme (define one) ** -** ------------------------------------------ */ +/* ----------------------------------------------------- ** +** memory alloc scheme (mandatory, define one) ** +** ----------------------------------------------------- */ #define YAP_MEMORY_ALLOC_SCHEME 1 /* #define SHM_MEMORY_ALLOC_SCHEME 1 */ /* #define MALLOC_MEMORY_ALLOC_SCHEME 1 */ @@ -33,9 +33,9 @@ ** ----------------------- */ #define MAX_TABLE_VARS 1000 -/* ------------------------------------------ ** -** trail freeze scheme (define one) ** -** ------------------------------------------ */ +/* ----------------------------------------------------- ** +** trail freeze scheme (mandatory, define one) ** +** ----------------------------------------------------- */ #define BFZ_TRAIL_SCHEME 1 /* #define BBREG_TRAIL_SCHEME 1 */ @@ -44,29 +44,36 @@ ** ----------------------------------------------- */ #define TABLING_EARLY_COMPLETION 1 -/* ------------------------------------------------ ** -** support trie compact pairs? (optional) ** -** ------------------------------------------------ */ +/* ------------------------------------------------- ** +** support trie compact pairs ? (optional) ** +** ------------------------------------------------- */ #define TRIE_COMPACT_PAIRS 1 -/* --------------------------------------------------- ** -** support deterministic tabling? (optional) ** -** --------------------------------------------------- */ +/* ------------------------------------------------------ ** +** support global trie ? (optional, define one) ** +** ------------------------------------------------------ */ +/* #define GLOBAL_TRIE_FOR_CALLS_ANSWERS 1 */ +/* #define GLOBAL_TRIE_FOR_TERMS 1 */ +/* #define GLOBAL_TRIE_FOR_SUBTERMS 1 */ + +/* ---------------------------------------------------- ** +** support deterministic tabling ? (optional) ** +** ---------------------------------------------------- */ /* #define DETERMINISTIC_TABLING 1 */ -/* ------------------------------------------------ ** -** limit the table space size? (optional) ** -** ------------------------------------------------ */ +/* ------------------------------------------------- ** +** limit the table space size ? (optional) ** +** ------------------------------------------------- */ /* #define LIMIT_TABLING 1 */ -/* ------------------------------------------------ ** -** support incomplete tabling? (optional) ** -** ------------------------------------------------ */ +/* ------------------------------------------------- ** +** support incomplete tabling ? (optional) ** +** ------------------------------------------------- */ /* #define INCOMPLETE_TABLING 1 */ -/* ---------------------------------------- -- ** -** enable error checking? (optional) ** -** ------------------------------------------- */ +/* ----------------------------------------- -- ** +** enable error checking ? (optional) ** +** -------------------------------------------- */ /* #define TABLING_ERRORS 1 */ @@ -84,15 +91,15 @@ #define MAX_BRANCH_DEPTH 1000 #define MAX_BEST_TIMES 21 -/* -------------------------------------------- ** -** memory mapping scheme (define one) ** -** -------------------------------------------- */ +/* ------------------------------------------------------- ** +** memory mapping scheme (mandatory, define one) ** +** ------------------------------------------------------- */ #define MMAP_MEMORY_MAPPING_SCHEME 1 /* #define SHM_MEMORY_MAPPING_SCHEME 1 */ -/* ---------------------------------------- -- ** -** enable error checking? (optional) ** -** ------------------------------------------- */ +/* -------------------------------------------- ** +** enable error checking ? (optional) ** +** -------------------------------------------- */ /* #define YAPOR_ERRORS 1 */ @@ -109,40 +116,38 @@ #define TABLE_LOCK_BUCKETS 512 #define TG_ANSWER_SLOTS 20 -/* ------------------------------------------- ** -** tries locking scheme (define one) ** -** ------------------------------------------- ** -** The TABLE_LOCK_AT_ENTRY_LEVEL scheme locks ** -** the access to the table space in the entry ** -** data structure. It restricts the number of ** -** lock operations needed to go through the ** -** table data structures. ** -** ** -** The TABLE_LOCK_AT_NODE_LEVEL scheme locks ** -** each data structure before accessing it. It ** -** decreases concurrrency for workers ** -** accessing commom parts of the table space. ** -** ** -** The TABLE_LOCK_AT_WRITE_LEVEL scheme is an ** -** hibrid scheme, it only locks a table data ** -** structure when it is going to update it. ** -** You can use ALLOC_BEFORE_CHECK with this ** -** scheme to allocate a node before checking ** -** if it will be necessary. ** -** ------------------------------------------- */ +/* ------------------------------------------------------ ** +** tries locking scheme (mandatory, define one) ** +** ------------------------------------------------------ ** +** The TABLE_LOCK_AT_ENTRY_LEVEL scheme locks the access ** +** to the table space in the entry data structure. It ** +** restricts the number of lock operations needed to go ** +** through the table data structures. ** +** ** +** The TABLE_LOCK_AT_NODE_LEVEL scheme locks each data ** +** structure before accessing it. It decreases ** +** concurrrency for workers accessing commom parts of the ** +** table space. ** +** ** +** The TABLE_LOCK_AT_WRITE_LEVEL scheme is an hibrid ** +** scheme, it only locks a table data structure when it ** +** is going to update it. You can use ALLOC_BEFORE_CHECK ** +** with this scheme to allocate a node before checking ** +** if it will be necessary. ** +** ------------------------------------------------------ */ /* #define TABLE_LOCK_AT_ENTRY_LEVEL 1 */ /* #define TABLE_LOCK_AT_NODE_LEVEL 1 */ #define TABLE_LOCK_AT_WRITE_LEVEL 1 /* #define ALLOC_BEFORE_CHECK 1 */ -/* ---------------------------------------- ** -** support inner cuts? (optional) ** -** ---------------------------------------- */ +/* ----------------------------------------- ** +** support inner cuts ? (optional) ** +** ----------------------------------------- */ #define TABLING_INNER_CUTS 1 -/* --------------------------------------------------- ** -** use timestamps for suspension? (optional) ** -** --------------------------------------------------- */ +/* ---------------------------------------------------- ** +** use timestamps for suspension ? (optional) ** +** ---------------------------------------------------- */ #define TIMESTAMP_CHECK 1 @@ -189,6 +194,17 @@ #if defined(BFZ_TRAIL_SCHEME) && defined(BBREG_TRAIL_SCHEME) #error Do not define multiple trail schemes #endif /* BFZ_TRAIL_SCHEME && BBREG_TRAIL_SCHEME */ +#if defined(GLOBAL_TRIE_FOR_CALLS_ANSWERS) +#if defined(GLOBAL_TRIE_FOR_TERMS) || defined(GLOBAL_TRIE_FOR_SUBTERMS) +#error Do not define multiple global trie schemes +#endif /* GLOBAL_TRIE_FOR_TERMS || GLOBAL_TRIE_FOR_SUBTERMS */ +#endif /* GLOBAL_TRIE_FOR_CALLS_ANSWERS */ +#if defined(GLOBAL_TRIE_FOR_TERMS) && defined(GLOBAL_TRIE_FOR_SUBTERMS) +#error Do not define multiple global trie schemes +#endif /* GLOBAL_TRIE_FOR_TERMS && GLOBAL_TRIE_FOR_SUBTERMS */ +#if defined(GLOBAL_TRIE_FOR_CALLS_ANSWERS) || defined(GLOBAL_TRIE_FOR_TERMS) || defined(GLOBAL_TRIE_FOR_SUBTERMS) +#define GLOBAL_TRIE +#endif /* GLOBAL_TRIE_FOR_CALLS_ANSWERS || GLOBAL_TRIE_FOR_TERMS || GLOBAL_TRIE_FOR_SUBTERMS */ #endif /* TABLING */ #if defined(YAPOR) && defined(TABLING) @@ -225,8 +241,11 @@ #ifndef TABLING #undef BFZ_TRAIL_SCHEME #undef BBREG_TRAIL_SCHEME -#undef TRIE_COMPACT_PAIRS #undef TABLING_EARLY_COMPLETION +#undef TRIE_COMPACT_PAIRS +#undef GLOBAL_TRIE_FOR_CALLS_ANSWERS +#undef GLOBAL_TRIE_FOR_TERMS +#undef GLOBAL_TRIE_FOR_SUBTERMS #undef DETERMINISTIC_TABLING #undef LIMIT_TABLING #undef INCOMPLETE_TABLING diff --git a/OPTYap/opt.init.c b/OPTYap/opt.init.c index 0180e4298..1b706b3c6 100644 --- a/OPTYap/opt.init.c +++ b/OPTYap/opt.init.c @@ -46,6 +46,7 @@ ma_h_inner_struct *Yap_ma_h_top; ** Local macros ** ** ---------------------- */ +#ifdef SHM_MEMORY_ALLOC_SCHEME #define STRUCTS_PER_PAGE(STR_TYPE) ((Yap_page_size - STRUCT_SIZE(struct page_header)) / STRUCT_SIZE(STR_TYPE)) #define INIT_PAGES(PG, STR_TYPE) \ @@ -54,6 +55,9 @@ ma_h_inner_struct *Yap_ma_h_top; Pg_str_in_use(PG) = 0; \ Pg_str_per_pg(PG) = STRUCTS_PER_PAGE(STR_TYPE); \ Pg_free_pg(PG) = NULL +#else +#define INIT_PAGES(PG, STR_TYPE) Pg_str_in_use(PG) = 0 +#endif /* SHM_MEMORY_ALLOC_SCHEME */ @@ -82,12 +86,16 @@ void Yap_init_global(int max_table_size, int n_workers, int sch_loop, int delay_ INIT_PAGES(GLOBAL_PAGES_tg_ans_fr, struct table_subgoal_answer_frame); #endif /* TABLING_INNER_CUTS */ #ifdef TABLING +#ifdef GLOBAL_TRIE + INIT_PAGES(GLOBAL_PAGES_gt_node, struct global_trie_node); + INIT_PAGES(GLOBAL_PAGES_gt_hash, struct global_trie_hash); +#endif /* GLOBAL_TRIE */ INIT_PAGES(GLOBAL_PAGES_tab_ent, struct table_entry); INIT_PAGES(GLOBAL_PAGES_sg_fr, struct subgoal_frame); INIT_PAGES(GLOBAL_PAGES_sg_node, struct subgoal_trie_node); INIT_PAGES(GLOBAL_PAGES_ans_node, struct answer_trie_node); - INIT_PAGES(GLOBAL_PAGES_sg_hash, struct subgoal_hash); - INIT_PAGES(GLOBAL_PAGES_ans_hash, struct answer_hash); + INIT_PAGES(GLOBAL_PAGES_sg_hash, struct subgoal_trie_hash); + INIT_PAGES(GLOBAL_PAGES_ans_hash, struct answer_trie_hash); INIT_PAGES(GLOBAL_PAGES_dep_fr, struct dependency_frame); #endif /* TABLING */ #if defined(YAPOR) && defined(TABLING) @@ -141,6 +149,9 @@ void Yap_init_global(int max_table_size, int n_workers, int sch_loop, int delay_ #ifdef TABLING /* global data related to tabling */ GLOBAL_root_tab_ent = NULL; +#ifdef GLOBAL_TRIE + new_global_trie_node(GLOBAL_root_gt, 0, NULL, NULL, NULL); +#endif /* GLOBAL_TRIE */ #ifdef LIMIT_TABLING GLOBAL_first_sg_fr = NULL; GLOBAL_last_sg_fr = NULL; diff --git a/OPTYap/opt.macros.h b/OPTYap/opt.macros.h index dd1c1bf19..1e86ad9cb 100644 --- a/OPTYap/opt.macros.h +++ b/OPTYap/opt.macros.h @@ -48,8 +48,6 @@ extern int Yap_page_size; #define PAGE_HEADER(STR) (pg_hd_ptr)((unsigned long int)STR - (unsigned long int)STR % Yap_page_size) #define STRUCT_NEXT(STR) ((STR)->next) - - #define UPDATE_STATS(STAT, VALUE) STAT += VALUE #ifdef MALLOC_MEMORY_ALLOC_SCHEME /* ---------------------------------------------------------------- */ @@ -148,7 +146,7 @@ extern int Yap_page_size; if (SgFr_first_answer(sg_fr) && \ SgFr_first_answer(sg_fr) != SgFr_answer_trie(sg_fr)) { \ SgFr_state(sg_fr) = ready; \ - free_answer_hash_chain(SgFr_hash_chain(sg_fr)); \ + free_answer_trie_hash_chain(SgFr_hash_chain(sg_fr)); \ SgFr_hash_chain(sg_fr) = NULL; \ SgFr_first_answer(sg_fr) = NULL; \ SgFr_last_answer(sg_fr) = NULL; \ @@ -384,51 +382,55 @@ extern int Yap_page_size; } #define FREE_HASH_BUCKETS(BUCKET_PTR) FREE_BLOCK(BUCKET_PTR) +#define ALLOC_OR_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_or_fr, struct or_frame) +#define FREE_OR_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_or_fr, struct or_frame) +#define ALLOC_QG_SOLUTION_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_qg_sol_fr, struct query_goal_solution_frame) +#define FREE_QG_SOLUTION_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_qg_sol_fr, struct query_goal_solution_frame) -#define ALLOC_OR_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_or_fr, struct or_frame) -#define FREE_OR_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_or_fr, struct or_frame) +#define ALLOC_QG_ANSWER_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_qg_ans_fr, struct query_goal_answer_frame) +#define FREE_QG_ANSWER_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_qg_ans_fr, struct query_goal_answer_frame) -#define ALLOC_QG_SOLUTION_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_qg_sol_fr, struct query_goal_solution_frame) -#define FREE_QG_SOLUTION_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_qg_sol_fr, struct query_goal_solution_frame) +#define ALLOC_TG_SOLUTION_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_tg_sol_fr, struct table_subgoal_solution_frame) +#define FREE_TG_SOLUTION_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_tg_sol_fr, struct table_subgoal_solution_frame) -#define ALLOC_QG_ANSWER_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_qg_ans_fr, struct query_goal_answer_frame) -#define FREE_QG_ANSWER_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_qg_ans_fr, struct query_goal_answer_frame) +#define ALLOC_TG_ANSWER_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_tg_ans_fr, struct table_subgoal_answer_frame) +#define FREE_TG_ANSWER_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_tg_ans_fr, struct table_subgoal_answer_frame) -#define ALLOC_TG_SOLUTION_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_tg_sol_fr, struct table_subgoal_solution_frame) -#define FREE_TG_SOLUTION_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_tg_sol_fr, struct table_subgoal_solution_frame) +#define ALLOC_TABLE_ENTRY(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_tab_ent, struct table_entry) +#define FREE_TABLE_ENTRY(STR) FREE_STRUCT(STR, GLOBAL_PAGES_tab_ent, struct table_entry) -#define ALLOC_TG_ANSWER_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_tg_ans_fr, struct table_subgoal_answer_frame) -#define FREE_TG_ANSWER_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_tg_ans_fr, struct table_subgoal_answer_frame) +#define ALLOC_GLOBAL_TRIE_NODE(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_gt_node, struct global_trie_node) +#define FREE_GLOBAL_TRIE_NODE(STR) FREE_STRUCT(STR, GLOBAL_PAGES_gt_node, struct global_trie_node) -#define ALLOC_TABLE_ENTRY(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_tab_ent, struct table_entry) -#define FREE_TABLE_ENTRY(STR) FREE_STRUCT(STR, GLOBAL_PAGES_tab_ent, struct table_entry) +#define ALLOC_SUBGOAL_TRIE_NODE(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_sg_node, struct subgoal_trie_node) +#define FREE_SUBGOAL_TRIE_NODE(STR) FREE_STRUCT(STR, GLOBAL_PAGES_sg_node, struct subgoal_trie_node) -#define ALLOC_SUBGOAL_TRIE_NODE(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_sg_node, struct subgoal_trie_node) -#define FREE_SUBGOAL_TRIE_NODE(STR) FREE_STRUCT(STR, GLOBAL_PAGES_sg_node, struct subgoal_trie_node) - -#define ALLOC_SUBGOAL_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_sg_fr, struct subgoal_frame) -#define FREE_SUBGOAL_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_sg_fr, struct subgoal_frame) +#define ALLOC_SUBGOAL_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_sg_fr, struct subgoal_frame) +#define FREE_SUBGOAL_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_sg_fr, struct subgoal_frame) #ifdef YAPOR -#define ALLOC_ANSWER_TRIE_NODE(STR) ALLOC_NEXT_FREE_STRUCT(STR, GLOBAL_PAGES_ans_node, struct answer_trie_node) +#define ALLOC_ANSWER_TRIE_NODE(STR) ALLOC_NEXT_FREE_STRUCT(STR, GLOBAL_PAGES_ans_node, struct answer_trie_node) #else /* TABLING */ -#define ALLOC_ANSWER_TRIE_NODE(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_ans_node, struct answer_trie_node) +#define ALLOC_ANSWER_TRIE_NODE(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_ans_node, struct answer_trie_node) #endif /* YAPOR - TABLING */ -#define FREE_ANSWER_TRIE_NODE(STR) FREE_STRUCT(STR, GLOBAL_PAGES_ans_node, struct answer_trie_node) +#define FREE_ANSWER_TRIE_NODE(STR) FREE_STRUCT(STR, GLOBAL_PAGES_ans_node, struct answer_trie_node) -#define ALLOC_DEPENDENCY_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_dep_fr, struct dependency_frame) -#define FREE_DEPENDENCY_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_dep_fr, struct dependency_frame) +#define ALLOC_DEPENDENCY_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_dep_fr, struct dependency_frame) +#define FREE_DEPENDENCY_FRAME(STR) FREE_STRUCT(STR, GLOBAL_PAGES_dep_fr, struct dependency_frame) -#define ALLOC_SUSPENSION_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_susp_fr, struct suspension_frame) -#define FREE_SUSPENSION_FRAME(STR) FREE_BLOCK(SuspFr_global_start(STR)); \ - FREE_STRUCT(STR, GLOBAL_PAGES_susp_fr, struct suspension_frame) +#define ALLOC_SUSPENSION_FRAME(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_susp_fr, struct suspension_frame) +#define FREE_SUSPENSION_FRAME(STR) FREE_BLOCK(SuspFr_global_start(STR)); \ + FREE_STRUCT(STR, GLOBAL_PAGES_susp_fr, struct suspension_frame) -#define ALLOC_SUBGOAL_HASH(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_sg_hash, struct subgoal_hash) -#define FREE_SUBGOAL_HASH(STR) FREE_STRUCT(STR, GLOBAL_PAGES_sg_hash, struct subgoal_hash) +#define ALLOC_GLOBAL_TRIE_HASH(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_gt_hash, struct global_trie_hash) +#define FREE_GLOBAL_TRIE_HASH(STR) FREE_STRUCT(STR, GLOBAL_PAGES_gt_hash, struct global_trie_hash) -#define ALLOC_ANSWER_HASH(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_ans_hash, struct answer_hash) -#define FREE_ANSWER_HASH(STR) FREE_STRUCT(STR, GLOBAL_PAGES_ans_hash, struct answer_hash) +#define ALLOC_SUBGOAL_TRIE_HASH(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_sg_hash, struct subgoal_trie_hash) +#define FREE_SUBGOAL_TRIE_HASH(STR) FREE_STRUCT(STR, GLOBAL_PAGES_sg_hash, struct subgoal_trie_hash) + +#define ALLOC_ANSWER_TRIE_HASH(STR) ALLOC_STRUCT(STR, GLOBAL_PAGES_ans_hash, struct answer_trie_hash) +#define FREE_ANSWER_TRIE_HASH(STR) FREE_STRUCT(STR, GLOBAL_PAGES_ans_hash, struct answer_trie_hash) diff --git a/OPTYap/opt.preds.c b/OPTYap/opt.preds.c index 67c48f35e..d5a8757b0 100644 --- a/OPTYap/opt.preds.c +++ b/OPTYap/opt.preds.c @@ -72,6 +72,9 @@ static Int p_abolish_all_tables(void); static Int p_show_tabled_predicates(void); static Int p_show_table(void); static Int p_show_all_tables(void); +#ifdef GLOBAL_TRIE +static Int p_show_global_trie(void); +#endif /* GLOBAL_TRIE */ static Int p_table_statistics(void); static Int p_tabling_statistics(void); #endif /* TABLING */ @@ -100,9 +103,13 @@ static void shm_table_entries(long *pages_in_use, long *bytes_in_use); static void shm_subgoal_frames(long *pages_in_use, long *bytes_in_use); static void shm_subgoal_trie_nodes(long *pages_in_use, long *bytes_in_use); static void shm_answer_trie_nodes(long *pages_in_use, long *bytes_in_use); -static void shm_subgoal_hashes(long *pages_in_use, long *bytes_in_use); -static void shm_answer_hashes(long *pages_in_use, long *bytes_in_use); +static void shm_subgoal_trie_hashes(long *pages_in_use, long *bytes_in_use); +static void shm_answer_trie_hashes(long *pages_in_use, long *bytes_in_use); static void shm_dependency_frames(long *pages_in_use, long *bytes_in_use); +#ifdef GLOBAL_TRIE +static void shm_global_trie_nodes(long *pages_in_use, long *bytes_in_use); +static void shm_global_trie_hashes(long *pages_in_use, long *bytes_in_use); +#endif /* GLOBAL_TRIE */ #endif /* TABLING */ #if defined(YAPOR) && defined(TABLING) static void shm_suspension_frames(long *pages_in_use, long *bytes_in_use); @@ -137,6 +144,9 @@ void Yap_init_optyap_preds(void) { Yap_InitCPred("show_tabled_predicates", 0, p_show_tabled_predicates, SafePredFlag|SyncPredFlag); Yap_InitCPred("$c_show_table", 2, p_show_table, SafePredFlag|SyncPredFlag|HiddenPredFlag); Yap_InitCPred("show_all_tables", 0, p_show_all_tables, SafePredFlag|SyncPredFlag); +#ifdef GLOBAL_TRIE + Yap_InitCPred("show_global_trie", 0, p_show_global_trie, SafePredFlag|SyncPredFlag); +#endif /* GLOBAL_TRIE */ Yap_InitCPred("$c_table_statistics", 2, p_table_statistics, SafePredFlag|SyncPredFlag|HiddenPredFlag); Yap_InitCPred("tabling_statistics", 0, p_tabling_statistics, SafePredFlag|SyncPredFlag); #endif /* TABLING */ @@ -499,27 +509,23 @@ Int p_or_statistics(void) { #ifdef SHM_MEMORY_ALLOC_SCHEME long pages_in_use = 0, bytes_in_use = 0; - fprintf(Yap_stdout, "\n"); shm_or_frames(&pages_in_use, &bytes_in_use); shm_query_goal_solution_frames(&pages_in_use, &bytes_in_use); shm_query_goal_answer_frames(&pages_in_use, &bytes_in_use); shm_pages(pages_in_use, bytes_in_use); - fprintf(Yap_stdout, "\n"); #else long bytes_in_use = 0; - fprintf(Yap_stdout, "\n"); - fprintf(Yap_stdout, "%s or frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Or-frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_or_fr) == 1 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_or_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_or_fr) * sizeof(struct or_frame); - fprintf(Yap_stdout, "%s query goal solution frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Query goal solution frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr) == 0 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr) * sizeof(struct query_goal_solution_frame); - fprintf(Yap_stdout, "%s query goal answer frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Query goal answer frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr) == 0 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr) * sizeof(struct query_goal_answer_frame); - fprintf(Yap_stdout, "\n total memory in use: %10ld bytes\n", bytes_in_use); - fprintf(Yap_stdout, "\n"); + fprintf(Yap_stdout, " Total memory in use: %10ld bytes\n", bytes_in_use); #endif /* MEMORY_ALLOC_SCHEME */ return (TRUE); } @@ -559,7 +565,6 @@ Int p_table(void) { Atom at; int arity; tab_ent_ptr tab_ent; - sg_node_ptr sg_node; mod = Deref(ARG1); t = Deref(ARG2); @@ -578,8 +583,7 @@ Int p_table(void) { if (pe->cs.p_code.FirstClause) return (FALSE); /* predicate already compiled */ pe->PredFlags |= TabledPredFlag; - new_subgoal_trie_node(sg_node, 0, NULL, NULL, NULL); - new_table_entry(tab_ent, pe, at, arity, sg_node); + new_table_entry(tab_ent, pe, at, arity); if (IsMode_Local(yap_flags[TABLING_MODE_FLAG])) SetMode_Local(TabEnt_mode(tab_ent)); if (IsMode_LoadAnswers(yap_flags[TABLING_MODE_FLAG])) @@ -680,11 +684,15 @@ Int p_abolish_table(void) { return (FALSE); hash = TabEnt_hash_chain(tab_ent); TabEnt_hash_chain(tab_ent) = NULL; - free_subgoal_hash_chain(hash); + free_subgoal_trie_hash_chain(hash); sg_node = TrNode_child(TabEnt_subgoal_trie(tab_ent)); if (sg_node) { TrNode_child(TabEnt_subgoal_trie(tab_ent)) = NULL; +#ifdef GLOBAL_TRIE + free_subgoal_trie_branch(sg_node, TabEnt_arity(tab_ent), TRAVERSE_POSITION_FIRST); +#else free_subgoal_trie_branch(sg_node, TabEnt_arity(tab_ent), 0, TRAVERSE_POSITION_FIRST); +#endif /* GLOBAL_TRIE */ } return (TRUE); } @@ -700,11 +708,15 @@ Int p_abolish_all_tables(void) { while(tab_ent) { hash = TabEnt_hash_chain(tab_ent); TabEnt_hash_chain(tab_ent) = NULL; - free_subgoal_hash_chain(hash); + free_subgoal_trie_hash_chain(hash); sg_node = TrNode_child(TabEnt_subgoal_trie(tab_ent)); if (sg_node) { TrNode_child(TabEnt_subgoal_trie(tab_ent)) = NULL; +#ifdef GLOBAL_TRIE + free_subgoal_trie_branch(sg_node, TabEnt_arity(tab_ent), TRAVERSE_POSITION_FIRST); +#else free_subgoal_trie_branch(sg_node, TabEnt_arity(tab_ent), 0, TRAVERSE_POSITION_FIRST); +#endif /* GLOBAL_TRIE */ } tab_ent = TabEnt_next(tab_ent); } @@ -742,8 +754,7 @@ Int p_show_table(void) { tab_ent = RepPredProp(PredPropByFunc(FunctorOfTerm(t), mod))->TableOfPred; else return (FALSE); - fprintf(Yap_stdout, "Table structure for predicate '%s/%d'\n", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); - traverse_table(tab_ent, TRUE); + show_table(tab_ent, SHOW_MODE_STRUCTURE); return (TRUE); } @@ -754,14 +765,22 @@ Int p_show_all_tables(void) { tab_ent = GLOBAL_root_tab_ent; while(tab_ent) { - fprintf(Yap_stdout, "Table structure for predicate '%s/%d'\n", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); - traverse_table(tab_ent, TRUE); + show_table(tab_ent, SHOW_MODE_STRUCTURE); tab_ent = TabEnt_next(tab_ent); } return (TRUE); } +#ifdef GLOBAL_TRIE +static +Int p_show_global_trie(void) { + show_global_trie(); + return (TRUE); +} +#endif /* GLOBAL_TRIE */ + + static Int p_table_statistics(void) { Term mod, t; @@ -775,9 +794,7 @@ Int p_table_statistics(void) { tab_ent = RepPredProp(PredPropByFunc(FunctorOfTerm(t), mod))->TableOfPred; else return (FALSE); - fprintf(Yap_stdout, "Table statistics for predicate '%s/%d'", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); - traverse_table(tab_ent, FALSE); - table_stats(); + show_table(tab_ent, SHOW_MODE_STATISTICS); return (TRUE); } @@ -787,37 +804,47 @@ Int p_tabling_statistics(void) { #ifdef SHM_MEMORY_ALLOC_SCHEME long pages_in_use = 0, bytes_in_use = 0; - fprintf(Yap_stdout, "\n"); shm_table_entries(&pages_in_use, &bytes_in_use); shm_subgoal_frames(&pages_in_use, &bytes_in_use); shm_subgoal_trie_nodes(&pages_in_use, &bytes_in_use); shm_answer_trie_nodes(&pages_in_use, &bytes_in_use); - shm_subgoal_hashes(&pages_in_use, &bytes_in_use); - shm_answer_hashes(&pages_in_use, &bytes_in_use); +#ifdef GLOBAL_TRIE + shm_global_trie_nodes(&pages_in_use, &bytes_in_use); +#endif /* GLOBAL_TRIE */ + shm_subgoal_trie_hashes(&pages_in_use, &bytes_in_use); + shm_answer_trie_hashes(&pages_in_use, &bytes_in_use); +#ifdef GLOBAL_TRIE + shm_global_trie_hashes(&pages_in_use, &bytes_in_use); +#endif /* GLOBAL_TRIE */ shm_dependency_frames(&pages_in_use, &bytes_in_use); shm_pages(pages_in_use, bytes_in_use); - fprintf(Yap_stdout, "\n"); #else long bytes_in_use = 0; - fprintf(Yap_stdout, "\n"); - fprintf(Yap_stdout, " table entries: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_tab_ent)); + fprintf(Yap_stdout, " Table entries: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_tab_ent)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_tab_ent) * sizeof(struct table_entry); - fprintf(Yap_stdout, " subgoal frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_fr)); + fprintf(Yap_stdout, " Subgoal frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_fr) * sizeof(struct subgoal_frame); - fprintf(Yap_stdout, " subgoal trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_node)); + fprintf(Yap_stdout, " Subgoal trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_node)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_node) * sizeof(struct subgoal_trie_node); - fprintf(Yap_stdout, " answer trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_node)); + fprintf(Yap_stdout, " Answer trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_node)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_node) * sizeof(struct answer_trie_node); - fprintf(Yap_stdout, " subgoal hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_hash)); - bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_hash) * sizeof(struct subgoal_hash); - fprintf(Yap_stdout, " answer hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_hash)); - bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_hash) * sizeof(struct answer_hash); - fprintf(Yap_stdout, "%s dependency frames: %10ld structs in use\n", +#ifdef GLOBAL_TRIE + fprintf(Yap_stderr, " Global trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_gt_node)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_gt_node) * sizeof(struct global_trie_node); +#endif /* GLOBAL_TRIE */ + fprintf(Yap_stdout, " Subgoal trie hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_hash)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_hash) * sizeof(struct subgoal_trie_hash); + fprintf(Yap_stdout, " Answer trie hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_hash)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_hash) * sizeof(struct answer_trie_hash); +#ifdef GLOBAL_TRIE + fprintf(Yap_stderr, " Global trie hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_gt_hash)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_gt_hash) * sizeof(struct global_trie_hash); +#endif /* GLOBAL_TRIE */ + fprintf(Yap_stdout, "%s Dependency frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_dep_fr) == 1 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_dep_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_dep_fr) * sizeof(struct dependency_frame); - fprintf(Yap_stdout, "\n total memory in use: %10ld bytes\n", bytes_in_use); - fprintf(Yap_stdout, "\n"); + fprintf(Yap_stdout, " Total memory in use: %10ld bytes\n", bytes_in_use); #endif /* MEMORY_ALLOC_SCHEME */ return (TRUE); } @@ -830,7 +857,6 @@ int p_opt_statistics(void) { #ifdef SHM_MEMORY_ALLOC_SCHEME long pages_in_use = 0, bytes_in_use = 0; - fprintf(Yap_stdout, "\n"); shm_or_frames(&pages_in_use, &bytes_in_use); shm_query_goal_solution_frames(&pages_in_use, &bytes_in_use); shm_query_goal_answer_frames(&pages_in_use, &bytes_in_use); @@ -842,53 +868,64 @@ int p_opt_statistics(void) { shm_subgoal_frames(&pages_in_use, &bytes_in_use); shm_subgoal_trie_nodes(&pages_in_use, &bytes_in_use); shm_answer_trie_nodes(&pages_in_use, &bytes_in_use); - shm_subgoal_hashes(&pages_in_use, &bytes_in_use); - shm_answer_hashes(&pages_in_use, &bytes_in_use); +#ifdef GLOBAL_TRIE + shm_global_trie_nodes(&pages_in_use, &bytes_in_use); +#endif /* GLOBAL_TRIE */ + shm_subgoal_trie_hashes(&pages_in_use, &bytes_in_use); + shm_answer_trie_hashes(&pages_in_use, &bytes_in_use); +#ifdef GLOBAL_TRIE + shm_global_trie_hashes(&pages_in_use, &bytes_in_use); +#endif /* GLOBAL_TRIE */ shm_dependency_frames(&pages_in_use, &bytes_in_use); shm_show_suspension_frames(&pages_in_use, &bytes_in_use); shm_pages(pages_in_use, bytes_in_use); - fprintf(Yap_stdout, "\n"); #else long bytes_in_use = 0; - fprintf(Yap_stdout, "\n"); - fprintf(Yap_stdout, "%s or frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Or-frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_or_fr) == 1 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_or_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_or_fr) * sizeof(struct or_frame); - fprintf(Yap_stdout, "%s query goal solution frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Query goal solution frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr) == 1 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr) * sizeof(struct query_goal_solution_frame); - fprintf(Yap_stdout, "%s query goal answer frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Query goal answer frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr) == 1 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr) * sizeof(struct query_goal_answer_frame); #ifdef TABLING_INNER_CUTS - fprintf(Yap_stdout, "%s table subgoal solution frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Table subgoal solution frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_tg_sol_fr) == 0 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_tg_sol_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_tg_sol_fr) * sizeof(struct table_subgoal_solution_frame); - fprintf(Yap_stdout, "%s table subgoal answer frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Table subgoal answer frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_tg_ans_fr) == 0 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_tg_ans_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_tg_ans_fr) * sizeof(struct table_subgoal_answer_frame); #endif /* TABLING_INNER_CUTS */ - fprintf(Yap_stdout, " table entries: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_tab_ent)); + fprintf(Yap_stdout, " Table entries: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_tab_ent)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_tab_ent) * sizeof(struct table_entry); - fprintf(Yap_stdout, " subgoal frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_fr)); + fprintf(Yap_stdout, " Subgoal frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_fr) * sizeof(struct subgoal_frame); - fprintf(Yap_stdout, " subgoal trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_node)); + fprintf(Yap_stdout, " Subgoal trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_node)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_node) * sizeof(struct subgoal_trie_node); - fprintf(Yap_stdout, " answer trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_node)); + fprintf(Yap_stdout, " Answer trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_node)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_node) * sizeof(struct answer_trie_node); - fprintf(Yap_stdout, " subgoal hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_hash)); - bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_hash) * sizeof(struct subgoal_hash); - fprintf(Yap_stdout, " answer hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_hash)); - bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_hash) * sizeof(struct answer_hash); - fprintf(Yap_stdout, "%s dependency frames: %10ld structs in use\n", +#ifdef GLOBAL_TRIE + fprintf(Yap_stderr, " Global trie nodes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_gt_node)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_gt_node) * sizeof(struct global_trie_node); +#endif /* GLOBAL_TRIE */ + fprintf(Yap_stdout, " Subgoal trie hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_sg_hash)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_hash) * sizeof(struct subgoal_trie_hash); + fprintf(Yap_stdout, " Answer trie hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_ans_hash)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_hash) * sizeof(struct answer_trie_hash); +#ifdef GLOBAL_TRIE + fprintf(Yap_stderr, " Global trie hashes: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_gt_hash)); + bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_gt_hash) * sizeof(struct global_trie_hash); +#endif /* GLOBAL_TRIE */ + fprintf(Yap_stdout, "%s Dependency frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_dep_fr) == 1 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_dep_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_dep_fr) * sizeof(struct dependency_frame); - fprintf(Yap_stdout, "%s suspension frames: %10ld structs in use\n", + fprintf(Yap_stdout, "%s Suspension frames: %10ld structs in use\n", Pg_str_in_use(GLOBAL_PAGES_susp_fr) == 0 ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_susp_fr)); bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_susp_fr) * sizeof(struct suspension_frame); - fprintf(Yap_stdout, "\n total memory in use: %10ld bytes\n", bytes_in_use); - fprintf(Yap_stdout, "\n"); + fprintf(Yap_stdout, " Total memory in use: %10ld bytes\n", bytes_in_use); #endif /* MEMORY_ALLOC_SCHEME */ return (TRUE); } @@ -932,11 +969,11 @@ void shm_pages(long pages_in_use, long bytes_in_use) { cont++; pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "\n%s total memory in use: %8ld pages %10ld bytes\n", + fprintf(Yap_stdout, "%s Total memory in use: %8ld pages %10ld bytes\n", Pg_str_in_use(GLOBAL_PAGES_void) == pages_in_use && Pg_pg_alloc(GLOBAL_PAGES_void) - pages_in_use == cont ? " ": "*", Pg_str_in_use(GLOBAL_PAGES_void), bytes_in_use); - fprintf(Yap_stdout, " total memory: %8ld pages %10ld bytes\n", + fprintf(Yap_stdout, " Total memory allocated: %8ld pages %10ld bytes\n", Pg_pg_alloc(GLOBAL_PAGES_void), Pg_pg_alloc(GLOBAL_PAGES_void) * Yap_page_size); return; } @@ -958,7 +995,7 @@ void shm_or_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s or frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Or-frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_or_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_or_fr) == 1 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_or_fr), Pg_str_in_use(GLOBAL_PAGES_or_fr)); @@ -983,7 +1020,7 @@ void shm_query_goal_solution_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s query goal solution frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Query goal solution frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_qg_sol_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr) == 0 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_qg_sol_fr), Pg_str_in_use(GLOBAL_PAGES_qg_sol_fr)); @@ -1008,7 +1045,7 @@ void shm_query_goal_answer_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s query goal answer frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Query goal answer frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_qg_ans_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr) == 0 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_qg_ans_fr), Pg_str_in_use(GLOBAL_PAGES_qg_ans_fr)); @@ -1035,7 +1072,7 @@ void shm_table_subgoal_solution_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s table subgoal solution frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Table subgoal solution frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_tg_sol_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_tg_sol_fr) == 0 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_tg_sol_fr), Pg_str_in_use(GLOBAL_PAGES_tg_sol_fr)); @@ -1060,7 +1097,7 @@ void shm_table_subgoal_answer_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s table subgoal answer frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Table subgoal answer frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_tg_ans_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_tg_ans_fr) == 0 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_tg_ans_fr), Pg_str_in_use(GLOBAL_PAGES_tg_ans_fr)); @@ -1092,7 +1129,7 @@ void shm_table_entries(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s table entries: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Table entries: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_tab_ent) + Pg_str_in_use(GLOBAL_PAGES_tab_ent) == cont ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_tab_ent), Pg_str_in_use(GLOBAL_PAGES_tab_ent)); *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_tab_ent); @@ -1123,7 +1160,7 @@ void shm_subgoal_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s subgoal frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Subgoal frames: %8ld pages %10ld structs in use\n", #ifdef LIMIT_TABLING Pg_str_in_use(GLOBAL_PAGES_sg_fr) + #endif /* LIMIT_TABLING */ @@ -1150,7 +1187,7 @@ void shm_subgoal_trie_nodes(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s subgoal trie nodes: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Subgoal trie nodes: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_sg_node) == cont ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_sg_node), Pg_str_in_use(GLOBAL_PAGES_sg_node)); *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_sg_node); @@ -1174,7 +1211,7 @@ void shm_answer_trie_nodes(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s answer trie nodes: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Answer trie nodes: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_ans_node) == cont ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_ans_node), Pg_str_in_use(GLOBAL_PAGES_ans_node)); *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_ans_node); @@ -1184,7 +1221,7 @@ void shm_answer_trie_nodes(long *pages_in_use, long *bytes_in_use) { static -void shm_subgoal_hashes(long *pages_in_use, long *bytes_in_use) { +void shm_subgoal_trie_hashes(long *pages_in_use, long *bytes_in_use) { long cont = 0; pg_hd_ptr pg_hd; sg_hash_ptr aux_ptr; @@ -1198,17 +1235,17 @@ void shm_subgoal_hashes(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s subgoal hashes: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Subgoal trie hashes: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_sg_hash) == cont ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_sg_hash), Pg_str_in_use(GLOBAL_PAGES_sg_hash)); *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_sg_hash); - *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_hash) * sizeof(struct subgoal_hash); + *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_sg_hash) * sizeof(struct subgoal_trie_hash); return; } static -void shm_answer_hashes(long *pages_in_use, long *bytes_in_use) { +void shm_answer_trie_hashes(long *pages_in_use, long *bytes_in_use) { long cont = 0; pg_hd_ptr pg_hd; ans_hash_ptr aux_ptr; @@ -1222,11 +1259,11 @@ void shm_answer_hashes(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s answer hashes: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Answer trie hashes: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_ans_hash) == cont ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_ans_hash), Pg_str_in_use(GLOBAL_PAGES_ans_hash)); *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_ans_hash); - *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_hash) * sizeof(struct answer_hash); + *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_ans_hash) * sizeof(struct answer_trie_hash); return; } @@ -1246,7 +1283,7 @@ void shm_dependency_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s dependency frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Dependency frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_dep_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_dep_fr) == 1 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_dep_fr), Pg_str_in_use(GLOBAL_PAGES_dep_fr)); @@ -1254,6 +1291,56 @@ void shm_dependency_frames(long *pages_in_use, long *bytes_in_use) { *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_dep_fr) * sizeof(struct dependency_frame); return; } + + +#ifdef GLOBAL_TRIE +static +void shm_global_trie_nodes(long *pages_in_use, long *bytes_in_use) { + long cont = 0; + pg_hd_ptr pg_hd; + gt_node_ptr aux_ptr; + + pg_hd = Pg_free_pg(GLOBAL_PAGES_gt_node); + while (pg_hd) { + aux_ptr = PgHd_free_str(pg_hd); + while (aux_ptr) { + cont++; + aux_ptr = TrNode_next(aux_ptr); + } + pg_hd = PgHd_next(pg_hd); + } + fprintf(Yap_stderr, "%s Global trie nodes: %8ld pages %10ld structs in use\n", + Pg_str_free(GLOBAL_PAGES_gt_node) == cont ? " ": "*", + Pg_pg_alloc(GLOBAL_PAGES_gt_node), Pg_str_in_use(GLOBAL_PAGES_gt_node)); + *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_gt_node); + *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_gt_node) * sizeof(struct global_trie_node); + return; +} + + +static +void shm_global_trie_hashes(long *pages_in_use, long *bytes_in_use) { + long cont = 0; + pg_hd_ptr pg_hd; + gt_hash_ptr aux_ptr; + + pg_hd = Pg_free_pg(GLOBAL_PAGES_gt_hash); + while (pg_hd) { + aux_ptr = PgHd_free_str(pg_hd); + while (aux_ptr) { + cont++; + aux_ptr = Hash_next(aux_ptr); + } + pg_hd = PgHd_next(pg_hd); + } + fprintf(Yap_stderr, "%s Global trie hashes: %8ld pages %10ld structs in use\n", + Pg_str_free(GLOBAL_PAGES_gt_hash) == cont ? " ": "*", + Pg_pg_alloc(GLOBAL_PAGES_gt_hash), Pg_str_in_use(GLOBAL_PAGES_gt_hash)); + *pages_in_use += Pg_pg_alloc(GLOBAL_PAGES_gt_hash); + *bytes_in_use += Pg_str_in_use(GLOBAL_PAGES_gt_hash) * sizeof(struct global_trie_hash); + return; +} +#endif /* GLOBAL_TRIE */ #endif /* TABLING */ @@ -1273,7 +1360,7 @@ void shm_suspension_frames(long *pages_in_use, long *bytes_in_use) { } pg_hd = PgHd_next(pg_hd); } - fprintf(Yap_stdout, "%s suspension frames: %8ld pages %10ld structs in use\n", + fprintf(Yap_stdout, "%s Suspension frames: %8ld pages %10ld structs in use\n", Pg_str_free(GLOBAL_PAGES_susp_fr) == cont && Pg_str_in_use(GLOBAL_PAGES_susp_fr) == 0 ? " ": "*", Pg_pg_alloc(GLOBAL_PAGES_susp_fr), Pg_str_in_use(GLOBAL_PAGES_susp_fr)); diff --git a/OPTYap/opt.proto.h b/OPTYap/opt.proto.h index a26522a55..46912aeae 100644 --- a/OPTYap/opt.proto.h +++ b/OPTYap/opt.proto.h @@ -65,13 +65,22 @@ void finish_yapor(void); #ifdef TABLING sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr); ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr); -void load_answer_trie(ans_node_ptr ans_node, CELL *subs_ptr); +void load_answer(ans_node_ptr ans_node, CELL *subs_ptr); +#ifdef GLOBAL_TRIE +CELL *load_substitution_variable(gt_node_ptr current_node, CELL *aux_stack_ptr); +#endif /* GLOBAL_TRIE */ void private_completion(sg_fr_ptr sg_fr); +#ifdef GLOBAL_TRIE +void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int position); +#else void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int nodes_extra, int position); +#endif /* GLOBAL_TRIE */ void free_answer_trie_branch(ans_node_ptr node, int position); void update_answer_trie(sg_fr_ptr sg_fr); -void traverse_table(tab_ent_ptr tab_ent, int show_table); -void table_stats(void); +void show_table(tab_ent_ptr tab_ent, int show_mode); +#ifdef GLOBAL_TRIE +void show_global_trie(void); +#endif /* GLOBAL_TRIE */ #endif /* TABLING */ diff --git a/OPTYap/opt.structs.h b/OPTYap/opt.structs.h index 3b4ee4510..838ffc77f 100644 --- a/OPTYap/opt.structs.h +++ b/OPTYap/opt.structs.h @@ -41,20 +41,22 @@ typedef struct page_header { ** ---------------------- */ struct pages { +#ifdef SHM_MEMORY_ALLOC_SCHEME #ifdef YAPOR lockvar lock; #endif /* YAPOR */ - volatile long pages_allocated; - volatile long structs_in_use; int structs_per_page; struct page_header *first_free_page; + volatile long pages_allocated; +#endif /* SHM_MEMORY_ALLOC_SCHEME */ + volatile long structs_in_use; }; #define Pg_lock(X) ((X).lock) -#define Pg_pg_alloc(X) ((X).pages_allocated) -#define Pg_str_in_use(X) ((X).structs_in_use) #define Pg_str_per_pg(X) ((X).structs_per_page) #define Pg_free_pg(X) ((X).first_free_page) +#define Pg_pg_alloc(X) ((X).pages_allocated) +#define Pg_str_in_use(X) ((X).structs_in_use) #define Pg_str_free(X) (Pg_pg_alloc(X) * Pg_str_per_pg(X) - Pg_str_in_use(X)) @@ -78,12 +80,16 @@ struct global_pages { struct pages table_subgoal_answer_frame_pages; #endif /* TABLING_INNER_CUTS */ #ifdef TABLING +#ifdef GLOBAL_TRIE + struct pages global_trie_node_pages; + struct pages global_trie_hash_pages; +#endif /* GLOBAL_TRIE */ struct pages table_entry_pages; struct pages subgoal_frame_pages; struct pages subgoal_trie_node_pages; struct pages answer_trie_node_pages; - struct pages subgoal_hash_pages; - struct pages answer_hash_pages; + struct pages subgoal_trie_hash_pages; + struct pages answer_trie_hash_pages; struct pages dependency_frame_pages; #endif /* TABLING */ #if defined(YAPOR) && defined(TABLING) @@ -166,6 +172,9 @@ struct global_data{ #ifdef TABLING /* global data related to tabling */ struct table_entry *root_table_entry; +#ifdef GLOBAL_TRIE + struct global_trie_node *root_global_trie; +#endif /* GLOBAL_TRIE */ #ifdef LIMIT_TABLING struct subgoal_frame *first_subgoal_frame; struct subgoal_frame *last_subgoal_frame; @@ -189,12 +198,14 @@ struct global_data{ #define GLOBAL_PAGES_qg_ans_fr (GLOBAL.pages.query_goal_answer_frame_pages) #define GLOBAL_PAGES_tg_sol_fr (GLOBAL.pages.table_subgoal_solution_frame_pages) #define GLOBAL_PAGES_tg_ans_fr (GLOBAL.pages.table_subgoal_answer_frame_pages) +#define GLOBAL_PAGES_gt_node (GLOBAL.pages.global_trie_node_pages) +#define GLOBAL_PAGES_gt_hash (GLOBAL.pages.global_trie_hash_pages) #define GLOBAL_PAGES_tab_ent (GLOBAL.pages.table_entry_pages) #define GLOBAL_PAGES_sg_fr (GLOBAL.pages.subgoal_frame_pages) #define GLOBAL_PAGES_sg_node (GLOBAL.pages.subgoal_trie_node_pages) #define GLOBAL_PAGES_ans_node (GLOBAL.pages.answer_trie_node_pages) -#define GLOBAL_PAGES_sg_hash (GLOBAL.pages.subgoal_hash_pages) -#define GLOBAL_PAGES_ans_hash (GLOBAL.pages.answer_hash_pages) +#define GLOBAL_PAGES_sg_hash (GLOBAL.pages.subgoal_trie_hash_pages) +#define GLOBAL_PAGES_ans_hash (GLOBAL.pages.answer_trie_hash_pages) #define GLOBAL_PAGES_dep_fr (GLOBAL.pages.dependency_frame_pages) #define GLOBAL_PAGES_susp_fr (GLOBAL.pages.suspension_frame_pages) #define SCHEDULER_LOOP (GLOBAL.scheduler_loop) @@ -231,6 +242,7 @@ struct global_data{ #define PARALLEL_EXECUTION_MODE (GLOBAL.parallel_execution_mode) #define GLOBAL_answers (GLOBAL.answers) #define GLOBAL_root_tab_ent (GLOBAL.root_table_entry) +#define GLOBAL_root_gt (GLOBAL.root_global_trie) #define GLOBAL_first_sg_fr (GLOBAL.first_subgoal_frame) #define GLOBAL_last_sg_fr (GLOBAL.last_subgoal_frame) #define GLOBAL_check_sg_fr (GLOBAL.check_subgoal_frame) diff --git a/OPTYap/tab.insts.i b/OPTYap/tab.insts.i index 8ca819423..0a51def57 100644 --- a/OPTYap/tab.insts.i +++ b/OPTYap/tab.insts.i @@ -210,7 +210,7 @@ } else { \ subs_ptr = (CELL *) (CONS_CP(B) + 1); \ } \ - load_answer_trie(ANSWER, subs_ptr); \ + load_answer(ANSWER, subs_ptr); \ /* procceed */ \ YENV = ENV; \ GONext(); \ @@ -317,7 +317,7 @@ } PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, subs_ptr); + load_answer(ans_node, subs_ptr); YENV = ENV; GONext(); ENDPBOp(); @@ -347,7 +347,7 @@ PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, subs_ptr); + load_answer(ans_node, subs_ptr); YENV = ENV; GONext(); } else { @@ -420,7 +420,7 @@ store_generator_node(tab_ent, sg_fr, PREG->u.Otapl.s, TRY_ANSWER); PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, subs_ptr); + load_answer(ans_node, subs_ptr); YENV = ENV; GONext(); #endif /* INCOMPLETE_TABLING */ @@ -479,7 +479,7 @@ } PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, YENV); + load_answer(ans_node, YENV); YENV = ENV; GONext(); } else { @@ -490,7 +490,9 @@ PREG = (yamop *) TrNode_child(SgFr_answer_trie(sg_fr)); PREFETCH_OP(PREG); *--YENV = 0; /* vars_arity */ +#ifndef GLOBAL_TRIE *--YENV = 0; /* heap_arity */ +#endif /* GLOBAL_TRIE */ GONext(); } } @@ -529,7 +531,7 @@ store_generator_node(tab_ent, sg_fr, PREG->u.Otapl.s, TRY_ANSWER); PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, subs_ptr); + load_answer(ans_node, subs_ptr); YENV = ENV; GONext(); #endif /* INCOMPLETE_TABLING */ @@ -588,7 +590,7 @@ } PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, YENV); + load_answer(ans_node, YENV); YENV = ENV; GONext(); } else { @@ -599,7 +601,9 @@ PREG = (yamop *) TrNode_child(SgFr_answer_trie(sg_fr)); PREFETCH_OP(PREG); *--YENV = 0; /* vars_arity */ +#ifndef GLOBAL_TRIE *--YENV = 0; /* heap_arity */ +#endif /* GLOBAL_TRIE */ GONext(); } } @@ -638,7 +642,7 @@ store_generator_node(tab_ent, sg_fr, PREG->u.Otapl.s, TRY_ANSWER); PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, subs_ptr); + load_answer(ans_node, subs_ptr); YENV = ENV; GONext(); #endif /* INCOMPLETE_TABLING */ @@ -697,7 +701,7 @@ } PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, YENV); + load_answer(ans_node, YENV); YENV = ENV; GONext(); } else { @@ -708,7 +712,9 @@ PREG = (yamop *) TrNode_child(SgFr_answer_trie(sg_fr)); PREFETCH_OP(PREG); *--YENV = 0; /* vars_arity */ +#ifndef GLOBAL_TRIE *--YENV = 0; /* heap_arity */ +#endif /* GLOBAL_TRIE */ GONext(); } } @@ -1685,7 +1691,7 @@ } PREG = (yamop *) CPREG; PREFETCH_OP(PREG); - load_answer_trie(ans_node, YENV); + load_answer(ans_node, YENV); YENV = ENV; GONext(); } else { @@ -1697,7 +1703,9 @@ PREG = (yamop *) TrNode_child(SgFr_answer_trie(sg_fr)); PREFETCH_OP(PREG); *--YENV = 0; /* vars_arity */ - *--YENV = 0; /* heap_arity */ +#ifndef GLOBAL_TRIE + *--YENV = 0; /* heap_arity */ +#endif /* GLOBAL_TRIE */ GONext(); } } diff --git a/OPTYap/tab.macros.h b/OPTYap/tab.macros.h index 91da4953c..a0df775b3 100644 --- a/OPTYap/tab.macros.h +++ b/OPTYap/tab.macros.h @@ -28,8 +28,8 @@ STD_PROTO(static inline void unbind_variables, (tr_fr_ptr, tr_fr_ptr)); STD_PROTO(static inline void rebind_variables, (tr_fr_ptr, tr_fr_ptr)); STD_PROTO(static inline void restore_bindings, (tr_fr_ptr, tr_fr_ptr)); STD_PROTO(static inline void abolish_incomplete_subgoals, (choiceptr)); -STD_PROTO(static inline void free_subgoal_hash_chain, (sg_hash_ptr)); -STD_PROTO(static inline void free_answer_hash_chain, (ans_hash_ptr)); +STD_PROTO(static inline void free_subgoal_trie_hash_chain, (sg_hash_ptr)); +STD_PROTO(static inline void free_answer_trie_hash_chain, (ans_hash_ptr)); STD_PROTO(static inline choiceptr freeze_current_cp, (void)); STD_PROTO(static inline void resume_frozen_cp, (choiceptr)); @@ -61,6 +61,10 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p ** Defines ** ** ----------------- */ +#define SHOW_MODE_STRUCTURE 0 +#define SHOW_MODE_STATISTICS 1 +#define TRAVERSE_TYPE_SUBGOAL 0 +#define TRAVERSE_TYPE_ANSWER 1 #define TRAVERSE_MODE_NORMAL 0 #define TRAVERSE_MODE_FLOAT 1 #define TRAVERSE_MODE_FLOAT2 2 @@ -91,9 +95,6 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p #define IS_BATCHED_GEN_CP(CP) (GEN_CP(CP)->cp_dep_fr == NULL) #endif /* DETERMINISTIC_TABLING */ -#define TAG_AS_ANSWER_LEAF_NODE(NODE) TrNode_parent(NODE) = (ans_node_ptr)((unsigned long int)TrNode_parent(NODE) | 0x1) -#define UNTAG_ANSWER_LEAF_NODE(NODE) ((ans_node_ptr)((unsigned long int)NODE & ~(0x1))) -#define IS_ANSWER_LEAF_NODE(NODE) ((unsigned long int)TrNode_parent(NODE) & 0x1) #define STACK_NOT_EMPTY(STACK, STACK_BASE) STACK != STACK_BASE #define STACK_PUSH_UP(ITEM, STACK) *--STACK = (CELL)(ITEM) @@ -101,8 +102,8 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p #define STACK_PUSH_DOWN(ITEM, STACK) *STACK++ = (CELL)(ITEM) #define STACK_POP_UP(STACK) *--STACK #ifdef YAPOR -#define STACK_CHECK_EXPAND(STACK, STACK_LIMIT, STACK_BASE) \ - if (STACK_LIMIT >= STACK) { \ +#define STACK_CHECK_EXPAND(STACK, STACK_LIMIT, STACK_BASE) \ + if (STACK_LIMIT >= STACK) { \ Yap_Error(INTERNAL_ERROR, TermNil, "stack full (STACK_CHECK_EXPAND)") #else #define STACK_CHECK_EXPAND(STACK, STACK_LIMIT, STACK_BASE) \ @@ -126,10 +127,30 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p #endif /* YAPOR */ +#ifdef GLOBAL_TRIE +#define INCREMENT_GLOBAL_TRIE_REFS(NODE) \ + { register gt_node_ptr gt_node = NODE; \ + TrNode_child(gt_node) = (gt_node_ptr) ((unsigned long int) TrNode_child(gt_node) + 1); \ + } +#define DECREMENT_GLOBAL_TRIE_REFS(NODE) \ + { register gt_node_ptr gt_node = NODE; \ + TrNode_child(gt_node) = (gt_node_ptr) ((unsigned long int) TrNode_child(gt_node) - 1); \ + if (TrNode_child(gt_node) == 0) \ + free_global_trie_branch(gt_node); \ + } +#else +#define INCREMENT_GLOBAL_TRIE_REFS(NODE) +#define DECREMENT_GLOBAL_TRIE_REFS(NODE) +#endif /* GLOBAL_TRIE */ +#define TAG_AS_ANSWER_LEAF_NODE(NODE) TrNode_parent(NODE) = (ans_node_ptr)((unsigned long int) TrNode_parent(NODE) | 0x1) +#define UNTAG_ANSWER_LEAF_NODE(NODE) ((ans_node_ptr)((unsigned long int) NODE & ~(0x1))) +#define IS_ANSWER_LEAF_NODE(NODE) ((unsigned long int) TrNode_parent(NODE) & 0x1) + + /* LowTagBits is 3 for 32 bit-machines and 7 for 64 bit-machines */ #define NumberOfLowTagBits (LowTagBits == 3 ? 2 : 3) #define MakeTableVarTerm(INDEX) (INDEX << NumberOfLowTagBits) -#define VarIndexOfTableTerm(TERM) (TERM >> NumberOfLowTagBits) +#define VarIndexOfTableTerm(TERM) (((unsigned int) TERM) >> NumberOfLowTagBits) #define VarIndexOfTerm(TERM) \ ((((CELL) TERM) - GLOBAL_table_var_enumerator(0)) / sizeof(CELL)) #define IsTableVarTerm(TERM) \ @@ -142,7 +163,8 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p #define CompactPairEndList AbsPair((Term *) (2*(LowTagBits + 1))) #endif /* TRIE_COMPACT_PAIRS */ -#define HASH_TABLE_LOCK(NODE) ((((unsigned long int)NODE) >> 5) & (TABLE_LOCK_BUCKETS - 1)) + +#define HASH_TABLE_LOCK(NODE) ((((unsigned long int) NODE) >> 5) & (TABLE_LOCK_BUCKETS - 1)) #define LOCK_TABLE(NODE) LOCK(GLOBAL_table_lock(HASH_TABLE_LOCK(NODE))) #define UNLOCK_TABLE(NODE) UNLOCK(GLOBAL_table_lock(HASH_TABLE_LOCK(NODE))) @@ -264,11 +286,11 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p #define new_subgoal_frame(SG_FR, CODE) \ { register ans_node_ptr ans_node; \ + new_root_answer_trie_node(ans_node); \ ALLOC_SUBGOAL_FRAME(SG_FR); \ INIT_LOCK(SgFr_lock(SG_FR)); \ SgFr_code(SG_FR) = CODE; \ SgFr_state(SG_FR) = ready; \ - new_answer_trie_node(ans_node, 0, 0, NULL, NULL, NULL); \ SgFr_hash_chain(SG_FR) = NULL; \ SgFr_answer_trie(SG_FR) = ans_node; \ SgFr_first_answer(SG_FR) = NULL; \ @@ -292,66 +314,102 @@ STD_PROTO(static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames, (tg_sol_fr_p DepFr_leader_cp(DEP_FR) = NORM_CP(LEADER_CP); \ DepFr_cons_cp(DEP_FR) = NORM_CP(CONS_CP); \ /* start with TrNode_child(DepFr_last_answer(DEP_FR)) pointing to SgFr_first_answer(SG_FR) */ \ - DepFr_last_answer(DEP_FR) = (ans_node_ptr)((unsigned long int)(SG_FR) + \ - (unsigned long int)(&SgFr_first_answer((sg_fr_ptr)DEP_FR)) - \ - (unsigned long int)(&TrNode_child((ans_node_ptr)DEP_FR))); \ + DepFr_last_answer(DEP_FR) = (ans_node_ptr) ((unsigned long int) (SG_FR) + \ + (unsigned long int) (&SgFr_first_answer((sg_fr_ptr)DEP_FR)) - \ + (unsigned long int) (&TrNode_child((ans_node_ptr)DEP_FR))); \ DepFr_next(DEP_FR) = NEXT -#define new_table_entry(TAB_ENT, PRED_ENTRY, ATOM, ARITY, SUBGOAL_TRIE) \ - ALLOC_TABLE_ENTRY(TAB_ENT); \ - TabEnt_init_lock_field(TAB_ENT); \ - TabEnt_pe(TAB_ENT) = PRED_ENTRY; \ - TabEnt_atom(TAB_ENT) = ATOM; \ - TabEnt_arity(TAB_ENT) = ARITY; \ - TabEnt_mode(TAB_ENT) = 0; \ - TabEnt_subgoal_trie(TAB_ENT) = SUBGOAL_TRIE; \ - TabEnt_hash_chain(TAB_ENT) = NULL; \ - TabEnt_next(TAB_ENT) = GLOBAL_root_tab_ent; \ - GLOBAL_root_tab_ent = TAB_ENT +#define new_table_entry(TAB_ENT, PRED_ENTRY, ATOM, ARITY) \ + { register sg_node_ptr sg_node; \ + new_root_subgoal_trie_node(sg_node); \ + ALLOC_TABLE_ENTRY(TAB_ENT); \ + TabEnt_init_lock_field(TAB_ENT); \ + TabEnt_pe(TAB_ENT) = PRED_ENTRY; \ + TabEnt_atom(TAB_ENT) = ATOM; \ + TabEnt_arity(TAB_ENT) = ARITY; \ + TabEnt_mode(TAB_ENT) = 0; \ + TabEnt_subgoal_trie(TAB_ENT) = sg_node; \ + TabEnt_hash_chain(TAB_ENT) = NULL; \ + TabEnt_next(TAB_ENT) = GLOBAL_root_tab_ent; \ + GLOBAL_root_tab_ent = TAB_ENT; \ + } -#define new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) \ - ALLOC_SUBGOAL_TRIE_NODE(NODE); \ - TrNode_entry(NODE) = ENTRY; \ - TrNode_init_lock_field(NODE); \ - TrNode_child(NODE) = CHILD; \ - TrNode_parent(NODE) = PARENT; \ +#define new_global_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) \ + ALLOC_GLOBAL_TRIE_NODE(NODE); \ + TrNode_entry(NODE) = ENTRY; \ + TrNode_child(NODE) = CHILD; \ + TrNode_parent(NODE) = PARENT; \ + TrNode_next(NODE) = NEXT + +#define new_root_subgoal_trie_node(NODE) \ + ALLOC_SUBGOAL_TRIE_NODE(NODE); \ + init_subgoal_trie_node(NODE, 0, NULL, NULL, NULL) +#define new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) \ + INCREMENT_GLOBAL_TRIE_REFS(ENTRY); \ + ALLOC_SUBGOAL_TRIE_NODE(NODE); \ + init_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) +#define init_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) \ + TrNode_entry(NODE) = ENTRY; \ + TrNode_init_lock_field(NODE); \ + TrNode_child(NODE) = CHILD; \ + TrNode_parent(NODE) = PARENT; \ + TrNode_next(NODE) = NEXT + + +#define new_root_answer_trie_node(NODE) \ + ALLOC_ANSWER_TRIE_NODE(NODE); \ + init_answer_trie_node(NODE, 0, 0, NULL, NULL, NULL) +#define new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ + INCREMENT_GLOBAL_TRIE_REFS(ENTRY); \ + ALLOC_ANSWER_TRIE_NODE(NODE); \ + init_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) +#define init_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ + TrNode_instr(NODE) = INSTR; \ + TrNode_entry(NODE) = ENTRY; \ + TrNode_init_lock_field(NODE); \ + TrNode_child(NODE) = CHILD; \ + TrNode_parent(NODE) = PARENT; \ TrNode_next(NODE) = NEXT -#define new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ - ALLOC_ANSWER_TRIE_NODE(NODE); \ - TrNode_instr(NODE) = INSTR; \ - TrNode_entry(NODE) = ENTRY; \ - TrNode_init_lock_field(NODE); \ - TrNode_child(NODE) = CHILD; \ - TrNode_parent(NODE) = PARENT; \ - TrNode_next(NODE) = NEXT +#define MAX_NODES_PER_TRIE_LEVEL 8 +#define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2) +#define BASE_HASH_BUCKETS 64 +#define HASH_ENTRY(ENTRY, SEED) ((((unsigned long int) ENTRY) >> NumberOfLowTagBits) & (SEED)) +#ifdef GLOBAL_TRIE +#define GLOBAL_TRIE_HASH_MARK ((Term) MakeTableVarTerm(MAX_TABLE_VARS)) +#define IS_GLOBAL_TRIE_HASH(NODE) (TrNode_entry(NODE) == GLOBAL_TRIE_HASH_MARK) +#define SUBGOAL_TRIE_HASH_MARK (NULL) +#else +#define SUBGOAL_TRIE_HASH_MARK ((Term) MakeTableVarTerm(MAX_TABLE_VARS)) +#endif /* GLOBAL_TRIE */ +#define IS_SUBGOAL_TRIE_HASH(NODE) (TrNode_entry(NODE) == SUBGOAL_TRIE_HASH_MARK) +#define ANSWER_TRIE_HASH_MARK 0 +#define IS_ANSWER_TRIE_HASH(NODE) (TrNode_instr(NODE) == ANSWER_TRIE_HASH_MARK) -#define MAX_NODES_PER_TRIE_LEVEL 8 -#define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2) -#define BASE_HASH_BUCKETS 64 -#define SUBGOAL_HASH_MARK ((Term) MakeTableVarTerm(MAX_TABLE_VARS)) -#define ANSWER_HASH_MARK 0 -#define IS_SUBGOAL_HASH(NODE) (TrNode_entry(NODE) == SUBGOAL_HASH_MARK) -#define IS_ANSWER_HASH(NODE) (TrNode_instr(NODE) == ANSWER_HASH_MARK) -#define HASH_TERM(TERM, SEED) (((TERM) >> NumberOfLowTagBits) & (SEED)) +#define new_global_trie_hash(HASH, NUM_NODES) \ + ALLOC_GLOBAL_TRIE_HASH(HASH); \ + Hash_mark(HASH) = GLOBAL_TRIE_HASH_MARK; \ + Hash_num_buckets(HASH) = BASE_HASH_BUCKETS; \ + ALLOC_HASH_BUCKETS(Hash_buckets(HASH), BASE_HASH_BUCKETS); \ + Hash_num_nodes(HASH) = NUM_NODES -#define new_subgoal_hash(HASH, NUM_NODES, TAB_ENT) \ - ALLOC_SUBGOAL_HASH(HASH); \ - Hash_mark(HASH) = SUBGOAL_HASH_MARK; \ +#define new_subgoal_trie_hash(HASH, NUM_NODES, TAB_ENT) \ + ALLOC_SUBGOAL_TRIE_HASH(HASH); \ + Hash_mark(HASH) = SUBGOAL_TRIE_HASH_MARK; \ Hash_num_buckets(HASH) = BASE_HASH_BUCKETS; \ ALLOC_HASH_BUCKETS(Hash_buckets(HASH), BASE_HASH_BUCKETS); \ Hash_num_nodes(HASH) = NUM_NODES; \ SgHash_init_next_field(HASH, TAB_ENT) -#define new_answer_hash(HASH, NUM_NODES, SG_FR) \ - ALLOC_ANSWER_HASH(HASH); \ - Hash_mark(HASH) = ANSWER_HASH_MARK; \ +#define new_answer_trie_hash(HASH, NUM_NODES, SG_FR) \ + ALLOC_ANSWER_TRIE_HASH(HASH); \ + Hash_mark(HASH) = ANSWER_TRIE_HASH_MARK; \ Hash_num_buckets(HASH) = BASE_HASH_BUCKETS; \ ALLOC_HASH_BUCKETS(Hash_buckets(HASH), BASE_HASH_BUCKETS); \ Hash_num_nodes(HASH) = NUM_NODES; \ @@ -621,7 +679,7 @@ void abolish_incomplete_subgoals(choiceptr prune_cp) { #else ans_node_ptr node; SgFr_state(sg_fr) = ready; - free_answer_hash_chain(SgFr_hash_chain(sg_fr)); + free_answer_trie_hash_chain(SgFr_hash_chain(sg_fr)); SgFr_hash_chain(sg_fr) = NULL; SgFr_first_answer(sg_fr) = NULL; SgFr_last_answer(sg_fr) = NULL; @@ -641,7 +699,7 @@ void abolish_incomplete_subgoals(choiceptr prune_cp) { static inline -void free_subgoal_hash_chain(sg_hash_ptr hash) { +void free_subgoal_trie_hash_chain(sg_hash_ptr hash) { while (hash) { sg_node_ptr chain_node, *bucket, *last_bucket; sg_hash_ptr next_hash; @@ -662,7 +720,7 @@ void free_subgoal_hash_chain(sg_hash_ptr hash) { } next_hash = Hash_next(hash); FREE_HASH_BUCKETS(Hash_buckets(hash)); - FREE_SUBGOAL_HASH(hash); + FREE_SUBGOAL_TRIE_HASH(hash); hash = next_hash; } return; @@ -670,7 +728,7 @@ void free_subgoal_hash_chain(sg_hash_ptr hash) { static inline -void free_answer_hash_chain(ans_hash_ptr hash) { +void free_answer_trie_hash_chain(ans_hash_ptr hash) { while (hash) { ans_node_ptr chain_node, *bucket, *last_bucket; ans_hash_ptr next_hash; @@ -691,7 +749,7 @@ void free_answer_hash_chain(ans_hash_ptr hash) { } next_hash = Hash_next(hash); FREE_HASH_BUCKETS(Hash_buckets(hash)); - FREE_ANSWER_HASH(hash); + FREE_ANSWER_TRIE_HASH(hash); hash = next_hash; } return; diff --git a/OPTYap/tab.structs.h b/OPTYap/tab.structs.h index b66362743..0e840f8a4 100644 --- a/OPTYap/tab.structs.h +++ b/OPTYap/tab.structs.h @@ -62,7 +62,7 @@ typedef struct table_entry { int pred_arity; int mode_flags; struct subgoal_trie_node *subgoal_trie; - struct subgoal_hash *hash_chain; + struct subgoal_trie_hash *hash_chain; struct table_entry *next; } *tab_ent_ptr; @@ -77,12 +77,25 @@ typedef struct table_entry { -/* -------------------------------------------------------- ** -** Structs subgoal_trie_node and answer_trie_node ** -** -------------------------------------------------------- */ +/* -------------------------------------------------------------------------- ** +** Structs global_trie_node, subgoal_trie_node and answer_trie_node ** +** -------------------------------------------------------------------------- */ + +#ifdef GLOBAL_TRIE +typedef struct global_trie_node { + Term entry; + struct global_trie_node *parent; + struct global_trie_node *child; + struct global_trie_node *next; +} *gt_node_ptr; +#endif /* GLOBAL_TRIE */ typedef struct subgoal_trie_node { +#ifdef GLOBAL_TRIE + struct global_trie_node *entry; +#else Term entry; +#endif /* GLOBAL_TRIE */ #ifdef TABLE_LOCK_AT_NODE_LEVEL lockvar lock; #endif /* TABLE_LOCK_AT_NODE_LEVEL */ @@ -96,7 +109,11 @@ typedef struct answer_trie_node { #ifdef YAPOR int or_arg; /* u.Otapl.or_arg */ #endif /* YAPOR */ +#ifdef GLOBAL_TRIE + struct global_trie_node *entry; +#else Term entry; +#endif /* GLOBAL_TRIE */ #ifdef TABLE_LOCK_AT_NODE_LEVEL lockvar lock; #endif /* TABLE_LOCK_AT_NODE_LEVEL */ @@ -116,28 +133,43 @@ typedef struct answer_trie_node { -/* ---------------------------------------------- ** -** Structs subgoal_hash and answer_hash ** -** ---------------------------------------------- */ +/* -------------------------------------------------------------------------- ** +** Structs global_trie_hash, subgoal_trie_hash and answer_trie_hash ** +** -------------------------------------------------------------------------- */ -typedef struct subgoal_hash { +#ifdef GLOBAL_TRIE +typedef struct global_trie_hash { + /* the first field is used for compatibility ** + ** with the global_trie_node data structure */ + Term mark; + int number_of_buckets; + struct global_trie_node **buckets; + int number_of_nodes; +} *gt_hash_ptr; +#endif /* GLOBAL_TRIE */ + +typedef struct subgoal_trie_hash { /* the first field is used for compatibility ** ** with the subgoal_trie_node data structure */ +#ifdef GLOBAL_TRIE + struct global_trie_node *mark; +#else Term mark; +#endif /* GLOBAL_TRIE */ int number_of_buckets; struct subgoal_trie_node **buckets; int number_of_nodes; - struct subgoal_hash *next; + struct subgoal_trie_hash *next; } *sg_hash_ptr; -typedef struct answer_hash { +typedef struct answer_trie_hash { /* the first field is used for compatibility ** ** with the answer_trie_node data structure */ OPCODE mark; int number_of_buckets; struct answer_trie_node **buckets; int number_of_nodes; - struct answer_hash *next; + struct answer_trie_hash *next; } *ans_hash_ptr; #define Hash_mark(X) ((X)->mark) @@ -171,7 +203,7 @@ typedef struct subgoal_frame { compiled_in_use = 6 /* LIMIT_TABLING */ } state_flag; /* do not change order !!! */ choiceptr generator_choice_point; - struct answer_hash *hash_chain; + struct answer_trie_hash *hash_chain; struct answer_trie_node *answer_trie; struct answer_trie_node *first_answer; struct answer_trie_node *last_answer; @@ -212,7 +244,7 @@ typedef struct subgoal_frame { SgFr_arity the arity of the subgoal. SgFr_state: a flag that indicates the subgoal state. SgFr_gen_cp: a pointer to the correspondent generator choice point. - SgFr_hash_chain: a pointer to the first answer_hash struct for the subgoal in hand. + SgFr_hash_chain: a pointer to the first answer_trie_hash struct for the subgoal in hand. SgFr_answer_trie: a pointer to the top answer trie node. It is used to check for/insert new answers. SgFr_first_answer: a pointer to the bottom answer trie node of the first available answer. diff --git a/OPTYap/tab.tries.c b/OPTYap/tab.tries.c index 6e9edbecb..db058fbd7 100644 --- a/OPTYap/tab.tries.c +++ b/OPTYap/tab.tries.c @@ -31,15 +31,22 @@ #ifdef YAPOR #ifdef TABLING_INNER_CUTS -static int update_answer_trie_branch(ans_node_ptr previous_node, ans_node_ptr node); +static int update_answer_trie_branch(ans_node_ptr previous_node, ans_node_ptr current_node); #else -static int update_answer_trie_branch(ans_node_ptr node); +static int update_answer_trie_branch(ans_node_ptr current_node); #endif /* TABLING_INNER_CUTS */ #else -static void update_answer_trie_branch(ans_node_ptr node, int position); +static void update_answer_trie_branch(ans_node_ptr current_node, int position); #endif /* YAPOR */ -static void traverse_subgoal_trie(sg_node_ptr sg_node, char *str, int str_index, int *arity, int depth, int mode, int position); -static void traverse_answer_trie(ans_node_ptr ans_node, char *str, int str_index, int *arity, int var_index, int depth, int mode, int position); +static void traverse_subgoal_trie(sg_node_ptr current_node, char *str, int str_index, int *arity, int mode, int position); +static void traverse_answer_trie(ans_node_ptr current_node, char *str, int str_index, int *arity, int var_index, int mode, int position); +static void traverse_trie_node(Term t, char *str, int *str_index_ptr, int *arity, int *mode_ptr, int type); +#ifdef GLOBAL_TRIE +static void free_global_trie_branch(gt_node_ptr current_node); +static void traverse_global_trie(gt_node_ptr current_node, char *str, int str_index, int *arity, int mode, int position); +static void traverse_global_trie_for_subgoal(gt_node_ptr current_node, char *str, int *str_index, int *arity, int *mode); +static void traverse_global_trie_for_answer(gt_node_ptr current_node, char *str, int *str_index, int *arity, int *mode); +#endif /* GLOBAL_TRIE */ @@ -47,8 +54,15 @@ static void traverse_answer_trie(ans_node_ptr ans_node, char *str, int str_index ** Local inlines ** ** ----------------------- */ +#ifdef GLOBAL_TRIE +STD_PROTO(static inline gt_node_ptr global_trie_node_check_insert, (gt_node_ptr, Term)); +STD_PROTO(static inline sg_node_ptr subgoal_trie_node_check_insert, (tab_ent_ptr, sg_node_ptr, gt_node_ptr)); +STD_PROTO(static inline ans_node_ptr answer_trie_node_check_insert, (sg_fr_ptr, ans_node_ptr, gt_node_ptr, int)); +#else STD_PROTO(static inline sg_node_ptr subgoal_trie_node_check_insert, (tab_ent_ptr, sg_node_ptr, Term)); STD_PROTO(static inline ans_node_ptr answer_trie_node_check_insert, (sg_fr_ptr, ans_node_ptr, Term, int)); +#endif /* GLOBAL_TRIE */ + #if defined(TABLE_LOCK_AT_WRITE_LEVEL) #define LOCK_NODE(NODE) LOCK_TABLE(NODE) @@ -62,6 +76,19 @@ STD_PROTO(static inline ans_node_ptr answer_trie_node_check_insert, (sg_fr_ptr, #endif /* TABLE_LOCK_LEVEL */ +#ifdef GLOBAL_TRIE +#define SUBGOAL_TOKEN_CHECK_INSERT(TAB_ENT, NODE, TOKEN) \ + NODE = global_trie_node_check_insert(NODE, TOKEN) +#define ANSWER_TOKEN_CHECK_INSERT(SG_FR, NODE, TOKEN, INSTR) \ + NODE = global_trie_node_check_insert(NODE, TOKEN) +#else +#define SUBGOAL_TOKEN_CHECK_INSERT(TAB_ENT, NODE, TOKEN) \ + NODE = subgoal_trie_node_check_insert(TAB_ENT, NODE, TOKEN) +#define ANSWER_TOKEN_CHECK_INSERT(SG_FR, NODE, TOKEN, INSTR) \ + NODE = answer_trie_node_check_insert(SG_FR, NODE, TOKEN, INSTR) +#endif /* GLOBAL_TRIE */ + + #ifdef TABLE_LOCK_AT_WRITE_LEVEL static inline sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr parent_node, Term t) { @@ -77,13 +104,13 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare LOCK_NODE(parent_node); if (TrNode_child(parent_node)) { sg_node_ptr chain_node = TrNode_child(parent_node); - if (IS_SUBGOAL_HASH(chain_node)) { + if (IS_SUBGOAL_TRIE_HASH(chain_node)) { #ifdef ALLOC_BEFORE_CHECK FREE_SUBGOAL_TRIE_NODE(child_node); #endif /* ALLOC_BEFORE_CHECK */ UNLOCK_NODE(parent_node); hash = (sg_hash_ptr) chain_node; - goto subgoal_hash; + goto subgoal_trie_hash; } do { if (TrNode_entry(chain_node) == t) { @@ -108,7 +135,7 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare return child_node; } - if (! IS_SUBGOAL_HASH(child_node)) { + if (! IS_SUBGOAL_TRIE_HASH(child_node)) { sg_node_ptr first_node = child_node; int count_nodes = 0; do { @@ -124,13 +151,13 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare LOCK_NODE(parent_node); if (first_node != TrNode_child(parent_node)) { sg_node_ptr chain_node = TrNode_child(parent_node); - if (IS_SUBGOAL_HASH(chain_node)) { + if (IS_SUBGOAL_TRIE_HASH(chain_node)) { #ifdef ALLOC_BEFORE_CHECK FREE_SUBGOAL_TRIE_NODE(child_node); #endif /* ALLOC_BEFORE_CHECK */ UNLOCK_NODE(parent_node); hash = (sg_hash_ptr) chain_node; - goto subgoal_hash; + goto subgoal_trie_hash; } do { if (TrNode_entry(chain_node) == t) { @@ -151,13 +178,14 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare new_subgoal_trie_node(child_node, t, NULL, parent_node, first_node); #endif /* ALLOC_BEFORE_CHECK */ } + count_nodes++; if (count_nodes >= MAX_NODES_PER_TRIE_LEVEL) { /* alloc a new hash */ sg_node_ptr chain_node, next_node, *bucket; - new_subgoal_hash(hash, count_nodes, tab_ent); + new_subgoal_trie_hash(hash, count_nodes, tab_ent); chain_node = child_node; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -172,13 +200,13 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare } hash = (sg_hash_ptr) child_node; -subgoal_hash: +subgoal_trie_hash: { /* trie nodes with hashing */ sg_node_ptr *bucket, first_node; int seed, count_nodes = 0; seed = Hash_seed(hash); - bucket = Hash_bucket(hash, HASH_TERM(t, seed)); + bucket = Hash_bucket(hash, HASH_ENTRY(t, seed)); first_node = child_node = *bucket; while (child_node) { if (TrNode_entry(child_node) == t) { @@ -197,7 +225,7 @@ subgoal_hash: FREE_SUBGOAL_TRIE_NODE(child_node); #endif /* ALLOC_BEFORE_CHECK */ UNLOCK_NODE(parent_node); - goto subgoal_hash; + goto subgoal_trie_hash; } if (first_node != *bucket) { sg_node_ptr chain_node = *bucket; @@ -222,6 +250,7 @@ subgoal_hash: } *bucket = child_node; Hash_num_nodes(hash)++; + count_nodes++; if (count_nodes >= MAX_NODES_PER_BUCKET && Hash_num_nodes(hash) > Hash_num_buckets(hash)) { /* expand current hash */ sg_node_ptr chain_node, next_node, *first_old_bucket, *old_bucket; @@ -229,12 +258,12 @@ subgoal_hash: old_bucket = first_old_bucket + Hash_num_buckets(hash); Hash_num_buckets(hash) *= 2; ALLOC_HASH_BUCKETS(Hash_buckets(hash), Hash_num_buckets(hash)); - seed = Hash_num_buckets(hash) - 1; + seed = Hash_seed(hash); do { if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), seed)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), seed)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -269,13 +298,13 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ LOCK_NODE(parent_node); if (TrNode_child(parent_node)) { ans_node_ptr chain_node = TrNode_child(parent_node); - if (IS_ANSWER_HASH(chain_node)) { + if (IS_ANSWER_TRIE_HASH(chain_node)) { #ifdef ALLOC_BEFORE_CHECK FREE_ANSWER_TRIE_NODE(child_node); #endif /* ALLOC_BEFORE_CHECK */ UNLOCK_NODE(parent_node); hash = (ans_hash_ptr) chain_node; - goto answer_hash; + goto answer_trie_hash; } do { if (TrNode_entry(chain_node) == t) { @@ -300,7 +329,7 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ return child_node; } - if (! IS_ANSWER_HASH(child_node)) { + if (! IS_ANSWER_TRIE_HASH(child_node)) { ans_node_ptr first_node = child_node; int count_nodes = 0; do { @@ -316,13 +345,13 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ LOCK_NODE(parent_node); if (first_node != TrNode_child(parent_node)) { ans_node_ptr chain_node = TrNode_child(parent_node); - if (IS_ANSWER_HASH(chain_node)) { + if (IS_ANSWER_TRIE_HASH(chain_node)) { #ifdef ALLOC_BEFORE_CHECK FREE_ANSWER_TRIE_NODE(child_node); #endif /* ALLOC_BEFORE_CHECK */ UNLOCK_NODE(parent_node); hash = (ans_hash_ptr) chain_node; - goto answer_hash; + goto answer_trie_hash; } do { if (TrNode_entry(chain_node) == t) { @@ -343,13 +372,14 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ new_answer_trie_node(child_node, instr, t, NULL, parent_node, first_node); #endif /* ALLOC_BEFORE_CHECK */ } + count_nodes++; if (count_nodes >= MAX_NODES_PER_TRIE_LEVEL) { /* alloc a new hash */ ans_node_ptr chain_node, next_node, *bucket; - new_answer_hash(hash, count_nodes, sg_fr); + new_answer_trie_hash(hash, count_nodes, sg_fr); chain_node = child_node; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -364,13 +394,13 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ } hash = (ans_hash_ptr) child_node; -answer_hash: +answer_trie_hash: { /* trie nodes with hashing */ ans_node_ptr *bucket, first_node; int seed, count_nodes = 0; seed = Hash_seed(hash); - bucket = Hash_bucket(hash, HASH_TERM(t, seed)); + bucket = Hash_bucket(hash, HASH_ENTRY(t, seed)); first_node = child_node = *bucket; while (child_node) { if (TrNode_entry(child_node) == t) { @@ -389,7 +419,7 @@ answer_hash: FREE_ANSWER_TRIE_NODE(child_node); #endif /* ALLOC_BEFORE_CHECK */ UNLOCK_NODE(parent_node); - goto answer_hash; + goto answer_trie_hash; } if (first_node != *bucket) { ans_node_ptr chain_node = *bucket; @@ -414,6 +444,7 @@ answer_hash: } *bucket = child_node; Hash_num_nodes(hash)++; + count_nodes++; if (count_nodes >= MAX_NODES_PER_BUCKET && Hash_num_nodes(hash) > Hash_num_buckets(hash)) { /* expand current hash */ ans_node_ptr chain_node, next_node, *first_old_bucket, *old_bucket; @@ -421,12 +452,12 @@ answer_hash: old_bucket = first_old_bucket + Hash_num_buckets(hash); Hash_num_buckets(hash) *= 2; ALLOC_HASH_BUCKETS(Hash_buckets(hash), Hash_num_buckets(hash)); - seed = Hash_num_buckets(hash) - 1; + seed = Hash_seed(hash); do { if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), seed)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), seed)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -441,8 +472,109 @@ answer_hash: } } #else /* TABLE_LOCK_AT_ENTRY_LEVEL || TABLE_LOCK_AT_NODE_LEVEL || ! YAPOR */ +#ifdef GLOBAL_TRIE static inline +gt_node_ptr global_trie_node_check_insert(gt_node_ptr parent_node, Term t) { + gt_node_ptr child_node; + + LOCK_NODE(parent_node); + child_node = TrNode_child(parent_node); + + if (child_node == NULL) { + new_global_trie_node(child_node, t, NULL, parent_node, NULL); + TrNode_child(parent_node) = child_node; + UNLOCK_NODE(parent_node); + return child_node; + } + + if (! IS_GLOBAL_TRIE_HASH(child_node)) { + int count_nodes = 0; + do { + if (TrNode_entry(child_node) == t) { + UNLOCK_NODE(parent_node); + return child_node; + } + count_nodes++; + child_node = TrNode_next(child_node); + } while (child_node); + new_global_trie_node(child_node, t, NULL, parent_node, TrNode_child(parent_node)); + count_nodes++; + if (count_nodes >= MAX_NODES_PER_TRIE_LEVEL) { + /* alloc a new hash */ + gt_hash_ptr hash; + gt_node_ptr chain_node, next_node, *bucket; + new_global_trie_hash(hash, count_nodes); + chain_node = child_node; + do { + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); + next_node = TrNode_next(chain_node); + TrNode_next(chain_node) = *bucket; + *bucket = chain_node; + chain_node = next_node; + } while (chain_node); + TrNode_child(parent_node) = (gt_node_ptr) hash; + } else { + TrNode_child(parent_node) = child_node; + } + UNLOCK_NODE(parent_node); + return child_node; + } + + { /* trie nodes with hashing */ + gt_hash_ptr hash; + gt_node_ptr *bucket; + int count_nodes = 0; + hash = (gt_hash_ptr) child_node; + bucket = Hash_bucket(hash, HASH_ENTRY(t, Hash_seed(hash))); + child_node = *bucket; + while (child_node) { + if (TrNode_entry(child_node) == t) { + UNLOCK_NODE(parent_node); + return child_node; + } + count_nodes++; + child_node = TrNode_next(child_node); + } + new_global_trie_node(child_node, t, NULL, parent_node, *bucket); + *bucket = child_node; + Hash_num_nodes(hash)++; + count_nodes++; + if (count_nodes >= MAX_NODES_PER_BUCKET && Hash_num_nodes(hash) > Hash_num_buckets(hash)) { + /* expand current hash */ + gt_node_ptr chain_node, next_node, *first_old_bucket, *old_bucket; + int seed; + first_old_bucket = Hash_buckets(hash); + old_bucket = first_old_bucket + Hash_num_buckets(hash); + Hash_num_buckets(hash) *= 2; + ALLOC_HASH_BUCKETS(Hash_buckets(hash), Hash_num_buckets(hash)); + seed = Hash_seed(hash); + do { + if (*--old_bucket) { + chain_node = *old_bucket; + do { + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), seed)); + next_node = TrNode_next(chain_node); + TrNode_next(chain_node) = *bucket; + *bucket = chain_node; + chain_node = next_node; + } while (chain_node); + } + } while (old_bucket != first_old_bucket); + FREE_HASH_BUCKETS(first_old_bucket); + } + UNLOCK_NODE(parent_node); + return child_node; + } +} +#endif /* GLOBAL_TRIE */ + + +static inline +#ifdef GLOBAL_TRIE +sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr parent_node, gt_node_ptr t) { +#else sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr parent_node, Term t) { +#endif /* GLOBAL_TRIE */ sg_node_ptr child_node; LOCK_NODE(parent_node); @@ -455,7 +587,7 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare return child_node; } - if (! IS_SUBGOAL_HASH(child_node)) { + if (! IS_SUBGOAL_TRIE_HASH(child_node)) { int count_nodes = 0; do { if (TrNode_entry(child_node) == t) { @@ -466,14 +598,15 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare child_node = TrNode_next(child_node); } while (child_node); new_subgoal_trie_node(child_node, t, NULL, parent_node, TrNode_child(parent_node)); + count_nodes++; if (count_nodes >= MAX_NODES_PER_TRIE_LEVEL) { /* alloc a new hash */ sg_hash_ptr hash; sg_node_ptr chain_node, next_node, *bucket; - new_subgoal_hash(hash, count_nodes, tab_ent); + new_subgoal_trie_hash(hash, count_nodes, tab_ent); chain_node = child_node; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -491,8 +624,8 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare sg_hash_ptr hash; sg_node_ptr *bucket; int count_nodes = 0; - hash = (sg_hash_ptr) child_node; - bucket = Hash_bucket(hash, HASH_TERM(t, Hash_seed(hash))); + hash = (sg_hash_ptr) child_node; + bucket = Hash_bucket(hash, HASH_ENTRY(t, Hash_seed(hash))); child_node = *bucket; while (child_node) { if (TrNode_entry(child_node) == t) { @@ -505,6 +638,7 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare new_subgoal_trie_node(child_node, t, NULL, parent_node, *bucket); *bucket = child_node; Hash_num_nodes(hash)++; + count_nodes++; if (count_nodes >= MAX_NODES_PER_BUCKET && Hash_num_nodes(hash) > Hash_num_buckets(hash)) { /* expand current hash */ sg_node_ptr chain_node, next_node, *first_old_bucket, *old_bucket; @@ -513,12 +647,12 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare old_bucket = first_old_bucket + Hash_num_buckets(hash); Hash_num_buckets(hash) *= 2; ALLOC_HASH_BUCKETS(Hash_buckets(hash), Hash_num_buckets(hash)); - seed = Hash_num_buckets(hash) - 1; + seed = Hash_seed(hash); do { if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), seed)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), seed)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -534,8 +668,12 @@ sg_node_ptr subgoal_trie_node_check_insert(tab_ent_ptr tab_ent, sg_node_ptr pare } -static inline +static inline +#ifdef GLOBAL_TRIE +ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_node, gt_node_ptr t, int instr) { +#else ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_node, Term t, int instr) { +#endif /* GLOBAL_TRIE */ ans_node_ptr child_node; #ifdef TABLING_ERRORS @@ -553,7 +691,7 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ return child_node; } - if (! IS_ANSWER_HASH(child_node)) { + if (! IS_ANSWER_TRIE_HASH(child_node)) { int count_nodes = 0; do { if (TrNode_entry(child_node) == t) { @@ -564,14 +702,15 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ child_node = TrNode_next(child_node); } while (child_node); new_answer_trie_node(child_node, instr, t, NULL, parent_node, TrNode_child(parent_node)); + count_nodes++; if (count_nodes >= MAX_NODES_PER_TRIE_LEVEL) { /* alloc a new hash */ ans_hash_ptr hash; ans_node_ptr chain_node, next_node, *bucket; - new_answer_hash(hash, count_nodes, sg_fr); + new_answer_trie_hash(hash, count_nodes, sg_fr); chain_node = child_node; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS - 1)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -589,8 +728,8 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ ans_hash_ptr hash; ans_node_ptr *bucket; int count_nodes = 0; - hash = (ans_hash_ptr) child_node; - bucket = Hash_bucket(hash, HASH_TERM(t, Hash_seed(hash))); + hash = (ans_hash_ptr) child_node; + bucket = Hash_bucket(hash, HASH_ENTRY(t, Hash_seed(hash))); child_node = *bucket; while (child_node) { if (TrNode_entry(child_node) == t) { @@ -603,6 +742,7 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ new_answer_trie_node(child_node, instr, t, NULL, parent_node, *bucket); *bucket = child_node; Hash_num_nodes(hash)++; + count_nodes++; if (count_nodes >= MAX_NODES_PER_BUCKET && Hash_num_nodes(hash) > Hash_num_buckets(hash)) { /* expand current hash */ ans_node_ptr chain_node, next_node, *first_old_bucket, *old_bucket; @@ -611,12 +751,12 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ old_bucket = first_old_bucket + Hash_num_buckets(hash); Hash_num_buckets(hash) *= 2; ALLOC_HASH_BUCKETS(Hash_buckets(hash), Hash_num_buckets(hash)); - seed = Hash_num_buckets(hash) - 1; + seed = Hash_seed(hash); do { if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = Hash_bucket(hash, HASH_TERM(TrNode_entry(chain_node), seed)); + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(chain_node), seed)); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -641,9 +781,15 @@ ans_node_ptr answer_trie_node_check_insert(sg_fr_ptr sg_fr, ans_node_ptr parent_ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { int i, j, count_vars, arity; CELL *stack_vars, *stack_terms_limit, *stack_terms_base, *stack_terms; - sg_node_ptr current_sg_node; tab_ent_ptr tab_ent; sg_fr_ptr sg_fr; +#ifdef GLOBAL_TRIE + gt_node_ptr current_node; + sg_node_ptr current_sg_node; +#else + sg_node_ptr current_node; +#define current_sg_node current_node +#endif /* GLOBAL_TRIE */ arity = preg->u.Otapl.s; tab_ent = preg->u.Otapl.te; @@ -652,11 +798,13 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { stack_terms_limit = (CELL *)TR; stack_terms_base = stack_terms = (CELL *)Yap_TrailTop; current_sg_node = TabEnt_subgoal_trie(tab_ent); - #ifdef TABLE_LOCK_AT_ENTRY_LEVEL LOCK(TabEnt_lock(tab_ent)); #endif /* TABLE_LOCK_LEVEL */ for (i = 1; i <= arity; i++) { +#ifdef GLOBAL_TRIE + current_node = GLOBAL_root_gt; +#endif /* GLOBAL_TRIE */ STACK_CHECK_EXPAND(stack_terms, stack_terms_limit, stack_terms_base); STACK_PUSH_UP(Deref(XREGS[i]), stack_terms); do { @@ -664,7 +812,7 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { if (IsVarTerm(t)) { if (IsTableVarTerm(t)) { t = MakeTableVarTerm(VarIndexOfTerm(t)); - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, t); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, t); } else { if (count_vars == MAX_TABLE_VARS) Yap_Error(INTERNAL_ERROR, TermNil, "MAX_TABLE_VARS exceeded (subgoal_search)"); @@ -672,10 +820,10 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { *((CELL *)t) = GLOBAL_table_var_enumerator(count_vars); t = MakeTableVarTerm(count_vars); count_vars++; - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, t); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, t); } } else if (IsAtomOrIntTerm(t)) { - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, t); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, t); } else if (IsPairTerm(t)) { #ifdef TRIE_COMPACT_PAIRS CELL *aux = RepPair(t); @@ -685,7 +833,7 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { aux = RepPair(t); t = Deref(*(aux + 1)); if (t == TermNil) { - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, CompactPairEndList); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, CompactPairEndList); } else { /* STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2, stack_terms_base); */ /* STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** @@ -695,14 +843,14 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { } STACK_PUSH_UP(Deref(*aux), stack_terms); } else { - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, CompactPairEndTerm); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, CompactPairEndTerm); STACK_PUSH_UP(t, stack_terms); } } else { - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, CompactPairInit); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, CompactPairInit); t = Deref(*(aux + 1)); if (t == TermNil) { - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, CompactPairEndList); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, CompactPairEndList); } else { STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2, stack_terms_base); STACK_PUSH_UP(t, stack_terms); @@ -711,24 +859,30 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { STACK_PUSH_UP(Deref(*aux), stack_terms); } #else - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, AbsPair(NULL)); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, AbsPair(NULL)); STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1, stack_terms_base); STACK_PUSH_UP(Deref(*(RepPair(t) + 1)), stack_terms); STACK_PUSH_UP(Deref(*(RepPair(t))), stack_terms); #endif /* TRIE_COMPACT_PAIRS */ } else if (IsApplTerm(t)) { Functor f = FunctorOfTerm(t); - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, AbsAppl((Term *)f)); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, AbsAppl((Term *)f)); if (f == FunctorDouble) { volatile Float dbl = FloatOfTerm(t); volatile Term *t_dbl = (Term *)((void *) &dbl); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, *(t_dbl + 1)); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, *(t_dbl + 1)); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, *t_dbl); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, *t_dbl); +#ifdef GLOBAL_TRIE + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, AbsAppl((Term *)f)); +#endif /* GLOBAL_TRIE */ } else if (f == FunctorLongInt) { Int li = LongIntOfTerm(t); - current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, li); + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, li); +#ifdef GLOBAL_TRIE + SUBGOAL_TOKEN_CHECK_INSERT(tab_ent, current_node, AbsAppl((Term *)f)); +#endif /* GLOBAL_TRIE */ } else if (f == FunctorDBRef) { Yap_Error(INTERNAL_ERROR, TermNil, "unsupported type tag (FunctorDBRef in subgoal_search)"); } else if (f == FunctorBigInt) { @@ -742,6 +896,9 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { Yap_Error(INTERNAL_ERROR, TermNil, "unknown type tag (subgoal_search)"); } } while (STACK_NOT_EMPTY(stack_terms, stack_terms_base)); +#ifdef GLOBAL_TRIE + current_sg_node = subgoal_trie_node_check_insert(tab_ent, current_sg_node, current_node); +#endif /* GLOBAL_TRIE */ } #if defined(TABLE_LOCK_AT_NODE_LEVEL) LOCK(TrNode_lock(current_sg_node)); @@ -777,13 +934,22 @@ sg_fr_ptr subgoal_search(yamop *preg, CELL **Yaddr) { } return sg_fr; +#ifndef GLOBAL_TRIE +#undef current_sg_node +#endif /* GLOBAL_TRIE */ } ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { int i, j, count_vars, subs_arity; CELL *stack_vars, *stack_terms_base, *stack_terms; +#ifdef GLOBAL_TRIE + gt_node_ptr current_node; ans_node_ptr current_ans_node; +#else + ans_node_ptr current_node; +#define current_ans_node current_node +#endif /* GLOBAL_TRIE */ #ifdef TRIE_COMPACT_PAIRS int in_new_pair = 0; #else @@ -795,8 +961,10 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { stack_vars = (CELL *)TR; stack_terms_base = stack_terms = (CELL *)Yap_TrailTop; current_ans_node = SgFr_answer_trie(sg_fr); - for (i = subs_arity; i >= 1; i--) { +#ifdef GLOBAL_TRIE + current_node = GLOBAL_root_gt; +#endif /* GLOBAL_TRIE */ #ifdef TABLING_ERRORS if (IsNonVarTerm(*(subs_ptr + i))) TABLING_ERROR_MESSAGE("IsNonVarTem(*(subs_ptr + i)) (answer_search)"); @@ -809,7 +977,7 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { t = Deref(t); if (IsTableVarTerm(t)) { t = MakeTableVarTerm(VarIndexOfTerm(t)); - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, t, _trie_retry_val + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, t, _trie_retry_val + in_new_pair); } else { if (count_vars == MAX_TABLE_VARS) Yap_Error(INTERNAL_ERROR, TermNil, "MAX_TABLE_VARS exceeded (answer_search)"); @@ -817,13 +985,13 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { *((CELL *)t) = GLOBAL_table_var_enumerator(count_vars); t = MakeTableVarTerm(count_vars); count_vars++; - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, t, _trie_retry_var + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, t, _trie_retry_var + in_new_pair); } #ifdef TRIE_COMPACT_PAIRS in_new_pair = 0; #endif /* TRIE_COMPACT_PAIRS */ } else if (IsAtomOrIntTerm(t)) { - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, t, _trie_retry_atom + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, t, _trie_retry_atom + in_new_pair); #ifdef TRIE_COMPACT_PAIRS in_new_pair = 0; #endif /* TRIE_COMPACT_PAIRS */ @@ -836,7 +1004,7 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { aux = RepPair(t); t = Deref(*(aux + 1)); if (t == TermNil) { - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, CompactPairEndList, _trie_retry_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); } else { /* STACK_CHECK_EXPAND(stack_terms, stack_vars + 2, stack_terms_base); */ /* STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** @@ -847,14 +1015,14 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { } STACK_PUSH_UP(Deref(*aux), stack_terms); } else { - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, CompactPairEndTerm, _trie_retry_null); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, CompactPairEndTerm, _trie_retry_null); STACK_PUSH_UP(t, stack_terms); } } else { - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, CompactPairInit, _trie_retry_null + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, CompactPairInit, _trie_retry_null + in_new_pair); t = Deref(*(aux + 1)); if (t == TermNil) { - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, CompactPairEndList, _trie_retry_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); in_new_pair = 0; } else { STACK_CHECK_EXPAND(stack_terms, stack_vars + 2, stack_terms_base); @@ -865,7 +1033,7 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { STACK_PUSH_UP(Deref(*aux), stack_terms); } #else - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, AbsPair(NULL), _trie_retry_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, AbsPair(NULL), _trie_retry_pair); STACK_CHECK_EXPAND(stack_terms, stack_vars + 1, stack_terms_base); STACK_PUSH_UP(Deref(*(RepPair(t) + 1)), stack_terms); STACK_PUSH_UP(Deref(*(RepPair(t))), stack_terms); @@ -875,23 +1043,23 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { if (f == FunctorDouble) { volatile Float dbl = FloatOfTerm(t); volatile Term *t_dbl = (Term *)((void *) &dbl); - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, AbsAppl((Term *)f), _trie_retry_null + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_new_pair); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, *(t_dbl + 1), _trie_retry_extension); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, *(t_dbl + 1), _trie_retry_extension); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, *t_dbl, _trie_retry_extension); - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, AbsAppl((Term *)f), _trie_retry_float); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, *t_dbl, _trie_retry_extension); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_float); } else if (f == FunctorLongInt) { Int li = LongIntOfTerm (t); - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, AbsAppl((Term *)f), _trie_retry_null + in_new_pair); - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, li, _trie_retry_extension); - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, AbsAppl((Term *)f), _trie_retry_long); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, li, _trie_retry_extension); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_long); } else if (f == FunctorDBRef) { Yap_Error(INTERNAL_ERROR, TermNil, "unsupported type tag (FunctorDBRef in answer_search)"); } else if (f == FunctorBigInt) { Yap_Error(INTERNAL_ERROR, TermNil, "unsupported type tag (FunctorBigInt in answer_search)"); } else { - current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, AbsAppl((Term *)f), _trie_retry_struct + in_new_pair); + ANSWER_TOKEN_CHECK_INSERT(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_struct + in_new_pair); STACK_CHECK_EXPAND(stack_terms, stack_vars + ArityOfFunctor(f) - 1, stack_terms_base); for (j = ArityOfFunctor(f); j >= 1; j--) STACK_PUSH_UP(Deref(*(RepAppl(t) + j)), stack_terms); @@ -903,6 +1071,9 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { Yap_Error(INTERNAL_ERROR, TermNil, "unknown type tag (answer_search)"); } } while (STACK_NOT_EMPTY(stack_terms, stack_terms_base)); +#ifdef GLOBAL_TRIE + current_ans_node = answer_trie_node_check_insert(sg_fr, current_ans_node, current_node, _trie_retry_atom); +#endif /* GLOBAL_TRIE */ } /* reset variables */ @@ -912,13 +1083,24 @@ ans_node_ptr answer_search(sg_fr_ptr sg_fr, CELL *subs_ptr) { } return current_ans_node; +#ifndef GLOBAL_TRIE +#undef current_ans_node +#endif /* GLOBAL_TRIE */ +#ifndef TRIE_COMPACT_PAIRS +#undef in_new_pair +#endif /* TRIE_COMPACT_PAIRS */ } -void load_answer_trie(ans_node_ptr ans_node, CELL *subs_ptr) { +void load_answer(ans_node_ptr current_ans_node, CELL *subs_ptr) { CELL *stack_vars_base, *stack_vars, *stack_terms_base, *stack_terms; - int subs_arity, i, n_vars = -1; + int i, subs_arity, vars_arity = 0; Term t; +#ifdef GLOBAL_TRIE + gt_node_ptr current_node; +#else +#define current_node current_ans_node +#endif /* GLOBAL_TRIE */ #ifdef TRIE_COMPACT_PAIRS int stack_terms_pair_offset = 0; #endif /* TRIE_COMPACT_PAIRS */ @@ -928,32 +1110,184 @@ void load_answer_trie(ans_node_ptr ans_node, CELL *subs_ptr) { #ifdef TABLING_ERRORS if (H < H_FZ) - TABLING_ERROR_MESSAGE("H < H_FZ (load_answer_trie)"); + TABLING_ERROR_MESSAGE("H < H_FZ (load_answer)"); #endif /* TABLING_ERRORS */ stack_vars_base = stack_vars = (CELL *)TR; stack_terms_base = stack_terms = (CELL *)Yap_TrailTop; +#ifdef GLOBAL_TRIE + for (i = subs_arity; i >= 1; i--) { + current_node = TrNode_entry(current_ans_node); + current_ans_node = UNTAG_ANSWER_LEAF_NODE(TrNode_parent(current_ans_node)); + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); +#else + { + t = TrNode_entry(current_node); + current_node = UNTAG_ANSWER_LEAF_NODE(TrNode_parent(current_node)); +#endif /* GLOBAL_TRIE */ + do { + if (IsVarTerm(t)) { + int var_index = VarIndexOfTableTerm(t); + STACK_CHECK_EXPAND(stack_terms, stack_vars_base + var_index + 1, stack_terms_base); + if (var_index >= vars_arity) { + while (vars_arity < var_index) + stack_vars_base[vars_arity++] = 0; + stack_vars_base[vars_arity++] = MkVarTerm(); + stack_vars = stack_vars_base + vars_arity; + } 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)) { + STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + 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); + } else { /* CompactPairEndList / CompactPairEndTerm */ + Term last; + STACK_CHECK_EXPAND(stack_terms, stack_vars + 1, stack_terms_base); + 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 + Term head = STACK_POP_DOWN(stack_terms); + Term tail = STACK_POP_DOWN(stack_terms); + t = MkPairTerm(head, tail); + STACK_PUSH_UP(t, stack_terms); +#endif /* TRIE_COMPACT_PAIRS */ + } else if (IsApplTerm(t)) { + Functor f = (Functor) RepAppl(t); + if (f == FunctorDouble) { + volatile Float dbl; + volatile Term *t_dbl = (Term *)((void *) &dbl); + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + *t_dbl = t; +#if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + *(t_dbl + 1) = t; +#endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ + current_node = TrNode_parent(current_node); + t = MkFloatTerm(dbl); + STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_PUSH_UP(t, stack_terms); + } 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); + STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_PUSH_UP(t, stack_terms); + } else { + int f_arity = ArityOfFunctor(f); + t = Yap_MkApplTerm(f, f_arity, stack_terms); + stack_terms += f_arity; + STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_PUSH_UP(t, stack_terms); + } + } + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + } while (current_node); + } - t = TrNode_entry(ans_node); - ans_node = UNTAG_ANSWER_LEAF_NODE(TrNode_parent(ans_node)); + for (i = subs_arity; i >= 1; i--) { + CELL *subs_var = (CELL *) *(subs_ptr + i); + t = STACK_POP_DOWN(stack_terms); + Bind(subs_var, t); + } + +#ifdef TABLING_ERRORS + if (stack_terms != (CELL *)Yap_TrailTop) + TABLING_ERROR_MESSAGE("stack_terms != Yap_TrailTop (load_answer)"); +#endif /* TABLING_ERRORS */ + + return; +#ifndef GLOBAL_TRIE +#undef current_node +#endif /* GLOBAL_TRIE */ +} + + +#ifdef GLOBAL_TRIE +CELL *load_substitution_variable(gt_node_ptr current_node, CELL *aux_stack_ptr) { + CELL *subs_ptr, *stack_terms_top, *stack_terms_base, *stack_terms; + int vars_arity; + Term t; +#ifdef TRIE_COMPACT_PAIRS + int stack_terms_pair_offset = 0; +#endif /* TRIE_COMPACT_PAIRS */ + + vars_arity = (int) *aux_stack_ptr; + stack_terms_top = (CELL *) TR; + stack_terms_base = stack_terms = (CELL *) Yap_TrailTop; + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); do { if (IsVarTerm(t)) { int var_index = VarIndexOfTableTerm(t); - STACK_CHECK_EXPAND(stack_terms, stack_vars_base + var_index + 1, stack_terms_base); - if(var_index > n_vars) { - for (i = var_index; i > n_vars; i--) - stack_vars_base[i] = 0; - n_vars = var_index; - stack_vars = stack_vars_base + var_index; - } - if (stack_vars_base[var_index] == 0) - stack_vars_base[var_index] = MkVarTerm(); - STACK_PUSH_UP(stack_vars_base[var_index], stack_terms); + t = MkVarTerm(); + if (var_index >= vars_arity) { + while (vars_arity < var_index) { + *aux_stack_ptr-- = 0; + vars_arity++; + } + *aux_stack_ptr-- = t; + vars_arity++; + *aux_stack_ptr = vars_arity; + } else { + /* do the same as in macro stack_trie_val_instr() */ + CELL aux_sub, aux_var, *vars_ptr; + vars_ptr = aux_stack_ptr + vars_arity - var_index; + aux_sub = *((CELL *) t); + aux_var = *vars_ptr; + if (aux_var == 0) { + *vars_ptr = t; + } else { + if (aux_sub > aux_var) { + if ((CELL *) aux_sub <= H) { + Bind_Global((CELL *) aux_sub, aux_var); + } else if ((CELL *) aux_var <= H) { + Bind_Local((CELL *) aux_sub, aux_var); + } else { + Bind_Local((CELL *) aux_var, aux_sub); + *vars_ptr = aux_sub; + } + } else { + if ((CELL *) aux_var <= H) { + Bind_Global((CELL *) aux_var, aux_sub); + *vars_ptr = aux_sub; + } else if ((CELL *) aux_sub <= H) { + Bind_Local((CELL *) aux_var, aux_sub); + *vars_ptr = aux_sub; + } else { + Bind_Local((CELL *) aux_sub, aux_var); + } + } + } + } + STACK_CHECK_EXPAND(stack_terms, stack_terms_top, stack_terms_base); + STACK_PUSH_UP(t, stack_terms); } else if (IsAtomOrIntTerm(t)) { - STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_CHECK_EXPAND(stack_terms, stack_terms_top, stack_terms_base); STACK_PUSH_UP(t, stack_terms); } else if (IsPairTerm(t)) { #ifdef TRIE_COMPACT_PAIRS - if (t == CompactPairInit) { + 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)) { @@ -965,7 +1299,7 @@ void load_answer_trie(ans_node_ptr ans_node, CELL *subs_ptr) { STACK_PUSH_UP(tail, stack_terms); } else { /* CompactPairEndList / CompactPairEndTerm */ Term last; - STACK_CHECK_EXPAND(stack_terms, stack_vars + 1, stack_terms_base); + STACK_CHECK_EXPAND(stack_terms, stack_terms_top + 1, stack_terms_base); 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); @@ -984,50 +1318,51 @@ void load_answer_trie(ans_node_ptr ans_node, CELL *subs_ptr) { if (f == FunctorDouble) { volatile Float dbl; volatile Term *t_dbl = (Term *)((void *) &dbl); - t = TrNode_entry(ans_node); - ans_node = TrNode_parent(ans_node); + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); *t_dbl = t; #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - t = TrNode_entry(ans_node); - ans_node = TrNode_parent(ans_node); + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); *(t_dbl + 1) = t; #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - ans_node = TrNode_parent(ans_node); + current_node = TrNode_parent(current_node); t = MkFloatTerm(dbl); - STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_CHECK_EXPAND(stack_terms, stack_terms_top, stack_terms_base); STACK_PUSH_UP(t, stack_terms); } else if (f == FunctorLongInt) { - Int li = TrNode_entry(ans_node); - ans_node = TrNode_parent(ans_node); - ans_node = TrNode_parent(ans_node); + Int li = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + current_node = TrNode_parent(current_node); t = MkLongIntTerm(li); - STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_CHECK_EXPAND(stack_terms, stack_terms_top, stack_terms_base); STACK_PUSH_UP(t, stack_terms); } else { int f_arity = ArityOfFunctor(f); t = Yap_MkApplTerm(f, f_arity, stack_terms); stack_terms += f_arity; - STACK_CHECK_EXPAND(stack_terms, stack_vars, stack_terms_base); + STACK_CHECK_EXPAND(stack_terms, stack_terms_top, stack_terms_base); STACK_PUSH_UP(t, stack_terms); } } - t = TrNode_entry(ans_node); - ans_node = TrNode_parent(ans_node); - } while (ans_node); + t = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + } while (current_node); - for (i = subs_arity; i >= 1; i--) { - CELL *subs_var = (CELL *) *(subs_ptr + i); - t = STACK_POP_DOWN(stack_terms); - Bind(subs_var, t); - } + subs_ptr = aux_stack_ptr + vars_arity + 1; + *subs_ptr = *subs_ptr - 1; + subs_ptr += *subs_ptr + 1; + t = STACK_POP_DOWN(stack_terms); + Bind((CELL *) *subs_ptr, t); #ifdef TABLING_ERRORS if (stack_terms != (CELL *)Yap_TrailTop) - TABLING_ERROR_MESSAGE("stack_terms != Yap_TrailTop (load_answer_trie)"); + TABLING_ERROR_MESSAGE("stack_terms != Yap_TrailTop (load_substitution_variable)"); #endif /* TABLING_ERRORS */ - return; + return aux_stack_ptr; } +#endif /* GLOBAL_TRIE */ void private_completion(sg_fr_ptr sg_fr) { @@ -1067,7 +1402,12 @@ void private_completion(sg_fr_ptr sg_fr) { } -void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int nodes_extra, int position) { +#ifdef GLOBAL_TRIE +void free_subgoal_trie_branch(sg_node_ptr current_node, int nodes_left, int position) { + if (nodes_left != 1) + free_subgoal_trie_branch(TrNode_child(current_node), nodes_left - 1, TRAVERSE_POSITION_FIRST); +#else +void free_subgoal_trie_branch(sg_node_ptr current_node, int nodes_left, int nodes_extra, int position) { int current_nodes_left = 0, current_nodes_extra = 0; /* save current state if first sibling node */ @@ -1079,7 +1419,7 @@ void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int nodes_extra, if (nodes_extra) { #ifdef TRIE_COMPACT_PAIRS if (nodes_extra < 0) { - Term t = TrNode_entry(node); + Term t = TrNode_entry(current_node); if (IsPairTerm(t)) { if (t == CompactPairInit) nodes_extra--; @@ -1091,7 +1431,7 @@ void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int nodes_extra, if (--nodes_extra == 0) nodes_left--; } else { - Term t = TrNode_entry(node); + Term t = TrNode_entry(current_node); if (IsVarTerm(t) || IsAtomOrIntTerm(t)) nodes_left--; else if (IsPairTerm(t)) @@ -1116,12 +1456,13 @@ void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int nodes_extra, } } if (nodes_left) - free_subgoal_trie_branch(TrNode_child(node), nodes_left, nodes_extra, TRAVERSE_POSITION_FIRST); + free_subgoal_trie_branch(TrNode_child(current_node), nodes_left, nodes_extra, TRAVERSE_POSITION_FIRST); +#endif /* GLOBAL_TRIE */ else { sg_fr_ptr sg_fr; ans_node_ptr ans_node; - sg_fr = (sg_fr_ptr) TrNode_sg_fr(node); - free_answer_hash_chain(SgFr_hash_chain(sg_fr)); + sg_fr = (sg_fr_ptr) TrNode_sg_fr(current_node); + free_answer_trie_hash_chain(SgFr_hash_chain(sg_fr)); ans_node = SgFr_answer_trie(sg_fr); if (TrNode_child(ans_node)) free_answer_trie_branch(TrNode_child(ans_node), TRAVERSE_POSITION_FIRST); @@ -1133,61 +1474,73 @@ void free_subgoal_trie_branch(sg_node_ptr node, int nodes_left, int nodes_extra, } if (position == TRAVERSE_POSITION_FIRST) { - sg_node_ptr next = TrNode_next(node); - FREE_SUBGOAL_TRIE_NODE(node); + sg_node_ptr next_node = TrNode_next(current_node); + DECREMENT_GLOBAL_TRIE_REFS(TrNode_entry(current_node)); + FREE_SUBGOAL_TRIE_NODE(current_node); +#ifndef GLOBAL_TRIE /* restore the initial state */ nodes_left = current_nodes_left; nodes_extra = current_nodes_extra; - while (next) { - node = next; - next = TrNode_next(node); - free_subgoal_trie_branch(node, nodes_left, nodes_extra, TRAVERSE_POSITION_NEXT); +#endif /* GLOBAL_TRIE */ + while (next_node) { + current_node = next_node; + next_node = TrNode_next(current_node); +#ifdef GLOBAL_TRIE + free_subgoal_trie_branch(current_node, nodes_left, TRAVERSE_POSITION_NEXT); +#else + free_subgoal_trie_branch(current_node, nodes_left, nodes_extra, TRAVERSE_POSITION_NEXT); +#endif /* GLOBAL_TRIE */ } - } else - FREE_SUBGOAL_TRIE_NODE(node); + } else { + DECREMENT_GLOBAL_TRIE_REFS(TrNode_entry(current_node)); + FREE_SUBGOAL_TRIE_NODE(current_node); + } return; } -void free_answer_trie_branch(ans_node_ptr node, int position) { +void free_answer_trie_branch(ans_node_ptr current_node, int position) { #ifdef TABLING_INNER_CUTS - if (TrNode_child(node) && ! IS_ANSWER_LEAF_NODE(node)) + if (TrNode_child(current_node) && ! IS_ANSWER_LEAF_NODE(current_node)) #else - if (! IS_ANSWER_LEAF_NODE(node)) + if (! IS_ANSWER_LEAF_NODE(current_node)) #endif /* TABLING_INNER_CUTS */ - free_answer_trie_branch(TrNode_child(node), TRAVERSE_POSITION_FIRST); + free_answer_trie_branch(TrNode_child(current_node), TRAVERSE_POSITION_FIRST); if (position == TRAVERSE_POSITION_FIRST) { - ans_node_ptr next = TrNode_next(node); - FREE_ANSWER_TRIE_NODE(node); - while (next) { - node = next; - next = TrNode_next(node); - free_answer_trie_branch(node, TRAVERSE_POSITION_NEXT); + ans_node_ptr next_node = TrNode_next(current_node); + DECREMENT_GLOBAL_TRIE_REFS(TrNode_entry(current_node)); + FREE_ANSWER_TRIE_NODE(current_node); + while (next_node) { + current_node = next_node; + next_node = TrNode_next(current_node); + free_answer_trie_branch(current_node, TRAVERSE_POSITION_NEXT); } - } else - FREE_ANSWER_TRIE_NODE(node); + } else { + DECREMENT_GLOBAL_TRIE_REFS(TrNode_entry(current_node)); + FREE_ANSWER_TRIE_NODE(current_node); + } return; } void update_answer_trie(sg_fr_ptr sg_fr) { - ans_node_ptr node; + ans_node_ptr current_node; - free_answer_hash_chain(SgFr_hash_chain(sg_fr)); + free_answer_trie_hash_chain(SgFr_hash_chain(sg_fr)); SgFr_hash_chain(sg_fr) = NULL; SgFr_state(sg_fr) += 2; /* complete --> compiled : complete_in_use --> compiled_in_use */ - node = TrNode_child(SgFr_answer_trie(sg_fr)); - if (node) { + current_node = TrNode_child(SgFr_answer_trie(sg_fr)); + if (current_node) { #ifdef YAPOR - TrNode_instr(node) -= 1; + TrNode_instr(current_node) -= 1; #ifdef TABLING_INNER_CUTS - update_answer_trie_branch(NULL, node); + update_answer_trie_branch(NULL, current_node); #else - update_answer_trie_branch(node); + update_answer_trie_branch(current_node); #endif /* TABLING_INNER_CUTS */ #else /* TABLING */ - update_answer_trie_branch(node, TRAVERSE_POSITION_FIRST); + update_answer_trie_branch(current_node, TRAVERSE_POSITION_FIRST); #endif /* YAPOR */ } return; @@ -1199,57 +1552,56 @@ static struct trie_statistics{ long subgoals; long subgoals_incomplete; long subgoal_trie_nodes; - long subgoal_linear_nodes; - int subgoal_trie_max_depth; - int subgoal_trie_min_depth; long answers; - long answers_yes; - long answers_no; +#ifdef TABLING_INNER_CUTS long answers_pruned; +#endif /* TABLING_INNER_CUTS */ + long answers_true; + long answers_no; long answer_trie_nodes; - long answer_linear_nodes; - int answer_trie_max_depth; - int answer_trie_min_depth; +#ifdef GLOBAL_TRIE + long global_trie_terms; + long global_trie_nodes; +#endif /* GLOBAL_TRIE */ } trie_stats; + #define TrStat_show trie_stats.show #define TrStat_subgoals trie_stats.subgoals #define TrStat_sg_incomplete trie_stats.subgoals_incomplete #define TrStat_sg_nodes trie_stats.subgoal_trie_nodes -#define TrStat_sg_linear_nodes trie_stats.subgoal_linear_nodes -#define TrStat_sg_max_depth trie_stats.subgoal_trie_max_depth -#define TrStat_sg_min_depth trie_stats.subgoal_trie_min_depth #define TrStat_answers trie_stats.answers -#define TrStat_answers_yes trie_stats.answers_yes +#define TrStat_answers_true trie_stats.answers_true #define TrStat_answers_no trie_stats.answers_no -#define TrStat_ans_pruned trie_stats.answers_pruned +#define TrStat_answers_pruned trie_stats.answers_pruned #define TrStat_ans_nodes trie_stats.answer_trie_nodes -#define TrStat_ans_linear_nodes trie_stats.answer_linear_nodes -#define TrStat_ans_max_depth trie_stats.answer_trie_max_depth -#define TrStat_ans_min_depth trie_stats.answer_trie_min_depth +#define TrStat_gt_terms trie_stats.global_trie_terms +#define TrStat_gt_nodes trie_stats.global_trie_nodes +#define SHOW_TABLE_STRUCTURE(MESG, ARGS...) if (TrStat_show == SHOW_MODE_STRUCTURE) fprintf(Yap_stdout, MESG, ##ARGS) #define STR_ARRAY_SIZE 100000 #define ARITY_ARRAY_SIZE 10000 -#define SHOW_TABLE(MESG, ARGS...) if (TrStat_show) fprintf(Yap_stdout, MESG, ##ARGS) -void traverse_table(tab_ent_ptr tab_ent, int show_table) { - sg_node_ptr sg_node = TrNode_child(TabEnt_subgoal_trie(tab_ent)); +void show_table(tab_ent_ptr tab_ent, int show_mode) { + sg_node_ptr sg_node; - TrStat_show = show_table; - TrStat_subgoals = 0; - TrStat_sg_incomplete = 0; - TrStat_sg_nodes = 1; - TrStat_sg_linear_nodes = 0; - TrStat_sg_max_depth = -1; - TrStat_sg_min_depth = -1; - TrStat_answers = 0; - TrStat_answers_yes = 0; - TrStat_answers_no = 0; - TrStat_ans_pruned = 0; - TrStat_ans_nodes = 0; - TrStat_ans_linear_nodes = 0; - TrStat_ans_max_depth = -1; - TrStat_ans_min_depth = -1; + TrStat_show = show_mode; + if (show_mode == SHOW_MODE_STATISTICS) { + TrStat_subgoals = 0; + TrStat_sg_incomplete = 0; + TrStat_sg_nodes = 1; + TrStat_answers = 0; + TrStat_answers_true = 0; + TrStat_answers_no = 0; +#ifdef TABLING_INNER_CUTS + TrStat_answers_pruned = 0; +#endif /* TABLING_INNER_CUTS */ + TrStat_ans_nodes = 0; + fprintf(Yap_stdout, "Table statistics for predicate '%s/%d'\n", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); + } else { /* show_mode == SHOW_MODE_STRUCTURE */ + fprintf(Yap_stdout, "Table structure for predicate '%s/%d'\n", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); + } + sg_node = TrNode_child(TabEnt_subgoal_trie(tab_ent)); if (sg_node) { if (TabEnt_arity(tab_ent)) { char *str = (char *) malloc(sizeof(char) * STR_ARRAY_SIZE); @@ -1257,68 +1609,72 @@ void traverse_table(tab_ent_ptr tab_ent, int show_table) { int *arity = (int *) malloc(sizeof(int) * ARITY_ARRAY_SIZE); arity[0] = 1; arity[1] = TabEnt_arity(tab_ent); - traverse_subgoal_trie(sg_node, str, str_index, arity, 1, TRAVERSE_MODE_NORMAL, TRAVERSE_POSITION_FIRST); + traverse_subgoal_trie(sg_node, str, str_index, arity, TRAVERSE_MODE_NORMAL, TRAVERSE_POSITION_FIRST); free(str); free(arity); } else { sg_fr_ptr sg_fr = (sg_fr_ptr) sg_node; TrStat_subgoals++; - TrStat_sg_linear_nodes = TrStat_sg_min_depth = TrStat_sg_max_depth = 0; - SHOW_TABLE(" ?- %s.\n", AtomName(TabEnt_atom(tab_ent))); + SHOW_TABLE_STRUCTURE(" ?- %s.\n", AtomName(TabEnt_atom(tab_ent))); TrStat_ans_nodes++; - TrStat_ans_max_depth = TrStat_ans_min_depth = 0; if (SgFr_first_answer(sg_fr) == NULL) { if (SgFr_state(sg_fr) < complete) { TrStat_sg_incomplete++; - SHOW_TABLE(" ---> INCOMPLETE\n"); + SHOW_TABLE_STRUCTURE(" ---> INCOMPLETE\n"); } else { TrStat_answers_no++; - SHOW_TABLE(" NO\n"); + SHOW_TABLE_STRUCTURE(" NO\n"); } } else { /* SgFr_first_answer(sg_fr) == SgFr_answer_trie(sg_fr) */ - TrStat_answers_yes++; - TrStat_answers++; - SHOW_TABLE(" TRUE\n"); + TrStat_answers_true++; + SHOW_TABLE_STRUCTURE(" TRUE\n"); } } } else - SHOW_TABLE(" EMPTY\n"); + SHOW_TABLE_STRUCTURE(" EMPTY\n"); + if (show_mode == SHOW_MODE_STATISTICS) { + fprintf(Yap_stdout, " Subgoal trie structure\n"); + fprintf(Yap_stdout, " Subgoals: %ld (%ld incomplete)\n", TrStat_subgoals, TrStat_sg_incomplete); + fprintf(Yap_stdout, " Subgoal trie nodes: %ld\n", TrStat_sg_nodes); + fprintf(Yap_stdout, " Answer trie structure(s)\n"); +#ifdef TABLING_INNER_CUTS + fprintf(Yap_stdout, " Answers: %ld (%ld pruned)\n", TrStat_answers, TrStat_answers_pruned); +#else + fprintf(Yap_stdout, " Answers: %ld\n", TrStat_answers); +#endif /* TABLING_INNER_CUTS */ + fprintf(Yap_stdout, " Answers 'TRUE': %ld\n", TrStat_answers_true); + fprintf(Yap_stdout, " Answers 'NO': %ld\n", TrStat_answers_no); + fprintf(Yap_stdout, " Answer trie nodes: %ld\n", TrStat_ans_nodes); + fprintf(Yap_stdout, " Total memory in use: %ld bytes\n", + sizeof(struct table_entry) + TrStat_sg_nodes * sizeof(struct subgoal_trie_node) + + TrStat_ans_nodes * sizeof(struct answer_trie_node) + TrStat_subgoals * sizeof(struct subgoal_frame)); + } return; } -void table_stats(void) { - fprintf(Yap_stdout, "\n Subgoal trie structure"); - fprintf(Yap_stdout, "\n subgoals: %ld", TrStat_subgoals); - fprintf(Yap_stdout, "\n subgoals incomplete: %ld", TrStat_sg_incomplete); - fprintf(Yap_stdout, "\n nodes: %ld (%ld%c saving)", - TrStat_sg_nodes, - TrStat_sg_linear_nodes == 0 ? 0 : (TrStat_sg_linear_nodes - TrStat_sg_nodes + 1) * 100 / TrStat_sg_linear_nodes, - '%'); - fprintf(Yap_stdout, "\n average depth: %.2f (%d min - %d max)", - TrStat_subgoals == 0 ? 0 : (float)TrStat_sg_linear_nodes / (float)TrStat_subgoals, - TrStat_sg_min_depth < 0 ? 0 : TrStat_sg_min_depth, - TrStat_sg_max_depth < 0 ? 0 : TrStat_sg_max_depth); - fprintf(Yap_stdout, "\n Answer trie structure"); - fprintf(Yap_stdout, "\n answers: %ld", TrStat_answers); - fprintf(Yap_stdout, "\n yes answers: %ld", TrStat_answers_yes); - fprintf(Yap_stdout, "\n no answers: %ld", TrStat_answers_no); - fprintf(Yap_stdout, "\n pruned answers: %ld", TrStat_ans_pruned); - fprintf(Yap_stdout, "\n nodes: %ld (%ld%c saving)", - TrStat_ans_nodes, - TrStat_ans_linear_nodes == 0 ? 0 : (TrStat_ans_linear_nodes - TrStat_ans_nodes + TrStat_subgoals) * 100 / TrStat_ans_linear_nodes, - '%'); - fprintf(Yap_stdout, "\n average depth: %.2f (%d min - %d max)", - TrStat_answers == 0 ? 0 : (float)TrStat_ans_linear_nodes / (float)TrStat_answers, - TrStat_ans_min_depth < 0 ? 0 : TrStat_ans_min_depth, - TrStat_ans_max_depth < 0 ? 0 : TrStat_ans_max_depth); - fprintf(Yap_stdout, "\n Total memory in use\n %ld bytes\n", - sizeof(struct table_entry) + - TrStat_sg_nodes * sizeof(struct subgoal_trie_node) + - TrStat_ans_nodes * sizeof(struct answer_trie_node) + - TrStat_subgoals * sizeof(struct subgoal_frame)); +#ifdef GLOBAL_TRIE +void show_global_trie(void) { + TrStat_show = SHOW_MODE_STRUCTURE; + TrStat_gt_terms = 0; + TrStat_gt_nodes = 1; + fprintf(Yap_stdout, "Global trie structure\n"); + if (TrNode_child(GLOBAL_root_gt)) { + char *str = (char *) malloc(sizeof(char) * STR_ARRAY_SIZE); + int *arity = (int *) malloc(sizeof(int) * ARITY_ARRAY_SIZE); + arity[0] = 0; + traverse_global_trie(TrNode_child(GLOBAL_root_gt), str, 0, arity, TRAVERSE_MODE_NORMAL, TRAVERSE_POSITION_FIRST); + free(str); + free(arity); + } else + SHOW_TABLE_STRUCTURE(" EMPTY\n"); + fprintf(Yap_stdout, "Global trie statistics\n"); + fprintf(Yap_stdout, " Terms: %ld\n", TrStat_gt_terms); + fprintf(Yap_stdout, " Global trie nodes: %ld\n", TrStat_gt_nodes); + fprintf(Yap_stdout, " Total memory in use: %ld bytes\n", TrStat_gt_nodes * sizeof(struct global_trie_node)); return; } +#endif /* GLOBAL_TRIE */ @@ -1329,19 +1685,19 @@ void table_stats(void) { #ifdef YAPOR #ifdef TABLING_INNER_CUTS static -int update_answer_trie_branch(ans_node_ptr previous_node, ans_node_ptr node) { +int update_answer_trie_branch(ans_node_ptr previous_node, ans_node_ptr current_node) { int ltt; - if (! IS_ANSWER_LEAF_NODE(node)) { - if (TrNode_child(node)) { - TrNode_instr(TrNode_child(node)) -= 1; /* retry --> try */ - update_answer_trie_branch(NULL, TrNode_child(node)); - if (TrNode_child(node)) + if (! IS_ANSWER_LEAF_NODE(current_node)) { + if (TrNode_child(current_node)) { + TrNode_instr(TrNode_child(current_node)) -= 1; /* retry --> try */ + update_answer_trie_branch(NULL, TrNode_child(current_node)); + if (TrNode_child(current_node)) goto update_next_trie_branch; } /* node belonging to a pruned answer */ if (previous_node) { - TrNode_next(previous_node) = TrNode_next(node); - FREE_ANSWER_TRIE_NODE(node); + TrNode_next(previous_node) = TrNode_next(current_node); + FREE_ANSWER_TRIE_NODE(current_node); if (TrNode_next(previous_node)) { return update_answer_trie_branch(previous_node, TrNode_next(previous_node)); } else { @@ -1349,53 +1705,53 @@ int update_answer_trie_branch(ans_node_ptr previous_node, ans_node_ptr node) { return 0; } } else { - TrNode_child(TrNode_parent(node)) = TrNode_next(node); - if (TrNode_next(node)) { - TrNode_instr(TrNode_next(node)) -= 1; /* retry --> try */ - update_answer_trie_branch(NULL, TrNode_next(node)); + TrNode_child(TrNode_parent(current_node)) = TrNode_next(current_node); + if (TrNode_next(current_node)) { + TrNode_instr(TrNode_next(current_node)) -= 1; /* retry --> try */ + update_answer_trie_branch(NULL, TrNode_next(current_node)); } - FREE_ANSWER_TRIE_NODE(node); + FREE_ANSWER_TRIE_NODE(current_node); return 0; } } update_next_trie_branch: - if (TrNode_next(node)) { - ltt = 1 + update_answer_trie_branch(node, TrNode_next(node)); + if (TrNode_next(current_node)) { + ltt = 1 + update_answer_trie_branch(current_node, TrNode_next(current_node)); } else { - TrNode_instr(node) -= 2; /* retry --> trust : try --> do */ + TrNode_instr(current_node) -= 2; /* retry --> trust : try --> do */ ltt = 1; } - TrNode_or_arg(node) = ltt; - TrNode_instr(node) = Yap_opcode(TrNode_instr(node)); + TrNode_or_arg(current_node) = ltt; + TrNode_instr(current_node) = Yap_opcode(TrNode_instr(current_node)); return ltt; } #else static -int update_answer_trie_branch(ans_node_ptr node) { +int update_answer_trie_branch(ans_node_ptr current_node) { int ltt; - if (! IS_ANSWER_LEAF_NODE(node)) { - TrNode_instr(TrNode_child(node)) -= 1; /* retry --> try */ - update_answer_trie_branch(TrNode_child(node)); + if (! IS_ANSWER_LEAF_NODE(current_node)) { + TrNode_instr(TrNode_child(current_node)) -= 1; /* retry --> try */ + update_answer_trie_branch(TrNode_child(current_node)); } - if (TrNode_next(node)) { - ltt = 1 + update_answer_trie_branch(TrNode_next(node)); + if (TrNode_next(current_node)) { + ltt = 1 + update_answer_trie_branch(TrNode_next(current_node)); } else { - TrNode_instr(node) -= 2; /* retry --> trust : try --> do */ + TrNode_instr(current_node) -= 2; /* retry --> trust : try --> do */ ltt = 1; } - TrNode_or_arg(node) = ltt; - TrNode_instr(node) = Yap_opcode(TrNode_instr(node)); + TrNode_or_arg(current_node) = ltt; + TrNode_instr(current_node) = Yap_opcode(TrNode_instr(current_node)); return ltt; } #endif /* TABLING_INNER_CUTS */ #else /* TABLING */ static -void update_answer_trie_branch(ans_node_ptr node, int position) { - if (! IS_ANSWER_LEAF_NODE(node)) - update_answer_trie_branch(TrNode_child(node), TRAVERSE_POSITION_FIRST); /* retry --> try */ +void update_answer_trie_branch(ans_node_ptr current_node, int position) { + if (! IS_ANSWER_LEAF_NODE(current_node)) + update_answer_trie_branch(TrNode_child(current_node), TRAVERSE_POSITION_FIRST); /* retry --> try */ if (position == TRAVERSE_POSITION_FIRST) { - ans_node_ptr next = TrNode_next(node); + ans_node_ptr next = TrNode_next(current_node); if (next) { while (TrNode_next(next)) { update_answer_trie_branch(next, TRAVERSE_POSITION_NEXT); /* retry --> retry */ @@ -1405,29 +1761,28 @@ void update_answer_trie_branch(ans_node_ptr node, int position) { } else position += TRAVERSE_POSITION_LAST; /* try --> do */ } - TrNode_instr(node) = Yap_opcode(TrNode_instr(node) - position); + TrNode_instr(current_node) = Yap_opcode(TrNode_instr(current_node) - position); return; } #endif /* YAPOR */ static -void traverse_subgoal_trie(sg_node_ptr sg_node, char *str, int str_index, int *arity, int depth, int mode, int position) { +void traverse_subgoal_trie(sg_node_ptr current_node, char *str, int str_index, int *arity, int mode, int position) { int *current_arity = NULL, current_str_index = 0, current_mode = 0; - Term t; /* test if hashing */ - if (IS_SUBGOAL_HASH(sg_node)) { + if (IS_SUBGOAL_TRIE_HASH(current_node)) { sg_node_ptr *bucket, *last_bucket; sg_hash_ptr hash; - hash = (sg_hash_ptr) sg_node; + hash = (sg_hash_ptr) current_node; bucket = Hash_buckets(hash); last_bucket = bucket + Hash_num_buckets(hash); current_arity = (int *) malloc(sizeof(int) * (arity[0] + 1)); memcpy(current_arity, arity, sizeof(int) * (arity[0] + 1)); do { if (*bucket) { - traverse_subgoal_trie(*bucket, str, str_index, arity, depth, mode, TRAVERSE_POSITION_FIRST); + traverse_subgoal_trie(*bucket, str, str_index, arity, mode, TRAVERSE_POSITION_FIRST); memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); #ifdef TRIE_COMPACT_PAIRS if (arity[arity[0]] == -2 && str[str_index - 1] != '[') @@ -1450,264 +1805,51 @@ void traverse_subgoal_trie(sg_node_ptr sg_node, char *str, int str_index, int *a current_mode = mode; } - /* test the node type */ - t = TrNode_entry(sg_node); -#if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - if (mode == TRAVERSE_MODE_FLOAT) { - arity[0]++; - arity[arity[0]] = (int) t; - mode = TRAVERSE_MODE_FLOAT2; - } else if (mode == TRAVERSE_MODE_FLOAT2) { - volatile Float dbl; - volatile Term *t_dbl = (Term *)((void *) &dbl); - *t_dbl = t; - *(t_dbl + 1) = (Term) arity[arity[0]]; - arity[0]--; -#else /* SIZEOF_DOUBLE == SIZEOF_INT_P */ - if (mode == TRAVERSE_MODE_FLOAT) { - volatile Float dbl; - volatile Term *t_dbl = (Term *)((void *) &dbl); - *t_dbl = t; -#endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - str_index += sprintf(& str[str_index], "%.15g", dbl); - while (arity[0]) { - if (arity[arity[0]] > 0) { - arity[arity[0]]--; - if (arity[arity[0]] == 0) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; - } else { - str_index += sprintf(& str[str_index], ","); - break; - } - } else { - if (arity[arity[0]] == -2) { -#ifdef TRIE_COMPACT_PAIRS - str_index += sprintf(& str[str_index], ","); -#else - str_index += sprintf(& str[str_index], "|"); - arity[arity[0]] = -1; -#endif /* TRIE_COMPACT_PAIRS */ - break; - } else { - str_index += sprintf(& str[str_index], "]"); - arity[0]--; - } - } - } - mode = TRAVERSE_MODE_NORMAL; - } else if (mode == TRAVERSE_MODE_LONG) { - Int li = (Int) t; -#if SHORT_INTS - str_index += sprintf(& str[str_index], "%ld", li); -#else - str_index += sprintf(& str[str_index], "%d", li); -#endif /* SHORT_INTS */ - while (arity[0]) { - if (arity[arity[0]] > 0) { - arity[arity[0]]--; - if (arity[arity[0]] == 0) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; - } else { - str_index += sprintf(& str[str_index], ","); - break; - } - } else { - if (arity[arity[0]] == -2) { -#ifdef TRIE_COMPACT_PAIRS - str_index += sprintf(& str[str_index], ","); -#else - str_index += sprintf(& str[str_index], "|"); - arity[arity[0]] = -1; -#endif /* TRIE_COMPACT_PAIRS */ - break; - } else { - str_index += sprintf(& str[str_index], "]"); - arity[0]--; - } - } - } - mode = TRAVERSE_MODE_NORMAL; - } else if (IsVarTerm(t)) { -#if SHORT_INTS - str_index += sprintf(& str[str_index], "VAR%ld", VarIndexOfTableTerm(t)); -#else - str_index += sprintf(& str[str_index], "VAR%d", VarIndexOfTableTerm(t)); -#endif /* SHORT_INTS */ - while (arity[0]) { - if (arity[arity[0]] > 0) { - arity[arity[0]]--; - if (arity[arity[0]] == 0) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; - } else { - str_index += sprintf(& str[str_index], ","); - break; - } - } else { - if (arity[arity[0]] == -2) { -#ifdef TRIE_COMPACT_PAIRS - str_index += sprintf(& str[str_index], ","); -#else - str_index += sprintf(& str[str_index], "|"); - arity[arity[0]] = -1; -#endif /* TRIE_COMPACT_PAIRS */ - break; - } else { - str_index += sprintf(& str[str_index], "]"); - arity[0]--; - } - } - } - } else if (IsIntTerm(t)) { -#if SHORT_INTS - str_index += sprintf(& str[str_index], "%ld", IntOfTerm(t)); -#else - str_index += sprintf(& str[str_index], "%d", IntOfTerm(t)); -#endif /* SHORT_INTS */ - while (arity[0]) { - if (arity[arity[0]] > 0) { - arity[arity[0]]--; - if (arity[arity[0]] == 0) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; - } else { - str_index += sprintf(& str[str_index], ","); - break; - } - } else { - if (arity[arity[0]] == -2) { -#ifdef TRIE_COMPACT_PAIRS - str_index += sprintf(& str[str_index], ","); -#else - str_index += sprintf(& str[str_index], "|"); - arity[arity[0]] = -1; -#endif /* TRIE_COMPACT_PAIRS */ - break; - } else { - str_index += sprintf(& str[str_index], "]"); - arity[0]--; - } - } - } - } else if (IsAtomTerm(t)) { -#ifndef TRIE_COMPACT_PAIRS - if (arity[arity[0]] == -1 && t == TermNil) { - str[str_index - 1] = ']'; - arity[0]--; - } else -#endif /* TRIE_COMPACT_PAIRS */ - str_index += sprintf(& str[str_index], "%s", AtomName(AtomOfTerm(t))); - while (arity[0]) { - if (arity[arity[0]] > 0) { - arity[arity[0]]--; - if (arity[arity[0]] == 0) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; - } else { - str_index += sprintf(& str[str_index], ","); - break; - } - } else { - if (arity[arity[0]] == -2) { -#ifdef TRIE_COMPACT_PAIRS - str_index += sprintf(& str[str_index], ","); -#else - str_index += sprintf(& str[str_index], "|"); - arity[arity[0]] = -1; -#endif /* TRIE_COMPACT_PAIRS */ - break; - } else { - str_index += sprintf(& str[str_index], "]"); - arity[0]--; - } - } - } - } else if (IsPairTerm(t)) { -#ifdef TRIE_COMPACT_PAIRS - if (t == CompactPairEndList) - arity[arity[0]] = -1; - else if (t == CompactPairEndTerm) { - str[str_index - 1] = '|'; - arity[arity[0]] = -1; -#else - if (arity[arity[0]] == -1) { - str[str_index - 1] = ','; - arity[arity[0]] = -2; -#endif /* TRIE_COMPACT_PAIRS */ - } else { - str_index += sprintf(& str[str_index], "["); - arity[0]++; - arity[arity[0]] = -2; - } - } else if (IsApplTerm(t)) { - Functor f = (Functor) RepAppl(t); - if (f == FunctorDouble) { - mode = TRAVERSE_MODE_FLOAT; - } else if (f == FunctorLongInt) { - mode = TRAVERSE_MODE_LONG; - } else { - str_index += sprintf(& str[str_index], "%s(", AtomName(NameOfFunctor(f))); - arity[0]++; - arity[arity[0]] = ArityOfFunctor(f); - } - } - + /* process current trie node */ TrStat_sg_nodes++; - /* show answers ... */ - if (arity[0] == 0) { - sg_fr_ptr sg_fr = (sg_fr_ptr) TrNode_child(sg_node); - str[str_index] = 0; +#ifdef GLOBAL_TRIE + traverse_global_trie_for_subgoal(TrNode_entry(current_node), str, &str_index, arity, &mode); +#else + traverse_trie_node(TrNode_entry(current_node), str, &str_index, arity, &mode, TRAVERSE_TYPE_SUBGOAL); +#endif /* GLOBAL_TRIE */ + + /* continue with child node ... */ + if (arity[0] != 0) + traverse_subgoal_trie(TrNode_child(current_node), str, str_index, arity, mode, TRAVERSE_POSITION_FIRST); + /* ... or show answers */ + else { + sg_fr_ptr sg_fr = (sg_fr_ptr) TrNode_sg_fr(current_node); TrStat_subgoals++; - TrStat_sg_linear_nodes+= depth; - if (TrStat_sg_max_depth < 0) { - TrStat_sg_min_depth = TrStat_sg_max_depth = depth; - } else if (depth < TrStat_sg_min_depth) { - TrStat_sg_min_depth = depth; - } else if (depth > TrStat_sg_max_depth) { - TrStat_sg_max_depth = depth; - } - SHOW_TABLE("%s.\n", str); + str[str_index] = 0; + SHOW_TABLE_STRUCTURE("%s.\n", str); TrStat_ans_nodes++; if (SgFr_first_answer(sg_fr) == NULL) { - if (TrStat_ans_max_depth < 0) - TrStat_ans_max_depth = 0; - TrStat_ans_min_depth = 0; if (SgFr_state(sg_fr) < complete) { TrStat_sg_incomplete++; - SHOW_TABLE(" ---> INCOMPLETE\n"); + SHOW_TABLE_STRUCTURE(" ---> INCOMPLETE\n"); } else { TrStat_answers_no++; - SHOW_TABLE(" NO\n"); + SHOW_TABLE_STRUCTURE(" NO\n"); } } else if (SgFr_first_answer(sg_fr) == SgFr_answer_trie(sg_fr)) { - if (TrStat_ans_max_depth < 0) - TrStat_ans_max_depth = 0; - TrStat_ans_min_depth = 0; - TrStat_answers_yes++; - TrStat_answers++; - SHOW_TABLE(" TRUE\n"); + TrStat_answers_true++; + SHOW_TABLE_STRUCTURE(" TRUE\n"); } else { arity[0] = 0; - traverse_answer_trie(TrNode_child(SgFr_answer_trie(sg_fr)), &str[str_index], 0, arity, 0, 1, TRAVERSE_MODE_NORMAL, TRAVERSE_POSITION_FIRST); + traverse_answer_trie(TrNode_child(SgFr_answer_trie(sg_fr)), &str[str_index], 0, arity, 0, TRAVERSE_MODE_NORMAL, TRAVERSE_POSITION_FIRST); if (SgFr_state(sg_fr) < complete) { TrStat_sg_incomplete++; - SHOW_TABLE(" ---> INCOMPLETE\n"); + SHOW_TABLE_STRUCTURE(" ---> INCOMPLETE\n"); } } } - /* ... or continue with child node */ - else - traverse_subgoal_trie(TrNode_child(sg_node), str, str_index, arity, depth + 1, mode, TRAVERSE_POSITION_FIRST); - /* continue iteratively with sibling nodes */ + /* restore the initial state and continue with sibling nodes */ if (position == TRAVERSE_POSITION_FIRST) { - /* restore the initial state */ str_index = current_str_index; mode = current_mode; - sg_node = TrNode_next(sg_node); - while (sg_node) { + current_node = TrNode_next(current_node); + while (current_node) { memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); #ifdef TRIE_COMPACT_PAIRS if (arity[arity[0]] == -2 && str[str_index - 1] != '[') @@ -1716,8 +1858,8 @@ void traverse_subgoal_trie(sg_node_ptr sg_node, char *str, int str_index, int *a if (arity[arity[0]] == -1) str[str_index - 1] = '|'; #endif /* TRIE_COMPACT_PAIRS */ - traverse_subgoal_trie(sg_node, str, str_index, arity, depth, mode, TRAVERSE_POSITION_NEXT); - sg_node = TrNode_next(sg_node); + traverse_subgoal_trie(current_node, str, str_index, arity, mode, TRAVERSE_POSITION_NEXT); + current_node = TrNode_next(current_node); } free(current_arity); } @@ -1727,22 +1869,21 @@ void traverse_subgoal_trie(sg_node_ptr sg_node, char *str, int str_index, int *a static - void traverse_answer_trie(ans_node_ptr ans_node, char *str, int str_index, int *arity, int var_index, int depth, int mode, int position) { +void traverse_answer_trie(ans_node_ptr current_node, char *str, int str_index, int *arity, int var_index, int mode, int position) { int *current_arity = NULL, current_str_index = 0, current_var_index = 0, current_mode = 0; - Term t; /* test if hashing */ - if (IS_ANSWER_HASH(ans_node)) { + if (IS_ANSWER_TRIE_HASH(current_node)) { ans_node_ptr *bucket, *last_bucket; ans_hash_ptr hash; - hash = (ans_hash_ptr) ans_node; + hash = (ans_hash_ptr) current_node; bucket = Hash_buckets(hash); last_bucket = bucket + Hash_num_buckets(hash); current_arity = (int *) malloc(sizeof(int) * (arity[0] + 1)); memcpy(current_arity, arity, sizeof(int) * (arity[0] + 1)); do { if (*bucket) { - traverse_answer_trie(*bucket, str, str_index, arity, var_index, depth, mode, TRAVERSE_POSITION_FIRST); + traverse_answer_trie(*bucket, str, str_index, arity, var_index, mode, TRAVERSE_POSITION_FIRST); memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); #ifdef TRIE_COMPACT_PAIRS if (arity[arity[0]] == -2 && str[str_index - 1] != '[') @@ -1766,14 +1907,67 @@ static current_mode = mode; } - /* print VAR when starting a term */ + /* print VAR if starting a term */ if (arity[0] == 0 && mode == TRAVERSE_MODE_NORMAL) { str_index += sprintf(& str[str_index], " VAR%d: ", var_index); var_index++; } + /* process current trie node */ + TrStat_ans_nodes++; +#ifdef GLOBAL_TRIE + traverse_global_trie_for_answer(TrNode_entry(current_node), str, &str_index, arity, &mode); +#else + traverse_trie_node(TrNode_entry(current_node), str, &str_index, arity, &mode, TRAVERSE_TYPE_ANSWER); +#endif /* GLOBAL_TRIE */ + + /* show answer .... */ + if (IS_ANSWER_LEAF_NODE(current_node)) { + TrStat_answers++; + str[str_index] = 0; + SHOW_TABLE_STRUCTURE("%s\n", str); + } +#ifdef TABLING_INNER_CUTS + /* ... or continue with pruned node */ + else if (TrNode_child(current_node) == NULL) + TrStat_answers++; + TrStat_answers_pruned++; +#endif /* TABLING_INNER_CUTS */ + /* ... or continue with child node */ + else + traverse_answer_trie(TrNode_child(current_node), str, str_index, arity, var_index, mode, TRAVERSE_POSITION_FIRST); + + /* restore the initial state and continue with sibling nodes */ + if (position == TRAVERSE_POSITION_FIRST) { + str_index = current_str_index; + var_index = current_var_index; + mode = current_mode; + current_node = TrNode_next(current_node); + while (current_node) { + memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); +#ifdef TRIE_COMPACT_PAIRS + if (arity[arity[0]] == -2 && str[str_index - 1] != '[') + str[str_index - 1] = ','; +#else + if (arity[arity[0]] == -1) + str[str_index - 1] = '|'; +#endif /* TRIE_COMPACT_PAIRS */ + traverse_answer_trie(current_node, str, str_index, arity, var_index, mode, TRAVERSE_POSITION_NEXT); + current_node = TrNode_next(current_node); + } + free(current_arity); + } + + return; +} + + +static +void traverse_trie_node(Term t, char *str, int *str_index_ptr, int *arity, int *mode_ptr, int type) { + int mode = *mode_ptr; + int str_index = *str_index_ptr; + /* test the node type */ - t = TrNode_entry(ans_node); if (mode == TRAVERSE_MODE_FLOAT) { #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P arity[0]++; @@ -1816,7 +2010,12 @@ static } } } - mode = TRAVERSE_MODE_FLOAT_END; +#ifndef GLOBAL_TRIE + if (type == TRAVERSE_TYPE_SUBGOAL) + mode = TRAVERSE_MODE_NORMAL; + else /* type == TRAVERSE_TYPE_ANSWER */ +#endif /* GLOBAL_TRIE */ + mode = TRAVERSE_MODE_FLOAT_END; } else if (mode == TRAVERSE_MODE_FLOAT_END) { mode = TRAVERSE_MODE_NORMAL; } else if (mode == TRAVERSE_MODE_LONG) { @@ -1851,15 +2050,19 @@ static } } } - mode = TRAVERSE_MODE_LONG_END; +#ifndef GLOBAL_TRIE + if (type == TRAVERSE_TYPE_SUBGOAL) + mode = TRAVERSE_MODE_NORMAL; + else /* type == TRAVERSE_TYPE_ANSWER */ +#endif /* GLOBAL_TRIE */ + mode = TRAVERSE_MODE_LONG_END; } else if (mode == TRAVERSE_MODE_LONG_END) { mode = TRAVERSE_MODE_NORMAL; } else if (IsVarTerm(t)) { -#if SHORT_INTS - str_index += sprintf(& str[str_index], "ANSVAR%ld", VarIndexOfTableTerm(t)); -#else - str_index += sprintf(& str[str_index], "ANSVAR%d", VarIndexOfTableTerm(t)); -#endif /* SHORT_INTS */ + if (type == TRAVERSE_TYPE_SUBGOAL) + str_index += sprintf(& str[str_index], "VAR%d", VarIndexOfTableTerm(t)); + else /* type == TRAVERSE_TYPE_ANSWER */ + str_index += sprintf(& str[str_index], "ANSVAR%d", VarIndexOfTableTerm(t)); while (arity[0]) { if (arity[arity[0]] > 0) { arity[arity[0]]--; @@ -1979,37 +2182,120 @@ static } } - TrStat_ans_nodes++; - /* show answer .... */ - if (IS_ANSWER_LEAF_NODE(ans_node)) { - str[str_index] = 0; - SHOW_TABLE("%s\n", str); - TrStat_answers++; - TrStat_ans_linear_nodes+= depth; - if (TrStat_ans_max_depth < 0) - TrStat_ans_min_depth = TrStat_ans_max_depth = depth; - else if (depth < TrStat_ans_min_depth) - TrStat_ans_min_depth = depth; - else if (depth > TrStat_ans_max_depth) - TrStat_ans_max_depth = depth; - } -#ifdef TABLING_INNER_CUTS - /* ... or continue with pruned node */ - else if (TrNode_child(ans_node) == NULL) - TrStat_ans_pruned++; -#endif /* TABLING_INNER_CUTS */ - /* ... or continue with child node */ - else - traverse_answer_trie(TrNode_child(ans_node), str, str_index, arity, var_index, depth + 1, mode, TRAVERSE_POSITION_FIRST); + *mode_ptr = mode; + *str_index_ptr = str_index; + return; +} - /* continue iteratively with sibling nodes */ + +#ifdef GLOBAL_TRIE +static +void free_global_trie_branch(gt_node_ptr current_node) { + gt_node_ptr parent_node, child_node; + + parent_node = TrNode_parent(current_node); + child_node = TrNode_child(parent_node); + if (IS_GLOBAL_TRIE_HASH(child_node)) { + gt_hash_ptr hash; + gt_node_ptr *bucket; + hash = (gt_hash_ptr) child_node; + Hash_num_nodes(hash)--; + bucket = Hash_bucket(hash, HASH_ENTRY(TrNode_entry(current_node), Hash_seed(hash))); + child_node = *bucket; + if (child_node != current_node) { + while (TrNode_next(child_node) != current_node) + child_node = TrNode_next(child_node); + TrNode_next(child_node) = TrNode_next(current_node); + FREE_GLOBAL_TRIE_NODE(current_node); + } else { + *bucket = TrNode_next(current_node); + FREE_GLOBAL_TRIE_NODE(current_node); + if (Hash_num_nodes(hash) == 0) { + FREE_HASH_BUCKETS(Hash_buckets(hash)); + FREE_GLOBAL_TRIE_HASH(hash); + if (parent_node != GLOBAL_root_gt) + free_global_trie_branch(parent_node); + else + TrNode_child(parent_node) = NULL; + } + } + } else if (child_node != current_node) { + while (TrNode_next(child_node) != current_node) + child_node = TrNode_next(child_node); + TrNode_next(child_node) = TrNode_next(current_node); + FREE_GLOBAL_TRIE_NODE(current_node); + } else if (TrNode_next(current_node) == NULL) { + FREE_GLOBAL_TRIE_NODE(current_node); + if (parent_node != GLOBAL_root_gt) + free_global_trie_branch(parent_node); + else + TrNode_child(parent_node) = NULL; + } else { + TrNode_child(parent_node) = TrNode_next(current_node); + FREE_GLOBAL_TRIE_NODE(current_node); + } + return; +} + + +static +void traverse_global_trie(gt_node_ptr current_node, char *str, int str_index, int *arity, int mode, int position) { + int *current_arity = NULL, current_str_index = 0, current_mode = 0; + + /* test if hashing */ + if (IS_GLOBAL_TRIE_HASH(current_node)) { + gt_node_ptr *bucket, *last_bucket; + gt_hash_ptr hash; + hash = (gt_hash_ptr) current_node; + bucket = Hash_buckets(hash); + last_bucket = bucket + Hash_num_buckets(hash); + current_arity = (int *) malloc(sizeof(int) * (arity[0] + 1)); + memcpy(current_arity, arity, sizeof(int) * (arity[0] + 1)); + do { + if (*bucket) { + traverse_global_trie(*bucket, str, str_index, arity, mode, TRAVERSE_POSITION_FIRST); + memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); +#ifdef TRIE_COMPACT_PAIRS + if (arity[arity[0]] == -2 && str[str_index - 1] != '[') + str[str_index - 1] = ','; +#else + if (arity[arity[0]] == -1) + str[str_index - 1] = '|'; +#endif /* TRIE_COMPACT_PAIRS */ + } + } while (++bucket != last_bucket); + free(current_arity); + return; + } + + /* save current state if first sibling node */ + if (position == TRAVERSE_POSITION_FIRST) { + current_arity = (int *) malloc(sizeof(int) * (arity[0] + 1)); + memcpy(current_arity, arity, sizeof(int) * (arity[0] + 1)); + current_str_index = str_index; + current_mode = mode; + } + + /* process current trie node */ + TrStat_gt_nodes++; + traverse_trie_node(TrNode_entry(current_node), str, &str_index, arity, &mode, TRAVERSE_TYPE_SUBGOAL); + + /* continue with child node ... */ + if (arity[0] != 0) + traverse_global_trie(TrNode_child(current_node), str, str_index, arity, mode, TRAVERSE_POSITION_FIRST); + /* ... or show term */ + else { + TrStat_gt_terms++; + str[str_index] = 0; + SHOW_TABLE_STRUCTURE(" TERM (x%ld): %s\n", (unsigned long int) TrNode_child(current_node), str); + } + + /* restore the initial state and continue with sibling nodes */ if (position == TRAVERSE_POSITION_FIRST) { - /* restore the initial state */ str_index = current_str_index; - var_index = current_var_index; mode = current_mode; - ans_node = TrNode_next(ans_node); - while (ans_node) { + current_node = TrNode_next(current_node); + while (current_node) { memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); #ifdef TRIE_COMPACT_PAIRS if (arity[arity[0]] == -2 && str[str_index - 1] != '[') @@ -2018,12 +2304,31 @@ static if (arity[arity[0]] == -1) str[str_index - 1] = '|'; #endif /* TRIE_COMPACT_PAIRS */ - traverse_answer_trie(ans_node, str, str_index, arity, var_index, depth, mode, TRAVERSE_POSITION_NEXT); - ans_node = TrNode_next(ans_node); + traverse_global_trie(current_node, str, str_index, arity, mode, TRAVERSE_POSITION_NEXT); + current_node = TrNode_next(current_node); } free(current_arity); } return; } + + +static +void traverse_global_trie_for_subgoal(gt_node_ptr current_node, char *str, int *str_index, int *arity, int *mode) { + if (TrNode_parent(current_node) != GLOBAL_root_gt) + traverse_global_trie_for_subgoal(TrNode_parent(current_node), str, str_index, arity, mode); + traverse_trie_node(TrNode_entry(current_node), str, str_index, arity, mode, TRAVERSE_TYPE_SUBGOAL); + return; +} + + +static +void traverse_global_trie_for_answer(gt_node_ptr current_node, char *str, int *str_index, int *arity, int *mode) { + if (TrNode_parent(current_node) != GLOBAL_root_gt) + traverse_global_trie_for_answer(TrNode_parent(current_node), str, str_index, arity, mode); + traverse_trie_node(TrNode_entry(current_node), str, str_index, arity, mode, TRAVERSE_TYPE_ANSWER); + return; +} +#endif /* GLOBAL_TRIE */ #endif /* TABLING */ diff --git a/OPTYap/tab.tries.insts.i b/OPTYap/tab.tries.insts.i index a4a27a88f..fd6cf05b8 100644 --- a/OPTYap/tab.tries.insts.i +++ b/OPTYap/tab.tries.insts.i @@ -9,35 +9,56 @@ **********************************************************************/ -/* ----------------------------------------------- ** -** Trie instructions: stack organization ** -** ----------------------------------------------- ** - ------------------- - | ha = heap_arity | - ------------------- -- - | heap ptr 1 | | - ------------------- | - | ... | -- heap_arity - ------------------- | - | heap ptr ha | | - ------------------- -- - | va = vars_arity | - ------------------- - | sa = subs_arity | - ------------------- -- - | subs ptr sa | | - ------------------- | - | ... | -- subs_arity - ------------------- | - | subs ptr 1 | | - ------------------- -- - | var ptr va | | - ------------------- | - | ... | -- vars_arity - ------------------- | - | var ptr 1 | | - ------------------- -- -** ----------------------------------------------- */ +/* --------------------------------------------------------- ** +** Trie instructions: auxiliary stack organization ** +** --------------------------------------------------------- ** + STANDARD_TRIE + ------------------- + | ha = heap_arity | + ------------------- -- + | heap ptr 1 | | + ------------------- | + | ... | -- heap_arity + ------------------- | + | heap ptr ha | | + ------------------- -- + | va = vars_arity | + ------------------- + | sa = subs_arity | + ------------------- -- + | subs ptr sa | | + ------------------- | + | ... | -- subs_arity + ------------------- | + | subs ptr 1 | | + ------------------- -- + | var ptr va | | + ------------------- | + | ... | -- vars_arity + ------------------- | + | var ptr 1 | | + ------------------- -- + + + GLOBAL_TRIE + ------------------- + | va = vars_arity | + ------------------- -- + | var ptr va | | + ------------------- | + | ... | -- vars_arity + ------------------- | + | var ptr 1 | | + ------------------- -- + | sa = subs_arity | + ------------------- -- + | subs ptr sa | | + ------------------- | + | ... | -- subs_arity + ------------------- | + | subs ptr 1 | | + ------------------- -- +** --------------------------------------------------------- */ @@ -45,12 +66,21 @@ ** Trie instructions: auxiliary macros ** ** --------------------------------------------- */ +#ifdef GLOBAL_TRIE +#define copy_arity_stack() \ + { int size = subs_arity + vars_arity + 2; \ + YENV -= size; \ + memcpy(YENV, aux_stack_ptr, size * sizeof(CELL *)); \ + aux_stack_ptr = YENV; \ + } +#else #define copy_arity_stack() \ { int size = heap_arity + subs_arity + vars_arity + 3; \ YENV -= size; \ - memcpy(YENV, aux_ptr, size * sizeof(CELL *)); \ - aux_ptr = YENV; \ + memcpy(YENV, aux_stack_ptr, size * sizeof(CELL *)); \ + aux_stack_ptr = YENV; \ } +#endif /* GLOBAL_TRIE */ #define next_trie_instruction(NODE) \ PREG = (yamop *) TrNode_child(NODE); \ @@ -136,35 +166,35 @@ ** trie_null ** ** ------------------- */ -#define stack_trie_null_instr() \ +#define stack_trie_null_instr() \ next_trie_instruction(node) #ifdef TRIE_COMPACT_PAIRS /* trie compiled code for term 'CompactPairInit' */ -#define stack_trie_null_in_new_pair_instr() \ - if (heap_arity) { \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr-- = (CELL) H; \ - *aux_ptr = heap_arity - 1 + 2; \ - YENV = aux_ptr; \ - } else { \ - int i; \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr-- = (CELL) H; \ - *aux_ptr = 2; \ - YENV = aux_ptr; \ - aux_ptr += 2 + 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - H += 2; \ +#define stack_trie_null_in_new_pair_instr() \ + if (heap_arity) { \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr-- = (CELL) H; \ + *aux_stack_ptr = heap_arity - 1 + 2; \ + YENV = aux_stack_ptr; \ + } else { \ + int i; \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr-- = (CELL) H; \ + *aux_stack_ptr = 2; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 2 + 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + H += 2; \ next_trie_instruction(node) #endif /* TRIE_COMPACT_PAIRS */ @@ -178,24 +208,24 @@ if (heap_arity) { \ CELL var; \ int i; \ - *aux_ptr = heap_arity - 1; \ - var = *++aux_ptr; \ + *aux_stack_ptr = heap_arity - 1; \ + var = *++aux_stack_ptr; \ RESET_VARIABLE(var); \ for (i = 0; i < heap_arity - 1; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ } \ - *aux_ptr++ = vars_arity + 1; \ - *aux_ptr++ = subs_arity; \ + *aux_stack_ptr++ = vars_arity + 1; \ + *aux_stack_ptr++ = subs_arity; \ for (i = 0; i < subs_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ } \ - *aux_ptr = var; \ + *aux_stack_ptr = var; \ next_instruction(heap_arity - 1 || subs_arity, node); \ } else { \ - *++aux_ptr = vars_arity + 1; \ - *++aux_ptr = subs_arity - 1; \ + *++aux_stack_ptr = vars_arity + 1; \ + *++aux_stack_ptr = subs_arity - 1; \ next_instruction(subs_arity - 1, node); \ } @@ -203,32 +233,32 @@ #define stack_trie_var_in_new_pair_instr() \ if (heap_arity) { \ int i; \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr = heap_arity - 1 + 1; \ - YENV = aux_ptr; \ - aux_ptr += 2; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr = heap_arity - 1 + 1; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 2; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ for (i = 0; i < heap_arity - 1; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ } \ - *aux_ptr++ = vars_arity + 1; \ - *aux_ptr++ = subs_arity; \ + *aux_stack_ptr++ = vars_arity + 1; \ + *aux_stack_ptr++ = subs_arity; \ for (i = 0; i < subs_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ } \ - *aux_ptr = (CELL) H; \ + *aux_stack_ptr = (CELL) H; \ } else { \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr = 1; \ - YENV = aux_ptr; \ - aux_ptr += 2; \ - *aux_ptr++ = vars_arity + 1; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr = (CELL) H; \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr = 1; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 2; \ + *aux_stack_ptr++ = vars_arity + 1; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr = (CELL) H; \ } \ RESET_VARIABLE((CELL) H); \ H += 2; \ @@ -241,101 +271,101 @@ ** trie_val ** ** ------------------ */ -#define stack_trie_val_instr() \ - if (heap_arity) { \ - CELL aux, subs, *subs_ptr; \ - YENV = ++aux_ptr; \ - subs_ptr = aux_ptr + heap_arity + 1 + subs_arity + vars_arity - var_index; \ - aux = *aux_ptr; \ - subs = *subs_ptr; \ - if (aux > subs) { \ - Bind_Global((CELL *) aux, subs); \ - } else { \ - RESET_VARIABLE(aux); \ - Bind_Local((CELL *) subs, aux); \ - *subs_ptr = aux; \ - } \ - *aux_ptr = heap_arity - 1; \ - next_instruction(heap_arity - 1 || subs_arity, node); \ - } else { \ - CELL aux, subs, *subs_ptr; \ - int i; \ - aux_ptr += 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - subs_ptr = aux_ptr + vars_arity - var_index; \ - aux = *aux_ptr; \ - subs = *subs_ptr; \ - if (aux > subs) { \ - if ((CELL *) aux <= H) { \ - Bind_Global((CELL *) aux, subs); \ - } else if ((CELL *) subs <= H) { \ - Bind_Local((CELL *) aux, subs); \ - } else { \ - Bind_Local((CELL *) subs, aux); \ - *subs_ptr = aux; \ - } \ - } else { \ - if ((CELL *) subs <= H) { \ - Bind_Global((CELL *) subs, aux); \ - *subs_ptr = aux; \ - } else if ((CELL *) aux <= H) { \ - Bind_Local((CELL *) subs, aux); \ - *subs_ptr = aux; \ - } else { \ - Bind_Local((CELL *) aux, subs); \ - } \ - } \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - next_instruction(subs_arity - 1, node); \ +#define stack_trie_val_instr() \ + if (heap_arity) { \ + CELL aux_sub, aux_var, *vars_ptr; \ + YENV = ++aux_stack_ptr; \ + vars_ptr = aux_stack_ptr + heap_arity + 1 + subs_arity + vars_arity - var_index; \ + aux_sub = *aux_stack_ptr; \ + aux_var = *vars_ptr; \ + if (aux_sub > aux_var) { \ + Bind_Global((CELL *) aux_sub, aux_var); \ + } else { \ + RESET_VARIABLE(aux_sub); \ + Bind_Local((CELL *) aux_var, aux_sub); \ + *vars_ptr = aux_sub; \ + } \ + *aux_stack_ptr = heap_arity - 1; \ + next_instruction(heap_arity - 1 || subs_arity, node); \ + } else { \ + CELL aux_sub, aux_var, *vars_ptr; \ + int i; \ + aux_stack_ptr += 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + vars_ptr = aux_stack_ptr + vars_arity - var_index; \ + aux_sub = *aux_stack_ptr; \ + aux_var = *vars_ptr; \ + if (aux_sub > aux_var) { \ + if ((CELL *) aux_sub <= H) { \ + Bind_Global((CELL *) aux_sub, aux_var); \ + } else if ((CELL *) aux_var <= H) { \ + Bind_Local((CELL *) aux_sub, aux_var); \ + } else { \ + Bind_Local((CELL *) aux_var, aux_sub); \ + *vars_ptr = aux_sub; \ + } \ + } else { \ + if ((CELL *) aux_var <= H) { \ + Bind_Global((CELL *) aux_var, aux_sub); \ + *vars_ptr = aux_sub; \ + } else if ((CELL *) aux_sub <= H) { \ + Bind_Local((CELL *) aux_var, aux_sub); \ + *vars_ptr = aux_sub; \ + } else { \ + Bind_Local((CELL *) aux_sub, aux_var); \ + } \ + } \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + next_instruction(subs_arity - 1, node); \ } -#ifdef TRIE_COMPACT_PAIRS -#define stack_trie_val_in_new_pair_instr() \ - if (heap_arity) { \ - CELL aux, subs, *subs_ptr; \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr = (CELL) (H + 1); \ - aux = (CELL) H; \ - subs_ptr = aux_ptr + heap_arity + 1 + subs_arity + vars_arity - var_index; \ - subs = *subs_ptr; \ - if (aux > subs) { \ - Bind_Global((CELL *) aux, subs); \ - } else { \ - RESET_VARIABLE(aux); \ - Bind_Local((CELL *) subs, aux); \ - *subs_ptr = aux; \ - } \ - } else { \ - CELL aux, subs, *subs_ptr; \ - int i; \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr = 1; \ - YENV = aux_ptr; \ - aux_ptr += 1 + 2; \ - aux = (CELL) H; \ - subs_ptr = aux_ptr + subs_arity + vars_arity - var_index; \ - subs = *subs_ptr; \ - if (aux > subs) { \ - Bind_Global((CELL *) aux, subs); \ - } else { \ - RESET_VARIABLE(aux); \ - Bind_Local((CELL *) subs, aux); \ - *subs_ptr = aux; \ - } \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - H += 2; \ +#ifdef TRIE_COMPACT_PAIRS +#define stack_trie_val_in_new_pair_instr() \ + if (heap_arity) { \ + CELL aux_sub, aux_var, *vars_ptr; \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr = (CELL) (H + 1); \ + aux_sub = (CELL) H; \ + vars_ptr = aux_stack_ptr + heap_arity + 1 + subs_arity + vars_arity - var_index; \ + aux_var = *vars_ptr; \ + if (aux_sub > aux_var) { \ + Bind_Global((CELL *) aux_sub, aux_var); \ + } else { \ + RESET_VARIABLE(aux_sub); \ + Bind_Local((CELL *) aux_var, aux_sub); \ + *vars_ptr = aux_sub; \ + } \ + } else { \ + CELL aux_sub, aux_var, *vars_ptr; \ + int i; \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr = 1; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 1 + 2; \ + aux_sub = (CELL) H; \ + vars_ptr = aux_stack_ptr + subs_arity + vars_arity - var_index; \ + aux_var = *vars_ptr; \ + if (aux_sub > aux_var) { \ + Bind_Global((CELL *) aux_sub, aux_var); \ + } else { \ + RESET_VARIABLE(aux_sub); \ + Bind_Local((CELL *) aux_var, aux_sub); \ + *vars_ptr = aux_sub; \ + } \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + H += 2; \ next_trie_instruction(node) #endif /* TRIE_COMPACT_PAIRS */ @@ -345,47 +375,47 @@ ** trie_atom ** ** ------------------- */ -#define stack_trie_atom_instr() \ - if (heap_arity) { \ - YENV = ++aux_ptr; \ - Bind_Global((CELL *) *aux_ptr, TrNode_entry(node)); \ - *aux_ptr = heap_arity - 1; \ - next_instruction(heap_arity - 1 || subs_arity, node); \ - } else { \ - int i; \ - aux_ptr += 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, TrNode_entry(node)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - next_instruction(subs_arity - 1, node); \ +#define stack_trie_atom_instr() \ + if (heap_arity) { \ + YENV = ++aux_stack_ptr; \ + Bind_Global((CELL *) *aux_stack_ptr, TrNode_entry(node)); \ + *aux_stack_ptr = heap_arity - 1; \ + next_instruction(heap_arity - 1 || subs_arity, node); \ + } else { \ + int i; \ + aux_stack_ptr += 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, TrNode_entry(node)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + next_instruction(subs_arity - 1, node); \ } #ifdef TRIE_COMPACT_PAIRS -#define stack_trie_atom_in_new_pair_instr() \ - if (heap_arity) { \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr = (CELL) (H + 1); \ - } else { \ - int i; \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr = 1; \ - YENV = aux_ptr; \ - aux_ptr += 1 + 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - Bind_Global(H, TrNode_entry(node)); \ - H += 2; \ +#define stack_trie_atom_in_new_pair_instr() \ + if (heap_arity) { \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr = (CELL) (H + 1); \ + } else { \ + int i; \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr = 1; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 1 + 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + Bind_Global(H, TrNode_entry(node)); \ + H += 2; \ next_trie_instruction(node) #endif /* TRIE_COMPACT_PAIRS */ @@ -397,53 +427,53 @@ #ifdef TRIE_COMPACT_PAIRS /* trie compiled code for term 'CompactPairEndList' */ -#define stack_trie_pair_instr() \ - if (heap_arity) { \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr = (CELL) H; \ - } else { \ - int i; \ - *aux_ptr-- = (CELL) H; \ - *aux_ptr = 1; \ - YENV = aux_ptr; \ - aux_ptr += 1 + 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - Bind_Global(H + 1, TermNil); \ - H += 2; \ +#define stack_trie_pair_instr() \ + if (heap_arity) { \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr = (CELL) H; \ + } else { \ + int i; \ + *aux_stack_ptr-- = (CELL) H; \ + *aux_stack_ptr = 1; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 1 + 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + Bind_Global(H + 1, TermNil); \ + H += 2; \ next_trie_instruction(node) #else -#define stack_trie_pair_instr() \ - if (heap_arity) { \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr-- = (CELL) H; \ - *aux_ptr = heap_arity - 1 + 2; \ - YENV = aux_ptr; \ - } else { \ - int i; \ - *aux_ptr-- = (CELL) (H + 1); \ - *aux_ptr-- = (CELL) H; \ - *aux_ptr = 2; \ - YENV = aux_ptr; \ - aux_ptr += 2 + 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - H += 2; \ +#define stack_trie_pair_instr() \ + if (heap_arity) { \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr-- = (CELL) H; \ + *aux_stack_ptr = heap_arity - 1 + 2; \ + YENV = aux_stack_ptr; \ + } else { \ + int i; \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + *aux_stack_ptr-- = (CELL) H; \ + *aux_stack_ptr = 2; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 2 + 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + H += 2; \ next_trie_instruction(node) #endif /* TRIE_COMPACT_PAIRS */ @@ -453,65 +483,65 @@ ** trie_struct ** ** --------------------- */ -#define stack_trie_struct_instr() \ - if (heap_arity) { \ - int i; \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsAppl(H)); \ - for (i = 0; i < func_arity; i++) \ - *aux_ptr-- = (CELL) (H + func_arity - i); \ - *aux_ptr = heap_arity - 1 + func_arity; \ - YENV = aux_ptr; \ - } else { \ - int i; \ - for (i = 0; i < func_arity; i++) \ - *aux_ptr-- = (CELL) (H + func_arity - i); \ - *aux_ptr = func_arity; \ - YENV = aux_ptr; \ - aux_ptr += func_arity + 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsAppl(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - *H = (CELL) func; \ - H += 1 + func_arity; \ +#define stack_trie_struct_instr() \ + if (heap_arity) { \ + int i; \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsAppl(H)); \ + for (i = 0; i < func_arity; i++) \ + *aux_stack_ptr-- = (CELL) (H + func_arity - i); \ + *aux_stack_ptr = heap_arity - 1 + func_arity; \ + YENV = aux_stack_ptr; \ + } else { \ + int i; \ + for (i = 0; i < func_arity; i++) \ + *aux_stack_ptr-- = (CELL) (H + func_arity - i); \ + *aux_stack_ptr = func_arity; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += func_arity + 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsAppl(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + *H = (CELL) func; \ + H += 1 + func_arity; \ next_trie_instruction(node) #ifdef TRIE_COMPACT_PAIRS -#define stack_trie_struct_in_new_pair_instr() \ - if (heap_arity) { \ - int i; \ - aux_ptr++; \ - Bind_Global((CELL *) *aux_ptr, AbsPair(H)); \ - *aux_ptr-- = (CELL) (H + 1); \ - for (i = 0; i < func_arity; i++) \ - *aux_ptr-- = (CELL) (H + 2 + func_arity - i); \ - *aux_ptr = heap_arity - 1 + 1 + func_arity; \ - YENV = aux_ptr; \ - } else { \ - int i; \ - *aux_ptr-- = (CELL) (H + 1); \ - for (i = 0; i < func_arity; i++) \ - *aux_ptr-- = (CELL) (H + 2 + func_arity - i); \ - *aux_ptr = 1 + func_arity; \ - YENV = aux_ptr; \ - aux_ptr += 1 + func_arity + 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, AbsPair(H)); \ - for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ - } \ - } \ - Bind_Global(H, AbsAppl(H + 2)); \ - H += 2; \ - *H = (CELL) func; \ - H += 1 + func_arity; \ +#define stack_trie_struct_in_new_pair_instr() \ + if (heap_arity) { \ + int i; \ + aux_stack_ptr++; \ + Bind_Global((CELL *) *aux_stack_ptr, AbsPair(H)); \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + for (i = 0; i < func_arity; i++) \ + *aux_stack_ptr-- = (CELL) (H + 2 + func_arity - i); \ + *aux_stack_ptr = heap_arity - 1 + 1 + func_arity; \ + YENV = aux_stack_ptr; \ + } else { \ + int i; \ + *aux_stack_ptr-- = (CELL) (H + 1); \ + for (i = 0; i < func_arity; i++) \ + *aux_stack_ptr-- = (CELL) (H + 2 + func_arity - i); \ + *aux_stack_ptr = 1 + func_arity; \ + YENV = aux_stack_ptr; \ + aux_stack_ptr += 1 + func_arity + 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, AbsPair(H)); \ + for (i = 0; i < vars_arity; i++) { \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ + } \ + } \ + Bind_Global(H, AbsAppl(H + 2)); \ + H += 2; \ + *H = (CELL) func; \ + H += 1 + func_arity; \ next_trie_instruction(node) #endif /* TRIE_COMPACT_PAIRS */ @@ -521,11 +551,11 @@ ** trie_extension ** ** ------------------------ */ -#define stack_trie_extension_instr() \ - *aux_ptr-- = 0; /* float/longint extension mark */ \ - *aux_ptr-- = TrNode_entry(node); \ - *aux_ptr = heap_arity + 2; \ - YENV = aux_ptr; \ +#define stack_trie_extension_instr() \ + *aux_stack_ptr-- = 0; /* float/longint extension mark */ \ + *aux_stack_ptr-- = TrNode_entry(node); \ + *aux_stack_ptr = heap_arity + 2; \ + YENV = aux_stack_ptr; \ next_trie_instruction(node) @@ -536,21 +566,21 @@ #define stack_trie_float_longint_instr() \ if (heap_arity) { \ - YENV = ++aux_ptr; \ - Bind_Global((CELL *) *aux_ptr, t); \ - *aux_ptr = heap_arity - 1; \ + YENV = ++aux_stack_ptr; \ + Bind_Global((CELL *) *aux_stack_ptr, t); \ + *aux_stack_ptr = heap_arity - 1; \ next_instruction(heap_arity - 1 || subs_arity, node); \ } else { \ int i; \ - YENV = aux_ptr; \ - *aux_ptr = 0; \ - aux_ptr += 2; \ - *aux_ptr = subs_arity - 1; \ - aux_ptr += subs_arity; \ - Bind((CELL *) *aux_ptr, t); \ + YENV = aux_stack_ptr; \ + *aux_stack_ptr = 0; \ + aux_stack_ptr += 2; \ + *aux_stack_ptr = subs_arity - 1; \ + aux_stack_ptr += subs_arity; \ + Bind((CELL *) *aux_stack_ptr, t); \ for (i = 0; i < vars_arity; i++) { \ - *aux_ptr = *(aux_ptr + 1); \ - aux_ptr++; \ + *aux_stack_ptr = *(aux_stack_ptr + 1); \ + aux_stack_ptr++; \ } \ next_instruction(subs_arity - 1, node); \ } @@ -562,575 +592,688 @@ ** --------------------------- */ PBOp(trie_do_null, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; stack_trie_null_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_null)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_null, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_null_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_null)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_null, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_null_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_null)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_null, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_null_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_null)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_null_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); stack_trie_null_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_null_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_null_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_null_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_null_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_null_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_null_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_null_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_null_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_null_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_null_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_var, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); stack_trie_var_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_var)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_var, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_var_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_var)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_var, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_var_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_var)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_var, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_var_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_var)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_var_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); stack_trie_var_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_var_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_var_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_var_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_var_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_var_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_var_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_var_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_var_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_var_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_var_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_val, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); stack_trie_val_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_val)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_val, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); pop_trie_node(); stack_trie_val_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_val)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_val, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); store_trie_node(TrNode_next(node)); stack_trie_val_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_val)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_val, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); restore_trie_node(TrNode_next(node)); stack_trie_val_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_val)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_val_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); stack_trie_val_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_val_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_val_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); pop_trie_node(); stack_trie_val_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_val_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_val_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); store_trie_node(TrNode_next(node)); stack_trie_val_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_val_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_val_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); int var_index = VarIndexOfTableTerm(TrNode_entry(node)); restore_trie_node(TrNode_next(node)); stack_trie_val_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_val_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_atom, e) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; +#ifdef GLOBAL_TRIE + int subs_arity = *(aux_stack_ptr + *aux_stack_ptr + 1); + YENV = aux_stack_ptr = load_substitution_variable(TrNode_entry(node), aux_stack_ptr); + next_instruction(subs_arity - 1 , node); +#else + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); stack_trie_atom_instr(); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_atom, e) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); - + register CELL *aux_stack_ptr = (CELL *) (B + 1); +#ifdef GLOBAL_TRIE + int vars_arity = *(aux_stack_ptr); + int subs_arity = *(aux_stack_ptr + vars_arity + 1); +#else + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); +#endif /* GLOBAL_TRIE */ pop_trie_node(); +#ifdef GLOBAL_TRIE + YENV = aux_stack_ptr = load_substitution_variable(TrNode_entry(node), aux_stack_ptr); + next_instruction(subs_arity - 1 , node); +#else stack_trie_atom_instr(); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_atom, e) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); - + register CELL *aux_stack_ptr = YENV; +#ifdef GLOBAL_TRIE + int vars_arity = *(aux_stack_ptr); + int subs_arity = *(aux_stack_ptr + vars_arity + 1); +#else + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); +#endif /* GLOBAL_TRIE */ store_trie_node(TrNode_next(node)); +#ifdef GLOBAL_TRIE + YENV = aux_stack_ptr = load_substitution_variable(TrNode_entry(node), aux_stack_ptr); + next_instruction(subs_arity - 1, node); +#else stack_trie_atom_instr(); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_atom, e) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); - + register CELL *aux_stack_ptr = (CELL *) (B + 1); +#ifdef GLOBAL_TRIE + int vars_arity = *(aux_stack_ptr); + int subs_arity = *(aux_stack_ptr + vars_arity + 1); +#else + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); +#endif /* GLOBAL_TRIE */ restore_trie_node(TrNode_next(node)); +#ifdef GLOBAL_TRIE + YENV = aux_stack_ptr = load_substitution_variable(TrNode_entry(node), aux_stack_ptr); + next_instruction(subs_arity - 1, node); +#else stack_trie_atom_instr(); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_atom_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); stack_trie_atom_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_atom_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_atom_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_atom_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_atom_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_atom_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_atom_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_atom_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_atom_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_atom_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_atom_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_pair, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); stack_trie_pair_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_pair)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_pair, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_pair_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_pair)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_pair, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_pair_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_pair)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_pair, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_pair_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_pair)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_struct, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); stack_trie_struct_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_struct)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_struct, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); pop_trie_node(); stack_trie_struct_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_struct)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_struct, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); store_trie_node(TrNode_next(node)); stack_trie_struct_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_struct)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_struct, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); restore_trie_node(TrNode_next(node)); stack_trie_struct_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_struct)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_struct_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); stack_trie_struct_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_struct_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_struct_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); @@ -1138,17 +1281,17 @@ stack_trie_struct_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_struct_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_struct_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); @@ -1156,17 +1299,17 @@ stack_trie_struct_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_struct_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_struct_in_new_pair, e) -#ifdef TRIE_COMPACT_PAIRS +#if defined(TRIE_COMPACT_PAIRS) && !defined(GLOBAL_TRIE) register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Functor func = (Functor) RepAppl(TrNode_entry(node)); int func_arity = ArityOfFunctor(func); @@ -1174,77 +1317,97 @@ stack_trie_struct_in_new_pair_instr(); #else Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_struct_in_new_pair)"); -#endif /* TRIE_COMPACT_PAIRS */ +#endif /* TRIE_COMPACT_PAIRS && GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_extension, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; stack_trie_extension_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_extension)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_trust_extension, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); pop_trie_node(); stack_trie_extension_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_trust_extension)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_try_extension, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); store_trie_node(TrNode_next(node)); stack_trie_extension_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_try_extension)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_retry_extension, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = (CELL *) (B + 1); - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = (CELL *) (B + 1); + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); restore_trie_node(TrNode_next(node)); stack_trie_extension_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_retry_extension)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); PBOp(trie_do_float, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); volatile Float dbl; volatile Term *t_dbl = (Term *)((void *) &dbl); Term t; #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P heap_arity -= 4; - *t_dbl = *++aux_ptr; - ++aux_ptr; /* jump the float/longint extension mark */ - *(t_dbl + 1) = *++aux_ptr; + *t_dbl = *++aux_stack_ptr; + ++aux_stack_ptr; /* jump the float/longint extension mark */ + *(t_dbl + 1) = *++aux_stack_ptr; #else /* SIZEOF_DOUBLE == SIZEOF_INT_P */ heap_arity -= 2; - *t_dbl = *++aux_ptr; + *t_dbl = *++aux_stack_ptr; #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - ++aux_ptr; /* jump the float/longint extension mark */ + ++aux_stack_ptr; /* jump the float/longint extension mark */ t = MkFloatTerm(dbl); stack_trie_float_longint_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_float)"); +#endif /* GLOBAL_TRIE */ ENDPBOp(); @@ -1264,17 +1427,21 @@ PBOp(trie_do_long, e) +#ifndef GLOBAL_TRIE register ans_node_ptr node = (ans_node_ptr) PREG; - register CELL *aux_ptr = YENV; - int heap_arity = *aux_ptr; - int vars_arity = *(aux_ptr + heap_arity + 1); - int subs_arity = *(aux_ptr + heap_arity + 2); + register CELL *aux_stack_ptr = YENV; + int heap_arity = *aux_stack_ptr; + int vars_arity = *(aux_stack_ptr + heap_arity + 1); + int subs_arity = *(aux_stack_ptr + heap_arity + 2); Term t; heap_arity -= 2; - t = MkLongIntTerm(*++aux_ptr); - ++aux_ptr; /* jump the float/longint extension mark */ + t = MkLongIntTerm(*++aux_stack_ptr); + ++aux_stack_ptr; /* jump the float/longint extension mark */ stack_trie_float_longint_instr(); +#else + Yap_Error(INTERNAL_ERROR, TermNil, "invalid instruction (trie_do_long)"); +#endif /* GLOBAL_TRIE */ ENDPBOp();