| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #include "Yap.h"
 | 
					
						
							|  |  |  | #include "clause.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-19 21:53:49 -05:00
										 |  |  | #include "tracer.h"
 | 
					
						
							|  |  |  | #ifdef YAPOR
 | 
					
						
							|  |  |  | #include "or.macros.h"
 | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  | #endif /* YAPOR */
 | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #include "clause_list.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 12:53:29 +00:00
										 |  |  | /* need to fix overflow handling */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  | static void mk_blob(int sz USES_REGS) { | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   MP_INT *dst; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   HR[0] = (CELL)FunctorBigInt; | 
					
						
							|  |  |  |   HR[1] = CLAUSE_LIST; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   dst = (MP_INT *)(HR + 2); | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   dst->_mp_size = 0L; | 
					
						
							|  |  |  |   dst->_mp_alloc = sz; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   HR += (1 + sizeof(MP_INT) / sizeof(CELL)); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   HR[sz] = EndSpecials; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   HR += sz + 1; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  | static CELL *extend_blob(CELL *start, int sz USES_REGS) { | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   UInt osize; | 
					
						
							|  |  |  |   MP_INT *dst; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (HR + sz > ASP) | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   dst = (MP_INT *)(start + 2); | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   osize = dst->_mp_alloc; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   start += (1 + sizeof(MP_INT) / sizeof(CELL)); | 
					
						
							|  |  |  |   start[sz + osize] = EndSpecials; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   dst->_mp_alloc += sz; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   HR += sz; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   return start + osize; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*init of ClasuseList*/ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |  clause_list_t Yap_ClauseListInit(clause_list_t in) { | 
					
						
							|  |  |  |   CACHE_REGS in->n = 0; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   in->start = HR; | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   mk_blob(0 PASS_REGS); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   in->end = HR; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   return in; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*add clause to ClauseList
 | 
					
						
							|  |  |  |  returns FALSE on error*/ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   int Yap_ClauseListExtend(clause_list_t cl, void *clause, void *pred) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   PredEntry *ap = (PredEntry *)pred; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 11:42:48 +00:00
										 |  |  |   /*  fprintf(stderr,"cl=%p\n",clause); */ | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (cl->end != HR) | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     return FALSE; | 
					
						
							|  |  |  |   if (cl->n == 0) { | 
					
						
							|  |  |  |     void **ptr; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |     if (!(ptr = (void **)extend_blob(cl->start, 1 PASS_REGS))) | 
					
						
							|  |  |  |       return FALSE; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     ptr[0] = clause; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   } else if (cl->n == 1) { | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     yamop **ptr; | 
					
						
							|  |  |  |     yamop *code_p, *fclause; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!(ptr = (yamop **)extend_blob( | 
					
						
							|  |  |  |               cl->start, 2 * (CELL)NEXTOP((yamop *)NULL, Otapl) / sizeof(CELL) - | 
					
						
							|  |  |  |                              1 PASS_REGS))) | 
					
						
							|  |  |  |       return FALSE; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     fclause = ptr[-1]; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |     code_p = (yamop *)(ptr - 1); | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     code_p->opc = Yap_opcode(_try_clause); | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     code_p->y_u.Otapl.d = fclause; | 
					
						
							|  |  |  |     code_p->y_u.Otapl.s = ap->ArityOfPE; | 
					
						
							|  |  |  |     code_p->y_u.Otapl.p = ap; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #ifdef TABLING
 | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     code_p->y_u.Otapl.te = ap->TableOfPred; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef YAPOR
 | 
					
						
							|  |  |  |     INIT_YAMOP_LTT(code_p, 0); | 
					
						
							|  |  |  | #endif /* YAPOR */
 | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |     code_p = NEXTOP(code_p, Otapl); | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     code_p->opc = Yap_opcode(_trust); | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     code_p->y_u.Otapl.d = clause; | 
					
						
							|  |  |  |     code_p->y_u.Otapl.s = ap->ArityOfPE; | 
					
						
							|  |  |  |     code_p->y_u.Otapl.p = ap; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #ifdef TABLING
 | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     code_p->y_u.Otapl.te = ap->TableOfPred; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef YAPOR
 | 
					
						
							|  |  |  |     INIT_YAMOP_LTT(code_p, 0); | 
					
						
							|  |  |  | #endif /* YAPOR */
 | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     yamop *code_p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |     if (!(code_p = (yamop *)extend_blob(cl->start, | 
					
						
							|  |  |  |                                         ((CELL)NEXTOP((yamop *)NULL, Otapl)) / | 
					
						
							|  |  |  |                                             sizeof(CELL) PASS_REGS))) | 
					
						
							|  |  |  |       return FALSE; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     code_p->opc = Yap_opcode(_trust); | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     code_p->y_u.Otapl.d = clause; | 
					
						
							|  |  |  |     code_p->y_u.Otapl.s = ap->ArityOfPE; | 
					
						
							|  |  |  |     code_p->y_u.Otapl.p = ap; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #ifdef TABLING
 | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     code_p->y_u.Otapl.te = ap->TableOfPred; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef YAPOR
 | 
					
						
							|  |  |  |     INIT_YAMOP_LTT(code_p, 0); | 
					
						
							|  |  |  | #endif /* YAPOR */
 | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |     code_p = PREVOP(code_p, Otapl); | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     code_p->opc = Yap_opcode(_retry); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   cl->end = HR; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   cl->n++; | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*closes the clause list*/ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |  void Yap_ClauseListClose(clause_list_t cl) { /* no need to do nothing */ | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*destroys the clause list freeing memory*/ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |  int Yap_ClauseListDestroy(clause_list_t cl) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (cl->end != HR) | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   HR = cl->start; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*destroys clause list and returns only first clause*/ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |  void *Yap_ClauseListToClause(clause_list_t cl) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   void **ptr; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (cl->end != HR) | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   if (cl->n != 1) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   if (!(ptr = (void **)extend_blob(cl->start, 0 PASS_REGS))) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   return ptr[-1]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*return pointer to start of try-retry-trust sequence*/ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |  void *Yap_ClauseListCode(clause_list_t cl) { | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   CELL *ptr; | 
					
						
							|  |  |  |   ptr = (CELL *)cl->start; | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |   ptr += (1 + sizeof(MP_INT) / sizeof(CELL)); | 
					
						
							| 
									
										
										
										
											2009-02-11 15:10:57 +00:00
										 |  |  |   return (void *)ptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* where to fail */ | 
					
						
							| 
									
										
										
										
											2016-01-31 10:14:28 +00:00
										 |  |  |  void *Yap_FAILCODE(void) { return (void *)FAILCODE; } |