00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "xsb_config.h"
00028 #include "xsb_debug.h"
00029
00030 #include <stdio.h>
00031 #include <string.h>
00032
00033 #include "auxlry.h"
00034 #include "cell_xsb.h"
00035 #include "error_xsb.h"
00036 #include "psc_xsb.h"
00037 #include "tries.h"
00038 #include "hash_xsb.h"
00039 #include "macro_xsb.h"
00040 #include "loader_xsb.h"
00041 #include "flags_xsb.h"
00042 #include "sig_xsb.h"
00043 #include "inst_xsb.h"
00044 #include "memory_xsb.h"
00045 #include "register.h"
00046 #include "thread_xsb.h"
00047
00048 extern Psc synint_proc(Psc, int);
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #define CHAR_PTR_SIZE sizeof(char *)
00068
00069 char *string_find(char *str, int insert) {
00070
00071 char **ptr, *str0;
00072
00073 SYS_MUTEX_LOCK_NOERROR( MUTEX_STRING ) ;
00074 ptr = (char **)string_table.table + hash(str, 0, string_table.size);
00075 while (*ptr) {
00076 str0 = *ptr + CHAR_PTR_SIZE;
00077 if (strcmp(str, str0) == 0)
00078 goto exit_string_find;
00079 ptr = (char **)(*ptr);
00080 }
00081
00082 if (insert) {
00083 str0 = (char *)mem_alloc(CHAR_PTR_SIZE + strlen(str) + 1,STRING_SPACE);
00084 *ptr = str0;
00085 *(char **)str0 = NULL;
00086 str0 = str0 + CHAR_PTR_SIZE;
00087 strcpy(str0, str);
00088 string_table_increment_and_check_for_overflow;
00089 }
00090 else
00091 str0 = NULL ;
00092
00093 exit_string_find:
00094 SYS_MUTEX_UNLOCK_NOERROR( MUTEX_STRING ) ;
00095 return str0;
00096 }
00097
00098 char *string_find_safe(char *str) {
00099
00100 char *ptr, *str0;
00101
00102 ptr = (char *)((Integer)(*(string_table.table + hash(str, 0, string_table.size))) & ~1);
00103 while (ptr) {
00104 str0 = ptr + CHAR_PTR_SIZE;
00105 if (strcmp(str, str0) == 0)
00106 return str0;
00107 ptr = (char *)(((Integer)(*(void **)ptr)) & ~1);
00108 }
00109 return NULL;
00110 }
00111
00112
00113
00114
00115
00116
00117 static Psc make_psc_rec(char *name, char arity) {
00118 Psc temp;
00119
00120 temp = (Psc)mem_alloc(sizeof(struct psc_rec),ATOM_SPACE);
00121 set_type(temp, 0);
00122 temp->env = 0;
00123
00124
00125
00126
00127 set_arity(temp, arity);
00128 set_data(temp, 0);
00129 set_ep(temp,(byte *)&(temp->load_inst));
00130 set_name(temp, string_find(name, 1));
00131 cell_opcode(&(temp->load_inst)) = load_pred;
00132 temp->this_psc = temp;
00133 return temp;
00134 }
00135
00136
00137
00138
00139
00140 static Pair make_psc_pair(Psc psc_ptr, Pair *link_ptr) {
00141
00142 Pair new_pair;
00143
00144 new_pair = (Pair)mem_alloc(sizeof(struct psc_pair),ATOM_SPACE);
00145
00146 pair_psc(new_pair) = psc_ptr;
00147 pair_next(new_pair) = *link_ptr;
00148 *link_ptr = new_pair;
00149 return new_pair;
00150 }
00151
00152
00153
00154 extern CPtr dynpredep_to_prortb(CTXTdeclc void *pred_ep);
00155
00156 TIFptr *get_tip_or_tdisp(Psc temp)
00157 {
00158 CPtr temp1 ;
00159
00160 switch (get_type(temp)) {
00161 case T_DYNA:
00162 case T_PRED:
00163 temp1 = (CPtr)get_ep(temp);
00164 if (temp1 != 0) {
00165 switch (*(pb)temp1) {
00166 case tabletry:
00167 case tabletrysingle:
00168 return (TIFptr *) (temp1+2) ;
00169 case test_heap:
00170 if (*(pb)(temp1+2) == tabletry ||
00171 *(pb)(temp1+2) == tabletrysingle)
00172 return (TIFptr *) (temp1+4) ;
00173 else return (TIFptr *)NULL;
00174 break;
00175 case switchon3bound:
00176 case switchonbound:
00177 case switchonterm:
00178 if ( *(pb) (temp1+3) == tabletry
00179 || *(pb) (temp1+3) == tabletrysingle)
00180 return (TIFptr *) (temp1+5) ;
00181 else return (TIFptr *) NULL;
00182 default:
00183 return (TIFptr *) NULL;
00184 }
00185 }
00186 else return (TIFptr *) NULL;
00187 default:
00188 return (TIFptr *) NULL;
00189 }
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199 TIFptr get_tip(CTXTdeclc Psc psc) {
00200 TIFptr *tip = get_tip_or_tdisp(psc);
00201 #ifndef MULTI_THREAD
00202 return tip?(*tip):NULL;
00203 #else
00204 if (!tip) {
00205 CPtr temp1 = (CPtr) get_ep(psc);
00206 if ((get_type(psc) == T_DYNA) &&
00207 (*(pb)(temp1) == switchonthread)) {
00208 temp1 = dynpredep_to_prortb(CTXTc temp1);
00209 if (temp1 && (*(pb)temp1 == tabletrysingle))
00210 return *(TIFptr *)(temp1+2);
00211 else return (TIFptr) NULL;
00212 } else {
00213 if (get_tabled(psc)) {
00214 xsb_error("Internal Error in table dispatch\n");
00215 } else { return NULL; }
00216 }
00217 }
00218 if (TIF_EvalMethod(*tip) != DISPATCH_BLOCK) return *tip;
00219
00220 { struct TDispBlk_t *tdispblk = (struct TDispBlk_t *) (*tip);
00221 TIFptr rtip = (TIFptr)((&(tdispblk->Thread0))[th->tid]);
00222 if (!rtip) {
00223 rtip = New_TIF(CTXTc psc);
00224 (&(tdispblk->Thread0))[th->tid] = rtip;
00225 }
00226 return rtip;
00227 }
00228 #endif
00229 }
00230
00231
00232
00233 static int is_globalmod(Psc mod_psc)
00234 {
00235
00236
00237
00238
00239
00240 if (mod_psc)
00241 return (((Cell)get_data(mod_psc) == USERMOD_PSC));
00243 else
00244 return 1;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254 static Pair search(int arity, char *name, Pair *search_ptr)
00255 {
00256 Psc psc_ptr;
00257
00258
00259
00260 while (*search_ptr) {
00261 psc_ptr = (*search_ptr)->psc_ptr;
00262 if (strcmp(name, get_name(psc_ptr)) == 0
00263 && arity == get_arity(psc_ptr) )
00264 return (*search_ptr);
00265 else
00266 search_ptr = &((*search_ptr)->next);
00267 }
00268 return NULL;
00269 }
00270
00271
00272
00273
00274 static Pair insert0(char *name, byte arity, Pair *search_ptr, int *is_new)
00275 {
00276 Pair pair;
00277
00278 pair = search(arity, name, search_ptr);
00279 if (pair==NULL) {
00280 *is_new = 1;
00281 pair = make_psc_pair(make_psc_rec(name,arity), search_ptr);
00282 }
00283 else
00284 *is_new = 0;
00285 return pair;
00286 }
00287
00288
00289
00290
00291 Pair insert(char *name, byte arity, Psc mod_psc, int *is_new)
00292 {
00293 Pair *search_ptr, temp;
00294
00295 SYS_MUTEX_LOCK_NOERROR( MUTEX_SYMBOL ) ;
00296
00297 if (is_globalmod(mod_psc)) {
00298 search_ptr = (Pair *)(symbol_table.table +
00299 hash(name, arity, symbol_table.size));
00300 temp = insert0(name, arity, search_ptr, is_new);
00301 if (*is_new)
00302 symbol_table_increment_and_check_for_overflow;
00303 }
00304 else {
00305 search_ptr = (Pair *)&(get_data(mod_psc));
00306 temp = insert0(name, arity, search_ptr, is_new);
00307 }
00308 SYS_MUTEX_UNLOCK_NOERROR( MUTEX_SYMBOL ) ;
00309 return temp ;
00310 }
00311
00312
00313
00314
00315 Pair insert_module(int type, char *name)
00316 {
00317 Pair new_pair;
00318 int is_new;
00319
00320 SYS_MUTEX_LOCK_NOERROR( MUTEX_SYMBOL ) ;
00321 new_pair = insert0(name, 0, (Pair *)&flags[MOD_LIST], &is_new);
00322 if (is_new) {
00323 set_type(new_pair->psc_ptr, type);
00324 } else {
00325 set_type(new_pair->psc_ptr, get_type(new_pair->psc_ptr) | type);
00326 }
00327 SYS_MUTEX_UNLOCK_NOERROR( MUTEX_SYMBOL ) ;
00328 return new_pair;
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 Pair link_sym(Psc psc, Psc mod_psc)
00345 {
00346 Pair *search_ptr, found_pair;
00347 char *name, message[120];
00348 byte arity, global_flag, type;
00349
00350 SYS_MUTEX_LOCK_NOERROR( MUTEX_SYMBOL ) ;
00351 name = get_name(psc);
00352 arity = get_arity(psc);
00353 if ( (global_flag = is_globalmod(mod_psc)) )
00354 search_ptr = (Pair *)symbol_table.table +
00355 hash(name, arity, symbol_table.size);
00356 else
00357 search_ptr = (Pair *)&get_data(mod_psc);
00358 if ((found_pair = search(arity, name, search_ptr))) {
00359 if (pair_psc(found_pair) != psc) {
00360
00361
00362
00363
00364 type = get_type(pair_psc(found_pair));
00365 if ( type != T_ORDI ) {
00366 if (type == T_DYNA || type == T_PRED) {
00367 Psc mod_psc = (Psc) get_data(pair_psc(found_pair));
00368 sprintf(message,
00369 "%s/%d (type %d) had been defined in module: %s",
00370 name, arity, type, mod_psc == 0 ? "usermod" : get_name(mod_psc));
00371 } else
00372 sprintf(message,
00373 "%s/%d (type %d) had been defined in another module!",
00374 name, arity, type);
00375 xsb_warn(message);
00376 }
00377 pair_psc(found_pair) = psc;
00378 }
00379 }
00380 else {
00381 found_pair = make_psc_pair(psc, search_ptr);
00382 if (global_flag)
00383 symbol_table_increment_and_check_for_overflow;
00384 }
00385 SYS_MUTEX_UNLOCK_NOERROR( MUTEX_SYMBOL ) ;
00386 return found_pair;
00387 }
00388
00389
00390
00391
00392
00393
00394 Psc get_ret_psc(int n)
00395 {
00396 Pair temp;
00397 int new_indicator;
00398
00399 if (!ret_psc[n]) {
00400 temp = (Pair) insert("ret", (byte) n, global_mod, &new_indicator);
00401 ret_psc[n] = pair_psc(temp);
00402 }
00403 return ret_psc[n];
00404 }
00405
00406
00407
00408
00409
00410
00411 Psc get_intern_psc() {
00412
00413 Pair intern_handle;
00414 int new_indicator;
00415
00416 intern_handle = insert("intern", 1, global_mod, &new_indicator);
00417 return (pair_psc(intern_handle));
00418 }
00419