exp.hh 3.47 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
//
Benoit Perrot's avatar
Benoit Perrot committed
2
// This file is part of Nolimips, a MIPS simulator with unlimited registers
Benoit Perrot's avatar
Benoit Perrot committed
3
// Copyright (C) 2003, 2004 Benoit Perrot <benoit@lrde.epita.fr>
Benoit Perrot's avatar
Benoit Perrot committed
4
//
Benoit Perrot's avatar
Benoit Perrot committed
5
// Nolimips is free software; you can redistribute it and/or modify
Benoit Perrot's avatar
Benoit Perrot committed
6
7
8
9
// 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.
// 
Benoit Perrot's avatar
Benoit Perrot committed
10
// Nolimips is distributed in the hope that it will be useful,
Benoit Perrot's avatar
Benoit Perrot committed
11
12
13
14
15
16
17
18
// 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
#ifndef INST_EXP_HH
# define INST_EXP_HH

# include <iostream>

# include "misc/contract.hh"
Benoit Perrot's avatar
Benoit Perrot committed
25
# include "misc/counted_ptr.hh"
Benoit Perrot's avatar
Benoit Perrot committed
26
# include "misc/unique_string.hh"
27
28
29
30
31
32
33
34
35
36

# include "inst/exp-visitor.hh"

namespace inst
{

  class Exp
  {
  public:
    Exp():
Benoit Perrot's avatar
Benoit Perrot committed
37
      immediate_(-2097) // Caracteristic initial value
38
39
    {
    }
Benoit Perrot's avatar
Benoit Perrot committed
40
    virtual ~Exp();
41
42
  
  public:
Benoit Perrot's avatar
Benoit Perrot committed
43
    virtual void	print(std::ostream& ostr) const;
44
45
46
47
48
49
50

    virtual void        accept (ConstExpVisitor& v) const = 0;
    virtual void        accept (ExpVisitor& v) = 0;

  public:
    operator int () const
    {
Benoit Perrot's avatar
Benoit Perrot committed
51
      return immediate_;
52
53
54
    }
    int		get_immediate()
    {
Benoit Perrot's avatar
Benoit Perrot committed
55
      return immediate_;
56
57
58
    }
    void	set_immediate(int i)
    {
Benoit Perrot's avatar
Benoit Perrot committed
59
      immediate_ = i;
60
61
    }
  protected:
Benoit Perrot's avatar
Benoit Perrot committed
62
    int		immediate_;
63
64
65
66
67
68
69
70
  };
  inline std::ostream&
  operator<<(std::ostream& ostr, const Exp& e)
  {
    e.print(ostr);
    return ostr;
  }

Benoit Perrot's avatar
Benoit Perrot committed
71
72
  typedef misc::counted_ptr<Exp> CountedExp;

73
74
75
76
77
  class IntExp:
    public Exp
  {
  public: 
    IntExp(int integer):
Benoit Perrot's avatar
Benoit Perrot committed
78
      integer_(integer)
79
80
    {
    }
Benoit Perrot's avatar
Benoit Perrot committed
81
    virtual ~IntExp();
82
83
    
  public:
Benoit Perrot's avatar
Benoit Perrot committed
84
85
    virtual void        accept (ConstExpVisitor& v) const;
    virtual void        accept (ExpVisitor& v);
86
87
88
89

  public:
    int			get_integer() const
    {
Benoit Perrot's avatar
Benoit Perrot committed
90
      return integer_;
91
92
93
    }
    
  public:
Benoit Perrot's avatar
Benoit Perrot committed
94
95
    virtual void	print(std::ostream& ostr) const;

96
  protected:
Benoit Perrot's avatar
Benoit Perrot committed
97
    int		integer_;
98
99
100
101
102
103
  };

  class LabelExp:
    public Exp
  {
  public: 
Benoit Perrot's avatar
Benoit Perrot committed
104
105
106
    LabelExp(const misc::unique_string &name):
      name_(name)
    {}
Benoit Perrot's avatar
Benoit Perrot committed
107
    virtual ~LabelExp();
108
109
    
  public:
Benoit Perrot's avatar
Benoit Perrot committed
110
111
    virtual void        accept (ConstExpVisitor& v) const;
    virtual void        accept (ExpVisitor& v);
112
113

  public:
Benoit Perrot's avatar
Benoit Perrot committed
114
    const misc::unique_string &get_name() const
115
    {
Benoit Perrot's avatar
Benoit Perrot committed
116
      return name_;
117
118
119
    }

  public:
Benoit Perrot's avatar
Benoit Perrot committed
120
121
    virtual void	print(std::ostream& ostr) const;

122
  protected:
Benoit Perrot's avatar
Benoit Perrot committed
123
    const misc::unique_string	&name_;
124
125
126
127
128
129
130
  };


  class OpExp:
    public Exp
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
131
    enum kind_type { add, sub, mul, div };
132
  public: 
Benoit Perrot's avatar
Benoit Perrot committed
133
    OpExp(const CountedExp &left, kind_type kind, const CountedExp &right):
Benoit Perrot's avatar
Benoit Perrot committed
134
      kind_(kind), left_(left), right_(right)
135
    {
Benoit Perrot's avatar
Benoit Perrot committed
136
      // assertion(left && right);
137
    }
Benoit Perrot's avatar
Benoit Perrot committed
138
    virtual ~OpExp();
139
140

  public:
Benoit Perrot's avatar
Benoit Perrot committed
141
142
    virtual void        accept (ConstExpVisitor& v) const;
    virtual void        accept (ExpVisitor& v);
143
144
145
146
    
  public:
    const Exp& get_left() const
    {
Benoit Perrot's avatar
Benoit Perrot committed
147
      return *left_;
148
149
150
    }
    Exp& get_left()
    {
Benoit Perrot's avatar
Benoit Perrot committed
151
      return *left_;
152
153
154
155
    }

    const Exp& get_right() const
    {
Benoit Perrot's avatar
Benoit Perrot committed
156
      return *right_;
157
158
159
    }
    Exp& get_right()
    {
Benoit Perrot's avatar
Benoit Perrot committed
160
      return *right_;
161
162
163
    }

  public:
Benoit Perrot's avatar
Benoit Perrot committed
164
    virtual void	print(std::ostream& ostr) const;
165
166
    
  public:
Benoit Perrot's avatar
Benoit Perrot committed
167
    const kind_type	kind_;
168
  protected:
Benoit Perrot's avatar
Benoit Perrot committed
169
170
    CountedExp	left_;
    CountedExp	right_;
171
172
173
174
175
  };

} // namespace inst

#endif // !INST_EXP_HH