virtual_machine.hh 5.53 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
# include <iostream>
Benoit Perrot's avatar
Benoit Perrot committed
23
# include <stack>
24

25
# include "misc/contract.hh"
26
# include "common.hh"
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# include "inst/program.hh"
# include "inst/visitor.hh"

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

namespace vm
{
  
  class VirtualMachine:
    protected inst::ConstVisitor
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
41
42
    VirtualMachine(bool check_callee_save_p,
		   std::istream& istr = std::cin,
43
		   std::ostream& ostr = std::cout):
Benoit Perrot's avatar
Benoit Perrot committed
44
      check_callee_save_p(check_callee_save_p),
45
      istr(istr), ostr(ostr),
46
      program(0), text_section(0)
47
48
49
50
51
    {
    }


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

66
  public:
67
    bool		step(bool trace = false)
68
69
70
71
72
73
74
    {
      const inst::Inst&	ri = (*text_section)[cpu.get_pc() / 4];
      cpu.set_pc(cpu.get_pc() + 4);
      
      if (trace)
	std::cout << ri << std::endl;
      ri.accept(*this);
75
76

      return halt;
77
78
79
80
81
82
    }
    void		execute(bool trace = false)
    {
      precondition(program);
     
      halt = false;
83
      while (!step(trace));
84
85
86
87
88
89
90
91
92
93
94
    }

  protected:
    virtual void	visit(const inst::Add& add);
    virtual void	visit(const inst::Addi& addi);
    virtual void	visit(const inst::Addu& addu);
    virtual void	visit(const inst::Addiu& addiu);
    virtual void	visit(const inst::Sub& sub);
    virtual void	visit(const inst::Subu& subu);

    virtual void	visit(const inst::Sll& sll);
95
96
97
98
99
    virtual void	visit(const inst::Sllv& slv);
    virtual void	visit(const inst::Sra& sra);
    virtual void	visit(const inst::Srav& srav);
    virtual void	visit(const inst::Srl& srl);
    virtual void	visit(const inst::Srlv& srlv);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

    virtual void	visit(const inst::Mul& mul);
    virtual void	visit(const inst::Div& div);
    virtual void	visit(const inst::Divu& divu);

    virtual void	visit(const inst::Lb& lb);
    virtual void	visit(const inst::Lbu& Lbu);
    virtual void	visit(const inst::Lw& lw);
    virtual void	visit(const inst::Li& li);
    virtual void	visit(const inst::Sb& sb);
    virtual void	visit(const inst::Sw& sw);

    virtual void	visit(const inst::And& _and);
    virtual void	visit(const inst::Andi& andi);
    virtual void	visit(const inst::Or& _or);
    virtual void	visit(const inst::Ori& ori);
    virtual void	visit(const inst::Nor& nor);
    virtual void	visit(const inst::Xor& _xor);
    virtual void	visit(const inst::Xori& xori);

    virtual void	visit(const inst::Slt& slt);
    virtual void	visit(const inst::Slti& slti);
    virtual void	visit(const inst::Sltu&);
    virtual void	visit(const inst::Sltiu& sltiu);

    virtual void	visit(const inst::Jmp& jmp);
    virtual void	visit(const inst::Jr& jr);
    virtual void	visit(const inst::Jal& jal);
    virtual void	visit(const inst::Jalr& jalr);

    virtual void	visit(const inst::Beq& beq);
    virtual void	visit(const inst::Bne& bne);

    virtual void	visit(const inst::Bgez& bgez);
    virtual void	visit(const inst::Bgezal& bgezal);
    virtual void	visit(const inst::Bgtz& bgtz);

    virtual void	visit(const inst::Blez& blez);
    virtual void	visit(const inst::Bltz& bltz);
    virtual void	visit(const inst::Bltzal& bltzal);

141
142
143
144
145
    virtual void	visit(const inst::Mfhi& mfhi);
    virtual void	visit(const inst::Mflo& mflo);
    virtual void	visit(const inst::Mthi& mthi);
    virtual void	visit(const inst::Mtlo& mtlo);

146
147
148
    virtual void	visit(const inst::Syscall& sycall);

  protected:
Benoit Perrot's avatar
Benoit Perrot committed
149
    void		call()
150
    {
Benoit Perrot's avatar
Benoit Perrot committed
151
152
153
154
155
      if (check_callee_save_p)
	for (int i = Cpu::s0; i <= Cpu::s7; ++i)
	  cpu.set_unlimited(-i, cpu.get_register((Cpu::kind_t) i));
      call_stack.push(cpu.get_pc ());

156
157
      cpu.begin_scope();
    }
Benoit Perrot's avatar
Benoit Perrot committed
158
    void		ret()
159
160
    {
      cpu.end_scope();
Benoit Perrot's avatar
Benoit Perrot committed
161
162
163
164
165
166
167
168
169
170
171
172
173

      if (check_callee_save_p)
	for (int i = Cpu::s0; i <= Cpu::s7; ++i)
	  if (cpu.get_unlimited(-i) != cpu.get_register((Cpu::kind_t) i))
	    {
	      std::cerr
		<< "Warning: callee save register `$s" << i - Cpu::s0
		<< "' was not preserved across last call to 0x"
		<< std::hex << call_stack.top()	<< std::dec << std::endl;
	      cpu.set_register((Cpu::kind_t) i, cpu.get_unlimited(-i));
	      exit_set(exit_runtime);
	    }
      call_stack.pop();
174
    }
Benoit Perrot's avatar
Benoit Perrot committed
175
    std::stack<register_t>	call_stack;
176
177

  protected:
178
    bool		halt;
Benoit Perrot's avatar
Benoit Perrot committed
179
180

    bool		check_callee_save_p;
181
182
183
    std::istream&	istr;
    std::ostream&	ostr;

184
185
186
  protected:
    Cpu			cpu;

187
188
189
    const inst::Program*	program;
    const inst::TextSection*	text_section;
    Memory		memory;
190
191
192
193
194
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH