int main(void) { struct hashmap symtab; unsigned char buf[1024], *bp = buf; unsigned long result; struct eval *eval = eval_new((symlook_fn)symtab_lookup, &symtab); hashmap_init(&symtab, 0, hash_str, cmp_str, NULL, NULL); while ((*bp = fgetc(stdin)) > 0) { if (*bp == '\n') { *bp = '\0'; bp = strchr(buf, '='); *bp++ = '\0'; eval_expression(eval, bp, bp + strlen(bp), &result); sprintf(bp, "%lu", result); printf(" %s=%ld\n", buf, result); hashmap_put(&symtab, strdup(buf), strdup(bp)); bp = buf; } else { bp++; } } return EXIT_SUCCESS; }
/* print all simulator stats */ void sim_print_stats(FILE *fd) /* output stream */ { #if 0 /* not portable... :-( */ extern char etext, *sbrk(int); #endif if (!running) return; /* get stats time */ sim_end_time = time((time_t *)NULL); sim_elapsed_time = MAX(sim_end_time - sim_start_time, 1); #if 0 /* not portable... :-( */ /* compute simulator memory usage */ sim_mem_usage = (sbrk(0) - &etext) / 1024; #endif /* print simulation stats */ fprintf(fd, "\nsim: ** simulation statistics **\n"); stat_print_stats(sim_sdb, fd); sim_aux_stats(fd); fprintf(fd, "\n"); //Yuanwen Added here struct stat_stat_t * stat_max_power = stat_find_stat(sim_sdb, "max_cycle_power_cc3"); FILE *f=fopen("MaxPower.txt", "w"); fprintf(f, "%g", *stat_max_power->variant.for_double.var); //Yuanwen Added /* instantiate a new evaluator to avoid recursion problems */ struct eval_state_t *es = eval_new(stat_eval_ident, sim_sdb); //stat_eval_ident() is a function char *endp; struct stat_stat_t * stat_avg_total_power = stat_find_stat(sim_sdb, "avg_total_power_cycle_cc3"); struct eval_value_t val = eval_expr(es, stat_avg_total_power->variant.for_formula.formula, &endp); // if (eval_error != ERR_NOERR || *endp != '\0') // fprintf(fd, "<error: %s>", eval_err_str[eval_error]); // else // myfprintf(fd, stat->format, eval_as_double(val)); // fprintf(fd, " # %s", stat->desc); fprintf(f, ",%g", eval_as_double(val)); struct stat_stat_t * stat_1 = stat_find_stat(sim_sdb, "avg_alu_power_cc3"); struct eval_value_t val_1 = eval_expr(es, stat_1->variant.for_formula.formula, &endp); fprintf(f, ",%g", eval_as_double(val_1)); struct stat_stat_t * stat_2 = stat_find_stat(sim_sdb, "avg_lsq_power_cc3"); struct eval_value_t val_2 = eval_expr(es, stat_2->variant.for_formula.formula, &endp); fprintf(f, ",%g", eval_as_double(val_2)); /* done with the evaluator */ eval_delete(es); fclose(f); }
/* create a new stats database */ struct stat_sdb_t * stat_new(void) { struct stat_sdb_t *sdb; sdb = (struct stat_sdb_t *)calloc(1, sizeof(struct stat_sdb_t)); if (!sdb) fatal("out of virtual memory"); sdb->stats = NULL; sdb->evaluator = eval_new(stat_eval_ident, sdb); return sdb; }
void main(void) { struct eval_state_t *es; /* set up test variables */ an_int.type = et_int; an_int.value.as_int = 1; a_uint.type = et_uint; a_uint.value.as_uint = 2; a_float.type = et_float; a_float.value.as_float = 3.0f; a_double.type = et_double; a_double.value.as_double = 4.0; a_symbol.type = et_symbol; a_symbol.value.as_symbol = "testing 1 2 3..."; /* instantiate an evaluator */ es = eval_new(my_eval_ident, NULL); while (1) { struct eval_value_t val; char expr_buf[1024]; fgets(expr_buf, 1024, stdin); /* chop */ if (expr_buf[strlen(expr_buf)-1] == '\n') expr_buf[strlen(expr_buf)-1] = '\0'; if (expr_buf[0] == '\0') exit(0); val = eval_expr(es, expr_buf, NULL); if (eval_error) fprintf(stdout, "eval error: %s\n", eval_err_str[eval_error]); else { fprintf(stdout, "%s == ", expr_buf); eval_print(stdout, val); fprintf(stdout, "\n"); } } }
/* print the value of stat variable STAT */ void stat_print_stat(struct stat_sdb_t *sdb, /* stat database */ struct stat_stat_t *stat,/* stat variable */ FILE *fd) /* output stream */ { struct eval_value_t val; switch (stat->sc) { case sc_int: fprintf(fd, "%-22s ", stat->name); myfprintf(fd, stat->format, *stat->variant.for_int.var); fprintf(fd, " # %s", stat->desc); break; case sc_uint: fprintf(fd, "%-22s ", stat->name); myfprintf(fd, stat->format, *stat->variant.for_uint.var); fprintf(fd, " # %s", stat->desc); break; #ifdef HOST_HAS_QWORD case sc_qword: { char buf[128]; fprintf(fd, "%-22s ", stat->name); mysprintf(buf, stat->format, *stat->variant.for_qword.var); fprintf(fd, "%s # %s", buf, stat->desc); } break; case sc_sqword: { char buf[128]; fprintf(fd, "%-22s ", stat->name); mysprintf(buf, stat->format, *stat->variant.for_sqword.var); fprintf(fd, "%s # %s", buf, stat->desc); } break; #endif /* HOST_HAS_QWORD */ case sc_float: fprintf(fd, "%-22s ", stat->name); myfprintf(fd, stat->format, (double)*stat->variant.for_float.var); fprintf(fd, " # %s", stat->desc); break; case sc_double: fprintf(fd, "%-22s ", stat->name); myfprintf(fd, stat->format, *stat->variant.for_double.var); fprintf(fd, " # %s", stat->desc); break; case sc_dist: print_dist(stat, fd); break; case sc_sdist: print_sdist(stat, fd); break; case sc_formula: { /* instantiate a new evaluator to avoid recursion problems */ struct eval_state_t *es = eval_new(stat_eval_ident, sdb); char *endp; fprintf(fd, "%-22s ", stat->name); val = eval_expr(es, stat->variant.for_formula.formula, &endp); if (eval_error != ERR_NOERR || *endp != '\0') fprintf(fd, "<error: %s>", eval_err_str[eval_error]); else myfprintf(fd, stat->format, eval_as_double(val)); fprintf(fd, " # %s", stat->desc); /* done with the evaluator */ eval_delete(es); } break; default: panic("bogus stat class"); } fprintf(fd, "\n"); }
/* evaluate a stat as an expression */ struct eval_value_t stat_eval_ident(struct eval_state_t *es)/* an expression evaluator */ { struct stat_sdb_t *sdb = es->user_ptr; struct stat_stat_t *stat; static struct eval_value_t err_value = { et_int, { 0 } }; struct eval_value_t val; /* locate the stat variable */ for (stat = sdb->stats; stat != NULL; stat = stat->next) { if (!strcmp(stat->name, es->tok_buf)) { /* found it! */ break; } } if (!stat) { /* could not find stat variable */ eval_error = ERR_UNDEFVAR; return err_value; } /* else, return the value of stat */ /* convert the stat variable value to a typed expression value */ switch (stat->sc) { case sc_int: val.type = et_int; val.value.as_int = *stat->variant.for_int.var; break; case sc_uint: val.type = et_uint; val.value.as_uint = *stat->variant.for_uint.var; break; #ifdef HOST_HAS_QWORD case sc_qword: /* FIXME: cast to double, eval package doesn't support long long's */ val.type = et_double; #ifdef _MSC_VER /* FIXME: MSC does not implement qword_t to dbl conversion */ val.value.as_double = (double)(sqword_t)*stat->variant.for_qword.var; #else /* !_MSC_VER */ val.value.as_double = (double)*stat->variant.for_qword.var; #endif /* _MSC_VER */ break; case sc_sqword: /* FIXME: cast to double, eval package doesn't support long long's */ val.type = et_double; val.value.as_double = (double)*stat->variant.for_sqword.var; break; #endif /* HOST_HAS_QWORD */ case sc_float: val.type = et_float; val.value.as_float = *stat->variant.for_float.var; break; case sc_double: val.type = et_double; val.value.as_double = *stat->variant.for_double.var; break; case sc_dist: case sc_sdist: fatal("stat distributions not allowed in formula expressions"); break; case sc_formula: { /* instantiate a new evaluator to avoid recursion problems */ struct eval_state_t *es = eval_new(stat_eval_ident, sdb); char *endp; val = eval_expr(es, stat->variant.for_formula.formula, &endp); if (eval_error != ERR_NOERR || *endp != '\0') { /* pass through eval_error */ val = err_value; } /* else, use value returned */ eval_delete(es); } break; default: panic("bogus stat class"); } return val; }
int init(simoutorder* in_simobj, int argc, char** argv, char **envp ) { in_simobj->sim_odb = opt_new(orphan_fn); opt_reg_flag(in_simobj->sim_odb, "-h", "print help message", &help_me, /* default */FALSE, /* !print */FALSE, NULL); opt_reg_flag(in_simobj->sim_odb, "-v", "verbose operation", &verbose, /* default */FALSE, /* !print */FALSE, NULL); #ifdef DEBUG opt_reg_flag(in_simobj->sim_odb, "-d", "enable debug message", &debugging, /* default */FALSE, /* !print */FALSE, NULL); #endif /* DEBUG */ opt_reg_flag(in_simobj->sim_odb, "-i", "start in Dlite debugger", &dlite_active, /* default */FALSE, /* !print */FALSE, NULL); opt_reg_int(in_simobj->sim_odb, "-seed", "random number generator seed (0 for timer seed)", &rand_seed, /* default */1, /* print */TRUE, NULL); opt_reg_flag(in_simobj->sim_odb, "-q", "initialize and terminate immediately", &init_quit, /* default */FALSE, /* !print */FALSE, NULL); opt_reg_string(in_simobj->sim_odb, "-chkpt", "restore EIO trace execution from <fname>", &sim_chkpt_fname, /* default */NULL, /* !print */FALSE, NULL); /* stdio redirection options */ opt_reg_string(in_simobj->sim_odb, "-redir:sim", "redirect simulator output to file (non-interactive only)", &sim_simout, /* default */NULL, /* !print */FALSE, NULL); opt_reg_string(in_simobj->sim_odb, "-redir:prog", "redirect simulated program output to file", &sim_progout, /* default */NULL, /* !print */FALSE, NULL); #ifndef _MSC_VER /* scheduling priority option */ opt_reg_int(in_simobj->sim_odb, "-nice", "simulator scheduling priority", &nice_priority, /* default */NICE_DEFAULT_VALUE, /* print */TRUE, NULL); #endif in_simobj->sim_reg_options(in_simobj->sim_odb); exec_index = -1; opt_process_options(in_simobj->sim_odb, argc, argv); /* redirect I/O? */ if (sim_simout != NULL) { /* send simulator non-interactive output (STDERR) to file SIM_SIMOUT */ fflush(stderr); if (!freopen(sim_simout, "w", stderr)) fatal("unable to redirect simulator output to file `%s'", sim_simout); } if (sim_progout != NULL) { /* redirect simulated program output to file SIM_PROGOUT */ sim_progfd = fopen(sim_progout, "w"); if (!sim_progfd) fatal("unable to redirect program output to file `%s'", sim_progout); } /* need at least two argv values to run */ if (argc < 2) { banner(stderr, argc, argv); usage(stderr, argc, argv, in_simobj->sim_odb); exit(1); } if (help_me) { /* print help message and exit */ usage(stderr, argc, argv, in_simobj->sim_odb); exit(1); } /* seed the random number generator */ if (rand_seed == 0) { /* seed with the timer value, true random */ mysrand(time((time_t *)NULL)); } else { /* seed with default or user-specified random number generator seed */ mysrand(rand_seed); } /* exec_index is set in orphan_fn() */ if (exec_index == -1) { /* executable was not found */ fprintf(stderr, "error: no executable specified\n"); usage(stderr, argc, argv, in_simobj->sim_odb); exit(1); } /* else, exec_index points to simulated program arguments */ /* check simulator-specific options */ in_simobj->sim_check_options(in_simobj->sim_odb, argc, argv); #ifndef _MSC_VER /* set simulator scheduling priority */ if (nice(0) < nice_priority) { if (nice(nice_priority - nice(0)) < 0) fatal("could not renice simulator process"); } #endif /* default architected value... */ sim_num_insn = 0; #ifdef BFD_LOADER /* initialize the bfd library */ bfd_init(); #endif /* BFD_LOADER */ /* initialize all simulation modules */ in_simobj->sim_init(); /* initialize architected state */ in_simobj->sim_load_prog(argv[exec_index], argc-exec_index, argv+exec_index, envp); /* register all simulator stats */ struct stat_sdb_t *sdb; in_simobj->sim_sdb = (struct stat_sdb_t *)calloc(1, sizeof(struct stat_sdb_t)); if (!in_simobj->sim_sdb) fatal("out of virtual memory"); in_simobj->sim_sdb->stats = NULL; in_simobj->sim_sdb->evaluator = eval_new(stat_eval_ident, in_simobj->sim_sdb); in_simobj->sim_reg_stats(in_simobj->sim_sdb); return 1; }