From 93bba89d52a68963031b525fb36b40b959eaddd9 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 5 Apr 2016 08:09:39 +0100 Subject: [PATCH] indenting + fix compilation --- OPTYap/tab.tries.h | 1363 +++++++++++++---------- packages/myddas/CMakeLists.txt | 59 +- packages/myddas/myddas_util.c | 8 +- packages/myddas/mysql/CMakeLists.txt | 3 +- packages/myddas/odbc/CMakeLists.txt | 3 +- packages/myddas/pl/myddas.ypp | 6 - packages/myddas/postgres/CMakeLists.txt | 5 +- packages/myddas/sqlite3/CMakeLists.txt | 42 +- 8 files changed, 818 insertions(+), 671 deletions(-) diff --git a/OPTYap/tab.tries.h b/OPTYap/tab.tries.h index 8932667d4..75ad519c9 100644 --- a/OPTYap/tab.tries.h +++ b/OPTYap/tab.tries.h @@ -16,75 +16,79 @@ *********************/ #ifdef MODE_GLOBAL_TRIE_ENTRY -#define INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY) \ - { register gt_node_ptr entry_node = (gt_node_ptr) (ENTRY); \ - TrNode_child(entry_node) = (gt_node_ptr) ((UInt) TrNode_child(entry_node) + 1); \ - } -#define NEW_SUBGOAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ - INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ - new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) -#define NEW_ANSWER_TRIE_NODE(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ - INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ - new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) -#define NEW_GLOBAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ - INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ - new_global_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) +#define INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY) \ + { \ + register gt_node_ptr entry_node = (gt_node_ptr)(ENTRY); \ + TrNode_child(entry_node) = \ + (gt_node_ptr)((UInt)TrNode_child(entry_node) + 1); \ + } +#define NEW_SUBGOAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ + INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ + new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) +#define NEW_ANSWER_TRIE_NODE(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ + INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ + new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) +#define NEW_GLOBAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ + INCREMENT_GLOBAL_TRIE_REFERENCE(ENTRY); \ + new_global_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) #else -#define NEW_SUBGOAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ - new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) -#define NEW_ANSWER_TRIE_NODE(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ - new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) -#define NEW_GLOBAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ - new_global_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) +#define NEW_SUBGOAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ + new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) +#define NEW_ANSWER_TRIE_NODE(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \ + new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) +#define NEW_GLOBAL_TRIE_NODE(NODE, ENTRY, CHILD, PARENT, NEXT) \ + new_global_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) #endif /* MODE_GLOBAL_TRIE_ENTRY */ - #ifdef MODE_GLOBAL_TRIE_LOOP -#define SUBGOAL_CHECK_INSERT_ENTRY(TAB_ENT, NODE, ENTRY) \ - NODE = global_trie_check_insert_entry(NODE, ENTRY PASS_REGS) -#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ - NODE = global_trie_check_insert_entry(NODE, ENTRY PASS_REGS) +#define SUBGOAL_CHECK_INSERT_ENTRY(TAB_ENT, NODE, ENTRY) \ + NODE = global_trie_check_insert_entry(NODE, ENTRY PASS_REGS) +#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ + NODE = global_trie_check_insert_entry(NODE, ENTRY PASS_REGS) #else -#define SUBGOAL_CHECK_INSERT_ENTRY(TAB_ENT, NODE, ENTRY) \ - NODE = subgoal_trie_check_insert_entry(TAB_ENT, NODE, ENTRY PASS_REGS) -#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ - NODE = answer_trie_check_insert_entry(SG_FR, NODE, ENTRY, INSTR PASS_REGS) +#define SUBGOAL_CHECK_INSERT_ENTRY(TAB_ENT, NODE, ENTRY) \ + NODE = subgoal_trie_check_insert_entry(TAB_ENT, NODE, ENTRY PASS_REGS) +#define ANSWER_CHECK_INSERT_ENTRY(SG_FR, NODE, ENTRY, INSTR) \ + NODE = answer_trie_check_insert_entry(SG_FR, NODE, ENTRY, INSTR PASS_REGS) #endif /* MODE_GLOBAL_TRIE_LOOP */ - #ifdef INCLUDE_ANSWER_SEARCH_MODE_DIRECTED -#define ANSWER_SAFE_INSERT_ENTRY(NODE, ENTRY, INSTR) \ - { ans_node_ptr new_node; \ - NEW_ANSWER_TRIE_NODE(new_node, INSTR, ENTRY, NULL, NODE, NULL); \ - TrNode_child(NODE) = new_node; \ - NODE = new_node; \ - } +#define ANSWER_SAFE_INSERT_ENTRY(NODE, ENTRY, INSTR) \ + { \ + ans_node_ptr new_node; \ + NEW_ANSWER_TRIE_NODE(new_node, INSTR, ENTRY, NULL, NODE, NULL); \ + TrNode_child(NODE) = new_node; \ + NODE = new_node; \ + } #ifdef THREADS -#define INVALIDATE_ANSWER_TRIE_NODE(NODE, SG_FR) \ - TrNode_next(NODE) = SgFr_invalid_chain(SG_FR); \ - SgFr_invalid_chain(SG_FR) = NODE +#define INVALIDATE_ANSWER_TRIE_NODE(NODE, SG_FR) \ + TrNode_next(NODE) = SgFr_invalid_chain(SG_FR); \ + SgFr_invalid_chain(SG_FR) = NODE #else -#define INVALIDATE_ANSWER_TRIE_NODE(NODE, SG_FR) \ - FREE_ANSWER_TRIE_NODE(NODE) +#define INVALIDATE_ANSWER_TRIE_NODE(NODE, SG_FR) FREE_ANSWER_TRIE_NODE(NODE) #endif /* THREADS */ -#define INVALIDATE_ANSWER_TRIE_LEAF_NODE(NODE, SG_FR) \ - TAG_AS_ANSWER_INVALID_NODE(NODE); \ - TrNode_next(NODE) = SgFr_invalid_chain(SG_FR); \ - SgFr_invalid_chain(SG_FR) = NODE +#define INVALIDATE_ANSWER_TRIE_LEAF_NODE(NODE, SG_FR) \ + TAG_AS_ANSWER_INVALID_NODE(NODE); \ + TrNode_next(NODE) = SgFr_invalid_chain(SG_FR); \ + SgFr_invalid_chain(SG_FR) = NODE #endif /* INCLUDE_ANSWER_SEARCH_MODE_DIRECTED */ - - /************************************************************************ ** subgoal_trie_check_insert_(gt)_entry ** ************************************************************************/ #ifdef INCLUDE_SUBGOAL_TRIE_CHECK_INSERT -#ifndef SUBGOAL_TRIE_LOCK_AT_WRITE_LEVEL /* SUBGOAL_TRIE_LOCK_AT_ENTRY_LEVEL || SUBGOAL_TRIE_LOCK_AT_NODE_LEVEL || ! YAPOR */ +#ifndef SUBGOAL_TRIE_LOCK_AT_WRITE_LEVEL /* SUBGOAL_TRIE_LOCK_AT_ENTRY_LEVEL \ + || SUBGOAL_TRIE_LOCK_AT_NODE_LEVEL \ + || ! YAPOR */ #ifdef MODE_GLOBAL_TRIE_ENTRY -static inline sg_node_ptr subgoal_trie_check_insert_gt_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, Term t USES_REGS) { +static inline sg_node_ptr +subgoal_trie_check_insert_gt_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, + Term t USES_REGS) { #else -static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, Term t USES_REGS) { +static inline sg_node_ptr +subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, + Term t USES_REGS) { #endif /* MODE_GLOBAL_TRIE_ENTRY */ sg_node_ptr child_node; @@ -97,7 +101,7 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s return child_node; } - if (! IS_SUBGOAL_TRIE_HASH(child_node)) { + if (!IS_SUBGOAL_TRIE_HASH(child_node)) { int count_nodes = 0; do { if (TrNode_entry(child_node) == t) { @@ -107,7 +111,8 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s count_nodes++; child_node = TrNode_next(child_node); } while (child_node); - NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, TrNode_child(parent_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 */ @@ -116,13 +121,14 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s new_subgoal_trie_hash(hash, count_nodes, tab_ent); chain_node = child_node; do { - bucket = Hash_buckets(hash) + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); + bucket = Hash_buckets(hash) + + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); 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) = (sg_node_ptr) hash; + TrNode_child(parent_node) = (sg_node_ptr)hash; } else { TrNode_child(parent_node) = child_node; } @@ -134,7 +140,7 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s sg_hash_ptr hash; sg_node_ptr *bucket; int count_nodes = 0; - hash = (sg_hash_ptr) child_node; + hash = (sg_hash_ptr)child_node; bucket = Hash_buckets(hash) + HASH_ENTRY(t, Hash_num_buckets(hash)); child_node = *bucket; while (child_node) { @@ -149,9 +155,11 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s *bucket = child_node; Hash_num_nodes(hash)++; count_nodes++; - if (count_nodes >= MAX_NODES_PER_BUCKET && Hash_num_nodes(hash) > Hash_num_buckets(hash)) { + 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, *old_bucket, *old_hash_buckets, *new_hash_buckets; + sg_node_ptr chain_node, next_node, *old_bucket, *old_hash_buckets, + *new_hash_buckets; int num_buckets; num_buckets = Hash_num_buckets(hash) * 2; ALLOC_BUCKETS(new_hash_buckets, num_buckets); @@ -161,7 +169,8 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = new_hash_buckets + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); + bucket = new_hash_buckets + + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -179,9 +188,13 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s } #else /* SUBGOAL_TRIE_LOCK_AT_WRITE_LEVEL */ #ifdef MODE_GLOBAL_TRIE_ENTRY -static inline sg_node_ptr subgoal_trie_check_insert_gt_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, Term t USES_REGS) { +static inline sg_node_ptr +subgoal_trie_check_insert_gt_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, + Term t USES_REGS) { #else -static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, Term t USES_REGS) { +static inline sg_node_ptr +subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, sg_node_ptr parent_node, + Term t USES_REGS) { #endif /* MODE_GLOBAL_TRIE_ENTRY */ sg_node_ptr child_node; sg_hash_ptr hash; @@ -199,7 +212,7 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s FREE_SUBGOAL_TRIE_NODE(child_node); #endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ UNLOCK_SUBGOAL_NODE(parent_node); - hash = (sg_hash_ptr) chain_node; + hash = (sg_hash_ptr)chain_node; goto subgoal_trie_hash; } do { @@ -215,7 +228,8 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s #ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK TrNode_next(child_node) = TrNode_child(parent_node); #else - NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, TrNode_child(parent_node)); + NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, + TrNode_child(parent_node)); } else { NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, NULL); #endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ @@ -223,9 +237,9 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s TrNode_child(parent_node) = child_node; UNLOCK_SUBGOAL_NODE(parent_node); return child_node; - } + } - if (! IS_SUBGOAL_TRIE_HASH(child_node)) { + if (!IS_SUBGOAL_TRIE_HASH(child_node)) { sg_node_ptr first_node = child_node; int count_nodes = 0; do { @@ -245,7 +259,7 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s FREE_SUBGOAL_TRIE_NODE(child_node); #endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ UNLOCK_SUBGOAL_NODE(parent_node); - hash = (sg_hash_ptr) chain_node; + hash = (sg_hash_ptr)chain_node; goto subgoal_trie_hash; } do { @@ -262,7 +276,8 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s #ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK TrNode_next(child_node) = TrNode_child(parent_node); #else - NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, TrNode_child(parent_node)); + NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, + TrNode_child(parent_node)); } else { NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); #endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ @@ -274,13 +289,14 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s new_subgoal_trie_hash(hash, count_nodes, tab_ent); chain_node = child_node; do { - bucket = Hash_buckets(hash) + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); + bucket = Hash_buckets(hash) + + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); 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) = (sg_node_ptr) hash; + TrNode_child(parent_node) = (sg_node_ptr)hash; } else { TrNode_child(parent_node) = child_node; } @@ -288,106 +304,113 @@ static inline sg_node_ptr subgoal_trie_check_insert_entry(tab_ent_ptr tab_ent, s return child_node; } - hash = (sg_hash_ptr) child_node; -subgoal_trie_hash: - { /* trie nodes with hashing */ - sg_node_ptr *bucket, first_node; - int num_buckets, count_nodes = 0; + hash = (sg_hash_ptr)child_node; +subgoal_trie_hash : { /* trie nodes with hashing */ + sg_node_ptr *bucket, first_node; + int num_buckets, count_nodes = 0; - do { - num_buckets = Hash_num_buckets(hash); - // __sync_synchronize(); - bucket = Hash_buckets(hash) + HASH_ENTRY(t, num_buckets); - first_node = child_node = *bucket; - } while (num_buckets != Hash_num_buckets(hash)); - while (child_node) { - if (TrNode_entry(child_node) == t) - return child_node; - count_nodes++; - child_node = TrNode_next(child_node); - } + do { + num_buckets = Hash_num_buckets(hash); + // __sync_synchronize(); + bucket = Hash_buckets(hash) + HASH_ENTRY(t, num_buckets); + first_node = child_node = *bucket; + } while (num_buckets != Hash_num_buckets(hash)); + while (child_node) { + if (TrNode_entry(child_node) == t) + return child_node; + count_nodes++; + child_node = TrNode_next(child_node); + } #ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK + NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); +#endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ + LOCK_SUBGOAL_NODE(parent_node); + if (num_buckets != Hash_num_buckets(hash)) { +/* the hash has been expanded */ +#ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK + FREE_SUBGOAL_TRIE_NODE(child_node); +#endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ + UNLOCK_SUBGOAL_NODE(parent_node); + goto subgoal_trie_hash; + } + if (first_node != *bucket) { + sg_node_ptr chain_node = *bucket; + do { + if (TrNode_entry(chain_node) == t) { +#ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK + FREE_SUBGOAL_TRIE_NODE(child_node); +#endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ + UNLOCK_SUBGOAL_NODE(parent_node); + return chain_node; + } + count_nodes++; + chain_node = TrNode_next(chain_node); + } while (chain_node != first_node); +#ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK + TrNode_next(child_node) = *bucket; +#else + NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, *bucket); + } else { NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); #endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ - LOCK_SUBGOAL_NODE(parent_node); - if (num_buckets != Hash_num_buckets(hash)) { - /* the hash has been expanded */ -#ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK - FREE_SUBGOAL_TRIE_NODE(child_node); -#endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ - UNLOCK_SUBGOAL_NODE(parent_node); - goto subgoal_trie_hash; - } - if (first_node != *bucket) { - sg_node_ptr chain_node = *bucket; - do { - if (TrNode_entry(chain_node) == t) { -#ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK - FREE_SUBGOAL_TRIE_NODE(child_node); -#endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ - UNLOCK_SUBGOAL_NODE(parent_node); - return chain_node; - } - count_nodes++; - chain_node = TrNode_next(chain_node); - } while (chain_node != first_node); -#ifdef SUBGOAL_TRIE_ALLOC_BEFORE_CHECK - TrNode_next(child_node) = *bucket; -#else - NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, *bucket); - } else { - NEW_SUBGOAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); -#endif /* SUBGOAL_TRIE_ALLOC_BEFORE_CHECK */ - } - *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, *old_bucket, *old_hash_buckets, *new_hash_buckets; - num_buckets = Hash_num_buckets(hash) * 2; - ALLOC_BUCKETS(new_hash_buckets, num_buckets); - old_hash_buckets = Hash_buckets(hash); - old_bucket = old_hash_buckets + Hash_num_buckets(hash); - do { - if (*--old_bucket) { - chain_node = *old_bucket; - do { - bucket = new_hash_buckets + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); - next_node = TrNode_next(chain_node); - TrNode_next(chain_node) = *bucket; - *bucket = chain_node; - chain_node = next_node; - } while (chain_node); - } - } while (old_bucket != old_hash_buckets); - Hash_buckets(hash) = new_hash_buckets; - Hash_num_buckets(hash) = num_buckets; - FREE_BUCKETS(old_hash_buckets); - } - UNLOCK_SUBGOAL_NODE(parent_node); - return child_node; } + *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, *old_bucket, *old_hash_buckets, + *new_hash_buckets; + num_buckets = Hash_num_buckets(hash) * 2; + ALLOC_BUCKETS(new_hash_buckets, num_buckets); + old_hash_buckets = Hash_buckets(hash); + old_bucket = old_hash_buckets + Hash_num_buckets(hash); + do { + if (*--old_bucket) { + chain_node = *old_bucket; + do { + bucket = new_hash_buckets + + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); + next_node = TrNode_next(chain_node); + TrNode_next(chain_node) = *bucket; + *bucket = chain_node; + chain_node = next_node; + } while (chain_node); + } + } while (old_bucket != old_hash_buckets); + Hash_buckets(hash) = new_hash_buckets; + Hash_num_buckets(hash) = num_buckets; + FREE_BUCKETS(old_hash_buckets); + } + UNLOCK_SUBGOAL_NODE(parent_node); + return child_node; +} } #endif /* SUBGOAL_TRIE_LOCK_LEVEL */ #endif /* INCLUDE_SUBGOAL_TRIE_CHECK_INSERT */ - - /************************************************************************ ** answer_trie_check_insert_(gt)_entry ** ************************************************************************/ #ifdef INCLUDE_ANSWER_TRIE_CHECK_INSERT -#ifndef ANSWER_TRIE_LOCK_AT_WRITE_LEVEL /* ANSWER_TRIE_LOCK_AT_ENTRY_LEVEL || ANSWER_TRIE_LOCK_AT_NODE_LEVEL || ! YAPOR */ +#ifndef ANSWER_TRIE_LOCK_AT_WRITE_LEVEL /* ANSWER_TRIE_LOCK_AT_ENTRY_LEVEL || \ + ANSWER_TRIE_LOCK_AT_NODE_LEVEL || ! \ + YAPOR */ #ifdef MODE_GLOBAL_TRIE_ENTRY -static inline ans_node_ptr answer_trie_check_insert_gt_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, Term t, int instr USES_REGS) { +static inline ans_node_ptr +answer_trie_check_insert_gt_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, + Term t, int instr USES_REGS) { #else -static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, Term t, int instr USES_REGS) { +static inline ans_node_ptr +answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, + Term t, int instr USES_REGS) { #endif /* MODE_GLOBAL_TRIE_ENTRY */ ans_node_ptr child_node; - TABLING_ERROR_CHECKING(answer_trie_check_insert_(gt)_entry, IS_ANSWER_LEAF_NODE(parent_node)); + TABLING_ERROR_CHECKING(answer_trie_check_insert_(gt) _entry, + IS_ANSWER_LEAF_NODE(parent_node)); LOCK_ANSWER_NODE(parent_node); child_node = TrNode_child(parent_node); if (child_node == NULL) { @@ -397,7 +420,7 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n return child_node; } - if (! IS_ANSWER_TRIE_HASH(child_node)) { + if (!IS_ANSWER_TRIE_HASH(child_node)) { int count_nodes = 0; do { if (TrNode_entry(child_node) == t) { @@ -407,7 +430,8 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n count_nodes++; child_node = TrNode_next(child_node); } while (child_node); - NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, TrNode_child(parent_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 */ @@ -416,13 +440,14 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n new_answer_trie_hash(hash, count_nodes, sg_fr); chain_node = child_node; do { - bucket = Hash_buckets(hash) + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); + bucket = Hash_buckets(hash) + + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); 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) = (ans_node_ptr) hash; + TrNode_child(parent_node) = (ans_node_ptr)hash; } else { TrNode_child(parent_node) = child_node; } @@ -434,7 +459,7 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n ans_hash_ptr hash; ans_node_ptr *bucket; int count_nodes = 0; - hash = (ans_hash_ptr) child_node; + hash = (ans_hash_ptr)child_node; bucket = Hash_buckets(hash) + HASH_ENTRY(t, Hash_num_buckets(hash)); child_node = *bucket; while (child_node) { @@ -449,9 +474,11 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n *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, *old_bucket, *old_hash_buckets, *new_hash_buckets; + 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, *old_bucket, *old_hash_buckets, + *new_hash_buckets; int num_buckets; num_buckets = Hash_num_buckets(hash) * 2; ALLOC_BUCKETS(new_hash_buckets, num_buckets); @@ -461,7 +488,8 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = new_hash_buckets + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); + bucket = new_hash_buckets + + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -479,14 +507,19 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n } #else /* ANSWER_TRIE_LOCK_AT_WRITE_LEVEL */ #ifdef MODE_GLOBAL_TRIE_ENTRY -static inline ans_node_ptr answer_trie_check_insert_gt_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, Term t, int instr USES_REGS) { +static inline ans_node_ptr +answer_trie_check_insert_gt_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, + Term t, int instr USES_REGS) { #else -static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, Term t, int instr USES_REGS) { +static inline ans_node_ptr +answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_node_ptr parent_node, + Term t, int instr USES_REGS) { #endif /* MODE_GLOBAL_TRIE_ENTRY */ ans_node_ptr child_node; ans_hash_ptr hash; - TABLING_ERROR_CHECKING(answer_trie_check_insert_(gt)_entry, IS_ANSWER_LEAF_NODE(parent_node)); + TABLING_ERROR_CHECKING(answer_trie_check_insert_(gt) _entry, + IS_ANSWER_LEAF_NODE(parent_node)); child_node = TrNode_child(parent_node); if (child_node == NULL) { #ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK @@ -500,7 +533,7 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n FREE_ANSWER_TRIE_NODE(child_node); #endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ UNLOCK_ANSWER_NODE(parent_node); - hash = (ans_hash_ptr) chain_node; + hash = (ans_hash_ptr)chain_node; goto answer_trie_hash; } do { @@ -516,7 +549,8 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n #ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK TrNode_next(child_node) = TrNode_child(parent_node); #else - NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, TrNode_child(parent_node)); + NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, + TrNode_child(parent_node)); } else { NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, NULL); #endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ @@ -524,9 +558,9 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n TrNode_child(parent_node) = child_node; UNLOCK_ANSWER_NODE(parent_node); return child_node; - } + } - if (! IS_ANSWER_TRIE_HASH(child_node)) { + if (!IS_ANSWER_TRIE_HASH(child_node)) { ans_node_ptr first_node = child_node; int count_nodes = 0; do { @@ -546,7 +580,7 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n FREE_ANSWER_TRIE_NODE(child_node); #endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ UNLOCK_ANSWER_NODE(parent_node); - hash = (ans_hash_ptr) chain_node; + hash = (ans_hash_ptr)chain_node; goto answer_trie_hash; } do { @@ -563,7 +597,8 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n #ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK TrNode_next(child_node) = TrNode_child(parent_node); #else - NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, TrNode_child(parent_node)); + NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, + TrNode_child(parent_node)); } else { NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, first_node); #endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ @@ -575,13 +610,14 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n new_answer_trie_hash(hash, count_nodes, sg_fr); chain_node = child_node; do { - bucket = Hash_buckets(hash) + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); + bucket = Hash_buckets(hash) + + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); 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) = (ans_node_ptr) hash; + TrNode_child(parent_node) = (ans_node_ptr)hash; } else { TrNode_child(parent_node) = child_node; } @@ -589,105 +625,108 @@ static inline ans_node_ptr answer_trie_check_insert_entry(sg_fr_ptr sg_fr, ans_n return child_node; } - hash = (ans_hash_ptr) child_node; -answer_trie_hash: - { /* trie nodes with hashing */ - ans_node_ptr *bucket, first_node; - int num_buckets, count_nodes = 0; + hash = (ans_hash_ptr)child_node; +answer_trie_hash : { /* trie nodes with hashing */ + ans_node_ptr *bucket, first_node; + int num_buckets, count_nodes = 0; - do { - num_buckets = Hash_num_buckets(hash); - // __sync_synchronize(); - bucket = Hash_buckets(hash) + HASH_ENTRY(t, num_buckets); - first_node = child_node = *bucket; - } while (num_buckets != Hash_num_buckets(hash)); - while (child_node) { - if (TrNode_entry(child_node) == t) - return child_node; - count_nodes++; - child_node = TrNode_next(child_node); - } + do { + num_buckets = Hash_num_buckets(hash); + // __sync_synchronize(); + bucket = Hash_buckets(hash) + HASH_ENTRY(t, num_buckets); + first_node = child_node = *bucket; + } while (num_buckets != Hash_num_buckets(hash)); + while (child_node) { + if (TrNode_entry(child_node) == t) + return child_node; + count_nodes++; + child_node = TrNode_next(child_node); + } #ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK + NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, first_node); +#endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ + LOCK_ANSWER_NODE(parent_node); + if (num_buckets != Hash_num_buckets(hash)) { +/* the hash has been expanded */ +#ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK + FREE_ANSWER_TRIE_NODE(child_node); +#endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ + UNLOCK_ANSWER_NODE(parent_node); + goto answer_trie_hash; + } + if (first_node != *bucket) { + ans_node_ptr chain_node = *bucket; + do { + if (TrNode_entry(chain_node) == t) { +#ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK + FREE_ANSWER_TRIE_NODE(child_node); +#endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ + UNLOCK_ANSWER_NODE(parent_node); + return chain_node; + } + count_nodes++; + chain_node = TrNode_next(chain_node); + } while (chain_node != first_node); +#ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK + TrNode_next(child_node) = *bucket; +#else + NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, *bucket); + } else { NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, first_node); #endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ - LOCK_ANSWER_NODE(parent_node); - if (num_buckets != Hash_num_buckets(hash)) { - /* the hash has been expanded */ -#ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK - FREE_ANSWER_TRIE_NODE(child_node); -#endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ - UNLOCK_ANSWER_NODE(parent_node); - goto answer_trie_hash; - } - if (first_node != *bucket) { - ans_node_ptr chain_node = *bucket; - do { - if (TrNode_entry(chain_node) == t) { -#ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK - FREE_ANSWER_TRIE_NODE(child_node); -#endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ - UNLOCK_ANSWER_NODE(parent_node); - return chain_node; - } - count_nodes++; - chain_node = TrNode_next(chain_node); - } while (chain_node != first_node); -#ifdef ANSWER_TRIE_ALLOC_BEFORE_CHECK - TrNode_next(child_node) = *bucket; -#else - NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, *bucket); - } else { - NEW_ANSWER_TRIE_NODE(child_node, instr, t, NULL, parent_node, first_node); -#endif /* ANSWER_TRIE_ALLOC_BEFORE_CHECK */ - } - *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, *old_bucket, *old_hash_buckets, *new_hash_buckets; - num_buckets = Hash_num_buckets(hash) * 2; - ALLOC_BUCKETS(new_hash_buckets, num_buckets); - old_hash_buckets = Hash_buckets(hash); - old_bucket = old_hash_buckets + Hash_num_buckets(hash); - do { - if (*--old_bucket) { - chain_node = *old_bucket; - do { - bucket = new_hash_buckets + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); - next_node = TrNode_next(chain_node); - TrNode_next(chain_node) = *bucket; - *bucket = chain_node; - chain_node = next_node; - } while (chain_node); - } - } while (old_bucket != old_hash_buckets); - Hash_buckets(hash) = new_hash_buckets; - Hash_num_buckets(hash) = num_buckets; - FREE_BUCKETS(old_hash_buckets); - } - UNLOCK_ANSWER_NODE(parent_node); - return child_node; } + *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, *old_bucket, *old_hash_buckets, + *new_hash_buckets; + num_buckets = Hash_num_buckets(hash) * 2; + ALLOC_BUCKETS(new_hash_buckets, num_buckets); + old_hash_buckets = Hash_buckets(hash); + old_bucket = old_hash_buckets + Hash_num_buckets(hash); + do { + if (*--old_bucket) { + chain_node = *old_bucket; + do { + bucket = new_hash_buckets + + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); + next_node = TrNode_next(chain_node); + TrNode_next(chain_node) = *bucket; + *bucket = chain_node; + chain_node = next_node; + } while (chain_node); + } + } while (old_bucket != old_hash_buckets); + Hash_buckets(hash) = new_hash_buckets; + Hash_num_buckets(hash) = num_buckets; + FREE_BUCKETS(old_hash_buckets); + } + UNLOCK_ANSWER_NODE(parent_node); + return child_node; +} } #endif /* ANSWER_TRIE_LOCK_LEVEL */ #endif /* INCLUDE_ANSWER_TRIE_CHECK_INSERT */ - - /************************************************************************ ** global_trie_check_insert_(gt)_entry ** ************************************************************************/ #ifdef INCLUDE_GLOBAL_TRIE_CHECK_INSERT -#ifndef GLOBAL_TRIE_LOCK_AT_WRITE_LEVEL /* GLOBAL_TRIE_LOCK_AT_NODE_LEVEL || ! YAPOR */ +#ifndef GLOBAL_TRIE_LOCK_AT_WRITE_LEVEL /* GLOBAL_TRIE_LOCK_AT_NODE_LEVEL || ! \ + YAPOR */ #ifdef MODE_GLOBAL_TRIE_ENTRY -static inline gt_node_ptr global_trie_check_insert_gt_entry(gt_node_ptr parent_node, Term t USES_REGS) { +static inline gt_node_ptr +global_trie_check_insert_gt_entry(gt_node_ptr parent_node, Term t USES_REGS) { #else -static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node, Term t USES_REGS) { +static inline gt_node_ptr +global_trie_check_insert_entry(gt_node_ptr parent_node, Term t USES_REGS) { #endif /* MODE_GLOBAL_TRIE_ENTRY */ gt_node_ptr child_node; - + LOCK_GLOBAL_NODE(parent_node); child_node = TrNode_child(parent_node); if (child_node == NULL) { @@ -697,7 +736,7 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node return child_node; } - if (! IS_GLOBAL_TRIE_HASH(child_node)) { + if (!IS_GLOBAL_TRIE_HASH(child_node)) { int count_nodes = 0; do { if (TrNode_entry(child_node) == t) { @@ -707,7 +746,8 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_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)); + 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 */ @@ -716,13 +756,14 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node new_global_trie_hash(hash, count_nodes); chain_node = child_node; do { - bucket = Hash_buckets(hash) + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); + bucket = Hash_buckets(hash) + + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); 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; + TrNode_child(parent_node) = (gt_node_ptr)hash; } else { TrNode_child(parent_node) = child_node; } @@ -734,24 +775,26 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node gt_hash_ptr hash; gt_node_ptr *bucket; int count_nodes = 0; - hash = (gt_hash_ptr) child_node; + hash = (gt_hash_ptr)child_node; bucket = Hash_buckets(hash) + HASH_ENTRY(t, Hash_num_buckets(hash)); child_node = *bucket; - while (child_node) { + while (child_node) { if (TrNode_entry(child_node) == t) { UNLOCK_GLOBAL_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)) { + 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, *old_bucket, *old_hash_buckets, *new_hash_buckets; + gt_node_ptr chain_node, next_node, *old_bucket, *old_hash_buckets, + *new_hash_buckets; int num_buckets; num_buckets = Hash_num_buckets(hash) * 2; ALLOC_BUCKETS(new_hash_buckets, num_buckets); @@ -761,7 +804,8 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node if (*--old_bucket) { chain_node = *old_bucket; do { - bucket = new_hash_buckets + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); + bucket = new_hash_buckets + + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); next_node = TrNode_next(chain_node); TrNode_next(chain_node) = *bucket; *bucket = chain_node; @@ -779,9 +823,11 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node } #else /* GLOBAL_TRIE_LOCK_AT_WRITE_LEVEL */ #ifdef MODE_GLOBAL_TRIE_ENTRY -static inline gt_node_ptr global_trie_check_insert_gt_entry(gt_node_ptr parent_node, Term t USES_REGS) { +static inline gt_node_ptr +global_trie_check_insert_gt_entry(gt_node_ptr parent_node, Term t USES_REGS) { #else -static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node, Term t USES_REGS) { +static inline gt_node_ptr +global_trie_check_insert_entry(gt_node_ptr parent_node, Term t USES_REGS) { #endif /* MODE_GLOBAL_TRIE_ENTRY */ gt_node_ptr child_node; gt_hash_ptr hash; @@ -799,7 +845,7 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node FREE_GLOBAL_TRIE_NODE(child_node); #endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ UNLOCK_GLOBAL_NODE(parent_node); - hash = (gt_hash_ptr) chain_node; + hash = (gt_hash_ptr)chain_node; goto global_trie_hash; } do { @@ -815,7 +861,8 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node #ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK TrNode_next(child_node) = TrNode_child(parent_node); #else - NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, TrNode_child(parent_node)); + NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, + TrNode_child(parent_node)); } else { NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, NULL); #endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ @@ -823,9 +870,9 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node TrNode_child(parent_node) = child_node; UNLOCK_GLOBAL_NODE(parent_node); return child_node; - } + } - if (! IS_GLOBAL_TRIE_HASH(child_node)) { + if (!IS_GLOBAL_TRIE_HASH(child_node)) { gt_node_ptr first_node = child_node; int count_nodes = 0; do { @@ -845,7 +892,7 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node FREE_GLOBAL_TRIE_NODE(child_node); #endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ UNLOCK_GLOBAL_NODE(parent_node); - hash = (gt_hash_ptr) chain_node; + hash = (gt_hash_ptr)chain_node; goto global_trie_hash; } do { @@ -862,7 +909,8 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node #ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK TrNode_next(child_node) = TrNode_child(parent_node); #else - NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, TrNode_child(parent_node)); + NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, + TrNode_child(parent_node)); } else { NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); #endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ @@ -874,13 +922,14 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node new_global_trie_hash(hash, count_nodes); chain_node = child_node; do { - bucket = Hash_buckets(hash) + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); + bucket = Hash_buckets(hash) + + HASH_ENTRY(TrNode_entry(chain_node), BASE_HASH_BUCKETS); 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; + TrNode_child(parent_node) = (gt_node_ptr)hash; } else { TrNode_child(parent_node) = child_node; } @@ -888,92 +937,92 @@ static inline gt_node_ptr global_trie_check_insert_entry(gt_node_ptr parent_node return child_node; } - hash = (gt_hash_ptr) child_node; -global_trie_hash: - { /* trie nodes with hashing */ - gt_node_ptr *bucket, first_node; - int num_buckets, count_nodes = 0; + hash = (gt_hash_ptr)child_node; +global_trie_hash : { /* trie nodes with hashing */ + gt_node_ptr *bucket, first_node; + int num_buckets, count_nodes = 0; - do { - num_buckets = Hash_num_buckets(hash); - // __sync_synchronize(); - bucket = Hash_buckets(hash) + HASH_ENTRY(t, num_buckets); - first_node = child_node = *bucket; - } while (num_buckets != Hash_num_buckets(hash)); - while (child_node) { - if (TrNode_entry(child_node) == t) - return child_node; - count_nodes++; - child_node = TrNode_next(child_node); - } + do { + num_buckets = Hash_num_buckets(hash); + // __sync_synchronize(); + bucket = Hash_buckets(hash) + HASH_ENTRY(t, num_buckets); + first_node = child_node = *bucket; + } while (num_buckets != Hash_num_buckets(hash)); + while (child_node) { + if (TrNode_entry(child_node) == t) + return child_node; + count_nodes++; + child_node = TrNode_next(child_node); + } #ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK + NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); +#endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ + LOCK_GLOBAL_NODE(parent_node); + if (num_buckets != Hash_num_buckets(hash)) { +/* the hash has been expanded */ +#ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK + FREE_GLOBAL_TRIE_NODE(child_node); +#endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ + UNLOCK_GLOBAL_NODE(parent_node); + goto global_trie_hash; + } + if (first_node != *bucket) { + gt_node_ptr chain_node = *bucket; + do { + if (TrNode_entry(chain_node) == t) { +#ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK + FREE_GLOBAL_TRIE_NODE(child_node); +#endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ + UNLOCK_GLOBAL_NODE(parent_node); + return chain_node; + } + count_nodes++; + chain_node = TrNode_next(chain_node); + } while (chain_node != first_node); +#ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK + TrNode_next(child_node) = *bucket; +#else + NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, *bucket); + } else { NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); #endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ - LOCK_GLOBAL_NODE(parent_node); - if (num_buckets != Hash_num_buckets(hash)) { - /* the hash has been expanded */ -#ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK - FREE_GLOBAL_TRIE_NODE(child_node); -#endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ - UNLOCK_GLOBAL_NODE(parent_node); - goto global_trie_hash; - } - if (first_node != *bucket) { - gt_node_ptr chain_node = *bucket; - do { - if (TrNode_entry(chain_node) == t) { -#ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK - FREE_GLOBAL_TRIE_NODE(child_node); -#endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ - UNLOCK_GLOBAL_NODE(parent_node); - return chain_node; - } - count_nodes++; - chain_node = TrNode_next(chain_node); - } while (chain_node != first_node); -#ifdef GLOBAL_TRIE_ALLOC_BEFORE_CHECK - TrNode_next(child_node) = *bucket; -#else - NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, *bucket); - } else { - NEW_GLOBAL_TRIE_NODE(child_node, t, NULL, parent_node, first_node); -#endif /* GLOBAL_TRIE_ALLOC_BEFORE_CHECK */ - } - *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, *old_bucket, *old_hash_buckets, *new_hash_buckets; - num_buckets = Hash_num_buckets(hash) * 2; - ALLOC_BUCKETS(new_hash_buckets, num_buckets); - old_hash_buckets = Hash_buckets(hash); - old_bucket = old_hash_buckets + Hash_num_buckets(hash); - do { - if (*--old_bucket) { - chain_node = *old_bucket; - do { - bucket = new_hash_buckets + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); - next_node = TrNode_next(chain_node); - TrNode_next(chain_node) = *bucket; - *bucket = chain_node; - chain_node = next_node; - } while (chain_node); - } - } while (old_bucket != old_hash_buckets); - Hash_buckets(hash) = new_hash_buckets; - Hash_num_buckets(hash) = num_buckets; - FREE_BUCKETS(old_hash_buckets); - } - UNLOCK_GLOBAL_NODE(parent_node); - return child_node; } + *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, *old_bucket, *old_hash_buckets, + *new_hash_buckets; + num_buckets = Hash_num_buckets(hash) * 2; + ALLOC_BUCKETS(new_hash_buckets, num_buckets); + old_hash_buckets = Hash_buckets(hash); + old_bucket = old_hash_buckets + Hash_num_buckets(hash); + do { + if (*--old_bucket) { + chain_node = *old_bucket; + do { + bucket = new_hash_buckets + + HASH_ENTRY(TrNode_entry(chain_node), num_buckets); + next_node = TrNode_next(chain_node); + TrNode_next(chain_node) = *bucket; + *bucket = chain_node; + chain_node = next_node; + } while (chain_node); + } + } while (old_bucket != old_hash_buckets); + Hash_buckets(hash) = new_hash_buckets; + Hash_num_buckets(hash) = num_buckets; + FREE_BUCKETS(old_hash_buckets); + } + UNLOCK_GLOBAL_NODE(parent_node); + return child_node; +} } #endif /* GLOBAL_TRIE_LOCK_LEVEL */ #endif /* INCLUDE_GLOBAL_TRIE_CHECK_INSERT */ - - /************************************************************************ ** subgoal_search(_global_trie)(_terms)_loop ** ************************************************************************/ @@ -981,15 +1030,26 @@ global_trie_hash: #ifdef INCLUDE_SUBGOAL_SEARCH_LOOP #ifdef MODE_GLOBAL_TRIE_LOOP #ifdef GLOBAL_TRIE_FOR_SUBTERMS -static inline gt_node_ptr subgoal_search_global_trie_terms_loop(Term t, int *subs_arity_ptr, CELL **stack_vars_ptr, CELL *stack_terms USES_REGS) { +static inline gt_node_ptr +subgoal_search_global_trie_terms_loop(Term t, int *subs_arity_ptr, + CELL **stack_vars_ptr, + CELL *stack_terms USES_REGS) { #else -static inline gt_node_ptr subgoal_search_global_trie_loop(Term t, int *subs_arity_ptr, CELL **stack_vars_ptr USES_REGS) { +static inline gt_node_ptr +subgoal_search_global_trie_loop(Term t, int *subs_arity_ptr, + CELL **stack_vars_ptr USES_REGS) { #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ #else #ifdef MODE_TERMS_LOOP -static inline sg_node_ptr subgoal_search_terms_loop(tab_ent_ptr tab_ent, sg_node_ptr current_node, Term t, int *subs_arity_ptr, CELL **stack_vars_ptr USES_REGS) { +static inline sg_node_ptr +subgoal_search_terms_loop(tab_ent_ptr tab_ent, sg_node_ptr current_node, Term t, + int *subs_arity_ptr, + CELL **stack_vars_ptr USES_REGS) { #else -static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr current_node, Term t, int *subs_arity_ptr, CELL **stack_vars_ptr USES_REGS) { +static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, + sg_node_ptr current_node, Term t, + int *subs_arity_ptr, + CELL **stack_vars_ptr USES_REGS) { #endif /* MODE_TERMS_LOOP */ #endif /* MODE_GLOBAL_TRIE_LOOP */ /************************************************************************ @@ -1036,22 +1096,25 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c #endif /* MODE_GLOBAL_TRIE_LOOP */ int subs_arity = *subs_arity_ptr; CELL *stack_vars = *stack_vars_ptr; -#if ! defined(MODE_GLOBAL_TRIE_LOOP) || ! defined(GLOBAL_TRIE_FOR_SUBTERMS) - CELL *stack_terms = (CELL *) LOCAL_TrailTop; +#if !defined(MODE_GLOBAL_TRIE_LOOP) || !defined(GLOBAL_TRIE_FOR_SUBTERMS) + CELL *stack_terms = (CELL *)LOCAL_TrailTop; #endif /* ! MODE_GLOBAL_TRIE_LOOP || ! GLOBAL_TRIE_FOR_SUBTERMS */ - CELL *stack_terms_limit = (CELL *) TR; - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); /* + 1 because initially we stiil haven't done any STACK_POP_DOWN */ + CELL *stack_terms_limit = (CELL *)TR; + AUX_STACK_CHECK_EXPAND( + stack_terms, stack_terms_limit + 1); /* + 1 because initially we stiil + haven't done any STACK_POP_DOWN */ STACK_PUSH_UP(NULL, stack_terms); #if defined(MODE_GLOBAL_TRIE_LOOP) - /* for the global trie, it is safe to skip the IsVarTerm() and IsAtomOrIntTerm() tests in the first iteration */ + /* for the global trie, it is safe to skip the IsVarTerm() and + * IsAtomOrIntTerm() tests in the first iteration */ goto subgoal_search_loop_non_atomic; #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_RATIONAL_TERMS /* Needed structures, variables to support rational terms */ term_array Ts; - void* CyclicTerm; + void *CyclicTerm; term_array_init(&Ts, 10); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ @@ -1062,7 +1125,8 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); } else { if (subs_arity == MAX_TABLE_VARS) - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "subgoal_search_loop: MAX_TABLE_VARS exceeded"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "subgoal_search_loop: MAX_TABLE_VARS exceeded"); STACK_PUSH_UP(t, stack_vars); *((CELL *)t) = GLOBAL_table_var_enumerator(subs_arity); t = MakeTableVarTerm(subs_arity); @@ -1075,22 +1139,25 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c } else { gt_node_ptr entry_node; #ifdef GLOBAL_TRIE_FOR_SUBTERMS - entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); + entry_node = subgoal_search_global_trie_terms_loop( + t, &subs_arity, &stack_vars, stack_terms PASS_REGS); #else - entry_node = subgoal_search_global_trie_loop(t, &subs_arity, &stack_vars PASS_REGS); + entry_node = subgoal_search_global_trie_loop(t, &subs_arity, + &stack_vars PASS_REGS); #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ - current_node = subgoal_trie_check_insert_gt_entry(tab_ent, current_node, (Term) entry_node PASS_REGS); + current_node = subgoal_trie_check_insert_gt_entry( + tab_ent, current_node, (Term)entry_node PASS_REGS); #else /* ! MODE_TERMS_LOOP */ - } else + } else #ifdef TRIE_RATIONAL_TERMS - if (IsRationalTerm(t)) { + if (IsRationalTerm(t)) { t = STACK_POP_DOWN(stack_terms); SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, t); - } else + } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ #if defined(MODE_GLOBAL_TRIE_LOOP) - /* for the global trie, it is safe to start here in the first iteration */ - subgoal_search_loop_non_atomic: + /* for the global trie, it is safe to start here in the first iteration */ + subgoal_search_loop_non_atomic: #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_COMPACT_PAIRS if (IsPairTerm(t)) { @@ -1101,8 +1168,8 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c if (aux_pair == PairTermMark) { t = STACK_POP_DOWN(stack_terms); #ifdef TRIE_RATIONAL_TERMS - if (IsPairTerm(t) && ! IsRationalTerm(t)) { - term_array_push(&Ts, (void *) t, (void *) current_node); + if (IsPairTerm(t) && !IsRationalTerm(t)) { + term_array_push(&Ts, (void *)t, (void *)current_node); #else if (IsPairTerm(t)) { #endif /* RATIONAL TERM SUPPORT FOR TRIES */ @@ -1110,59 +1177,64 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c t = Deref(aux_pair[1]); #ifdef TRIE_RATIONAL_TERMS if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); + CyclicTerm = term_array_member(Ts, (void *)t); } if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); + if (t == TermNil) { + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, + CompactPairEndList); } else { - /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ - /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** - ** up 3 terms has already initially checked for the CompactPairInit term */ + /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ + /* AUX_STACK_CHECK_EXPAND is not necessary here because the + *situation of pushing ** + ** up 3 terms has already initially checked for the CompactPairInit + *term */ STACK_PUSH_UP(t, stack_terms); STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); } #ifdef TRIE_RATIONAL_TERMS CyclicTerm = NULL; if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + CyclicTerm = term_array_member(Ts, (void *)Deref(aux_pair[0])); if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } else { SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndTerm); STACK_PUSH_UP(t, stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop( + t, &subs_arity, &stack_vars, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry( + current_node, (Term)entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ } else { #ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); + term_array_push(&Ts, (void *)t, (void *)current_node); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairInit); t = Deref(aux_pair[1]); #ifdef TRIE_RATIONAL_TERMS if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); + CyclicTerm = term_array_member(Ts, (void *)t); } if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); - } else + } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { + if (t == TermNil) { SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, CompactPairEndList); } else { AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); @@ -1172,27 +1244,31 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c #ifdef TRIE_RATIONAL_TERMS CyclicTerm = NULL; if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + CyclicTerm = term_array_member(Ts, (void *)Deref(aux_pair[0])); if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop( + t, &subs_arity, &stack_vars, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry( + current_node, (Term)entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ -#else /* ! TRIE_COMPACT_PAIRS */ +#else /* ! TRIE_COMPACT_PAIRS */ #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) - if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop(t, &subs_arity, &stack_vars, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); - } else + if (current_node != GLOBAL_root_gt) { + gt_node_ptr entry_node = subgoal_search_global_trie_terms_loop( + t, &subs_arity, &stack_vars, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry( + current_node, (Term)entry_node PASS_REGS); + } else #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ - if (IsPairTerm(t)) { + if (IsPairTerm(t)) { CELL *aux_pair = RepPair(t); SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsPair(NULL)); AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); @@ -1203,7 +1279,7 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; + Term t_dbl[sizeof(Float) / sizeof(Term)]; Float dbl; } u; u.dbl = FloatOfTerm(t); @@ -1223,37 +1299,40 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); #endif /* MODE_GLOBAL_TRIE_LOOP */ } else if (f == FunctorBigInt || f == FunctorString) { - CELL *new = Yap_HeapStoreOpaqueTerm(t); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, (CELL)new); + CELL *new = Yap_HeapStoreOpaqueTerm(t); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, (CELL) new); #ifdef MODE_GLOBAL_TRIE_LOOP - SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); -#endif /* MODE_GLOBAL_TRIE_LOOP */ + SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); +#endif /* MODE_GLOBAL_TRIE_LOOP */ } else if (f == FunctorDBRef) { - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "subgoal_search_loop: unsupported type tag FunctorDBRef"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "subgoal_search_loop: unsupported type tag FunctorDBRef"); } else { #ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); + term_array_push(&Ts, (void *)t, (void *)current_node); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ int i; CELL *aux_appl = RepAppl(t); SUBGOAL_CHECK_INSERT_ENTRY(tab_ent, current_node, AbsAppl((Term *)f)); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); + AUX_STACK_CHECK_EXPAND(stack_terms, + stack_terms_limit + ArityOfFunctor(f) - 1); for (i = ArityOfFunctor(f); i >= 1; i--) { #ifdef TRIE_RATIONAL_TERMS CyclicTerm = NULL; if (IsVarTerm(aux_appl[i]) || IsApplTerm(aux_appl[i])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_appl[i])); + CyclicTerm = term_array_member(Ts, (void *)Deref(aux_appl[i])); if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); } } } else { - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "subgoal_search_loop: unknown type tag"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "subgoal_search_loop: unknown type tag"); #endif /* MODE_TERMS_LOOP */ } t = STACK_POP_DOWN(stack_terms); @@ -1267,8 +1346,6 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c } #endif /* INCLUDE_SUBGOAL_SEARCH_LOOP */ - - /************************************************************************ ** answer_search(_global_trie)(_terms)_loop ** ************************************************************************/ @@ -1276,15 +1353,22 @@ static inline sg_node_ptr subgoal_search_loop(tab_ent_ptr tab_ent, sg_node_ptr c #ifdef INCLUDE_ANSWER_SEARCH_LOOP #ifdef MODE_GLOBAL_TRIE_LOOP #ifdef GLOBAL_TRIE_FOR_SUBTERMS -static inline gt_node_ptr answer_search_global_trie_terms_loop(Term t, int *vars_arity_ptr, CELL *stack_terms USES_REGS) { +static inline gt_node_ptr +answer_search_global_trie_terms_loop(Term t, int *vars_arity_ptr, + CELL *stack_terms USES_REGS) { #else -static inline gt_node_ptr answer_search_global_trie_loop(Term t, int *vars_arity_ptr USES_REGS) { +static inline gt_node_ptr +answer_search_global_trie_loop(Term t, int *vars_arity_ptr USES_REGS) { #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ #else #ifdef MODE_TERMS_LOOP -static inline ans_node_ptr answer_search_terms_loop(sg_fr_ptr sg_fr, ans_node_ptr current_node, Term t, int *vars_arity_ptr USES_REGS) { +static inline ans_node_ptr +answer_search_terms_loop(sg_fr_ptr sg_fr, ans_node_ptr current_node, Term t, + int *vars_arity_ptr USES_REGS) { #else -static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr current_node, Term t, int *vars_arity_ptr USES_REGS) { +static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, + ans_node_ptr current_node, Term t, + int *vars_arity_ptr USES_REGS) { #endif /* MODE_TERMS_LOOP */ #endif /* MODE_GLOBAL_TRIE_LOOP */ /************************************************************************ @@ -1320,27 +1404,30 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr gt_node_ptr current_node = GLOBAL_root_gt; #endif /* MODE_GLOBAL_TRIE_LOOP */ int vars_arity = *vars_arity_ptr; -#if ! defined(MODE_GLOBAL_TRIE_LOOP) || ! defined(GLOBAL_TRIE_FOR_SUBTERMS) - CELL *stack_terms = (CELL *) LOCAL_TrailTop; +#if !defined(MODE_GLOBAL_TRIE_LOOP) || !defined(GLOBAL_TRIE_FOR_SUBTERMS) + CELL *stack_terms = (CELL *)LOCAL_TrailTop; #endif /* ! MODE_GLOBAL_TRIE_LOOP || ! GLOBAL_TRIE_FOR_SUBTERMS */ - CELL *stack_vars_base = (CELL *) TR; + CELL *stack_vars_base = (CELL *)TR; #define stack_terms_limit (stack_vars_base + vars_arity) #ifdef TRIE_COMPACT_PAIRS int in_pair = 0; #else #define in_pair 0 #endif /* TRIE_COMPACT_PAIRS */ - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); /* + 1 because initially we stiil haven't done any STACK_POP_DOWN */ + AUX_STACK_CHECK_EXPAND( + stack_terms, stack_terms_limit + 1); /* + 1 because initially we stiil + haven't done any STACK_POP_DOWN */ STACK_PUSH_UP(NULL, stack_terms); #if defined(MODE_GLOBAL_TRIE_LOOP) - /* for the global trie, it is safe to skip the IsVarTerm() and IsAtomOrIntTerm() tests in the first iteration */ + /* for the global trie, it is safe to skip the IsVarTerm() and + * IsAtomOrIntTerm() tests in the first iteration */ goto answer_search_loop_non_atomic; #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_RATIONAL_TERMS term_array Ts; - void* CyclicTerm; + void *CyclicTerm; term_array_init(&Ts, 10); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ @@ -1349,21 +1436,25 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr t = Deref(t); if (IsTableVarTerm(t)) { t = MakeTableVarTerm(VarIndexOfTerm(t)); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_val + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, + _trie_retry_val + in_pair); } else { if (vars_arity == MAX_TABLE_VARS) - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "answer_search_loop: MAX_TABLE_VARS exceeded"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "answer_search_loop: MAX_TABLE_VARS exceeded"); stack_vars_base[vars_arity] = t; *((CELL *)t) = GLOBAL_table_var_enumerator(vars_arity); t = MakeTableVarTerm(vars_arity); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, + _trie_retry_var + in_pair); vars_arity = vars_arity + 1; } #ifdef TRIE_COMPACT_PAIRS in_pair = 0; #endif /* TRIE_COMPACT_PAIRS */ } else if (IsAtomOrIntTerm(t)) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_atom + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, + _trie_retry_atom + in_pair); #ifdef TRIE_COMPACT_PAIRS in_pair = 0; #endif /* TRIE_COMPACT_PAIRS */ @@ -1371,34 +1462,39 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr } else { gt_node_ptr entry_node; #ifdef GLOBAL_TRIE_FOR_SUBTERMS - entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); + entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, + stack_terms PASS_REGS); #else entry_node = answer_search_global_trie_loop(t, &vars_arity PASS_REGS); #endif /* GLOBAL_TRIE_FOR_SUBTERMS */ - current_node = answer_trie_check_insert_gt_entry(sg_fr, current_node, (Term) entry_node, _trie_retry_gterm + in_pair PASS_REGS); + current_node = answer_trie_check_insert_gt_entry( + sg_fr, current_node, (Term)entry_node, + _trie_retry_gterm + in_pair PASS_REGS); #else /* ! MODE_TERMS_LOOP */ } else #ifdef TRIE_RATIONAL_TERMS - if (IsRationalTerm(t)) { + if (IsRationalTerm(t)) { t = STACK_POP_DOWN(stack_terms); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, _trie_retry_var + in_pair); //TODO create _trie_.._rational + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, t, + _trie_retry_var + + in_pair); // TODO create _trie_.._rational } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ #if defined(MODE_GLOBAL_TRIE_LOOP) - /* for the global trie, it is safe to start here in the first iteration */ - answer_search_loop_non_atomic: + /* for the global trie, it is safe to start here in the first iteration */ + answer_search_loop_non_atomic: #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_COMPACT_PAIRS if (IsPairTerm(t)) { #ifdef TRIE_RATIONAL_TERMS - CyclicTerm = NULL; + CyclicTerm = NULL; #endif /* RATIONAL TERM SUPPORT FOR TRIES */ CELL *aux_pair = RepPair(t); if (aux_pair == PairTermMark) { t = STACK_POP_DOWN(stack_terms); #ifdef TRIE_RATIONAL_TERMS - if (IsPairTerm(t) && ! IsRationalTerm(t)) { - term_array_push(&Ts, (void *) t, (void *) current_node); + if (IsPairTerm(t) && !IsRationalTerm(t)) { + term_array_push(&Ts, (void *)t, (void *)current_node); #else if (IsPairTerm(t)) { #endif /* RATIONAL TERM SUPPORT FOR TRIES */ @@ -1406,21 +1502,24 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr t = Deref(aux_pair[1]); #ifdef TRIE_RATIONAL_TERMS if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); + CyclicTerm = term_array_member(Ts, (void *)t); } if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); // CyclicTerm - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); // CyclicTerm + STACK_PUSH_UP((Term)RationalMark, stack_terms); STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); in_pair = 4; } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); + if (t == TermNil) { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, + _trie_retry_pair); } else { - /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ - /* AUX_STACK_CHECK_EXPAND is not necessary here because the situation of pushing ** - ** up 3 terms has already initially checked for the CompactPairInit term */ + /* AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); */ + /* AUX_STACK_CHECK_EXPAND is not necessary here because the + *situation of pushing ** + ** up 3 terms has already initially checked for the CompactPairInit + *term */ STACK_PUSH_UP(t, stack_terms); STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); in_pair = 4; @@ -1428,42 +1527,47 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr #ifdef TRIE_RATIONAL_TERMS CyclicTerm = NULL; if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + CyclicTerm = term_array_member(Ts, (void *)Deref(aux_pair[0])); if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } else { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndTerm, _trie_retry_null); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndTerm, + _trie_retry_null); STACK_PUSH_UP(t, stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = answer_search_global_trie_terms_loop( + t, &vars_arity, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry( + current_node, (Term)entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ } else { #ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); + term_array_push(&Ts, (void *)t, (void *)current_node); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairInit, _trie_retry_null + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairInit, + _trie_retry_null + in_pair); t = Deref(aux_pair[1]); #ifdef TRIE_RATIONAL_TERMS if (IsVarTerm(aux_pair[1]) || IsPairTerm(aux_pair[1])) { - CyclicTerm = term_array_member(Ts, (void *) t); + CyclicTerm = term_array_member(Ts, (void *)t); } if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); STACK_PUSH_UP(AbsPair(PairTermMark), stack_terms); in_pair = 4; } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - if (t == TermNil) { - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, _trie_retry_pair); - in_pair = 0; + if (t == TermNil) { + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, CompactPairEndList, + _trie_retry_pair); + in_pair = 0; } else { AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 2); STACK_PUSH_UP(t, stack_terms); @@ -1473,29 +1577,34 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr #ifdef TRIE_RATIONAL_TERMS CyclicTerm = NULL; if (IsVarTerm(aux_pair[0]) || IsPairTerm(aux_pair[0])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_pair[0])); + CyclicTerm = term_array_member(Ts, (void *)Deref(aux_pair[0])); if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); + STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); } #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) } else if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); + gt_node_ptr entry_node = answer_search_global_trie_terms_loop( + t, &vars_arity, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry( + current_node, (Term)entry_node PASS_REGS); #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ -#else /* ! TRIE_COMPACT_PAIRS */ +#else /* ! TRIE_COMPACT_PAIRS */ #if defined(MODE_GLOBAL_TRIE_LOOP) && defined(GLOBAL_TRIE_FOR_SUBTERMS) - if (current_node != GLOBAL_root_gt) { - gt_node_ptr entry_node = answer_search_global_trie_terms_loop(t, &vars_arity, stack_terms PASS_REGS); - current_node = global_trie_check_insert_gt_entry(current_node, (Term) entry_node PASS_REGS); - } else + if (current_node != GLOBAL_root_gt) { + gt_node_ptr entry_node = answer_search_global_trie_terms_loop( + t, &vars_arity, stack_terms PASS_REGS); + current_node = global_trie_check_insert_gt_entry( + current_node, (Term)entry_node PASS_REGS); + } else #endif /* MODE_GLOBAL_TRIE_LOOP && GLOBAL_TRIE_FOR_SUBTERMS */ - if (IsPairTerm(t)) { + if (IsPairTerm(t)) { CELL *aux_pair = RepPair(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsPair(NULL), _trie_retry_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsPair(NULL), + _trie_retry_pair); AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); STACK_PUSH_UP(Deref(aux_pair[1]), stack_terms); STACK_PUSH_UP(Deref(aux_pair[0]), stack_terms); @@ -1504,54 +1613,68 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; + Term t_dbl[sizeof(Float) / sizeof(Term)]; Float dbl; } u; u.dbl = FloatOfTerm(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_null + in_pair); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[1], _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[1], + _trie_retry_extension); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[0], _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_double); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, u.t_dbl[0], + _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_double); } else if (f == FunctorLongInt) { - Int li = LongIntOfTerm (t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, li, _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_longint); + Int li = LongIntOfTerm(t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_null + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, li, + _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_longint); } else if (f == FunctorBigInt || f == FunctorString) { - CELL *opq = Yap_HeapStoreOpaqueTerm(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_null + in_pair); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, (CELL)opq, _trie_retry_extension); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_bigint); + CELL *opq = Yap_HeapStoreOpaqueTerm(t); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_null + in_pair); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, (CELL)opq, + _trie_retry_extension); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_bigint); } else if (f == FunctorDBRef) { - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "answer_search_loop: unsupported type tag FunctorDBRef"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "answer_search_loop: unsupported type tag FunctorDBRef"); } else { #ifdef TRIE_RATIONAL_TERMS - term_array_push(&Ts, (void *) t, (void *) current_node); + term_array_push(&Ts, (void *)t, (void *)current_node); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ int i; CELL *aux_appl = RepAppl(t); - ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), _trie_retry_appl + in_pair); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + ArityOfFunctor(f) - 1); + ANSWER_CHECK_INSERT_ENTRY(sg_fr, current_node, AbsAppl((Term *)f), + _trie_retry_appl + in_pair); + AUX_STACK_CHECK_EXPAND(stack_terms, + stack_terms_limit + ArityOfFunctor(f) - 1); for (i = ArityOfFunctor(f); i >= 1; i--) { #ifdef TRIE_RATIONAL_TERMS CyclicTerm = NULL; if (IsVarTerm(aux_appl[i]) || IsApplTerm(aux_appl[i])) - CyclicTerm = term_array_member(Ts, (void *) Deref(aux_appl[i])); + CyclicTerm = term_array_member(Ts, (void *)Deref(aux_appl[i])); if (CyclicTerm != NULL) { - STACK_PUSH_UP((Term) CyclicTerm, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)CyclicTerm, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); + STACK_PUSH_UP(Deref(aux_appl[i]), stack_terms); } } #ifdef TRIE_COMPACT_PAIRS in_pair = 0; #endif /* TRIE_COMPACT_PAIRS */ } else { - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "answer_search_loop: unknown type tag"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "answer_search_loop: unknown type tag"); #endif /* MODE_TERMS_LOOP */ } t = STACK_POP_DOWN(stack_terms); @@ -1569,14 +1692,14 @@ static inline ans_node_ptr answer_search_loop(sg_fr_ptr sg_fr, ans_node_ptr curr } #endif /* INCLUDE_ANSWER_SEARCH_LOOP */ - - /************************************************************** ** answer_search_min_max ** **************************************************************/ #ifdef INCLUDE_ANSWER_SEARCH_MODE_DIRECTED -static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr current_node, Term t, int mode USES_REGS) { +static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, + ans_node_ptr current_node, + Term t, int mode USES_REGS) { ans_node_ptr child_node; Term child_term; Term trie_value = 0, term_value = t; @@ -1588,13 +1711,13 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c if (IsIntTerm(child_term)) { trie_value = child_term; } else if (IsApplTerm(child_term)) { - Functor f = (Functor) RepAppl(child_term); + Functor f = (Functor)RepAppl(child_term); child_node = TrNode_child(child_node); if (f == FunctorLongInt) { - trie_value = MkLongIntTerm( (Int) TrNode_entry(child_node) ); + trie_value = MkLongIntTerm((Int)TrNode_entry(child_node)); } else if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; + Term t_dbl[sizeof(Float) / sizeof(Term)]; Float dbl; } u; u.t_dbl[0] = TrNode_entry(child_node); @@ -1604,15 +1727,17 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ trie_value = MkFloatTerm(u.dbl); } else if (f == FunctorBigInt) { - trie_value = AbsAppl( (CELL *) TrNode_entry(child_node) ); + trie_value = AbsAppl((CELL *)TrNode_entry(child_node)); } else - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "answer_search_min_max: invalid arithmetic value"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "answer_search_min_max: invalid arithmetic value"); child_node = TrNode_child(child_node); } - cmp = Yap_acmp( term_value, trie_value PASS_REGS); + cmp = Yap_acmp(term_value, trie_value PASS_REGS); /* worse answer */ - if ((mode == MODE_DIRECTED_MIN && cmp > 0) || (mode == MODE_DIRECTED_MAX && cmp < 0)) + if ((mode == MODE_DIRECTED_MIN && cmp > 0) || + (mode == MODE_DIRECTED_MAX && cmp < 0)) return NULL; /* equal answer */ if (cmp == 0) @@ -1624,40 +1749,46 @@ static inline ans_node_ptr answer_search_min_max(sg_fr_ptr sg_fr, ans_node_ptr c Functor f = FunctorOfTerm(t); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; + Term t_dbl[sizeof(Float) / sizeof(Term)]; Float dbl; } u; u.dbl = FloatOfTerm(t); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_null); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P ANSWER_SAFE_INSERT_ENTRY(current_node, u.t_dbl[1], _trie_retry_extension); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ ANSWER_SAFE_INSERT_ENTRY(current_node, u.t_dbl[0], _trie_retry_extension); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_double); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_double); } else if (f == FunctorLongInt) { Int li = LongIntOfTerm(t); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_null); ANSWER_SAFE_INSERT_ENTRY(current_node, li, _trie_retry_extension); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_longint); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_longint); } else if (f == FunctorBigInt) { CELL *li = Yap_HeapStoreOpaqueTerm(t); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_null); ANSWER_SAFE_INSERT_ENTRY(current_node, (CELL)li, _trie_retry_extension); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_bigint); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_bigint); } } return current_node; } #endif /* INCLUDE_ANSWER_SEARCH_MODE_DIRECTED */ - - /********************************************************** ** answer_search_sum ** **********************************************************/ #ifdef INCLUDE_ANSWER_SEARCH_MODE_DIRECTED -static inline ans_node_ptr answer_search_sum(sg_fr_ptr sg_fr, ans_node_ptr current_node, Term t USES_REGS) { +static inline ans_node_ptr answer_search_sum(sg_fr_ptr sg_fr, + ans_node_ptr current_node, + Term t USES_REGS) { ans_node_ptr child_node; Term child_term; Term trie_value = 0, term_value = t, sum_value = 0; @@ -1668,13 +1799,13 @@ static inline ans_node_ptr answer_search_sum(sg_fr_ptr sg_fr, ans_node_ptr curre if (IsIntTerm(child_term)) { trie_value = child_term; } else if (IsApplTerm(child_term)) { - Functor f = (Functor) RepAppl(child_term); + Functor f = (Functor)RepAppl(child_term); child_node = TrNode_child(child_node); if (f == FunctorLongInt) { - trie_value = MkLongIntTerm( (Int) TrNode_entry(child_node) ); + trie_value = MkLongIntTerm((Int)TrNode_entry(child_node)); } else if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; + Term t_dbl[sizeof(Float) / sizeof(Term)]; Float dbl; } u; u.t_dbl[0] = TrNode_entry(child_node); @@ -1684,57 +1815,63 @@ static inline ans_node_ptr answer_search_sum(sg_fr_ptr sg_fr, ans_node_ptr curre #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ trie_value = MkFloatTerm(u.dbl); } else if (f == FunctorBigInt) { - trie_value = AbsAppl( (CELL *) TrNode_entry(child_node) ); + trie_value = AbsAppl((CELL *)TrNode_entry(child_node)); } else - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "answer_search_min_max: invalid arithmetic value"); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, + "answer_search_min_max: invalid arithmetic value"); child_node = TrNode_child(child_node); } - sum_value = p_plus(trie_value, term_value PASS_REGS); + sum_value = p_plus(trie_value, term_value PASS_REGS); if (IsAtomOrIntTerm(sum_value)) { ANSWER_SAFE_INSERT_ENTRY(current_node, sum_value, _trie_retry_atom); } else if (IsApplTerm(sum_value)) { Functor f = FunctorOfTerm(sum_value); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; - Float dbl; + Term t_dbl[sizeof(Float) / sizeof(Term)]; + Float dbl; } u; u.dbl = FloatOfTerm(sum_value); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_null); #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P ANSWER_SAFE_INSERT_ENTRY(current_node, u.t_dbl[1], _trie_retry_extension); #endif /* SIZEOF_DOUBLE x SIZEOF_INT_P */ ANSWER_SAFE_INSERT_ENTRY(current_node, u.t_dbl[0], _trie_retry_extension); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_double); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_double); } else if (f == FunctorLongInt) { Int li = LongIntOfTerm(sum_value); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_null); ANSWER_SAFE_INSERT_ENTRY(current_node, li, _trie_retry_extension); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_longint); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_longint); } else if (f == FunctorBigInt) { CELL *li = Yap_HeapStoreOpaqueTerm(sum_value); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_null); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_null); ANSWER_SAFE_INSERT_ENTRY(current_node, (CELL)li, _trie_retry_extension); - ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), _trie_retry_bigint); + ANSWER_SAFE_INSERT_ENTRY(current_node, AbsAppl((Term *)f), + _trie_retry_bigint); } } return current_node; } #endif /* INCLUDE_ANSWER_SEARCH_MODE_DIRECTED */ - - /*************************************************************** ** invalidate_answer_trie ** ***************************************************************/ #ifdef INCLUDE_ANSWER_SEARCH_MODE_DIRECTED -static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, int position USES_REGS) { +static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, + int position USES_REGS) { if (IS_ANSWER_TRIE_HASH(current_node)) { ans_hash_ptr hash; ans_node_ptr *bucket, *last_bucket; - hash = (ans_hash_ptr) current_node; + hash = (ans_hash_ptr)current_node; bucket = Hash_buckets(hash); last_bucket = bucket + Hash_num_buckets(hash); do { @@ -1744,16 +1881,18 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i if (IS_ANSWER_LEAF_NODE(current_node)) { INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, + TRAVERSE_POSITION_FIRST PASS_REGS); INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } while (next_node) { current_node = next_node; next_node = TrNode_next(current_node); - invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); + invalidate_answer_trie(current_node, sg_fr, + TRAVERSE_POSITION_NEXT PASS_REGS); } } - } while (++bucket != last_bucket); + } while (++bucket != last_bucket); if (Hash_next(hash)) Hash_previous(Hash_next(hash)) = Hash_previous(hash); if (Hash_previous(hash)) @@ -1768,19 +1907,22 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i if (IS_ANSWER_LEAF_NODE(current_node)) { INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, + TRAVERSE_POSITION_FIRST PASS_REGS); INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } while (next_node) { current_node = next_node; next_node = TrNode_next(current_node); - invalidate_answer_trie(current_node, sg_fr, TRAVERSE_POSITION_NEXT PASS_REGS); + invalidate_answer_trie(current_node, sg_fr, + TRAVERSE_POSITION_NEXT PASS_REGS); } } else { if (IS_ANSWER_LEAF_NODE(current_node)) { INVALIDATE_ANSWER_TRIE_LEAF_NODE(current_node, sg_fr); } else { - invalidate_answer_trie(TrNode_child(current_node), sg_fr, TRAVERSE_POSITION_FIRST PASS_REGS); + invalidate_answer_trie(TrNode_child(current_node), sg_fr, + TRAVERSE_POSITION_FIRST PASS_REGS); INVALIDATE_ANSWER_TRIE_NODE(current_node, sg_fr); } } @@ -1789,15 +1931,15 @@ static void invalidate_answer_trie(ans_node_ptr current_node, sg_fr_ptr sg_fr, i } #endif /* INCLUDE_ANSWER_SEARCH_MODE_DIRECTED */ - - /************************************************************************ ** load_(answer|substitution)_loop ** ************************************************************************/ #ifdef INCLUDE_LOAD_ANSWER_LOOP #ifdef MODE_GLOBAL_TRIE_LOOP -static inline CELL *load_substitution_loop(gt_node_ptr current_node, int *vars_arity_ptr, CELL *stack_terms USES_REGS) { +static inline CELL *load_substitution_loop(gt_node_ptr current_node, + int *vars_arity_ptr, + CELL *stack_terms USES_REGS) { #else static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { #endif /* MODE_GLOBAL_TRIE_LOOP */ @@ -1822,7 +1964,8 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { | TERM_N | <-- stack_terms ----------| * | ... | /|\ - ----------| | stack_terms_pair_offset (TRIE_COMPACT_PAIRS) + ----------| | stack_terms_pair_offset +(TRIE_COMPACT_PAIRS) | TERM_1 | \|/ =========== * LOCAL_TrailTop --> | | <-- stack_terms_base (TRIE_COMPACT_PAIRS) @@ -1832,37 +1975,40 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { int vars_arity = *vars_arity_ptr; #else int vars_arity = 0; - CELL *stack_terms = (CELL *) LOCAL_TrailTop; + CELL *stack_terms = (CELL *)LOCAL_TrailTop; #endif /* MODE_GLOBAL_TRIE_LOOP */ - CELL *stack_vars_base = (CELL *) TR; + CELL *stack_vars_base = (CELL *)TR; #define stack_terms_limit (stack_vars_base + vars_arity) #ifdef TRIE_COMPACT_PAIRS -#define stack_terms_base ((CELL *) LOCAL_TrailTop) +#define stack_terms_base ((CELL *)LOCAL_TrailTop) int stack_terms_pair_offset = 0; #endif /* TRIE_COMPACT_PAIRS */ Term t = TrNode_entry(current_node); #ifdef MODE_GLOBAL_TRIE_LOOP current_node = TrNode_parent(current_node); #else - current_node = (ans_node_ptr) UNTAG_ANSWER_NODE(TrNode_parent(current_node)); + current_node = (ans_node_ptr)UNTAG_ANSWER_NODE(TrNode_parent(current_node)); #endif /* MODE_GLOBAL_TRIE_LOOP */ #ifdef TRIE_RATIONAL_TERMS term_array Ts; - void* CyclicTerm; + void *CyclicTerm; term_array_init(&Ts, 10); Term RationalTermTMP; // a temporary temp to be used from the rational code -#endif /* RATIONAL TERM SUPPORT FOR TRIES */ +#endif /* RATIONAL TERM SUPPORT FOR TRIES */ do { #ifdef TRIE_RATIONAL_TERMS - CyclicTerm = term_array_member(Ts, (void *) current_node); + CyclicTerm = term_array_member(Ts, (void *)current_node); #endif /* RATIONAL TERM SUPPORT FOR TRIES */ if (IsVarTerm(t)) { #ifdef TRIE_RATIONAL_TERMS - if (t > VarIndexOfTableTerm(MAX_TABLE_VARS) && TrNode_child((gt_node_ptr) t) != (gt_node_ptr)(1)) { //TODO: substitute the != 1 test to something more appropriate + if (t > VarIndexOfTableTerm(MAX_TABLE_VARS) && + TrNode_child((gt_node_ptr)t) != + (gt_node_ptr)(1)) { // TODO: substitute the != 1 test to something + // more appropriate /* Rational term */ - RationalTermTMP = (Term) term_array_member(Ts, (void *) t); + RationalTermTMP = (Term)term_array_member(Ts, (void *)t); if (RationalTermTMP) { /* rational term is assigned a variable already */ AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit); @@ -1871,24 +2017,27 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { RationalTermTMP = MkVarTerm(); STACK_PUSH_UP(RationalTermTMP, stack_terms); /* memorize the rational term and assign it a variable */ - term_array_push(&Ts, (void *) t, (void *) RationalTermTMP); + term_array_push(&Ts, (void *)t, (void *)RationalTermTMP); } } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ { -#if ! defined(MODE_GLOBAL_TRIE_LOOP) || defined(GLOBAL_TRIE_FOR_SUBTERMS) +#if !defined(MODE_GLOBAL_TRIE_LOOP) || defined(GLOBAL_TRIE_FOR_SUBTERMS) if (t > VarIndexOfTableTerm(MAX_TABLE_VARS)) { - stack_terms = load_substitution_loop((gt_node_ptr) t, &vars_arity, stack_terms PASS_REGS); - } else + stack_terms = load_substitution_loop((gt_node_ptr)t, &vars_arity, + stack_terms PASS_REGS); + } else #endif /* ! MODE_GLOBAL_TRIE_LOOP || GLOBAL_TRIE_FOR_SUBTERMS */ - { int var_index = VarIndexOfTableTerm(t); - AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit - vars_arity + var_index + 1); + { + int var_index = VarIndexOfTableTerm(t); + AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit - vars_arity + + var_index + 1); if (var_index >= vars_arity) { while (vars_arity < var_index) - stack_vars_base[vars_arity++] = 0; - stack_vars_base[vars_arity++] = MkVarTerm(); + stack_vars_base[vars_arity++] = 0; + stack_vars_base[vars_arity++] = MkVarTerm(); } else if (stack_vars_base[var_index] == 0) - stack_vars_base[var_index] = MkVarTerm(); + stack_vars_base[var_index] = MkVarTerm(); STACK_PUSH_UP(stack_vars_base[var_index], stack_terms); } } @@ -1897,77 +2046,91 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { #ifdef TRIE_RATIONAL_TERMS if (CyclicTerm) { AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 4); - STACK_PUSH_UP((Term) RationalMark, stack_terms); // Add a rational term marker necessary as we read both ways the stack // - STACK_PUSH_UP(t, stack_terms); // Add the term // - STACK_PUSH_UP(CyclicTerm, stack_terms); // Add the variable that the term will unify with // - STACK_PUSH_UP((Term) RationalMark, stack_terms); // Add a rational term marker necessary as we read both ways the stack // + STACK_PUSH_UP((Term)RationalMark, stack_terms); // Add a rational term + // marker necessary as + // we read both ways the + // stack // + STACK_PUSH_UP(t, stack_terms); // Add the term // + STACK_PUSH_UP( + CyclicTerm, + stack_terms); // Add the variable that the term will unify with // + STACK_PUSH_UP((Term)RationalMark, stack_terms); // Add a rational term + // marker necessary as + // we read both ways the + // stack // } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(t, stack_terms); + 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); #ifdef TRIE_RATIONAL_TERMS if (IsRationalTerm(tail)) { - Yap_Error(SYSTEM_ERROR_INTERNAL, tail, "Rational element of a Rational Term appears as the first Tail of a list"); + Yap_Error(SYSTEM_ERROR_INTERNAL, tail, "Rational element of a " + "Rational Term appears as the " + "first Tail of a list"); } #endif /* RATIONAL TERM SUPPORT FOR TRIES */ while (STACK_NOT_EMPTY(stack_aux, stack_terms)) { head = STACK_POP_UP(stack_aux); #ifdef TRIE_RATIONAL_TERMS if (IsRationalTerm(head)) { - head = STACK_POP_UP(stack_aux); // thats the rational term - RationalTermTMP = STACK_POP_UP(stack_aux); // that is the variable to unify with - (void) STACK_POP_UP(stack_aux); // eat the second rational mark + head = STACK_POP_UP(stack_aux); // thats the rational term + RationalTermTMP = + STACK_POP_UP(stack_aux); // that is the variable to unify with + (void)STACK_POP_UP(stack_aux); // eat the second rational mark tail = MkPairTerm(head, tail); Yap_unify(RationalTermTMP, tail); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - tail = MkPairTerm(head, tail); + tail = MkPairTerm(head, tail); } stack_terms = stack_terms_base - stack_terms_pair_offset; - stack_terms_pair_offset = (int) STACK_POP_DOWN(stack_terms); + stack_terms_pair_offset = (int)STACK_POP_DOWN(stack_terms); STACK_PUSH_UP(tail, stack_terms); - } else { /* CompactPairEndList / CompactPairEndTerm */ + } else { /* CompactPairEndList / CompactPairEndTerm */ Term last; AUX_STACK_CHECK_EXPAND(stack_terms, stack_terms_limit + 1); last = STACK_POP_DOWN(stack_terms); #ifdef TRIE_RATIONAL_TERMS RationalTermTMP = TermNil; - if (IsRationalTerm(last)) { // rather unlikely case the rational term is the last of a list - RationalTermTMP = STACK_POP_DOWN(stack_terms); // in this case we need to invert the term with the end of list - last = STACK_POP_DOWN(stack_terms); // variable to unify with - (void) STACK_POP_DOWN(stack_terms); // eat the second rational mark + if (IsRationalTerm(last)) { // rather unlikely case the rational term is + // the last of a list + RationalTermTMP = STACK_POP_DOWN(stack_terms); // in this case we need + // to invert the term + // with the end of list + last = STACK_POP_DOWN(stack_terms); // variable to unify with + (void)STACK_POP_DOWN(stack_terms); // eat the second rational mark } #endif /* RATIONAL TERM SUPPORT FOR TRIES */ STACK_PUSH_UP(stack_terms_pair_offset, stack_terms); - stack_terms_pair_offset = (int) (stack_terms_base - stack_terms); + stack_terms_pair_offset = (int)(stack_terms_base - stack_terms); if (t == CompactPairEndList) STACK_PUSH_UP(TermNil, stack_terms); #ifdef TRIE_RATIONAL_TERMS if (RationalTermTMP && RationalTermTMP != TermNil) { /* most probably this never occurs */ - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); STACK_PUSH_UP(last, stack_terms); STACK_PUSH_UP(RationalTermTMP, stack_terms); - STACK_PUSH_UP((Term) RationalMark, stack_terms); + STACK_PUSH_UP((Term)RationalMark, stack_terms); } else #endif /* RATIONAL TERM SUPPORT FOR TRIES */ - STACK_PUSH_UP(last, stack_terms); + STACK_PUSH_UP(last, stack_terms); } -#else /* ! TRIE_COMPACT_PAIRS */ +#else /* ! TRIE_COMPACT_PAIRS */ 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); + Functor f = (Functor)RepAppl(t); if (f == FunctorDouble) { union { - Term t_dbl[sizeof(Float)/sizeof(Term)]; + Term t_dbl[sizeof(Float) / sizeof(Term)]; Float dbl; } u; t = TrNode_entry(current_node); @@ -1981,15 +2144,15 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { current_node = TrNode_parent(current_node); t = MkFloatTerm(u.dbl); } else if (f == FunctorLongInt) { - Int li = TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - current_node = TrNode_parent(current_node); - t = MkLongIntTerm(li); + Int li = TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + current_node = TrNode_parent(current_node); + t = MkLongIntTerm(li); } else if (f == FunctorBigInt || f == FunctorString) { - CELL *ptr = (CELL *)TrNode_entry(current_node); - current_node = TrNode_parent(current_node); - current_node = TrNode_parent(current_node); - t = AbsAppl( ptr ); + CELL *ptr = (CELL *)TrNode_entry(current_node); + current_node = TrNode_parent(current_node); + current_node = TrNode_parent(current_node); + t = AbsAppl(ptr); } else { int f_arity = ArityOfFunctor(f); t = Yap_MkApplTerm(f, f_arity, stack_terms); @@ -2001,12 +2164,14 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { #ifdef TRIE_RATIONAL_TERMS if (CyclicTerm) { RationalTermTMP = STACK_POP_DOWN(stack_terms); - if IsRationalTerm(RationalTermTMP) { - //printf("Special Case\n"); - } else if (IsPairTerm(RationalTermTMP)) { - Yap_unify((Term) CyclicTerm, RationalTermTMP); + if + IsRationalTerm(RationalTermTMP) { + // printf("Special Case\n"); + } + else if (IsPairTerm(RationalTermTMP)) { + Yap_unify((Term)CyclicTerm, RationalTermTMP); } else if (IsApplTerm(RationalTermTMP)) { - Yap_unify((Term) CyclicTerm, RationalTermTMP); + Yap_unify((Term)CyclicTerm, RationalTermTMP); } STACK_PUSH_UP(RationalTermTMP, stack_terms); } @@ -2031,8 +2196,6 @@ static inline CELL *load_answer_loop(ans_node_ptr current_node USES_REGS) { } #endif /* INCLUDE_LOAD_ANSWER_LOOP */ - - /*************************** ** Undef Macros ** ***************************/ diff --git a/packages/myddas/CMakeLists.txt b/packages/myddas/CMakeLists.txt index cf8e71c81..46c7fd890 100644 --- a/packages/myddas/CMakeLists.txt +++ b/packages/myddas/CMakeLists.txt @@ -1,13 +1,11 @@ SET( CMAKE_FIND_FRAMEWORK LAST) SET( CMAKE_FIND_APPBUNDLE LAST) - # Be sure to also update these in Makefile! set(SO_MAJOR 1) set(SO_MINOR 0) set(SO_PATCH 0) - set( MYDDAS_SOURCES myddas_initialization.c myddas_shared.c @@ -15,6 +13,9 @@ set( MYDDAS_SOURCES myddas_top_level.c ) + include_directories (. sqlite3) + + set( MYDDAS_HEADERS myddas.h myddas_statistics.h @@ -27,66 +28,34 @@ set( MYDDAS_UTIL_SOURCES myddas_util.c myddas_initialization.c ) + set_property( GLOBAL + APPEND PROPERTY COMPILE_DEFINITIONS USE_MYDDAS=1) + + # internal MYDDAS flag, used to preprocess ypp files. + set (MYDDAS_FLAGS -DUSE_MYDDAS=1) + add_subdirectory (mysql) add_subdirectory (odbc) add_subdirectory (postgres) add_subdirectory (sqlite3) -set( MYDDAS_YPP - pl/myddas.ypp - pl/myddas_assert_predicates.ypp - pl/myddas_top_level.ypp - pl/myddas_errors.ypp - pl/myddas_prolog2sql.ypp - pl/myddas_util_predicates.ypp - pl/myddas_prolog2sql_optimizer.ypp ) -set( MYDDAS_PROLOG - myddas.yap - myddas_assert_predicates.yap - myddas_top_level.yap - myddas_errors.yap - myddas_prolog2sql.yap - myddas_util_predicates.yap - myddas_prolog2sql_optimizer.yap ) - -function(cpp_compile filename) - get_filename_component(base ${filename} NAME_WE) - set(base_abs ${CMAKE_CURRENT_BINARY_DIR}/${base}) - set(output ${base_abs}.yap) - add_custom_command( - OUTPUT ${output} - COMMAND ${CMAKE_C_COMPILER} -E ${CMAKE_C_FLAGS} -P -E -w -o ${CMAKE_CURRENT_BINARY_DIR} ${filename}) - set_source_files_properties(${output} PROPERTIES GENERATED TRUE) -endfunction() - -foreach(file ${MYDDAS_YPP}) - cpp_compile(GENERATED_SOURCES ${file}) -endforeach() - -source_group("" FILES ${MYDDAS_YPP}) -source_group(generated FILES ${MYDDAS_YAP}) - - set_property( DIRECTORY ../.. - APPEND PROPERTY COMPILE_DEFINITIONS USE_MYDDAS=1) - - Add_custom_target (plmyddas ALL DEPENDS ${MYDDAS_YAP} ) # WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) + add_subdirectory (pl) - add_library (myddas OBJECT +add_library (myddas OBJECT ${MYDDAS_SOURCES} ${MYDDAS_UTIL_SOURCES} ${MYDDAS_HEADERS} ) + +add_dependencies(myddas plmyddas) + set_target_properties (myddas PROPERTIES POSITION_INDEPENDENT_CODE ON ) - - cmake_dependent_option (USE_MYDDAS_Stats "enable the MYDDAS library statistics support" OFF USE_MYDDAS OFF) -#TODO: - diff --git a/packages/myddas/myddas_util.c b/packages/myddas/myddas_util.c index c643171e2..ccd77dd61 100644 --- a/packages/myddas/myddas_util.c +++ b/packages/myddas/myddas_util.c @@ -1,13 +1,13 @@ -#include -#include #include "Yap.h" #include "cut_c.h" +#include +#include #include "myddas.h" #include "myddas_util.h" /* Type: MYSQL->1 ODBC->2*/ -Short myddas_util_connection_type(void *con) { +int myddas_util_connection_type(void *con) { MYDDAS_UTIL_CONNECTION con_node = myddas_util_search_connection(con); @@ -157,7 +157,7 @@ void myddas_util_set_total_multi_queries_number(MYDDAS_UTIL_CONNECTION con, con->total_number_queries = number; } -#ifdef MYDDAS_MYSQL +#if defined(MYDDAS_MYSQL) && 0 /* Auxilary function to table_write*/ static void n_print(Int n, char c) { for (; n > 0; n--) diff --git a/packages/myddas/mysql/CMakeLists.txt b/packages/myddas/mysql/CMakeLists.txt index c8318b8f9..9c942a568 100644 --- a/packages/myddas/mysql/CMakeLists.txt +++ b/packages/myddas/mysql/CMakeLists.txt @@ -20,7 +20,8 @@ macro_log_feature (MYSQL_FOUND "MySQL" # MYSQL_INCLUDE_DIR - where to find mysql.h, etc. # MYSQL_LIBRARIES - List of libraries when using MySQL. # MYSQL_FOUND - True if MySQL found. -add_definitions (-DMYDDAS_MYSQL=1) + set (MYDDAS_FLAGS ${MYDDAS_FLAGS} -DMYDDAS_MYSQL=1 PARENT_SCOPE) + set_property( DIRECTORY .. APPEND PROPERTY COMPILE_DEFINITIONS MYDDAS_MYSQL=1 ) add_library (Yapmysql SHARED ${YAPMYSQL_SOURCES}) target_link_libraries(Yapmysql ${MYSQL_LIBRARIES} libYap) include_directories (${MYSQL_INCLUDE_DIR} ..) diff --git a/packages/myddas/odbc/CMakeLists.txt b/packages/myddas/odbc/CMakeLists.txt index a16b26ac2..e1e69ddec 100644 --- a/packages/myddas/odbc/CMakeLists.txt +++ b/packages/myddas/odbc/CMakeLists.txt @@ -17,7 +17,8 @@ if (ODBC_FOUND) # ODBC_INCLUDE_DIRECTORIES, where to find sql.h # ODBC_LIBRARIES, the libraries to link against to use ODBC # ODBC_FOUND. If false, you cannot build anything that requires Odbc. -add_definitions (-DMYDDAS_ODBC=1) + set (MYDDAS_FLAGS ${MYDDAS_FLAGS} -DMYDDAS_ODBC=1 PARENT_SCOPE) + set_property( DIRECTORY .. APPEND PROPERTY COMPILE_DEFINITIONS MYDDAS_ODBC=1 ) add_library (Yapodbc SHARED ${YAPODBC_SOURCES}) target_link_libraries(Yapodbc libYap ${ODBC_LIBRARIES}) include_directories (${ODBC_INCLUDE_DIRECTORIES} ..) diff --git a/packages/myddas/pl/myddas.ypp b/packages/myddas/pl/myddas.ypp index 22a2f8ca1..b5cea49bc 100644 --- a/packages/myddas/pl/myddas.ypp +++ b/packages/myddas/pl/myddas.ypp @@ -17,12 +17,6 @@ #if USE_MYDDAS -#ifndef __ANDROID__ -:- load_foreign_files([myddas], [], init_myddas). -/* Initialize MYDDAS GLOBAL STRUCTURES */ -:- . -#endif - #ifdef DEBUG :- yap_flag(single_var_warnings,on). #endif diff --git a/packages/myddas/postgres/CMakeLists.txt b/packages/myddas/postgres/CMakeLists.txt index a47f0e1fb..2e45f1754 100644 --- a/packages/myddas/postgres/CMakeLists.txt +++ b/packages/myddas/postgres/CMakeLists.txt @@ -19,7 +19,9 @@ if (PostgreSQL_FOUND) # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries # PostgreSQL_LIBRARIES - The PostgreSQL libraries. add_library (Yappostgres SHARED ${YAPPOSTGRES_SOURCES}) - add_definitions (-DMYDDAS_POSTGRES=1) + set (MYDDAS_FLAGS ${MYDDAS_FLAGS} -DMYDDAS_POSTGRES=1 PARENT_SCOPE) + set_property( DIRECTORY .. APPEND PROPERTY COMPILE_DEFINITIONS MYDDAS_POSTGRES=1 ) + target_link_libraries(Yappostgres libYap ${PostgreSQL_LIBRARIES}) include_directories (${PostgreSQL_INCLUDE_DIRS} ..) set_target_properties (Yappostgres PROPERTIES @@ -35,4 +37,3 @@ if (PostgreSQL_FOUND) else() add_definitions (-DMYDDAS_PostgreSQL=0) endif (PostgreSQL_FOUND) - diff --git a/packages/myddas/sqlite3/CMakeLists.txt b/packages/myddas/sqlite3/CMakeLists.txt index 578cd6aac..75bd1a290 100644 --- a/packages/myddas/sqlite3/CMakeLists.txt +++ b/packages/myddas/sqlite3/CMakeLists.txt @@ -1,25 +1,44 @@ set( YAPSQLITE3_SOURCES myddas_sqlite3.c + sqlite3.c + sqlite3.h ) -macro_optional_find_package(Sqlite3 ON) +set( YAPSQLITE3_ANDROID_SOURCES +Android/jni/sqlite/ALog-priv.h +Android/jni/sqlite/JNIHelp.cpp +Android/jni/sqlite/JniConstants.cpp +Android/jni/sqlite/android_database_SQLiteCommon.cpp +Android/jni/sqlite/android_database_SQLiteCommon.h +Android/jni/sqlite/android_database_SQLiteConnection.cpp +Android/jni/sqlite/android_database_SQLiteDebug.cpp +Android/jni/sqlite/android_database_SQLiteGlobal.cpp +Android/jni/sqlite/nativehelper/JNIHelp.h +Android/jni/sqlite/nativehelper/JniConstants.h +Android/jni/sqlite/nativehelper/ScopedLocalRef.h +Android/jni/sqlite/nativehelper/jni.h + ) +#sqlite3 is now in the system + +set (SQLITE3_FOUND ON PARENT_SCOPE) macro_log_feature (SQLITE3_FOUND "Sqlite3" "Sqlite3 Data-Base " "http://www.sqlite3ql.org" FALSE) -if (SQLITE3_FOUND) - # SQLITE3_INCLUDE_DIRECTORIES, where to find sql.h - # SQLITE3_LIBRARIES, the libraries to link against to use SQLITE3 - # SQLITE3_FOUND. If false, you cannot build anything that requires Sqlite3. - add_library (Yapsqlite3 SHARED ${YAPSQLITE3_SOURCES}) - add_definitions (-DMYDDAS_SQLITE3=1) - target_link_libraries(Yapsqlite3 ${SQLITE3_LIBRARIES} libYap) - include_directories (${SQLITE3_INCLUDE_DIRECTORIES} .. ) + include_directories (${SQLITE3_INCLUDE_DIRECTORIES} .. . Android/jni/sqlite Android/jni/sqlite/nativehelper) + + set (MYDDAS_FLAGS ${MYDDAS_FLAGS} -DMYDDAS_SQLITE3=1 PARENT_SCOPE) + set_property( DIRECTORY .. APPEND PROPERTY COMPILE_DEFINITIONS MYDDAS_SQLITE3=1 ) + + if (ANDROID) + add_library (Yapsqlite3 OBJECT ${YAPSQLITE3_SOURCES} ${YAPSQLITE3_ANDROID_SOURCES}) + else() + add_library (Yapsqlite3 SHARED ${YAPSQLITE3_SOURCES}) + target_link_libraries(Yapsqlite3 libYap) set_target_properties (Yapsqlite3 PROPERTIES - POSITION_INDEPENDENT_CODE ON VERSION "${SO_MAJOR}.${SO_MINOR}.${SO_PATCH}" SOVERSION ${SO_MAJOR} ) @@ -28,5 +47,4 @@ if (SQLITE3_FOUND) LIBRARY DESTINATION ${libdir} ARCHIVE DESTINATION ${libdir} ) - -endif (SQLITE3_FOUND) +endif()