From 6de73e6469249b9f17c6a6305e508dff9304237e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Wed, 11 Jun 2014 19:35:45 +0100 Subject: [PATCH] introudce handle_t and arity_t Android: /assets and debugging --- C/c_interface.c | 55 ++++++++++++++++++++++++++++--------------------- C/iopreds.c | 2 -- C/pl-yap.c | 48 +++++++++++++++++++++++++++++++++--------- C/qlyr.c | 2 +- C/qlyw.c | 2 +- C/save.c | 2 +- C/sysbits.c | 31 ++++++---------------------- C/threads.c | 4 ++++ C/tracer.c | 2 +- 9 files changed, 84 insertions(+), 64 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index 034504aae..ad9eee0bd 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -407,58 +407,58 @@ For implementation details and more information, please check term_t_slots in th /// @brief report the current position of the slots, assuming that they occupy the top of the stack. /// /// -X_API Int YAP_CurrentSlot(void); +X_API handle_t YAP_CurrentSlot(void); /// @brief allocate n empty new slots /// /// Return a handle to the system's default slot. -X_API Int YAP_NewSlots(int NumberOfSlots); +X_API handle_t YAP_NewSlots(int NumberOfSlots); /// @brief allocate n empty new slots /// /// Allocate _NumberOfSlots_ from the stack and return an handle to the /// last one. The other handle can be obtained by decrementing the handle. -X_API Int YAP_InitSlot(YAP_Term t); +X_API handle_t YAP_InitSlot(YAP_Term t); /// @brief read from a slot. /// /// -X_API YAP_Term YAP_GetFromSlot(YAP_Int slot); +X_API YAP_Term YAP_GetFromSlot(YAP_handle_t slot); /// @brief get the memory address of a slot /// /// Return the address of slot _slot_: please use with care. -X_API YAP_Term *YAP_AddressFromSlot(YAP_Int); +X_API YAP_Term *YAP_AddressFromSlot(YAP_handle_t); /// @brief get the memory address of the term actually stored in a slot /// /// -X_API YAP_Term *YAP_AddressOfTermInSlot(YAP_Int); +X_API YAP_Term *YAP_AddressOfTermInSlot(YAP_handle_t); /// @brief store term in a slot /// /// -X_API void YAP_PutInSlot(YAP_Int slot, YAP_Term t); +X_API void YAP_PutInSlot(YAP_handle_t slot, YAP_Term t); /// @brief Succeeds if it recovers the space allocated for $n$ contiguous slots starting at topSlot. /// /// Set the contents of slot _slot_ to _t_. -X_API int YAP_RecoverSlots(int, YAP_Int topSlot); +X_API int YAP_RecoverSlots(int, YAP_handle_t topSlot); /// @brief copies the first new n YAAM registers to slots /// /// Store the current first _HowMany_ arguments in new slots. -X_API YAP_Int YAP_ArgsToSlots(int HowMany); +X_API YAP_handle_t YAP_ArgsToSlots(int HowMany); /// @brief copies n slots such that sl is copied to the last abstract ,achine register. /// /// Set the first _HowMany_ arguments to the _HowMany_ slots // starting at _slot_. -X_API void YAP_SlotsToArgs(int HowMany, YAP_Int slot); +X_API void YAP_SlotsToArgs(int HowMany, YAP_handle_t slot); /// @} -static UInt +static arity_t current_arity(void) { CACHE_REGS @@ -2288,7 +2288,6 @@ YAP_EnterGoal(PredEntry *pe, Term *ptr, YAP_dogoalinfo *dgi) CACHE_REGS int out; - __android_log_print(ANDROID_LOG_INFO, "yapi.cpp", "got %p pred %p slot %d", LOCAL, pe, dgi->CurSlot); BACKUP_MACHINE_REGS(); LOCAL_PrologMode = UserMode; dgi->p = P; @@ -2299,12 +2298,16 @@ YAP_EnterGoal(PredEntry *pe, Term *ptr, YAP_dogoalinfo *dgi) Yap_PrepGoal(pe->ArityOfPE, ptr, B PASS_REGS); P = pe->CodeOfPred; dgi->b = LCL0-(CELL*)B; + { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "EMUL IN Yap_regp=%p LCL0=(%p) %x", &Yap_REGS, LCL0, LCL0[-15]) ; } out = run_emulator(dgi PASS_REGS); + //{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "EMUL DONE Yap_regp=%p LCL0=(%p) %x", &Yap_REGS, LCL0, LCL0[-15]) ; } RECOVER_MACHINE_REGS(); if (out) { - LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal Yap_StartSlots( PASS_REGS1 ); + } else { + LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal } + { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "EMUL DONE %d", out) ; } return out; } @@ -3068,6 +3071,7 @@ YAP_Init(YAP_init_args *yap_init) if (restore_result == FAIL_RESTORE) { yap_init->ErrorNo = LOCAL_Error_TYPE; yap_init->ErrorCause = LOCAL_ErrorMessage; + __android_log_print(ANDROID_LOG_INFO, __FILE__, "restore failed, %s ", LOCAL_ErrorMessage); /* shouldn't RECOVER_MACHINE_REGS(); be here ??? */ return YAP_BOOT_ERROR; } @@ -3104,6 +3108,7 @@ YAP_Init(YAP_init_args *yap_init) #endif /* YAPOR */ RECOVER_MACHINE_REGS(); } + __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %p", yap_init); /* make sure we do this after restore */ if (yap_init->MaxStackSize) { GLOBAL_AllowLocalExpansion = FALSE; @@ -3120,7 +3125,7 @@ YAP_Init(YAP_init_args *yap_init) } else { GLOBAL_AllowTrailExpansion = TRUE; } - if (yap_init->YapPrologRCFile) { + if (yap_init->YapPrologRCFile) { Yap_PutValue(AtomConsultOnBoot, MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologRCFile))); /* This must be done again after restore, as yap_flags @@ -3128,6 +3133,7 @@ YAP_Init(YAP_init_args *yap_init) */ yap_flags[HALT_AFTER_CONSULT_FLAG] = yap_init->HaltAfterConsult; } + __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %s",yap_init->YapPrologRCFile); if (yap_init->YapPrologTopLevelGoal) { Yap_PutValue(AtomTopLevelGoal, MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal))); } @@ -3140,6 +3146,7 @@ YAP_Init(YAP_init_args *yap_init) if (yap_init->QuietMode) { yap_flags[QUIET_MODE_FLAG] = TRUE; } + __android_log_print(ANDROID_LOG_INFO, __FILE__, "after setting startup "); if (BOOT_FROM_SAVED_STATE && !do_bootstrap) { if (restore_result == FAIL_RESTORE) { yap_init->ErrorNo = LOCAL_Error_TYPE; @@ -3155,9 +3162,11 @@ YAP_Init(YAP_init_args *yap_init) if (restore_result == DO_ONLY_CODE) { /* first, initialise the saved state */ Term t_goal = MkAtomTerm(AtomInitProlog); - YAP_RunGoalOnce(t_goal); + __android_log_print(ANDROID_LOG_INFO, __FILE__, "restore init goal "); + YAP_RunGoalOnce(t_goal); Yap_InitYaamRegs( 0 ); - return YAP_BOOT_FROM_SAVED_CODE; + __android_log_print(ANDROID_LOG_INFO, __FILE__, "restore done, loaded startup "); + return YAP_BOOT_FROM_SAVED_CODE; } else { return YAP_BOOT_FROM_SAVED_STACKS; } @@ -3569,7 +3578,7 @@ X_API char * YAP_cwd(void) { CACHE_REGS - char *buf; + char *buf = NULL; int len; if (!Yap_getcwd(LOCAL_FileNameBuf, YAP_FILENAME_MAX)) return FALSE; @@ -3883,11 +3892,11 @@ YAP_Erase(void *handle) return 1; } -X_API Int +X_API handle_t YAP_ArgsToSlots(int n) { CACHE_REGS - Int slot = Yap_NewSlots(n PASS_REGS); + handle_t slot = Yap_NewSlots(n PASS_REGS); CELL *ptr0 = LCL0+slot, *ptr1=&ARG1; while (n--) { *ptr0++ = *ptr1++; @@ -3896,7 +3905,7 @@ YAP_ArgsToSlots(int n) } X_API void -YAP_SlotsToArgs(int n, Int slot) +YAP_SlotsToArgs(int n, handle_t slot) { CACHE_REGS CELL *ptr0 = LCL0+slot, *ptr1=&ARG1; @@ -3938,8 +3947,8 @@ YAP_SetYAPFlag(yap_flag_t flag, int val) } -/* Int YAP_VarSlotToNumber(Int) */ -Int YAP_VarSlotToNumber(Int s) { +/* handle_t YAP_VarSlotToNumber(handle_t) */ +handle_t YAP_VarSlotToNumber(handle_t s) { CACHE_REGS Term *t = (CELL *)Deref(Yap_GetFromSlot(s PASS_REGS)); if (t < HR) @@ -3953,7 +3962,7 @@ Term YAP_ModuleUser(void) { } /* int YAP_PredicateHasClauses() */ -Int YAP_NumberOfClausesForPredicate(PredEntry *pe) { +handle_t YAP_NumberOfClausesForPredicate(PredEntry *pe) { return pe->cs.p_code.NOfClauses; } diff --git a/C/iopreds.c b/C/iopreds.c index fe8179cbe..493fe79c2 100755 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -958,8 +958,6 @@ p_write_string( USES_REGS1 ) int encoding; char buf[256]; - if ((s = Yap_TermToString( in, NULL, 0, &length, &encoding, 0))) - fprintf(stderr,"%ld %s\n",length, s); if ((s = Yap_TermToString( in, buf, 256, &length, &encoding, 0))) fprintf(stderr,"%ld %s\n",length, s); return TRUE; diff --git a/C/pl-yap.c b/C/pl-yap.c index 667daadc5..6a4b9c307 100755 --- a/C/pl-yap.c +++ b/C/pl-yap.c @@ -8,17 +8,11 @@ #include "Yatom.h" #include "pl-incl.h" #include "YapText.h" +#include "yapio.h" #if HAVE_MATH_H #include #endif -#define Quote_illegal_f 1 -#define Ignore_ops_f 2 -#define Handle_vars_f 4 -#define Use_portray_f 8 -#define To_heap_f 16 -#define Unfold_cyclics_f 32 - #ifdef HAVE_LIMITS_H #include #endif @@ -649,7 +643,7 @@ numberVars(term_t t, nv_options *opts, int n ARG_LD) { * PROMOTION * *******************************/ -static int +int check_float(double f) { #ifdef HAVE_FPCLASSIFY @@ -856,8 +850,6 @@ PL_get_chars(term_t t, char **s, unsigned flags) { return PL_get_nchars(t, NULL, s, flags); } -char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, int *encoding, int flags); - char * Yap_TermToString(Term t, char *s, size_t sz, size_t *length, int *encoding, int flags) { @@ -921,6 +913,42 @@ Yap_TermToString(Term t, char *s, size_t sz, size_t *length, int *encoding, int return NULL; } +char * +Yap_HandleToString(term_t l, size_t sz, size_t *length, int *encoding, int flags) +{ + CACHE_REGS + + char *r, buf[4096]; + + int64_t size; + IOSTREAM *fd; + + r = buf; + fd = Sopenmem(&r, &sz, "w"); + fd->encoding = ENC_UTF8; + { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I %d LCL0+%s=(%p) %p", l, buf, LCL0, &LCL0 ); } + if ( PL_write_term(fd, l, 1200, flags) && + Sputcode(EOS, fd) >= 0 && + Sflush(fd) >= 0 ) + { + { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I LCL0+%s=(%p) %p", buf, LCL0, &LCL0 ); } + size = Stell64(fd); + *length = size-1; + char *bf = malloc(*length+1); + if (!bf) + return NULL; + strncpy(bf,buf,*length+1); + Sclose(fd); + r = bf; + return r; + } + /* failed */ + if ( r != buf ) { + Sfree(r); + } + return NULL; +} + X_API int PL_ttymode(IOSTREAM *s) diff --git a/C/qlyr.c b/C/qlyr.c index fb6a70784..f953fa46d 100755 --- a/C/qlyr.c +++ b/C/qlyr.c @@ -16,8 +16,8 @@ * * *************************************************************************/ -#include #include "absmi.h" +#include #include "Foreign.h" #include "alloc.h" #include "yapio.h" diff --git a/C/qlyw.c b/C/qlyw.c index c01cc4d67..c5bd7f1ee 100755 --- a/C/qlyw.c +++ b/C/qlyw.c @@ -16,8 +16,8 @@ * * *************************************************************************/ -#include #include "absmi.h" +#include #include "Foreign.h" #include "alloc.h" #include "yapio.h" diff --git a/C/save.c b/C/save.c index 250fed0ef..52bd79384 100755 --- a/C/save.c +++ b/C/save.c @@ -19,6 +19,7 @@ static char SccsId[] = "@(#)save.c 1.3 3/15/90"; #endif #include "config.h" +#include "absmi.h" #include "SWI-Stream.h" #if _MSC_VER || defined(__MINGW32__) #if HAVE_WINSOCK2_H @@ -27,7 +28,6 @@ static char SccsId[] = "@(#)save.c 1.3 3/15/90"; #include #include #endif -#include "absmi.h" #include "alloc.h" #if USE_DL_MALLOC #include "dlmalloc.h" diff --git a/C/sysbits.c b/C/sysbits.c index a22ac155d..0c1e9bd61 100755 --- a/C/sysbits.c +++ b/C/sysbits.c @@ -1805,32 +1805,9 @@ Yap_volume_header(char *file) } -int Yap_getcwd(const char *buf, int len) +char * Yap_getcwd(const char *cwd, size_t cwdlen) { - CACHE_REGS -#if __simplescalar__ - /* does not implement getcwd */ - strncpy(Yap_buf,GLOBAL_pwd,len); -#elif HAVE_GETCWD - if (getcwd ((char *)buf, len) == NULL) { -#if HAVE_STRERROR - Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "%s in getcwd/1", strerror(errno)); -#else - Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "error %d in getcwd/1", errno); -#endif - return FALSE; - } -#else - if (getwd (buf) == NULL) { -#if HAVE_STRERROR - Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "%s in getcwd/1", strerror(errno)); -#else - Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "in getcwd/1"); -#endif - return FALSE; - } -#endif - return TRUE; + return PL_cwd(cwd, cwdlen); } /****** @@ -2743,7 +2720,11 @@ p_yap_paths( USES_REGS1 ) { out3 = MkAtomTerm(Yap_LookupAtom(DESTDIR "/" YAP_BINDIR)); } else { out1 = MkAtomTerm(Yap_LookupAtom(YAP_LIBDIR)); +#if __ANDROID__ + out2 = MkAtomTerm(Yap_LookupAtom("/assets/share")); +#else out2 = MkAtomTerm(Yap_LookupAtom(YAP_SHAREDIR)); +#endif out3 = MkAtomTerm(Yap_LookupAtom(YAP_BINDIR)); } return(Yap_unify(out1,ARG1) && diff --git a/C/threads.c b/C/threads.c index ba8e517f7..cae57b3a5 100755 --- a/C/threads.c +++ b/C/threads.c @@ -700,11 +700,15 @@ p_thread_set_concurrency( USES_REGS1 ) Yap_Error(TYPE_ERROR_INTEGER,tnew,"thread_set_concurrency/2"); return(FALSE); } +#if HAVE_PTHREAD_GETCONCURRENCY cur = MkIntegerTerm(pthread_getconcurrency()); if (pthread_setconcurrency(newc) != 0) { return FALSE; } return Yap_unify(ARG1, MkIntegerTerm(cur)); +#else + return FALSE; +#endif } static Int diff --git a/C/tracer.c b/C/tracer.c index 13dcf2eeb..ef85b083a 100755 --- a/C/tracer.c +++ b/C/tracer.c @@ -146,7 +146,7 @@ low_level_trace(yap_low_level_port port, PredEntry *pred, CELL *args) // if (!worker_id) return; LOCK(Yap_heap_regs->low_level_trace_lock); sc = Yap_heap_regs; - if (vsc_count == 161862) jmp_deb(1); + //if (vsc_count == 161862) jmp_deb(1); // Sfprintf(stderr,"B=%p ", B); #ifdef THREADS LOCAL_ThreadHandle.thread_inst_count++;