inst-solver-gen.py 5.48 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
##
## This file is part of Mipsy, a tiny MIPS simulator
## Copyright (C) 2003, 2004 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
##

import sys, os
import string, re

import mipsy
from mipsy import license, depth, class_id

#### Mipsy's program solver generator ----------------------

parser = mipsy.InstructionSetParser()
instructions = parser.parse(sys.stdin)


## Generate interface
sys.stdout = file("program_solver.hh.tmp", "w")

print license
print """#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\"

// FIXME: dirty access unlimited registers flag
# include \"parse/libparse.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	solve_exp(Exp &exp)
    {
      exp.accept(*this);
    }

    virtual void	visit(IntExp &i);
    virtual void	visit(LabelExp &l);
    virtual void	visit(OpExp &o);
Benoit Perrot's avatar
Benoit Perrot committed
86
87
88
89

  protected:
    void	solve_cpu_register(Register &reg) const;
    void	solve_cop_register(Register &reg) const;
Benoit Perrot's avatar
Benoit Perrot committed
90
91
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
133
134
135
136
137
138
139
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
"""

print "  protected:"
for inst in instructions:
  if inst.level == "native":
    print "    virtual void	visit(" + mipsy.class_id(inst.opcode) + " &);"
    
print """
  protected:
    Program	*program_;
  };

} // namespace inst

#endif // !INST_PROGRAM_SOLVER_HH
"""

sys.stdout.close()
sys.stdout = sys.__stdout__
mipsy.lazy_overwrite("program_solver.hh", "program_solver.hh.tmp")
os.remove("program_solver.hh.tmp")


## Generate implementation
sys.stdout = file("program_solver.cc.tmp", "w")

print license
print """#include \"inst/program_solver.hh\"

namespace inst
{
    void
    ProgramSolver::visit(IntExp &i)
    {
      i.set_immediate(i.get_integer());
    }

    void
    ProgramSolver::visit(LabelExp &l)
    {
      if (! program_->data_section().has_label(l.get_label()))
	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_);
      else
	l.set_immediate(program_->data_section().get_offset(l.get_label()));
    }

    void
    ProgramSolver::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;
	}
    }
Benoit Perrot's avatar
Benoit Perrot committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200


    void
    ProgramSolver::solve_cpu_register(Register &reg) const
    {
      switch (reg.get_kind())
	{
	case Register::generic:
	  reg.set_kind(Register::general);
	  break;
	case Register::general:
	  break;
	case Register::unlimited:
	  if (!parse::unlimited_regs_p)
	    {
	      std::cerr << \"Unlimited registers prohibited.\" << std::endl;
	      exit_set(exit_solve);
	    }
	  break;
	};
    }
    
    void
    ProgramSolver::solve_cop_register(Register &reg) const
    {
      if (reg.get_kind() != Register::generic)
	{
	  std::cerr << \"Coprocessor registers must be generic.\" << std::endl;
	  exit_set(exit_solve);
	}
    }

Benoit Perrot's avatar
Benoit Perrot committed
201
202
203
204
205
206
207
208
"""

for inst in instructions:
  if inst.level == "native":
    var_id = mipsy.var_id(inst.opcode)
    
    content = ""
    for attribute in inst.format.attributes:
Benoit Perrot's avatar
Benoit Perrot committed
209
      if attribute.type == "Exp":
Benoit Perrot's avatar
Benoit Perrot committed
210
211
        content = content + "    solve_exp(" + \
                  var_id + ".get_" + attribute.name + "());\n"
Benoit Perrot's avatar
Benoit Perrot committed
212
213
214
      elif attribute.type == "Register":
        content = content + "    solve_cpu_register(" + \
                  var_id + ".get_" + attribute.name + "());"
Benoit Perrot's avatar
Benoit Perrot committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    
    print "  void"
    if content != "":
      print "  ProgramSolver::visit(" + mipsy.class_id(inst.opcode) + \
            " &" + var_id + ")"
      print "  {\n" + content + "  }"
    else:
      print "  ProgramSolver::visit(" + mipsy.class_id(inst.opcode) + " &)"
      print "  {"
      print "  }"
  
print """
} // namespace inst"""

sys.stdout.close()
sys.stdout = sys.__stdout__
mipsy.lazy_overwrite("program_solver.cc", "program_solver.cc.tmp")
os.remove("program_solver.cc.tmp")