virtual_machine.hh 2.26 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// This file is part of Mipsy, a tiny MIPS simulator
// Copyright (C) 2003 Benoit Perrot <benoit@lrde.epita.fr>
//
// Mipsy is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Mipsy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
19
20
21
#ifndef VM_VIRTUAL_MACHINE_HH
# define VM_VIRTUAL_MACHINE_HH

22
23
# include <iostream>

24
# include "misc/contract.hh"
25
# include "common.hh"
26
27
28
29
30
31
32
33
34

# include "inst/program.hh"

# include "vm/cpu.hh"
# include "vm/memory.hh"

namespace vm
{
  
35
  class VirtualMachine
36
37
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
38
39
    VirtualMachine(bool check_callee_save_p,
		   std::istream& istr = std::cin,
40
		   std::ostream& ostr = std::cout):
41
      cpu(memory, istr, ostr, check_callee_save_p),
42
      program(0), text_section(0)
43
44
45
46
47
    {
    }


  public:
48
    void		load_program(const inst::Program& program)
49
    {
50
      if (! program.text_section ().has_label(inst::Label("main")))
Benoit Perrot's avatar
Benoit Perrot committed
51
52
	{
	  std::cerr << "No `main' label in assembly file." << std::endl;
53
	  exit_set(exit_runtime);
Benoit Perrot's avatar
Benoit Perrot committed
54
55
	  return;
	}
56
57
58
59
60
      this->program = &program;
      text_section = &(program.text_section ());
      memory.store(program.data_section());
      cpu.set_pc(text_section->get_offset(inst::Label("main")));
    }
61

62
  public:
63
    bool		step(bool trace = false)
64
65
66
67
68
69
    {
      const inst::Inst&	ri = (*text_section)[cpu.get_pc() / 4];
      cpu.set_pc(cpu.get_pc() + 4);
      
      if (trace)
	std::cout << ri << std::endl;
70
      ri.accept(cpu);
71

72
      return cpu.get_halt();
73
74
75
76
77
    }
    void		execute(bool trace = false)
    {
      precondition(program);
     
78
      while (!step(trace));
79
80
81
82
83
    }

  protected:
    Cpu			cpu;

84
85
86
    const inst::Program*	program;
    const inst::TextSection*	text_section;
    Memory		memory;
87
88
89
90
91
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH