program_solver.hh 5.72 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef INST_PROGRAM_SOLVER_HH
# define INST_PROGRAM_SOLVER_HH

// FIXME: lack of compatibility
# include <stdint.h>

# include "common.hh"

# include "inst/exp-visitor.hh"
# include "inst/exp.hh"
# include "inst/visitor.hh"
# include "inst/all.hh"
# include "inst/program.hh"

namespace inst
{

  class ProgramSolver:
    protected Visitor, 
    protected ExpVisitor
  {
  public:
    virtual ~ProgramSolver()
    {
    }

  public:
    void	solve(Program& program)
    {
      _program = &program;

      int i;
      for (i = 0, _pc = 4; i < program.text_section().size(); ++i, _pc += 4)
	_program->text_section()[i].accept(*this);
    }
  protected:
    int			_pc;

  protected:
    void		resolve_exp(Exp& exp)
    {
      exp.accept(*this);
    }

    virtual void	visit(IntExp& i)
    {
      i.set_immediate(i.get_integer());
    }
67

68
69
70
    virtual void	visit(LabelExp& l)
    {
      if (! _program->data_section().has_label(l.get_label()))
71
72
73
74
75
76
77
78
79
	if (! _program->text_section().has_label(l.get_label()))
	  {
	    std::cerr << "Undefined label: "<< l.get_label()
		      << std::endl;
	    exit_set(exit_solve);
	  }
	else
	  l.set_immediate(_program->text_section().
			  get_offset(l.get_label()) - _pc);
80
81
82
      else
	l.set_immediate(_program->data_section().get_offset(l.get_label()));
    }
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    virtual void	visit(OpExp& o)
    {
      o.get_left().accept(*this);
      o.get_right().accept(*this);
      switch (o.kind)
	{
	case OpExp::add:
	  o.set_immediate(o.get_left().get_immediate() + 
			  o.get_right().get_immediate());
	  break;
	case OpExp::sub:
	  o.set_immediate(o.get_left().get_immediate() - 
			  o.get_right().get_immediate());
	  break;
	case OpExp::mul: 
	  o.set_immediate(o.get_left().get_immediate() * 
			  o.get_right().get_immediate());
	  break;
	case OpExp::div:
	  o.set_immediate(o.get_left().get_immediate() / 
			  o.get_right().get_immediate());
	  break;
	}
    }
108
    
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

  protected:
    virtual void	visit(Add&) {}
    virtual void	visit(Addi& addi)
    {
      resolve_exp(addi.get_imm());
    }
    virtual void	visit(Addu&) {}
    virtual void	visit(Addiu& addiu)
    {
      resolve_exp(addiu.get_imm());
    }
    virtual void	visit(Sub&) {}
    virtual void	visit(Subu&) {}

    virtual void	visit(Sll& sll)
    {
      resolve_exp(sll.get_imm());
    }
128
129
130
131
132
133
134
135
136
137
138
139
    virtual void	visit(Sllv&) {}
    virtual void	visit(Sra& sra)
    {
      resolve_exp(sra.get_imm());
    }
    virtual void	visit(Srav&) {}
    virtual void	visit(Srl& srl)
    {
      resolve_exp(srl.get_imm());
    }
    virtual void	visit(Srlv&) {}

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

    virtual void	visit(Mul&) {}
    virtual void	visit(Div&) {}
    virtual void	visit(Divu&) {}

  protected:
    virtual void	visit(And&) {}
    virtual void	visit(Andi& andi)
    {
      resolve_exp(andi.get_imm());
    }
    virtual void	visit(Or&) {}
    virtual void	visit(Ori& ori)
    {
      resolve_exp(ori.get_imm());
    }
    virtual void	visit(Nor&) {}
    virtual void	visit(Xor&) {}
    virtual void	visit(Xori& xori)
    {
      resolve_exp(xori.get_imm());
    }

  protected:
    virtual void	visit(Slt&) {}
    virtual void	visit(Slti& slti)
    {
      resolve_exp(slti.get_imm());
    }
    virtual void	visit(Sltu&) {}
    virtual void	visit(Sltiu& sltiu)
    {
      resolve_exp(sltiu.get_imm());
    }

  protected:
    virtual void	visit(Beq& beq)
    {
178
      resolve_exp(beq.get_label());
179
180
181
    }
    virtual void	visit(Bne& bne)
    {
182
      resolve_exp(bne.get_label());
183
184
185
186
    }

    virtual void	visit(Bgez& bgez)
    {
187
      resolve_exp(bgez.get_label());
188
189
190
    }
    virtual void	visit(Bgezal& bgezal)
    {
191
      resolve_exp(bgezal.get_label());
192
193
194
    }
    virtual void	visit(Bgtz& bgtz)
    {
195
      resolve_exp(bgtz.get_label());
196
197
198
199
    }

    virtual void	visit(Blez& blez)
    {
200
      resolve_exp(blez.get_label());
201
202
203
    }
    virtual void	visit(Bltz& bltz)
    {
204
      resolve_exp(bltz.get_label());
205
206
207
    }
    virtual void	visit(Bltzal& bltzal)
    {
208
      resolve_exp(bltzal.get_label());
209
210
211
212
    }

    virtual void	visit(Jmp& jmp)
    {
213
      resolve_exp(jmp.get_label());
214
215
216
217
    }
    virtual void	visit(Jr&) {}
    virtual void	visit(Jal& jal)
    {
218
      resolve_exp(jal.get_label());
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    }
    virtual void	visit(Jalr&) {}

  protected:
    virtual void	visit(Lb& lb)
    {
      resolve_exp(lb.get_offset());
    }
    virtual void	visit(Lbu& lbu)
    {
      resolve_exp(lbu.get_offset());
    }
    virtual void	visit(Lw& lw)
    {
      resolve_exp(lw.get_offset());
    }
    virtual void	visit(Li& li)
    {
      resolve_exp(li.get_imm());
    }
    virtual void	visit(Sb& sb)
    {
      resolve_exp(sb.get_offset());
    }
    virtual void	visit(Sw& sw)
    {
      resolve_exp(sw.get_offset());
    }

248
249
250
251
252
253
  protected:
    virtual void	visit(Mfhi&) {}
    virtual void	visit(Mflo&) {}
    virtual void	visit(Mthi&) {}
    virtual void	visit(Mtlo&) {}

254
255
256
  protected:
    virtual void	visit(Syscall&) {}

257
258
259
  protected:
    virtual void	visit(Nop&) {}

260
261
262
263
264
265
266
  protected:
    Program*		_program;
  };

} // namespace inst

#endif // !INST_PROGRAM_SOLVER_HH