📄 cmdmain.c
字号:
/* * Dinero IV * Written by Jan Edler and Mark D. Hill * * Copyright (C) 1997 NEC Research Institute, Inc. and Mark D. Hill. * All rights reserved. * Copyright (C) 1985, 1989 Mark D. Hill. All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its associated documentation for non-commercial purposes is hereby * granted (for commercial purposes see below), provided that the above * copyright notice appears in all copies, derivative works or modified * versions of the software and any portions thereof, and that both the * copyright notice and this permission notice appear in the documentation. * NEC Research Institute Inc. and Mark D. Hill shall be given a copy of * any such derivative work or modified version of the software and NEC * Research Institute Inc. and any of its affiliated companies (collectively * referred to as NECI) and Mark D. Hill shall be granted permission to use, * copy, modify, and distribute the software for internal use and research. * The name of NEC Research Institute Inc. and its affiliated companies * shall not be used in advertising or publicity related to the distribution * of the software, without the prior written consent of NECI. All copies, * derivative works, or modified versions of the software shall be exported * or reexported in accordance with applicable laws and regulations relating * to export control. This software is experimental. NECI and Mark D. Hill * make no representations regarding the suitability of this software for * any purpose and neither NECI nor Mark D. Hill will support the software. * * Use of this software for commercial purposes is also possible, but only * if, in addition to the above requirements for non-commercial use, written * permission for such use is obtained by the commercial user from NECI or * Mark D. Hill prior to the fabrication and distribution of the software. * * THE SOFTWARE IS PROVIDED AS IS. NECI AND MARK D. HILL DO NOT MAKE * ANY WARRANTEES EITHER EXPRESS OR IMPLIED WITH REGARD TO THE SOFTWARE. * NECI AND MARK D. HILL ALSO DISCLAIM ANY WARRANTY THAT THE SOFTWARE IS * FREE OF INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS OF OTHERS. * NO OTHER LICENSE EXPRESS OR IMPLIED IS HEREBY GRANTED. NECI AND MARK * D. HILL SHALL NOT BE LIABLE FOR ANY DAMAGES, INCLUDING GENERAL, SPECIAL, * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY * TO USE THE SOFTWARE. * * * This file contains the startup, overall driving code, and * miscellaneous stuff needed for Dinero IV when running as a * self-contained simulator. * All the really hard stuff is in the callable Dinero IV subroutines, * which may be used independently of this program. * * $Header: /home/edler/dinero/d4/RCS/cmdmain.c,v 1.14 1998/02/06 20:58:11 edler Exp $ */#include <stddef.h>#include <stdlib.h>#include <stdarg.h>#include <unistd.h>#include <limits.h>#include <stdio.h>#include <errno.h>#include <math.h>#include <ctype.h>#include <string.h>#include <sys/stat.h>#include "d4.h"#include "cmdd4.h"#include "cmdargs.h"#include "tracein.h"/* some global variables */char *progname = "dineroIV"; /* for error messages */int optstringmax; /* for help_* functions */d4cache *levcache[3][MAX_LEV]; /* to locate cache by level and type */d4cache *mem; /* which cache represents simulated memory? */#if !D4CUSTOMconst char **cust_argv; /* for args to pass to custom version */int cust_argc = 1; /* how many args for custom version */char *customname; /* for -custom, name of executable */#endif/* private prototypes for this file */extern int do1arg (const char *, const char *);extern void doargs (int, char **);extern void summarize_caches (d4cache *, d4cache *);extern void dostats (void);extern void do1stats (d4cache *);extern d4memref next_trace_item (void);#if !D4CUSTOMextern void customize_caches (void);#endif/* * Generic functions for handling command line arguments. * Most argument-specific knowledge is in cmdargs.c. *//* * Internal function to handle one command line option. * Return number of command line args consumed. * (Doesn't really support more than 1 or 2 consumed.) */intdo1arg (const char *opt, const char *arg){ struct arglist *adesc; for (adesc = args; adesc->optstring != NULL; adesc++) { int eaten = adesc->match (opt, adesc); if (eaten > 0) { if (eaten > 1 && (arg == NULL || *arg == '-')) shorthelp ("\"%s\" option requires additional argument\n", opt); adesc->valf (opt, arg, adesc);#if !D4CUSTOM if (adesc->customf == NULL) { cust_argv[cust_argc++] = opt; if (eaten>1) cust_argv[cust_argc++] = arg; }#endif return eaten; } }#if !D4CUSTOM /* does it look like a possible Dinero III option? */ if (opt[0]=='-' && strchr ("uidbSarfpPwAQzZ", opt[1]) != NULL) shorthelp ("\"%s\" option not recognized for Dinero IV;\n" "try \"%s -dineroIII\" for Dinero III --> IV option correspondence.\n", opt, progname);#endif shorthelp ("\"%s\" option not recognized.\n", opt); return 0; /* can't really get here, but some compilers get upset if we don't have a return value */}/* * Process all the command line args */voiddoargs (int argc, char **argv){ struct arglist *adesc; char **v = argv+1; int x;#if !D4CUSTOM cust_argv = malloc ((argc+1) * sizeof(argv[0])); if (cust_argv == NULL) die ("no memory to copy args for possible -custom\n");#endif for (adesc = args; adesc->optstring != NULL; adesc++) if (optstringmax < (int)strlen(adesc->optstring) + adesc->pad) optstringmax = strlen(adesc->optstring) + adesc->pad; while (argc > 1) { const char *opt = v[0]; const char *arg = (argc>1) ? v[1] : NULL; x = do1arg (opt, arg); v += x; argc -= x; } verify_options();}/* * Get the level and idu portion of a -ln-idu prefix. * The return value is a pointer to the next character after the prefix, * or NULL if the prefix is not recognized. */char *level_idu (const char *opt, int *levelp, int *idup){ int level; char *nextc; if (*opt++ != '-' || *opt++ != 'l') return NULL; /* no initial -l */ if (*opt == '-' || *opt == '+') return NULL; /* we don't accept a sign here */ level = strtol (opt, &nextc, 10); if (nextc == opt) return NULL; /* no digits */ if (level <= 0 || level > MAX_LEV) return NULL; /* level out of range */ if (*nextc++ != '-') /* missing - after level */ return NULL; switch (*nextc++) { default: return NULL; /* bad idu value */ case 'u': *idup = 0; break; case 'i': *idup = 1; break; case 'd': *idup = 2; break; } *levelp = level - 1; if (level > maxlevel) maxlevel = level; return nextc;}/* * Helper for scaled unsigned integer arguments */unsigned intargscale_uint (const char *arg, unsigned int *var){ char *nextc; unsigned long x; errno = 0; if (!isdigit ((int)*arg)) return 0; /* no good: doesn't start with a number */ x = strtoul (arg, &nextc, 10); if (nextc == arg) return 0; /* no good: no chars consumed */ if (x == ULONG_MAX && errno == ERANGE) return 0; /* no good: overflow */ switch (nextc[0]) { default: return 0; /* no good; some other trailing char */ case 0: break; /* ok: no scale factor */ case 'k': case 'K': if ((x>>(sizeof(x)*CHAR_BIT-10)) != 0) return 0; /* no good: overflow */ x <<= 10; break; case 'm': case 'M': if ((x>>(sizeof(x)*CHAR_BIT-20)) != 0) return 0; /* no good: overflow */ x <<= 20; break; case 'g': case 'G': if ((x>>(sizeof(x)*CHAR_BIT-30)) != 0) return 0; /* no good: overflow */ x <<= 30; break; } if (nextc[0] != 0 && nextc[1] != 0) return 0; /* no good: trailing junk */ *var = x; return 1;}/* * Helper for scaled unsigned integer arguments, but using double for extra range * We go through various difficulties just to allow use of strtod * while still preventing non-integer use. * XXX We should probably use autoconf to help us use long or long long * or whatever, if sufficient. */doubleargscale_uintd (const char *arg, double *var){ char *nextc; double x, ipart; errno = 0; x = strtod (arg, &nextc); if (nextc == arg) return 0; /* no good: no chars consumed */ if (x == HUGE_VAL && errno == ERANGE) return 0; /* no good: overflow */ /* make sure value is an integer */ switch (nextc[0]) { default: return 0; /* no good; some other trailing char */ case 0: break; /* ok: no scale factor */ case 'k': case 'K': x *= (1<<10); break; case 'm': case 'M': x *= (1<<20); break; case 'g': case 'G': x *= (1<<30); break; } if (nextc[0] != 0 && nextc[1] != 0) return 0; /* no good: trailing junk */ /* make sure number was unsigned integer; no decimal pt, exponent, or sign */ for (; arg < nextc; arg++) if (!isdigit ((int)*arg)) return 0; /* no good: non-digits */ /* make sure we don't have a fractional part due to scaling */ if (modf (x, &ipart) != 0) return 0; /* no good: fraction != 0 */ *var = x; return 1;}/* * Recognize an option with no args */intmatch_0arg (const char *opt, const struct arglist *adesc){ return strcmp (opt, adesc->optstring) == 0;}/* * Recognize an option with no args and the -ln-idu prefix */intpmatch_0arg (const char *opt, const struct arglist *adesc){ int level; int idu; const char *nextc = level_idu (opt, &level, &idu); if (nextc == NULL) return 0; /* not recognized */ return 1 * (strcmp (nextc, adesc->optstring) == 0);}/* * Recognize an option with 1 arg */intmatch_1arg (const char *opt, const struct arglist *adesc){ return 2 * (strcmp (opt, adesc->optstring) == 0);}/* * Recognize an option with 1 arg and the -ln-idu prefix */intpmatch_1arg (const char *opt, const struct arglist *adesc){ int level; int idu; const char *nextc = level_idu (opt, &level, &idu); if (nextc == NULL) return 0; /* not recognized */ return 2 * (strcmp (nextc, adesc->optstring) == 0);}#if D4CUSTOM/* * Recognize no option; used as a bogus match function for options * which have been customized. */intmatch_bogus (const char *opt, const struct arglist *adesc){ return 0;}#endif/* * Produce help message in response to -help */voidval_help (const char *opt, const char *arg, const struct arglist *adesc){ int i; printf ("Usage: %s [options]\nValid options:\n", progname); for (i = 0; i < nargs; i++) { if (args[i].optstring != NULL && args[i].help != NULL) { putchar (' '); args[i].help (&args[i]); if (args[i].defstr != NULL) printf (" (default %s)", args[i].defstr); putchar ('\n'); } } printf ("Key:\n"); printf (" U unsigned decimal integer\n"); printf (" S like U but with optional [kKmMgG] scaling suffix\n"); printf (" P like S but must be a power of 2\n"); printf (" C single character\n"); printf (" A hexadecimal address\n"); printf (" F string\n"); printf (" N cache level (1 <= N <= %d)\n", MAX_LEV); printf (" T cache type (u=unified, i=instruction, d=data)\n"); exit(0);}/* * Produce help message in response to -copyright */voidval_helpcr (const char *opt, const char *arg, const struct arglist *adesc){ printf ("Dinero IV is copyrighted software\n"); printf ("\n"); printf ("Copyright (C) 1997 NEC Research Institute, Inc. and Mark D. Hill.\n"); printf ("All rights reserved.\n"); printf ("Copyright (C) 1985, 1989 Mark D. Hill. All rights reserved.\n"); printf ("\n"); printf ("Permission to use, copy, modify, and distribute this software and\n"); printf ("its associated documentation for non-commercial purposes is hereby\n"); printf ("granted (for commercial purposes see below), provided that the above\n"); printf ("copyright notice appears in all copies, derivative works or modified\n"); printf ("versions of the software and any portions thereof, and that both the\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -