dis.c

00001 /* File:      dis.c
00002 ** Author(s): Warren, Swift, Xu, Sagonas
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** Copyright (C) ECRC, Germany, 1990
00007 ** 
00008 ** XSB is free software; you can redistribute it and/or modify it under the
00009 ** terms of the GNU Library General Public License as published by the Free
00010 ** Software Foundation; either version 2 of the License, or (at your option)
00011 ** any later version.
00012 ** 
00013 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00014 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00016 ** more details.
00017 ** 
00018 ** You should have received a copy of the GNU Library General Public License
00019 ** along with XSB; if not, write to the Free Software Foundation,
00020 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021 **
00022 ** $Id: dis.c,v 1.29 2006/07/14 16:49:36 tswift Exp $
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 "context.h"
00035 #include "psc_xsb.h"
00036 #include "hash_xsb.h"
00037 #include "loader_xsb.h"
00038 #include "cell_xsb.h"
00039 #include "inst_xsb.h"
00040 #include "builtin.h"
00041 #include "memory_xsb.h"
00042 #include "flags_xsb.h"
00043 #include "tries.h"
00044 #include "macro_xsb.h"
00045 
00046 /* --------------- The following are working variables ---------------- */
00047 
00048 extern Cell builtin_table[BUILTIN_TBL_SZ][2];
00049 extern TIFptr get_tip_or_tdisp(Psc);
00050 
00051 /* Include these so that the Gnu C Compiler does not complain */
00052 void dis_data(FILE *);
00053 void dis_text(FILE *);
00054 static void dis_data_sub(FILE *, Pair *, char *);
00055 
00056 void dis(xsbBool distext)
00057 {  
00058   FILE *filedes ;
00059 
00060    filedes = fopen("stdout","w"); 
00061    dis_data(filedes);
00062    if (distext) dis_text(filedes);
00063    fflush(filedes);
00064    fclose(filedes); 
00065 }
00066 
00067 void dis_data(FILE *filedes)
00068 {
00069         int i;
00070         Pair *temp_ptr;
00071         Psc psc_ptr;
00072         char *modname;
00073 
00074         temp_ptr = (Pair *)(&flags[MOD_LIST]);
00075         while(*temp_ptr) {
00076            psc_ptr = (*temp_ptr)->psc_ptr;
00077            modname = get_name(psc_ptr);
00078            if (get_type(psc_ptr))       /* 00000100 */
00079              fprintf(filedes, "module('%s',loaded).\n",modname);
00080            else 
00081              fprintf(filedes, "module('%s', unloaded).\n", modname);
00082            if (strcmp(modname,"global")==0)
00083                 for(i=0; i < (int)symbol_table.size; i++) {
00084                   if ( symbol_table.table[i] ) {
00085 /*                  fprintf(filedes, "... ... BUCKET NO. %d\n", i); */
00086                     dis_data_sub(filedes, (Pair *)(symbol_table.table + i),modname);
00087                   }
00088                 }
00089            else if (strcmp(modname,"usermod")==0) 
00090              fprintf(filedes, "equiv(usermod,global).\n");
00091            else 
00092              dis_data_sub(filedes, (Pair *)&get_data(psc_ptr),modname);
00093            fprintf(filedes, "\n");
00094            temp_ptr = &((*temp_ptr)->next);
00095         }
00096 }
00097 
00098 static void dis_data_sub(FILE *filedes, Pair *chain_ptr, char* modname)
00099 {
00100    Psc temp;
00101 
00102    while (*chain_ptr) {
00103         temp = (*chain_ptr)->psc_ptr;
00104         fprintf(filedes,"entry('%s',",modname);
00105         fprintf(filedes, "%p,", temp);
00106         fflush(filedes);
00107         fprintf(filedes, "'%s'", get_name(temp));
00108         fprintf(filedes, "/%d,", get_arity(temp));
00109         switch(get_type(temp)) {
00110             case T_PRED: fprintf(filedes, "'PRED',"); break;
00111             case T_DYNA: fprintf(filedes, "'DYNA',"); break;
00112             case T_ORDI: fprintf(filedes, "'ORDI',"); break;
00113               //            case T_FILE: fprintf(filedes, "'FILE',"); break;
00114             case T_MODU: fprintf(filedes, "'MODU',"); break;
00115             case T_FORN: fprintf(filedes, "'FORN',"); break;
00116             case T_UDEF: fprintf(filedes, "'UDEF',"); break;
00117             default:     fprintf(filedes, "\'????\',"); break;
00118         }
00119         switch(get_env(temp)) {
00120             case T_VISIBLE:  fprintf(filedes, "'VISIBLE',"); break;
00121             case T_HIDDEN:   fprintf(filedes, "'HIDDEN',"); break;
00122             case T_UNLOADED: fprintf(filedes, "'UNLOADED',"); break;
00123             default:         fprintf(filedes, "error_env,"); break;
00124         }
00125         // TLS: should T_DYNA be checked, also???
00126         if (get_type(temp) == T_PRED) {
00127           if (get_tip_or_tdisp(temp) == NULL) 
00128             fprintf(filedes, "'UNTABLED',"); 
00129           else 
00130             fprintf(filedes, "'TABLED',");
00131         } else
00132           fprintf(filedes, "'n/a',");
00133         fprintf(filedes, "%p).\n", get_ep(temp));  /* dsw ???? */
00134         chain_ptr = &((*chain_ptr)->next);
00135    } /* while */
00136 }
00137 
00138 CPtr print_inst(FILE *fd, CPtr inst_ptr)
00139 {
00140     Cell instr ;
00141     CPtr loc_pcreg ;
00142     int i,a;
00143     Psc psc;
00144 
00145     loc_pcreg = (CPtr) inst_ptr;
00146     fprintf(fd,"     inst("),
00147     fprintf(fd,"%p, ", loc_pcreg);
00148     instr = cell(loc_pcreg++) ;
00149 /* We want the instruction string printed out below.  
00150  * Someday we should ANSI-fy it. 
00151  */
00152     fprintf(fd, "%s",(char *)inst_table[cell_opcode(&instr)][0]);
00153     a = 1 ; /* current operand */
00154     for (i=1; i<=4; i++) {
00155         switch (inst_table[cell_opcode(&instr)][i]) {
00156          case A:
00157            if (cell_opcode(&instr) == (byte) builtin) {
00158              a++;
00159              fprintf(fd, ", '%d'", cell_operand3(&instr));
00160              fprintf(fd, ", %s", 
00161                      (char *)builtin_table[cell_operand3(&instr)][0]);
00162            } else 
00163              fprintf(fd, ", %d", cell_operandn(&instr,a++));
00164            break;
00165          case V:
00166            fprintf(fd, ", %d", cell_operandn(&instr,a++));
00167            break;
00168          case R:
00169            fprintf(fd, ", r%d", cell_operandn(&instr,a++));
00170            break;
00171          case T:
00172            fprintf(fd, ", %lx", cell(loc_pcreg++));
00173            break;
00174          case P:
00175            a++;
00176            break;
00177          case S:
00178            if (cell_opcode(&instr) == (byte) call ||
00179                cell_opcode(&instr) == (byte) xsb_execute) {
00180              fprintf(fd, ", 0x%lx", *loc_pcreg);
00181              psc = (Psc) cell(loc_pcreg++);
00182              fprintf(fd,", '%s'/%d", get_name(psc), get_arity(psc));
00183            }
00184            else
00185              fprintf(fd, ", 0x%lx", cell(loc_pcreg++));
00186            break;
00187          case C:
00188          case L:
00189          case G:
00190            fprintf(fd, ", 0x%lx", cell(loc_pcreg++));
00191            break;
00192          case I:
00193          case N:
00194            fprintf(fd, ", %ld", cell(loc_pcreg++));
00195            break;
00196          case B:
00197            fprintf(fd, ", %ld", (long) int_val(cell(loc_pcreg)));
00198            loc_pcreg++;
00199            break;
00200          case F:
00201            fprintf(fd, ", %f", ofloat_val(cell(loc_pcreg)));
00202            loc_pcreg++;
00203            break;
00204          case PP:
00205            a += 2;
00206            break;
00207          case PPP:
00208            break;
00209          case PPR:
00210            fprintf(fd, ", r%d", cell_operand3(&instr));
00211            break;
00212          case RRR:
00213            fprintf(fd, ", r%d", cell_operand1(&instr));
00214            fprintf(fd, ", r%d", cell_operand2(&instr));
00215            fprintf(fd, ", r%d", cell_operand3(&instr));
00216            break;
00217          case X:
00218            break;
00219          default:
00220            break;
00221         }  /* switch */
00222         /*if (cell_opcode(&instr) == noop) loc_pcreg += 2 * *(loc_pcreg-1); */
00223         if (cell_opcode(&instr) == noop) loc_pcreg += cell_operand3(&instr)/2; /* ?!@% */
00224         else if (cell_opcode(&instr) == dynnoop) loc_pcreg += cell_operand3(&instr)/2; /* ?!@% */
00225     } /* for */
00226     fprintf(fd, ")");
00227     fflush(fd);
00228     return loc_pcreg;
00229 } /* print_inst */
00230 
00231 
00232 void dis_text(FILE * filedes)
00233 {
00234    pseg   this_seg;
00235    pindex index_seg ;
00236    CPtr   endaddr, inst_addr2 ;
00237    int comma;
00238 
00239    fprintf(filedes, "\n/*text below\t\t*/\n\n");
00240    this_seg = (pseg) inst_begin_gl;
00241    while (this_seg) {           /* repeat for all text segment */
00242       fprintf(filedes, "segment([\n");
00243       endaddr = (CPtr) ((pb) seg_hdr(this_seg) + seg_size(this_seg)) ;
00244       inst_addr2 = seg_text(this_seg);
00245       comma = 0;
00246       while (inst_addr2<endaddr) {
00247         if (comma) 
00248           fprintf(filedes,", \n");
00249         comma = 1;
00250         inst_addr2 = print_inst(filedes, inst_addr2);
00251       }
00252       index_seg = seg_index(this_seg);
00253       while (index_seg) {
00254         inst_addr2 = i_block(index_seg);
00255         endaddr = (CPtr)((pb)index_seg + i_size(index_seg));
00256         if (cell_opcode(i_block(index_seg)) == try ||
00257             cell_opcode(i_block(index_seg)) == tabletry ||
00258             cell_opcode(i_block(index_seg)) == tabletrysingle) {        
00259                                                    /* is try/retry/trust */
00260           while (inst_addr2<endaddr) {
00261             if (comma) 
00262               fprintf(filedes,", \n");
00263             comma = 1;
00264             inst_addr2 = print_inst(filedes, inst_addr2);
00265           }
00266         } else {                                        /* is hash table */
00267           if (comma) 
00268             fprintf(filedes,", \n");
00269           fprintf(filedes, "     hash_table([\n");
00270           comma = 0;
00271           while (inst_addr2<endaddr) {
00272             if (comma) {
00273               fprintf(filedes, ", \n");
00274             }
00275             comma = 1;
00276             fprintf(filedes, 
00277                     "          hash_entry(%p,%lx)", 
00278                     inst_addr2, 
00279                     cell(inst_addr2));
00280             inst_addr2 ++;
00281           }
00282           fprintf(filedes, "])");
00283         }
00284         index_seg = i_next(index_seg);
00285       }
00286       fprintf(filedes, "]).\n");
00287       this_seg = seg_next(this_seg);
00288    }  
00289 }

Generated on Wed Jul 26 13:30:40 2006 for XSB by  doxygen 1.4.5