Exemplo n.º 1
0
void Yap_PrintPredName(PredEntry *ap) {
  CACHE_REGS
  Term tmod = ap->ModuleOfPred;
  if (!tmod)
    tmod = TermProlog;
#if THREADS
  Yap_DebugPlWrite(MkIntegerTerm(worker_id));
  Yap_DebugPutc(stderr, ' ');
#endif
  Yap_DebugPutc(stderr, '>');
  Yap_DebugPutc(stderr, '\t');
  Yap_DebugPlWrite(tmod);
  Yap_DebugPutc(stderr, ':');
  if (ap->ModuleOfPred == IDB_MODULE) {
    Term t = Deref(ARG1);
    if (IsAtomTerm(t)) {
      Yap_DebugPlWrite(t);
    } else if (IsIntegerTerm(t)) {
      Yap_DebugPlWrite(t);
    } else {
      Functor f = FunctorOfTerm(t);
      Atom At = NameOfFunctor(f);
      Yap_DebugPlWrite(MkAtomTerm(At));
      Yap_DebugPutc(stderr, '/');
      Yap_DebugPlWrite(MkIntegerTerm(ArityOfFunctor(f)));
    }
  } else {
    if (ap->ArityOfPE == 0) {
      Atom At = (Atom)ap->FunctorOfPred;
      Yap_DebugPlWrite(MkAtomTerm(At));
    } else {
      Functor f = ap->FunctorOfPred;
      Atom At = NameOfFunctor(f);
      Yap_DebugPlWrite(MkAtomTerm(At));
      Yap_DebugPutc(stderr, '/');
      Yap_DebugPlWrite(MkIntegerTerm(ArityOfFunctor(f)));
    }
  }
  char s[1024];
  if (ap->PredFlags & StandardPredFlag)
    fprintf(stderr, "S");
  if (ap->PredFlags & CPredFlag)
    fprintf(stderr, "C");
  if (ap->PredFlags & UserCPredFlag)
    fprintf(stderr, "U");
  if (ap->PredFlags & SyncPredFlag)
    fprintf(stderr, "Y");
  if (ap->PredFlags & LogUpdatePredFlag)
    fprintf(stderr, "Y");
  if (ap->PredFlags & HiddenPredFlag)
    fprintf(stderr, "H");
  sprintf(s, "   %llx\n", ap->PredFlags);
  Yap_DebugPuts(stderr, s);
}
Exemplo n.º 2
0
static void
LookupFunctor(Functor fun)
{
  CACHE_REGS
  CELL hash = ((CELL)(fun))/(2*sizeof(CELL)) % LOCAL_ExportFunctorHashTableSize;
  export_functor_hash_entry_t *f;
  Atom name = NameOfFunctor(fun);
  UInt arity  = ArityOfFunctor(fun);

  f = LOCAL_ExportFunctorHashChain+hash;
  while (f->val) {
    if (f->val == fun) {
      return;
    }
    f++;
    if (f == LOCAL_ExportFunctorHashChain+LOCAL_ExportFunctorHashTableSize)
      f = LOCAL_ExportFunctorHashChain;
  }
  LookupAtom(name);
  f->val = fun;
  f->name = name;
  f->arity = arity;
  LOCAL_ExportFunctorHashTableNum++;
  if (LOCAL_ExportFunctorHashTableNum >
      LOCAL_ExportFunctorHashTableSize/2
      ) {
    GrowFunctorTable();
    if (!LOCAL_ExportFunctorHashChain) {
      return;
    }
  }
}
Exemplo n.º 3
0
static Term
get_matrix_element(Term t1, Term t2 USES_REGS)
{
  if (!IsPairTerm(t2)) {
    if (t2 == MkAtomTerm(AtomLength)) {
      Int sz = 1;
      while (IsApplTerm(t1)) {
	Functor f = FunctorOfTerm(t1);
	if (NameOfFunctor(f) != AtomNil) {
	  return MkIntegerTerm(sz);
	}
	sz *= ArityOfFunctor(f);
	t1 = ArgOfTerm(1, t1);
      }
      return MkIntegerTerm(sz);
    }
    Yap_ArithError(TYPE_ERROR_EVALUABLE, t2, "X is Y^[A]");
    return FALSE;      
  }
  while (IsPairTerm(t2)) {
    Int indx;
    Term indxt = Eval(HeadOfTerm(t2) PASS_REGS);
    if (!IsIntegerTerm(indxt)) {
      Yap_ArithError(TYPE_ERROR_EVALUABLE, t2, "X is Y^[A]");
      return FALSE;      
    }
    indx = IntegerOfTerm(indxt);
    if (!IsApplTerm(t1)) {
      Yap_ArithError(TYPE_ERROR_EVALUABLE, t1, "X is Y^[A]");
      return FALSE;      
    } else {
      Functor f = FunctorOfTerm(t1);
      if (ArityOfFunctor(f) < indx) {
	Yap_ArithError(TYPE_ERROR_EVALUABLE, t1, "X is Y^[A]");
	return FALSE;      
      }
    }
    t1 = ArgOfTerm(indx, t1);
    t2 = TailOfTerm(t2);
  }
  if (t2 != TermNil) {
    Yap_ArithError(TYPE_ERROR_EVALUABLE, t2, "X is Y^[A]");
    return FALSE;
  }
  return Eval(t1 PASS_REGS);
}
Exemplo n.º 4
0
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) {
  BACKUP_H();
  arity_t arity = ArityOfFunctor(f.f);
  Term o = Yap_MkNewApplTerm(f.f, arity);
  Term *tt = RepAppl(o) + 1;
  for (arity_t i = 0; i < arity; i++)
    tt[i] = ts[i].term();
  mk(o);
  RECOVER_H();
}
Exemplo n.º 5
0
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() {
  BACKUP_H();
  arity_t arity = ArityOfFunctor(f.f);
  Term *tt = new Term[arity];
  for (arity_t i = 0; i < arity; i++)
    tt[i] = ts[i].term();
  mk(Yap_MkApplTerm(f.f, arity, tt));
  delete[] tt;
  RECOVER_H();
}
Exemplo n.º 6
0
void
Yap_PrintPredName( PredEntry *ap )
{
    CACHE_REGS
    Term tmod = ap->ModuleOfPred;
    if (!tmod) tmod = TermProlog;
#if THREADS
    Yap_DebugPlWrite(MkIntegerTerm(worker_id));
    Yap_DebugPutc(LOCAL_c_error_stream,' ');
#endif
    Yap_DebugPutc(LOCAL_c_error_stream,'>');
    Yap_DebugPutc(LOCAL_c_error_stream,'\t');
    Yap_DebugPlWrite(tmod);
    Yap_DebugPutc(LOCAL_c_error_stream,':');
    if (ap->ModuleOfPred == IDB_MODULE) {
      Term t = Deref(ARG1);
      if (IsAtomTerm(t)) {
	Yap_DebugPlWrite(t);
      } else if (IsIntegerTerm(t)) {
	Yap_DebugPlWrite(t);
      } else {
	Functor f = FunctorOfTerm(t);
	Atom At = NameOfFunctor(f);
	Yap_DebugPlWrite(MkAtomTerm(At));
	Yap_DebugPutc(LOCAL_c_error_stream,'/');
	Yap_DebugPlWrite(MkIntegerTerm(ArityOfFunctor(f)));
      }
    } else {
      if (ap->ArityOfPE == 0) {
	Atom At = (Atom)ap->FunctorOfPred;
	Yap_DebugPlWrite(MkAtomTerm(At));
      } else {
	Functor f = ap->FunctorOfPred;
	Atom At = NameOfFunctor(f);
	Yap_DebugPlWrite(MkAtomTerm(At));
	Yap_DebugPutc(LOCAL_c_error_stream,'/');
	Yap_DebugPlWrite(MkIntegerTerm(ArityOfFunctor(f)));
      }
    }
    Yap_DebugPutc(LOCAL_c_error_stream,'\n');
}
Exemplo n.º 7
0
static inline int
rational_tree(Term d0) {
CACHE_REGS
  CELL  **to_visit_max = (CELL **)AuxBase, **to_visit  = (CELL **)AuxSp;

  if (IsPairTerm(d0)) {
    CELL *pt0 = RepPair(d0);

    return Yap_rational_tree_loop(pt0-1, pt0+1, to_visit, to_visit_max);
  } else if (IsApplTerm(d0)) {
    CELL *pt0 = RepAppl(d0);
    Functor f = (Functor)(*pt0);

    if (IsExtensionFunctor(f))
      return FALSE;
    return Yap_rational_tree_loop(pt0, pt0+ArityOfFunctor(f), to_visit, to_visit_max);
  } else
    return FALSE;
}
Exemplo n.º 8
0
Term YAPTerm::getArg(arity_t i) {
  BACKUP_MACHINE_REGS();
  Term tf = 0;
  Term t0 = gt();

  if (IsApplTerm(t0)) {
    if (i > ArityOfFunctor(FunctorOfTerm(t0)))
      throw YAPError(SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
    tf = (ArgOfTerm(i, t0));
  } else if (IsPairTerm(t0)) {
    if (i == 1)
      tf = (HeadOfTerm(t0));
    else if (i == 2)
      tf = (TailOfTerm(t0));
    else
      throw YAPError(SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
  } else {
    throw YAPError(SOURCE(), TYPE_ERROR_COMPOUND, t0, "t0.getArg()");
  }
  RECOVER_MACHINE_REGS();
  return tf;
}
Exemplo n.º 9
0
static Int compare_complex(register CELL *pt0, register CELL *pt0_end,
                           register CELL *pt1) {
  CACHE_REGS
  register CELL **to_visit = (CELL **)HR;
  register Int out = 0;

loop:
  while (pt0 < pt0_end) {
    register CELL d0, d1;
    ++pt0;
    ++pt1;
    d0 = Derefa(pt0);
    d1 = Derefa(pt1);
    if (IsVarTerm(d0)) {
      if (IsVarTerm(d1)) {
        out = Signed(d0) - Signed(d1);
        if (out)
          goto done;
      } else {
        out = -1;
        goto done;
      }
    } else if (IsVarTerm(d1)) {
      out = 1;
      goto done;
    } else {
      if (d0 == d1)
        continue;
      else if (IsAtomTerm(d0)) {
        if (IsAtomTerm(d1))
          out = cmp_atoms(AtomOfTerm(d0), AtomOfTerm(d1));
        else if (IsPrimitiveTerm(d1))
          out = 1;
        else
          out = -1;
        /* I know out must be != 0 */
        goto done;
      } else if (IsIntTerm(d0)) {
        if (IsIntTerm(d1))
          out = IntOfTerm(d0) - IntOfTerm(d1);
        else if (IsFloatTerm(d1)) {
          out = 1;
        } else if (IsLongIntTerm(d1)) {
          out = IntOfTerm(d0) - LongIntOfTerm(d1);
#ifdef USE_GMP
        } else if (IsBigIntTerm(d1)) {
          out = Yap_gmp_tcmp_int_big(IntOfTerm(d0), d1);
#endif
        } else if (IsRefTerm(d1))
          out = 1;
        else
          out = -1;
        if (out != 0)
          goto done;
      } else if (IsFloatTerm(d0)) {
        if (IsFloatTerm(d1)) {
          out = rfloat(FloatOfTerm(d0) - FloatOfTerm(d1));
        } else if (IsRefTerm(d1)) {
          out = 1;
        } else {
          out = -1;
        }
        if (out != 0)
          goto done;
      } else if (IsStringTerm(d0)) {
        if (IsStringTerm(d1)) {
          out = strcmp((char *)StringOfTerm(d0), (char *)StringOfTerm(d1));
        } else if (IsIntTerm(d1))
          out = 1;
        else if (IsFloatTerm(d1)) {
          out = 1;
        } else if (IsLongIntTerm(d1)) {
          out = 1;
#ifdef USE_GMP
        } else if (IsBigIntTerm(d1)) {
          out = 1;
#endif
        } else if (IsRefTerm(d1)) {
          out = 1;
        } else {
          out = -1;
        }
        if (out != 0)
          goto done;
      } else if (IsLongIntTerm(d0)) {
        if (IsIntTerm(d1))
          out = LongIntOfTerm(d0) - IntOfTerm(d1);
        else if (IsFloatTerm(d1)) {
          out = 1;
        } else if (IsLongIntTerm(d1)) {
          out = LongIntOfTerm(d0) - LongIntOfTerm(d1);
#ifdef USE_GMP
        } else if (IsBigIntTerm(d1)) {
          out = Yap_gmp_tcmp_int_big(LongIntOfTerm(d0), d1);
#endif
        } else if (IsRefTerm(d1)) {
          out = 1;
        } else {
          out = -1;
        }
        if (out != 0)
          goto done;
      }
#ifdef USE_GMP
      else if (IsBigIntTerm(d0)) {
        if (IsIntTerm(d1)) {
          out = Yap_gmp_tcmp_big_int(d0, IntOfTerm(d1));
        } else if (IsFloatTerm(d1)) {
          out = 1;
        } else if (IsLongIntTerm(d1)) {
          out = Yap_gmp_tcmp_big_int(d0, LongIntOfTerm(d1));
        } else if (IsBigIntTerm(d1)) {
          out = Yap_gmp_tcmp_big_big(d0, d1);
        } else if (IsRefTerm(d1))
          out = 1;
        else
          out = -1;
        if (out != 0)
          goto done;
      }
#endif
      else if (IsPairTerm(d0)) {
        if (!IsPairTerm(d1)) {
          if (IsApplTerm(d1)) {
            Functor f = FunctorOfTerm(d1);
            if (IsExtensionFunctor(f))
              out = 1;
            else if (!(out = 2 - ArityOfFunctor(f)))
              out = strcmp(".", (char *)RepAtom(NameOfFunctor(f))->StrOfAE);
          } else
            out = 1;
          goto done;
        }
#ifdef RATIONAL_TREES
        to_visit[0] = pt0;
        to_visit[1] = pt0_end;
        to_visit[2] = pt1;
        to_visit[3] = (CELL *)*pt0;
        to_visit += 4;
        *pt0 = d1;
#else
        /* store the terms to visit */
        if (pt0 < pt0_end) {
          to_visit[0] = pt0;
          to_visit[1] = pt0_end;
          to_visit[2] = pt1;
          to_visit += 3;
        }
#endif
        pt0 = RepPair(d0) - 1;
        pt0_end = RepPair(d0) + 1;
        pt1 = RepPair(d1) - 1;
        continue;
      } else if (IsRefTerm(d0)) {
        if (IsRefTerm(d1))
          out = Unsigned(RefOfTerm(d1)) - Unsigned(RefOfTerm(d0));
        else
          out = -1;
        goto done;
      } else if (IsApplTerm(d0)) {
        register Functor f;
        register CELL *ap2, *ap3;
        if (!IsApplTerm(d1)) {
          out = 1;
          goto done;
        } else {
          /* store the terms to visit */
          Functor f2;
          ap2 = RepAppl(d0);
          ap3 = RepAppl(d1);
          f = (Functor)(*ap2);
          if (IsExtensionFunctor(f)) {
            out = 1;
            goto done;
          }
          f2 = (Functor)(*ap3);
          if (IsExtensionFunctor(f2)) {
            out = -1;
            goto done;
          }
          /* compare functors */
          if (f != (Functor)*ap3) {
            if (!(out = ArityOfFunctor(f) - ArityOfFunctor(f2)))
              out = cmp_atoms(NameOfFunctor(f), NameOfFunctor(f2));
            goto done;
          }
#ifdef RATIONAL_TREES
          to_visit[0] = pt0;
          to_visit[1] = pt0_end;
          to_visit[2] = pt1;
          to_visit[3] = (CELL *)*pt0;
          to_visit += 4;
          *pt0 = d1;
#else
          /* store the terms to visit */
          if (pt0 < pt0_end) {
            to_visit[0] = pt0;
            to_visit[1] = pt0_end;
            to_visit[2] = pt1;
            to_visit += 3;
          }
#endif
          d0 = ArityOfFunctor(f);
          pt0 = ap2;
          pt0_end = ap2 + d0;
          pt1 = ap3;
          continue;
        }
      }
    }
  }
  /* Do we still have compound terms to visit */
  if (to_visit > (CELL **)HR) {
#ifdef RATIONAL_TREES
    to_visit -= 4;
    pt0 = to_visit[0];
    pt0_end = to_visit[1];
    pt1 = to_visit[2];
    *pt0 = (CELL)to_visit[3];
#else
    to_visit -= 3;
    pt0 = to_visit[0];
    pt0_end = to_visit[1];
    pt1 = to_visit[2];
#endif
    goto loop;
  }

done:
/* failure */
#ifdef RATIONAL_TREES
  while (to_visit > (CELL **)HR) {
    to_visit -= 4;
    pt0 = to_visit[0];
    pt0_end = to_visit[1];
    pt1 = to_visit[2];
    *pt0 = (CELL)to_visit[3];
  }
#endif
  return (out);
}
Exemplo n.º 10
0
static Int p_table( USES_REGS1 ) {
  Term mod, t, list;
  PredEntry *pe;
  Atom at;
  int arity;
  tab_ent_ptr tab_ent;
#ifdef MODE_DIRECTED_TABLING
  int* mode_directed = NULL;
#endif /* MODE_DIRECTED_TABLING */
  
  mod = Deref(ARG1);
  t = Deref(ARG2);
  list = Deref(ARG3);

  if (IsAtomTerm(t)) {
    at = AtomOfTerm(t);
    pe = RepPredProp(PredPropByAtom(at, mod));
    arity = 0;
  } else if (IsApplTerm(t)) {
    at = NameOfFunctor(FunctorOfTerm(t));
    pe = RepPredProp(PredPropByFunc(FunctorOfTerm(t), mod));
    arity = ArityOfFunctor(FunctorOfTerm(t));
  } else
    return (FALSE);
  if (list != TermNil) {  /* non-empty list */
#ifndef MODE_DIRECTED_TABLING
    Yap_Error(INTERNAL_COMPILER_ERROR, TermNil, "invalid tabling declaration for %s/%d (mode directed tabling not enabled)", AtomName(at), arity);
    return(FALSE);
#else 
    /*************************************************************************************
      The mode operator declaration is reordered as follows:
         1. arguments with mode 'index'         (any number)
         2. arguments with mode 'min' and 'max' (any number, following the original order)
         3. arguments with mode 'all'           (any number)
         4. arguments with mode 'sum' or 'last' (only one of the two is allowed)
         5. arguments with mode 'first'         (any number)
    *************************************************************************************/
    int pos_index = 0;
    int pos_min_max = 0;
    int pos_all = 0;
    int pos_sum_last = 0;
    int pos_first = 0;
    int i;
    int *aux_mode_directed;

    aux_mode_directed = malloc(arity * sizeof(int));
    for (i = 0; i < arity; i++) {
      int mode = IntOfTerm(HeadOfTerm(list));
      if (mode == MODE_DIRECTED_INDEX)
        pos_index++;
      else if (mode == MODE_DIRECTED_MIN || mode == MODE_DIRECTED_MAX)
        pos_min_max++;
      else if (mode == MODE_DIRECTED_ALL)
        pos_all++;
      else if (mode == MODE_DIRECTED_SUM || mode == MODE_DIRECTED_LAST) {
        if (pos_sum_last) {
          free(aux_mode_directed);
          Yap_Error(INTERNAL_COMPILER_ERROR, TermNil, "invalid tabling declaration for %s/%d (more than one argument with modes 'sum' and/or 'last')", AtomName(at), arity);
          return(FALSE);
        } else
          pos_sum_last = 1;
      }
      aux_mode_directed[i] = mode;
      list = TailOfTerm(list);
    }
    pos_first = pos_index + pos_min_max + pos_all + pos_sum_last;
    pos_sum_last = pos_index + pos_min_max + pos_all;
    pos_all = pos_index + pos_min_max;
    pos_min_max = pos_index;
    pos_index = 0;
    ALLOC_BLOCK(mode_directed, arity * sizeof(int), int);
    for (i = 0; i < arity; i++) {
      int aux_pos = 0;
      if (aux_mode_directed[i] == MODE_DIRECTED_INDEX)
        aux_pos = pos_index++;        
      else if (aux_mode_directed[i] == MODE_DIRECTED_MIN || aux_mode_directed[i] == MODE_DIRECTED_MAX)
        aux_pos = pos_min_max++;
      else if (aux_mode_directed[i] == MODE_DIRECTED_ALL)
        aux_pos = pos_all++;                
      else if (aux_mode_directed[i] == MODE_DIRECTED_SUM || aux_mode_directed[i] == MODE_DIRECTED_LAST)
        aux_pos = pos_sum_last++;        
      else if(aux_mode_directed[i] == MODE_DIRECTED_FIRST)
        aux_pos = pos_first++;
      mode_directed[aux_pos] = MODE_DIRECTED_SET(i, aux_mode_directed[i]);
    }
    free(aux_mode_directed);
#endif /* MODE_DIRECTED_TABLING */
  }
Exemplo n.º 11
0
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() {
  BACKUP_H();
  UInt arity = ArityOfFunctor(f.f);
  mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) );
  RECOVER_H();
}
Exemplo n.º 12
0
void eam_pass(CInstr *ppc)
{
  int alloc_found=0;
  int body=0;

	while (ppc) {
		switch ((int) ppc->op) {

		case get_var_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			    emit_inst(_get_var_X_op);
			    emit_par(ppc->new1);
			    emit_par(X_Var((Ventry *) ppc->new4));
			} else {
		            emit_inst(_get_var_Y_op);
			    emit_par(ppc->new1);
			    emit_par(Y_Var((Ventry *) ppc->new4));
			}
			break;
		case get_val_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			    emit_inst(_get_val_X_op);
			    emit_par(ppc->new1);
			    emit_par(X_Var((Ventry *) ppc->new4));
			} else {
			    emit_inst(_get_val_Y_op);
			    emit_par(ppc->new1);
			    emit_par(Y_Var((Ventry *) ppc->new4));
			}
		        break;

		case get_num_op:
		case get_atom_op:
		        emit_inst(_get_atom_op);
			emit_par(ppc->new1);
			emit_par(ppc->new4);
			break;

		case get_list_op:
		        emit_inst(_get_list_op);
			emit_par(ppc->new1);
			break;
		case get_struct_op:
		        emit_inst(_get_struct_op);
			emit_par(ppc->new1);
			emit_par(ppc->new4);
			emit_par(ArityOfFunctor((Functor ) ppc->new4));
			break;

		case unify_last_local_op:
		case unify_local_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			          emit_inst(_unify_local_X_op);
			          emit_par(X_Var((Ventry *) ppc->new4));
			} else {
			          emit_inst(_unify_local_Y_op);
			          emit_par(Y_Var((Ventry *) ppc->new4));
		        }
			break;

		case unify_last_val_op:
		case unify_val_op:
		        if (((Ventry *)(ppc->new4))->KindOfVE!=VoidVar) {
		           if (Is_X_Var((Ventry *) ppc->new4)) {
			          emit_inst(_unify_val_X_op);
			          emit_par(X_Var((Ventry *) ppc->new4));
			   } else {
			          emit_inst(_unify_val_Y_op);
			          emit_par(Y_Var((Ventry *) ppc->new4));

			   }
		        } else { emit_inst(_unify_void_op); }
			break;

		case unify_last_var_op:
		case unify_var_op:
		        if (((Ventry *)(ppc->new4))->KindOfVE!=VoidVar) {
		           if (Is_X_Var((Ventry *) ppc->new4)) {
			          emit_inst(_unify_var_X_op);
			          emit_par(X_Var((Ventry *) ppc->new4));
			   } else {
			          emit_inst(_unify_var_Y_op);
			          emit_par(Y_Var((Ventry *) ppc->new4));
			   }
		        } else { emit_inst(_unify_void_op); }
			break;

		case unify_last_atom_op:
		case unify_last_num_op:
		        emit_inst(_unify_last_atom_op);
			emit_par(ppc->new4);
			break;
		case unify_num_op:
		case unify_atom_op:
		        emit_inst(_unify_atom_op);
			emit_par(ppc->new4);
			break;
		case unify_list_op:
		        emit_inst(_unify_list_op);
			break;
		case unify_last_list_op:
		        emit_inst(_unify_last_list_op);
			break;
		case unify_struct_op:
		        emit_inst(_unify_struct_op);
			emit_par(ppc->new4);
			emit_par(ArityOfFunctor((Functor )ppc->new4));
			break;
		case unify_last_struct_op:
		        emit_inst(_unify_last_struct_op);
			emit_par(ppc->new4);
			emit_par(ArityOfFunctor((Functor )ppc->new4));
			break;

		case put_unsafe_op:
		  /*
		  printf("Got a put_unsafe...\n");
		        emit_inst(_put_unsafe_op);
			emit_par(ppc->new1);
			emit_par(Y_Var((Ventry *) ppc->new4));
			break;
		  */
		case put_val_op:
		  /*
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			       emit_inst(_put_val_X_op);
			       emit_par(ppc->new1);
			       emit_par(X_Var((Ventry *) ppc->new4));
			       break;
			} else {
			       emit_inst(_put_val_Y_op);
			       emit_par(ppc->new1);
			       emit_par(Y_Var((Ventry *) ppc->new4));
			       break;
			}
		  */
		case put_var_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			       emit_inst(_put_var_X_op);
			       emit_par(ppc->new1);
			       emit_par(X_Var((Ventry *) ppc->new4));
			} else {
 		           if (Is_P_Var((Ventry *) ppc->new4)) emit_inst(_put_var_P_op);
			   else emit_inst(_put_var_Y_op);
			       emit_par(ppc->new1);
			       emit_par(Y_Var((Ventry *) ppc->new4));
			}
			break;

		case put_num_op:
		case put_atom_op:
		        emit_inst(_put_atom_op);
			emit_par(ppc->new1);
			emit_par(ppc->new4);
			break;
		case put_list_op:
		        emit_inst(_put_list_op);
			emit_par(ppc->new1);
			break;
		case put_struct_op:
		        emit_inst(_put_struct_op);
			emit_par(ppc->new1);
			emit_par(ppc->new4);
			emit_par(ArityOfFunctor((Functor )ppc->new4));
			break;

		case write_local_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
		                  emit_inst(_write_local_X_op);
	  		          emit_par(X_Var((Ventry *) ppc->new4));
			} else {
		                  emit_inst(_write_local_Y_op);
			          emit_par(Y_Var((Ventry *) ppc->new4));
			}
			break;

		case write_val_op:
		        if (((Ventry *)(ppc->new4))->KindOfVE!=VoidVar) {
		           if (Is_X_Var((Ventry *) ppc->new4)) {
		                  emit_inst(_write_val_X_op);
	  		          emit_par(X_Var((Ventry *) ppc->new4));
			   } else {
		                  emit_inst(_write_val_Y_op);
			          emit_par(Y_Var((Ventry *) ppc->new4));
			   }
		        } else emit_inst(_write_void);
			break;

		case write_var_op:
		        if (((Ventry *)(ppc->new4))->KindOfVE!=VoidVar) {
		           if (Is_X_Var((Ventry *) ppc->new4)) {
		                  emit_inst(_write_var_X_op);
	  		          emit_par(X_Var((Ventry *) ppc->new4));
			   } else {
 		              if (Is_P_Var((Ventry *) ppc->new4)) emit_inst(_write_var_P_op);
		              else emit_inst(_write_var_Y_op);
			           emit_par(Y_Var((Ventry *) ppc->new4));
			   }
		        } else emit_inst(_write_void);
			break;


		case write_num_op:
		case write_atom_op:
		        emit_inst(_write_atom_op);
			emit_par(ppc->new4);
			break;
		case write_list_op:
		        emit_inst(_write_list_op);
			break;
		case write_last_list_op:
		        emit_inst(_write_last_list_op);
			break;
		case write_struct_op:
		        emit_inst(_write_struct_op);
			emit_par(ppc->new4);
			emit_par(ArityOfFunctor((Functor )ppc->new4));
			break;
		case write_last_struct_op:
		        emit_inst(_write_last_struct_op);
			emit_par(ppc->new4);
			emit_par(ArityOfFunctor((Functor )ppc->new4));
			break;

		case fail_op:
		        emit_inst(_fail_op);
			break;
		case cutexit_op:
		        printf("cutexit \n");
			exit(1);
			break;

		case cut_op:
		        emit_inst(_cut_op);
			break;
		case commit_op:
		        emit_inst(_commit_op);
			break;

		case procceed_op:
		        emit_inst(_proceed_op);
			break;
		case pop_op:
			emit_inst(_pop_op);
			emit_par(ppc->new4);
			break;
		case save_b_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			   emit_inst(_save_b_X_op);
			   emit_par(X_Var((Ventry *) ppc->new4));
		        } else {
			   emit_inst(_save_b_Y_op);
			   emit_par(Y_Var((Ventry *) ppc->new4));
		        }
			break;
	        case save_pair_op:
		       if (Is_X_Var((Ventry *) ppc->new4)) {
			  emit_inst(_save_pair_X_op);
			  emit_par(X_Var((Ventry *) ppc->new4));
		       } else {
			   emit_inst(_save_pair_Y_op);
			   emit_par(Y_Var((Ventry *) ppc->new4));
		       }
		       break;
	        case save_appl_op:
		        if (Is_X_Var((Ventry *) ppc->new4)) {
			  emit_inst(_save_appl_X_op);
			  emit_par(X_Var((Ventry *) ppc->new4));
		         } else {
			   emit_inst(_save_appl_Y_op);
			   emit_par(Y_Var((Ventry *) ppc->new4));
		         }
			break;
		case std_base_op:
		        emit_inst(_std_base+ppc->new4);
			break;

		case safe_call_op:
		        if (ppc->new1==1) {
		           emit_inst(_safe_call_unary_op);
			} else if (ppc->new1==2) {
		           emit_inst(_safe_call_binary_op);
			} else {
		           emit_inst(_safe_call_op);
			}
			emit_par(ppc->new4);
			break;

		case direct_safe_call_op:
		        if (ppc->new1==1) {
  		           emit_inst(_direct_safe_call_unary_op);
		        } else if (ppc->new1==2) {
  		           emit_inst(_direct_safe_call_binary_op);
			} else {
  		           emit_inst(_direct_safe_call_op);
			}
			emit_par(ppc->new4);
			break;

		case call_op:
			emit_inst(_call_op);
			emit_par(ppc->new4);
			break;

		case skip_while_var_op:
			emit_inst(_skip_while_var);
			break;
		case wait_while_var_op:
			emit_inst(_wait_while_var);
			break;
		case force_wait_op:
			emit_inst(_force_wait);
			break;
		case write_op:
		        if (ppc->new1=='\n') {
			  static Atom a=NULL;
			  if (a==NULL) a=Yap_LookupAtom("\n");
		          emit_inst(_put_atom_op);
			  emit_par(1);
			  emit_par((Cell) MkAtomTerm(a));
			}
 		        emit_inst(_write_call);
			break;
		case is_op:
			emit_inst(_is_call);
			break;
		case equal_op:
			emit_inst(_equal_call);
			break;

		case either_op:
			emit_inst(_either_op);
			emit_par(ppc->new1);
                        emit_upar((Cell) Code_Start+ (Cell) labels[ppc->new4]);
			break;
		case orelse_op:
	                emit_inst(_orelse_op);
                        emit_upar((Cell) Code_Start+ (Cell) labels[ppc->new4]);
			break;
		case orlast_op:
			emit_inst(_orlast_op);
			break;

		case create_first_box_op:
		case create_box_op:
		case create_last_box_op:
			emit_upar((Cell) Code_Start+ (Cell) labels[ppc->new4]);
		        alloc_found=1;
			break;

		case remove_box_op:
		case remove_last_box_op:
			break;

		case jump_op:
		        emit_inst(_jump_op);
			emit_upar((Cell) Code_Start+ (Cell) labels[ppc->new4]);
			break;
		case label_op:
		        if (pass==0) labels[ppc->new4] = get_addr();
			break;

		case run_op:
/* se ficar vazio, retirar no eam_am.c o +5 das linhas pc=clause->code+5 no only_1_clause e no call */
		        emit_inst(_try_me_op);
			emit_par(0);
			emit_par(0);
			emit_par(0);
			emit_par(0);
		        break;

		case only_1_clause_op:
		        emit_inst(_only_1_clause_op);
		        emit_par(ppc->new4);
			emit_par(((struct Clauses *)ppc->new4)->predi->arity);
		        emit_par(((struct Clauses *)ppc->new4)->nr_vars);
			emit_par(0); /* Nr da alternativa */
			break;
		case try_me_op:
		        emit_inst(_try_me_op);
			emit_par(ppc->new4);
			emit_par(((struct Clauses *)ppc->new4)->predi->arity);
		        emit_par(((struct Clauses *)ppc->new4)->nr_vars);
			emit_par(0); /* Nr da alternativa */
		        break;
		case retry_me_op:
		        emit_inst(_retry_me_op);
			emit_par(ppc->new4);
			emit_par(((struct Clauses *)ppc->new4)->predi->arity);
		        emit_par(((struct Clauses *)ppc->new4)->nr_vars);
			emit_par(ppc->new1);
		        break;
		case trust_me_op:
		        emit_inst(_trust_me_op);
			emit_par(ppc->new4);
			emit_par(((struct Clauses *)ppc->new4)->predi->arity);
		        emit_par(((struct Clauses *)ppc->new4)->nr_vars);
			emit_par(ppc->new1);
		        break;

		case body_op:
		        if (next_not_nop_inst(ppc->nextInst)==procceed_op) {
			  //emit_inst(_proceed_op);
			    break;
		        } else if (next_not_nop_inst(ppc->nextInst)==fail_op) {
			  //emit_inst(_fail_op);
			    break;
			}
			if (ppc->new4!=0) {
 		           emit_inst(_prepare_calls);
			   emit_par(ppc->new4); /* nr_calls */
			}
			body=1;
			break;

		case prepare_tries:
		        emit_inst(_prepare_tries);
			emit_par(ppc->new1);
			emit_par(ppc->new4);
			break;

		case exit_op:
		        emit_inst(_exit_eam);
			break;

		case mark_initialized_pvars_op:
		        break;
		case fetch_args_for_bccall:
		case bccall_op:
	 	        printf("[ Fatal Error: fetch and bccall instructions not supported ]\n");
 			exit(1);
		        break;

		case endgoal_op:
		case nop_op:
		case name_op:
			break;

		default:
		  if (pass) {
			printf("[ Sorry, there is at least one unsupported instruction in your code... %3d] %d\n",ppc->op,exit_op);
			printf("[ please note that beam still does not support a lot of builtins          ]\n");
		  }
		        emit_inst(_fail_op);

		}
		ppc = ppc->nextInst;
	}
	emit_inst(_exit_eam);
        emit_par(-1);
}
Exemplo n.º 13
0
static Int
p_setarg( USES_REGS1 )
{
  CELL ti = Deref(ARG1), ts = Deref(ARG2), t3 = Deref(ARG3);
  Int i;

  if (IsVarTerm(t3) &&
      VarOfTerm(t3) > H &&VarOfTerm(t3) < ASP) {
    /* local variable */
    Term tn = MkVarTerm();
    Bind_Local(VarOfTerm(t3), tn);
    t3 = tn;
  }
  if (IsVarTerm(ti)) {
    Yap_Error(INSTANTIATION_ERROR,ti,"setarg/3");
    return FALSE;
  } else {
    if (IsIntTerm(ti))
      i = IntOfTerm(ti);
    else {
      Term te = Yap_Eval(ti);
      if (IsIntegerTerm(te)) {
	i = IntegerOfTerm(te);
      } else {
	Yap_Error(TYPE_ERROR_INTEGER,ti,"setarg/3");
	return FALSE;
      }
    }
  }
  if (IsVarTerm(ts)) {
    Yap_Error(INSTANTIATION_ERROR,ts,"setarg/3");
  } else if(IsApplTerm(ts)) {
    CELL *pt;
    if (IsExtensionFunctor(FunctorOfTerm(ts))) {
      Yap_Error(TYPE_ERROR_COMPOUND,ts,"setarg/3");
      return FALSE;
    }
    if (i < 1 || i > (Int)ArityOfFunctor(FunctorOfTerm(ts))) {
      if (i<0)
	Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO,ts,"setarg/3");
      return FALSE;
      if (i==0)
	Yap_Error(DOMAIN_ERROR_NOT_ZERO,ts,"setarg/3");
      return FALSE;
    }
    pt = RepAppl(ts)+i;
    /* the evil deed is to be done now */
    MaBind(pt, t3);
  } else if(IsPairTerm(ts)) {
    CELL *pt;
    if (i < 1 || i > 2) {
      if (i<0)
	Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO,ts,"setarg/3");
      return FALSE;
    }
    pt = RepPair(ts)+i-1;
    /* the evil deed is to be done now */
    MaBind(pt, t3);    
  } else {
    Yap_Error(TYPE_ERROR_COMPOUND,ts,"setarg/3");
    return FALSE;
  }
  return TRUE;
}
Exemplo n.º 14
0
xarg *
Yap_ArgListToVector (Term listl, const param_t *def, int n)
{
  CACHE_REGS
    xarg *a = calloc(  n , sizeof(xarg) );
  if (IsApplTerm(listl) && FunctorOfTerm(listl) == FunctorModule)
    listl = ArgOfTerm(2,listl);
  if (!IsPairTerm(listl) && listl != TermNil) {
    if (IsVarTerm(listl) ) {
	free( a );
	LOCAL_Error_TYPE = INSTANTIATION_ERROR;
	LOCAL_Error_Term = listl;
	return NULL;
      }
    if (IsAtomTerm(listl) ) {
      xarg *na = matchKey( AtomOfTerm(listl), a, n, def);
      if (!na) {
	free( a );
	LOCAL_Error_TYPE = TYPE_ERROR_LIST;
	LOCAL_Error_Term = listl;
	return NULL;
      }
    } else if (IsApplTerm(listl)) {
      Functor f = FunctorOfTerm( listl );
      if (IsExtensionFunctor(f)) {
	free( a );
	LOCAL_Error_TYPE = TYPE_ERROR_LIST;
	LOCAL_Error_Term = listl;
	return NULL;    
      }
      arity_t arity = ArityOfFunctor( f );
      if (arity != 1) {
	free( a );
	LOCAL_Error_TYPE = TYPE_ERROR_LIST;
	LOCAL_Error_Term = listl;
	return NULL;
      }
      xarg *na = matchKey( NameOfFunctor( f ), a, n, def);
      if (!na) {
	free( a );
	LOCAL_Error_TYPE = TYPE_ERROR_LIST;
	LOCAL_Error_Term = listl;
	return NULL;
      }
    } else {
      free( a );
      LOCAL_Error_TYPE = TYPE_ERROR_LIST;
      LOCAL_Error_Term = listl;
      return NULL;
    }
    listl = MkPairTerm( listl, TermNil );
  }
  while (IsPairTerm(listl)) {
    Term hd = HeadOfTerm( listl );
    listl = TailOfTerm( listl );
    if (IsVarTerm(hd) || IsVarTerm(listl))  {
      LOCAL_Error_TYPE = INSTANTIATION_ERROR;
      if (IsVarTerm(hd)) {
	LOCAL_Error_Term = hd;
      } else {
	LOCAL_Error_Term = listl;
      }
      free( a );
      return NULL;
    }
    if (IsAtomTerm(hd)) {
      xarg *na = matchKey( AtomOfTerm( hd ), a, n, def);
      if (!na)
	return NULL;

      na->used = true;
      na->tvalue = TermNil;
      continue;
    } else if (IsApplTerm( hd )) {
      Functor f = FunctorOfTerm( hd );
      if (IsExtensionFunctor(f)) {
	LOCAL_Error_TYPE = TYPE_ERROR_PARAMETER;
	LOCAL_Error_Term = hd;
	free( a );
	return NULL;    
      }
      arity_t arity = ArityOfFunctor( f );
      if (arity != 1) {
	LOCAL_Error_TYPE = DOMAIN_ERROR_OUT_OF_RANGE;
	LOCAL_Error_Term = hd;
	free( a );
	return NULL;
      }
      xarg *na = matchKey( NameOfFunctor( f ), a, n, def);
      if (!na) {
        free( a );
	return NULL;
      }
      na->used = 1;
      na->tvalue = ArgOfTerm(1, hd);      
    } else {
      LOCAL_Error_TYPE = TYPE_ERROR_PARAMETER;
      free( a );
      return NULL;    
    }
  }
  if (IsVarTerm(listl)) {
    LOCAL_Error_TYPE = INSTANTIATION_ERROR;
    LOCAL_Error_Term = listl;
    free( a );
    return NULL;
  } else if (listl != TermNil) {
    LOCAL_Error_TYPE = TYPE_ERROR_LIST;
    LOCAL_Error_Term = listl;
    free( a );
    return NULL;
  }
  return a;
}              
Exemplo n.º 15
0
static int 
unifiable(CELL d0, CELL d1)
{
CACHE_REGS
#if THREADS
#undef Yap_REGS
  register REGSTORE *regp = Yap_regp;
#define Yap_REGS (*regp)
#elif SHADOW_REGS
#if defined(B) || defined(TR)
  register REGSTORE *regp = &Yap_REGS;

#define Yap_REGS (*regp)
#endif /* defined(B) || defined(TR) */
#endif

#if SHADOW_HB
  register CELL *HBREG = HB;
#endif

  register CELL *pt0, *pt1;

  deref_head(d0, unifiable_unk);

unifiable_nvar:
  /* d0 is bound */
  deref_head(d1, unifiable_nvar_unk);
unifiable_nvar_nvar:
  /* both arguments are bound */
  if (d0 == d1)
    return TRUE;
  if (IsPairTerm(d0)) {
    if (!IsPairTerm(d1)) {
      return (FALSE);
    }
    pt0 = RepPair(d0);
    pt1 = RepPair(d1);
    return (unifiable_complex(pt0 - 1, pt0 + 1, pt1 - 1));
  }
  else if (IsApplTerm(d0)) {
    pt0 = RepAppl(d0);
    d0 = *pt0;
    if (!IsApplTerm(d1))
      return (FALSE);      
    pt1 = RepAppl(d1);
    d1 = *pt1;
    if (d0 != d1) {
      return (FALSE);
    } else {
      if (IsExtensionFunctor((Functor)d0)) {
	switch(d0) {
	case (CELL)FunctorDBRef:
	  return(pt0 == pt1);
	case (CELL)FunctorLongInt:
	  return(pt0[1] == pt1[1]);
	case (CELL)FunctorString:
	  return(strcmp( (const char *)(pt0+2),  (const char *)(pt1+2)) == 0);
	case (CELL)FunctorDouble:
	  return(FloatOfTerm(AbsAppl(pt0)) == FloatOfTerm(AbsAppl(pt1)));
#ifdef USE_GMP
	case (CELL)FunctorBigInt:
	  return(Yap_gmp_tcmp_big_big(AbsAppl(pt0),AbsAppl(pt0)) == 0);
#endif /* USE_GMP */
	default:
	  return(FALSE);
	}
      }
      return (unifiable_complex(pt0, pt0 + ArityOfFunctor((Functor) d0),
			     pt1));
    }
  } else {
    return (FALSE);
  }

  deref_body(d1, pt1, unifiable_nvar_unk, unifiable_nvar_nvar);
  /* d0 is bound and d1 is unbound */
  *(pt1) = d0;
  DO_TRAIL(pt1, d0);
  return (TRUE);

  deref_body(d0, pt0, unifiable_unk, unifiable_nvar);
  /* pt0 is unbound */
  deref_head(d1, unifiable_var_unk);
unifiable_var_nvar:
  /* pt0 is unbound and d1 is bound */
  *pt0 = d1;
   DO_TRAIL(pt0, d1);
  return TRUE;

  deref_body(d1, pt1, unifiable_var_unk, unifiable_var_nvar);
  /* d0 and pt1 are unbound */
  UnifyAndTrailCells(pt0, pt1);
  return (TRUE);
#if THREADS
#undef Yap_REGS
#define Yap_REGS (*Yap_regp)  
#elif SHADOW_REGS
#if defined(B) || defined(TR)
#undef Yap_REGS
#endif /* defined(B) || defined(TR) */
#endif
}
Exemplo n.º 16
0
static int 
unifiable_complex(CELL *pt0, CELL *pt0_end, CELL *pt1)
{
CACHE_REGS
#ifdef THREADS
#undef Yap_REGS
  register REGSTORE *regp = Yap_regp;
#define Yap_REGS (*regp)
#elif defined(SHADOW_REGS)
#if defined(B) || defined(TR)
  register REGSTORE *regp = &Yap_REGS;

#define Yap_REGS (*regp)
#endif /* defined(B) || defined(TR) || defined(HB) */
#endif

#ifdef SHADOW_HB
  register CELL *HBREG = HB;
#endif /* SHADOW_HB */

  struct unif_record  *unif = (struct unif_record *)AuxBase;
  struct v_record *to_visit  = (struct v_record *)AuxSp;
#define unif_base ((struct unif_record *)AuxBase)
#define to_visit_base ((struct v_record *)AuxSp)

loop:
  while (pt0 < pt0_end) {
    register CELL *ptd0 = pt0+1; 
    register CELL d0;

    ++pt1;
    pt0 = ptd0;
    d0 = *ptd0;
    deref_head(d0, unifiable_comp_unk);
  unifiable_comp_nvar:
    {
      register CELL *ptd1 = pt1;
      register CELL d1 = *ptd1;

      deref_head(d1, unifiable_comp_nvar_unk);
    unifiable_comp_nvar_nvar:
      if (d0 == d1)
	continue;
      if (IsPairTerm(d0)) {
	if (!IsPairTerm(d1)) {
	  goto cufail;
	}
	/* now link the two structures so that no one else will */
	/* come here */
	/* store the terms to visit */
	if (RATIONAL_TREES || pt0 < pt0_end) {
	  to_visit --;
#ifdef RATIONAL_TREES
	  unif++;
#endif
	  if ((void *)to_visit < (void *)unif) {
	    CELL **urec = (CELL **)unif;
	    to_visit = (struct v_record *)Yap_shift_visit((CELL **)to_visit, &urec, NULL);
	    unif = (struct unif_record *)urec;
	  }
	  to_visit->start0 = pt0;
	  to_visit->end0 = pt0_end;
	  to_visit->start1 = pt1;
#ifdef RATIONAL_TREES
	  unif[-1].old = *pt0;
	  unif[-1].ptr = pt0;
	  *pt0 = d1;
#endif
	}
	pt0_end = (pt0 = RepPair(d0) - 1) + 2;
	pt1 = RepPair(d1) - 1;
	continue;
      }
      if (IsApplTerm(d0)) {
	register Functor f;
	register CELL *ap2, *ap3;

	if (!IsApplTerm(d1)) {
	  goto cufail;
	}
	/* store the terms to visit */
	ap2 = RepAppl(d0);
	ap3 = RepAppl(d1);
	f = (Functor) (*ap2);
	/* compare functors */
	if (f != (Functor) *ap3)
	  goto cufail;
	if (IsExtensionFunctor(f)) {
	  if (unify_extension(f, d0, ap2, d1))
	    continue;
	  goto cufail;
	}
	/* now link the two structures so that no one else will */
	/* come here */
	/* store the terms to visit */
	if (RATIONAL_TREES || pt0 < pt0_end) {
	  to_visit --;
#ifdef RATIONAL_TREES
	  unif++;
#endif
	  if ((void *)to_visit < (void *)unif) {
	    CELL **urec = (CELL **)unif;
	    to_visit = (struct v_record *)Yap_shift_visit((CELL **)to_visit, &urec, NULL);
	    unif = (struct unif_record *)urec;
	  }
	  to_visit->start0 = pt0;
	  to_visit->end0 = pt0_end;
	  to_visit->start1 = pt1;
#ifdef RATIONAL_TREES
	  unif[-1].old = *pt0;
	  unif[-1].ptr = pt0;
	  *pt0 = d1;
#endif
	}
	d0 = ArityOfFunctor(f);
	pt0 = ap2;
	pt0_end = ap2 + d0;
	pt1 = ap3;
	continue;
      }
      goto cufail;

      derefa_body(d1, ptd1, unifiable_comp_nvar_unk, unifiable_comp_nvar_nvar);
	/* d1 and pt2 have the unbound value, whereas d0 is bound */
      *(ptd1) = d0;
      DO_TRAIL(ptd1, d0);
      continue;
    }

    derefa_body(d0, ptd0, unifiable_comp_unk, unifiable_comp_nvar);
    /* first arg var */
    {
      register CELL d1;
      register CELL *ptd1;

      ptd1 = pt1;
      d1 = ptd1[0];
      /* pt2 is unbound */
      deref_head(d1, unifiable_comp_var_unk);
    unifiable_comp_var_nvar:
      /* pt2 is unbound and d1 is bound */
      *ptd0 = d1;
       DO_TRAIL(ptd0, d1);
      continue;

      derefa_body(d1, ptd1, unifiable_comp_var_unk, unifiable_comp_var_nvar);
      /* ptd0 and ptd1 are unbound */
      UnifyAndTrailGlobalCells(ptd0, ptd1);
    }
  }
  /* Do we still have compound terms to visit */
  if (to_visit < to_visit_base) {
    pt0 = to_visit->start0;
    pt0_end = to_visit->end0;
    pt1 = to_visit->start1;
    to_visit++;
    goto loop;
  }
#ifdef RATIONAL_TREES
  /* restore bindigs */
  while (unif-- != unif_base) {
    CELL *pt0;

    pt0 = unif->ptr;
    *pt0 = unif->old;
  }
#endif
  return TRUE;

cufail:
#ifdef RATIONAL_TREES
  /* restore bindigs */
  while (unif-- != unif_base) {
    CELL *pt0;

    pt0 = unif->ptr;
    *pt0 = unif->old;
  }
#endif
  return FALSE;
#ifdef THREADS
#undef Yap_REGS
#define Yap_REGS (*Yap_regp)  
#elif defined(SHADOW_REGS)
#if defined(B) || defined(TR)
#undef Yap_REGS
#endif /* defined(B) || defined(TR) */
#endif
}
Exemplo n.º 17
0
int
Yap_rational_tree_loop(CELL *pt0, CELL *pt0_end, CELL **to_visit, CELL **to_visit_max)
{
  CELL ** base = to_visit;
rtree_loop:
  while (pt0 < pt0_end) {
    register CELL *ptd0;
    register CELL d0;

    ptd0 = ++pt0; 
    pt0 = ptd0;
    d0 = *ptd0;
    deref_head(d0, rtree_loop_unk);
  rtree_loop_nvar:
    {
      if (d0 == TermFoundVar)
	goto cufail;
      if (IsPairTerm(d0)) {
	to_visit -= 3;
	if (to_visit < to_visit_max) {
	  to_visit = Yap_shift_visit(to_visit, &to_visit_max, &base);
	}
	to_visit[0] = pt0;
	to_visit[1] = pt0_end;
	to_visit[2] = (CELL *)*pt0;
	*pt0 = TermFoundVar;
	pt0_end = (pt0 = RepPair(d0) - 1) + 2;
	continue;
      }
      if (IsApplTerm(d0)) {
	register Functor f;
	register CELL *ap2;

	/* store the terms to visit */
	ap2 = RepAppl(d0);
	f = (Functor) (*ap2);
	/* compare functors */
	if (IsExtensionFunctor(f)) {
	  continue;
	}
	to_visit -= 3;
	if (to_visit < to_visit_max) {
	  to_visit = Yap_shift_visit(to_visit, &to_visit_max, &base);
	}
	to_visit[0] = pt0;
	to_visit[1] = pt0_end;
	to_visit[2] = (CELL *)*pt0;
	*pt0 = TermFoundVar;
	d0 = ArityOfFunctor(f);
	pt0 = ap2;
	pt0_end = ap2 + d0;
	continue;
      }
      continue;
    }

    derefa_body(d0, ptd0, rtree_loop_unk, rtree_loop_nvar);
  }
  /* Do we still have compound terms to visit */
  if (to_visit < base) {
    pt0 = to_visit[0];
    pt0_end = to_visit[1];
    *pt0 = (CELL)to_visit[2];
    to_visit += 3;
    goto rtree_loop;
  }
  return FALSE;

cufail:
  /* we found an infinite term */
  while (to_visit < (CELL **)base) {
    CELL *pt0;
    pt0 = to_visit[0];
    *pt0 = (CELL)to_visit[2];
    to_visit += 3;
  }
  return TRUE;
}
Exemplo n.º 18
0
static int 
OCUnify(register CELL d0, register CELL d1)
{
CACHE_REGS
  register CELL *pt0, *pt1;

#if SHADOW_HB
  register CELL *HBREG = HB;
#endif

  deref_head(d0, oc_unify_unk);

oc_unify_nvar:
  /* d0 is bound */
  deref_head(d1, oc_unify_nvar_unk);
oc_unify_nvar_nvar:

  if (d0 == d1) {
    return (!rational_tree(d0));
  }
  /* both arguments are bound */
  if (IsPairTerm(d0)) {
    if (!IsPairTerm(d1)) {
	return (FALSE);
    }
    pt0 = RepPair(d0);
    pt1 = RepPair(d1);
    return (OCUnify_complex(pt0 - 1, pt0 + 1, pt1 - 1));
  }
  else if (IsApplTerm(d0)) {
    if (!IsApplTerm(d1))
	return (FALSE);
    pt0 = RepAppl(d0);
    d0 = *pt0;
    pt1 = RepAppl(d1);
    d1 = *pt1;
    if (d0 != d1) {
      return (FALSE);
    } else {
      if (IsExtensionFunctor((Functor)d0)) {
	switch(d0) {
	case (CELL)FunctorDBRef:
	  return(pt0 == pt1);
	case (CELL)FunctorLongInt:
	  return(pt0[1] == pt1[1]);
	case (CELL)FunctorDouble:
	  return(FloatOfTerm(AbsAppl(pt0)) == FloatOfTerm(AbsAppl(pt1)));
	case (CELL)FunctorString:
	  return(strcmp( (const char *)(pt0+2),  (const char *)(pt1+2)) == 0);
#ifdef USE_GMP
	case (CELL)FunctorBigInt:
	  return(Yap_gmp_tcmp_big_big(AbsAppl(pt0),AbsAppl(pt0)) == 0);
#endif /* USE_GMP */
	default:
	  return(FALSE);
	}
      }
      return (OCUnify_complex(pt0, pt0 + ArityOfFunctor((Functor) d0),
			      pt1));
    }
  } else {
    return(FALSE);
  }

  deref_body(d1, pt1, oc_unify_nvar_unk, oc_unify_nvar_nvar);
  /* d0 is bound and d1 is unbound */
  YapBind(pt1, d0);
  /* local variables cannot be in a term */
  if (pt1 > HR && pt1 < LCL0)
    return TRUE;
  if (rational_tree(d0))
    return(FALSE);
  return (TRUE);

  deref_body(d0, pt0, oc_unify_unk, oc_unify_nvar);
  /* pt0 is unbound */
  deref_head(d1, oc_unify_var_unk);
oc_unify_var_nvar:
  /* pt0 is unbound and d1 is bound */
  YapBind(pt0, d1);
  /* local variables cannot be in a term */
  if (pt0 > HR && pt0 < LCL0)
    return TRUE;
  if (rational_tree(d1))
    return(FALSE);
  return (TRUE);

  deref_body(d1, pt1, oc_unify_var_unk, oc_unify_var_nvar);
  /* d0 and pt1 are unbound */
  UnifyCells(pt0, pt1);
  return (TRUE);
  return (TRUE);
}
Exemplo n.º 19
0
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() {
  BACKUP_H();
  UInt arity = ArityOfFunctor(f.f);
  mk ( Yap_MkNewApplTerm( f.f, arity) );
  RECOVER_H();
}
Exemplo n.º 20
0
inline static Int compare(Term t1, Term t2) /* compare terms t1 and t2	 */
{

  if (t1 == t2)
    return 0;
  if (IsVarTerm(t1)) {
    if (IsVarTerm(t2))
      return Signed(t1) - Signed(t2);
    return -1;
  } else if (IsVarTerm(t2)) {
    /* get rid of variables */
    return 1;
  }
  if (IsAtomOrIntTerm(t1)) {
    if (IsAtomTerm(t1)) {
      if (IsAtomTerm(t2))
        return cmp_atoms(AtomOfTerm(t1), AtomOfTerm(t2));
      if (IsPrimitiveTerm(t2))
        return 1;
      if (IsStringTerm(t2))
        return 1;
      return -1;
    } else {
      if (IsIntTerm(t2)) {
        return IntOfTerm(t1) - IntOfTerm(t2);
      }
      if (IsApplTerm(t2)) {
        Functor fun2 = FunctorOfTerm(t2);
        switch ((CELL)fun2) {
        case double_e:
          return 1;
        case long_int_e:
          return IntOfTerm(t1) - LongIntOfTerm(t2);
#ifdef USE_GMP
        case big_int_e:
          return Yap_gmp_tcmp_int_big(IntOfTerm(t1), t2);
#endif
        case db_ref_e:
          return 1;
        case string_e:
          return -1;
        }
      }
      return -1;
    }
  } else if (IsPairTerm(t1)) {
    if (IsApplTerm(t2)) {
      Functor f = FunctorOfTerm(t2);
      if (IsExtensionFunctor(f))
        return 1;
      else {
        if (f != FunctorDot)
          return strcmp(".", RepAtom(NameOfFunctor(f))->StrOfAE);
        else {
          return compare_complex(RepPair(t1) - 1, RepPair(t1) + 1, RepAppl(t2));
        }
      }
    }
    if (IsPairTerm(t2)) {
      return (
          compare_complex(RepPair(t1) - 1, RepPair(t1) + 1, RepPair(t2) - 1));
    } else
      return 1;
  } else {
    /* compound term */
    Functor fun1 = FunctorOfTerm(t1);

    if (IsExtensionFunctor(fun1)) {
      /* float, long, big, dbref */
      switch ((CELL)fun1) {
      case double_e: {
        if (IsFloatTerm(t2))
          return (rfloat(FloatOfTerm(t1) - FloatOfTerm(t2)));
        if (IsRefTerm(t2))
          return 1;
        return -1;
      }
      case long_int_e: {
        if (IsIntTerm(t2))
          return LongIntOfTerm(t1) - IntOfTerm(t2);
        if (IsFloatTerm(t2)) {
          return 1;
        }
        if (IsLongIntTerm(t2))
          return LongIntOfTerm(t1) - LongIntOfTerm(t2);
#ifdef USE_GMP
        if (IsBigIntTerm(t2)) {
          return Yap_gmp_tcmp_int_big(LongIntOfTerm(t1), t2);
        }
#endif
        if (IsRefTerm(t2))
          return 1;
        return -1;
      }
#ifdef USE_GMP
      case big_int_e: {
        if (IsIntTerm(t2))
          return Yap_gmp_tcmp_big_int(t1, IntOfTerm(t2));
        if (IsFloatTerm(t2)) {
          return 1;
        }
        if (IsLongIntTerm(t2))
          return Yap_gmp_tcmp_big_int(t1, LongIntOfTerm(t2));
        if (IsBigIntTerm(t2)) {
          return Yap_gmp_tcmp_big_big(t1, t2);
        }
        if (IsRefTerm(t2))
          return 1;
        return -1;
      }
#endif
      case string_e: {
        if (IsApplTerm(t2)) {
          Functor fun2 = FunctorOfTerm(t2);
          switch ((CELL)fun2) {
          case double_e:
            return 1;
          case long_int_e:
            return 1;
#ifdef USE_GMP
          case big_int_e:
            return 1;
#endif
          case db_ref_e:
            return 1;
          case string_e:
            return strcmp((char *)StringOfTerm(t1), (char *)StringOfTerm(t2));
          }
          return -1;
        }
        return -1;
      }
      case db_ref_e:
        if (IsRefTerm(t2))
          return Unsigned(RefOfTerm(t2)) - Unsigned(RefOfTerm(t1));
        return -1;
      }
    }
    if (!IsApplTerm(t2)) {
      if (IsPairTerm(t2)) {
        Int out;
        Functor f = FunctorOfTerm(t1);

        if (!(out = ArityOfFunctor(f)) - 2)
          out = strcmp((char *)RepAtom(NameOfFunctor(f))->StrOfAE, ".");
        return out;
      }
      return 1;
    } else {
      Functor fun2 = FunctorOfTerm(t2);
      Int r;

      if (IsExtensionFunctor(fun2)) {
        return 1;
      }
      r = ArityOfFunctor(fun1) - ArityOfFunctor(fun2);
      if (r)
        return r;
      r = cmp_atoms(NameOfFunctor(fun1), NameOfFunctor(fun2));
      if (r)
        return r;
      else
        return (compare_complex(RepAppl(t1), RepAppl(t1) + ArityOfFunctor(fun1),
                                RepAppl(t2)));
    }
  }
}
Exemplo n.º 21
0
static Term
Eval(Term t USES_REGS)
{

  if (IsVarTerm(t)) {
    return Yap_ArithError(INSTANTIATION_ERROR,t,"in arithmetic");
  } else if (IsNumTerm(t)) {
    return t;
  } else if (IsAtomTerm(t)) {
    ExpEntry *p;
    Atom name  = AtomOfTerm(t);

    if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 0)))) {
      /* error */
      Term ti[2];

      /* error */
      ti[0] = t;
      ti[1] = MkIntTerm(0);
      t = Yap_MkApplTerm(FunctorSlash, 2, ti);

      return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
			    "atom %s in arithmetic expression",
			    RepAtom(name)->StrOfAE);
    }
    return Yap_eval_atom(p->FOfEE);
  } else if (IsApplTerm(t)) {
    Functor fun = FunctorOfTerm(t);
    if (fun == FunctorString) {
      const char *s = StringOfTerm(t);
      if (s[1] == '\0')
	return MkIntegerTerm(s[0]);
      return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
			    "string in arithmetic expression");
    } else if ((Atom)fun == AtomFoundVar) {
      return Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil,
			    "cyclic term in arithmetic expression");
    } else {
      Int n = ArityOfFunctor(fun);
      Atom name  = NameOfFunctor(fun);
      ExpEntry *p;
      Term t1, t2;
      
      if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, n)))) {
	Term ti[2];

	/* error */
	ti[0] = t;
	ti[1] = MkIntegerTerm(n);
	t = Yap_MkApplTerm(FunctorSlash, 2, ti);
	return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
			      "functor %s/%d for arithmetic expression",
			      RepAtom(name)->StrOfAE,n);
      }
      if (p->FOfEE == op_power && p->ArityOfEE == 2) {
	t2 = ArgOfTerm(2, t);
	if (IsPairTerm(t2)) {
	  return get_matrix_element(ArgOfTerm(1, t), t2 PASS_REGS);
	}
      }
      *RepAppl(t) = (CELL)AtomFoundVar;
      t1 = Eval(ArgOfTerm(1,t) PASS_REGS);
      if (t1 == 0L) {
	*RepAppl(t) = (CELL)fun;
	return FALSE;
      }
      if (n == 1) {
	*RepAppl(t) = (CELL)fun;
	return Yap_eval_unary(p->FOfEE, t1);
      }
      t2 = Eval(ArgOfTerm(2,t) PASS_REGS);
      *RepAppl(t) = (CELL)fun;
      if (t2 == 0L)
	return FALSE;
      return Yap_eval_binary(p->FOfEE,t1,t2);
    }
  } /* else if (IsPairTerm(t)) */ {
    if (TailOfTerm(t) != TermNil) {
      return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
			    "string must contain a single character to be evaluated as an arithmetic expression");
    }
    return Eval(HeadOfTerm(t) PASS_REGS);
  }
}
Exemplo n.º 22
0
/// Yap_ArgList2ToVector is much the same as before,
/// but assumes parameters also have something called a
/// scope
xarg *
Yap_ArgList2ToVector (Term listl, const param2_t *def, int n)
{
  CACHE_REGS
    xarg *a = calloc(  n , sizeof(xarg) );
  if (!IsPairTerm(listl) && listl != TermNil) {
    if (IsVarTerm(listl) ) {
      return failed( INSTANTIATION_ERROR, listl, a);      
    }
    if (IsAtomTerm(listl) ) {
      xarg *na = matchKey2( AtomOfTerm(listl), a, n, def);
      if (!na) {
        return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, listl, a);
      }
    }
    if (IsApplTerm(listl)) {
      Functor f = FunctorOfTerm( listl );
      if (IsExtensionFunctor(f)) {
        return failed( TYPE_ERROR_PARAMETER, listl, a);      
      }
      arity_t arity = ArityOfFunctor( f );
      if (arity != 1) {
        return failed( TYPE_ERROR_LIST, listl, a);      
      }
      xarg *na = matchKey2( NameOfFunctor( f ), a, n, def);
      if (!na) {
        return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, listl, a);
      }
    } else {
      return failed( TYPE_ERROR_LIST, listl, a);      
    }
    listl = MkPairTerm( listl, TermNil );
  }
  while (IsPairTerm(listl)) {
    Term hd = HeadOfTerm( listl );
    if (IsVarTerm(hd))  {
      return failed( INSTANTIATION_ERROR, hd, a);      
    }
    if (IsAtomTerm(hd)) {
      xarg *na = matchKey2( AtomOfTerm( hd ), a, n, def);
      if (!na) {
        return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a);
      }
      na->used = true;
      na->tvalue = TermNil;
      continue;
    } else if (IsApplTerm( hd )) {
      Functor f = FunctorOfTerm( hd );
      if (IsExtensionFunctor(f)) {
        return failed( TYPE_ERROR_PARAMETER, hd, a);      
      }
      arity_t arity = ArityOfFunctor( f );
      if (arity != 1) {
        return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a);
      }
      xarg *na = matchKey2( NameOfFunctor( f ), a, n, def);
      if (na) {
	na->used = 1;
	na->tvalue = ArgOfTerm(1, hd);
      } else {
        return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a);
      }
    } else {
      return failed( INSTANTIATION_ERROR, hd, a);      
    }
    listl = TailOfTerm(listl);
  }
  if (IsVarTerm(listl))  {
    return failed( INSTANTIATION_ERROR, listl, a);      
  }
  if (TermNil != listl) {
    return failed( TYPE_ERROR_LIST, listl, a);      
  }
  return a;
}