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) |
314 |
ilt_postprocessing(ilt); |
ilt_postprocessing(ilt); |
315 |
} |
} |
316 |
|
|
317 |
|
/* Dump an instruction lookup table */ |
318 |
|
static void ilt_dump(insn_lookup_t *ilt) |
319 |
|
{ |
320 |
|
rfc_array_t *rfct; |
321 |
|
int i,j; |
322 |
|
|
323 |
|
printf("ILT %p: nr_insn=%d, cbm_size=%d\n",ilt,ilt->nr_insn,ilt->cbm_size); |
324 |
|
|
325 |
|
for(i=0;i<RFC_ARRAY_NUMBER;i++) { |
326 |
|
rfct = ilt->rfct[i]; |
327 |
|
|
328 |
|
for(j=0;j<rfct->nr_elements;j++) |
329 |
|
printf(" (0x%4.4x,0x%4.4x) = 0x%4.4x\n",i,j,rfct->eqID[j]); |
330 |
|
} |
331 |
|
} |
332 |
|
|
333 |
|
/* Write the specified RFC array to disk */ |
334 |
|
static void ilt_store_rfct(FILE *fd,int id,rfc_array_t *rfct) |
335 |
|
{ |
336 |
|
/* Store RFC array ID + number of elements */ |
337 |
|
fwrite(&id,sizeof(id),1,fd); |
338 |
|
fwrite(&rfct->nr_elements,sizeof(rfct->nr_elements),1,fd); |
339 |
|
fwrite(&rfct->nr_eqid,sizeof(rfct->nr_eqid),1,fd); |
340 |
|
|
341 |
|
fwrite(rfct->eqID,rfct->nr_elements,sizeof(int),fd); |
342 |
|
} |
343 |
|
|
344 |
|
/* Write the full instruction lookup table */ |
345 |
|
static void ilt_store_table(FILE *fd,insn_lookup_t *ilt) |
346 |
|
{ |
347 |
|
int i; |
348 |
|
|
349 |
|
for(i=0;i<RFC_ARRAY_NUMBER;i++) |
350 |
|
if (ilt->rfct[i] != NULL) |
351 |
|
ilt_store_rfct(fd,i,ilt->rfct[i]); |
352 |
|
} |
353 |
|
|
354 |
|
/* Load an RFC array from disk */ |
355 |
|
static int ilt_load_rfct(FILE *fd,insn_lookup_t *ilt) |
356 |
|
{ |
357 |
|
u_int id,nr_elements,nr_eqid; |
358 |
|
rfc_array_t *rfct; |
359 |
|
size_t len; |
360 |
|
|
361 |
|
/* Read ID and number of elements */ |
362 |
|
if ((fread(&id,sizeof(id),1,fd) != 1) || |
363 |
|
(fread(&nr_elements,sizeof(nr_elements),1,fd) != 1) || |
364 |
|
(fread(&nr_eqid,sizeof(nr_eqid),1,fd) != 1)) |
365 |
|
return(-1); |
366 |
|
|
367 |
|
if ((id >= RFC_ARRAY_NUMBER) || (nr_elements > RFC_ARRAY_MAXSIZE)) |
368 |
|
return(-1); |
369 |
|
|
370 |
|
/* Allocate the RFC array with the eqID table */ |
371 |
|
len = sizeof(*rfct) + (nr_elements * sizeof(int)); |
372 |
|
|
373 |
|
if (!(rfct = malloc(len))) |
374 |
|
return(-1); |
375 |
|
|
376 |
|
memset(rfct,0,sizeof(*rfct)); |
377 |
|
rfct->nr_elements = nr_elements; |
378 |
|
rfct->nr_eqid = nr_eqid; |
379 |
|
|
380 |
|
/* Read the equivalent ID array */ |
381 |
|
fread(rfct->eqID,rfct->nr_elements,sizeof(int),fd); |
382 |
|
|
383 |
|
ilt->rfct[id] = rfct; |
384 |
|
return(0); |
385 |
|
} |
386 |
|
|
387 |
|
/* Check an instruction table loaded from disk */ |
388 |
|
static int ilt_check_cached_table(insn_lookup_t *ilt) |
389 |
|
{ |
390 |
|
int i; |
391 |
|
|
392 |
|
/* All arrays must have been loaded */ |
393 |
|
for(i=0;i<RFC_ARRAY_NUMBER;i++) |
394 |
|
if (!ilt->rfct[i]) |
395 |
|
return(-1); |
396 |
|
|
397 |
|
return(0); |
398 |
|
} |
399 |
|
|
400 |
|
/* Load a full instruction table from disk */ |
401 |
|
static insn_lookup_t *ilt_load_table(FILE *fd) |
402 |
|
{ |
403 |
|
insn_lookup_t *ilt; |
404 |
|
|
405 |
|
if (!(ilt = malloc(sizeof(*ilt)))) |
406 |
|
return NULL; |
407 |
|
|
408 |
|
memset(ilt,0,sizeof(*ilt)); |
409 |
|
|
410 |
|
while(!feof(fd)) { |
411 |
|
if (ilt_load_rfct(fd,ilt) == -1) |
412 |
|
break; |
413 |
|
} |
414 |
|
|
415 |
|
if (ilt_check_cached_table(ilt) == -1) |
416 |
|
return NULL; |
417 |
|
|
418 |
|
return ilt; |
419 |
|
} |
420 |
|
|
421 |
|
/* Build a filename for a cached ILT table on disk */ |
422 |
|
static char *ilt_build_filename(char *table_name) |
423 |
|
{ |
424 |
|
return(dyn_sprintf("ilt_%s_%s",sw_version_tag,table_name)); |
425 |
|
} |
426 |
|
|
427 |
|
/* Try to load a cached ILT table from disk */ |
428 |
|
static insn_lookup_t *ilt_cache_load(char *table_name) |
429 |
|
{ |
430 |
|
insn_lookup_t *ilt; |
431 |
|
char *filename; |
432 |
|
FILE *fd; |
433 |
|
|
434 |
|
if (!(filename = ilt_build_filename(table_name))) |
435 |
|
return NULL; |
436 |
|
|
437 |
|
if (!(fd = fopen(filename,"r"))) { |
438 |
|
free(filename); |
439 |
|
return NULL; |
440 |
|
} |
441 |
|
|
442 |
|
ilt = ilt_load_table(fd); |
443 |
|
fclose(fd); |
444 |
|
return ilt; |
445 |
|
} |
446 |
|
|
447 |
|
/* Store the specified ILT table on disk for future use (cache) */ |
448 |
|
static int ilt_cache_store(char *table_name,insn_lookup_t *ilt) |
449 |
|
{ |
450 |
|
char *filename; |
451 |
|
FILE *fd; |
452 |
|
|
453 |
|
if (!(filename = ilt_build_filename(table_name))) |
454 |
|
return(-1); |
455 |
|
|
456 |
|
if (!(fd = fopen(filename,"w"))) { |
457 |
|
free(filename); |
458 |
|
return(-1); |
459 |
|
} |
460 |
|
|
461 |
|
ilt_store_table(fd,ilt); |
462 |
|
fclose(fd); |
463 |
|
return(0); |
464 |
|
} |
465 |
|
|
466 |
/* Create an instruction lookup table */ |
/* Create an instruction lookup table */ |
467 |
insn_lookup_t *ilt_create(int nr_insn,ilt_get_insn_cbk_t get_insn, |
insn_lookup_t *ilt_create(char *table_name, |
468 |
|
int nr_insn,ilt_get_insn_cbk_t get_insn, |
469 |
ilt_check_cbk_t chk_lo,ilt_check_cbk_t chk_hi) |
ilt_check_cbk_t chk_lo,ilt_check_cbk_t chk_hi) |
470 |
{ |
{ |
471 |
insn_lookup_t *ilt; |
insn_lookup_t *ilt; |
472 |
|
|
473 |
|
/* Try to load a cached table from disk */ |
474 |
|
if ((ilt = ilt_cache_load(table_name))) { |
475 |
|
printf("ILT: loaded table \"%s\" from cache.\n",table_name); |
476 |
|
return ilt; |
477 |
|
} |
478 |
|
|
479 |
|
/* We have to build the full table... */ |
480 |
ilt = malloc(sizeof(*ilt)); |
ilt = malloc(sizeof(*ilt)); |
481 |
assert(ilt); |
assert(ilt); |
482 |
memset(ilt,0,sizeof(*ilt)); |
memset(ilt,0,sizeof(*ilt)); |
487 |
ilt->chk_lo = chk_lo; |
ilt->chk_lo = chk_lo; |
488 |
ilt->chk_hi = chk_hi; |
ilt->chk_hi = chk_hi; |
489 |
|
|
490 |
|
/* Compile the instruction opcodes */ |
491 |
ilt_compile(ilt); |
ilt_compile(ilt); |
492 |
|
|
493 |
|
/* Store the result on disk for future exec */ |
494 |
|
ilt_cache_store(table_name,ilt); |
495 |
return(ilt); |
return(ilt); |
496 |
} |
} |