virtual_machine.hh 4.58 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
22
#ifndef VM_VIRTUAL_MACHINE_HH
# define VM_VIRTUAL_MACHINE_HH

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

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

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

namespace vm
{
  
  class VirtualMachine:
    protected inst::ConstVisitor
  {
  public:
38
39
    VirtualMachine():
      program(0), text_section(0)
40
41
42
43
44
    {
    }


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

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  public:
    void		step(bool trace = false)
    {
      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);
    }
    void		execute(bool trace = false)
    {
      precondition(program);
     
      halt = false;
      while (!halt)
	step(trace);
76
77
78
79
80
81
82
83
84
85
86
    }

  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);
87
88
89
90
91
    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);
92
93
94
95
96
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

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

133
134
135
136
137
    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);

138
139
140
141
142
143
144
145
146
147
148
149
150
    virtual void	visit(const inst::Syscall& sycall);

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

  protected:
151
    bool		halt;
152
153
154
  protected:
    Cpu			cpu;

155
156
157
    const inst::Program*	program;
    const inst::TextSection*	text_section;
    Memory		memory;
158
159
160
161
162
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH