virtual_machine.hh 2 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
    VirtualMachine(bool check_callee_save_p,
Benoit Perrot's avatar
Benoit Perrot committed
39
		   bool trace_exec_p,
Benoit Perrot's avatar
Benoit Perrot committed
40
		   std::istream& istr = std::cin,
41
		   std::ostream& ostr = std::cout):
Benoit Perrot's avatar
Benoit Perrot committed
42
43
      cpu(memory, istr, ostr, check_callee_save_p, trace_exec_p),
      program(0)
44
45
46
47
48
    {
    }


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

63
  public:
Benoit Perrot's avatar
Benoit Perrot committed
64
    void		execute()
65
66
67
    {
      precondition(program);
     
Benoit Perrot's avatar
Benoit Perrot committed
68
69
      while (!cpu.get_halt())
	cpu.step();
70
71
72
73
74
    }

  protected:
    Cpu			cpu;

75
76
    const inst::Program*	program;
    Memory		memory;
77
78
79
80
81
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH