⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spim.c

📁 Spim软件的一些源码。其中有Xspim的
💻 C
📖 第 1 页 / 共 2 页
字号:
/* SPIM S20 MIPS simulator.   Terminal interface for SPIM simulator.   Copyright (C) 1990-2004 by James Larus (larus@cs.wisc.edu).   ALL RIGHTS RESERVED.   Changes for DOS and Windows versions by David A. Carley (dac@cs.wisc.edu)   SPIM is distributed under the following conditions:     You may make copies of SPIM for your own use and modify those copies.     All copies of SPIM must retain my name and copyright notice.     You may not sell SPIM or distributed SPIM in conjunction with a     commerical product or service without the expressed written consent of     James Larus.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   PURPOSE. *//* $Header: /Software/SPIM/src/spim.c 36    10/18/04 9:31p Larus $*/#ifndef WIN32#include <unistd.h>#endif#include <stdio.h>#include <ctype.h>#include <setjmp.h>#include <signal.h>#include <arpa/inet.h>#ifdef RS/* This is problem on HP Snakes, which define RS in syscall.h */#undef RS#endif#include <sys/types.h>#include <sys/select.h>#ifdef _AIX#ifndef NBBY#define NBBY 8#endif#endif#ifndef WIN32#include <sys/time.h>#ifdef USE_TERMIOS#include <termios.h>#else#include <sys/ioctl.h>#include <sgtty.h>#endif#endif#include <stdarg.h>#include "spim.h"#include "string-stream.h"#include "spim-utils.h"#include "inst.h"#include "reg.h"#include "mem.h"#include "parser.h"#include "sym-tbl.h"#include "scanner.h"#include "y.tab.h"/* Internal functions: */static void console_to_program ();static void console_to_spim ();static void flush_to_newline ();static int get_opt_int ();static int parse_spim_command (FILE *file, int redo);static int print_reg (int reg_no);static int print_fp_reg (int reg_no);static int print_reg_from_string (char *reg);static void print_all_regs (int hex_flag);static int read_assembly_command ();static int str_prefix (char *s1, char *s2, int min_match);static void top_level ();static int read_token ();/* Exported Variables: *//* Not local, but not export so all files don't need setjmp.h */jmp_buf spim_top_level_env;	/* For ^C */int bare_machine;		/* Non-Zero => simulate bare machine */int delayed_branches;		/* Non-Zero => simulate delayed branches */int delayed_loads;		/* Non-Zero => simulate delayed loads */int accept_pseudo_insts;	/* Non-Zero => parse pseudo instructions  */int quiet;			/* Non-Zero => no warning messages */char *exception_file_name = DEFAULT_EXCEPTION_HANDLER;port message_out, console_out, console_in;int mapped_io;			/* Non-zero => activate memory-mapped IO */int pipe_out;int spim_return_value;		/* Value returned when spim exits *//* Local variables: *//* Non-zero => load standard exception handler */static int load_exception_handler = 1;static int console_state_saved;#ifdef USE_TERMIOSstatic struct termios saved_console_state;#elsestatic struct sgttyb saved_console_state;#endifstatic int program_argc;static char** program_argv;intmain (int argc, char **argv){  int i;  int assembly_file_loaded = 0;  int print_usage_msg = 0;  console_out.f = stdout;  message_out.f = stdout;  bare_machine = 0;  delayed_branches = 0;  delayed_loads = 0;  accept_pseudo_insts = 1;  quiet = 0;  spim_return_value = 0;  /* Input comes directly (not through stdio): */  console_in.i = 0;  mapped_io = 0;  write_startup_message ();  if (getenv ("SPIM_EXCEPTION_HANDLER") != NULL)    exception_file_name = getenv ("SPIM_EXCEPTION_HANDLER");  for (i = 1; i < argc; i++)    {#ifdef WIN32      if (argv [i][0] == '/') { argv [i][0] = '-'; }#endif      if (streq (argv [i], "-asm")	  || streq (argv [i], "-a"))	{	  bare_machine = 0;	  delayed_branches = 0;	  delayed_loads = 0;	}      else if (streq (argv [i], "-bare")	       || streq (argv [i], "-b"))	{	  bare_machine = 1;	  delayed_branches = 1;	  delayed_loads = 1;	  quiet = 1;	}      else if (streq (argv [i], "-delayed_branches")	       || streq (argv [i], "-db"))	{	  delayed_branches = 1;	}      else if (streq (argv [i], "-delayed_loads")	       || streq (argv [i], "-dl"))	{	  delayed_loads = 1;	}      else if (streq (argv [i], "-exception")	       || streq (argv [i], "-e"))	{ load_exception_handler = 1; }      else if (streq (argv [i], "-noexception")	       || streq (argv [i], "-ne"))	{ load_exception_handler = 0; }      else if (streq (argv [i], "-exception_file")	       || streq (argv [i], "-ef"))	{	  exception_file_name = argv[++i];	  load_exception_handler = 1;	}      else if (streq (argv [i], "-mapped_io")	       || streq (argv [i], "-mio"))	{ mapped_io = 1; }      else if (streq (argv [i], "-nomapped_io")	       || streq (argv [i], "-nmio"))	{ mapped_io = 0; }      else if (streq (argv [i], "-pseudo")	       || streq (argv [i], "-p"))	{ accept_pseudo_insts = 1; }      else if (streq (argv [i], "-nopseudo")	       || streq (argv [i], "-np"))	{ accept_pseudo_insts = 0; }      else if (streq (argv [i], "-quiet")	       || streq (argv [i], "-q"))	{ quiet = 1; }      else if (streq (argv [i], "-noquiet")	       || streq (argv [i], "-nq"))	{ quiet = 0; }      else if (streq (argv [i], "-trap")	       || streq (argv [i], "-t"))	{ load_exception_handler = 1; }      else if (streq (argv [i], "-notrap")	       || streq (argv [i], "-nt"))	{ load_exception_handler = 0; }      else if (streq (argv [i], "-trap_file")	       || streq (argv [i], "-tf"))	{	  exception_file_name = argv[++i];	  load_exception_handler = 1;	}      else if (streq (argv [i], "-stext")	       || streq (argv [i], "-st"))	{ initial_text_size = atoi (argv[++i]); }      else if (streq (argv [i], "-sdata")	       || streq (argv [i], "-sd"))	{ initial_data_size = atoi (argv[++i]); }      else if (streq (argv [i], "-ldata")	       || streq (argv [i], "-ld"))	{ initial_data_limit = atoi (argv[++i]); }      else if (streq (argv [i], "-sstack")	       || streq (argv [i], "-ss"))	{ initial_stack_size = atoi (argv[++i]); }      else if (streq (argv [i], "-lstack")	       || streq (argv [i], "-ls"))	{ initial_stack_limit = atoi (argv[++i]); }      else if (streq (argv [i], "-sktext")	       || streq (argv [i], "-skt"))	{ initial_k_text_size = atoi (argv[++i]); }      else if (streq (argv [i], "-skdata")	       || streq (argv [i], "-skd"))	{ initial_k_data_size = atoi (argv[++i]); }      else if (streq (argv [i], "-lkdata")	       || streq (argv [i], "-lkd"))	{ initial_k_data_limit = atoi (argv[++i]); }      else if ((streq (argv [i], "-file")		|| streq (argv [i], "-f"))	       && (i + 1 < argc))	{	  program_argc = argc - (i + 1);	  program_argv = &argv[i + 1]; /* Everything following is argv */	  if (!assembly_file_loaded)	    {	      initialize_world (load_exception_handler ? exception_file_name : NULL);	    }	  assembly_file_loaded |= !read_assembly_file (argv[++i]);	  break;	}      else if (argv [i][0] != '-')	{	  /* Assume this argument is a file name and everything following is	     arguments to program */	  program_argc = argc - i;	  program_argv = &argv[i]; /* Everything following is argv */	  if (!assembly_file_loaded)	    {	      initialize_world (load_exception_handler ? exception_file_name : NULL);	    }	  assembly_file_loaded |= !read_assembly_file (argv[i]);	  break;	}      else	{	  error ("\nUnknown argument: %s (ignored)\n", argv[i]);	  print_usage_msg = 1;	}    }  if (print_usage_msg)    {      error ("Usage: spim\n\	-bare			Bare machine (no pseudo-ops, delayed branches and loads)\n\	-asm			Extended machine (pseudo-ops, no delayed branches and loads) (default)\n\	-delayed_branches	Execute delayed branches\n\	-delayed_loads		Execute delayed loads\n\	-exception		Load exception handler (default)\n\	-noexception		Do not load exception handler\n\	-exception_file <file>	Specify exception handler in place of default\n\	-quiet			Do not print warnings\n\	-noquiet		Print warnings (default)\n\	-mapped_io		Enable memory-mapped IO\n\	-nomapped_io		Do not enable memory-mapped IO (default)\n\	-file <file> <args>	Assembly code file and arguments to program\n");    }  if (!assembly_file_loaded)    {      initialize_world (load_exception_handler ? exception_file_name : NULL);      top_level ();    }  else /* assembly_file_loaded */    {      console_to_program ();      initialize_run_stack (program_argc, program_argv);      if (!setjmp (spim_top_level_env))	{	  char *undefs = undefined_symbol_string ();	  if (undefs != NULL)	    {	      write_output (message_out, "The following symbols are undefined:\n");	      write_output (message_out, undefs);	      write_output (message_out, "\n");	      free (undefs);	    }	  run_program (find_symbol_address (DEFAULT_RUN_LOCATION),		       DEFAULT_RUN_STEPS, 0, 0);	}      console_to_spim ();    }  spim_return_value = 0;  return (spim_return_value);}/* Top-level read-eval-print loop for SPIM. */static voidtop_level (){  int redo = 0;			/* Non-zero means reexecute last command */  (void)signal (SIGINT, control_c_seen);  while (1)    {      if (!redo)	write_output (message_out, "(spim) ");      if (!setjmp (spim_top_level_env))	redo = parse_spim_command (stdin, redo);      else	redo = 0;      fflush (stdout);      fflush (stderr);    }}voidcontrol_c_seen (int arg){  console_to_spim ();  write_output (message_out, "\nExecution interrupted\n");  longjmp (spim_top_level_env, 1);}/* SPIM commands */enum {  UNKNOWN_CMD = 0,  EXIT_CMD,  READ_CMD,  RUN_CMD,  STEP_CMD,  PRINT_CMD,  PRINT_SYM_CMD,  PRINT_ALL_REGS_CMD,  REINITIALIZE_CMD,  ASM_CMD,  REDO_CMD,  NOP_CMD,  HELP_CMD,  CONTINUE_CMD,  SET_BKPT_CMD,  DELETE_BKPT_CMD,  LIST_BKPT_CMD,  DUMPNATIVE_TEXT_CMD,  DUMP_TEXT_CMD};/* Parse a SPIM command from the FILE and execute it.  If REDO is non-zero,   don't read a new command; just rexecute the previous one.   Return non-zero if the command was to redo the previous command. */static intparse_spim_command (FILE *file, int redo){  static int prev_cmd = NOP_CMD; /* Default redo */  static int prev_token;  int cmd;  initialize_scanner (file);  initialize_parser ("<standard input>");  switch (cmd = (redo ? prev_cmd : read_assembly_command ()))    {    case EXIT_CMD:      console_to_spim ();      exit (0);    case READ_CMD:      {	int token = (redo ? prev_token : read_token ());	if (!redo) flush_to_newline ();	if (token == Y_STR)	  {	    read_assembly_file ((char *) yylval.p);	    initialize_scanner (file); /* Reinitialize! */	  }	else	  error ("Must supply a filename to read\n");	prev_cmd = READ_CMD;	return (0);      }    case RUN_CMD:      {	static mem_addr addr;	addr = (redo ? addr : get_opt_int ());	if (addr == 0)	  addr = starting_address ();	initialize_run_stack (program_argc, program_argv);	console_to_program ();	if (addr != 0)	{	  char *undefs = undefined_symbol_string ();	  if (undefs != NULL)	    {	      write_output (message_out, "The following symbols are undefined:\n");	      write_output (message_out, undefs);	      write_output (message_out, "\n");	      free (undefs);	    }	  if (run_program (addr, DEFAULT_RUN_STEPS, 0, 0))	    write_output (message_out, "Breakpoint encountered at 0x%08x\n",			  PC);	}	console_to_spim ();	prev_cmd = RUN_CMD;	return (0);      }    case CONTINUE_CMD:      {	if (PC != 0)	  {	    console_to_program ();	    if (run_program (PC, DEFAULT_RUN_STEPS, 0, 1))	      write_output (message_out, "Breakpoint encountered at 0x%08x\n",			    PC);	    console_to_spim ();	  }	prev_cmd = CONTINUE_CMD;	return (0);      }    case STEP_CMD:      {	static int steps;	mem_addr addr;	steps = (redo ? steps : get_opt_int ());	addr = starting_address ();	if (steps == 0)	  steps = 1;	if (addr != 0)	  {	    console_to_program ();	    if (run_program (addr, steps, 1, 1))	      write_output (message_out, "Breakpoint encountered at 0x%08x\n",			    PC);	    console_to_spim ();	  }	prev_cmd = STEP_CMD;	return (0);      }    case PRINT_CMD:      {	int token = (redo ? prev_token : read_token ());	static int loc;	if (token == Y_REG)	  {	    if (redo) loc += 1;	    else loc = yylval.i;	    print_reg (loc);	  }	else if (token == Y_FP_REG)	  {	    if (redo) loc += 2;	    else loc = yylval.i;	    print_fp_reg (loc);	  }	else if (token == Y_INT)	  {	    if (redo) loc += 4;	    else loc = yylval.i;	    print_mem (loc);	  }	else if (token == Y_ID)	  {	    if (!print_reg_from_string ((char *) yylval.p))	      {		if (redo) loc += 4;		else loc = find_symbol_address ((char *) yylval.p);		if (loc != 0)		  print_mem (loc);		else		  error ("Unknown label: %s\n", yylval.p);	      }	  }	else	  error ("Print what?\n");	if (!redo) flush_to_newline ();	prev_cmd = PRINT_CMD;	prev_token = token;	return (0);      }    case PRINT_SYM_CMD:      print_symbols ();      if (!redo) flush_to_newline ();      prev_cmd = NOP_CMD;      return (0);    case PRINT_ALL_REGS_CMD:      {	int hex_flag = 0;	int token = (redo ? prev_token : read_token ());	if (token == Y_ID && streq(yylval.p, "hex"))	  hex_flag = 1;	print_all_regs (hex_flag);	if (!redo) flush_to_newline ();	prev_cmd = NOP_CMD;	return (0);      }    case REINITIALIZE_CMD:      flush_to_newline ();      initialize_world (load_exception_handler ? exception_file_name : NULL);      write_startup_message ();      prev_cmd = NOP_CMD;      return (0);    case ASM_CMD:      yyparse ();      prev_cmd = ASM_CMD;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -