/[dynamips]/trunk/insn_lookup.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/insn_lookup.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

upstream/dynamips-0.2.6-RC2/insn_lookup.c revision 3 by dpavlin, Sat Oct 6 16:05:34 2007 UTC upstream/dynamips-0.2.8-RC1/insn_lookup.c revision 11 by dpavlin, Sat Oct 6 16:33:40 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   * Cisco 7200 (Predator) simulation platform.   * Cisco router simulation platform.
3   * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)   * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
4   *   *
5   * Instruction Lookup Tables.   * Instruction Lookup Tables.
# Line 17  Line 17 
17  #include "utils.h"  #include "utils.h"
18  #include "hash.h"  #include "hash.h"
19  #include "insn_lookup.h"  #include "insn_lookup.h"
20    #include "dynamips.h"
21    
22  /* Hash function for a CBM */  /* Hash function for a CBM */
23  static inline u_int cbm_hash_f(void *ccbm)  static inline u_int cbm_hash_f(void *ccbm)
# Line 313  static void ilt_compile(insn_lookup_t *i Line 314  static void ilt_compile(insn_lookup_t *i
314     ilt_postprocessing(ilt);     ilt_postprocessing(ilt);
315  }  }
316    
317    /* Dump an instruction lookup table */
318    static int ilt_dump(char *table_name,insn_lookup_t *ilt)
319    {
320       rfc_array_t *rfct;
321       char *filename;
322       FILE *fd;
323       int i,j;
324      
325       filename = dyn_sprintf("ilt_dump_%s_%s.txt",sw_version_tag,table_name);
326       assert(filename != NULL);
327    
328       fd = fopen(filename,"w");
329       assert(fd != NULL);
330      
331       fprintf(fd,"ILT %p: nr_insn=%d, cbm_size=%d\n",
332             ilt,ilt->nr_insn,ilt->cbm_size);
333    
334       for(i=0;i<RFC_ARRAY_NUMBER;i++) {
335          rfct = ilt->rfct[i];
336          
337          fprintf(fd,"RFCT %d: nr_elements=%d, nr_eqid=%d\n",
338                  i,rfct->nr_elements,rfct->nr_eqid);
339          
340          for(j=0;j<rfct->nr_elements;j++)
341             fprintf(fd,"  (0x%4.4x,0x%4.4x) = 0x%4.4x\n",i,j,rfct->eqID[j]);
342       }
343      
344       fclose(fd);
345       return(0);
346    }
347    
348    /* Write the specified RFC array to disk */
349    static void ilt_store_rfct(FILE *fd,int id,rfc_array_t *rfct)
350    {
351       /* Store RFC array ID + number of elements */
352       fwrite(&id,sizeof(id),1,fd);
353       fwrite(&rfct->nr_elements,sizeof(rfct->nr_elements),1,fd);
354       fwrite(&rfct->nr_eqid,sizeof(rfct->nr_eqid),1,fd);
355    
356       fwrite(rfct->eqID,sizeof(int),rfct->nr_elements,fd);
357    }
358    
359    /* Write the full instruction lookup table */
360    static void ilt_store_table(FILE *fd,insn_lookup_t *ilt)
361    {
362       int i;
363    
364       for(i=0;i<RFC_ARRAY_NUMBER;i++)
365          if (ilt->rfct[i] != NULL)
366             ilt_store_rfct(fd,i,ilt->rfct[i]);
367    }
368    
369    /* Load an RFC array from disk */
370    static int ilt_load_rfct(FILE *fd,insn_lookup_t *ilt)
371    {
372       u_int id,nr_elements,nr_eqid;
373       rfc_array_t *rfct;
374       size_t len;
375    
376       /* Read ID and number of elements */
377       if ((fread(&id,sizeof(id),1,fd) != 1) ||
378           (fread(&nr_elements,sizeof(nr_elements),1,fd) != 1) ||
379           (fread(&nr_eqid,sizeof(nr_eqid),1,fd) != 1))
380          return(-1);
381          
382       if ((id >= RFC_ARRAY_NUMBER) || (nr_elements > RFC_ARRAY_MAXSIZE))
383          return(-1);
384    
385       /* Allocate the RFC array with the eqID table */
386       len = sizeof(*rfct) + (nr_elements * sizeof(int));
387    
388       if (!(rfct = malloc(len)))
389          return(-1);
390    
391       memset(rfct,0,sizeof(*rfct));
392       rfct->nr_elements = nr_elements;
393       rfct->nr_eqid = nr_eqid;
394      
395       /* Read the equivalent ID array */
396       if (fread(rfct->eqID,sizeof(int),nr_elements,fd) != nr_elements) {
397          free(rfct);
398          return(-1);
399       }
400    
401       ilt->rfct[id] = rfct;
402       return(0);
403    }
404    
405    /* Check an instruction table loaded from disk */
406    static int ilt_check_cached_table(insn_lookup_t *ilt)
407    {
408       int i;
409    
410       /* All arrays must have been loaded */
411       for(i=0;i<RFC_ARRAY_NUMBER;i++)
412          if (!ilt->rfct[i])
413             return(-1);
414    
415       return(0);
416    }
417    
418    /* Load a full instruction table from disk */
419    static insn_lookup_t *ilt_load_table(FILE *fd)
420    {
421       insn_lookup_t *ilt;
422       int i;
423      
424       if (!(ilt = malloc(sizeof(*ilt))))
425          return NULL;
426    
427       memset(ilt,0,sizeof(*ilt));
428       fseek(fd,0,SEEK_SET);
429    
430       for(i=0;i<RFC_ARRAY_NUMBER;i++) {
431          if (ilt_load_rfct(fd,ilt) == -1)
432             return NULL;
433       }
434    
435       if (ilt_check_cached_table(ilt) == -1)
436          return NULL;
437    
438       return ilt;
439    }
440    
441    /* Build a filename for a cached ILT table on disk */
442    static char *ilt_build_filename(char *table_name)
443    {
444       return(dyn_sprintf("ilt_%s_%s",sw_version_tag,table_name));
445    }
446    
447    /* Try to load a cached ILT table from disk */
448    static insn_lookup_t *ilt_cache_load(char *table_name)
449    {
450       insn_lookup_t *ilt;
451       char *filename;
452       FILE *fd;
453    
454       if (!(filename = ilt_build_filename(table_name)))
455          return NULL;
456    
457       if (!(fd = fopen(filename,"rb"))) {
458          free(filename);
459          return NULL;
460       }
461    
462       ilt = ilt_load_table(fd);
463       fclose(fd);
464       return ilt;
465    }
466    
467    /* Store the specified ILT table on disk for future use (cache) */
468    static int ilt_cache_store(char *table_name,insn_lookup_t *ilt)
469    {
470       char *filename;
471       FILE *fd;
472    
473       if (!(filename = ilt_build_filename(table_name)))
474          return(-1);
475    
476       if (!(fd = fopen(filename,"wb"))) {
477          free(filename);
478          return(-1);
479       }
480    
481       ilt_store_table(fd,ilt);
482       fclose(fd);
483       free(filename);
484       return(0);
485    }
486    
487  /* Create an instruction lookup table */  /* Create an instruction lookup table */
488  insn_lookup_t *ilt_create(int nr_insn,ilt_get_insn_cbk_t get_insn,  insn_lookup_t *ilt_create(char *table_name,
489                              int nr_insn,ilt_get_insn_cbk_t get_insn,
490                            ilt_check_cbk_t chk_lo,ilt_check_cbk_t chk_hi)                            ilt_check_cbk_t chk_lo,ilt_check_cbk_t chk_hi)
491  {  {
492     insn_lookup_t *ilt;     insn_lookup_t *ilt;
493        
494       /* Try to load a cached table from disk */
495       if ((ilt = ilt_cache_load(table_name))) {
496          printf("ILT: loaded table \"%s\" from cache.\n",table_name);
497          return ilt;
498       }
499    
500       /* We have to build the full table... */
501     ilt = malloc(sizeof(*ilt));     ilt = malloc(sizeof(*ilt));
502     assert(ilt);     assert(ilt);
503     memset(ilt,0,sizeof(*ilt));     memset(ilt,0,sizeof(*ilt));
# Line 329  insn_lookup_t *ilt_create(int nr_insn,il Line 508  insn_lookup_t *ilt_create(int nr_insn,il
508     ilt->chk_lo   = chk_lo;     ilt->chk_lo   = chk_lo;
509     ilt->chk_hi   = chk_hi;     ilt->chk_hi   = chk_hi;
510    
511       /* Compile the instruction opcodes */
512     ilt_compile(ilt);     ilt_compile(ilt);
513      
514       /* Store the result on disk for future exec */
515       ilt_cache_store(table_name,ilt);
516     return(ilt);     return(ilt);
517  }  }

Legend:
Removed from v.3  
changed lines
  Added in v.11

  ViewVC Help
Powered by ViewVC 1.1.26