virtual_machine.hh 4.76 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
35
36
37
38
39

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

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

namespace vm
{
  
  class VirtualMachine:
    protected inst::ConstVisitor
  {
  public:
40
41
42
    VirtualMachine(std::istream& istr = std::cin,
		   std::ostream& ostr = std::cout):
      istr(istr), ostr(ostr),
43
      program(0), text_section(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
59
60
61
      this->program = &program;
      text_section = &(program.text_section ());
      memory.store(program.data_section());
      cpu.set_pc(text_section->get_offset(inst::Label("main")));
    }
62

63
  public:
64
    bool		step(bool trace = false)
65
66
67
68
69
70
71
    {
      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);
72
73

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

  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);
92
93
94
95
96
    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);
97
98
99
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

    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);

138
139
140
141
142
    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);

143
144
145
146
147
148
149
150
151
152
153
154
155
    virtual void	visit(const inst::Syscall& sycall);

  protected:
    void		begin_scope()
    {
      cpu.begin_scope();
    }
    void		end_scope()
    {
      cpu.end_scope();
    }

  protected:
156
    bool		halt;
157
158
159
    std::istream&	istr;
    std::ostream&	ostr;

160
161
162
  protected:
    Cpu			cpu;

163
164
165
    const inst::Program*	program;
    const inst::TextSection*	text_section;
    Memory		memory;
166
167
168
169
170
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH